5 #include <linux/kernel.h>
9 #include "util/debug.h"
10 #include "util/callchain.h"
14 bool srcline_full_filename;
16 static const char *dso__name(struct dso *dso)
20 if (dso->symsrc_filename)
21 dso_name = dso->symsrc_filename;
23 dso_name = dso->long_name;
25 if (dso_name[0] == '[')
28 if (!strncmp(dso_name, "/tmp/perf-", 10))
34 static int inline_list__append(char *filename, char *funcname, int line_nr,
35 struct inline_node *node, struct dso *dso)
37 struct inline_list *ilist;
40 ilist = zalloc(sizeof(*ilist));
44 ilist->filename = filename;
45 ilist->line_nr = line_nr;
48 demangled = dso__demangle_sym(dso, 0, funcname);
49 if (demangled == NULL) {
50 ilist->funcname = funcname;
52 ilist->funcname = demangled;
57 list_add_tail(&ilist->list, &node->val);
62 #ifdef HAVE_LIBBFD_SUPPORT
65 * Implement addr2line using libbfd.
67 #define PACKAGE "perf"
83 static int bfd_error(const char *string)
87 errmsg = bfd_errmsg(bfd_get_error());
91 pr_debug("%s: %s\n", string, errmsg);
93 pr_debug("%s\n", errmsg);
98 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
103 bfd_boolean dynamic = FALSE;
105 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
106 return bfd_error(bfd_get_filename(abfd));
108 storage = bfd_get_symtab_upper_bound(abfd);
110 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
114 return bfd_error(bfd_get_filename(abfd));
116 syms = malloc(storage);
118 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
120 symcount = bfd_canonicalize_symtab(abfd, syms);
124 return bfd_error(bfd_get_filename(abfd));
131 static void find_address_in_section(bfd *abfd, asection *section, void *data)
135 struct a2l_data *a2l = data;
140 if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
144 vma = bfd_get_section_vma(abfd, section);
145 size = bfd_get_section_size(section);
147 if (pc < vma || pc >= vma + size)
150 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
151 &a2l->filename, &a2l->funcname,
155 static struct a2l_data *addr2line_init(const char *path)
158 struct a2l_data *a2l = NULL;
160 abfd = bfd_openr(path, NULL);
164 if (!bfd_check_format(abfd, bfd_object))
167 a2l = zalloc(sizeof(*a2l));
172 a2l->input = strdup(path);
173 if (a2l->input == NULL)
176 if (slurp_symtab(abfd, a2l))
183 zfree((char **)&a2l->input);
190 static void addr2line_cleanup(struct a2l_data *a2l)
193 bfd_close(a2l->abfd);
194 zfree((char **)&a2l->input);
199 #define MAX_INLINE_NEST 1024
201 static void inline_list__reverse(struct inline_node *node)
203 struct inline_list *ilist, *n;
205 list_for_each_entry_safe_reverse(ilist, n, &node->val, list)
206 list_move_tail(&ilist->list, &node->val);
209 static int addr2line(const char *dso_name, u64 addr,
210 char **file, unsigned int *line, struct dso *dso,
211 bool unwind_inlines, struct inline_node *node)
214 struct a2l_data *a2l = dso->a2l;
217 dso->a2l = addr2line_init(dso_name);
222 pr_warning("addr2line_init failed for %s\n", dso_name);
229 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
231 if (a2l->found && unwind_inlines) {
234 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
235 &a2l->funcname, &a2l->line) &&
236 cnt++ < MAX_INLINE_NEST) {
239 if (inline_list__append(strdup(a2l->filename),
240 strdup(a2l->funcname),
247 if ((node != NULL) &&
248 (callchain_param.order != ORDER_CALLEE)) {
249 inline_list__reverse(node);
253 if (a2l->found && a2l->filename) {
254 *file = strdup(a2l->filename);
264 void dso__free_a2l(struct dso *dso)
266 struct a2l_data *a2l = dso->a2l;
271 addr2line_cleanup(a2l);
276 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
280 unsigned int line = 0;
281 struct inline_node *node;
283 node = zalloc(sizeof(*node));
285 perror("not enough memory for the inline node");
289 INIT_LIST_HEAD(&node->val);
292 if (!addr2line(dso_name, addr, &file, &line, dso, TRUE, node))
293 goto out_free_inline_node;
295 if (list_empty(&node->val))
296 goto out_free_inline_node;
300 out_free_inline_node:
301 inline_node__delete(node);
305 #else /* HAVE_LIBBFD_SUPPORT */
307 static int filename_split(char *filename, unsigned int *line_nr)
311 sep = strchr(filename, '\n');
315 if (!strcmp(filename, "??:0"))
318 sep = strchr(filename, ':');
321 *line_nr = strtoul(sep, NULL, 0);
328 static int addr2line(const char *dso_name, u64 addr,
329 char **file, unsigned int *line_nr,
330 struct dso *dso __maybe_unused,
331 bool unwind_inlines __maybe_unused,
332 struct inline_node *node __maybe_unused)
336 char *filename = NULL;
340 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
343 fp = popen(cmd, "r");
345 pr_warning("popen failed for %s\n", dso_name);
349 if (getline(&filename, &len, fp) < 0 || !len) {
350 pr_warning("addr2line has no output for %s\n", dso_name);
354 ret = filename_split(filename, line_nr);
367 void dso__free_a2l(struct dso *dso __maybe_unused)
371 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
372 struct dso *dso __maybe_unused)
376 struct inline_node *node;
377 char *filename = NULL;
379 unsigned int line_nr = 0;
381 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
384 fp = popen(cmd, "r");
386 pr_err("popen failed for %s\n", dso_name);
390 node = zalloc(sizeof(*node));
392 perror("not enough memory for the inline node");
396 INIT_LIST_HEAD(&node->val);
399 while (getline(&filename, &len, fp) != -1) {
400 if (filename_split(filename, &line_nr) != 1) {
405 if (inline_list__append(filename, NULL, line_nr, node,
415 if (list_empty(&node->val)) {
416 inline_node__delete(node);
423 #endif /* HAVE_LIBBFD_SUPPORT */
426 * Number of addr2line failures (without success) before disabling it for that
429 #define A2L_FAIL_LIMIT 123
431 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
432 bool show_sym, bool show_addr, bool unwind_inlines)
437 const char *dso_name;
439 if (!dso->has_srcline)
442 dso_name = dso__name(dso);
443 if (dso_name == NULL)
446 if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines, NULL))
449 if (asprintf(&srcline, "%s:%u",
450 srcline_full_filename ? file : basename(file),
462 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
463 dso->has_srcline = 0;
468 return (show_sym && sym) ?
469 strndup(sym->name, sym->namelen) : NULL;
472 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
473 addr - sym->start) < 0)
474 return SRCLINE_UNKNOWN;
475 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
476 return SRCLINE_UNKNOWN;
480 void free_srcline(char *srcline)
482 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
486 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
487 bool show_sym, bool show_addr)
489 return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
492 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr)
494 const char *dso_name;
496 dso_name = dso__name(dso);
497 if (dso_name == NULL)
500 return addr2inlines(dso_name, addr, dso);
503 void inline_node__delete(struct inline_node *node)
505 struct inline_list *ilist, *tmp;
507 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
508 list_del_init(&ilist->list);
509 zfree(&ilist->filename);
510 zfree(&ilist->funcname);