1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/zalloc.h>
14 #include "util/debug.h"
15 #include "util/callchain.h"
16 #include "util/symbol_conf.h"
20 #include "subcmd/run-command.h"
22 bool srcline_full_filename;
24 static const char *dso__name(struct dso *dso)
28 if (dso->symsrc_filename)
29 dso_name = dso->symsrc_filename;
31 dso_name = dso->long_name;
33 if (dso_name[0] == '[')
36 if (!strncmp(dso_name, "/tmp/perf-", 10))
42 static int inline_list__append(struct symbol *symbol, char *srcline,
43 struct inline_node *node)
45 struct inline_list *ilist;
47 ilist = zalloc(sizeof(*ilist));
51 ilist->symbol = symbol;
52 ilist->srcline = srcline;
54 if (callchain_param.order == ORDER_CALLEE)
55 list_add_tail(&ilist->list, &node->val);
57 list_add(&ilist->list, &node->val);
62 /* basename version that takes a const input string */
63 static const char *gnu_basename(const char *path)
65 const char *base = strrchr(path, '/');
67 return base ? base + 1 : path;
70 static char *srcline_from_fileline(const char *file, unsigned int line)
77 if (!srcline_full_filename)
78 file = gnu_basename(file);
80 if (asprintf(&srcline, "%s:%u", file, line) < 0)
86 static struct symbol *new_inline_sym(struct dso *dso,
87 struct symbol *base_sym,
90 struct symbol *inline_sym;
91 char *demangled = NULL;
97 demangled = dso__demangle_sym(dso, 0, funcname);
102 if (base_sym && strcmp(funcname, base_sym->name) == 0) {
103 /* reuse the real, existing symbol */
104 inline_sym = base_sym;
105 /* ensure that we don't alias an inlined symbol, which could
106 * lead to double frees in inline_node__delete
108 assert(!base_sym->inlined);
110 /* create a fake symbol for the inline frame */
111 inline_sym = symbol__new(base_sym ? base_sym->start : 0,
112 base_sym ? (base_sym->end - base_sym->start) : 0,
113 base_sym ? base_sym->binding : 0,
114 base_sym ? base_sym->type : 0,
117 inline_sym->inlined = 1;
125 #define MAX_INLINE_NEST 1024
127 #ifdef HAVE_LIBBFD_SUPPORT
130 * Implement addr2line using libbfd.
132 #define PACKAGE "perf"
140 const char *filename;
141 const char *funcname;
148 static int bfd_error(const char *string)
152 errmsg = bfd_errmsg(bfd_get_error());
156 pr_debug("%s: %s\n", string, errmsg);
158 pr_debug("%s\n", errmsg);
163 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
168 bfd_boolean dynamic = FALSE;
170 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
171 return bfd_error(bfd_get_filename(abfd));
173 storage = bfd_get_symtab_upper_bound(abfd);
175 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
179 return bfd_error(bfd_get_filename(abfd));
181 syms = malloc(storage);
183 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
185 symcount = bfd_canonicalize_symtab(abfd, syms);
189 return bfd_error(bfd_get_filename(abfd));
196 static void find_address_in_section(bfd *abfd, asection *section, void *data)
200 struct a2l_data *a2l = data;
206 #ifdef bfd_get_section_flags
207 flags = bfd_get_section_flags(abfd, section);
209 flags = bfd_section_flags(section);
211 if ((flags & SEC_ALLOC) == 0)
215 #ifdef bfd_get_section_vma
216 vma = bfd_get_section_vma(abfd, section);
218 vma = bfd_section_vma(section);
220 #ifdef bfd_get_section_size
221 size = bfd_get_section_size(section);
223 size = bfd_section_size(section);
226 if (pc < vma || pc >= vma + size)
229 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
230 &a2l->filename, &a2l->funcname,
233 if (a2l->filename && !strlen(a2l->filename))
234 a2l->filename = NULL;
237 static struct a2l_data *addr2line_init(const char *path)
240 struct a2l_data *a2l = NULL;
242 abfd = bfd_openr(path, NULL);
246 if (!bfd_check_format(abfd, bfd_object))
249 a2l = zalloc(sizeof(*a2l));
254 a2l->input = strdup(path);
255 if (a2l->input == NULL)
258 if (slurp_symtab(abfd, a2l))
265 zfree((char **)&a2l->input);
272 static void addr2line_cleanup(struct a2l_data *a2l)
275 bfd_close(a2l->abfd);
276 zfree((char **)&a2l->input);
281 static int inline_list__append_dso_a2l(struct dso *dso,
282 struct inline_node *node,
285 struct a2l_data *a2l = dso->a2l;
286 struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
287 char *srcline = NULL;
290 srcline = srcline_from_fileline(a2l->filename, a2l->line);
292 return inline_list__append(inline_sym, srcline, node);
295 static int addr2line(const char *dso_name, u64 addr,
296 char **file, unsigned int *line, struct dso *dso,
297 bool unwind_inlines, struct inline_node *node,
301 struct a2l_data *a2l = dso->a2l;
304 dso->a2l = addr2line_init(dso_name);
309 if (!symbol_conf.disable_add2line_warn)
310 pr_warning("addr2line_init failed for %s\n", dso_name);
317 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
322 if (unwind_inlines) {
325 if (node && inline_list__append_dso_a2l(dso, node, sym))
328 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
329 &a2l->funcname, &a2l->line) &&
330 cnt++ < MAX_INLINE_NEST) {
332 if (a2l->filename && !strlen(a2l->filename))
333 a2l->filename = NULL;
336 if (inline_list__append_dso_a2l(dso, node, sym))
338 // found at least one inline frame
345 *file = a2l->filename ? strdup(a2l->filename) : NULL;
355 void dso__free_a2l(struct dso *dso)
357 struct a2l_data *a2l = dso->a2l;
362 addr2line_cleanup(a2l);
367 #else /* HAVE_LIBBFD_SUPPORT */
369 struct a2l_subprocess {
370 struct child_process addr2line;
375 static int filename_split(char *filename, unsigned int *line_nr)
379 sep = strchr(filename, '\n');
383 if (!strcmp(filename, "??:0"))
386 sep = strchr(filename, ':');
389 *line_nr = strtoul(sep, NULL, 0);
396 static void addr2line_subprocess_cleanup(struct a2l_subprocess *a2l)
398 if (a2l->addr2line.pid != -1) {
399 kill(a2l->addr2line.pid, SIGKILL);
400 finish_command(&a2l->addr2line); /* ignore result, we don't care */
401 a2l->addr2line.pid = -1;
404 if (a2l->to_child != NULL) {
405 fclose(a2l->to_child);
406 a2l->to_child = NULL;
409 if (a2l->from_child != NULL) {
410 fclose(a2l->from_child);
411 a2l->from_child = NULL;
417 static struct a2l_subprocess *addr2line_subprocess_init(const char *path)
419 const char *argv[] = { "addr2line", "-e", path, "-i", "-f", NULL };
420 struct a2l_subprocess *a2l = zalloc(sizeof(*a2l));
421 int start_command_status = 0;
426 a2l->to_child = NULL;
427 a2l->from_child = NULL;
429 a2l->addr2line.pid = -1;
430 a2l->addr2line.in = -1;
431 a2l->addr2line.out = -1;
432 a2l->addr2line.no_stderr = 1;
434 a2l->addr2line.argv = argv;
435 start_command_status = start_command(&a2l->addr2line);
436 a2l->addr2line.argv = NULL; /* it's not used after start_command; avoid dangling pointers */
438 if (start_command_status != 0) {
439 pr_warning("could not start addr2line for %s: start_command return code %d\n",
441 start_command_status);
445 a2l->to_child = fdopen(a2l->addr2line.in, "w");
446 if (a2l->to_child == NULL) {
447 pr_warning("could not open write-stream to addr2line of %s\n", path);
451 a2l->from_child = fdopen(a2l->addr2line.out, "r");
452 if (a2l->from_child == NULL) {
453 pr_warning("could not open read-stream from addr2line of %s\n", path);
461 addr2line_subprocess_cleanup(a2l);
466 static int read_addr2line_record(struct a2l_subprocess *a2l,
469 unsigned int *line_nr)
474 * 0 ==> sentinel (or other ill-formed) record read
475 * 1 ==> a genuine record read
479 unsigned int dummy_line_nr = 0;
482 if (function != NULL)
485 if (filename != NULL)
491 if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len)
494 if (function != NULL)
495 *function = strdup(strim(line));
500 if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len)
503 if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0) {
508 if (filename != NULL)
509 *filename = strdup(line);
518 if (function != NULL)
520 if (filename != NULL)
525 static int inline_list__append_record(struct dso *dso,
526 struct inline_node *node,
528 const char *function,
529 const char *filename,
530 unsigned int line_nr)
532 struct symbol *inline_sym = new_inline_sym(dso, sym, function);
534 return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node);
537 static int addr2line(const char *dso_name, u64 addr,
538 char **file, unsigned int *line_nr,
541 struct inline_node *node,
542 struct symbol *sym __maybe_unused)
544 struct a2l_subprocess *a2l = dso->a2l;
545 char *record_function = NULL;
546 char *record_filename = NULL;
547 unsigned int record_line_nr = 0;
548 int record_status = -1;
550 size_t inline_count = 0;
553 dso->a2l = addr2line_subprocess_init(dso_name);
558 if (!symbol_conf.disable_add2line_warn)
559 pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name);
564 * Send our request and then *deliberately* send something that can't be interpreted as
565 * a valid address to ask addr2line about (namely, ","). This causes addr2line to first
566 * write out the answer to our request, in an unbounded/unknown number of records, and
567 * then to write out the lines "??" and "??:0", so that we can detect when it has
568 * finished giving us anything useful. We have to be careful about the first record,
569 * though, because it may be genuinely unknown, in which case we'll get two sets of
572 if (fprintf(a2l->to_child, "%016"PRIx64"\n,\n", addr) < 0 || fflush(a2l->to_child) != 0) {
573 pr_warning("%s %s: could not send request\n", __func__, dso_name);
577 switch (read_addr2line_record(a2l, &record_function, &record_filename, &record_line_nr)) {
579 pr_warning("%s %s: could not read first record\n", __func__, dso_name);
583 * The first record was invalid, so return failure, but first read another
584 * record, since we asked a junk question and have to clear the answer out.
586 switch (read_addr2line_record(a2l, NULL, NULL, NULL)) {
588 pr_warning("%s %s: could not read delimiter record\n", __func__, dso_name);
594 pr_warning("%s %s: unexpected record instead of sentinel",
604 *file = strdup(record_filename);
608 *line_nr = record_line_nr;
610 if (unwind_inlines) {
611 if (node && inline_list__append_record(dso, node, sym,
620 /* We have to read the records even if we don't care about the inline info. */
621 while ((record_status = read_addr2line_record(a2l,
624 &record_line_nr)) == 1) {
625 if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) {
626 if (inline_list__append_record(dso, node, sym,
633 ret = 1; /* found at least one inline frame */
638 free(record_function);
639 free(record_filename);
643 void dso__free_a2l(struct dso *dso)
645 struct a2l_subprocess *a2l = dso->a2l;
650 addr2line_subprocess_cleanup(a2l);
655 #endif /* HAVE_LIBBFD_SUPPORT */
657 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
658 struct dso *dso, struct symbol *sym)
660 struct inline_node *node;
662 node = zalloc(sizeof(*node));
664 perror("not enough memory for the inline node");
668 INIT_LIST_HEAD(&node->val);
671 addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
676 * Number of addr2line failures (without success) before disabling it for that
679 #define A2L_FAIL_LIMIT 123
681 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
682 bool show_sym, bool show_addr, bool unwind_inlines,
688 const char *dso_name;
690 if (!dso->has_srcline)
693 dso_name = dso__name(dso);
694 if (dso_name == NULL)
697 if (!addr2line(dso_name, addr, &file, &line, dso,
698 unwind_inlines, NULL, sym))
701 srcline = srcline_from_fileline(file, line);
712 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
713 dso->has_srcline = 0;
718 return (show_sym && sym) ?
719 strndup(sym->name, sym->namelen) : NULL;
722 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
723 ip - sym->start) < 0)
724 return SRCLINE_UNKNOWN;
725 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
726 return SRCLINE_UNKNOWN;
730 /* Returns filename and fills in line number in line */
731 char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)
734 const char *dso_name;
736 if (!dso->has_srcline)
739 dso_name = dso__name(dso);
740 if (dso_name == NULL)
743 if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL))
750 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
751 dso->has_srcline = 0;
758 void free_srcline(char *srcline)
760 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
764 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
765 bool show_sym, bool show_addr, u64 ip)
767 return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
770 struct srcline_node {
773 struct rb_node rb_node;
776 void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline)
778 struct rb_node **p = &tree->rb_root.rb_node;
779 struct rb_node *parent = NULL;
780 struct srcline_node *i, *node;
781 bool leftmost = true;
783 node = zalloc(sizeof(struct srcline_node));
785 perror("not enough memory for the srcline node");
790 node->srcline = srcline;
794 i = rb_entry(parent, struct srcline_node, rb_node);
802 rb_link_node(&node->rb_node, parent, p);
803 rb_insert_color_cached(&node->rb_node, tree, leftmost);
806 char *srcline__tree_find(struct rb_root_cached *tree, u64 addr)
808 struct rb_node *n = tree->rb_root.rb_node;
811 struct srcline_node *i = rb_entry(n, struct srcline_node,
816 else if (addr > i->addr)
825 void srcline__tree_delete(struct rb_root_cached *tree)
827 struct srcline_node *pos;
828 struct rb_node *next = rb_first_cached(tree);
831 pos = rb_entry(next, struct srcline_node, rb_node);
832 next = rb_next(&pos->rb_node);
833 rb_erase_cached(&pos->rb_node, tree);
834 free_srcline(pos->srcline);
839 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
842 const char *dso_name;
844 dso_name = dso__name(dso);
845 if (dso_name == NULL)
848 return addr2inlines(dso_name, addr, dso, sym);
851 void inline_node__delete(struct inline_node *node)
853 struct inline_list *ilist, *tmp;
855 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
856 list_del_init(&ilist->list);
857 free_srcline(ilist->srcline);
858 /* only the inlined symbols are owned by the list */
859 if (ilist->symbol && ilist->symbol->inlined)
860 symbol__delete(ilist->symbol);
867 void inlines__tree_insert(struct rb_root_cached *tree,
868 struct inline_node *inlines)
870 struct rb_node **p = &tree->rb_root.rb_node;
871 struct rb_node *parent = NULL;
872 const u64 addr = inlines->addr;
873 struct inline_node *i;
874 bool leftmost = true;
878 i = rb_entry(parent, struct inline_node, rb_node);
886 rb_link_node(&inlines->rb_node, parent, p);
887 rb_insert_color_cached(&inlines->rb_node, tree, leftmost);
890 struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr)
892 struct rb_node *n = tree->rb_root.rb_node;
895 struct inline_node *i = rb_entry(n, struct inline_node,
900 else if (addr > i->addr)
909 void inlines__tree_delete(struct rb_root_cached *tree)
911 struct inline_node *pos;
912 struct rb_node *next = rb_first_cached(tree);
915 pos = rb_entry(next, struct inline_node, rb_node);
916 next = rb_next(&pos->rb_node);
917 rb_erase_cached(&pos->rb_node, tree);
918 inline_node__delete(pos);