blob: 3ce28f702b36bbb03387a163848f235f9f03b616 [file] [log] [blame]
Namhyung Kimf048d542013-09-11 14:09:28 +09001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include <linux/kernel.h>
6
Namhyung Kim86c98ca2013-09-11 14:09:30 +09007#include "util/dso.h"
Namhyung Kimf048d542013-09-11 14:09:28 +09008#include "util/util.h"
9#include "util/debug.h"
Jin Yaoa64489c2017-03-26 04:34:26 +080010#include "util/callchain.h"
Namhyung Kimf048d542013-09-11 14:09:28 +090011
Andi Kleen85c116a2014-11-12 18:05:27 -080012#include "symbol.h"
13
Andi Kleena9710ba2015-08-07 15:24:05 -070014bool srcline_full_filename;
15
Jin Yao55803382017-03-26 04:34:25 +080016static const char *dso__name(struct dso *dso)
17{
18 const char *dso_name;
19
20 if (dso->symsrc_filename)
21 dso_name = dso->symsrc_filename;
22 else
23 dso_name = dso->long_name;
24
25 if (dso_name[0] == '[')
26 return NULL;
27
28 if (!strncmp(dso_name, "/tmp/perf-", 10))
29 return NULL;
30
31 return dso_name;
32}
33
Jin Yaoa64489c2017-03-26 04:34:26 +080034static int inline_list__append(char *filename, char *funcname, int line_nr,
35 struct inline_node *node, struct dso *dso)
36{
37 struct inline_list *ilist;
38 char *demangled;
39
40 ilist = zalloc(sizeof(*ilist));
41 if (ilist == NULL)
42 return -1;
43
44 ilist->filename = filename;
45 ilist->line_nr = line_nr;
46
47 if (dso != NULL) {
48 demangled = dso__demangle_sym(dso, 0, funcname);
49 if (demangled == NULL) {
50 ilist->funcname = funcname;
51 } else {
52 ilist->funcname = demangled;
53 free(funcname);
54 }
55 }
56
57 list_add_tail(&ilist->list, &node->val);
58
59 return 0;
60}
61
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +090062#ifdef HAVE_LIBBFD_SUPPORT
63
64/*
65 * Implement addr2line using libbfd.
66 */
67#define PACKAGE "perf"
68#include <bfd.h>
69
70struct a2l_data {
71 const char *input;
Wang Nanac931f82014-12-16 14:19:06 +080072 u64 addr;
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +090073
74 bool found;
75 const char *filename;
76 const char *funcname;
77 unsigned line;
78
79 bfd *abfd;
80 asymbol **syms;
81};
82
83static int bfd_error(const char *string)
84{
85 const char *errmsg;
86
87 errmsg = bfd_errmsg(bfd_get_error());
88 fflush(stdout);
89
90 if (string)
91 pr_debug("%s: %s\n", string, errmsg);
92 else
93 pr_debug("%s\n", errmsg);
94
95 return -1;
96}
97
98static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
99{
100 long storage;
101 long symcount;
102 asymbol **syms;
103 bfd_boolean dynamic = FALSE;
104
105 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
106 return bfd_error(bfd_get_filename(abfd));
107
108 storage = bfd_get_symtab_upper_bound(abfd);
109 if (storage == 0L) {
110 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
111 dynamic = TRUE;
112 }
113 if (storage < 0L)
114 return bfd_error(bfd_get_filename(abfd));
115
116 syms = malloc(storage);
117 if (dynamic)
118 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
119 else
120 symcount = bfd_canonicalize_symtab(abfd, syms);
121
122 if (symcount < 0) {
123 free(syms);
124 return bfd_error(bfd_get_filename(abfd));
125 }
126
127 a2l->syms = syms;
128 return 0;
129}
130
131static void find_address_in_section(bfd *abfd, asection *section, void *data)
132{
133 bfd_vma pc, vma;
134 bfd_size_type size;
135 struct a2l_data *a2l = data;
136
137 if (a2l->found)
138 return;
139
140 if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
141 return;
142
143 pc = a2l->addr;
144 vma = bfd_get_section_vma(abfd, section);
145 size = bfd_get_section_size(section);
146
147 if (pc < vma || pc >= vma + size)
148 return;
149
150 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
151 &a2l->filename, &a2l->funcname,
152 &a2l->line);
153}
154
155static struct a2l_data *addr2line_init(const char *path)
156{
157 bfd *abfd;
158 struct a2l_data *a2l = NULL;
159
160 abfd = bfd_openr(path, NULL);
161 if (abfd == NULL)
162 return NULL;
163
164 if (!bfd_check_format(abfd, bfd_object))
165 goto out;
166
167 a2l = zalloc(sizeof(*a2l));
168 if (a2l == NULL)
169 goto out;
170
171 a2l->abfd = abfd;
172 a2l->input = strdup(path);
173 if (a2l->input == NULL)
174 goto out;
175
176 if (slurp_symtab(abfd, a2l))
177 goto out;
178
179 return a2l;
180
181out:
182 if (a2l) {
Namhyung Kim7d16c632014-01-09 23:07:59 +0900183 zfree((char **)&a2l->input);
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900184 free(a2l);
185 }
186 bfd_close(abfd);
187 return NULL;
188}
189
190static void addr2line_cleanup(struct a2l_data *a2l)
191{
192 if (a2l->abfd)
193 bfd_close(a2l->abfd);
Namhyung Kim7d16c632014-01-09 23:07:59 +0900194 zfree((char **)&a2l->input);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300195 zfree(&a2l->syms);
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900196 free(a2l);
197}
198
Andi Kleen2f84b422015-09-01 11:47:19 -0700199#define MAX_INLINE_NEST 1024
200
Jin Yaoa64489c2017-03-26 04:34:26 +0800201static void inline_list__reverse(struct inline_node *node)
202{
203 struct inline_list *ilist, *n;
204
205 list_for_each_entry_safe_reverse(ilist, n, &node->val, list)
206 list_move_tail(&ilist->list, &node->val);
207}
208
Wang Nanac931f82014-12-16 14:19:06 +0800209static int addr2line(const char *dso_name, u64 addr,
Andi Kleen2f84b422015-09-01 11:47:19 -0700210 char **file, unsigned int *line, struct dso *dso,
Jin Yaoa64489c2017-03-26 04:34:26 +0800211 bool unwind_inlines, struct inline_node *node)
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900212{
213 int ret = 0;
Adrian Hunter454ff002013-12-03 09:23:07 +0200214 struct a2l_data *a2l = dso->a2l;
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900215
Adrian Hunter454ff002013-12-03 09:23:07 +0200216 if (!a2l) {
217 dso->a2l = addr2line_init(dso_name);
218 a2l = dso->a2l;
219 }
220
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900221 if (a2l == NULL) {
222 pr_warning("addr2line_init failed for %s\n", dso_name);
223 return 0;
224 }
225
226 a2l->addr = addr;
Adrian Hunter454ff002013-12-03 09:23:07 +0200227 a2l->found = false;
228
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900229 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
230
Andi Kleen2f84b422015-09-01 11:47:19 -0700231 if (a2l->found && unwind_inlines) {
232 int cnt = 0;
233
234 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
235 &a2l->funcname, &a2l->line) &&
Jin Yaoa64489c2017-03-26 04:34:26 +0800236 cnt++ < MAX_INLINE_NEST) {
237
238 if (node != NULL) {
239 if (inline_list__append(strdup(a2l->filename),
240 strdup(a2l->funcname),
241 a2l->line, node,
242 dso) != 0)
243 return 0;
244 }
245 }
246
247 if ((node != NULL) &&
248 (callchain_param.order != ORDER_CALLEE)) {
249 inline_list__reverse(node);
250 }
Andi Kleen2f84b422015-09-01 11:47:19 -0700251 }
252
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900253 if (a2l->found && a2l->filename) {
254 *file = strdup(a2l->filename);
255 *line = a2l->line;
256
257 if (*file)
258 ret = 1;
259 }
260
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900261 return ret;
262}
263
Adrian Hunter454ff002013-12-03 09:23:07 +0200264void dso__free_a2l(struct dso *dso)
265{
266 struct a2l_data *a2l = dso->a2l;
267
268 if (!a2l)
269 return;
270
271 addr2line_cleanup(a2l);
272
273 dso->a2l = NULL;
274}
275
Jin Yaoa64489c2017-03-26 04:34:26 +0800276static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
277 struct dso *dso)
278{
279 char *file = NULL;
280 unsigned int line = 0;
281 struct inline_node *node;
282
283 node = zalloc(sizeof(*node));
284 if (node == NULL) {
285 perror("not enough memory for the inline node");
286 return NULL;
287 }
288
289 INIT_LIST_HEAD(&node->val);
290 node->addr = addr;
291
292 if (!addr2line(dso_name, addr, &file, &line, dso, TRUE, node))
293 goto out_free_inline_node;
294
295 if (list_empty(&node->val))
296 goto out_free_inline_node;
297
298 return node;
299
300out_free_inline_node:
301 inline_node__delete(node);
302 return NULL;
303}
304
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900305#else /* HAVE_LIBBFD_SUPPORT */
306
Jin Yao55803382017-03-26 04:34:25 +0800307static int filename_split(char *filename, unsigned int *line_nr)
308{
309 char *sep;
310
311 sep = strchr(filename, '\n');
312 if (sep)
313 *sep = '\0';
314
315 if (!strcmp(filename, "??:0"))
316 return 0;
317
318 sep = strchr(filename, ':');
319 if (sep) {
320 *sep++ = '\0';
321 *line_nr = strtoul(sep, NULL, 0);
322 return 1;
323 }
324
325 return 0;
326}
327
Wang Nanac931f82014-12-16 14:19:06 +0800328static int addr2line(const char *dso_name, u64 addr,
Adrian Hunter454ff002013-12-03 09:23:07 +0200329 char **file, unsigned int *line_nr,
Andi Kleen2f84b422015-09-01 11:47:19 -0700330 struct dso *dso __maybe_unused,
Jin Yaoa64489c2017-03-26 04:34:26 +0800331 bool unwind_inlines __maybe_unused,
332 struct inline_node *node __maybe_unused)
Namhyung Kimf048d542013-09-11 14:09:28 +0900333{
334 FILE *fp;
335 char cmd[PATH_MAX];
336 char *filename = NULL;
337 size_t len;
Namhyung Kimf048d542013-09-11 14:09:28 +0900338 int ret = 0;
339
340 scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
341 dso_name, addr);
342
343 fp = popen(cmd, "r");
344 if (fp == NULL) {
345 pr_warning("popen failed for %s\n", dso_name);
346 return 0;
347 }
348
349 if (getline(&filename, &len, fp) < 0 || !len) {
350 pr_warning("addr2line has no output for %s\n", dso_name);
351 goto out;
352 }
353
Jin Yao55803382017-03-26 04:34:25 +0800354 ret = filename_split(filename, line_nr);
355 if (ret != 1) {
Namhyung Kimf048d542013-09-11 14:09:28 +0900356 free(filename);
357 goto out;
358 }
359
Jin Yao55803382017-03-26 04:34:25 +0800360 *file = filename;
361
Namhyung Kimf048d542013-09-11 14:09:28 +0900362out:
363 pclose(fp);
364 return ret;
365}
Adrian Hunter454ff002013-12-03 09:23:07 +0200366
367void dso__free_a2l(struct dso *dso __maybe_unused)
368{
369}
370
Jin Yaoa64489c2017-03-26 04:34:26 +0800371static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
372 struct dso *dso __maybe_unused)
373{
374 FILE *fp;
375 char cmd[PATH_MAX];
376 struct inline_node *node;
377 char *filename = NULL;
378 size_t len;
379 unsigned int line_nr = 0;
380
381 scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
382 dso_name, addr);
383
384 fp = popen(cmd, "r");
385 if (fp == NULL) {
386 pr_err("popen failed for %s\n", dso_name);
387 return NULL;
388 }
389
390 node = zalloc(sizeof(*node));
391 if (node == NULL) {
392 perror("not enough memory for the inline node");
393 goto out;
394 }
395
396 INIT_LIST_HEAD(&node->val);
397 node->addr = addr;
398
399 while (getline(&filename, &len, fp) != -1) {
400 if (filename_split(filename, &line_nr) != 1) {
401 free(filename);
402 goto out;
403 }
404
405 if (inline_list__append(filename, NULL, line_nr, node,
406 NULL) != 0)
407 goto out;
408
409 filename = NULL;
410 }
411
412out:
413 pclose(fp);
414
415 if (list_empty(&node->val)) {
416 inline_node__delete(node);
417 return NULL;
418 }
419
420 return node;
421}
422
Roberto Vitillo2f48fcd2013-09-11 14:09:32 +0900423#endif /* HAVE_LIBBFD_SUPPORT */
Namhyung Kimf048d542013-09-11 14:09:28 +0900424
Adrian Hunter906049c82013-12-03 09:23:10 +0200425/*
426 * Number of addr2line failures (without success) before disabling it for that
427 * dso.
428 */
429#define A2L_FAIL_LIMIT 123
430
Andi Kleen2f84b422015-09-01 11:47:19 -0700431char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
432 bool show_sym, bool unwind_inlines)
Namhyung Kimf048d542013-09-11 14:09:28 +0900433{
David Aherna949fff2013-10-09 21:51:31 -0600434 char *file = NULL;
435 unsigned line = 0;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900436 char *srcline;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300437 const char *dso_name;
Namhyung Kimf048d542013-09-11 14:09:28 +0900438
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900439 if (!dso->has_srcline)
Andi Kleen23f09812014-11-12 18:05:24 -0800440 goto out;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900441
Jin Yao55803382017-03-26 04:34:25 +0800442 dso_name = dso__name(dso);
443 if (dso_name == NULL)
Namhyung Kim58d91a02013-09-11 14:09:29 +0900444 goto out;
445
Jin Yaoa64489c2017-03-26 04:34:26 +0800446 if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines, NULL))
Namhyung Kim58d91a02013-09-11 14:09:29 +0900447 goto out;
Namhyung Kimf048d542013-09-11 14:09:28 +0900448
Andi Kleena9710ba2015-08-07 15:24:05 -0700449 if (asprintf(&srcline, "%s:%u",
450 srcline_full_filename ? file : basename(file),
451 line) < 0) {
Adrian Hunter906049c82013-12-03 09:23:10 +0200452 free(file);
453 goto out;
454 }
455
456 dso->a2l_fails = 0;
Namhyung Kimf048d542013-09-11 14:09:28 +0900457
458 free(file);
Namhyung Kimf048d542013-09-11 14:09:28 +0900459 return srcline;
Namhyung Kim2cc9d0e2013-09-11 14:09:31 +0900460
461out:
Adrian Hunter906049c82013-12-03 09:23:10 +0200462 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
463 dso->has_srcline = 0;
464 dso__free_a2l(dso);
465 }
Andi Kleen85c116a2014-11-12 18:05:27 -0800466 if (sym) {
Wang Nanac931f82014-12-16 14:19:06 +0800467 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
Andi Kleen85c116a2014-11-12 18:05:27 -0800468 addr - sym->start) < 0)
469 return SRCLINE_UNKNOWN;
Wang Nanac931f82014-12-16 14:19:06 +0800470 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
Andi Kleen23f09812014-11-12 18:05:24 -0800471 return SRCLINE_UNKNOWN;
472 return srcline;
Namhyung Kimf048d542013-09-11 14:09:28 +0900473}
474
475void free_srcline(char *srcline)
476{
477 if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
478 free(srcline);
479}
Andi Kleen2f84b422015-09-01 11:47:19 -0700480
481char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
482 bool show_sym)
483{
484 return __get_srcline(dso, addr, sym, show_sym, false);
485}
Jin Yaoa64489c2017-03-26 04:34:26 +0800486
487struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr)
488{
489 const char *dso_name;
490
491 dso_name = dso__name(dso);
492 if (dso_name == NULL)
493 return NULL;
494
495 return addr2inlines(dso_name, addr, dso);
496}
497
498void inline_node__delete(struct inline_node *node)
499{
500 struct inline_list *ilist, *tmp;
501
502 list_for_each_entry_safe(ilist, tmp, &node->val, list) {
503 list_del_init(&ilist->list);
504 zfree(&ilist->filename);
505 zfree(&ilist->funcname);
506 free(ilist);
507 }
508
509 free(node);
510}