blob: c0af61b355eda003fc2226963bbac2c2d17a6b81 [file] [log] [blame]
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03001#include <inttypes.h>
Namhyung Kimf048d542013-09-11 14:09:28 +09002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include <linux/kernel.h>
7
Namhyung Kim86c98ca2013-09-11 14:09:30 +09008#include "util/dso.h"
Namhyung Kimf048d542013-09-11 14:09:28 +09009#include "util/util.h"
10#include "util/debug.h"
Jin Yaoa64489c2017-03-26 04:34:26 +080011#include "util/callchain.h"
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030012#include "srcline.h"
Namhyung Kimf048d542013-09-11 14:09:28 +090013
Andi Kleen85c116a2014-11-12 18:05:27 -080014#include "symbol.h"
15
Andi Kleena9710ba2015-08-07 15:24:05 -070016bool srcline_full_filename;
17
Jin Yao55803382017-03-26 04:34:25 +080018static 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 Wolfffea0cf82017-10-09 22:32:57 +020036static int inline_list__append(struct symbol *symbol, char *filename,
37 int line_nr, struct inline_node *node)
Jin Yaoa64489c2017-03-26 04:34:26 +080038{
39 struct inline_list *ilist;
Jin Yaoa64489c2017-03-26 04:34:26 +080040
41 ilist = zalloc(sizeof(*ilist));
42 if (ilist == NULL)
43 return -1;
44
Milian Wolfffea0cf82017-10-09 22:32:57 +020045 ilist->symbol = symbol;
Jin Yaoa64489c2017-03-26 04:34:26 +080046 ilist->filename = filename;
47 ilist->line_nr = line_nr;
48
Milian Wolff28071f52017-05-24 15:21:27 +090049 if (callchain_param.order == ORDER_CALLEE)
50 list_add_tail(&ilist->list, &node->val);
51 else
52 list_add(&ilist->list, &node->val);
Jin Yaoa64489c2017-03-26 04:34:26 +080053
54 return 0;
55}
56
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +090057#ifdef HAVE_LIBBFD_SUPPORT
58
59/*
60 * Implement addr2line using libbfd.
61 */
62#define PACKAGE "perf"
63#include <bfd.h>
64
65struct a2l_data {
66 const char *input;
Wang Nanac931f82014-12-16 14:19:06 +080067 u64 addr;
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +090068
69 bool found;
70 const char *filename;
71 const char *funcname;
72 unsigned line;
73
74 bfd *abfd;
75 asymbol **syms;
76};
77
78static 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
93static 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
126static 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 Wolffd964b1c2017-08-06 23:24:45 +0200148
149 if (a2l->filename && !strlen(a2l->filename))
150 a2l->filename = NULL;
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900151}
152
153static 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
179out:
180 if (a2l) {
Namhyung Kim7d16c632014-01-09 23:07:59 +0900181 zfree((char **)&a2l->input);
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900182 free(a2l);
183 }
184 bfd_close(abfd);
185 return NULL;
186}
187
188static void addr2line_cleanup(struct a2l_data *a2l)
189{
190 if (a2l->abfd)
191 bfd_close(a2l->abfd);
Namhyung Kim7d16c632014-01-09 23:07:59 +0900192 zfree((char **)&a2l->input);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300193 zfree(&a2l->syms);
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900194 free(a2l);
195}
196
Andi Kleen2f84b422015-09-01 11:47:19 -0700197#define MAX_INLINE_NEST 1024
198
Milian Wolfffea0cf82017-10-09 22:32:57 +0200199static 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 Wolff4d53b9d2017-05-24 15:21:28 +0900234static int inline_list__append_dso_a2l(struct dso *dso,
Milian Wolfffea0cf82017-10-09 22:32:57 +0200235 struct inline_node *node,
236 struct symbol *sym)
Milian Wolff4d53b9d2017-05-24 15:21:28 +0900237{
238 struct a2l_data *a2l = dso->a2l;
Milian Wolfffea0cf82017-10-09 22:32:57 +0200239 struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
Milian Wolff4d53b9d2017-05-24 15:21:28 +0900240
Milian Wolfffea0cf82017-10-09 22:32:57 +0200241 return inline_list__append(inline_sym, strdup(a2l->filename),
242 a2l->line, node);
Milian Wolff4d53b9d2017-05-24 15:21:28 +0900243}
244
Wang Nanac931f82014-12-16 14:19:06 +0800245static int addr2line(const char *dso_name, u64 addr,
Andi Kleen2f84b422015-09-01 11:47:19 -0700246 char **file, unsigned int *line, struct dso *dso,
Milian Wolfffea0cf82017-10-09 22:32:57 +0200247 bool unwind_inlines, struct inline_node *node,
248 struct symbol *sym)
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900249{
250 int ret = 0;
Adrian Hunter454ff002013-12-03 09:23:07 +0200251 struct a2l_data *a2l = dso->a2l;
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900252
Adrian Hunter454ff002013-12-03 09:23:07 +0200253 if (!a2l) {
254 dso->a2l = addr2line_init(dso_name);
255 a2l = dso->a2l;
256 }
257
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900258 if (a2l == NULL) {
259 pr_warning("addr2line_init failed for %s\n", dso_name);
260 return 0;
261 }
262
263 a2l->addr = addr;
Adrian Hunter454ff002013-12-03 09:23:07 +0200264 a2l->found = false;
265
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900266 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
267
Milian Wolffb21cc972017-05-24 15:21:24 +0900268 if (!a2l->found)
269 return 0;
270
271 if (unwind_inlines) {
Andi Kleen2f84b422015-09-01 11:47:19 -0700272 int cnt = 0;
273
Milian Wolfffea0cf82017-10-09 22:32:57 +0200274 if (node && inline_list__append_dso_a2l(dso, node, sym))
Milian Wolff4d53b9d2017-05-24 15:21:28 +0900275 return 0;
276
Andi Kleen2f84b422015-09-01 11:47:19 -0700277 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
278 &a2l->funcname, &a2l->line) &&
Jin Yaoa64489c2017-03-26 04:34:26 +0800279 cnt++ < MAX_INLINE_NEST) {
280
Milian Wolffd964b1c2017-08-06 23:24:45 +0200281 if (a2l->filename && !strlen(a2l->filename))
282 a2l->filename = NULL;
283
Jin Yaoa64489c2017-03-26 04:34:26 +0800284 if (node != NULL) {
Milian Wolfffea0cf82017-10-09 22:32:57 +0200285 if (inline_list__append_dso_a2l(dso, node, sym))
Jin Yaoa64489c2017-03-26 04:34:26 +0800286 return 0;
Milian Wolffb21cc972017-05-24 15:21:24 +0900287 // found at least one inline frame
288 ret = 1;
Jin Yaoa64489c2017-03-26 04:34:26 +0800289 }
290 }
Andi Kleen2f84b422015-09-01 11:47:19 -0700291 }
292
Milian Wolffb21cc972017-05-24 15:21:24 +0900293 if (file) {
294 *file = a2l->filename ? strdup(a2l->filename) : NULL;
295 ret = *file ? 1 : 0;
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900296 }
297
Milian Wolffb21cc972017-05-24 15:21:24 +0900298 if (line)
299 *line = a2l->line;
300
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900301 return ret;
302}
303
Adrian Hunter454ff002013-12-03 09:23:07 +0200304void 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 Yaoa64489c2017-03-26 04:34:26 +0800316static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
Milian Wolfffea0cf82017-10-09 22:32:57 +0200317 struct dso *dso, struct symbol *sym)
Jin Yaoa64489c2017-03-26 04:34:26 +0800318{
Jin Yaoa64489c2017-03-26 04:34:26 +0800319 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 Wolfffea0cf82017-10-09 22:32:57 +0200330 if (!addr2line(dso_name, addr, NULL, NULL, dso, TRUE, node, sym))
Jin Yaoa64489c2017-03-26 04:34:26 +0800331 goto out_free_inline_node;
332
333 if (list_empty(&node->val))
334 goto out_free_inline_node;
335
336 return node;
337
338out_free_inline_node:
339 inline_node__delete(node);
340 return NULL;
341}
342
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900343#else /* HAVE_LIBBFD_SUPPORT */
344
Jin Yao55803382017-03-26 04:34:25 +0800345static 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 Nanac931f82014-12-16 14:19:06 +0800366static int addr2line(const char *dso_name, u64 addr,
Adrian Hunter454ff002013-12-03 09:23:07 +0200367 char **file, unsigned int *line_nr,
Andi Kleen2f84b422015-09-01 11:47:19 -0700368 struct dso *dso __maybe_unused,
Jin Yaoa64489c2017-03-26 04:34:26 +0800369 bool unwind_inlines __maybe_unused,
Milian Wolfffea0cf82017-10-09 22:32:57 +0200370 struct inline_node *node __maybe_unused,
371 struct symbol *sym __maybe_unused)
Namhyung Kimf048d542013-09-11 14:09:28 +0900372{
373 FILE *fp;
374 char cmd[PATH_MAX];
375 char *filename = NULL;
376 size_t len;
Namhyung Kimf048d542013-09-11 14:09:28 +0900377 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 Yao55803382017-03-26 04:34:25 +0800393 ret = filename_split(filename, line_nr);
394 if (ret != 1) {
Namhyung Kimf048d542013-09-11 14:09:28 +0900395 free(filename);
396 goto out;
397 }
398
Jin Yao55803382017-03-26 04:34:25 +0800399 *file = filename;
400
Namhyung Kimf048d542013-09-11 14:09:28 +0900401out:
402 pclose(fp);
403 return ret;
404}
Adrian Hunter454ff002013-12-03 09:23:07 +0200405
406void dso__free_a2l(struct dso *dso __maybe_unused)
407{
408}
409
Jin Yaoa64489c2017-03-26 04:34:26 +0800410static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
Milian Wolfffea0cf82017-10-09 22:32:57 +0200411 struct dso *dso __maybe_unused,
412 struct symbol *sym)
Jin Yaoa64489c2017-03-26 04:34:26 +0800413{
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 Wolfffea0cf82017-10-09 22:32:57 +0200440
Jin Yaoa64489c2017-03-26 04:34:26 +0800441 if (filename_split(filename, &line_nr) != 1) {
442 free(filename);
443 goto out;
444 }
445
Milian Wolfffea0cf82017-10-09 22:32:57 +0200446 if (inline_list__append(sym, filename, line_nr, node) != 0)
Jin Yaoa64489c2017-03-26 04:34:26 +0800447 goto out;
448
449 filename = NULL;
450 }
451
452out:
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 Vitillo2f48fcd2013-09-11 14:09:32 +0900463#endif /* HAVE_LIBBFD_SUPPORT */
Namhyung Kimf048d542013-09-11 14:09:28 +0900464
Adrian Hunter906049c82013-12-03 09:23:10 +0200465/*
466 * Number of addr2line failures (without success) before disabling it for that
467 * dso.
468 */
469#define A2L_FAIL_LIMIT 123
470
Andi Kleen2f84b422015-09-01 11:47:19 -0700471char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
Milian Wolff5dfa2102017-03-18 22:49:28 +0100472 bool show_sym, bool show_addr, bool unwind_inlines)
Namhyung Kimf048d542013-09-11 14:09:28 +0900473{
David Aherna949fff2013-10-09 21:51:31 -0600474 char *file = NULL;
475 unsigned line = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900476 char *srcline;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300477 const char *dso_name;
Namhyung Kimf048d542013-09-11 14:09:28 +0900478
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900479 if (!dso->has_srcline)
Andi Kleen23f09812014-11-12 18:05:24 -0800480 goto out;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900481
Jin Yao55803382017-03-26 04:34:25 +0800482 dso_name = dso__name(dso);
483 if (dso_name == NULL)
Namhyung Kim58d91a02013-09-11 14:09:29 +0900484 goto out;
485
Milian Wolfffea0cf82017-10-09 22:32:57 +0200486 if (!addr2line(dso_name, addr, &file, &line, dso,
487 unwind_inlines, NULL, sym))
Namhyung Kim58d91a02013-09-11 14:09:29 +0900488 goto out;
Namhyung Kimf048d542013-09-11 14:09:28 +0900489
Andi Kleena9710ba2015-08-07 15:24:05 -0700490 if (asprintf(&srcline, "%s:%u",
491 srcline_full_filename ? file : basename(file),
492 line) < 0) {
Adrian Hunter906049c82013-12-03 09:23:10 +0200493 free(file);
494 goto out;
495 }
496
497 dso->a2l_fails = 0;
Namhyung Kimf048d542013-09-11 14:09:28 +0900498
499 free(file);
Namhyung Kimf048d542013-09-11 14:09:28 +0900500 return srcline;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900501
502out:
Adrian Hunter906049c82013-12-03 09:23:10 +0200503 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
504 dso->has_srcline = 0;
505 dso__free_a2l(dso);
506 }
Milian Wolff5dfa2102017-03-18 22:49:28 +0100507
508 if (!show_addr)
509 return (show_sym && sym) ?
510 strndup(sym->name, sym->namelen) : NULL;
511
Andi Kleen85c116a2014-11-12 18:05:27 -0800512 if (sym) {
Wang Nanac931f82014-12-16 14:19:06 +0800513 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
Andi Kleen85c116a2014-11-12 18:05:27 -0800514 addr - sym->start) < 0)
515 return SRCLINE_UNKNOWN;
Wang Nanac931f82014-12-16 14:19:06 +0800516 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
Andi Kleen23f09812014-11-12 18:05:24 -0800517 return SRCLINE_UNKNOWN;
518 return srcline;
Namhyung Kimf048d542013-09-11 14:09:28 +0900519}
520
521void free_srcline(char *srcline)
522{
523 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
524 free(srcline);
525}
Andi Kleen2f84b422015-09-01 11:47:19 -0700526
527char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
Milian Wolff5dfa2102017-03-18 22:49:28 +0100528 bool show_sym, bool show_addr)
Andi Kleen2f84b422015-09-01 11:47:19 -0700529{
Milian Wolff5dfa2102017-03-18 22:49:28 +0100530 return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
Andi Kleen2f84b422015-09-01 11:47:19 -0700531}
Jin Yaoa64489c2017-03-26 04:34:26 +0800532
Milian Wolfffea0cf82017-10-09 22:32:57 +0200533struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
534 struct symbol *sym)
Jin Yaoa64489c2017-03-26 04:34:26 +0800535{
536 const char *dso_name;
537
538 dso_name = dso__name(dso);
539 if (dso_name == NULL)
540 return NULL;
541
Milian Wolfffea0cf82017-10-09 22:32:57 +0200542 return addr2inlines(dso_name, addr, dso, sym);
Jin Yaoa64489c2017-03-26 04:34:26 +0800543}
544
545void 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 Wolfffea0cf82017-10-09 22:32:57 +0200552 /* only the inlined symbols are owned by the list */
553 if (ilist->symbol && ilist->symbol->inlined)
554 symbol__delete(ilist->symbol);
Jin Yaoa64489c2017-03-26 04:34:26 +0800555 free(ilist);
556 }
557
558 free(node);
559}