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