xref: /linux/tools/perf/util/unwind-libdw.c (revision 657a3efee43a29d13c4f30e4c8f6a178fd2bf14a)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <elfutils/libdw.h>
4 #include <elfutils/libdwfl.h>
5 #include <inttypes.h>
6 #include <errno.h>
7 #include "debug.h"
8 #include "dso.h"
9 #include "unwind.h"
10 #include "unwind-libdw.h"
11 #include "machine.h"
12 #include "map.h"
13 #include "symbol.h"
14 #include "thread.h"
15 #include <linux/types.h>
16 #include <linux/zalloc.h>
17 #include "event.h"
18 #include "perf_regs.h"
19 #include "callchain.h"
20 
21 static char *debuginfo_path;
22 
23 static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,
24 			    const char *modname __maybe_unused, Dwarf_Addr base __maybe_unused,
25 			    const char *file_name, const char *debuglink_file __maybe_unused,
26 			    GElf_Word debuglink_crc __maybe_unused, char **debuginfo_file_name)
27 {
28 	const struct dso *dso = *userdata;
29 
30 	assert(dso);
31 	if (dso->symsrc_filename && strcmp (file_name, dso->symsrc_filename))
32 		*debuginfo_file_name = strdup(dso->symsrc_filename);
33 	return -1;
34 }
35 
36 static const Dwfl_Callbacks offline_callbacks = {
37 	.find_debuginfo		= __find_debuginfo,
38 	.debuginfo_path		= &debuginfo_path,
39 	.section_address	= dwfl_offline_section_address,
40 	// .find_elf is not set as we use dwfl_report_elf() instead.
41 };
42 
43 static int __report_module(struct addr_location *al, u64 ip,
44 			    struct unwind_info *ui)
45 {
46 	Dwfl_Module *mod;
47 	struct dso *dso = NULL;
48 	/*
49 	 * Some callers will use al->sym, so we can't just use the
50 	 * cheaper thread__find_map() here.
51 	 */
52 	thread__find_symbol(ui->thread, PERF_RECORD_MISC_USER, ip, al);
53 
54 	if (al->map)
55 		dso = map__dso(al->map);
56 
57 	if (!dso)
58 		return 0;
59 
60 	mod = dwfl_addrmodule(ui->dwfl, ip);
61 	if (mod) {
62 		Dwarf_Addr s;
63 
64 		dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
65 		if (s != map__start(al->map) - map__pgoff(al->map))
66 			mod = 0;
67 	}
68 
69 	if (!mod)
70 		mod = dwfl_report_elf(ui->dwfl, dso->short_name, dso->long_name, -1,
71 				      map__start(al->map) - map__pgoff(al->map), false);
72 	if (!mod) {
73 		char filename[PATH_MAX];
74 
75 		if (dso__build_id_filename(dso, filename, sizeof(filename), false))
76 			mod = dwfl_report_elf(ui->dwfl, dso->short_name, filename, -1,
77 					      map__start(al->map) - map__pgoff(al->map), false);
78 	}
79 
80 	if (mod) {
81 		void **userdatap;
82 
83 		dwfl_module_info(mod, &userdatap, NULL, NULL, NULL, NULL, NULL, NULL);
84 		*userdatap = dso;
85 	}
86 
87 	return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
88 }
89 
90 static int report_module(u64 ip, struct unwind_info *ui)
91 {
92 	struct addr_location al;
93 	int res;
94 
95 	addr_location__init(&al);
96 	res = __report_module(&al, ip, ui);
97 	addr_location__exit(&al);
98 	return res;
99 }
100 
101 /*
102  * Store all entries within entries array,
103  * we will process it after we finish unwind.
104  */
105 static int entry(u64 ip, struct unwind_info *ui)
106 
107 {
108 	struct unwind_entry *e = &ui->entries[ui->idx++];
109 	struct addr_location al;
110 
111 	addr_location__init(&al);
112 	if (__report_module(&al, ip, ui)) {
113 		addr_location__exit(&al);
114 		return -1;
115 	}
116 
117 	e->ip	  = ip;
118 	e->ms.maps = al.maps;
119 	e->ms.map = al.map;
120 	e->ms.sym = al.sym;
121 
122 	pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
123 		 al.sym ? al.sym->name : "''",
124 		 ip,
125 		 al.map ? map__map_ip(al.map, ip) : (u64) 0);
126 	addr_location__exit(&al);
127 	return 0;
128 }
129 
130 static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
131 {
132 	/* We want only single thread to be processed. */
133 	if (*thread_argp != NULL)
134 		return 0;
135 
136 	*thread_argp = arg;
137 	return dwfl_pid(dwfl);
138 }
139 
140 static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
141 			  Dwarf_Word *data)
142 {
143 	struct addr_location al;
144 	ssize_t size;
145 	struct dso *dso;
146 
147 	addr_location__init(&al);
148 	if (!thread__find_map(ui->thread, PERF_RECORD_MISC_USER, addr, &al)) {
149 		pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
150 		goto out_fail;
151 	}
152 	dso = map__dso(al.map);
153 	if (!dso)
154 		goto out_fail;
155 
156 	size = dso__data_read_addr(dso, al.map, ui->machine, addr, (u8 *) data, sizeof(*data));
157 
158 	addr_location__exit(&al);
159 	return !(size == sizeof(*data));
160 out_fail:
161 	addr_location__exit(&al);
162 	return -1;
163 }
164 
165 static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
166 			void *arg)
167 {
168 	struct unwind_info *ui = arg;
169 	struct stack_dump *stack = &ui->sample->user_stack;
170 	u64 start, end;
171 	int offset;
172 	int ret;
173 
174 	ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
175 	if (ret)
176 		return false;
177 
178 	end = start + stack->size;
179 
180 	/* Check overflow. */
181 	if (addr + sizeof(Dwarf_Word) < addr)
182 		return false;
183 
184 	if (addr < start || addr + sizeof(Dwarf_Word) > end) {
185 		ret = access_dso_mem(ui, addr, result);
186 		if (ret) {
187 			pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
188 				 " 0x%" PRIx64 "-0x%" PRIx64 "\n",
189 				addr, start, end);
190 			return false;
191 		}
192 		return true;
193 	}
194 
195 	offset  = addr - start;
196 	*result = *(Dwarf_Word *)&stack->data[offset];
197 	pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
198 		 addr, (unsigned long)*result, offset);
199 	return true;
200 }
201 
202 static const Dwfl_Thread_Callbacks callbacks = {
203 	.next_thread		= next_thread,
204 	.memory_read		= memory_read,
205 	.set_initial_registers	= libdw__arch_set_initial_registers,
206 };
207 
208 static int
209 frame_callback(Dwfl_Frame *state, void *arg)
210 {
211 	struct unwind_info *ui = arg;
212 	Dwarf_Addr pc;
213 	bool isactivation;
214 
215 	if (!dwfl_frame_pc(state, &pc, NULL)) {
216 		if (!ui->best_effort)
217 			pr_err("%s", dwfl_errmsg(-1));
218 		return DWARF_CB_ABORT;
219 	}
220 
221 	// report the module before we query for isactivation
222 	report_module(pc, ui);
223 
224 	if (!dwfl_frame_pc(state, &pc, &isactivation)) {
225 		if (!ui->best_effort)
226 			pr_err("%s", dwfl_errmsg(-1));
227 		return DWARF_CB_ABORT;
228 	}
229 
230 	if (!isactivation)
231 		--pc;
232 
233 	return entry(pc, ui) || !(--ui->max_stack) ?
234 	       DWARF_CB_ABORT : DWARF_CB_OK;
235 }
236 
237 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
238 			struct thread *thread,
239 			struct perf_sample *data,
240 			int max_stack,
241 			bool best_effort)
242 {
243 	struct unwind_info *ui, ui_buf = {
244 		.sample		= data,
245 		.thread		= thread,
246 		.machine	= RC_CHK_ACCESS(thread__maps(thread))->machine,
247 		.cb		= cb,
248 		.arg		= arg,
249 		.max_stack	= max_stack,
250 		.best_effort    = best_effort
251 	};
252 	Dwarf_Word ip;
253 	int err = -EINVAL, i;
254 
255 	if (!data->user_regs.regs)
256 		return -EINVAL;
257 
258 	ui = zalloc(sizeof(ui_buf) + sizeof(ui_buf.entries[0]) * max_stack);
259 	if (!ui)
260 		return -ENOMEM;
261 
262 	*ui = ui_buf;
263 
264 	ui->dwfl = dwfl_begin(&offline_callbacks);
265 	if (!ui->dwfl)
266 		goto out;
267 
268 	err = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
269 	if (err)
270 		goto out;
271 
272 	err = report_module(ip, ui);
273 	if (err)
274 		goto out;
275 
276 	err = !dwfl_attach_state(ui->dwfl, EM_NONE, thread__tid(thread), &callbacks, ui);
277 	if (err)
278 		goto out;
279 
280 	err = dwfl_getthread_frames(ui->dwfl, thread__tid(thread), frame_callback, ui);
281 
282 	if (err && ui->max_stack != max_stack)
283 		err = 0;
284 
285 	/*
286 	 * Display what we got based on the order setup.
287 	 */
288 	for (i = 0; i < ui->idx && !err; i++) {
289 		int j = i;
290 
291 		if (callchain_param.order == ORDER_CALLER)
292 			j = ui->idx - i - 1;
293 
294 		err = ui->entries[j].ip ? ui->cb(&ui->entries[j], ui->arg) : 0;
295 	}
296 
297  out:
298 	if (err)
299 		pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
300 
301 	dwfl_end(ui->dwfl);
302 	free(ui);
303 	return 0;
304 }
305