xref: /linux/tools/perf/util/unwind-libdw.c (revision c7decec2f2d2ab0366567f9e30c0e1418cece43f)
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 <dwarf-regs.h>
10 #include "unwind.h"
11 #include "unwind-libdw.h"
12 #include "machine.h"
13 #include "map.h"
14 #include "symbol.h"
15 #include "thread.h"
16 #include <linux/types.h>
17 #include <linux/zalloc.h>
18 #include "event.h"
19 #include "perf_regs.h"
20 #include "callchain.h"
21 #include "util/env.h"
22 
23 /*
24  * The dwfl thread argument passed to functions like memory_read. Memory has to
25  * be allocated to persist of multiple uses of the dwfl.
26  */
27 struct dwfl_ui_thread_info {
28 	/* Back link to the dwfl. */
29 	Dwfl *dwfl;
30 	/* The current unwind info, only 1 is supported. */
31 	struct unwind_info *ui;
32 };
33 
34 static char *debuginfo_path;
35 
__find_debuginfo(Dwfl_Module * mod __maybe_unused,void ** userdata,const char * modname __maybe_unused,Dwarf_Addr base __maybe_unused,const char * file_name,const char * debuglink_file __maybe_unused,GElf_Word debuglink_crc __maybe_unused,char ** debuginfo_file_name)36 static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,
37 			    const char *modname __maybe_unused, Dwarf_Addr base __maybe_unused,
38 			    const char *file_name, const char *debuglink_file __maybe_unused,
39 			    GElf_Word debuglink_crc __maybe_unused, char **debuginfo_file_name)
40 {
41 	const struct dso *dso = *userdata;
42 
43 	assert(dso);
44 	if (dso__symsrc_filename(dso) && strcmp(file_name, dso__symsrc_filename(dso)))
45 		*debuginfo_file_name = strdup(dso__symsrc_filename(dso));
46 	return -1;
47 }
48 
libdw__invalidate_dwfl(struct maps * maps,void * arg)49 void libdw__invalidate_dwfl(struct maps *maps, void *arg)
50 {
51 	struct dwfl_ui_thread_info *dwfl_ui_ti = arg;
52 
53 	if (!dwfl_ui_ti)
54 		return;
55 
56 	assert(dwfl_ui_ti->ui == NULL);
57 	maps__set_libdw_addr_space_dwfl(maps, NULL);
58 	dwfl_end(dwfl_ui_ti->dwfl);
59 	free(dwfl_ui_ti);
60 }
61 
62 static const Dwfl_Callbacks offline_callbacks = {
63 	.find_debuginfo		= __find_debuginfo,
64 	.debuginfo_path		= &debuginfo_path,
65 	.section_address	= dwfl_offline_section_address,
66 	// .find_elf is not set as we use dwfl_report_elf() instead.
67 };
68 
__report_module(struct addr_location * al,u64 ip,struct unwind_info * ui)69 static int __report_module(struct addr_location *al, u64 ip,
70 			    struct unwind_info *ui)
71 {
72 	Dwfl_Module *mod;
73 	struct dso *dso = NULL;
74 	Dwarf_Addr base;
75 	/*
76 	 * Some callers will use al->sym, so we can't just use the
77 	 * cheaper thread__find_map() here.
78 	 */
79 	thread__find_symbol(ui->thread, PERF_RECORD_MISC_USER, ip, al);
80 
81 	if (al->map)
82 		dso = map__dso(al->map);
83 
84 	if (!dso)
85 		return 0;
86 
87 	/*
88 	 * The generated JIT DSO files only map the code segment without
89 	 * ELF headers.  Since JIT codes used to be packed in a memory
90 	 * segment, calculating the base address using pgoff falls into
91 	 * a different code in another DSO.  So just use the map->start
92 	 * directly to pick the correct one.
93 	 */
94 	if (!strncmp(dso__long_name(dso), "/tmp/jitted-", 12))
95 		base = map__start(al->map);
96 	else
97 		base = map__start(al->map) - map__pgoff(al->map);
98 
99 	mod = dwfl_addrmodule(ui->dwfl, ip);
100 	if (mod) {
101 		Dwarf_Addr s;
102 
103 		dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
104 		if (s != base)
105 			mod = NULL;
106 	}
107 
108 	if (!mod) {
109 		char filename[PATH_MAX];
110 
111 		__symbol__join_symfs(filename, sizeof(filename), dso__long_name(dso));
112 		/* Don't hang up on device files like /dev/dri/renderD128. */
113 		if (is_regular_file(filename)) {
114 			mod = dwfl_report_elf(ui->dwfl, dso__short_name(dso), filename, -1,
115 					      base, false);
116 		}
117 	}
118 	if (!mod) {
119 		char filename[PATH_MAX];
120 
121 		if (dso__build_id_filename(dso, filename, sizeof(filename), false))
122 			mod = dwfl_report_elf(ui->dwfl, dso__short_name(dso), filename, -1,
123 					      base, false);
124 	}
125 
126 	if (mod) {
127 		void **userdatap;
128 
129 		dwfl_module_info(mod, &userdatap, NULL, NULL, NULL, NULL, NULL, NULL);
130 		*userdatap = dso;
131 	}
132 
133 	return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
134 }
135 
report_module(u64 ip,struct unwind_info * ui)136 static int report_module(u64 ip, struct unwind_info *ui)
137 {
138 	struct addr_location al;
139 	int res;
140 
141 	addr_location__init(&al);
142 	res = __report_module(&al, ip, ui);
143 	addr_location__exit(&al);
144 	return res;
145 }
146 
147 /*
148  * Store all entries within entries array,
149  * we will process it after we finish unwind.
150  */
entry(u64 ip,struct unwind_info * ui)151 static int entry(u64 ip, struct unwind_info *ui)
152 
153 {
154 	struct unwind_entry *e = &ui->entries[ui->idx++];
155 	struct addr_location al;
156 
157 	addr_location__init(&al);
158 	if (__report_module(&al, ip, ui)) {
159 		addr_location__exit(&al);
160 		return -1;
161 	}
162 
163 	e->ip	  = ip;
164 	e->ms.thread = thread__get(al.thread);
165 	e->ms.map = map__get(al.map);
166 	e->ms.sym = al.sym;
167 
168 	pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
169 		 al.sym ? al.sym->name : "''",
170 		 ip,
171 		 al.map ? map__map_ip(al.map, ip) : (u64) 0);
172 	addr_location__exit(&al);
173 	return 0;
174 }
175 
next_thread(Dwfl * dwfl,void * arg,void ** thread_argp)176 static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
177 {
178 	/* We want only single thread to be processed. */
179 	if (*thread_argp != NULL)
180 		return 0;
181 
182 	*thread_argp = arg;
183 	return dwfl_pid(dwfl);
184 }
185 
access_dso_mem(struct unwind_info * ui,Dwarf_Addr addr,Dwarf_Word * data)186 static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
187 			  Dwarf_Word *data)
188 {
189 	struct addr_location al;
190 	ssize_t size;
191 	struct dso *dso;
192 
193 	addr_location__init(&al);
194 	if (!thread__find_map(ui->thread, PERF_RECORD_MISC_USER, addr, &al)) {
195 		pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
196 		goto out_fail;
197 	}
198 	dso = map__dso(al.map);
199 	if (!dso)
200 		goto out_fail;
201 
202 	size = dso__data_read_addr(dso, al.map, ui->machine, addr, (u8 *) data, sizeof(*data));
203 
204 	addr_location__exit(&al);
205 	return !(size == sizeof(*data));
206 out_fail:
207 	addr_location__exit(&al);
208 	return -1;
209 }
210 
memory_read(Dwfl * dwfl __maybe_unused,Dwarf_Addr addr,Dwarf_Word * result,void * arg)211 static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
212 			void *arg)
213 {
214 	struct dwfl_ui_thread_info *dwfl_ui_ti = arg;
215 	struct unwind_info *ui = dwfl_ui_ti->ui;
216 	struct stack_dump *stack = &ui->sample->user_stack;
217 	u64 start, end;
218 	int offset;
219 	int ret;
220 
221 	if (!ui->sample->user_regs)
222 		return false;
223 
224 	ret = perf_reg_value(&start, ui->sample->user_regs,
225 			     perf_arch_reg_sp(ui->e_machine));
226 	if (ret)
227 		return false;
228 
229 	end = start + stack->size;
230 
231 	/* Check overflow. */
232 	if (addr + sizeof(Dwarf_Word) < addr)
233 		return false;
234 
235 	if (addr < start || addr + sizeof(Dwarf_Word) > end) {
236 		ret = access_dso_mem(ui, addr, result);
237 		if (ret) {
238 			pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
239 				 " 0x%" PRIx64 "-0x%" PRIx64 "\n",
240 				addr, start, end);
241 			return false;
242 		}
243 		return true;
244 	}
245 
246 	offset  = addr - start;
247 	*result = *(Dwarf_Word *)&stack->data[offset];
248 	pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
249 		 addr, (unsigned long)*result, offset);
250 	return true;
251 }
252 
libdw_set_initial_registers(Dwfl_Thread * thread,void * arg)253 static bool libdw_set_initial_registers(Dwfl_Thread *thread, void *arg)
254 {
255 	struct dwfl_ui_thread_info *dwfl_ui_ti = arg;
256 	struct unwind_info *ui = dwfl_ui_ti->ui;
257 	struct regs_dump *user_regs = perf_sample__user_regs(ui->sample);
258 	Dwarf_Word *dwarf_regs;
259 	int max_dwarf_reg = 0;
260 	bool ret;
261 	uint16_t e_machine = ui->e_machine;
262 	int e_flags = ui->e_flags;
263 	uint64_t ip_perf_reg = perf_arch_reg_ip(e_machine);
264 	Dwarf_Word val = 0;
265 
266 
267 	/*
268 	 * For every possible perf register in the bitmap determine the dwarf
269 	 * register and use to compute the max.
270 	 */
271 	for (int perf_reg = 0; perf_reg < 64; perf_reg++) {
272 		if (user_regs->mask & (1ULL << perf_reg)) {
273 			int dwarf_reg =
274 				get_dwarf_regnum_for_perf_regnum(perf_reg, e_machine,
275 								 e_flags,
276 								 /*only_libdw_supported=*/true);
277 			if (dwarf_reg > max_dwarf_reg)
278 				max_dwarf_reg = dwarf_reg;
279 		}
280 	}
281 
282 	dwarf_regs = calloc(max_dwarf_reg + 1, sizeof(*dwarf_regs));
283 	if (!dwarf_regs)
284 		return false;
285 
286 	for (int perf_reg = 0; perf_reg < 64; perf_reg++) {
287 		if (user_regs->mask & (1ULL << perf_reg)) {
288 			int dwarf_reg =
289 				get_dwarf_regnum_for_perf_regnum(perf_reg, e_machine,
290 								 e_flags,
291 								 /*only_libdw_supported=*/true);
292 			if (dwarf_reg >= 0) {
293 				val = 0;
294 				if (perf_reg_value(&val, user_regs, perf_reg) == 0)
295 					dwarf_regs[dwarf_reg] = val;
296 			}
297 		}
298 	}
299 	if (perf_reg_value(&val, user_regs, ip_perf_reg) == 0)
300 		dwfl_thread_state_register_pc(thread, val);
301 
302 	ret = dwfl_thread_state_registers(thread, 0, max_dwarf_reg + 1, dwarf_regs);
303 	free(dwarf_regs);
304 	return ret;
305 }
306 
307 static const Dwfl_Thread_Callbacks callbacks = {
308 	.next_thread           = next_thread,
309 	.memory_read           = memory_read,
310 	.set_initial_registers = libdw_set_initial_registers,
311 };
312 
313 static int
frame_callback(Dwfl_Frame * state,void * arg)314 frame_callback(Dwfl_Frame *state, void *arg)
315 {
316 	struct unwind_info *ui = arg;
317 	Dwarf_Addr pc;
318 	bool isactivation;
319 
320 	if (!dwfl_frame_pc(state, &pc, NULL)) {
321 		if (!ui->best_effort)
322 			pr_err("%s", dwfl_errmsg(-1));
323 		return DWARF_CB_ABORT;
324 	}
325 
326 	// report the module before we query for isactivation
327 	report_module(pc, ui);
328 
329 	if (!dwfl_frame_pc(state, &pc, &isactivation)) {
330 		if (!ui->best_effort)
331 			pr_err("%s", dwfl_errmsg(-1));
332 		return DWARF_CB_ABORT;
333 	}
334 
335 	if (!isactivation)
336 		--pc;
337 
338 	return entry(pc, ui) || !(--ui->max_stack) ?
339 	       DWARF_CB_ABORT : DWARF_CB_OK;
340 }
341 
unwind__get_entries(unwind_entry_cb_t cb,void * arg,struct thread * thread,struct perf_sample * data,int max_stack,bool best_effort)342 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
343 			struct thread *thread,
344 			struct perf_sample *data,
345 			int max_stack,
346 			bool best_effort)
347 {
348 	struct maps *maps = thread__maps(thread);
349 	struct machine *machine = maps__machine(maps);
350 	uint32_t e_flags = 0;
351 	uint16_t e_machine = thread__e_machine(thread, machine, &e_flags);
352 	struct dwfl_ui_thread_info *dwfl_ui_ti;
353 	static struct unwind_info *ui;
354 	Dwfl *dwfl;
355 	Dwarf_Word ip;
356 	int err = -EINVAL, i;
357 
358 	if (!data->user_regs || !data->user_regs->regs)
359 		return -EINVAL;
360 
361 	ui = zalloc(sizeof(*ui) + sizeof(ui->entries[0]) * max_stack);
362 	if (!ui)
363 		return -ENOMEM;
364 
365 	*ui = (struct unwind_info){
366 		.sample		= data,
367 		.thread		= thread,
368 		.machine	= machine,
369 		.cb		= cb,
370 		.arg		= arg,
371 		.max_stack	= max_stack,
372 		.e_machine	= e_machine,
373 		.e_flags	= e_flags,
374 		.best_effort    = best_effort
375 	};
376 
377 	dwfl_ui_ti = maps__libdw_addr_space_dwfl(maps);
378 	if (dwfl_ui_ti) {
379 		dwfl = dwfl_ui_ti->dwfl;
380 	} else {
381 		dwfl_ui_ti = zalloc(sizeof(*dwfl_ui_ti));
382 		dwfl = dwfl_begin(&offline_callbacks);
383 		if (!dwfl)
384 			goto out;
385 
386 		dwfl_ui_ti->dwfl = dwfl;
387 		maps__set_libdw_addr_space_dwfl(maps, dwfl_ui_ti);
388 	}
389 	assert(dwfl_ui_ti->ui == NULL);
390 	assert(dwfl_ui_ti->dwfl == dwfl);
391 	assert(dwfl_ui_ti == maps__libdw_addr_space_dwfl(maps));
392 	dwfl_ui_ti->ui = ui;
393 	ui->dwfl = dwfl;
394 
395 	err = perf_reg_value(&ip, data->user_regs, perf_arch_reg_ip(e_machine));
396 	if (err)
397 		goto out;
398 
399 	err = report_module(ip, ui);
400 	if (err)
401 		goto out;
402 
403 	dwfl_attach_state(dwfl, /*elf=*/NULL, thread__tid(thread), &callbacks,
404 			  /* Dwfl thread function argument*/dwfl_ui_ti);
405 	// Ignore thread already attached error.
406 
407 	err = dwfl_getthread_frames(dwfl, thread__tid(thread), frame_callback,
408 				    /* Dwfl frame function argument*/ui);
409 
410 	if (err && ui->max_stack != max_stack)
411 		err = 0;
412 
413 	/*
414 	 * Display what we got based on the order setup.
415 	 */
416 	for (i = 0; i < ui->idx && !err; i++) {
417 		int j = i;
418 
419 		if (callchain_param.order == ORDER_CALLER)
420 			j = ui->idx - i - 1;
421 
422 		err = ui->entries[j].ip ? ui->cb(&ui->entries[j], ui->arg) : 0;
423 	}
424 
425  out:
426 	if (err)
427 		pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
428 
429 	for (i = 0; i < ui->idx; i++)
430 		map_symbol__exit(&ui->entries[i].ms);
431 
432 	dwfl_ui_ti->ui = NULL;
433 	free(ui);
434 	return 0;
435 }
436