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