5 #include <linux/kernel.h>
9 #include "util/debug.h"
13 #ifdef HAVE_LIBBFD_SUPPORT
16 * Implement addr2line using libbfd.
18 #define PACKAGE "perf"
34 static int bfd_error(const char *string
)
38 errmsg
= bfd_errmsg(bfd_get_error());
42 pr_debug("%s: %s\n", string
, errmsg
);
44 pr_debug("%s\n", errmsg
);
49 static int slurp_symtab(bfd
*abfd
, struct a2l_data
*a2l
)
54 bfd_boolean dynamic
= FALSE
;
56 if ((bfd_get_file_flags(abfd
) & HAS_SYMS
) == 0)
57 return bfd_error(bfd_get_filename(abfd
));
59 storage
= bfd_get_symtab_upper_bound(abfd
);
61 storage
= bfd_get_dynamic_symtab_upper_bound(abfd
);
65 return bfd_error(bfd_get_filename(abfd
));
67 syms
= malloc(storage
);
69 symcount
= bfd_canonicalize_dynamic_symtab(abfd
, syms
);
71 symcount
= bfd_canonicalize_symtab(abfd
, syms
);
75 return bfd_error(bfd_get_filename(abfd
));
82 static void find_address_in_section(bfd
*abfd
, asection
*section
, void *data
)
86 struct a2l_data
*a2l
= data
;
91 if ((bfd_get_section_flags(abfd
, section
) & SEC_ALLOC
) == 0)
95 vma
= bfd_get_section_vma(abfd
, section
);
96 size
= bfd_get_section_size(section
);
98 if (pc
< vma
|| pc
>= vma
+ size
)
101 a2l
->found
= bfd_find_nearest_line(abfd
, section
, a2l
->syms
, pc
- vma
,
102 &a2l
->filename
, &a2l
->funcname
,
106 static struct a2l_data
*addr2line_init(const char *path
)
109 struct a2l_data
*a2l
= NULL
;
111 abfd
= bfd_openr(path
, NULL
);
115 if (!bfd_check_format(abfd
, bfd_object
))
118 a2l
= zalloc(sizeof(*a2l
));
123 a2l
->input
= strdup(path
);
124 if (a2l
->input
== NULL
)
127 if (slurp_symtab(abfd
, a2l
))
134 zfree((char **)&a2l
->input
);
141 static void addr2line_cleanup(struct a2l_data
*a2l
)
144 bfd_close(a2l
->abfd
);
145 zfree((char **)&a2l
->input
);
150 static int addr2line(const char *dso_name
, u64 addr
,
151 char **file
, unsigned int *line
, struct dso
*dso
)
154 struct a2l_data
*a2l
= dso
->a2l
;
157 dso
->a2l
= addr2line_init(dso_name
);
162 pr_warning("addr2line_init failed for %s\n", dso_name
);
169 bfd_map_over_sections(a2l
->abfd
, find_address_in_section
, a2l
);
171 if (a2l
->found
&& a2l
->filename
) {
172 *file
= strdup(a2l
->filename
);
182 void dso__free_a2l(struct dso
*dso
)
184 struct a2l_data
*a2l
= dso
->a2l
;
189 addr2line_cleanup(a2l
);
194 #else /* HAVE_LIBBFD_SUPPORT */
196 static int addr2line(const char *dso_name
, u64 addr
,
197 char **file
, unsigned int *line_nr
,
198 struct dso
*dso __maybe_unused
)
202 char *filename
= NULL
;
207 scnprintf(cmd
, sizeof(cmd
), "addr2line -e %s %016"PRIx64
,
210 fp
= popen(cmd
, "r");
212 pr_warning("popen failed for %s\n", dso_name
);
216 if (getline(&filename
, &len
, fp
) < 0 || !len
) {
217 pr_warning("addr2line has no output for %s\n", dso_name
);
221 sep
= strchr(filename
, '\n');
225 if (!strcmp(filename
, "??:0")) {
226 pr_debug("no debugging info in %s\n", dso_name
);
231 sep
= strchr(filename
, ':');
235 *line_nr
= strtoul(sep
, NULL
, 0);
243 void dso__free_a2l(struct dso
*dso __maybe_unused
)
247 #endif /* HAVE_LIBBFD_SUPPORT */
250 * Number of addr2line failures (without success) before disabling it for that
253 #define A2L_FAIL_LIMIT 123
255 char *get_srcline(struct dso
*dso
, u64 addr
, struct symbol
*sym
,
261 const char *dso_name
;
263 if (!dso
->has_srcline
)
266 if (dso
->symsrc_filename
)
267 dso_name
= dso
->symsrc_filename
;
269 dso_name
= dso
->long_name
;
271 if (dso_name
[0] == '[')
274 if (!strncmp(dso_name
, "/tmp/perf-", 10))
277 if (!addr2line(dso_name
, addr
, &file
, &line
, dso
))
280 if (asprintf(&srcline
, "%s:%u", basename(file
), line
) < 0) {
291 if (dso
->a2l_fails
&& ++dso
->a2l_fails
> A2L_FAIL_LIMIT
) {
292 dso
->has_srcline
= 0;
296 if (asprintf(&srcline
, "%s+%" PRIu64
, show_sym
? sym
->name
: "",
297 addr
- sym
->start
) < 0)
298 return SRCLINE_UNKNOWN
;
299 } else if (asprintf(&srcline
, "%s[%" PRIx64
"]", dso
->short_name
, addr
) < 0)
300 return SRCLINE_UNKNOWN
;
304 void free_srcline(char *srcline
)
306 if (srcline
&& strcmp(srcline
, SRCLINE_UNKNOWN
) != 0)