5 #include <linux/kernel.h>
9 #include "util/debug.h"
13 bool srcline_full_filename;
15 static const char *dso__name(struct dso *dso)
19 if (dso->symsrc_filename)
20 dso_name = dso->symsrc_filename;
22 dso_name = dso->long_name;
24 if (dso_name[0] == '[')
27 if (!strncmp(dso_name, "/tmp/perf-", 10))
33 #ifdef HAVE_LIBBFD_SUPPORT
36 * Implement addr2line using libbfd.
38 #define PACKAGE "perf"
54 static int bfd_error(const char *string)
58 errmsg = bfd_errmsg(bfd_get_error());
62 pr_debug("%s: %s\n", string, errmsg);
64 pr_debug("%s\n", errmsg);
69 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
74 bfd_boolean dynamic = FALSE;
76 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
77 return bfd_error(bfd_get_filename(abfd));
79 storage = bfd_get_symtab_upper_bound(abfd);
81 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
85 return bfd_error(bfd_get_filename(abfd));
87 syms = malloc(storage);
89 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
91 symcount = bfd_canonicalize_symtab(abfd, syms);
95 return bfd_error(bfd_get_filename(abfd));
102 static void find_address_in_section(bfd *abfd, asection *section, void *data)
106 struct a2l_data *a2l = data;
111 if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
115 vma = bfd_get_section_vma(abfd, section);
116 size = bfd_get_section_size(section);
118 if (pc < vma || pc >= vma + size)
121 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
122 &a2l->filename, &a2l->funcname,
126 static struct a2l_data *addr2line_init(const char *path)
129 struct a2l_data *a2l = NULL;
131 abfd = bfd_openr(path, NULL);
135 if (!bfd_check_format(abfd, bfd_object))
138 a2l = zalloc(sizeof(*a2l));
143 a2l->input = strdup(path);
144 if (a2l->input == NULL)
147 if (slurp_symtab(abfd, a2l))
154 zfree((char **)&a2l->input);
161 static void addr2line_cleanup(struct a2l_data *a2l)
164 bfd_close(a2l->abfd);
165 zfree((char **)&a2l->input);
170 #define MAX_INLINE_NEST 1024
172 static int addr2line(const char *dso_name, u64 addr,
173 char **file, unsigned int *line, struct dso *dso,
177 struct a2l_data *a2l = dso->a2l;
180 dso->a2l = addr2line_init(dso_name);
185 pr_warning("addr2line_init failed for %s\n", dso_name);
192 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
194 if (a2l->found && unwind_inlines) {
197 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
198 &a2l->funcname, &a2l->line) &&
199 cnt++ < MAX_INLINE_NEST)
203 if (a2l->found && a2l->filename) {
204 *file = strdup(a2l->filename);
214 void dso__free_a2l(struct dso *dso)
216 struct a2l_data *a2l = dso->a2l;
221 addr2line_cleanup(a2l);
226 #else /* HAVE_LIBBFD_SUPPORT */
228 static int filename_split(char *filename, unsigned int *line_nr)
232 sep = strchr(filename, '\n');
236 if (!strcmp(filename, "??:0"))
239 sep = strchr(filename, ':');
242 *line_nr = strtoul(sep, NULL, 0);
249 static int addr2line(const char *dso_name, u64 addr,
250 char **file, unsigned int *line_nr,
251 struct dso *dso __maybe_unused,
252 bool unwind_inlines __maybe_unused)
256 char *filename = NULL;
260 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
263 fp = popen(cmd, "r");
265 pr_warning("popen failed for %s\n", dso_name);
269 if (getline(&filename, &len, fp) < 0 || !len) {
270 pr_warning("addr2line has no output for %s\n", dso_name);
274 ret = filename_split(filename, line_nr);
287 void dso__free_a2l(struct dso *dso __maybe_unused)
291 #endif /* HAVE_LIBBFD_SUPPORT */
294 * Number of addr2line failures (without success) before disabling it for that
297 #define A2L_FAIL_LIMIT 123
299 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
300 bool show_sym, bool unwind_inlines)
305 const char *dso_name;
307 if (!dso->has_srcline)
310 dso_name = dso__name(dso);
311 if (dso_name == NULL)
314 if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines))
317 if (asprintf(&srcline, "%s:%u",
318 srcline_full_filename ? file : basename(file),
330 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
331 dso->has_srcline = 0;
335 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
336 addr - sym->start) < 0)
337 return SRCLINE_UNKNOWN;
338 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
339 return SRCLINE_UNKNOWN;
343 void free_srcline(char *srcline)
345 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
349 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
352 return __get_srcline(dso, addr, sym, show_sym, false);