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