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 if (callchain_param.order == ORDER_CALLEE)
60 list_add_tail(&ilist->list, &node->val);
62 list_add(&ilist->list, &node->val);
67 #ifdef HAVE_LIBBFD_SUPPORT
70 * Implement addr2line using libbfd.
72 #define PACKAGE "perf"
88 static int bfd_error(const char *string)
92 errmsg = bfd_errmsg(bfd_get_error());
96 pr_debug("%s: %s\n", string, errmsg);
98 pr_debug("%s\n", errmsg);
103 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
108 bfd_boolean dynamic = FALSE;
110 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
111 return bfd_error(bfd_get_filename(abfd));
113 storage = bfd_get_symtab_upper_bound(abfd);
115 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
119 return bfd_error(bfd_get_filename(abfd));
121 syms = malloc(storage);
123 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
125 symcount = bfd_canonicalize_symtab(abfd, syms);
129 return bfd_error(bfd_get_filename(abfd));
136 static void find_address_in_section(bfd *abfd, asection *section, void *data)
140 struct a2l_data *a2l = data;
145 if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
149 vma = bfd_get_section_vma(abfd, section);
150 size = bfd_get_section_size(section);
152 if (pc < vma || pc >= vma + size)
155 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
156 &a2l->filename, &a2l->funcname,
159 if (a2l->filename && !strlen(a2l->filename))
160 a2l->filename = NULL;
163 static struct a2l_data *addr2line_init(const char *path)
166 struct a2l_data *a2l = NULL;
168 abfd = bfd_openr(path, NULL);
172 if (!bfd_check_format(abfd, bfd_object))
175 a2l = zalloc(sizeof(*a2l));
180 a2l->input = strdup(path);
181 if (a2l->input == NULL)
184 if (slurp_symtab(abfd, a2l))
191 zfree((char **)&a2l->input);
198 static void addr2line_cleanup(struct a2l_data *a2l)
201 bfd_close(a2l->abfd);
202 zfree((char **)&a2l->input);
207 #define MAX_INLINE_NEST 1024
209 static int inline_list__append_dso_a2l(struct dso *dso,
210 struct inline_node *node)
212 struct a2l_data *a2l = dso->a2l;
213 char *funcname = a2l->funcname ? strdup(a2l->funcname) : NULL;
214 char *filename = a2l->filename ? strdup(a2l->filename) : NULL;
216 return inline_list__append(filename, funcname, a2l->line, node, dso);
219 static int addr2line(const char *dso_name, u64 addr,
220 char **file, unsigned int *line, struct dso *dso,
221 bool unwind_inlines, struct inline_node *node)
224 struct a2l_data *a2l = dso->a2l;
227 dso->a2l = addr2line_init(dso_name);
232 pr_warning("addr2line_init failed for %s\n", dso_name);
239 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
244 if (unwind_inlines) {
247 if (node && inline_list__append_dso_a2l(dso, node))
250 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
251 &a2l->funcname, &a2l->line) &&
252 cnt++ < MAX_INLINE_NEST) {
254 if (a2l->filename && !strlen(a2l->filename))
255 a2l->filename = NULL;
258 if (inline_list__append_dso_a2l(dso, node))
260 // found at least one inline frame
267 *file = a2l->filename ? strdup(a2l->filename) : NULL;
277 void dso__free_a2l(struct dso *dso)
279 struct a2l_data *a2l = dso->a2l;
284 addr2line_cleanup(a2l);
289 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
292 struct inline_node *node;
294 node = zalloc(sizeof(*node));
296 perror("not enough memory for the inline node");
300 INIT_LIST_HEAD(&node->val);
303 if (!addr2line(dso_name, addr, NULL, NULL, dso, TRUE, node))
304 goto out_free_inline_node;
306 if (list_empty(&node->val))
307 goto out_free_inline_node;
311 out_free_inline_node:
312 inline_node__delete(node);
316 #else /* HAVE_LIBBFD_SUPPORT */
318 static int filename_split(char *filename, unsigned int *line_nr)
322 sep = strchr(filename, '\n');
326 if (!strcmp(filename, "??:0"))
329 sep = strchr(filename, ':');
332 *line_nr = strtoul(sep, NULL, 0);
339 static int addr2line(const char *dso_name, u64 addr,
340 char **file, unsigned int *line_nr,
341 struct dso *dso __maybe_unused,
342 bool unwind_inlines __maybe_unused,
343 struct inline_node *node __maybe_unused)
347 char *filename = NULL;
351 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
354 fp = popen(cmd, "r");
356 pr_warning("popen failed for %s\n", dso_name);
360 if (getline(&filename, &len, fp) < 0 || !len) {
361 pr_warning("addr2line has no output for %s\n", dso_name);
365 ret = filename_split(filename, line_nr);
378 void dso__free_a2l(struct dso *dso __maybe_unused)
382 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
383 struct dso *dso __maybe_unused)
387 struct inline_node *node;
388 char *filename = NULL;
390 unsigned int line_nr = 0;
392 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
395 fp = popen(cmd, "r");
397 pr_err("popen failed for %s\n", dso_name);
401 node = zalloc(sizeof(*node));
403 perror("not enough memory for the inline node");
407 INIT_LIST_HEAD(&node->val);
410 while (getline(&filename, &len, fp) != -1) {
411 if (filename_split(filename, &line_nr) != 1) {
416 if (inline_list__append(filename, NULL, line_nr, node,
426 if (list_empty(&node->val)) {
427 inline_node__delete(node);
434 #endif /* HAVE_LIBBFD_SUPPORT */
437 * Number of addr2line failures (without success) before disabling it for that
440 #define A2L_FAIL_LIMIT 123
442 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
443 bool show_sym, bool show_addr, bool unwind_inlines)
448 const char *dso_name;
450 if (!dso->has_srcline)
453 dso_name = dso__name(dso);
454 if (dso_name == NULL)
457 if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines, NULL))
460 if (asprintf(&srcline, "%s:%u",
461 srcline_full_filename ? file : basename(file),
473 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
474 dso->has_srcline = 0;
479 return (show_sym && sym) ?
480 strndup(sym->name, sym->namelen) : NULL;
483 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
484 addr - sym->start) < 0)
485 return SRCLINE_UNKNOWN;
486 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
487 return SRCLINE_UNKNOWN;
491 void free_srcline(char *srcline)
493 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
497 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
498 bool show_sym, bool show_addr)
500 return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
503 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr)
505 const char *dso_name;
507 dso_name = dso__name(dso);
508 if (dso_name == NULL)
511 return addr2inlines(dso_name, addr, dso);
514 void inline_node__delete(struct inline_node *node)
516 struct inline_list *ilist, *tmp;
518 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
519 list_del_init(&ilist->list);
520 zfree(&ilist->filename);
521 zfree(&ilist->funcname);