1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/kernel.h>
10 #include "util/util.h"
11 #include "util/debug.h"
12 #include "util/callchain.h"
17 bool srcline_full_filename;
19 static const char *dso__name(struct dso *dso)
23 if (dso->symsrc_filename)
24 dso_name = dso->symsrc_filename;
26 dso_name = dso->long_name;
28 if (dso_name[0] == '[')
31 if (!strncmp(dso_name, "/tmp/perf-", 10))
37 static int inline_list__append(struct symbol *symbol, char *srcline,
38 struct inline_node *node)
40 struct inline_list *ilist;
42 ilist = zalloc(sizeof(*ilist));
46 ilist->symbol = symbol;
47 ilist->srcline = srcline;
49 if (callchain_param.order == ORDER_CALLEE)
50 list_add_tail(&ilist->list, &node->val);
52 list_add(&ilist->list, &node->val);
57 /* basename version that takes a const input string */
58 static const char *gnu_basename(const char *path)
60 const char *base = strrchr(path, '/');
62 return base ? base + 1 : path;
65 static char *srcline_from_fileline(const char *file, unsigned int line)
72 if (!srcline_full_filename)
73 file = gnu_basename(file);
75 if (asprintf(&srcline, "%s:%u", file, line) < 0)
81 static struct symbol *new_inline_sym(struct dso *dso,
82 struct symbol *base_sym,
85 struct symbol *inline_sym;
86 char *demangled = NULL;
92 demangled = dso__demangle_sym(dso, 0, funcname);
97 if (base_sym && strcmp(funcname, base_sym->name) == 0) {
98 /* reuse the real, existing symbol */
99 inline_sym = base_sym;
100 /* ensure that we don't alias an inlined symbol, which could
101 * lead to double frees in inline_node__delete
103 assert(!base_sym->inlined);
105 /* create a fake symbol for the inline frame */
106 inline_sym = symbol__new(base_sym ? base_sym->start : 0,
107 base_sym ? (base_sym->end - base_sym->start) : 0,
108 base_sym ? base_sym->binding : 0,
109 base_sym ? base_sym->type : 0,
112 inline_sym->inlined = 1;
120 #ifdef HAVE_LIBBFD_SUPPORT
123 * Implement addr2line using libbfd.
125 #define PACKAGE "perf"
133 const char *filename;
134 const char *funcname;
141 static int bfd_error(const char *string)
145 errmsg = bfd_errmsg(bfd_get_error());
149 pr_debug("%s: %s\n", string, errmsg);
151 pr_debug("%s\n", errmsg);
156 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
161 bfd_boolean dynamic = FALSE;
163 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
164 return bfd_error(bfd_get_filename(abfd));
166 storage = bfd_get_symtab_upper_bound(abfd);
168 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
172 return bfd_error(bfd_get_filename(abfd));
174 syms = malloc(storage);
176 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
178 symcount = bfd_canonicalize_symtab(abfd, syms);
182 return bfd_error(bfd_get_filename(abfd));
189 static void find_address_in_section(bfd *abfd, asection *section, void *data)
193 struct a2l_data *a2l = data;
199 #ifdef bfd_get_section_flags
200 flags = bfd_get_section_flags(abfd, section);
202 flags = bfd_section_flags(section);
204 if ((flags & SEC_ALLOC) == 0)
208 #ifdef bfd_get_section_vma
209 vma = bfd_get_section_vma(abfd, section);
211 vma = bfd_section_vma(section);
213 #ifdef bfd_get_section_size
214 size = bfd_get_section_size(section);
216 size = bfd_section_size(section);
219 if (pc < vma || pc >= vma + size)
222 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
223 &a2l->filename, &a2l->funcname,
226 if (a2l->filename && !strlen(a2l->filename))
227 a2l->filename = NULL;
230 static struct a2l_data *addr2line_init(const char *path)
233 struct a2l_data *a2l = NULL;
235 abfd = bfd_openr(path, NULL);
239 if (!bfd_check_format(abfd, bfd_object))
242 a2l = zalloc(sizeof(*a2l));
247 a2l->input = strdup(path);
248 if (a2l->input == NULL)
251 if (slurp_symtab(abfd, a2l))
258 zfree((char **)&a2l->input);
265 static void addr2line_cleanup(struct a2l_data *a2l)
268 bfd_close(a2l->abfd);
269 zfree((char **)&a2l->input);
274 #define MAX_INLINE_NEST 1024
276 static int inline_list__append_dso_a2l(struct dso *dso,
277 struct inline_node *node,
280 struct a2l_data *a2l = dso->a2l;
281 struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
282 char *srcline = NULL;
285 srcline = srcline_from_fileline(a2l->filename, a2l->line);
287 return inline_list__append(inline_sym, srcline, node);
290 static int addr2line(const char *dso_name, u64 addr,
291 char **file, unsigned int *line, struct dso *dso,
292 bool unwind_inlines, struct inline_node *node,
296 struct a2l_data *a2l = dso->a2l;
299 dso->a2l = addr2line_init(dso_name);
304 pr_warning("addr2line_init failed for %s\n", dso_name);
311 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
316 if (unwind_inlines) {
319 if (node && inline_list__append_dso_a2l(dso, node, sym))
322 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
323 &a2l->funcname, &a2l->line) &&
324 cnt++ < MAX_INLINE_NEST) {
326 if (a2l->filename && !strlen(a2l->filename))
327 a2l->filename = NULL;
330 if (inline_list__append_dso_a2l(dso, node, sym))
332 // found at least one inline frame
339 *file = a2l->filename ? strdup(a2l->filename) : NULL;
349 void dso__free_a2l(struct dso *dso)
351 struct a2l_data *a2l = dso->a2l;
356 addr2line_cleanup(a2l);
361 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
362 struct dso *dso, struct symbol *sym)
364 struct inline_node *node;
366 node = zalloc(sizeof(*node));
368 perror("not enough memory for the inline node");
372 INIT_LIST_HEAD(&node->val);
375 addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
379 #else /* HAVE_LIBBFD_SUPPORT */
381 static int filename_split(char *filename, unsigned int *line_nr)
385 sep = strchr(filename, '\n');
389 if (!strcmp(filename, "??:0"))
392 sep = strchr(filename, ':');
395 *line_nr = strtoul(sep, NULL, 0);
402 static int addr2line(const char *dso_name, u64 addr,
403 char **file, unsigned int *line_nr,
404 struct dso *dso __maybe_unused,
405 bool unwind_inlines __maybe_unused,
406 struct inline_node *node __maybe_unused,
407 struct symbol *sym __maybe_unused)
411 char *filename = NULL;
415 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
418 fp = popen(cmd, "r");
420 pr_warning("popen failed for %s\n", dso_name);
424 if (getline(&filename, &len, fp) < 0 || !len) {
425 pr_warning("addr2line has no output for %s\n", dso_name);
429 ret = filename_split(filename, line_nr);
442 void dso__free_a2l(struct dso *dso __maybe_unused)
446 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
447 struct dso *dso __maybe_unused,
452 struct inline_node *node;
453 char *filename = NULL;
454 char *funcname = NULL;
455 size_t filelen, funclen;
456 unsigned int line_nr = 0;
458 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i -f %016"PRIx64,
461 fp = popen(cmd, "r");
463 pr_err("popen failed for %s\n", dso_name);
467 node = zalloc(sizeof(*node));
469 perror("not enough memory for the inline node");
473 INIT_LIST_HEAD(&node->val);
476 /* addr2line -f generates two lines for each inlined functions */
477 while (getline(&funcname, &funclen, fp) != -1) {
479 struct symbol *inline_sym;
483 if (getline(&filename, &filelen, fp) == -1)
486 if (filename_split(filename, &line_nr) != 1)
489 srcline = srcline_from_fileline(filename, line_nr);
490 inline_sym = new_inline_sym(dso, sym, funcname);
492 if (inline_list__append(inline_sym, srcline, node) != 0) {
494 if (inline_sym && inline_sym->inlined)
495 symbol__delete(inline_sym);
508 #endif /* HAVE_LIBBFD_SUPPORT */
511 * Number of addr2line failures (without success) before disabling it for that
514 #define A2L_FAIL_LIMIT 123
516 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
517 bool show_sym, bool show_addr, bool unwind_inlines,
523 const char *dso_name;
525 if (!dso->has_srcline)
528 dso_name = dso__name(dso);
529 if (dso_name == NULL)
532 if (!addr2line(dso_name, addr, &file, &line, dso,
533 unwind_inlines, NULL, sym))
536 srcline = srcline_from_fileline(file, line);
547 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
548 dso->has_srcline = 0;
553 return (show_sym && sym) ?
554 strndup(sym->name, sym->namelen) : NULL;
557 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
558 ip - sym->start) < 0)
559 return SRCLINE_UNKNOWN;
560 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
561 return SRCLINE_UNKNOWN;
565 void free_srcline(char *srcline)
567 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
571 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
572 bool show_sym, bool show_addr, u64 ip)
574 return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
577 struct srcline_node {
580 struct rb_node rb_node;
583 void srcline__tree_insert(struct rb_root *tree, u64 addr, char *srcline)
585 struct rb_node **p = &tree->rb_node;
586 struct rb_node *parent = NULL;
587 struct srcline_node *i, *node;
589 node = zalloc(sizeof(struct srcline_node));
591 perror("not enough memory for the srcline node");
596 node->srcline = srcline;
600 i = rb_entry(parent, struct srcline_node, rb_node);
606 rb_link_node(&node->rb_node, parent, p);
607 rb_insert_color(&node->rb_node, tree);
610 char *srcline__tree_find(struct rb_root *tree, u64 addr)
612 struct rb_node *n = tree->rb_node;
615 struct srcline_node *i = rb_entry(n, struct srcline_node,
620 else if (addr > i->addr)
629 void srcline__tree_delete(struct rb_root *tree)
631 struct srcline_node *pos;
632 struct rb_node *next = rb_first(tree);
635 pos = rb_entry(next, struct srcline_node, rb_node);
636 next = rb_next(&pos->rb_node);
637 rb_erase(&pos->rb_node, tree);
638 free_srcline(pos->srcline);
643 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
646 const char *dso_name;
648 dso_name = dso__name(dso);
649 if (dso_name == NULL)
652 return addr2inlines(dso_name, addr, dso, sym);
655 void inline_node__delete(struct inline_node *node)
657 struct inline_list *ilist, *tmp;
659 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
660 list_del_init(&ilist->list);
661 free_srcline(ilist->srcline);
662 /* only the inlined symbols are owned by the list */
663 if (ilist->symbol && ilist->symbol->inlined)
664 symbol__delete(ilist->symbol);
671 void inlines__tree_insert(struct rb_root *tree, struct inline_node *inlines)
673 struct rb_node **p = &tree->rb_node;
674 struct rb_node *parent = NULL;
675 const u64 addr = inlines->addr;
676 struct inline_node *i;
680 i = rb_entry(parent, struct inline_node, rb_node);
686 rb_link_node(&inlines->rb_node, parent, p);
687 rb_insert_color(&inlines->rb_node, tree);
690 struct inline_node *inlines__tree_find(struct rb_root *tree, u64 addr)
692 struct rb_node *n = tree->rb_node;
695 struct inline_node *i = rb_entry(n, struct inline_node,
700 else if (addr > i->addr)
709 void inlines__tree_delete(struct rb_root *tree)
711 struct inline_node *pos;
712 struct rb_node *next = rb_first(tree);
715 pos = rb_entry(next, struct inline_node, rb_node);
716 next = rb_next(&pos->rb_node);
717 rb_erase(&pos->rb_node, tree);
718 inline_node__delete(pos);