perf srcline: Show correct function name for srcline of callchains
[linux-2.6-microblaze.git] / tools / perf / util / srcline.c
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include <linux/kernel.h>
7
8 #include "util/dso.h"
9 #include "util/util.h"
10 #include "util/debug.h"
11 #include "util/callchain.h"
12 #include "srcline.h"
13 #include "string2.h"
14 #include "symbol.h"
15
16 bool srcline_full_filename;
17
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
36 static int inline_list__append(struct symbol *symbol, char *srcline,
37                                struct inline_node *node)
38 {
39         struct inline_list *ilist;
40
41         ilist = zalloc(sizeof(*ilist));
42         if (ilist == NULL)
43                 return -1;
44
45         ilist->symbol = symbol;
46         ilist->srcline = srcline;
47
48         if (callchain_param.order == ORDER_CALLEE)
49                 list_add_tail(&ilist->list, &node->val);
50         else
51                 list_add(&ilist->list, &node->val);
52
53         return 0;
54 }
55
56 /* basename version that takes a const input string */
57 static const char *gnu_basename(const char *path)
58 {
59         const char *base = strrchr(path, '/');
60
61         return base ? base + 1 : path;
62 }
63
64 static char *srcline_from_fileline(const char *file, unsigned int line)
65 {
66         char *srcline;
67
68         if (!file)
69                 return NULL;
70
71         if (!srcline_full_filename)
72                 file = gnu_basename(file);
73
74         if (asprintf(&srcline, "%s:%u", file, line) < 0)
75                 return NULL;
76
77         return srcline;
78 }
79
80 static struct symbol *new_inline_sym(struct dso *dso,
81                                      struct symbol *base_sym,
82                                      const char *funcname)
83 {
84         struct symbol *inline_sym;
85         char *demangled = NULL;
86
87         if (dso) {
88                 demangled = dso__demangle_sym(dso, 0, funcname);
89                 if (demangled)
90                         funcname = demangled;
91         }
92
93         if (base_sym && strcmp(funcname, base_sym->name) == 0) {
94                 /* reuse the real, existing symbol */
95                 inline_sym = base_sym;
96                 /* ensure that we don't alias an inlined symbol, which could
97                  * lead to double frees in inline_node__delete
98                  */
99                 assert(!base_sym->inlined);
100         } else {
101                 /* create a fake symbol for the inline frame */
102                 inline_sym = symbol__new(base_sym ? base_sym->start : 0,
103                                          base_sym ? base_sym->end : 0,
104                                          base_sym ? base_sym->binding : 0,
105                                          funcname);
106                 if (inline_sym)
107                         inline_sym->inlined = 1;
108         }
109
110         free(demangled);
111
112         return inline_sym;
113 }
114
115 #ifdef HAVE_LIBBFD_SUPPORT
116
117 /*
118  * Implement addr2line using libbfd.
119  */
120 #define PACKAGE "perf"
121 #include <bfd.h>
122
123 struct a2l_data {
124         const char      *input;
125         u64             addr;
126
127         bool            found;
128         const char      *filename;
129         const char      *funcname;
130         unsigned        line;
131
132         bfd             *abfd;
133         asymbol         **syms;
134 };
135
136 static int bfd_error(const char *string)
137 {
138         const char *errmsg;
139
140         errmsg = bfd_errmsg(bfd_get_error());
141         fflush(stdout);
142
143         if (string)
144                 pr_debug("%s: %s\n", string, errmsg);
145         else
146                 pr_debug("%s\n", errmsg);
147
148         return -1;
149 }
150
151 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
152 {
153         long storage;
154         long symcount;
155         asymbol **syms;
156         bfd_boolean dynamic = FALSE;
157
158         if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
159                 return bfd_error(bfd_get_filename(abfd));
160
161         storage = bfd_get_symtab_upper_bound(abfd);
162         if (storage == 0L) {
163                 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
164                 dynamic = TRUE;
165         }
166         if (storage < 0L)
167                 return bfd_error(bfd_get_filename(abfd));
168
169         syms = malloc(storage);
170         if (dynamic)
171                 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
172         else
173                 symcount = bfd_canonicalize_symtab(abfd, syms);
174
175         if (symcount < 0) {
176                 free(syms);
177                 return bfd_error(bfd_get_filename(abfd));
178         }
179
180         a2l->syms = syms;
181         return 0;
182 }
183
184 static void find_address_in_section(bfd *abfd, asection *section, void *data)
185 {
186         bfd_vma pc, vma;
187         bfd_size_type size;
188         struct a2l_data *a2l = data;
189
190         if (a2l->found)
191                 return;
192
193         if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
194                 return;
195
196         pc = a2l->addr;
197         vma = bfd_get_section_vma(abfd, section);
198         size = bfd_get_section_size(section);
199
200         if (pc < vma || pc >= vma + size)
201                 return;
202
203         a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
204                                            &a2l->filename, &a2l->funcname,
205                                            &a2l->line);
206
207         if (a2l->filename && !strlen(a2l->filename))
208                 a2l->filename = NULL;
209 }
210
211 static struct a2l_data *addr2line_init(const char *path)
212 {
213         bfd *abfd;
214         struct a2l_data *a2l = NULL;
215
216         abfd = bfd_openr(path, NULL);
217         if (abfd == NULL)
218                 return NULL;
219
220         if (!bfd_check_format(abfd, bfd_object))
221                 goto out;
222
223         a2l = zalloc(sizeof(*a2l));
224         if (a2l == NULL)
225                 goto out;
226
227         a2l->abfd = abfd;
228         a2l->input = strdup(path);
229         if (a2l->input == NULL)
230                 goto out;
231
232         if (slurp_symtab(abfd, a2l))
233                 goto out;
234
235         return a2l;
236
237 out:
238         if (a2l) {
239                 zfree((char **)&a2l->input);
240                 free(a2l);
241         }
242         bfd_close(abfd);
243         return NULL;
244 }
245
246 static void addr2line_cleanup(struct a2l_data *a2l)
247 {
248         if (a2l->abfd)
249                 bfd_close(a2l->abfd);
250         zfree((char **)&a2l->input);
251         zfree(&a2l->syms);
252         free(a2l);
253 }
254
255 #define MAX_INLINE_NEST 1024
256
257 static int inline_list__append_dso_a2l(struct dso *dso,
258                                        struct inline_node *node,
259                                        struct symbol *sym)
260 {
261         struct a2l_data *a2l = dso->a2l;
262         struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
263         char *srcline = NULL;
264
265         if (a2l->filename)
266                 srcline = srcline_from_fileline(a2l->filename, a2l->line);
267
268         return inline_list__append(inline_sym, srcline, node);
269 }
270
271 static int addr2line(const char *dso_name, u64 addr,
272                      char **file, unsigned int *line, struct dso *dso,
273                      bool unwind_inlines, struct inline_node *node,
274                      struct symbol *sym)
275 {
276         int ret = 0;
277         struct a2l_data *a2l = dso->a2l;
278
279         if (!a2l) {
280                 dso->a2l = addr2line_init(dso_name);
281                 a2l = dso->a2l;
282         }
283
284         if (a2l == NULL) {
285                 pr_warning("addr2line_init failed for %s\n", dso_name);
286                 return 0;
287         }
288
289         a2l->addr = addr;
290         a2l->found = false;
291
292         bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
293
294         if (!a2l->found)
295                 return 0;
296
297         if (unwind_inlines) {
298                 int cnt = 0;
299
300                 if (node && inline_list__append_dso_a2l(dso, node, sym))
301                         return 0;
302
303                 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
304                                              &a2l->funcname, &a2l->line) &&
305                        cnt++ < MAX_INLINE_NEST) {
306
307                         if (a2l->filename && !strlen(a2l->filename))
308                                 a2l->filename = NULL;
309
310                         if (node != NULL) {
311                                 if (inline_list__append_dso_a2l(dso, node, sym))
312                                         return 0;
313                                 // found at least one inline frame
314                                 ret = 1;
315                         }
316                 }
317         }
318
319         if (file) {
320                 *file = a2l->filename ? strdup(a2l->filename) : NULL;
321                 ret = *file ? 1 : 0;
322         }
323
324         if (line)
325                 *line = a2l->line;
326
327         return ret;
328 }
329
330 void dso__free_a2l(struct dso *dso)
331 {
332         struct a2l_data *a2l = dso->a2l;
333
334         if (!a2l)
335                 return;
336
337         addr2line_cleanup(a2l);
338
339         dso->a2l = NULL;
340 }
341
342 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
343                                         struct dso *dso, struct symbol *sym)
344 {
345         struct inline_node *node;
346
347         node = zalloc(sizeof(*node));
348         if (node == NULL) {
349                 perror("not enough memory for the inline node");
350                 return NULL;
351         }
352
353         INIT_LIST_HEAD(&node->val);
354         node->addr = addr;
355
356         addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
357         return node;
358 }
359
360 #else /* HAVE_LIBBFD_SUPPORT */
361
362 static int filename_split(char *filename, unsigned int *line_nr)
363 {
364         char *sep;
365
366         sep = strchr(filename, '\n');
367         if (sep)
368                 *sep = '\0';
369
370         if (!strcmp(filename, "??:0"))
371                 return 0;
372
373         sep = strchr(filename, ':');
374         if (sep) {
375                 *sep++ = '\0';
376                 *line_nr = strtoul(sep, NULL, 0);
377                 return 1;
378         }
379
380         return 0;
381 }
382
383 static int addr2line(const char *dso_name, u64 addr,
384                      char **file, unsigned int *line_nr,
385                      struct dso *dso __maybe_unused,
386                      bool unwind_inlines __maybe_unused,
387                      struct inline_node *node __maybe_unused,
388                      struct symbol *sym __maybe_unused)
389 {
390         FILE *fp;
391         char cmd[PATH_MAX];
392         char *filename = NULL;
393         size_t len;
394         int ret = 0;
395
396         scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
397                   dso_name, addr);
398
399         fp = popen(cmd, "r");
400         if (fp == NULL) {
401                 pr_warning("popen failed for %s\n", dso_name);
402                 return 0;
403         }
404
405         if (getline(&filename, &len, fp) < 0 || !len) {
406                 pr_warning("addr2line has no output for %s\n", dso_name);
407                 goto out;
408         }
409
410         ret = filename_split(filename, line_nr);
411         if (ret != 1) {
412                 free(filename);
413                 goto out;
414         }
415
416         *file = filename;
417
418 out:
419         pclose(fp);
420         return ret;
421 }
422
423 void dso__free_a2l(struct dso *dso __maybe_unused)
424 {
425 }
426
427 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
428                                         struct dso *dso __maybe_unused,
429                                         struct symbol *sym)
430 {
431         FILE *fp;
432         char cmd[PATH_MAX];
433         struct inline_node *node;
434         char *filename = NULL;
435         char *funcname = NULL;
436         size_t filelen, funclen;
437         unsigned int line_nr = 0;
438
439         scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i -f %016"PRIx64,
440                   dso_name, addr);
441
442         fp = popen(cmd, "r");
443         if (fp == NULL) {
444                 pr_err("popen failed for %s\n", dso_name);
445                 return NULL;
446         }
447
448         node = zalloc(sizeof(*node));
449         if (node == NULL) {
450                 perror("not enough memory for the inline node");
451                 goto out;
452         }
453
454         INIT_LIST_HEAD(&node->val);
455         node->addr = addr;
456
457         /* addr2line -f generates two lines for each inlined functions */
458         while (getline(&funcname, &funclen, fp) != -1) {
459                 char *srcline;
460                 struct symbol *inline_sym;
461
462                 rtrim(funcname);
463
464                 if (getline(&filename, &filelen, fp) == -1)
465                         goto out;
466
467                 if (filename_split(filename, &line_nr) != 1)
468                         goto out;
469
470                 srcline = srcline_from_fileline(filename, line_nr);
471                 inline_sym = new_inline_sym(dso, sym, funcname);
472
473                 if (inline_list__append(inline_sym, srcline, node) != 0) {
474                         free(srcline);
475                         if (inline_sym && inline_sym->inlined)
476                                 symbol__delete(inline_sym);
477                         goto out;
478                 }
479         }
480
481 out:
482         pclose(fp);
483         free(filename);
484         free(funcname);
485
486         return node;
487 }
488
489 #endif /* HAVE_LIBBFD_SUPPORT */
490
491 /*
492  * Number of addr2line failures (without success) before disabling it for that
493  * dso.
494  */
495 #define A2L_FAIL_LIMIT 123
496
497 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
498                   bool show_sym, bool show_addr, bool unwind_inlines)
499 {
500         char *file = NULL;
501         unsigned line = 0;
502         char *srcline;
503         const char *dso_name;
504
505         if (!dso->has_srcline)
506                 goto out;
507
508         dso_name = dso__name(dso);
509         if (dso_name == NULL)
510                 goto out;
511
512         if (!addr2line(dso_name, addr, &file, &line, dso,
513                        unwind_inlines, NULL, sym))
514                 goto out;
515
516         srcline = srcline_from_fileline(file, line);
517         free(file);
518
519         if (!srcline)
520                 goto out;
521
522         dso->a2l_fails = 0;
523
524         return srcline;
525
526 out:
527         if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
528                 dso->has_srcline = 0;
529                 dso__free_a2l(dso);
530         }
531
532         if (!show_addr)
533                 return (show_sym && sym) ?
534                             strndup(sym->name, sym->namelen) : NULL;
535
536         if (sym) {
537                 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
538                                         addr - sym->start) < 0)
539                         return SRCLINE_UNKNOWN;
540         } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
541                 return SRCLINE_UNKNOWN;
542         return srcline;
543 }
544
545 void free_srcline(char *srcline)
546 {
547         if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
548                 free(srcline);
549 }
550
551 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
552                   bool show_sym, bool show_addr)
553 {
554         return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
555 }
556
557 struct srcline_node {
558         u64                     addr;
559         char                    *srcline;
560         struct rb_node          rb_node;
561 };
562
563 void srcline__tree_insert(struct rb_root *tree, u64 addr, char *srcline)
564 {
565         struct rb_node **p = &tree->rb_node;
566         struct rb_node *parent = NULL;
567         struct srcline_node *i, *node;
568
569         node = zalloc(sizeof(struct srcline_node));
570         if (!node) {
571                 perror("not enough memory for the srcline node");
572                 return;
573         }
574
575         node->addr = addr;
576         node->srcline = srcline;
577
578         while (*p != NULL) {
579                 parent = *p;
580                 i = rb_entry(parent, struct srcline_node, rb_node);
581                 if (addr < i->addr)
582                         p = &(*p)->rb_left;
583                 else
584                         p = &(*p)->rb_right;
585         }
586         rb_link_node(&node->rb_node, parent, p);
587         rb_insert_color(&node->rb_node, tree);
588 }
589
590 char *srcline__tree_find(struct rb_root *tree, u64 addr)
591 {
592         struct rb_node *n = tree->rb_node;
593
594         while (n) {
595                 struct srcline_node *i = rb_entry(n, struct srcline_node,
596                                                   rb_node);
597
598                 if (addr < i->addr)
599                         n = n->rb_left;
600                 else if (addr > i->addr)
601                         n = n->rb_right;
602                 else
603                         return i->srcline;
604         }
605
606         return NULL;
607 }
608
609 void srcline__tree_delete(struct rb_root *tree)
610 {
611         struct srcline_node *pos;
612         struct rb_node *next = rb_first(tree);
613
614         while (next) {
615                 pos = rb_entry(next, struct srcline_node, rb_node);
616                 next = rb_next(&pos->rb_node);
617                 rb_erase(&pos->rb_node, tree);
618                 free_srcline(pos->srcline);
619                 zfree(&pos);
620         }
621 }
622
623 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
624                                             struct symbol *sym)
625 {
626         const char *dso_name;
627
628         dso_name = dso__name(dso);
629         if (dso_name == NULL)
630                 return NULL;
631
632         return addr2inlines(dso_name, addr, dso, sym);
633 }
634
635 void inline_node__delete(struct inline_node *node)
636 {
637         struct inline_list *ilist, *tmp;
638
639         list_for_each_entry_safe(ilist, tmp, &node->val, list) {
640                 list_del_init(&ilist->list);
641                 free_srcline(ilist->srcline);
642                 /* only the inlined symbols are owned by the list */
643                 if (ilist->symbol && ilist->symbol->inlined)
644                         symbol__delete(ilist->symbol);
645                 free(ilist);
646         }
647
648         free(node);
649 }
650
651 void inlines__tree_insert(struct rb_root *tree, struct inline_node *inlines)
652 {
653         struct rb_node **p = &tree->rb_node;
654         struct rb_node *parent = NULL;
655         const u64 addr = inlines->addr;
656         struct inline_node *i;
657
658         while (*p != NULL) {
659                 parent = *p;
660                 i = rb_entry(parent, struct inline_node, rb_node);
661                 if (addr < i->addr)
662                         p = &(*p)->rb_left;
663                 else
664                         p = &(*p)->rb_right;
665         }
666         rb_link_node(&inlines->rb_node, parent, p);
667         rb_insert_color(&inlines->rb_node, tree);
668 }
669
670 struct inline_node *inlines__tree_find(struct rb_root *tree, u64 addr)
671 {
672         struct rb_node *n = tree->rb_node;
673
674         while (n) {
675                 struct inline_node *i = rb_entry(n, struct inline_node,
676                                                  rb_node);
677
678                 if (addr < i->addr)
679                         n = n->rb_left;
680                 else if (addr > i->addr)
681                         n = n->rb_right;
682                 else
683                         return i;
684         }
685
686         return NULL;
687 }
688
689 void inlines__tree_delete(struct rb_root *tree)
690 {
691         struct inline_node *pos;
692         struct rb_node *next = rb_first(tree);
693
694         while (next) {
695                 pos = rb_entry(next, struct inline_node, rb_node);
696                 next = rb_next(&pos->rb_node);
697                 rb_erase(&pos->rb_node, tree);
698                 inline_node__delete(pos);
699         }
700 }