GNU Linux-libre 4.9.311-gnu1
[releases.git] / tools / perf / util / srcline.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <linux/kernel.h>
6
7 #include "util/dso.h"
8 #include "util/util.h"
9 #include "util/debug.h"
10
11 #include "symbol.h"
12
13 bool srcline_full_filename;
14
15 #ifdef HAVE_LIBBFD_SUPPORT
16
17 /*
18  * Implement addr2line using libbfd.
19  */
20 #define PACKAGE "perf"
21 #include <bfd.h>
22
23 struct a2l_data {
24         const char      *input;
25         u64             addr;
26
27         bool            found;
28         const char      *filename;
29         const char      *funcname;
30         unsigned        line;
31
32         bfd             *abfd;
33         asymbol         **syms;
34 };
35
36 static int bfd_error(const char *string)
37 {
38         const char *errmsg;
39
40         errmsg = bfd_errmsg(bfd_get_error());
41         fflush(stdout);
42
43         if (string)
44                 pr_debug("%s: %s\n", string, errmsg);
45         else
46                 pr_debug("%s\n", errmsg);
47
48         return -1;
49 }
50
51 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
52 {
53         long storage;
54         long symcount;
55         asymbol **syms;
56         bfd_boolean dynamic = FALSE;
57
58         if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
59                 return bfd_error(bfd_get_filename(abfd));
60
61         storage = bfd_get_symtab_upper_bound(abfd);
62         if (storage == 0L) {
63                 storage = bfd_get_dynamic_symtab_upper_bound(abfd);
64                 dynamic = TRUE;
65         }
66         if (storage < 0L)
67                 return bfd_error(bfd_get_filename(abfd));
68
69         syms = malloc(storage);
70         if (dynamic)
71                 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
72         else
73                 symcount = bfd_canonicalize_symtab(abfd, syms);
74
75         if (symcount < 0) {
76                 free(syms);
77                 return bfd_error(bfd_get_filename(abfd));
78         }
79
80         a2l->syms = syms;
81         return 0;
82 }
83
84 static void find_address_in_section(bfd *abfd, asection *section, void *data)
85 {
86         bfd_vma pc, vma;
87         bfd_size_type size;
88         struct a2l_data *a2l = data;
89         flagword flags;
90
91         if (a2l->found)
92                 return;
93
94 #ifdef bfd_get_section_flags
95         flags = bfd_get_section_flags(abfd, section);
96 #else
97         flags = bfd_section_flags(section);
98 #endif
99         if ((flags & SEC_ALLOC) == 0)
100                 return;
101
102         pc = a2l->addr;
103 #ifdef bfd_get_section_vma
104         vma = bfd_get_section_vma(abfd, section);
105 #else
106         vma = bfd_section_vma(section);
107 #endif
108 #ifdef bfd_get_section_size
109         size = bfd_get_section_size(section);
110 #else
111         size = bfd_section_size(section);
112 #endif
113
114         if (pc < vma || pc >= vma + size)
115                 return;
116
117         a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
118                                            &a2l->filename, &a2l->funcname,
119                                            &a2l->line);
120 }
121
122 static struct a2l_data *addr2line_init(const char *path)
123 {
124         bfd *abfd;
125         struct a2l_data *a2l = NULL;
126
127         abfd = bfd_openr(path, NULL);
128         if (abfd == NULL)
129                 return NULL;
130
131         if (!bfd_check_format(abfd, bfd_object))
132                 goto out;
133
134         a2l = zalloc(sizeof(*a2l));
135         if (a2l == NULL)
136                 goto out;
137
138         a2l->abfd = abfd;
139         a2l->input = strdup(path);
140         if (a2l->input == NULL)
141                 goto out;
142
143         if (slurp_symtab(abfd, a2l))
144                 goto out;
145
146         return a2l;
147
148 out:
149         if (a2l) {
150                 zfree((char **)&a2l->input);
151                 free(a2l);
152         }
153         bfd_close(abfd);
154         return NULL;
155 }
156
157 static void addr2line_cleanup(struct a2l_data *a2l)
158 {
159         if (a2l->abfd)
160                 bfd_close(a2l->abfd);
161         zfree((char **)&a2l->input);
162         zfree(&a2l->syms);
163         free(a2l);
164 }
165
166 #define MAX_INLINE_NEST 1024
167
168 static int addr2line(const char *dso_name, u64 addr,
169                      char **file, unsigned int *line, struct dso *dso,
170                      bool unwind_inlines)
171 {
172         int ret = 0;
173         struct a2l_data *a2l = dso->a2l;
174
175         if (!a2l) {
176                 dso->a2l = addr2line_init(dso_name);
177                 a2l = dso->a2l;
178         }
179
180         if (a2l == NULL) {
181                 pr_warning("addr2line_init failed for %s\n", dso_name);
182                 return 0;
183         }
184
185         a2l->addr = addr;
186         a2l->found = false;
187
188         bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
189
190         if (a2l->found && unwind_inlines) {
191                 int cnt = 0;
192
193                 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
194                                              &a2l->funcname, &a2l->line) &&
195                        cnt++ < MAX_INLINE_NEST)
196                         ;
197         }
198
199         if (a2l->found && a2l->filename) {
200                 *file = strdup(a2l->filename);
201                 *line = a2l->line;
202
203                 if (*file)
204                         ret = 1;
205         }
206
207         return ret;
208 }
209
210 void dso__free_a2l(struct dso *dso)
211 {
212         struct a2l_data *a2l = dso->a2l;
213
214         if (!a2l)
215                 return;
216
217         addr2line_cleanup(a2l);
218
219         dso->a2l = NULL;
220 }
221
222 #else /* HAVE_LIBBFD_SUPPORT */
223
224 static int addr2line(const char *dso_name, u64 addr,
225                      char **file, unsigned int *line_nr,
226                      struct dso *dso __maybe_unused,
227                      bool unwind_inlines __maybe_unused)
228 {
229         FILE *fp;
230         char cmd[PATH_MAX];
231         char *filename = NULL;
232         size_t len;
233         char *sep;
234         int ret = 0;
235
236         scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
237                   dso_name, addr);
238
239         fp = popen(cmd, "r");
240         if (fp == NULL) {
241                 pr_warning("popen failed for %s\n", dso_name);
242                 return 0;
243         }
244
245         if (getline(&filename, &len, fp) < 0 || !len) {
246                 pr_warning("addr2line has no output for %s\n", dso_name);
247                 goto out;
248         }
249
250         sep = strchr(filename, '\n');
251         if (sep)
252                 *sep = '\0';
253
254         if (!strcmp(filename, "??:0")) {
255                 pr_debug("no debugging info in %s\n", dso_name);
256                 free(filename);
257                 goto out;
258         }
259
260         sep = strchr(filename, ':');
261         if (sep) {
262                 *sep++ = '\0';
263                 *file = filename;
264                 *line_nr = strtoul(sep, NULL, 0);
265                 ret = 1;
266         }
267 out:
268         pclose(fp);
269         return ret;
270 }
271
272 void dso__free_a2l(struct dso *dso __maybe_unused)
273 {
274 }
275
276 #endif /* HAVE_LIBBFD_SUPPORT */
277
278 /*
279  * Number of addr2line failures (without success) before disabling it for that
280  * dso.
281  */
282 #define A2L_FAIL_LIMIT 123
283
284 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
285                   bool show_sym, bool unwind_inlines)
286 {
287         char *file = NULL;
288         unsigned line = 0;
289         char *srcline;
290         const char *dso_name;
291
292         if (!dso->has_srcline)
293                 goto out;
294
295         if (dso->symsrc_filename)
296                 dso_name = dso->symsrc_filename;
297         else
298                 dso_name = dso->long_name;
299
300         if (dso_name[0] == '[')
301                 goto out;
302
303         if (!strncmp(dso_name, "/tmp/perf-", 10))
304                 goto out;
305
306         if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines))
307                 goto out;
308
309         if (asprintf(&srcline, "%s:%u",
310                                 srcline_full_filename ? file : basename(file),
311                                 line) < 0) {
312                 free(file);
313                 goto out;
314         }
315
316         dso->a2l_fails = 0;
317
318         free(file);
319         return srcline;
320
321 out:
322         if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
323                 dso->has_srcline = 0;
324                 dso__free_a2l(dso);
325         }
326         if (sym) {
327                 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
328                                         addr - sym->start) < 0)
329                         return SRCLINE_UNKNOWN;
330         } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
331                 return SRCLINE_UNKNOWN;
332         return srcline;
333 }
334
335 void free_srcline(char *srcline)
336 {
337         if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
338                 free(srcline);
339 }
340
341 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
342                   bool show_sym)
343 {
344         return __get_srcline(dso, addr, sym, show_sym, false);
345 }