xref: /linux/tools/perf/util/srcline.c (revision 62d7cdc8d58702feb31b320f1713642e3977d7f3)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "srcline.h"
3 
4 #include <inttypes.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 
9 #include <linux/string.h>
10 #include <linux/zalloc.h>
11 
12 #include "addr2line.h"
13 #include "callchain.h"
14 #include "debug.h"
15 #include "dso.h"
16 #include "libbfd.h"
17 #include "libdw.h"
18 #include "llvm.h"
19 #include "symbol.h"
20 #include "util.h"
21 
22 bool srcline_full_filename;
23 
24 char *srcline__unknown = (char *)"??:0";
25 
26 static char *srcline_dso_name(struct dso *dso)
27 {
28 	const char *dso_name;
29 	char *ret = NULL;
30 
31 	mutex_lock(dso__lock(dso));
32 	if (dso__symsrc_filename(dso))
33 		dso_name = dso__symsrc_filename(dso);
34 	else
35 		dso_name = dso__long_name(dso);
36 
37 	if (dso_name[0] == '[')
38 		goto out;
39 
40 	if (is_perf_pid_map_name(dso_name))
41 		goto out;
42 
43 	ret = strdup(dso_name);
44 out:
45 	mutex_unlock(dso__lock(dso));
46 	return ret;
47 }
48 
49 int inline_list__append(struct symbol *symbol, char *srcline, struct inline_node *node)
50 {
51 	struct inline_list *ilist;
52 
53 	ilist = zalloc(sizeof(*ilist));
54 	if (ilist == NULL)
55 		return -1;
56 
57 	ilist->symbol = symbol;
58 	ilist->srcline = srcline;
59 
60 	if (callchain_param.order == ORDER_CALLEE)
61 		list_add_tail(&ilist->list, &node->val);
62 	else
63 		list_add(&ilist->list, &node->val);
64 
65 	return 0;
66 }
67 
68 int inline_list__append_tail(struct symbol *symbol, char *srcline, struct inline_node *node)
69 {
70 	struct inline_list *ilist;
71 
72 	ilist = zalloc(sizeof(*ilist));
73 	if (ilist == NULL)
74 		return -1;
75 
76 	ilist->symbol = symbol;
77 	ilist->srcline = srcline;
78 
79 	if (callchain_param.order == ORDER_CALLEE)
80 		list_add(&ilist->list, &node->val);
81 	else
82 		list_add_tail(&ilist->list, &node->val);
83 
84 	return 0;
85 }
86 
87 char *srcline_from_fileline(const char *file, unsigned int line)
88 {
89 	char *srcline;
90 
91 	if (!file)
92 		return NULL;
93 
94 	if (!srcline_full_filename)
95 		file = perf_basename(file);
96 
97 	if (asprintf(&srcline, "%s:%u", file, line) < 0)
98 		return NULL;
99 
100 	return srcline;
101 }
102 
103 struct symbol *new_inline_sym(struct dso *dso,
104 			      struct symbol *base_sym,
105 			      const char *funcname)
106 {
107 	struct symbol *inline_sym;
108 	char *demangled = NULL;
109 
110 	if (!funcname)
111 		funcname = "??";
112 
113 	if (dso) {
114 		demangled = dso__demangle_sym(dso, 0, funcname);
115 		if (demangled)
116 			funcname = demangled;
117 	}
118 
119 	if (base_sym && strcmp(funcname, base_sym->name) == 0) {
120 		/* reuse the real, existing symbol */
121 		inline_sym = base_sym;
122 		/* ensure that we don't alias an inlined symbol, which could
123 		 * lead to double frees in inline_node__delete
124 		 */
125 		assert(!symbol__inlined(base_sym));
126 	} else {
127 		/* create a fake symbol for the inline frame */
128 		inline_sym = symbol__new(base_sym ? base_sym->start : 0,
129 					 base_sym ? (base_sym->end - base_sym->start) : 0,
130 					 base_sym ? symbol__binding(base_sym) : 0,
131 					 base_sym ? symbol__type(base_sym) : 0,
132 					 funcname);
133 		if (inline_sym)
134 			symbol__set_inlined(inline_sym, true);
135 	}
136 
137 	free(demangled);
138 
139 	return inline_sym;
140 }
141 
142 static int addr2line(const char *dso_name, u64 addr, char **file, unsigned int *line_nr,
143 		     struct dso *dso, bool unwind_inlines, struct inline_node *node,
144 		     struct symbol *sym)
145 {
146 	int ret = 0;
147 
148 	if (symbol_conf.addr2line_style[0] == A2L_STYLE_UNKNOWN) {
149 		int i = 0;
150 
151 		/* Default addr2line fallback order. */
152 #ifdef HAVE_LIBDW_SUPPORT
153 		symbol_conf.addr2line_style[i++] = A2L_STYLE_LIBDW;
154 #endif
155 #ifdef HAVE_LIBLLVM_SUPPORT
156 		symbol_conf.addr2line_style[i++] = A2L_STYLE_LLVM;
157 #endif
158 #ifdef HAVE_LIBBFD_SUPPORT
159 		symbol_conf.addr2line_style[i++] = A2L_STYLE_LIBBFD;
160 #endif
161 		symbol_conf.addr2line_style[i++] = A2L_STYLE_CMD;
162 	}
163 
164 	for (size_t i = 0; i < ARRAY_SIZE(symbol_conf.addr2line_style); i++) {
165 		switch (symbol_conf.addr2line_style[i]) {
166 		case A2L_STYLE_LIBDW:
167 			ret = libdw__addr2line(addr, file, line_nr, dso, unwind_inlines,
168 					       node, sym);
169 			break;
170 		case A2L_STYLE_LLVM:
171 			ret = llvm__addr2line(dso_name, addr, file, line_nr, dso, unwind_inlines,
172 					      node, sym);
173 			break;
174 		case A2L_STYLE_LIBBFD:
175 			ret = libbfd__addr2line(dso_name, addr, file, line_nr, dso, unwind_inlines,
176 						node, sym);
177 			break;
178 		case A2L_STYLE_CMD:
179 			ret = cmd__addr2line(dso_name, addr, file, line_nr, dso, unwind_inlines,
180 					     node, sym);
181 			break;
182 		case A2L_STYLE_UNKNOWN:
183 		default:
184 			return 0;
185 		}
186 		if (ret >= 0)
187 			return ret;
188 	}
189 
190 	return 0;
191 }
192 
193 int addr2line_configure(const char *var, const char *value, void *cb __maybe_unused)
194 {
195 	static const char * const a2l_style_names[] = {
196 		[A2L_STYLE_LIBDW] = "libdw",
197 		[A2L_STYLE_LLVM] = "llvm",
198 		[A2L_STYLE_LIBBFD] = "libbfd",
199 		[A2L_STYLE_CMD] = "addr2line",
200 		NULL
201 	};
202 
203 	char *s, *p, *saveptr;
204 	size_t i = 0;
205 
206 	if (strcmp(var, "addr2line.style"))
207 		return 0;
208 
209 	if (!value)
210 		return -1;
211 
212 	s = strdup(value);
213 	if (!s)
214 		return -1;
215 
216 	p = strtok_r(s, ",", &saveptr);
217 	while (p && i < ARRAY_SIZE(symbol_conf.addr2line_style)) {
218 		bool found = false;
219 		char *q = strim(p);
220 
221 		for (size_t j = A2L_STYLE_LIBDW; j < MAX_A2L_STYLE; j++) {
222 			if (!strcasecmp(q, a2l_style_names[j])) {
223 				symbol_conf.addr2line_style[i++] = j;
224 				found = true;
225 				break;
226 			}
227 		}
228 		if (!found)
229 			pr_warning("Unknown addr2line style: %s\n", q);
230 		p = strtok_r(NULL, ",", &saveptr);
231 	}
232 
233 	free(s);
234 	return 0;
235 }
236 
237 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
238 					struct dso *dso, struct symbol *sym)
239 {
240 	struct inline_node *node;
241 
242 	node = zalloc(sizeof(*node));
243 	if (node == NULL) {
244 		perror("not enough memory for the inline node");
245 		return NULL;
246 	}
247 
248 	INIT_LIST_HEAD(&node->val);
249 	node->addr = addr;
250 
251 	addr2line(dso_name, addr, /*file=*/NULL, /*line_nr=*/NULL, dso,
252 		  /*unwind_inlines=*/true, node, sym);
253 	if (!dso__has_srcline(dso) && list_empty(&node->val)) {
254 		inline_node__delete(node);
255 		return NULL;
256 	}
257 
258 	return node;
259 }
260 
261 /*
262  * Number of addr2line failures (without success) before disabling it for that
263  * dso.
264  */
265 #define A2L_FAIL_LIMIT 123
266 
267 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
268 		  bool show_sym, bool show_addr, bool unwind_inlines,
269 		  u64 ip)
270 {
271 	char *file = NULL;
272 	unsigned line = 0;
273 	char *srcline;
274 	char *dso_name;
275 
276 	if (!dso__has_srcline(dso))
277 		goto out;
278 
279 	dso_name = srcline_dso_name(dso);
280 	if (dso_name == NULL)
281 		goto out_err;
282 
283 	if (addr2line(dso_name, addr, &file, &line, dso,
284 		       unwind_inlines, /*node=*/NULL, sym) <= 0) {
285 		free(dso_name);
286 		goto out_err;
287 	}
288 	free(dso_name);
289 
290 	srcline = srcline_from_fileline(file, line);
291 	free(file);
292 
293 	if (!srcline)
294 		goto out_err;
295 
296 	mutex_lock(dso__lock(dso));
297 	dso__set_a2l_fails(dso, 0);
298 	mutex_unlock(dso__lock(dso));
299 
300 	return srcline;
301 
302 out_err:
303 	mutex_lock(dso__lock(dso));
304 	dso__set_a2l_fails(dso, dso__a2l_fails(dso) + 1);
305 	if (dso__a2l_fails(dso) > A2L_FAIL_LIMIT) {
306 		dso__set_has_srcline(dso, false);
307 		dso__free_a2l(dso);
308 		dso__free_a2l_libbfd(dso);
309 		dso__free_libdw(dso);
310 	}
311 	mutex_unlock(dso__lock(dso));
312 out:
313 	if (!show_addr)
314 		return (show_sym && sym) ?
315 			    strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN;
316 
317 	if (sym) {
318 		if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
319 					ip - sym->start) < 0)
320 			return SRCLINE_UNKNOWN;
321 	} else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso__short_name(dso), addr) < 0)
322 		return SRCLINE_UNKNOWN;
323 	return srcline;
324 }
325 
326 /* Returns filename and fills in line number in line */
327 char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)
328 {
329 	char *file = NULL;
330 	char *dso_name;
331 
332 	if (!dso__has_srcline(dso))
333 		return NULL;
334 
335 	dso_name = srcline_dso_name(dso);
336 	if (dso_name == NULL)
337 		goto out_err;
338 
339 	if (addr2line(dso_name, addr, &file, line, dso, /*unwind_inlines=*/true,
340 			/*node=*/NULL, /*sym=*/NULL) <= 0) {
341 		free(dso_name);
342 		goto out_err;
343 	}
344 	free(dso_name);
345 
346 	mutex_lock(dso__lock(dso));
347 	dso__set_a2l_fails(dso, 0);
348 	mutex_unlock(dso__lock(dso));
349 	return file;
350 
351 out_err:
352 	mutex_lock(dso__lock(dso));
353 	dso__set_a2l_fails(dso, dso__a2l_fails(dso) + 1);
354 	if (dso__a2l_fails(dso) > A2L_FAIL_LIMIT) {
355 		dso__set_has_srcline(dso, false);
356 		dso__free_a2l(dso);
357 		dso__free_a2l_libbfd(dso);
358 		dso__free_libdw(dso);
359 	}
360 	mutex_unlock(dso__lock(dso));
361 
362 	return NULL;
363 }
364 
365 void zfree_srcline(char **srcline)
366 {
367 	if (*srcline == NULL)
368 		return;
369 
370 	if (*srcline != SRCLINE_UNKNOWN)
371 		free(*srcline);
372 
373 	*srcline = NULL;
374 }
375 
376 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
377 		  bool show_sym, bool show_addr, u64 ip)
378 {
379 	return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
380 }
381 
382 struct srcline_node {
383 	u64			addr;
384 	char			*srcline;
385 	struct rb_node		rb_node;
386 };
387 
388 void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline)
389 {
390 	struct rb_node **p = &tree->rb_root.rb_node;
391 	struct rb_node *parent = NULL;
392 	struct srcline_node *i, *node;
393 	bool leftmost = true;
394 
395 	node = zalloc(sizeof(struct srcline_node));
396 	if (!node) {
397 		perror("not enough memory for the srcline node");
398 		return;
399 	}
400 
401 	node->addr = addr;
402 	node->srcline = srcline;
403 
404 	while (*p != NULL) {
405 		parent = *p;
406 		i = rb_entry(parent, struct srcline_node, rb_node);
407 		if (addr < i->addr)
408 			p = &(*p)->rb_left;
409 		else {
410 			p = &(*p)->rb_right;
411 			leftmost = false;
412 		}
413 	}
414 	rb_link_node(&node->rb_node, parent, p);
415 	rb_insert_color_cached(&node->rb_node, tree, leftmost);
416 }
417 
418 char *srcline__tree_find(struct rb_root_cached *tree, u64 addr)
419 {
420 	struct rb_node *n = tree->rb_root.rb_node;
421 
422 	while (n) {
423 		struct srcline_node *i = rb_entry(n, struct srcline_node,
424 						  rb_node);
425 
426 		if (addr < i->addr)
427 			n = n->rb_left;
428 		else if (addr > i->addr)
429 			n = n->rb_right;
430 		else
431 			return i->srcline;
432 	}
433 
434 	return NULL;
435 }
436 
437 void srcline__tree_delete(struct rb_root_cached *tree)
438 {
439 	struct srcline_node *pos;
440 	struct rb_node *next = rb_first_cached(tree);
441 
442 	while (next) {
443 		pos = rb_entry(next, struct srcline_node, rb_node);
444 		next = rb_next(&pos->rb_node);
445 		rb_erase_cached(&pos->rb_node, tree);
446 		zfree_srcline(&pos->srcline);
447 		zfree(&pos);
448 	}
449 }
450 
451 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
452 					    struct symbol *sym)
453 {
454 	char *dso_name;
455 	struct inline_node *node;
456 
457 	if (!dso__has_srcline(dso))
458 		return NULL;
459 
460 	dso_name = srcline_dso_name(dso);
461 	if (dso_name == NULL)
462 		return NULL;
463 
464 	node = addr2inlines(dso_name, addr, dso, sym);
465 	free(dso_name);
466 	return node;
467 }
468 
469 void inline_node__clear_frames(struct inline_node *node)
470 {
471 	struct inline_list *ilist, *tmp;
472 
473 	if (node == NULL)
474 		return;
475 
476 	list_for_each_entry_safe(ilist, tmp, &node->val, list) {
477 		list_del_init(&ilist->list);
478 		zfree_srcline(&ilist->srcline);
479 		/* only the inlined symbols are owned by the list */
480 		if (ilist->symbol && symbol__inlined(ilist->symbol))
481 			symbol__delete(ilist->symbol);
482 		free(ilist);
483 	}
484 }
485 
486 void inline_node__delete(struct inline_node *node)
487 {
488 	inline_node__clear_frames(node);
489 	free(node);
490 }
491 
492 void inlines__tree_insert(struct rb_root_cached *tree,
493 			  struct inline_node *inlines)
494 {
495 	struct rb_node **p = &tree->rb_root.rb_node;
496 	struct rb_node *parent = NULL;
497 	const u64 addr = inlines->addr;
498 	struct inline_node *i;
499 	bool leftmost = true;
500 
501 	while (*p != NULL) {
502 		parent = *p;
503 		i = rb_entry(parent, struct inline_node, rb_node);
504 		if (addr < i->addr)
505 			p = &(*p)->rb_left;
506 		else {
507 			p = &(*p)->rb_right;
508 			leftmost = false;
509 		}
510 	}
511 	rb_link_node(&inlines->rb_node, parent, p);
512 	rb_insert_color_cached(&inlines->rb_node, tree, leftmost);
513 }
514 
515 struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr)
516 {
517 	struct rb_node *n = tree->rb_root.rb_node;
518 
519 	while (n) {
520 		struct inline_node *i = rb_entry(n, struct inline_node,
521 						 rb_node);
522 
523 		if (addr < i->addr)
524 			n = n->rb_left;
525 		else if (addr > i->addr)
526 			n = n->rb_right;
527 		else
528 			return i;
529 	}
530 
531 	return NULL;
532 }
533 
534 void inlines__tree_delete(struct rb_root_cached *tree)
535 {
536 	struct inline_node *pos;
537 	struct rb_node *next = rb_first_cached(tree);
538 
539 	while (next) {
540 		pos = rb_entry(next, struct inline_node, rb_node);
541 		next = rb_next(&pos->rb_node);
542 		rb_erase_cached(&pos->rb_node, tree);
543 		inline_node__delete(pos);
544 	}
545 }
546