5 #include <linux/kernel.h>
9 #include "util/debug.h"
13 bool srcline_full_filename;
15 #ifdef HAVE_LIBBFD_SUPPORT
18 * Implement addr2line using libbfd.
20 #define PACKAGE "perf"
36 static int bfd_error(const char *string)
40 errmsg = bfd_errmsg(bfd_get_error());
44 pr_debug("%s: %s\n", string, errmsg);
46 pr_debug("%s\n", errmsg);
51 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
56 bfd_boolean dynamic = FALSE;
58 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
59 return bfd_error(bfd_get_filename(abfd));
61 storage = bfd_get_symtab_upper_bound(abfd);
63 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
67 return bfd_error(bfd_get_filename(abfd));
69 syms = malloc(storage);
71 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
73 symcount = bfd_canonicalize_symtab(abfd, syms);
77 return bfd_error(bfd_get_filename(abfd));
84 static void find_address_in_section(bfd *abfd, asection *section, void *data)
88 struct a2l_data *a2l = data;
94 #ifdef bfd_get_section_flags
95 flags = bfd_get_section_flags(abfd, section);
97 flags = bfd_section_flags(section);
99 if ((flags & SEC_ALLOC) == 0)
103 #ifdef bfd_get_section_vma
104 vma = bfd_get_section_vma(abfd, section);
106 vma = bfd_section_vma(section);
108 #ifdef bfd_get_section_size
109 size = bfd_get_section_size(section);
111 size = bfd_section_size(section);
114 if (pc < vma || pc >= vma + size)
117 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
118 &a2l->filename, &a2l->funcname,
122 static struct a2l_data *addr2line_init(const char *path)
125 struct a2l_data *a2l = NULL;
127 abfd = bfd_openr(path, NULL);
131 if (!bfd_check_format(abfd, bfd_object))
134 a2l = zalloc(sizeof(*a2l));
139 a2l->input = strdup(path);
140 if (a2l->input == NULL)
143 if (slurp_symtab(abfd, a2l))
150 zfree((char **)&a2l->input);
157 static void addr2line_cleanup(struct a2l_data *a2l)
160 bfd_close(a2l->abfd);
161 zfree((char **)&a2l->input);
166 #define MAX_INLINE_NEST 1024
168 static int addr2line(const char *dso_name, u64 addr,
169 char **file, unsigned int *line, struct dso *dso,
173 struct a2l_data *a2l = dso->a2l;
176 dso->a2l = addr2line_init(dso_name);
181 pr_warning("addr2line_init failed for %s\n", dso_name);
188 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
190 if (a2l->found && unwind_inlines) {
193 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
194 &a2l->funcname, &a2l->line) &&
195 cnt++ < MAX_INLINE_NEST)
199 if (a2l->found && a2l->filename) {
200 *file = strdup(a2l->filename);
210 void dso__free_a2l(struct dso *dso)
212 struct a2l_data *a2l = dso->a2l;
217 addr2line_cleanup(a2l);
222 #else /* HAVE_LIBBFD_SUPPORT */
224 static int addr2line(const char *dso_name, u64 addr,
225 char **file, unsigned int *line_nr,
226 struct dso *dso __maybe_unused,
227 bool unwind_inlines __maybe_unused)
231 char *filename = NULL;
236 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
239 fp = popen(cmd, "r");
241 pr_warning("popen failed for %s\n", dso_name);
245 if (getline(&filename, &len, fp) < 0 || !len) {
246 pr_warning("addr2line has no output for %s\n", dso_name);
250 sep = strchr(filename, '\n');
254 if (!strcmp(filename, "??:0")) {
255 pr_debug("no debugging info in %s\n", dso_name);
260 sep = strchr(filename, ':');
264 *line_nr = strtoul(sep, NULL, 0);
272 void dso__free_a2l(struct dso *dso __maybe_unused)
276 #endif /* HAVE_LIBBFD_SUPPORT */
279 * Number of addr2line failures (without success) before disabling it for that
282 #define A2L_FAIL_LIMIT 123
284 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
285 bool show_sym, bool unwind_inlines)
290 const char *dso_name;
292 if (!dso->has_srcline)
295 if (dso->symsrc_filename)
296 dso_name = dso->symsrc_filename;
298 dso_name = dso->long_name;
300 if (dso_name[0] == '[')
303 if (!strncmp(dso_name, "/tmp/perf-", 10))
306 if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines))
309 if (asprintf(&srcline, "%s:%u",
310 srcline_full_filename ? file : basename(file),
322 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
323 dso->has_srcline = 0;
327 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
328 addr - sym->start) < 0)
329 return SRCLINE_UNKNOWN;
330 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
331 return SRCLINE_UNKNOWN;
335 void free_srcline(char *srcline)
337 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
341 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
344 return __get_srcline(dso, addr, sym, show_sym, false);