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) {
464 srcline = srcline_from_fileline(filename, line_nr);
465 if (inline_list__append(sym, srcline, node) != 0)
477 #endif /* HAVE_LIBBFD_SUPPORT */
480 * Number of addr2line failures (without success) before disabling it for that
483 #define A2L_FAIL_LIMIT 123
485 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
486 bool show_sym, bool show_addr, bool unwind_inlines)
491 const char *dso_name;
493 if (!dso->has_srcline)
496 dso_name = dso__name(dso);
497 if (dso_name == NULL)
500 if (!addr2line(dso_name, addr, &file, &line, dso,
501 unwind_inlines, NULL, sym))
504 srcline = srcline_from_fileline(file, line);
515 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
516 dso->has_srcline = 0;
521 return (show_sym && sym) ?
522 strndup(sym->name, sym->namelen) : NULL;
525 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
526 addr - sym->start) < 0)
527 return SRCLINE_UNKNOWN;
528 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
529 return SRCLINE_UNKNOWN;
533 void free_srcline(char *srcline)
535 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
539 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
540 bool show_sym, bool show_addr)
542 return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
545 struct srcline_node {
548 struct rb_node rb_node;
551 void srcline__tree_insert(struct rb_root *tree, u64 addr, char *srcline)
553 struct rb_node **p = &tree->rb_node;
554 struct rb_node *parent = NULL;
555 struct srcline_node *i, *node;
557 node = zalloc(sizeof(struct srcline_node));
559 perror("not enough memory for the srcline node");
564 node->srcline = srcline;
568 i = rb_entry(parent, struct srcline_node, rb_node);
574 rb_link_node(&node->rb_node, parent, p);
575 rb_insert_color(&node->rb_node, tree);
578 char *srcline__tree_find(struct rb_root *tree, u64 addr)
580 struct rb_node *n = tree->rb_node;
583 struct srcline_node *i = rb_entry(n, struct srcline_node,
588 else if (addr > i->addr)
597 void srcline__tree_delete(struct rb_root *tree)
599 struct srcline_node *pos;
600 struct rb_node *next = rb_first(tree);
603 pos = rb_entry(next, struct srcline_node, rb_node);
604 next = rb_next(&pos->rb_node);
605 rb_erase(&pos->rb_node, tree);
606 free_srcline(pos->srcline);
611 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
614 const char *dso_name;
616 dso_name = dso__name(dso);
617 if (dso_name == NULL)
620 return addr2inlines(dso_name, addr, dso, sym);
623 void inline_node__delete(struct inline_node *node)
625 struct inline_list *ilist, *tmp;
627 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
628 list_del_init(&ilist->list);
629 free_srcline(ilist->srcline);
630 /* only the inlined symbols are owned by the list */
631 if (ilist->symbol && ilist->symbol->inlined)
632 symbol__delete(ilist->symbol);
639 void inlines__tree_insert(struct rb_root *tree, struct inline_node *inlines)
641 struct rb_node **p = &tree->rb_node;
642 struct rb_node *parent = NULL;
643 const u64 addr = inlines->addr;
644 struct inline_node *i;
648 i = rb_entry(parent, struct inline_node, rb_node);
654 rb_link_node(&inlines->rb_node, parent, p);
655 rb_insert_color(&inlines->rb_node, tree);
658 struct inline_node *inlines__tree_find(struct rb_root *tree, u64 addr)
660 struct rb_node *n = tree->rb_node;
663 struct inline_node *i = rb_entry(n, struct inline_node,
668 else if (addr > i->addr)
677 void inlines__tree_delete(struct rb_root *tree)
679 struct inline_node *pos;
680 struct rb_node *next = rb_first(tree);
683 pos = rb_entry(next, struct inline_node, rb_node);
684 next = rb_next(&pos->rb_node);
685 rb_erase(&pos->rb_node, tree);
686 inline_node__delete(pos);