1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/zalloc.h>
16 #include "util/debug.h"
17 #include "util/callchain.h"
18 #include "util/symbol_conf.h"
22 #include "subcmd/run-command.h"
24 bool srcline_full_filename;
26 static const char *dso__name(struct dso *dso)
30 if (dso->symsrc_filename)
31 dso_name = dso->symsrc_filename;
33 dso_name = dso->long_name;
35 if (dso_name[0] == '[')
38 if (!strncmp(dso_name, "/tmp/perf-", 10))
44 static int inline_list__append(struct symbol *symbol, char *srcline,
45 struct inline_node *node)
47 struct inline_list *ilist;
49 ilist = zalloc(sizeof(*ilist));
53 ilist->symbol = symbol;
54 ilist->srcline = srcline;
56 if (callchain_param.order == ORDER_CALLEE)
57 list_add_tail(&ilist->list, &node->val);
59 list_add(&ilist->list, &node->val);
64 /* basename version that takes a const input string */
65 static const char *gnu_basename(const char *path)
67 const char *base = strrchr(path, '/');
69 return base ? base + 1 : path;
72 static char *srcline_from_fileline(const char *file, unsigned int line)
79 if (!srcline_full_filename)
80 file = gnu_basename(file);
82 if (asprintf(&srcline, "%s:%u", file, line) < 0)
88 static struct symbol *new_inline_sym(struct dso *dso,
89 struct symbol *base_sym,
92 struct symbol *inline_sym;
93 char *demangled = NULL;
99 demangled = dso__demangle_sym(dso, 0, funcname);
101 funcname = demangled;
104 if (base_sym && strcmp(funcname, base_sym->name) == 0) {
105 /* reuse the real, existing symbol */
106 inline_sym = base_sym;
107 /* ensure that we don't alias an inlined symbol, which could
108 * lead to double frees in inline_node__delete
110 assert(!base_sym->inlined);
112 /* create a fake symbol for the inline frame */
113 inline_sym = symbol__new(base_sym ? base_sym->start : 0,
114 base_sym ? (base_sym->end - base_sym->start) : 0,
115 base_sym ? base_sym->binding : 0,
116 base_sym ? base_sym->type : 0,
119 inline_sym->inlined = 1;
127 #define MAX_INLINE_NEST 1024
129 #ifdef HAVE_LIBBFD_SUPPORT
132 * Implement addr2line using libbfd.
134 #define PACKAGE "perf"
142 const char *filename;
143 const char *funcname;
150 static int bfd_error(const char *string)
154 errmsg = bfd_errmsg(bfd_get_error());
158 pr_debug("%s: %s\n", string, errmsg);
160 pr_debug("%s\n", errmsg);
165 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
170 bfd_boolean dynamic = FALSE;
172 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
173 return bfd_error(bfd_get_filename(abfd));
175 storage = bfd_get_symtab_upper_bound(abfd);
177 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
181 return bfd_error(bfd_get_filename(abfd));
183 syms = malloc(storage);
185 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
187 symcount = bfd_canonicalize_symtab(abfd, syms);
191 return bfd_error(bfd_get_filename(abfd));
198 static void find_address_in_section(bfd *abfd, asection *section, void *data)
202 struct a2l_data *a2l = data;
208 #ifdef bfd_get_section_flags
209 flags = bfd_get_section_flags(abfd, section);
211 flags = bfd_section_flags(section);
213 if ((flags & SEC_ALLOC) == 0)
217 #ifdef bfd_get_section_vma
218 vma = bfd_get_section_vma(abfd, section);
220 vma = bfd_section_vma(section);
222 #ifdef bfd_get_section_size
223 size = bfd_get_section_size(section);
225 size = bfd_section_size(section);
228 if (pc < vma || pc >= vma + size)
231 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
232 &a2l->filename, &a2l->funcname,
235 if (a2l->filename && !strlen(a2l->filename))
236 a2l->filename = NULL;
239 static struct a2l_data *addr2line_init(const char *path)
242 struct a2l_data *a2l = NULL;
244 abfd = bfd_openr(path, NULL);
248 if (!bfd_check_format(abfd, bfd_object))
251 a2l = zalloc(sizeof(*a2l));
256 a2l->input = strdup(path);
257 if (a2l->input == NULL)
260 if (slurp_symtab(abfd, a2l))
267 zfree((char **)&a2l->input);
274 static void addr2line_cleanup(struct a2l_data *a2l)
277 bfd_close(a2l->abfd);
278 zfree((char **)&a2l->input);
283 static int inline_list__append_dso_a2l(struct dso *dso,
284 struct inline_node *node,
287 struct a2l_data *a2l = dso->a2l;
288 struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
289 char *srcline = NULL;
292 srcline = srcline_from_fileline(a2l->filename, a2l->line);
294 return inline_list__append(inline_sym, srcline, node);
297 static int addr2line(const char *dso_name, u64 addr,
298 char **file, unsigned int *line, struct dso *dso,
299 bool unwind_inlines, struct inline_node *node,
303 struct a2l_data *a2l = dso->a2l;
306 dso->a2l = addr2line_init(dso_name);
311 if (!symbol_conf.disable_add2line_warn)
312 pr_warning("addr2line_init failed for %s\n", dso_name);
319 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
324 if (unwind_inlines) {
327 if (node && inline_list__append_dso_a2l(dso, node, sym))
330 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
331 &a2l->funcname, &a2l->line) &&
332 cnt++ < MAX_INLINE_NEST) {
334 if (a2l->filename && !strlen(a2l->filename))
335 a2l->filename = NULL;
338 if (inline_list__append_dso_a2l(dso, node, sym))
340 // found at least one inline frame
347 *file = a2l->filename ? strdup(a2l->filename) : NULL;
357 void dso__free_a2l(struct dso *dso)
359 struct a2l_data *a2l = dso->a2l;
364 addr2line_cleanup(a2l);
369 #else /* HAVE_LIBBFD_SUPPORT */
371 static int filename_split(char *filename, unsigned int *line_nr)
375 sep = strchr(filename, '\n');
379 if (!strcmp(filename, "??:0"))
382 sep = strchr(filename, ':');
385 *line_nr = strtoul(sep, NULL, 0);
392 static void addr2line_subprocess_cleanup(struct child_process *a2l)
394 if (a2l->pid != -1) {
395 kill(a2l->pid, SIGKILL);
396 finish_command(a2l); /* ignore result, we don't care */
403 static struct child_process *addr2line_subprocess_init(const char *addr2line_path,
404 const char *binary_path)
406 const char *argv[] = {
407 addr2line_path ?: "addr2line",
411 struct child_process *a2l = zalloc(sizeof(*a2l));
412 int start_command_status = 0;
415 pr_err("Failed to allocate memory for addr2line");
425 start_command_status = start_command(a2l);
426 a2l->argv = NULL; /* it's not used after start_command; avoid dangling pointers */
428 if (start_command_status != 0) {
429 pr_warning("could not start addr2line (%s) for %s: start_command return code %d\n",
430 addr2line_path, binary_path, start_command_status);
431 addr2line_subprocess_cleanup(a2l);
444 static enum a2l_style addr2line_configure(struct child_process *a2l)
447 static enum a2l_style style;
454 if (write(a2l->in, ",\n", 2) != 2)
457 io__init(&io, a2l->out, buf, sizeof(buf));
458 ch = io__get_char(&io);
462 } else if (ch == '?') {
463 style = GNU_BINUTILS;
469 ch = io__get_char(&io);
470 } while (ch > 0 && ch != '\n');
471 if (style == GNU_BINUTILS) {
473 ch = io__get_char(&io);
474 } while (ch > 0 && ch != '\n');
476 /* Ignore SIGPIPE in the event addr2line exits. */
477 signal(SIGPIPE, SIG_IGN);
482 static int read_addr2line_record(struct io *io,
483 enum a2l_style style,
486 unsigned int *line_nr)
491 * 0 ==> sentinel (or other ill-formed) record read
492 * 1 ==> a genuine record read
496 unsigned int dummy_line_nr = 0;
499 if (function != NULL)
502 if (filename != NULL)
508 if (io__getline(io, &line, &line_len) < 0 || !line_len)
511 if (style == LLVM && line_len == 2 && line[0] == ',') {
516 if (function != NULL)
517 *function = strdup(strim(line));
522 if (io__getline(io, &line, &line_len) < 0 || !line_len)
525 if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0 &&
526 style == GNU_BINUTILS) {
531 if (filename != NULL)
532 *filename = strdup(line);
541 if (function != NULL)
543 if (filename != NULL)
548 static int inline_list__append_record(struct dso *dso,
549 struct inline_node *node,
551 const char *function,
552 const char *filename,
553 unsigned int line_nr)
555 struct symbol *inline_sym = new_inline_sym(dso, sym, function);
557 return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node);
560 static int addr2line(const char *dso_name, u64 addr,
561 char **file, unsigned int *line_nr,
564 struct inline_node *node,
565 struct symbol *sym __maybe_unused)
567 struct child_process *a2l = dso->a2l;
568 char *record_function = NULL;
569 char *record_filename = NULL;
570 unsigned int record_line_nr = 0;
571 int record_status = -1;
573 size_t inline_count = 0;
578 enum a2l_style a2l_style;
581 if (!filename__has_section(dso_name, ".debug_line"))
584 dso->a2l = addr2line_subprocess_init(symbol_conf.addr2line_path,
590 if (!symbol_conf.disable_add2line_warn)
591 pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name);
594 a2l_style = addr2line_configure(a2l);
595 if (a2l_style == BROKEN) {
596 if (!symbol_conf.disable_add2line_warn)
597 pr_warning("%s: addr2line configuration failed\n", __func__);
602 * Send our request and then *deliberately* send something that can't be interpreted as
603 * a valid address to ask addr2line about (namely, ","). This causes addr2line to first
604 * write out the answer to our request, in an unbounded/unknown number of records, and
605 * then to write out the lines "??" and "??:0", for GNU binutils, or "," for
606 * llvm-addr2line, so that we can detect when it has finished giving us anything
607 * useful. With GNU binutils, we have to be careful about the first record, though,
608 * because it may be genuinely unknown, in which case we'll get two sets of "??"/"??:0"
611 len = snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr);
612 written = len > 0 ? write(a2l->in, buf, len) : -1;
613 if (written != len) {
614 if (!symbol_conf.disable_add2line_warn)
615 pr_warning("%s %s: could not send request\n", __func__, dso_name);
618 io__init(&io, a2l->out, buf, sizeof(buf));
620 switch (read_addr2line_record(&io, a2l_style,
621 &record_function, &record_filename, &record_line_nr)) {
623 if (!symbol_conf.disable_add2line_warn)
624 pr_warning("%s %s: could not read first record\n", __func__, dso_name);
628 * The first record was invalid, so return failure, but first read another
629 * record, since we asked a junk question and have to clear the answer out.
631 switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) {
633 if (!symbol_conf.disable_add2line_warn)
634 pr_warning("%s %s: could not read delimiter record\n",
641 if (!symbol_conf.disable_add2line_warn)
642 pr_warning("%s %s: unexpected record instead of sentinel",
652 *file = strdup(record_filename);
656 *line_nr = record_line_nr;
658 if (unwind_inlines) {
659 if (node && inline_list__append_record(dso, node, sym,
668 /* We have to read the records even if we don't care about the inline info. */
669 while ((record_status = read_addr2line_record(&io,
673 &record_line_nr)) == 1) {
674 if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) {
675 if (inline_list__append_record(dso, node, sym,
682 ret = 1; /* found at least one inline frame */
687 free(record_function);
688 free(record_filename);
692 void dso__free_a2l(struct dso *dso)
694 struct child_process *a2l = dso->a2l;
699 addr2line_subprocess_cleanup(a2l);
704 #endif /* HAVE_LIBBFD_SUPPORT */
706 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
707 struct dso *dso, struct symbol *sym)
709 struct inline_node *node;
711 node = zalloc(sizeof(*node));
713 perror("not enough memory for the inline node");
717 INIT_LIST_HEAD(&node->val);
720 addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
725 * Number of addr2line failures (without success) before disabling it for that
728 #define A2L_FAIL_LIMIT 123
730 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
731 bool show_sym, bool show_addr, bool unwind_inlines,
737 const char *dso_name;
739 if (!dso->has_srcline)
742 dso_name = dso__name(dso);
743 if (dso_name == NULL)
746 if (!addr2line(dso_name, addr, &file, &line, dso,
747 unwind_inlines, NULL, sym))
750 srcline = srcline_from_fileline(file, line);
761 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
762 dso->has_srcline = 0;
767 return (show_sym && sym) ?
768 strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN;
771 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
772 ip - sym->start) < 0)
773 return SRCLINE_UNKNOWN;
774 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
775 return SRCLINE_UNKNOWN;
779 /* Returns filename and fills in line number in line */
780 char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)
783 const char *dso_name;
785 if (!dso->has_srcline)
788 dso_name = dso__name(dso);
789 if (dso_name == NULL)
792 if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL))
799 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
800 dso->has_srcline = 0;
807 void free_srcline(char *srcline)
809 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
813 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
814 bool show_sym, bool show_addr, u64 ip)
816 return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
819 struct srcline_node {
822 struct rb_node rb_node;
825 void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline)
827 struct rb_node **p = &tree->rb_root.rb_node;
828 struct rb_node *parent = NULL;
829 struct srcline_node *i, *node;
830 bool leftmost = true;
832 node = zalloc(sizeof(struct srcline_node));
834 perror("not enough memory for the srcline node");
839 node->srcline = srcline;
843 i = rb_entry(parent, struct srcline_node, rb_node);
851 rb_link_node(&node->rb_node, parent, p);
852 rb_insert_color_cached(&node->rb_node, tree, leftmost);
855 char *srcline__tree_find(struct rb_root_cached *tree, u64 addr)
857 struct rb_node *n = tree->rb_root.rb_node;
860 struct srcline_node *i = rb_entry(n, struct srcline_node,
865 else if (addr > i->addr)
874 void srcline__tree_delete(struct rb_root_cached *tree)
876 struct srcline_node *pos;
877 struct rb_node *next = rb_first_cached(tree);
880 pos = rb_entry(next, struct srcline_node, rb_node);
881 next = rb_next(&pos->rb_node);
882 rb_erase_cached(&pos->rb_node, tree);
883 free_srcline(pos->srcline);
888 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
891 const char *dso_name;
893 dso_name = dso__name(dso);
894 if (dso_name == NULL)
897 return addr2inlines(dso_name, addr, dso, sym);
900 void inline_node__delete(struct inline_node *node)
902 struct inline_list *ilist, *tmp;
904 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
905 list_del_init(&ilist->list);
906 free_srcline(ilist->srcline);
907 /* only the inlined symbols are owned by the list */
908 if (ilist->symbol && ilist->symbol->inlined)
909 symbol__delete(ilist->symbol);
916 void inlines__tree_insert(struct rb_root_cached *tree,
917 struct inline_node *inlines)
919 struct rb_node **p = &tree->rb_root.rb_node;
920 struct rb_node *parent = NULL;
921 const u64 addr = inlines->addr;
922 struct inline_node *i;
923 bool leftmost = true;
927 i = rb_entry(parent, struct inline_node, rb_node);
935 rb_link_node(&inlines->rb_node, parent, p);
936 rb_insert_color_cached(&inlines->rb_node, tree, leftmost);
939 struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr)
941 struct rb_node *n = tree->rb_root.rb_node;
944 struct inline_node *i = rb_entry(n, struct inline_node,
949 else if (addr > i->addr)
958 void inlines__tree_delete(struct rb_root_cached *tree)
960 struct inline_node *pos;
961 struct rb_node *next = rb_first_cached(tree);
964 pos = rb_entry(next, struct inline_node, rb_node);
965 next = rb_next(&pos->rb_node);
966 rb_erase_cached(&pos->rb_node, tree);
967 inline_node__delete(pos);