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 list_add_tail(&ilist
->list
, &node
->val
);
64 #ifdef HAVE_LIBBFD_SUPPORT
67 * Implement addr2line using libbfd.
69 #define PACKAGE "perf"
85 static int bfd_error(const char *string
)
89 errmsg
= bfd_errmsg(bfd_get_error());
93 pr_debug("%s: %s\n", string
, errmsg
);
95 pr_debug("%s\n", errmsg
);
100 static int slurp_symtab(bfd
*abfd
, struct a2l_data
*a2l
)
105 bfd_boolean dynamic
= FALSE
;
107 if ((bfd_get_file_flags(abfd
) & HAS_SYMS
) == 0)
108 return bfd_error(bfd_get_filename(abfd
));
110 storage
= bfd_get_symtab_upper_bound(abfd
);
112 storage
= bfd_get_dynamic_symtab_upper_bound(abfd
);
116 return bfd_error(bfd_get_filename(abfd
));
118 syms
= malloc(storage
);
120 symcount
= bfd_canonicalize_dynamic_symtab(abfd
, syms
);
122 symcount
= bfd_canonicalize_symtab(abfd
, syms
);
126 return bfd_error(bfd_get_filename(abfd
));
133 static void find_address_in_section(bfd
*abfd
, asection
*section
, void *data
)
137 struct a2l_data
*a2l
= data
;
142 if ((bfd_get_section_flags(abfd
, section
) & SEC_ALLOC
) == 0)
146 vma
= bfd_get_section_vma(abfd
, section
);
147 size
= bfd_get_section_size(section
);
149 if (pc
< vma
|| pc
>= vma
+ size
)
152 a2l
->found
= bfd_find_nearest_line(abfd
, section
, a2l
->syms
, pc
- vma
,
153 &a2l
->filename
, &a2l
->funcname
,
157 static struct a2l_data
*addr2line_init(const char *path
)
160 struct a2l_data
*a2l
= NULL
;
162 abfd
= bfd_openr(path
, NULL
);
166 if (!bfd_check_format(abfd
, bfd_object
))
169 a2l
= zalloc(sizeof(*a2l
));
174 a2l
->input
= strdup(path
);
175 if (a2l
->input
== NULL
)
178 if (slurp_symtab(abfd
, a2l
))
185 zfree((char **)&a2l
->input
);
192 static void addr2line_cleanup(struct a2l_data
*a2l
)
195 bfd_close(a2l
->abfd
);
196 zfree((char **)&a2l
->input
);
201 #define MAX_INLINE_NEST 1024
203 static void inline_list__reverse(struct inline_node
*node
)
205 struct inline_list
*ilist
, *n
;
207 list_for_each_entry_safe_reverse(ilist
, n
, &node
->val
, list
)
208 list_move_tail(&ilist
->list
, &node
->val
);
211 static int addr2line(const char *dso_name
, u64 addr
,
212 char **file
, unsigned int *line
, struct dso
*dso
,
213 bool unwind_inlines
, struct inline_node
*node
)
216 struct a2l_data
*a2l
= dso
->a2l
;
219 dso
->a2l
= addr2line_init(dso_name
);
224 pr_warning("addr2line_init failed for %s\n", dso_name
);
231 bfd_map_over_sections(a2l
->abfd
, find_address_in_section
, a2l
);
233 if (a2l
->found
&& unwind_inlines
) {
236 while (bfd_find_inliner_info(a2l
->abfd
, &a2l
->filename
,
237 &a2l
->funcname
, &a2l
->line
) &&
238 cnt
++ < MAX_INLINE_NEST
) {
241 if (inline_list__append(strdup(a2l
->filename
),
242 strdup(a2l
->funcname
),
249 if ((node
!= NULL
) &&
250 (callchain_param
.order
!= ORDER_CALLEE
)) {
251 inline_list__reverse(node
);
255 if (a2l
->found
&& a2l
->filename
) {
256 *file
= strdup(a2l
->filename
);
266 void dso__free_a2l(struct dso
*dso
)
268 struct a2l_data
*a2l
= dso
->a2l
;
273 addr2line_cleanup(a2l
);
278 static struct inline_node
*addr2inlines(const char *dso_name
, u64 addr
,
282 unsigned int line
= 0;
283 struct inline_node
*node
;
285 node
= zalloc(sizeof(*node
));
287 perror("not enough memory for the inline node");
291 INIT_LIST_HEAD(&node
->val
);
294 if (!addr2line(dso_name
, addr
, &file
, &line
, dso
, TRUE
, node
))
295 goto out_free_inline_node
;
297 if (list_empty(&node
->val
))
298 goto out_free_inline_node
;
302 out_free_inline_node
:
303 inline_node__delete(node
);
307 #else /* HAVE_LIBBFD_SUPPORT */
309 static int filename_split(char *filename
, unsigned int *line_nr
)
313 sep
= strchr(filename
, '\n');
317 if (!strcmp(filename
, "??:0"))
320 sep
= strchr(filename
, ':');
323 *line_nr
= strtoul(sep
, NULL
, 0);
330 static int addr2line(const char *dso_name
, u64 addr
,
331 char **file
, unsigned int *line_nr
,
332 struct dso
*dso __maybe_unused
,
333 bool unwind_inlines __maybe_unused
,
334 struct inline_node
*node __maybe_unused
)
338 char *filename
= NULL
;
342 scnprintf(cmd
, sizeof(cmd
), "addr2line -e %s %016"PRIx64
,
345 fp
= popen(cmd
, "r");
347 pr_warning("popen failed for %s\n", dso_name
);
351 if (getline(&filename
, &len
, fp
) < 0 || !len
) {
352 pr_warning("addr2line has no output for %s\n", dso_name
);
356 ret
= filename_split(filename
, line_nr
);
369 void dso__free_a2l(struct dso
*dso __maybe_unused
)
373 static struct inline_node
*addr2inlines(const char *dso_name
, u64 addr
,
374 struct dso
*dso __maybe_unused
)
378 struct inline_node
*node
;
379 char *filename
= NULL
;
381 unsigned int line_nr
= 0;
383 scnprintf(cmd
, sizeof(cmd
), "addr2line -e %s -i %016"PRIx64
,
386 fp
= popen(cmd
, "r");
388 pr_err("popen failed for %s\n", dso_name
);
392 node
= zalloc(sizeof(*node
));
394 perror("not enough memory for the inline node");
398 INIT_LIST_HEAD(&node
->val
);
401 while (getline(&filename
, &len
, fp
) != -1) {
402 if (filename_split(filename
, &line_nr
) != 1) {
407 if (inline_list__append(filename
, NULL
, line_nr
, node
,
417 if (list_empty(&node
->val
)) {
418 inline_node__delete(node
);
425 #endif /* HAVE_LIBBFD_SUPPORT */
428 * Number of addr2line failures (without success) before disabling it for that
431 #define A2L_FAIL_LIMIT 123
433 char *__get_srcline(struct dso
*dso
, u64 addr
, struct symbol
*sym
,
434 bool show_sym
, bool show_addr
, bool unwind_inlines
)
439 const char *dso_name
;
441 if (!dso
->has_srcline
)
444 dso_name
= dso__name(dso
);
445 if (dso_name
== NULL
)
448 if (!addr2line(dso_name
, addr
, &file
, &line
, dso
, unwind_inlines
, NULL
))
451 if (asprintf(&srcline
, "%s:%u",
452 srcline_full_filename
? file
: basename(file
),
464 if (dso
->a2l_fails
&& ++dso
->a2l_fails
> A2L_FAIL_LIMIT
) {
465 dso
->has_srcline
= 0;
470 return (show_sym
&& sym
) ?
471 strndup(sym
->name
, sym
->namelen
) : NULL
;
474 if (asprintf(&srcline
, "%s+%" PRIu64
, show_sym
? sym
->name
: "",
475 addr
- sym
->start
) < 0)
476 return SRCLINE_UNKNOWN
;
477 } else if (asprintf(&srcline
, "%s[%" PRIx64
"]", dso
->short_name
, addr
) < 0)
478 return SRCLINE_UNKNOWN
;
482 void free_srcline(char *srcline
)
484 if (srcline
&& strcmp(srcline
, SRCLINE_UNKNOWN
) != 0)
488 char *get_srcline(struct dso
*dso
, u64 addr
, struct symbol
*sym
,
489 bool show_sym
, bool show_addr
)
491 return __get_srcline(dso
, addr
, sym
, show_sym
, show_addr
, false);
494 struct inline_node
*dso__parse_addr_inlines(struct dso
*dso
, u64 addr
)
496 const char *dso_name
;
498 dso_name
= dso__name(dso
);
499 if (dso_name
== NULL
)
502 return addr2inlines(dso_name
, addr
, dso
);
505 void inline_node__delete(struct inline_node
*node
)
507 struct inline_list
*ilist
, *tmp
;
509 list_for_each_entry_safe(ilist
, tmp
, &node
->val
, list
) {
510 list_del_init(&ilist
->list
);
511 zfree(&ilist
->filename
);
512 zfree(&ilist
->funcname
);