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 if (!addr2line(dso_name, addr, NULL, NULL, dso, TRUE, node, sym))
357 goto out_free_inline_node;
359 if (list_empty(&node->val))
360 goto out_free_inline_node;
364 out_free_inline_node:
365 inline_node__delete(node);
369 #else /* HAVE_LIBBFD_SUPPORT */
371 static int filename_split(char *filename, unsigned int *line_nr)
375 sep = strchr(filename, '\n');
379 if (!strcmp(filename, "??:0"))
382 sep = strchr(filename, ':');
385 *line_nr = strtoul(sep, NULL, 0);
392 static int addr2line(const char *dso_name, u64 addr,
393 char **file, unsigned int *line_nr,
394 struct dso *dso __maybe_unused,
395 bool unwind_inlines __maybe_unused,
396 struct inline_node *node __maybe_unused,
397 struct symbol *sym __maybe_unused)
401 char *filename = NULL;
405 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
408 fp = popen(cmd, "r");
410 pr_warning("popen failed for %s\n", dso_name);
414 if (getline(&filename, &len, fp) < 0 || !len) {
415 pr_warning("addr2line has no output for %s\n", dso_name);
419 ret = filename_split(filename, line_nr);
432 void dso__free_a2l(struct dso *dso __maybe_unused)
436 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
437 struct dso *dso __maybe_unused,
442 struct inline_node *node;
443 char *filename = NULL;
445 unsigned int line_nr = 0;
447 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
450 fp = popen(cmd, "r");
452 pr_err("popen failed for %s\n", dso_name);
456 node = zalloc(sizeof(*node));
458 perror("not enough memory for the inline node");
462 INIT_LIST_HEAD(&node->val);
465 while (getline(&filename, &len, fp) != -1) {
468 if (filename_split(filename, &line_nr) != 1) {
473 srcline = srcline_from_fileline(filename, line_nr);
474 if (inline_list__append(sym, srcline, node) != 0)
483 if (list_empty(&node->val)) {
484 inline_node__delete(node);
491 #endif /* HAVE_LIBBFD_SUPPORT */
494 * Number of addr2line failures (without success) before disabling it for that
497 #define A2L_FAIL_LIMIT 123
499 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
500 bool show_sym, bool show_addr, bool unwind_inlines)
505 const char *dso_name;
507 if (!dso->has_srcline)
510 dso_name = dso__name(dso);
511 if (dso_name == NULL)
514 if (!addr2line(dso_name, addr, &file, &line, dso,
515 unwind_inlines, NULL, sym))
518 srcline = srcline_from_fileline(file, line);
529 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
530 dso->has_srcline = 0;
535 return (show_sym && sym) ?
536 strndup(sym->name, sym->namelen) : NULL;
539 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
540 addr - sym->start) < 0)
541 return SRCLINE_UNKNOWN;
542 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
543 return SRCLINE_UNKNOWN;
547 void free_srcline(char *srcline)
549 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
553 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
554 bool show_sym, bool show_addr)
556 return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
559 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
562 const char *dso_name;
564 dso_name = dso__name(dso);
565 if (dso_name == NULL)
568 return addr2inlines(dso_name, addr, dso, sym);
571 void inline_node__delete(struct inline_node *node)
573 struct inline_list *ilist, *tmp;
575 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
576 list_del_init(&ilist->list);
577 free_srcline(ilist->srcline);
578 /* only the inlined symbols are owned by the list */
579 if (ilist->symbol && ilist->symbol->inlined)
580 symbol__delete(ilist->symbol);