6 #include <linux/kernel.h>
10 #include "util/debug.h"
11 #include "util/callchain.h"
16 bool srcline_full_filename;
18 static const char *dso__name(struct dso *dso)
22 if (dso->symsrc_filename)
23 dso_name = dso->symsrc_filename;
25 dso_name = dso->long_name;
27 if (dso_name[0] == '[')
30 if (!strncmp(dso_name, "/tmp/perf-", 10))
36 static int inline_list__append(struct symbol *symbol, char *srcline,
37 struct inline_node *node)
39 struct inline_list *ilist;
41 ilist = zalloc(sizeof(*ilist));
45 ilist->symbol = symbol;
46 ilist->srcline = srcline;
48 if (callchain_param.order == ORDER_CALLEE)
49 list_add_tail(&ilist->list, &node->val);
51 list_add(&ilist->list, &node->val);
56 /* basename version that takes a const input string */
57 static const char *gnu_basename(const char *path)
59 const char *base = strrchr(path, '/');
61 return base ? base + 1 : path;
64 static char *srcline_from_fileline(const char *file, unsigned int line)
71 if (!srcline_full_filename)
72 file = gnu_basename(file);
74 if (asprintf(&srcline, "%s:%u", file, line) < 0)
80 #ifdef HAVE_LIBBFD_SUPPORT
83 * Implement addr2line using libbfd.
85 #define PACKAGE "perf"
101 static int bfd_error(const char *string)
105 errmsg = bfd_errmsg(bfd_get_error());
109 pr_debug("%s: %s\n", string, errmsg);
111 pr_debug("%s\n", errmsg);
116 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
121 bfd_boolean dynamic = FALSE;
123 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
124 return bfd_error(bfd_get_filename(abfd));
126 storage = bfd_get_symtab_upper_bound(abfd);
128 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
132 return bfd_error(bfd_get_filename(abfd));
134 syms = malloc(storage);
136 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
138 symcount = bfd_canonicalize_symtab(abfd, syms);
142 return bfd_error(bfd_get_filename(abfd));
149 static void find_address_in_section(bfd *abfd, asection *section, void *data)
153 struct a2l_data *a2l = data;
158 if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
162 vma = bfd_get_section_vma(abfd, section);
163 size = bfd_get_section_size(section);
165 if (pc < vma || pc >= vma + size)
168 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
169 &a2l->filename, &a2l->funcname,
172 if (a2l->filename && !strlen(a2l->filename))
173 a2l->filename = NULL;
176 static struct a2l_data *addr2line_init(const char *path)
179 struct a2l_data *a2l = NULL;
181 abfd = bfd_openr(path, NULL);
185 if (!bfd_check_format(abfd, bfd_object))
188 a2l = zalloc(sizeof(*a2l));
193 a2l->input = strdup(path);
194 if (a2l->input == NULL)
197 if (slurp_symtab(abfd, a2l))
204 zfree((char **)&a2l->input);
211 static void addr2line_cleanup(struct a2l_data *a2l)
214 bfd_close(a2l->abfd);
215 zfree((char **)&a2l->input);
220 #define MAX_INLINE_NEST 1024
222 static struct symbol *new_inline_sym(struct dso *dso,
223 struct symbol *base_sym,
224 const char *funcname)
226 struct symbol *inline_sym;
227 char *demangled = NULL;
230 demangled = dso__demangle_sym(dso, 0, funcname);
232 funcname = demangled;
235 if (base_sym && strcmp(funcname, base_sym->name) == 0) {
236 /* reuse the real, existing symbol */
237 inline_sym = base_sym;
238 /* ensure that we don't alias an inlined symbol, which could
239 * lead to double frees in inline_node__delete
241 assert(!base_sym->inlined);
243 /* create a fake symbol for the inline frame */
244 inline_sym = symbol__new(base_sym ? base_sym->start : 0,
245 base_sym ? base_sym->end : 0,
246 base_sym ? base_sym->binding : 0,
249 inline_sym->inlined = 1;
257 static int inline_list__append_dso_a2l(struct dso *dso,
258 struct inline_node *node,
261 struct a2l_data *a2l = dso->a2l;
262 struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
263 char *srcline = NULL;
266 srcline = srcline_from_fileline(a2l->filename, a2l->line);
268 return inline_list__append(inline_sym, srcline, node);
271 static int addr2line(const char *dso_name, u64 addr,
272 char **file, unsigned int *line, struct dso *dso,
273 bool unwind_inlines, struct inline_node *node,
277 struct a2l_data *a2l = dso->a2l;
280 dso->a2l = addr2line_init(dso_name);
285 pr_warning("addr2line_init failed for %s\n", dso_name);
292 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
297 if (unwind_inlines) {
300 if (node && inline_list__append_dso_a2l(dso, node, sym))
303 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
304 &a2l->funcname, &a2l->line) &&
305 cnt++ < MAX_INLINE_NEST) {
307 if (a2l->filename && !strlen(a2l->filename))
308 a2l->filename = NULL;
311 if (inline_list__append_dso_a2l(dso, node, sym))
313 // found at least one inline frame
320 *file = a2l->filename ? strdup(a2l->filename) : NULL;
330 void dso__free_a2l(struct dso *dso)
332 struct a2l_data *a2l = dso->a2l;
337 addr2line_cleanup(a2l);
342 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
343 struct dso *dso, struct symbol *sym)
345 struct inline_node *node;
347 node = zalloc(sizeof(*node));
349 perror("not enough memory for the inline node");
353 INIT_LIST_HEAD(&node->val);
356 addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
360 #else /* HAVE_LIBBFD_SUPPORT */
362 static int filename_split(char *filename, unsigned int *line_nr)
366 sep = strchr(filename, '\n');
370 if (!strcmp(filename, "??:0"))
373 sep = strchr(filename, ':');
376 *line_nr = strtoul(sep, NULL, 0);
383 static int addr2line(const char *dso_name, u64 addr,
384 char **file, unsigned int *line_nr,
385 struct dso *dso __maybe_unused,
386 bool unwind_inlines __maybe_unused,
387 struct inline_node *node __maybe_unused,
388 struct symbol *sym __maybe_unused)
392 char *filename = NULL;
396 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
399 fp = popen(cmd, "r");
401 pr_warning("popen failed for %s\n", dso_name);
405 if (getline(&filename, &len, fp) < 0 || !len) {
406 pr_warning("addr2line has no output for %s\n", dso_name);
410 ret = filename_split(filename, line_nr);
423 void dso__free_a2l(struct dso *dso __maybe_unused)
427 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
428 struct dso *dso __maybe_unused,
433 struct inline_node *node;
434 char *filename = NULL;
436 unsigned int line_nr = 0;
438 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
441 fp = popen(cmd, "r");
443 pr_err("popen failed for %s\n", dso_name);
447 node = zalloc(sizeof(*node));
449 perror("not enough memory for the inline node");
453 INIT_LIST_HEAD(&node->val);
456 while (getline(&filename, &len, fp) != -1) {
459 if (filename_split(filename, &line_nr) != 1)
462 srcline = srcline_from_fileline(filename, line_nr);
463 if (inline_list__append(sym, srcline, node) != 0)
474 #endif /* HAVE_LIBBFD_SUPPORT */
477 * Number of addr2line failures (without success) before disabling it for that
480 #define A2L_FAIL_LIMIT 123
482 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
483 bool show_sym, bool show_addr, bool unwind_inlines)
488 const char *dso_name;
490 if (!dso->has_srcline)
493 dso_name = dso__name(dso);
494 if (dso_name == NULL)
497 if (!addr2line(dso_name, addr, &file, &line, dso,
498 unwind_inlines, NULL, sym))
501 srcline = srcline_from_fileline(file, line);
512 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
513 dso->has_srcline = 0;
518 return (show_sym && sym) ?
519 strndup(sym->name, sym->namelen) : NULL;
522 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
523 addr - sym->start) < 0)
524 return SRCLINE_UNKNOWN;
525 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
526 return SRCLINE_UNKNOWN;
530 void free_srcline(char *srcline)
532 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
536 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
537 bool show_sym, bool show_addr)
539 return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
542 struct srcline_node {
545 struct rb_node rb_node;
548 void srcline__tree_insert(struct rb_root *tree, u64 addr, char *srcline)
550 struct rb_node **p = &tree->rb_node;
551 struct rb_node *parent = NULL;
552 struct srcline_node *i, *node;
554 node = zalloc(sizeof(struct srcline_node));
556 perror("not enough memory for the srcline node");
561 node->srcline = srcline;
565 i = rb_entry(parent, struct srcline_node, rb_node);
571 rb_link_node(&node->rb_node, parent, p);
572 rb_insert_color(&node->rb_node, tree);
575 char *srcline__tree_find(struct rb_root *tree, u64 addr)
577 struct rb_node *n = tree->rb_node;
580 struct srcline_node *i = rb_entry(n, struct srcline_node,
585 else if (addr > i->addr)
594 void srcline__tree_delete(struct rb_root *tree)
596 struct srcline_node *pos;
597 struct rb_node *next = rb_first(tree);
600 pos = rb_entry(next, struct srcline_node, rb_node);
601 next = rb_next(&pos->rb_node);
602 rb_erase(&pos->rb_node, tree);
603 free_srcline(pos->srcline);
608 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
611 const char *dso_name;
613 dso_name = dso__name(dso);
614 if (dso_name == NULL)
617 return addr2inlines(dso_name, addr, dso, sym);
620 void inline_node__delete(struct inline_node *node)
622 struct inline_list *ilist, *tmp;
624 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
625 list_del_init(&ilist->list);
626 free_srcline(ilist->srcline);
627 /* only the inlined symbols are owned by the list */
628 if (ilist->symbol && ilist->symbol->inlined)
629 symbol__delete(ilist->symbol);
636 void inlines__tree_insert(struct rb_root *tree, struct inline_node *inlines)
638 struct rb_node **p = &tree->rb_node;
639 struct rb_node *parent = NULL;
640 const u64 addr = inlines->addr;
641 struct inline_node *i;
645 i = rb_entry(parent, struct inline_node, rb_node);
651 rb_link_node(&inlines->rb_node, parent, p);
652 rb_insert_color(&inlines->rb_node, tree);
655 struct inline_node *inlines__tree_find(struct rb_root *tree, u64 addr)
657 struct rb_node *n = tree->rb_node;
660 struct inline_node *i = rb_entry(n, struct inline_node,
665 else if (addr > i->addr)
674 void inlines__tree_delete(struct rb_root *tree)
676 struct inline_node *pos;
677 struct rb_node *next = rb_first(tree);
680 pos = rb_entry(next, struct inline_node, rb_node);
681 next = rb_next(&pos->rb_node);
682 rb_erase(&pos->rb_node, tree);
683 inline_node__delete(pos);