Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 1 | #include <inttypes.h> |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | |
Namhyung Kim | 86c98ca | 2013-09-11 14:09:30 +0900 | [diff] [blame] | 8 | #include "util/dso.h" |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 9 | #include "util/util.h" |
| 10 | #include "util/debug.h" |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 11 | #include "util/callchain.h" |
Arnaldo Carvalho de Melo | 632a5ca | 2017-04-17 16:30:49 -0300 | [diff] [blame] | 12 | #include "srcline.h" |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 13 | |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 14 | #include "symbol.h" |
| 15 | |
Andi Kleen | a9710ba | 2015-08-07 15:24:05 -0700 | [diff] [blame] | 16 | bool srcline_full_filename; |
| 17 | |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 18 | static const char *dso__name(struct dso *dso) |
| 19 | { |
| 20 | const char *dso_name; |
| 21 | |
| 22 | if (dso->symsrc_filename) |
| 23 | dso_name = dso->symsrc_filename; |
| 24 | else |
| 25 | dso_name = dso->long_name; |
| 26 | |
| 27 | if (dso_name[0] == '[') |
| 28 | return NULL; |
| 29 | |
| 30 | if (!strncmp(dso_name, "/tmp/perf-", 10)) |
| 31 | return NULL; |
| 32 | |
| 33 | return dso_name; |
| 34 | } |
| 35 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 36 | static int inline_list__append(struct symbol *symbol, char *filename, |
| 37 | int line_nr, struct inline_node *node) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 38 | { |
| 39 | struct inline_list *ilist; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 40 | |
| 41 | ilist = zalloc(sizeof(*ilist)); |
| 42 | if (ilist == NULL) |
| 43 | return -1; |
| 44 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 45 | ilist->symbol = symbol; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 46 | ilist->filename = filename; |
| 47 | ilist->line_nr = line_nr; |
| 48 | |
Milian Wolff | 28071f5 | 2017-05-24 15:21:27 +0900 | [diff] [blame] | 49 | if (callchain_param.order == ORDER_CALLEE) |
| 50 | list_add_tail(&ilist->list, &node->val); |
| 51 | else |
| 52 | list_add(&ilist->list, &node->val); |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 57 | #ifdef HAVE_LIBBFD_SUPPORT |
| 58 | |
| 59 | /* |
| 60 | * Implement addr2line using libbfd. |
| 61 | */ |
| 62 | #define PACKAGE "perf" |
| 63 | #include <bfd.h> |
| 64 | |
| 65 | struct a2l_data { |
| 66 | const char *input; |
Wang Nan | ac931f8 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 67 | u64 addr; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 68 | |
| 69 | bool found; |
| 70 | const char *filename; |
| 71 | const char *funcname; |
| 72 | unsigned line; |
| 73 | |
| 74 | bfd *abfd; |
| 75 | asymbol **syms; |
| 76 | }; |
| 77 | |
| 78 | static int bfd_error(const char *string) |
| 79 | { |
| 80 | const char *errmsg; |
| 81 | |
| 82 | errmsg = bfd_errmsg(bfd_get_error()); |
| 83 | fflush(stdout); |
| 84 | |
| 85 | if (string) |
| 86 | pr_debug("%s: %s\n", string, errmsg); |
| 87 | else |
| 88 | pr_debug("%s\n", errmsg); |
| 89 | |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) |
| 94 | { |
| 95 | long storage; |
| 96 | long symcount; |
| 97 | asymbol **syms; |
| 98 | bfd_boolean dynamic = FALSE; |
| 99 | |
| 100 | if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) |
| 101 | return bfd_error(bfd_get_filename(abfd)); |
| 102 | |
| 103 | storage = bfd_get_symtab_upper_bound(abfd); |
| 104 | if (storage == 0L) { |
| 105 | storage = bfd_get_dynamic_symtab_upper_bound(abfd); |
| 106 | dynamic = TRUE; |
| 107 | } |
| 108 | if (storage < 0L) |
| 109 | return bfd_error(bfd_get_filename(abfd)); |
| 110 | |
| 111 | syms = malloc(storage); |
| 112 | if (dynamic) |
| 113 | symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); |
| 114 | else |
| 115 | symcount = bfd_canonicalize_symtab(abfd, syms); |
| 116 | |
| 117 | if (symcount < 0) { |
| 118 | free(syms); |
| 119 | return bfd_error(bfd_get_filename(abfd)); |
| 120 | } |
| 121 | |
| 122 | a2l->syms = syms; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static void find_address_in_section(bfd *abfd, asection *section, void *data) |
| 127 | { |
| 128 | bfd_vma pc, vma; |
| 129 | bfd_size_type size; |
| 130 | struct a2l_data *a2l = data; |
| 131 | |
| 132 | if (a2l->found) |
| 133 | return; |
| 134 | |
| 135 | if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0) |
| 136 | return; |
| 137 | |
| 138 | pc = a2l->addr; |
| 139 | vma = bfd_get_section_vma(abfd, section); |
| 140 | size = bfd_get_section_size(section); |
| 141 | |
| 142 | if (pc < vma || pc >= vma + size) |
| 143 | return; |
| 144 | |
| 145 | a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, |
| 146 | &a2l->filename, &a2l->funcname, |
| 147 | &a2l->line); |
Milian Wolff | d964b1c | 2017-08-06 23:24:45 +0200 | [diff] [blame] | 148 | |
| 149 | if (a2l->filename && !strlen(a2l->filename)) |
| 150 | a2l->filename = NULL; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static struct a2l_data *addr2line_init(const char *path) |
| 154 | { |
| 155 | bfd *abfd; |
| 156 | struct a2l_data *a2l = NULL; |
| 157 | |
| 158 | abfd = bfd_openr(path, NULL); |
| 159 | if (abfd == NULL) |
| 160 | return NULL; |
| 161 | |
| 162 | if (!bfd_check_format(abfd, bfd_object)) |
| 163 | goto out; |
| 164 | |
| 165 | a2l = zalloc(sizeof(*a2l)); |
| 166 | if (a2l == NULL) |
| 167 | goto out; |
| 168 | |
| 169 | a2l->abfd = abfd; |
| 170 | a2l->input = strdup(path); |
| 171 | if (a2l->input == NULL) |
| 172 | goto out; |
| 173 | |
| 174 | if (slurp_symtab(abfd, a2l)) |
| 175 | goto out; |
| 176 | |
| 177 | return a2l; |
| 178 | |
| 179 | out: |
| 180 | if (a2l) { |
Namhyung Kim | 7d16c63 | 2014-01-09 23:07:59 +0900 | [diff] [blame] | 181 | zfree((char **)&a2l->input); |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 182 | free(a2l); |
| 183 | } |
| 184 | bfd_close(abfd); |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | static void addr2line_cleanup(struct a2l_data *a2l) |
| 189 | { |
| 190 | if (a2l->abfd) |
| 191 | bfd_close(a2l->abfd); |
Namhyung Kim | 7d16c63 | 2014-01-09 23:07:59 +0900 | [diff] [blame] | 192 | zfree((char **)&a2l->input); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 193 | zfree(&a2l->syms); |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 194 | free(a2l); |
| 195 | } |
| 196 | |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 197 | #define MAX_INLINE_NEST 1024 |
| 198 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 199 | static struct symbol *new_inline_sym(struct dso *dso, |
| 200 | struct symbol *base_sym, |
| 201 | const char *funcname) |
| 202 | { |
| 203 | struct symbol *inline_sym; |
| 204 | char *demangled = NULL; |
| 205 | |
| 206 | if (dso) { |
| 207 | demangled = dso__demangle_sym(dso, 0, funcname); |
| 208 | if (demangled) |
| 209 | funcname = demangled; |
| 210 | } |
| 211 | |
| 212 | if (base_sym && strcmp(funcname, base_sym->name) == 0) { |
| 213 | /* reuse the real, existing symbol */ |
| 214 | inline_sym = base_sym; |
| 215 | /* ensure that we don't alias an inlined symbol, which could |
| 216 | * lead to double frees in inline_node__delete |
| 217 | */ |
| 218 | assert(!base_sym->inlined); |
| 219 | } else { |
| 220 | /* create a fake symbol for the inline frame */ |
| 221 | inline_sym = symbol__new(base_sym ? base_sym->start : 0, |
| 222 | base_sym ? base_sym->end : 0, |
| 223 | base_sym ? base_sym->binding : 0, |
| 224 | funcname); |
| 225 | if (inline_sym) |
| 226 | inline_sym->inlined = 1; |
| 227 | } |
| 228 | |
| 229 | free(demangled); |
| 230 | |
| 231 | return inline_sym; |
| 232 | } |
| 233 | |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 234 | static int inline_list__append_dso_a2l(struct dso *dso, |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 235 | struct inline_node *node, |
| 236 | struct symbol *sym) |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 237 | { |
| 238 | struct a2l_data *a2l = dso->a2l; |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 239 | struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname); |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 240 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 241 | return inline_list__append(inline_sym, strdup(a2l->filename), |
| 242 | a2l->line, node); |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 243 | } |
| 244 | |
Wang Nan | ac931f8 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 245 | static int addr2line(const char *dso_name, u64 addr, |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 246 | char **file, unsigned int *line, struct dso *dso, |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 247 | bool unwind_inlines, struct inline_node *node, |
| 248 | struct symbol *sym) |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 249 | { |
| 250 | int ret = 0; |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 251 | struct a2l_data *a2l = dso->a2l; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 252 | |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 253 | if (!a2l) { |
| 254 | dso->a2l = addr2line_init(dso_name); |
| 255 | a2l = dso->a2l; |
| 256 | } |
| 257 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 258 | if (a2l == NULL) { |
| 259 | pr_warning("addr2line_init failed for %s\n", dso_name); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | a2l->addr = addr; |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 264 | a2l->found = false; |
| 265 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 266 | bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); |
| 267 | |
Milian Wolff | b21cc97 | 2017-05-24 15:21:24 +0900 | [diff] [blame] | 268 | if (!a2l->found) |
| 269 | return 0; |
| 270 | |
| 271 | if (unwind_inlines) { |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 272 | int cnt = 0; |
| 273 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 274 | if (node && inline_list__append_dso_a2l(dso, node, sym)) |
Milian Wolff | 4d53b9d | 2017-05-24 15:21:28 +0900 | [diff] [blame] | 275 | return 0; |
| 276 | |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 277 | while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, |
| 278 | &a2l->funcname, &a2l->line) && |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 279 | cnt++ < MAX_INLINE_NEST) { |
| 280 | |
Milian Wolff | d964b1c | 2017-08-06 23:24:45 +0200 | [diff] [blame] | 281 | if (a2l->filename && !strlen(a2l->filename)) |
| 282 | a2l->filename = NULL; |
| 283 | |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 284 | if (node != NULL) { |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 285 | if (inline_list__append_dso_a2l(dso, node, sym)) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 286 | return 0; |
Milian Wolff | b21cc97 | 2017-05-24 15:21:24 +0900 | [diff] [blame] | 287 | // found at least one inline frame |
| 288 | ret = 1; |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 289 | } |
| 290 | } |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Milian Wolff | b21cc97 | 2017-05-24 15:21:24 +0900 | [diff] [blame] | 293 | if (file) { |
| 294 | *file = a2l->filename ? strdup(a2l->filename) : NULL; |
| 295 | ret = *file ? 1 : 0; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 296 | } |
| 297 | |
Milian Wolff | b21cc97 | 2017-05-24 15:21:24 +0900 | [diff] [blame] | 298 | if (line) |
| 299 | *line = a2l->line; |
| 300 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 301 | return ret; |
| 302 | } |
| 303 | |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 304 | void dso__free_a2l(struct dso *dso) |
| 305 | { |
| 306 | struct a2l_data *a2l = dso->a2l; |
| 307 | |
| 308 | if (!a2l) |
| 309 | return; |
| 310 | |
| 311 | addr2line_cleanup(a2l); |
| 312 | |
| 313 | dso->a2l = NULL; |
| 314 | } |
| 315 | |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 316 | static struct inline_node *addr2inlines(const char *dso_name, u64 addr, |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 317 | struct dso *dso, struct symbol *sym) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 318 | { |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 319 | struct inline_node *node; |
| 320 | |
| 321 | node = zalloc(sizeof(*node)); |
| 322 | if (node == NULL) { |
| 323 | perror("not enough memory for the inline node"); |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | INIT_LIST_HEAD(&node->val); |
| 328 | node->addr = addr; |
| 329 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 330 | if (!addr2line(dso_name, addr, NULL, NULL, dso, TRUE, node, sym)) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 331 | goto out_free_inline_node; |
| 332 | |
| 333 | if (list_empty(&node->val)) |
| 334 | goto out_free_inline_node; |
| 335 | |
| 336 | return node; |
| 337 | |
| 338 | out_free_inline_node: |
| 339 | inline_node__delete(node); |
| 340 | return NULL; |
| 341 | } |
| 342 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 343 | #else /* HAVE_LIBBFD_SUPPORT */ |
| 344 | |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 345 | static int filename_split(char *filename, unsigned int *line_nr) |
| 346 | { |
| 347 | char *sep; |
| 348 | |
| 349 | sep = strchr(filename, '\n'); |
| 350 | if (sep) |
| 351 | *sep = '\0'; |
| 352 | |
| 353 | if (!strcmp(filename, "??:0")) |
| 354 | return 0; |
| 355 | |
| 356 | sep = strchr(filename, ':'); |
| 357 | if (sep) { |
| 358 | *sep++ = '\0'; |
| 359 | *line_nr = strtoul(sep, NULL, 0); |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Wang Nan | ac931f8 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 366 | static int addr2line(const char *dso_name, u64 addr, |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 367 | char **file, unsigned int *line_nr, |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 368 | struct dso *dso __maybe_unused, |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 369 | bool unwind_inlines __maybe_unused, |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 370 | struct inline_node *node __maybe_unused, |
| 371 | struct symbol *sym __maybe_unused) |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 372 | { |
| 373 | FILE *fp; |
| 374 | char cmd[PATH_MAX]; |
| 375 | char *filename = NULL; |
| 376 | size_t len; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 377 | int ret = 0; |
| 378 | |
| 379 | scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64, |
| 380 | dso_name, addr); |
| 381 | |
| 382 | fp = popen(cmd, "r"); |
| 383 | if (fp == NULL) { |
| 384 | pr_warning("popen failed for %s\n", dso_name); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | if (getline(&filename, &len, fp) < 0 || !len) { |
| 389 | pr_warning("addr2line has no output for %s\n", dso_name); |
| 390 | goto out; |
| 391 | } |
| 392 | |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 393 | ret = filename_split(filename, line_nr); |
| 394 | if (ret != 1) { |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 395 | free(filename); |
| 396 | goto out; |
| 397 | } |
| 398 | |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 399 | *file = filename; |
| 400 | |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 401 | out: |
| 402 | pclose(fp); |
| 403 | return ret; |
| 404 | } |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 405 | |
| 406 | void dso__free_a2l(struct dso *dso __maybe_unused) |
| 407 | { |
| 408 | } |
| 409 | |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 410 | static struct inline_node *addr2inlines(const char *dso_name, u64 addr, |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 411 | struct dso *dso __maybe_unused, |
| 412 | struct symbol *sym) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 413 | { |
| 414 | FILE *fp; |
| 415 | char cmd[PATH_MAX]; |
| 416 | struct inline_node *node; |
| 417 | char *filename = NULL; |
| 418 | size_t len; |
| 419 | unsigned int line_nr = 0; |
| 420 | |
| 421 | scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64, |
| 422 | dso_name, addr); |
| 423 | |
| 424 | fp = popen(cmd, "r"); |
| 425 | if (fp == NULL) { |
| 426 | pr_err("popen failed for %s\n", dso_name); |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | node = zalloc(sizeof(*node)); |
| 431 | if (node == NULL) { |
| 432 | perror("not enough memory for the inline node"); |
| 433 | goto out; |
| 434 | } |
| 435 | |
| 436 | INIT_LIST_HEAD(&node->val); |
| 437 | node->addr = addr; |
| 438 | |
| 439 | while (getline(&filename, &len, fp) != -1) { |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 440 | |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 441 | if (filename_split(filename, &line_nr) != 1) { |
| 442 | free(filename); |
| 443 | goto out; |
| 444 | } |
| 445 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 446 | if (inline_list__append(sym, filename, line_nr, node) != 0) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 447 | goto out; |
| 448 | |
| 449 | filename = NULL; |
| 450 | } |
| 451 | |
| 452 | out: |
| 453 | pclose(fp); |
| 454 | |
| 455 | if (list_empty(&node->val)) { |
| 456 | inline_node__delete(node); |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | return node; |
| 461 | } |
| 462 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 463 | #endif /* HAVE_LIBBFD_SUPPORT */ |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 464 | |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 465 | /* |
| 466 | * Number of addr2line failures (without success) before disabling it for that |
| 467 | * dso. |
| 468 | */ |
| 469 | #define A2L_FAIL_LIMIT 123 |
| 470 | |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 471 | char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym, |
Milian Wolff | 5dfa210 | 2017-03-18 22:49:28 +0100 | [diff] [blame] | 472 | bool show_sym, bool show_addr, bool unwind_inlines) |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 473 | { |
David Ahern | a949fff | 2013-10-09 21:51:31 -0600 | [diff] [blame] | 474 | char *file = NULL; |
| 475 | unsigned line = 0; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 476 | char *srcline; |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 477 | const char *dso_name; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 478 | |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 479 | if (!dso->has_srcline) |
Andi Kleen | 23f0981 | 2014-11-12 18:05:24 -0800 | [diff] [blame] | 480 | goto out; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 481 | |
Jin Yao | 5580338 | 2017-03-26 04:34:25 +0800 | [diff] [blame] | 482 | dso_name = dso__name(dso); |
| 483 | if (dso_name == NULL) |
Namhyung Kim | 58d91a0 | 2013-09-11 14:09:29 +0900 | [diff] [blame] | 484 | goto out; |
| 485 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 486 | if (!addr2line(dso_name, addr, &file, &line, dso, |
| 487 | unwind_inlines, NULL, sym)) |
Namhyung Kim | 58d91a0 | 2013-09-11 14:09:29 +0900 | [diff] [blame] | 488 | goto out; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 489 | |
Andi Kleen | a9710ba | 2015-08-07 15:24:05 -0700 | [diff] [blame] | 490 | if (asprintf(&srcline, "%s:%u", |
| 491 | srcline_full_filename ? file : basename(file), |
| 492 | line) < 0) { |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 493 | free(file); |
| 494 | goto out; |
| 495 | } |
| 496 | |
| 497 | dso->a2l_fails = 0; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 498 | |
| 499 | free(file); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 500 | return srcline; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 501 | |
| 502 | out: |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 503 | if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { |
| 504 | dso->has_srcline = 0; |
| 505 | dso__free_a2l(dso); |
| 506 | } |
Milian Wolff | 5dfa210 | 2017-03-18 22:49:28 +0100 | [diff] [blame] | 507 | |
| 508 | if (!show_addr) |
| 509 | return (show_sym && sym) ? |
| 510 | strndup(sym->name, sym->namelen) : NULL; |
| 511 | |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 512 | if (sym) { |
Wang Nan | ac931f8 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 513 | if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "", |
Andi Kleen | 85c116a | 2014-11-12 18:05:27 -0800 | [diff] [blame] | 514 | addr - sym->start) < 0) |
| 515 | return SRCLINE_UNKNOWN; |
Wang Nan | ac931f8 | 2014-12-16 14:19:06 +0800 | [diff] [blame] | 516 | } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0) |
Andi Kleen | 23f0981 | 2014-11-12 18:05:24 -0800 | [diff] [blame] | 517 | return SRCLINE_UNKNOWN; |
| 518 | return srcline; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void free_srcline(char *srcline) |
| 522 | { |
| 523 | if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0) |
| 524 | free(srcline); |
| 525 | } |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 526 | |
| 527 | char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym, |
Milian Wolff | 5dfa210 | 2017-03-18 22:49:28 +0100 | [diff] [blame] | 528 | bool show_sym, bool show_addr) |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 529 | { |
Milian Wolff | 5dfa210 | 2017-03-18 22:49:28 +0100 | [diff] [blame] | 530 | return __get_srcline(dso, addr, sym, show_sym, show_addr, false); |
Andi Kleen | 2f84b42 | 2015-09-01 11:47:19 -0700 | [diff] [blame] | 531 | } |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 532 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 533 | struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr, |
| 534 | struct symbol *sym) |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 535 | { |
| 536 | const char *dso_name; |
| 537 | |
| 538 | dso_name = dso__name(dso); |
| 539 | if (dso_name == NULL) |
| 540 | return NULL; |
| 541 | |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 542 | return addr2inlines(dso_name, addr, dso, sym); |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | void inline_node__delete(struct inline_node *node) |
| 546 | { |
| 547 | struct inline_list *ilist, *tmp; |
| 548 | |
| 549 | list_for_each_entry_safe(ilist, tmp, &node->val, list) { |
| 550 | list_del_init(&ilist->list); |
| 551 | zfree(&ilist->filename); |
Milian Wolff | fea0cf8 | 2017-10-09 22:32:57 +0200 | [diff] [blame^] | 552 | /* only the inlined symbols are owned by the list */ |
| 553 | if (ilist->symbol && ilist->symbol->inlined) |
| 554 | symbol__delete(ilist->symbol); |
Jin Yao | a64489c | 2017-03-26 04:34:26 +0800 | [diff] [blame] | 555 | free(ilist); |
| 556 | } |
| 557 | |
| 558 | free(node); |
| 559 | } |