1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/kernel.h>
10 #include "util/util.h"
11 #include "util/debug.h"
12 #include "util/callchain.h"
17 bool srcline_full_filename;
19 static const char *dso__name(struct dso *dso)
23 if (dso->symsrc_filename)
24 dso_name = dso->symsrc_filename;
26 dso_name = dso->long_name;
28 if (dso_name[0] == '[')
31 if (!strncmp(dso_name, "/tmp/perf-", 10))
37 static int inline_list__append(char *filename, char *funcname, int line_nr,
38 struct inline_node *node, struct dso *dso)
40 struct inline_list *ilist;
43 ilist = zalloc(sizeof(*ilist));
47 ilist->filename = filename;
48 ilist->line_nr = line_nr;
51 demangled = dso__demangle_sym(dso, 0, funcname);
52 if (demangled == NULL) {
53 ilist->funcname = funcname;
55 ilist->funcname = demangled;
60 if (callchain_param.order == ORDER_CALLEE)
61 list_add_tail(&ilist->list, &node->val);
63 list_add(&ilist->list, &node->val);
68 #ifdef HAVE_LIBBFD_SUPPORT
71 * Implement addr2line using libbfd.
73 #define PACKAGE "perf"
89 static int bfd_error(const char *string)
93 errmsg = bfd_errmsg(bfd_get_error());
97 pr_debug("%s: %s\n", string, errmsg);
99 pr_debug("%s\n", errmsg);
104 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
109 bfd_boolean dynamic = FALSE;
111 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
112 return bfd_error(bfd_get_filename(abfd));
114 storage = bfd_get_symtab_upper_bound(abfd);
116 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
120 return bfd_error(bfd_get_filename(abfd));
122 syms = malloc(storage);
124 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
126 symcount = bfd_canonicalize_symtab(abfd, syms);
130 return bfd_error(bfd_get_filename(abfd));
137 static void find_address_in_section(bfd *abfd, asection *section, void *data)
141 struct a2l_data *a2l = data;
147 #ifdef bfd_get_section_flags
148 flags = bfd_get_section_flags(abfd, section);
150 flags = bfd_section_flags(section);
152 if ((flags & SEC_ALLOC) == 0)
156 #ifdef bfd_get_section_vma
157 vma = bfd_get_section_vma(abfd, section);
159 vma = bfd_section_vma(section);
161 #ifdef bfd_get_section_size
162 size = bfd_get_section_size(section);
164 size = bfd_section_size(section);
167 if (pc < vma || pc >= vma + size)
170 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
171 &a2l->filename, &a2l->funcname,
174 if (a2l->filename && !strlen(a2l->filename))
175 a2l->filename = NULL;
178 static struct a2l_data *addr2line_init(const char *path)
181 struct a2l_data *a2l = NULL;
183 abfd = bfd_openr(path, NULL);
187 if (!bfd_check_format(abfd, bfd_object))
190 a2l = zalloc(sizeof(*a2l));
195 a2l->input = strdup(path);
196 if (a2l->input == NULL)
199 if (slurp_symtab(abfd, a2l))
206 zfree((char **)&a2l->input);
213 static void addr2line_cleanup(struct a2l_data *a2l)
216 bfd_close(a2l->abfd);
217 zfree((char **)&a2l->input);
222 #define MAX_INLINE_NEST 1024
224 static int inline_list__append_dso_a2l(struct dso *dso,
225 struct inline_node *node)
227 struct a2l_data *a2l = dso->a2l;
228 char *funcname = a2l->funcname ? strdup(a2l->funcname) : NULL;
229 char *filename = a2l->filename ? strdup(a2l->filename) : NULL;
231 return inline_list__append(filename, funcname, a2l->line, node, dso);
234 static int addr2line(const char *dso_name, u64 addr,
235 char **file, unsigned int *line, struct dso *dso,
236 bool unwind_inlines, struct inline_node *node)
239 struct a2l_data *a2l = dso->a2l;
242 dso->a2l = addr2line_init(dso_name);
247 pr_warning("addr2line_init failed for %s\n", dso_name);
254 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
259 if (unwind_inlines) {
262 if (node && inline_list__append_dso_a2l(dso, node))
265 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
266 &a2l->funcname, &a2l->line) &&
267 cnt++ < MAX_INLINE_NEST) {
269 if (a2l->filename && !strlen(a2l->filename))
270 a2l->filename = NULL;
273 if (inline_list__append_dso_a2l(dso, node))
275 // found at least one inline frame
282 *file = a2l->filename ? strdup(a2l->filename) : NULL;
292 void dso__free_a2l(struct dso *dso)
294 struct a2l_data *a2l = dso->a2l;
299 addr2line_cleanup(a2l);
304 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
307 struct inline_node *node;
309 node = zalloc(sizeof(*node));
311 perror("not enough memory for the inline node");
315 INIT_LIST_HEAD(&node->val);
318 if (!addr2line(dso_name, addr, NULL, NULL, dso, TRUE, node))
319 goto out_free_inline_node;
321 if (list_empty(&node->val))
322 goto out_free_inline_node;
326 out_free_inline_node:
327 inline_node__delete(node);
331 #else /* HAVE_LIBBFD_SUPPORT */
333 static int filename_split(char *filename, unsigned int *line_nr)
337 sep = strchr(filename, '\n');
341 if (!strcmp(filename, "??:0"))
344 sep = strchr(filename, ':');
347 *line_nr = strtoul(sep, NULL, 0);
354 static int addr2line(const char *dso_name, u64 addr,
355 char **file, unsigned int *line_nr,
356 struct dso *dso __maybe_unused,
357 bool unwind_inlines __maybe_unused,
358 struct inline_node *node __maybe_unused)
362 char *filename = NULL;
366 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
369 fp = popen(cmd, "r");
371 pr_warning("popen failed for %s\n", dso_name);
375 if (getline(&filename, &len, fp) < 0 || !len) {
376 pr_warning("addr2line has no output for %s\n", dso_name);
380 ret = filename_split(filename, line_nr);
393 void dso__free_a2l(struct dso *dso __maybe_unused)
397 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
398 struct dso *dso __maybe_unused)
402 struct inline_node *node;
403 char *filename = NULL;
405 unsigned int line_nr = 0;
407 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
410 fp = popen(cmd, "r");
412 pr_err("popen failed for %s\n", dso_name);
416 node = zalloc(sizeof(*node));
418 perror("not enough memory for the inline node");
422 INIT_LIST_HEAD(&node->val);
425 while (getline(&filename, &len, fp) != -1) {
426 if (filename_split(filename, &line_nr) != 1) {
431 if (inline_list__append(filename, NULL, line_nr, node,
441 if (list_empty(&node->val)) {
442 inline_node__delete(node);
449 #endif /* HAVE_LIBBFD_SUPPORT */
452 * Number of addr2line failures (without success) before disabling it for that
455 #define A2L_FAIL_LIMIT 123
457 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
458 bool show_sym, bool show_addr, bool unwind_inlines)
463 const char *dso_name;
465 if (!dso->has_srcline)
468 dso_name = dso__name(dso);
469 if (dso_name == NULL)
472 if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines, NULL))
475 if (asprintf(&srcline, "%s:%u",
476 srcline_full_filename ? file : basename(file),
488 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
489 dso->has_srcline = 0;
494 return (show_sym && sym) ?
495 strndup(sym->name, sym->namelen) : NULL;
498 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
499 addr - sym->start) < 0)
500 return SRCLINE_UNKNOWN;
501 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
502 return SRCLINE_UNKNOWN;
506 void free_srcline(char *srcline)
508 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
512 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
513 bool show_sym, bool show_addr)
515 return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
518 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr)
520 const char *dso_name;
522 dso_name = dso__name(dso);
523 if (dso_name == NULL)
526 return addr2inlines(dso_name, addr, dso);
529 void inline_node__delete(struct inline_node *node)
531 struct inline_list *ilist, *tmp;
533 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
534 list_del_init(&ilist->list);
535 zfree(&ilist->filename);
536 zfree(&ilist->funcname);