Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 2 | #include <inttypes.h> |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 3 | #include <signal.h> |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 7 | #include <sys/types.h> |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 8 | |
| 9 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 13c230a | 2019-06-26 12:13:13 -0300 | [diff] [blame] | 10 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 11 | #include <linux/zalloc.h> |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 12 | |
Namhyung Kim | 86c98ca | 2013-09-11 14:09:30 +0900 | [diff] [blame] | 13 | #include "util/dso.h" |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 14 | #include "util/debug.h" |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 15 | #include "util/callchain.h" |
Jin Yao | b10c78c | 2019-06-28 17:23:03 +0800 | [diff] [blame] | 16 | #include "util/symbol_conf.h" |
Arnaldo Carvalho de Melo | 632a5ca | 2017-04-17 16:30:49 -0300 | [diff] [blame] | 17 | #include "srcline.h" |
Namhyung Kim | 7285cf3 | 2017-10-31 11:06:54 +0900 | [diff] [blame] | 18 | #include "string2.h" |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 19 | #include "symbol.h" |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 20 | #include "subcmd/run-command.h" |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 21 | |
Andi Kleen | a9710ba | 2015-08-07 15:24:05 -0700 | [diff] [blame] | 22 | bool srcline_full_filename; |
| 23 | |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 24 | static const char *dso__name(struct dso *dso) |
| 25 | { |
| 26 | const char *dso_name; |
| 27 | |
| 28 | if (dso->symsrc_filename) |
| 29 | dso_name = dso->symsrc_filename; |
| 30 | else |
| 31 | dso_name = dso->long_name; |
| 32 | |
| 33 | if (dso_name[0] == '[') |
| 34 | return NULL; |
| 35 | |
| 36 | if (!strncmp(dso_name, "/tmp/perf-", 10)) |
| 37 | return NULL; |
| 38 | |
| 39 | return dso_name; |
| 40 | } |
| 41 | |
Milian Wolff | 2be8832 | 2017-10-09 22:32:58 +0200 | [diff] [blame] | 42 | static int inline_list__append(struct symbol *symbol, char *srcline, |
| 43 | struct inline_node *node) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 44 | { |
| 45 | struct inline_list *ilist; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 46 | |
| 47 | ilist = zalloc(sizeof(*ilist)); |
| 48 | if (ilist == NULL) |
| 49 | return -1; |
| 50 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 51 | ilist->symbol = symbol; |
Milian Wolff | 2be8832 | 2017-10-09 22:32:58 +0200 | [diff] [blame] | 52 | ilist->srcline = srcline; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 53 | |
Milian Wolff | 28071f5 | 2017-05-24 15:21:27 +0900 | [diff] [blame] | 54 | if (callchain_param.order == ORDER_CALLEE) |
| 55 | list_add_tail(&ilist->list, &node->val); |
| 56 | else |
| 57 | list_add(&ilist->list, &node->val); |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Milian Wolff | 2be8832 | 2017-10-09 22:32:58 +0200 | [diff] [blame] | 62 | /* basename version that takes a const input string */ |
| 63 | static const char *gnu_basename(const char *path) |
| 64 | { |
| 65 | const char *base = strrchr(path, '/'); |
| 66 | |
| 67 | return base ? base + 1 : path; |
| 68 | } |
| 69 | |
| 70 | static char *srcline_from_fileline(const char *file, unsigned int line) |
| 71 | { |
| 72 | char *srcline; |
| 73 | |
| 74 | if (!file) |
| 75 | return NULL; |
| 76 | |
| 77 | if (!srcline_full_filename) |
| 78 | file = gnu_basename(file); |
| 79 | |
| 80 | if (asprintf(&srcline, "%s:%u", file, line) < 0) |
| 81 | return NULL; |
| 82 | |
| 83 | return srcline; |
| 84 | } |
| 85 | |
Namhyung Kim | 7285cf3 | 2017-10-31 11:06:54 +0900 | [diff] [blame] | 86 | static struct symbol *new_inline_sym(struct dso *dso, |
| 87 | struct symbol *base_sym, |
| 88 | const char *funcname) |
| 89 | { |
| 90 | struct symbol *inline_sym; |
| 91 | char *demangled = NULL; |
| 92 | |
Milian Wolff | d4046e8 | 2018-09-26 15:52:07 +0200 | [diff] [blame] | 93 | if (!funcname) |
| 94 | funcname = "??"; |
| 95 | |
Namhyung Kim | 7285cf3 | 2017-10-31 11:06:54 +0900 | [diff] [blame] | 96 | if (dso) { |
| 97 | demangled = dso__demangle_sym(dso, 0, funcname); |
| 98 | if (demangled) |
| 99 | funcname = demangled; |
| 100 | } |
| 101 | |
| 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 |
| 107 | */ |
| 108 | assert(!base_sym->inlined); |
| 109 | } else { |
| 110 | /* create a fake symbol for the inline frame */ |
| 111 | inline_sym = symbol__new(base_sym ? base_sym->start : 0, |
He Kuang | 7346195 | 2019-02-19 21:05:31 +0800 | [diff] [blame] | 112 | base_sym ? (base_sym->end - base_sym->start) : 0, |
Namhyung Kim | 7285cf3 | 2017-10-31 11:06:54 +0900 | [diff] [blame] | 113 | base_sym ? base_sym->binding : 0, |
Arnaldo Carvalho de Melo | af30bff | 2018-04-26 11:09:10 -0300 | [diff] [blame] | 114 | base_sym ? base_sym->type : 0, |
Namhyung Kim | 7285cf3 | 2017-10-31 11:06:54 +0900 | [diff] [blame] | 115 | funcname); |
| 116 | if (inline_sym) |
| 117 | inline_sym->inlined = 1; |
| 118 | } |
| 119 | |
| 120 | free(demangled); |
| 121 | |
| 122 | return inline_sym; |
| 123 | } |
| 124 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 125 | #define MAX_INLINE_NEST 1024 |
| 126 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 127 | #ifdef HAVE_LIBBFD_SUPPORT |
| 128 | |
| 129 | /* |
| 130 | * Implement addr2line using libbfd. |
| 131 | */ |
| 132 | #define PACKAGE "perf" |
| 133 | #include <bfd.h> |
| 134 | |
| 135 | struct a2l_data { |
| 136 | const char *input; |
Wang Nan | ac931f87 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 137 | u64 addr; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 138 | |
| 139 | bool found; |
| 140 | const char *filename; |
| 141 | const char *funcname; |
| 142 | unsigned line; |
| 143 | |
| 144 | bfd *abfd; |
| 145 | asymbol **syms; |
| 146 | }; |
| 147 | |
| 148 | static int bfd_error(const char *string) |
| 149 | { |
| 150 | const char *errmsg; |
| 151 | |
| 152 | errmsg = bfd_errmsg(bfd_get_error()); |
| 153 | fflush(stdout); |
| 154 | |
| 155 | if (string) |
| 156 | pr_debug("%s: %s\n", string, errmsg); |
| 157 | else |
| 158 | pr_debug("%s\n", errmsg); |
| 159 | |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) |
| 164 | { |
| 165 | long storage; |
| 166 | long symcount; |
| 167 | asymbol **syms; |
| 168 | bfd_boolean dynamic = FALSE; |
| 169 | |
| 170 | if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) |
| 171 | return bfd_error(bfd_get_filename(abfd)); |
| 172 | |
| 173 | storage = bfd_get_symtab_upper_bound(abfd); |
| 174 | if (storage == 0L) { |
| 175 | storage = bfd_get_dynamic_symtab_upper_bound(abfd); |
| 176 | dynamic = TRUE; |
| 177 | } |
| 178 | if (storage < 0L) |
| 179 | return bfd_error(bfd_get_filename(abfd)); |
| 180 | |
| 181 | syms = malloc(storage); |
| 182 | if (dynamic) |
| 183 | symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); |
| 184 | else |
| 185 | symcount = bfd_canonicalize_symtab(abfd, syms); |
| 186 | |
| 187 | if (symcount < 0) { |
| 188 | free(syms); |
| 189 | return bfd_error(bfd_get_filename(abfd)); |
| 190 | } |
| 191 | |
| 192 | a2l->syms = syms; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void find_address_in_section(bfd *abfd, asection *section, void *data) |
| 197 | { |
| 198 | bfd_vma pc, vma; |
| 199 | bfd_size_type size; |
| 200 | struct a2l_data *a2l = data; |
Changbin Du | 0ada120 | 2020-01-28 23:29:38 +0800 | [diff] [blame] | 201 | flagword flags; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 202 | |
| 203 | if (a2l->found) |
| 204 | return; |
| 205 | |
Changbin Du | 0ada120 | 2020-01-28 23:29:38 +0800 | [diff] [blame] | 206 | #ifdef bfd_get_section_flags |
| 207 | flags = bfd_get_section_flags(abfd, section); |
| 208 | #else |
| 209 | flags = bfd_section_flags(section); |
| 210 | #endif |
| 211 | if ((flags & SEC_ALLOC) == 0) |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 212 | return; |
| 213 | |
| 214 | pc = a2l->addr; |
Changbin Du | 0ada120 | 2020-01-28 23:29:38 +0800 | [diff] [blame] | 215 | #ifdef bfd_get_section_vma |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 216 | vma = bfd_get_section_vma(abfd, section); |
Changbin Du | 0ada120 | 2020-01-28 23:29:38 +0800 | [diff] [blame] | 217 | #else |
| 218 | vma = bfd_section_vma(section); |
| 219 | #endif |
| 220 | #ifdef bfd_get_section_size |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 221 | size = bfd_get_section_size(section); |
Changbin Du | 0ada120 | 2020-01-28 23:29:38 +0800 | [diff] [blame] | 222 | #else |
| 223 | size = bfd_section_size(section); |
| 224 | #endif |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 225 | |
| 226 | if (pc < vma || pc >= vma + size) |
| 227 | return; |
| 228 | |
| 229 | a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, |
| 230 | &a2l->filename, &a2l->funcname, |
| 231 | &a2l->line); |
Milian Wolff | d964b1c | 2017-08-06 23:24:45 +0200 | [diff] [blame] | 232 | |
| 233 | if (a2l->filename && !strlen(a2l->filename)) |
| 234 | a2l->filename = NULL; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static struct a2l_data *addr2line_init(const char *path) |
| 238 | { |
| 239 | bfd *abfd; |
| 240 | struct a2l_data *a2l = NULL; |
| 241 | |
| 242 | abfd = bfd_openr(path, NULL); |
| 243 | if (abfd == NULL) |
| 244 | return NULL; |
| 245 | |
| 246 | if (!bfd_check_format(abfd, bfd_object)) |
| 247 | goto out; |
| 248 | |
| 249 | a2l = zalloc(sizeof(*a2l)); |
| 250 | if (a2l == NULL) |
| 251 | goto out; |
| 252 | |
| 253 | a2l->abfd = abfd; |
| 254 | a2l->input = strdup(path); |
| 255 | if (a2l->input == NULL) |
| 256 | goto out; |
| 257 | |
| 258 | if (slurp_symtab(abfd, a2l)) |
| 259 | goto out; |
| 260 | |
| 261 | return a2l; |
| 262 | |
| 263 | out: |
| 264 | if (a2l) { |
Namhyung Kim | 7d16c63 | 2014-01-09 23:07:59 +0900 | [diff] [blame] | 265 | zfree((char **)&a2l->input); |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 266 | free(a2l); |
| 267 | } |
| 268 | bfd_close(abfd); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | static void addr2line_cleanup(struct a2l_data *a2l) |
| 273 | { |
| 274 | if (a2l->abfd) |
| 275 | bfd_close(a2l->abfd); |
Namhyung Kim | 7d16c63 | 2014-01-09 23:07:59 +0900 | [diff] [blame] | 276 | zfree((char **)&a2l->input); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 277 | zfree(&a2l->syms); |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 278 | free(a2l); |
| 279 | } |
| 280 | |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 281 | static int inline_list__append_dso_a2l(struct dso *dso, |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 282 | struct inline_node *node, |
| 283 | struct symbol *sym) |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 284 | { |
| 285 | struct a2l_data *a2l = dso->a2l; |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 286 | struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname); |
Milian Wolff | 2be8832 | 2017-10-09 22:32:58 +0200 | [diff] [blame] | 287 | char *srcline = NULL; |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 288 | |
Milian Wolff | 2be8832 | 2017-10-09 22:32:58 +0200 | [diff] [blame] | 289 | if (a2l->filename) |
| 290 | srcline = srcline_from_fileline(a2l->filename, a2l->line); |
| 291 | |
| 292 | return inline_list__append(inline_sym, srcline, node); |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 293 | } |
| 294 | |
Wang Nan | ac931f87 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 295 | static int addr2line(const char *dso_name, u64 addr, |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 296 | char **file, unsigned int *line, struct dso *dso, |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 297 | bool unwind_inlines, struct inline_node *node, |
| 298 | struct symbol *sym) |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 299 | { |
| 300 | int ret = 0; |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 301 | struct a2l_data *a2l = dso->a2l; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 302 | |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 303 | if (!a2l) { |
| 304 | dso->a2l = addr2line_init(dso_name); |
| 305 | a2l = dso->a2l; |
| 306 | } |
| 307 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 308 | if (a2l == NULL) { |
Jin Yao | b10c78c | 2019-06-28 17:23:03 +0800 | [diff] [blame] | 309 | if (!symbol_conf.disable_add2line_warn) |
| 310 | pr_warning("addr2line_init failed for %s\n", dso_name); |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | a2l->addr = addr; |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 315 | a2l->found = false; |
| 316 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 317 | bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); |
| 318 | |
Milian Wolff | b21cc97 | 2017-05-24 15:21:24 +0900 | [diff] [blame] | 319 | if (!a2l->found) |
| 320 | return 0; |
| 321 | |
| 322 | if (unwind_inlines) { |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 323 | int cnt = 0; |
| 324 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 325 | if (node && inline_list__append_dso_a2l(dso, node, sym)) |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 326 | return 0; |
| 327 | |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 328 | while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, |
| 329 | &a2l->funcname, &a2l->line) && |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 330 | cnt++ < MAX_INLINE_NEST) { |
| 331 | |
Milian Wolff | d964b1c | 2017-08-06 23:24:45 +0200 | [diff] [blame] | 332 | if (a2l->filename && !strlen(a2l->filename)) |
| 333 | a2l->filename = NULL; |
| 334 | |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 335 | if (node != NULL) { |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 336 | if (inline_list__append_dso_a2l(dso, node, sym)) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 337 | return 0; |
Milian Wolff | b21cc97 | 2017-05-24 15:21:24 +0900 | [diff] [blame] | 338 | // found at least one inline frame |
| 339 | ret = 1; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 340 | } |
| 341 | } |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Milian Wolff | b21cc97 | 2017-05-24 15:21:24 +0900 | [diff] [blame] | 344 | if (file) { |
| 345 | *file = a2l->filename ? strdup(a2l->filename) : NULL; |
| 346 | ret = *file ? 1 : 0; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 347 | } |
| 348 | |
Milian Wolff | b21cc97 | 2017-05-24 15:21:24 +0900 | [diff] [blame] | 349 | if (line) |
| 350 | *line = a2l->line; |
| 351 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 352 | return ret; |
| 353 | } |
| 354 | |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 355 | void dso__free_a2l(struct dso *dso) |
| 356 | { |
| 357 | struct a2l_data *a2l = dso->a2l; |
| 358 | |
| 359 | if (!a2l) |
| 360 | return; |
| 361 | |
| 362 | addr2line_cleanup(a2l); |
| 363 | |
| 364 | dso->a2l = NULL; |
| 365 | } |
| 366 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 367 | #else /* HAVE_LIBBFD_SUPPORT */ |
| 368 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 369 | struct a2l_subprocess { |
| 370 | struct child_process addr2line; |
| 371 | FILE *to_child; |
| 372 | FILE *from_child; |
| 373 | }; |
| 374 | |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 375 | static int filename_split(char *filename, unsigned int *line_nr) |
| 376 | { |
| 377 | char *sep; |
| 378 | |
| 379 | sep = strchr(filename, '\n'); |
| 380 | if (sep) |
| 381 | *sep = '\0'; |
| 382 | |
| 383 | if (!strcmp(filename, "??:0")) |
| 384 | return 0; |
| 385 | |
| 386 | sep = strchr(filename, ':'); |
| 387 | if (sep) { |
| 388 | *sep++ = '\0'; |
| 389 | *line_nr = strtoul(sep, NULL, 0); |
| 390 | return 1; |
| 391 | } |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 396 | static void addr2line_subprocess_cleanup(struct a2l_subprocess *a2l) |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 397 | { |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 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; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 402 | } |
| 403 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 404 | if (a2l->to_child != NULL) { |
| 405 | fclose(a2l->to_child); |
| 406 | a2l->to_child = NULL; |
| 407 | } |
| 408 | |
| 409 | if (a2l->from_child != NULL) { |
| 410 | fclose(a2l->from_child); |
| 411 | a2l->from_child = NULL; |
| 412 | } |
| 413 | |
| 414 | free(a2l); |
| 415 | } |
| 416 | |
| 417 | static struct a2l_subprocess *addr2line_subprocess_init(const char *path) |
| 418 | { |
| 419 | const char *argv[] = { "addr2line", "-e", path, "-i", "-f", NULL }; |
| 420 | struct a2l_subprocess *a2l = zalloc(sizeof(*a2l)); |
| 421 | int start_command_status = 0; |
| 422 | |
| 423 | if (a2l == NULL) |
| 424 | goto out; |
| 425 | |
| 426 | a2l->to_child = NULL; |
| 427 | a2l->from_child = NULL; |
| 428 | |
| 429 | a2l->addr2line.pid = -1; |
| 430 | a2l->addr2line.in = -1; |
| 431 | a2l->addr2line.out = -1; |
| 432 | a2l->addr2line.no_stderr = 1; |
| 433 | |
| 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 */ |
| 437 | |
| 438 | if (start_command_status != 0) { |
| 439 | pr_warning("could not start addr2line for %s: start_command return code %d\n", |
| 440 | path, |
| 441 | start_command_status); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 442 | goto out; |
| 443 | } |
| 444 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 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); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 448 | goto out; |
| 449 | } |
| 450 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 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); |
| 454 | goto out; |
| 455 | } |
| 456 | |
| 457 | return a2l; |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 458 | |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 459 | out: |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 460 | if (a2l) |
| 461 | addr2line_subprocess_cleanup(a2l); |
| 462 | |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | static int read_addr2line_record(struct a2l_subprocess *a2l, |
| 467 | char **function, |
| 468 | char **filename, |
| 469 | unsigned int *line_nr) |
| 470 | { |
| 471 | /* |
| 472 | * Returns: |
| 473 | * -1 ==> error |
| 474 | * 0 ==> sentinel (or other ill-formed) record read |
| 475 | * 1 ==> a genuine record read |
| 476 | */ |
| 477 | char *line = NULL; |
| 478 | size_t line_len = 0; |
| 479 | unsigned int dummy_line_nr = 0; |
| 480 | int ret = -1; |
| 481 | |
| 482 | if (function != NULL) |
| 483 | zfree(function); |
| 484 | |
| 485 | if (filename != NULL) |
| 486 | zfree(filename); |
| 487 | |
| 488 | if (line_nr != NULL) |
| 489 | *line_nr = 0; |
| 490 | |
| 491 | if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len) |
| 492 | goto error; |
| 493 | |
| 494 | if (function != NULL) |
| 495 | *function = strdup(strim(line)); |
| 496 | |
| 497 | zfree(&line); |
| 498 | line_len = 0; |
| 499 | |
| 500 | if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len) |
| 501 | goto error; |
| 502 | |
| 503 | if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0) { |
| 504 | ret = 0; |
| 505 | goto error; |
| 506 | } |
| 507 | |
| 508 | if (filename != NULL) |
| 509 | *filename = strdup(line); |
| 510 | |
| 511 | zfree(&line); |
| 512 | line_len = 0; |
| 513 | |
| 514 | return 1; |
| 515 | |
| 516 | error: |
| 517 | free(line); |
| 518 | if (function != NULL) |
| 519 | zfree(function); |
| 520 | if (filename != NULL) |
| 521 | zfree(filename); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 522 | return ret; |
| 523 | } |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 524 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 525 | static int inline_list__append_record(struct dso *dso, |
| 526 | struct inline_node *node, |
| 527 | struct symbol *sym, |
| 528 | const char *function, |
| 529 | const char *filename, |
| 530 | unsigned int line_nr) |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 531 | { |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 532 | struct symbol *inline_sym = new_inline_sym(dso, sym, function); |
| 533 | |
| 534 | return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node); |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 537 | static int addr2line(const char *dso_name, u64 addr, |
| 538 | char **file, unsigned int *line_nr, |
| 539 | struct dso *dso, |
| 540 | bool unwind_inlines, |
| 541 | struct inline_node *node, |
| 542 | struct symbol *sym __maybe_unused) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 543 | { |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 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; |
| 549 | int ret = 0; |
| 550 | size_t inline_count = 0; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 551 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 552 | if (!a2l) { |
Namhyung Kim | 3b27222 | 2022-12-15 11:28:12 -0800 | [diff] [blame] | 553 | if (!filename__has_section(dso_name, ".debug_line")) |
| 554 | goto out; |
| 555 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 556 | dso->a2l = addr2line_subprocess_init(dso_name); |
| 557 | a2l = dso->a2l; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 558 | } |
| 559 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 560 | if (a2l == NULL) { |
| 561 | if (!symbol_conf.disable_add2line_warn) |
| 562 | pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name); |
| 563 | goto out; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Send our request and then *deliberately* send something that can't be interpreted as |
| 568 | * a valid address to ask addr2line about (namely, ","). This causes addr2line to first |
| 569 | * write out the answer to our request, in an unbounded/unknown number of records, and |
| 570 | * then to write out the lines "??" and "??:0", so that we can detect when it has |
| 571 | * finished giving us anything useful. We have to be careful about the first record, |
| 572 | * though, because it may be genuinely unknown, in which case we'll get two sets of |
| 573 | * "??"/"??:0" lines. |
| 574 | */ |
| 575 | if (fprintf(a2l->to_child, "%016"PRIx64"\n,\n", addr) < 0 || fflush(a2l->to_child) != 0) { |
Namhyung Kim | d5e33ce | 2022-12-15 11:28:13 -0800 | [diff] [blame] | 576 | if (!symbol_conf.disable_add2line_warn) |
| 577 | pr_warning("%s %s: could not send request\n", __func__, dso_name); |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 578 | goto out; |
| 579 | } |
| 580 | |
| 581 | switch (read_addr2line_record(a2l, &record_function, &record_filename, &record_line_nr)) { |
| 582 | case -1: |
Namhyung Kim | d5e33ce | 2022-12-15 11:28:13 -0800 | [diff] [blame] | 583 | if (!symbol_conf.disable_add2line_warn) |
| 584 | pr_warning("%s %s: could not read first record\n", __func__, dso_name); |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 585 | goto out; |
| 586 | case 0: |
| 587 | /* |
| 588 | * The first record was invalid, so return failure, but first read another |
| 589 | * record, since we asked a junk question and have to clear the answer out. |
| 590 | */ |
| 591 | switch (read_addr2line_record(a2l, NULL, NULL, NULL)) { |
| 592 | case -1: |
Namhyung Kim | d5e33ce | 2022-12-15 11:28:13 -0800 | [diff] [blame] | 593 | if (!symbol_conf.disable_add2line_warn) |
| 594 | pr_warning("%s %s: could not read delimiter record\n", |
| 595 | __func__, dso_name); |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 596 | break; |
| 597 | case 0: |
| 598 | /* As expected. */ |
| 599 | break; |
| 600 | default: |
Namhyung Kim | d5e33ce | 2022-12-15 11:28:13 -0800 | [diff] [blame] | 601 | if (!symbol_conf.disable_add2line_warn) |
| 602 | pr_warning("%s %s: unexpected record instead of sentinel", |
| 603 | __func__, dso_name); |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 604 | break; |
| 605 | } |
| 606 | goto out; |
| 607 | default: |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | if (file) { |
| 612 | *file = strdup(record_filename); |
| 613 | ret = 1; |
| 614 | } |
| 615 | if (line_nr) |
| 616 | *line_nr = record_line_nr; |
| 617 | |
| 618 | if (unwind_inlines) { |
| 619 | if (node && inline_list__append_record(dso, node, sym, |
| 620 | record_function, |
| 621 | record_filename, |
| 622 | record_line_nr)) { |
| 623 | ret = 0; |
| 624 | goto out; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* We have to read the records even if we don't care about the inline info. */ |
| 629 | while ((record_status = read_addr2line_record(a2l, |
| 630 | &record_function, |
| 631 | &record_filename, |
| 632 | &record_line_nr)) == 1) { |
| 633 | if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) { |
| 634 | if (inline_list__append_record(dso, node, sym, |
| 635 | record_function, |
| 636 | record_filename, |
| 637 | record_line_nr)) { |
| 638 | ret = 0; |
| 639 | goto out; |
| 640 | } |
| 641 | ret = 1; /* found at least one inline frame */ |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | out: |
| 646 | free(record_function); |
| 647 | free(record_filename); |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | void dso__free_a2l(struct dso *dso) |
| 652 | { |
| 653 | struct a2l_subprocess *a2l = dso->a2l; |
| 654 | |
| 655 | if (!a2l) |
| 656 | return; |
| 657 | |
| 658 | addr2line_subprocess_cleanup(a2l); |
| 659 | |
| 660 | dso->a2l = NULL; |
| 661 | } |
| 662 | |
| 663 | #endif /* HAVE_LIBBFD_SUPPORT */ |
| 664 | |
| 665 | static struct inline_node *addr2inlines(const char *dso_name, u64 addr, |
| 666 | struct dso *dso, struct symbol *sym) |
| 667 | { |
| 668 | struct inline_node *node; |
| 669 | |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 670 | node = zalloc(sizeof(*node)); |
| 671 | if (node == NULL) { |
| 672 | perror("not enough memory for the inline node"); |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 673 | return NULL; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | INIT_LIST_HEAD(&node->val); |
| 677 | node->addr = addr; |
| 678 | |
Tony Garnock-Jones | be8ecc5 | 2021-09-16 14:09:39 +0200 | [diff] [blame] | 679 | addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym); |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 680 | return node; |
| 681 | } |
| 682 | |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 683 | /* |
| 684 | * Number of addr2line failures (without success) before disabling it for that |
| 685 | * dso. |
| 686 | */ |
| 687 | #define A2L_FAIL_LIMIT 123 |
| 688 | |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 689 | char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym, |
Jin Yao | 935f5a9 | 2017-12-30 00:26:52 +0800 | [diff] [blame] | 690 | bool show_sym, bool show_addr, bool unwind_inlines, |
| 691 | u64 ip) |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 692 | { |
David Ahern | a949fff | 2013-10-09 21:51:31 -0600 | [diff] [blame] | 693 | char *file = NULL; |
| 694 | unsigned line = 0; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 695 | char *srcline; |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 696 | const char *dso_name; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 697 | |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 698 | if (!dso->has_srcline) |
Andi Kleen | 23f0981 | 2014-11-12 18:05:24 -0800 | [diff] [blame] | 699 | goto out; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 700 | |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 701 | dso_name = dso__name(dso); |
| 702 | if (dso_name == NULL) |
Namhyung Kim | 58d91a0 | 2013-09-11 14:09:29 +0900 | [diff] [blame] | 703 | goto out; |
| 704 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 705 | if (!addr2line(dso_name, addr, &file, &line, dso, |
| 706 | unwind_inlines, NULL, sym)) |
Namhyung Kim | 58d91a0 | 2013-09-11 14:09:29 +0900 | [diff] [blame] | 707 | goto out; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 708 | |
Milian Wolff | 2be8832 | 2017-10-09 22:32:58 +0200 | [diff] [blame] | 709 | srcline = srcline_from_fileline(file, line); |
| 710 | free(file); |
| 711 | |
| 712 | if (!srcline) |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 713 | goto out; |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 714 | |
| 715 | dso->a2l_fails = 0; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 716 | |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 717 | return srcline; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 718 | |
| 719 | out: |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 720 | if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { |
| 721 | dso->has_srcline = 0; |
| 722 | dso__free_a2l(dso); |
| 723 | } |
Milian Wolff | 5dfa210 | 2017-03-18 22:49:28 +0100 | [diff] [blame] | 724 | |
| 725 | if (!show_addr) |
| 726 | return (show_sym && sym) ? |
Namhyung Kim | ea335ef | 2022-12-15 11:28:09 -0800 | [diff] [blame] | 727 | strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN; |
Milian Wolff | 5dfa210 | 2017-03-18 22:49:28 +0100 | [diff] [blame] | 728 | |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 729 | if (sym) { |
Wang Nan | ac931f87 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 730 | if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "", |
Jin Yao | 935f5a9 | 2017-12-30 00:26:52 +0800 | [diff] [blame] | 731 | ip - sym->start) < 0) |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 732 | return SRCLINE_UNKNOWN; |
Wang Nan | ac931f87 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 733 | } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0) |
Andi Kleen | 23f0981 | 2014-11-12 18:05:24 -0800 | [diff] [blame] | 734 | return SRCLINE_UNKNOWN; |
| 735 | return srcline; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 736 | } |
| 737 | |
Andi Kleen | dd2e18e | 2018-12-03 16:18:48 -0800 | [diff] [blame] | 738 | /* Returns filename and fills in line number in line */ |
| 739 | char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line) |
| 740 | { |
| 741 | char *file = NULL; |
| 742 | const char *dso_name; |
| 743 | |
| 744 | if (!dso->has_srcline) |
| 745 | goto out; |
| 746 | |
| 747 | dso_name = dso__name(dso); |
| 748 | if (dso_name == NULL) |
| 749 | goto out; |
| 750 | |
| 751 | if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL)) |
| 752 | goto out; |
| 753 | |
| 754 | dso->a2l_fails = 0; |
| 755 | return file; |
| 756 | |
| 757 | out: |
| 758 | if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { |
| 759 | dso->has_srcline = 0; |
| 760 | dso__free_a2l(dso); |
| 761 | } |
| 762 | |
| 763 | return NULL; |
| 764 | } |
| 765 | |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 766 | void free_srcline(char *srcline) |
| 767 | { |
| 768 | if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0) |
| 769 | free(srcline); |
| 770 | } |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 771 | |
| 772 | char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym, |
Jin Yao | 935f5a9 | 2017-12-30 00:26:52 +0800 | [diff] [blame] | 773 | bool show_sym, bool show_addr, u64 ip) |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 774 | { |
Jin Yao | 935f5a9 | 2017-12-30 00:26:52 +0800 | [diff] [blame] | 775 | return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip); |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 776 | } |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 777 | |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 778 | struct srcline_node { |
| 779 | u64 addr; |
| 780 | char *srcline; |
| 781 | struct rb_node rb_node; |
| 782 | }; |
| 783 | |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 784 | void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline) |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 785 | { |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 786 | struct rb_node **p = &tree->rb_root.rb_node; |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 787 | struct rb_node *parent = NULL; |
| 788 | struct srcline_node *i, *node; |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 789 | bool leftmost = true; |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 790 | |
| 791 | node = zalloc(sizeof(struct srcline_node)); |
| 792 | if (!node) { |
| 793 | perror("not enough memory for the srcline node"); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | node->addr = addr; |
| 798 | node->srcline = srcline; |
| 799 | |
| 800 | while (*p != NULL) { |
| 801 | parent = *p; |
| 802 | i = rb_entry(parent, struct srcline_node, rb_node); |
| 803 | if (addr < i->addr) |
| 804 | p = &(*p)->rb_left; |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 805 | else { |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 806 | p = &(*p)->rb_right; |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 807 | leftmost = false; |
| 808 | } |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 809 | } |
| 810 | rb_link_node(&node->rb_node, parent, p); |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 811 | rb_insert_color_cached(&node->rb_node, tree, leftmost); |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 814 | char *srcline__tree_find(struct rb_root_cached *tree, u64 addr) |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 815 | { |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 816 | struct rb_node *n = tree->rb_root.rb_node; |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 817 | |
| 818 | while (n) { |
| 819 | struct srcline_node *i = rb_entry(n, struct srcline_node, |
| 820 | rb_node); |
| 821 | |
| 822 | if (addr < i->addr) |
| 823 | n = n->rb_left; |
| 824 | else if (addr > i->addr) |
| 825 | n = n->rb_right; |
| 826 | else |
| 827 | return i->srcline; |
| 828 | } |
| 829 | |
| 830 | return NULL; |
| 831 | } |
| 832 | |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 833 | void srcline__tree_delete(struct rb_root_cached *tree) |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 834 | { |
| 835 | struct srcline_node *pos; |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 836 | struct rb_node *next = rb_first_cached(tree); |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 837 | |
| 838 | while (next) { |
| 839 | pos = rb_entry(next, struct srcline_node, rb_node); |
| 840 | next = rb_next(&pos->rb_node); |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 841 | rb_erase_cached(&pos->rb_node, tree); |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 842 | free_srcline(pos->srcline); |
| 843 | zfree(&pos); |
| 844 | } |
| 845 | } |
| 846 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 847 | struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr, |
| 848 | struct symbol *sym) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 849 | { |
| 850 | const char *dso_name; |
| 851 | |
| 852 | dso_name = dso__name(dso); |
| 853 | if (dso_name == NULL) |
| 854 | return NULL; |
| 855 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 856 | return addr2inlines(dso_name, addr, dso, sym); |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | void inline_node__delete(struct inline_node *node) |
| 860 | { |
| 861 | struct inline_list *ilist, *tmp; |
| 862 | |
| 863 | list_for_each_entry_safe(ilist, tmp, &node->val, list) { |
| 864 | list_del_init(&ilist->list); |
Milian Wolff | 2be8832 | 2017-10-09 22:32:58 +0200 | [diff] [blame] | 865 | free_srcline(ilist->srcline); |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame] | 866 | /* only the inlined symbols are owned by the list */ |
| 867 | if (ilist->symbol && ilist->symbol->inlined) |
| 868 | symbol__delete(ilist->symbol); |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 869 | free(ilist); |
| 870 | } |
| 871 | |
| 872 | free(node); |
| 873 | } |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 874 | |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 875 | void inlines__tree_insert(struct rb_root_cached *tree, |
| 876 | struct inline_node *inlines) |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 877 | { |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 878 | struct rb_node **p = &tree->rb_root.rb_node; |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 879 | struct rb_node *parent = NULL; |
| 880 | const u64 addr = inlines->addr; |
| 881 | struct inline_node *i; |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 882 | bool leftmost = true; |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 883 | |
| 884 | while (*p != NULL) { |
| 885 | parent = *p; |
| 886 | i = rb_entry(parent, struct inline_node, rb_node); |
| 887 | if (addr < i->addr) |
| 888 | p = &(*p)->rb_left; |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 889 | else { |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 890 | p = &(*p)->rb_right; |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 891 | leftmost = false; |
| 892 | } |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 893 | } |
| 894 | rb_link_node(&inlines->rb_node, parent, p); |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 895 | rb_insert_color_cached(&inlines->rb_node, tree, leftmost); |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 896 | } |
| 897 | |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 898 | struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr) |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 899 | { |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 900 | struct rb_node *n = tree->rb_root.rb_node; |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 901 | |
| 902 | while (n) { |
| 903 | struct inline_node *i = rb_entry(n, struct inline_node, |
| 904 | rb_node); |
| 905 | |
| 906 | if (addr < i->addr) |
| 907 | n = n->rb_left; |
| 908 | else if (addr > i->addr) |
| 909 | n = n->rb_right; |
| 910 | else |
| 911 | return i; |
| 912 | } |
| 913 | |
| 914 | return NULL; |
| 915 | } |
| 916 | |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 917 | void inlines__tree_delete(struct rb_root_cached *tree) |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 918 | { |
| 919 | struct inline_node *pos; |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 920 | struct rb_node *next = rb_first_cached(tree); |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 921 | |
| 922 | while (next) { |
| 923 | pos = rb_entry(next, struct inline_node, rb_node); |
| 924 | next = rb_next(&pos->rb_node); |
Davidlohr Bueso | 55ecd63 | 2018-12-06 11:18:15 -0800 | [diff] [blame] | 925 | rb_erase_cached(&pos->rb_node, tree); |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 926 | inline_node__delete(pos); |
| 927 | } |
| 928 | } |