1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/kernel.h>
8 #include <linux/string.h>
9 #include <linux/zalloc.h>
12 #include "util/debug.h"
13 #include "util/callchain.h"
14 #include "util/symbol_conf.h"
19 bool srcline_full_filename;
21 static const char *dso__name(struct dso *dso)
25 if (dso->symsrc_filename)
26 dso_name = dso->symsrc_filename;
28 dso_name = dso->long_name;
30 if (dso_name[0] == '[')
33 if (!strncmp(dso_name, "/tmp/perf-", 10))
39 static int inline_list__append(struct symbol *symbol, char *srcline,
40 struct inline_node *node)
42 struct inline_list *ilist;
44 ilist = zalloc(sizeof(*ilist));
48 ilist->symbol = symbol;
49 ilist->srcline = srcline;
51 if (callchain_param.order == ORDER_CALLEE)
52 list_add_tail(&ilist->list, &node->val);
54 list_add(&ilist->list, &node->val);
59 /* basename version that takes a const input string */
60 static const char *gnu_basename(const char *path)
62 const char *base = strrchr(path, '/');
64 return base ? base + 1 : path;
67 static char *srcline_from_fileline(const char *file, unsigned int line)
74 if (!srcline_full_filename)
75 file = gnu_basename(file);
77 if (asprintf(&srcline, "%s:%u", file, line) < 0)
83 static struct symbol *new_inline_sym(struct dso *dso,
84 struct symbol *base_sym,
87 struct symbol *inline_sym;
88 char *demangled = NULL;
94 demangled = dso__demangle_sym(dso, 0, funcname);
99 if (base_sym && strcmp(funcname, base_sym->name) == 0) {
100 /* reuse the real, existing symbol */
101 inline_sym = base_sym;
102 /* ensure that we don't alias an inlined symbol, which could
103 * lead to double frees in inline_node__delete
105 assert(!base_sym->inlined);
107 /* create a fake symbol for the inline frame */
108 inline_sym = symbol__new(base_sym ? base_sym->start : 0,
109 base_sym ? (base_sym->end - base_sym->start) : 0,
110 base_sym ? base_sym->binding : 0,
111 base_sym ? base_sym->type : 0,
114 inline_sym->inlined = 1;
122 #ifdef HAVE_LIBBFD_SUPPORT
125 * Implement addr2line using libbfd.
127 #define PACKAGE "perf"
135 const char *filename;
136 const char *funcname;
143 static int bfd_error(const char *string)
147 errmsg = bfd_errmsg(bfd_get_error());
151 pr_debug("%s: %s\n", string, errmsg);
153 pr_debug("%s\n", errmsg);
158 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
163 bfd_boolean dynamic = FALSE;
165 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
166 return bfd_error(bfd_get_filename(abfd));
168 storage = bfd_get_symtab_upper_bound(abfd);
170 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
174 return bfd_error(bfd_get_filename(abfd));
176 syms = malloc(storage);
178 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
180 symcount = bfd_canonicalize_symtab(abfd, syms);
184 return bfd_error(bfd_get_filename(abfd));
191 static void find_address_in_section(bfd *abfd, asection *section, void *data)
195 struct a2l_data *a2l = data;
201 #ifdef bfd_get_section_flags
202 flags = bfd_get_section_flags(abfd, section);
204 flags = bfd_section_flags(section);
206 if ((flags & SEC_ALLOC) == 0)
210 #ifdef bfd_get_section_vma
211 vma = bfd_get_section_vma(abfd, section);
213 vma = bfd_section_vma(section);
215 #ifdef bfd_get_section_size
216 size = bfd_get_section_size(section);
218 size = bfd_section_size(section);
221 if (pc < vma || pc >= vma + size)
224 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
225 &a2l->filename, &a2l->funcname,
228 if (a2l->filename && !strlen(a2l->filename))
229 a2l->filename = NULL;
232 static struct a2l_data *addr2line_init(const char *path)
235 struct a2l_data *a2l = NULL;
237 abfd = bfd_openr(path, NULL);
241 if (!bfd_check_format(abfd, bfd_object))
244 a2l = zalloc(sizeof(*a2l));
249 a2l->input = strdup(path);
250 if (a2l->input == NULL)
253 if (slurp_symtab(abfd, a2l))
260 zfree((char **)&a2l->input);
267 static void addr2line_cleanup(struct a2l_data *a2l)
270 bfd_close(a2l->abfd);
271 zfree((char **)&a2l->input);
276 #define MAX_INLINE_NEST 1024
278 static int inline_list__append_dso_a2l(struct dso *dso,
279 struct inline_node *node,
282 struct a2l_data *a2l = dso->a2l;
283 struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
284 char *srcline = NULL;
287 srcline = srcline_from_fileline(a2l->filename, a2l->line);
289 return inline_list__append(inline_sym, srcline, node);
292 static int addr2line(const char *dso_name, u64 addr,
293 char **file, unsigned int *line, struct dso *dso,
294 bool unwind_inlines, struct inline_node *node,
298 struct a2l_data *a2l = dso->a2l;
301 dso->a2l = addr2line_init(dso_name);
306 if (!symbol_conf.disable_add2line_warn)
307 pr_warning("addr2line_init failed for %s\n", dso_name);
314 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
319 if (unwind_inlines) {
322 if (node && inline_list__append_dso_a2l(dso, node, sym))
325 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
326 &a2l->funcname, &a2l->line) &&
327 cnt++ < MAX_INLINE_NEST) {
329 if (a2l->filename && !strlen(a2l->filename))
330 a2l->filename = NULL;
333 if (inline_list__append_dso_a2l(dso, node, sym))
335 // found at least one inline frame
342 *file = a2l->filename ? strdup(a2l->filename) : NULL;
352 void dso__free_a2l(struct dso *dso)
354 struct a2l_data *a2l = dso->a2l;
359 addr2line_cleanup(a2l);
364 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
365 struct dso *dso, struct symbol *sym)
367 struct inline_node *node;
369 node = zalloc(sizeof(*node));
371 perror("not enough memory for the inline node");
375 INIT_LIST_HEAD(&node->val);
378 addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
382 #else /* HAVE_LIBBFD_SUPPORT */
384 static int filename_split(char *filename, unsigned int *line_nr)
388 sep = strchr(filename, '\n');
392 if (!strcmp(filename, "??:0"))
395 sep = strchr(filename, ':');
398 *line_nr = strtoul(sep, NULL, 0);
405 static int addr2line(const char *dso_name, u64 addr,
406 char **file, unsigned int *line_nr,
407 struct dso *dso __maybe_unused,
408 bool unwind_inlines __maybe_unused,
409 struct inline_node *node __maybe_unused,
410 struct symbol *sym __maybe_unused)
414 char *filename = NULL;
418 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
421 fp = popen(cmd, "r");
423 pr_warning("popen failed for %s\n", dso_name);
427 if (getline(&filename, &len, fp) < 0 || !len) {
428 pr_warning("addr2line has no output for %s\n", dso_name);
432 ret = filename_split(filename, line_nr);
445 void dso__free_a2l(struct dso *dso __maybe_unused)
449 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
450 struct dso *dso __maybe_unused,
455 struct inline_node *node;
456 char *filename = NULL;
457 char *funcname = NULL;
458 size_t filelen, funclen;
459 unsigned int line_nr = 0;
461 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i -f %016"PRIx64,
464 fp = popen(cmd, "r");
466 pr_err("popen failed for %s\n", dso_name);
470 node = zalloc(sizeof(*node));
472 perror("not enough memory for the inline node");
476 INIT_LIST_HEAD(&node->val);
479 /* addr2line -f generates two lines for each inlined functions */
480 while (getline(&funcname, &funclen, fp) != -1) {
482 struct symbol *inline_sym;
486 if (getline(&filename, &filelen, fp) == -1)
489 if (filename_split(filename, &line_nr) != 1)
492 srcline = srcline_from_fileline(filename, line_nr);
493 inline_sym = new_inline_sym(dso, sym, funcname);
495 if (inline_list__append(inline_sym, srcline, node) != 0) {
497 if (inline_sym && inline_sym->inlined)
498 symbol__delete(inline_sym);
511 #endif /* HAVE_LIBBFD_SUPPORT */
514 * Number of addr2line failures (without success) before disabling it for that
517 #define A2L_FAIL_LIMIT 123
519 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
520 bool show_sym, bool show_addr, bool unwind_inlines,
526 const char *dso_name;
528 if (!dso->has_srcline)
531 dso_name = dso__name(dso);
532 if (dso_name == NULL)
535 if (!addr2line(dso_name, addr, &file, &line, dso,
536 unwind_inlines, NULL, sym))
539 srcline = srcline_from_fileline(file, line);
550 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
551 dso->has_srcline = 0;
556 return (show_sym && sym) ?
557 strndup(sym->name, sym->namelen) : NULL;
560 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
561 ip - sym->start) < 0)
562 return SRCLINE_UNKNOWN;
563 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
564 return SRCLINE_UNKNOWN;
568 /* Returns filename and fills in line number in line */
569 char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)
572 const char *dso_name;
574 if (!dso->has_srcline)
577 dso_name = dso__name(dso);
578 if (dso_name == NULL)
581 if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL))
588 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
589 dso->has_srcline = 0;
596 void free_srcline(char *srcline)
598 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
602 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
603 bool show_sym, bool show_addr, u64 ip)
605 return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
608 struct srcline_node {
611 struct rb_node rb_node;
614 void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline)
616 struct rb_node **p = &tree->rb_root.rb_node;
617 struct rb_node *parent = NULL;
618 struct srcline_node *i, *node;
619 bool leftmost = true;
621 node = zalloc(sizeof(struct srcline_node));
623 perror("not enough memory for the srcline node");
628 node->srcline = srcline;
632 i = rb_entry(parent, struct srcline_node, rb_node);
640 rb_link_node(&node->rb_node, parent, p);
641 rb_insert_color_cached(&node->rb_node, tree, leftmost);
644 char *srcline__tree_find(struct rb_root_cached *tree, u64 addr)
646 struct rb_node *n = tree->rb_root.rb_node;
649 struct srcline_node *i = rb_entry(n, struct srcline_node,
654 else if (addr > i->addr)
663 void srcline__tree_delete(struct rb_root_cached *tree)
665 struct srcline_node *pos;
666 struct rb_node *next = rb_first_cached(tree);
669 pos = rb_entry(next, struct srcline_node, rb_node);
670 next = rb_next(&pos->rb_node);
671 rb_erase_cached(&pos->rb_node, tree);
672 free_srcline(pos->srcline);
677 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
680 const char *dso_name;
682 dso_name = dso__name(dso);
683 if (dso_name == NULL)
686 return addr2inlines(dso_name, addr, dso, sym);
689 void inline_node__delete(struct inline_node *node)
691 struct inline_list *ilist, *tmp;
693 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
694 list_del_init(&ilist->list);
695 free_srcline(ilist->srcline);
696 /* only the inlined symbols are owned by the list */
697 if (ilist->symbol && ilist->symbol->inlined)
698 symbol__delete(ilist->symbol);
705 void inlines__tree_insert(struct rb_root_cached *tree,
706 struct inline_node *inlines)
708 struct rb_node **p = &tree->rb_root.rb_node;
709 struct rb_node *parent = NULL;
710 const u64 addr = inlines->addr;
711 struct inline_node *i;
712 bool leftmost = true;
716 i = rb_entry(parent, struct inline_node, rb_node);
724 rb_link_node(&inlines->rb_node, parent, p);
725 rb_insert_color_cached(&inlines->rb_node, tree, leftmost);
728 struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr)
730 struct rb_node *n = tree->rb_root.rb_node;
733 struct inline_node *i = rb_entry(n, struct inline_node,
738 else if (addr > i->addr)
747 void inlines__tree_delete(struct rb_root_cached *tree)
749 struct inline_node *pos;
750 struct rb_node *next = rb_first_cached(tree);
753 pos = rb_entry(next, struct inline_node, rb_node);
754 next = rb_next(&pos->rb_node);
755 rb_erase_cached(&pos->rb_node, tree);
756 inline_node__delete(pos);