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