1 // SPDX-License-Identifier: GPL-2.0 2 #include "dso.h" 3 #include "libdw.h" 4 #include "srcline.h" 5 #include "symbol.h" 6 #include "dwarf-aux.h" 7 #include <fcntl.h> 8 #include <unistd.h> 9 #include <elfutils/libdwfl.h> 10 11 void dso__free_a2l_libdw(struct dso *dso) 12 { 13 Dwfl *dwfl = dso__a2l_libdw(dso); 14 15 if (dwfl) { 16 dwfl_end(dwfl); 17 dso__set_a2l_libdw(dso, NULL); 18 } 19 } 20 21 struct libdw_a2l_cb_args { 22 struct dso *dso; 23 struct symbol *sym; 24 struct inline_node *node; 25 char *leaf_srcline; 26 bool leaf_srcline_used; 27 }; 28 29 static int libdw_a2l_cb(Dwarf_Die *die, void *_args) 30 { 31 struct libdw_a2l_cb_args *args = _args; 32 struct symbol *inline_sym = new_inline_sym(args->dso, args->sym, dwarf_diename(die)); 33 const char *call_fname = die_get_call_file(die); 34 char *call_srcline = srcline__unknown; 35 struct inline_list *ilist; 36 37 if (!inline_sym) 38 return -ENOMEM; 39 40 /* Assign caller information to the parent. */ 41 if (call_fname) 42 call_srcline = srcline_from_fileline(call_fname, die_get_call_lineno(die)); 43 44 list_for_each_entry(ilist, &args->node->val, list) { 45 ilist->srcline = call_srcline; 46 call_srcline = NULL; 47 break; 48 } 49 if (call_srcline && call_fname) 50 free(call_srcline); 51 52 /* Add this symbol to the chain as the leaf. */ 53 inline_list__append_tail(inline_sym, args->leaf_srcline, args->node); 54 args->leaf_srcline_used = true; 55 return 0; 56 } 57 58 int libdw__addr2line(const char *dso_name, u64 addr, 59 char **file, unsigned int *line_nr, 60 struct dso *dso, bool unwind_inlines, 61 struct inline_node *node, struct symbol *sym) 62 { 63 static const Dwfl_Callbacks offline_callbacks = { 64 .find_debuginfo = dwfl_standard_find_debuginfo, 65 .section_address = dwfl_offline_section_address, 66 .find_elf = dwfl_build_id_find_elf, 67 }; 68 Dwfl *dwfl = dso__a2l_libdw(dso); 69 Dwfl_Module *mod; 70 Dwfl_Line *dwline; 71 Dwarf_Addr bias; 72 const char *src; 73 int lineno = 0; 74 75 if (!dwfl) { 76 /* 77 * Initialize Dwfl session. 78 * We need to open the DSO file to report it to libdw. 79 */ 80 int fd; 81 82 fd = open(dso_name, O_RDONLY); 83 if (fd < 0) 84 return 0; 85 86 dwfl = dwfl_begin(&offline_callbacks); 87 if (!dwfl) { 88 close(fd); 89 return 0; 90 } 91 92 /* 93 * If the report is successful, the file descriptor fd is consumed 94 * and closed by the Dwfl. If not, it is not closed. 95 */ 96 mod = dwfl_report_offline(dwfl, dso_name, dso_name, fd); 97 if (!mod) { 98 dwfl_end(dwfl); 99 close(fd); 100 return 0; 101 } 102 103 dwfl_report_end(dwfl, /*removed=*/NULL, /*arg=*/NULL); 104 dso__set_a2l_libdw(dso, dwfl); 105 } else { 106 /* Dwfl session already initialized, get module for address. */ 107 mod = dwfl_addrmodule(dwfl, addr); 108 } 109 110 if (!mod) 111 return 0; 112 113 /* 114 * Get/ignore the dwarf information. Determine the bias, difference 115 * between the regular ELF addr2line addresses and those to use with 116 * libdw. 117 */ 118 if (!dwfl_module_getdwarf(mod, &bias)) 119 return 0; 120 121 /* Find source line information for the address. */ 122 dwline = dwfl_module_getsrc(mod, addr + bias); 123 if (!dwline) 124 return 0; 125 126 /* Get line information. */ 127 src = dwfl_lineinfo(dwline, /*addr=*/NULL, &lineno, /*col=*/NULL, /*mtime=*/NULL, 128 /*length=*/NULL); 129 130 if (file) 131 *file = src ? strdup(src) : NULL; 132 if (line_nr) 133 *line_nr = lineno; 134 135 /* Optionally unwind inline function call chain. */ 136 if (unwind_inlines && node) { 137 Dwarf_Addr unused_bias; 138 Dwarf_Die *cudie = dwfl_module_addrdie(mod, addr + bias, &unused_bias); 139 struct libdw_a2l_cb_args args = { 140 .dso = dso, 141 .sym = sym, 142 .node = node, 143 .leaf_srcline = srcline_from_fileline(src ?: "<unknown>", lineno), 144 }; 145 146 /* Walk from the parent down to the leaf. */ 147 cu_walk_functions_at(cudie, addr, libdw_a2l_cb, &args); 148 149 if (!args.leaf_srcline_used) 150 free(args.leaf_srcline); 151 } 152 return 1; 153 } 154