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(char *filename, char *funcname, int line_nr,
37 struct inline_node *node, struct dso *dso)
39 struct inline_list *ilist;
42 ilist = zalloc(sizeof(*ilist));
46 ilist->filename = filename;
47 ilist->line_nr = line_nr;
50 demangled = dso__demangle_sym(dso, 0, funcname);
51 if (demangled == NULL) {
52 ilist->funcname = funcname;
54 ilist->funcname = demangled;
59 list_add_tail(&ilist->list, &node->val);
64 #ifdef HAVE_LIBBFD_SUPPORT
67 * Implement addr2line using libbfd.
69 #define PACKAGE "perf"
85 static int bfd_error(const char *string)
89 errmsg = bfd_errmsg(bfd_get_error());
93 pr_debug("%s: %s\n", string, errmsg);
95 pr_debug("%s\n", errmsg);
100 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
105 bfd_boolean dynamic = FALSE;
107 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
108 return bfd_error(bfd_get_filename(abfd));
110 storage = bfd_get_symtab_upper_bound(abfd);
112 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
116 return bfd_error(bfd_get_filename(abfd));
118 syms = malloc(storage);
120 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
122 symcount = bfd_canonicalize_symtab(abfd, syms);
126 return bfd_error(bfd_get_filename(abfd));
133 static void find_address_in_section(bfd *abfd, asection *section, void *data)
137 struct a2l_data *a2l = data;
142 if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
146 vma = bfd_get_section_vma(abfd, section);
147 size = bfd_get_section_size(section);
149 if (pc < vma || pc >= vma + size)
152 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
153 &a2l->filename, &a2l->funcname,
157 static struct a2l_data *addr2line_init(const char *path)
160 struct a2l_data *a2l = NULL;
162 abfd = bfd_openr(path, NULL);
166 if (!bfd_check_format(abfd, bfd_object))
169 a2l = zalloc(sizeof(*a2l));
174 a2l->input = strdup(path);
175 if (a2l->input == NULL)
178 if (slurp_symtab(abfd, a2l))
185 zfree((char **)&a2l->input);
192 static void addr2line_cleanup(struct a2l_data *a2l)
195 bfd_close(a2l->abfd);
196 zfree((char **)&a2l->input);
201 #define MAX_INLINE_NEST 1024
203 static void inline_list__reverse(struct inline_node *node)
205 struct inline_list *ilist, *n;
207 list_for_each_entry_safe_reverse(ilist, n, &node->val, list)
208 list_move_tail(&ilist->list, &node->val);
211 static int addr2line(const char *dso_name, u64 addr,
212 char **file, unsigned int *line, struct dso *dso,
213 bool unwind_inlines, struct inline_node *node)
216 struct a2l_data *a2l = dso->a2l;
219 dso->a2l = addr2line_init(dso_name);
224 pr_warning("addr2line_init failed for %s\n", dso_name);
231 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
236 if (unwind_inlines) {
239 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
240 &a2l->funcname, &a2l->line) &&
241 cnt++ < MAX_INLINE_NEST) {
244 if (inline_list__append(strdup(a2l->filename),
245 strdup(a2l->funcname),
249 // found at least one inline frame
254 if ((node != NULL) &&
255 (callchain_param.order != ORDER_CALLEE)) {
256 inline_list__reverse(node);
261 *file = a2l->filename ? strdup(a2l->filename) : NULL;
271 void dso__free_a2l(struct dso *dso)
273 struct a2l_data *a2l = dso->a2l;
278 addr2line_cleanup(a2l);
283 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
286 struct inline_node *node;
288 node = zalloc(sizeof(*node));
290 perror("not enough memory for the inline node");
294 INIT_LIST_HEAD(&node->val);
297 if (!addr2line(dso_name, addr, NULL, NULL, dso, TRUE, node))
298 goto out_free_inline_node;
300 if (list_empty(&node->val))
301 goto out_free_inline_node;
305 out_free_inline_node:
306 inline_node__delete(node);
310 #else /* HAVE_LIBBFD_SUPPORT */
312 static int filename_split(char *filename, unsigned int *line_nr)
316 sep = strchr(filename, '\n');
320 if (!strcmp(filename, "??:0"))
323 sep = strchr(filename, ':');
326 *line_nr = strtoul(sep, NULL, 0);
333 static int addr2line(const char *dso_name, u64 addr,
334 char **file, unsigned int *line_nr,
335 struct dso *dso __maybe_unused,
336 bool unwind_inlines __maybe_unused,
337 struct inline_node *node __maybe_unused)
341 char *filename = NULL;
345 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
348 fp = popen(cmd, "r");
350 pr_warning("popen failed for %s\n", dso_name);
354 if (getline(&filename, &len, fp) < 0 || !len) {
355 pr_warning("addr2line has no output for %s\n", dso_name);
359 ret = filename_split(filename, line_nr);
372 void dso__free_a2l(struct dso *dso __maybe_unused)
376 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
377 struct dso *dso __maybe_unused)
381 struct inline_node *node;
382 char *filename = NULL;
384 unsigned int line_nr = 0;
386 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
389 fp = popen(cmd, "r");
391 pr_err("popen failed for %s\n", dso_name);
395 node = zalloc(sizeof(*node));
397 perror("not enough memory for the inline node");
401 INIT_LIST_HEAD(&node->val);
404 while (getline(&filename, &len, fp) != -1) {
405 if (filename_split(filename, &line_nr) != 1) {
410 if (inline_list__append(filename, NULL, line_nr, node,
420 if (list_empty(&node->val)) {
421 inline_node__delete(node);
428 #endif /* HAVE_LIBBFD_SUPPORT */
431 * Number of addr2line failures (without success) before disabling it for that
434 #define A2L_FAIL_LIMIT 123
436 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
437 bool show_sym, bool show_addr, bool unwind_inlines)
442 const char *dso_name;
444 if (!dso->has_srcline)
447 dso_name = dso__name(dso);
448 if (dso_name == NULL)
451 if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines, NULL))
454 if (asprintf(&srcline, "%s:%u",
455 srcline_full_filename ? file : basename(file),
467 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
468 dso->has_srcline = 0;
473 return (show_sym && sym) ?
474 strndup(sym->name, sym->namelen) : NULL;
477 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
478 addr - sym->start) < 0)
479 return SRCLINE_UNKNOWN;
480 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
481 return SRCLINE_UNKNOWN;
485 void free_srcline(char *srcline)
487 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
491 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
492 bool show_sym, bool show_addr)
494 return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
497 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr)
499 const char *dso_name;
501 dso_name = dso__name(dso);
502 if (dso_name == NULL)
505 return addr2inlines(dso_name, addr, dso);
508 void inline_node__delete(struct inline_node *node)
510 struct inline_list *ilist, *tmp;
512 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
513 list_del_init(&ilist->list);
514 zfree(&ilist->filename);
515 zfree(&ilist->funcname);