blob: 10cdbad76058e169243c3a10b4d2c47f93023e7f [file] [log] [blame]
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-annotate.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
10#include "util.h"
11#include "build-id.h"
12#include "color.h"
13#include "cache.h"
14#include "symbol.h"
15#include "debug.h"
16#include "annotate.h"
17
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020018int symbol__alloc_hist(struct symbol *sym, int nevents)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020019{
20 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020021
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020022 notes->sizeof_sym_hist = (sizeof(*notes->histograms) +
23 (sym->end - sym->start) * sizeof(u64));
24 notes->histograms = calloc(nevents, notes->sizeof_sym_hist);
25 return notes->histograms == NULL ? -1 : 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020026}
27
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020028int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
29 int evidx, u64 addr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020030{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020031 unsigned offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020032 struct annotation *notes;
33 struct sym_hist *h;
34
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020035 notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020036 if (notes->histograms == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020037 return -ENOMEM;
38
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020039 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
40
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020041 if (addr >= sym->end)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020042 return 0;
43
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020044 offset = addr - sym->start;
45 h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020046 h->sum++;
47 h->addr[offset]++;
48
49 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020050 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
51 addr, addr - sym->start, evidx, h->addr[offset]);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020052 return 0;
53}
54
55static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
56{
57 struct objdump_line *self = malloc(sizeof(*self) + privsize);
58
59 if (self != NULL) {
60 self->offset = offset;
61 self->line = line;
62 }
63
64 return self;
65}
66
67void objdump_line__free(struct objdump_line *self)
68{
69 free(self->line);
70 free(self);
71}
72
73static void objdump__add_line(struct list_head *head, struct objdump_line *line)
74{
75 list_add_tail(&line->node, head);
76}
77
78struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
79 struct objdump_line *pos)
80{
81 list_for_each_entry_continue(pos, head, node)
82 if (pos->offset >= 0)
83 return pos;
84
85 return NULL;
86}
87
88static void objdump_line__print(struct objdump_line *oline,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020089 struct list_head *head, struct symbol *sym,
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -020090 int evidx, u64 len, int min_pcnt)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020091{
92 static const char *prev_line;
93 static const char *prev_color;
94
95 if (oline->offset != -1) {
96 const char *path = NULL;
97 unsigned int hits = 0;
98 double percent = 0.0;
99 const char *color;
100 struct annotation *notes = symbol__annotation(sym);
101 struct source_line *src_line = notes->src_line;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200102 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200103 s64 offset = oline->offset;
104 struct objdump_line *next = objdump__get_next_ip_line(head, oline);
105
106 while (offset < (s64)len &&
107 (next == NULL || offset < next->offset)) {
108 if (src_line) {
109 if (path == NULL)
110 path = src_line[offset].path;
111 percent += src_line[offset].percent;
112 } else
113 hits += h->addr[offset];
114
115 ++offset;
116 }
117
118 if (src_line == NULL && h->sum)
119 percent = 100.0 * hits / h->sum;
120
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200121 if (percent < min_pcnt)
122 return;
123
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200124 color = get_percent_color(percent);
125
126 /*
127 * Also color the filename and line if needed, with
128 * the same color than the percentage. Don't print it
129 * twice for close colored addr with the same filename:line
130 */
131 if (path) {
132 if (!prev_line || strcmp(prev_line, path)
133 || color != prev_color) {
134 color_fprintf(stdout, color, " %s", path);
135 prev_line = path;
136 prev_color = color;
137 }
138 }
139
140 color_fprintf(stdout, color, " %7.2f", percent);
141 printf(" : ");
142 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
143 } else {
144 if (!*oline->line)
145 printf(" :\n");
146 else
147 printf(" : %s\n", oline->line);
148 }
149}
150
151static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, FILE *file,
152 struct list_head *head, size_t privsize)
153{
154 struct objdump_line *objdump_line;
155 char *line = NULL, *tmp, *tmp2, *c;
156 size_t line_len;
157 s64 line_ip, offset = -1;
158
159 if (getline(&line, &line_len, file) < 0)
160 return -1;
161
162 if (!line)
163 return -1;
164
165 while (line_len != 0 && isspace(line[line_len - 1]))
166 line[--line_len] = '\0';
167
168 c = strchr(line, '\n');
169 if (c)
170 *c = 0;
171
172 line_ip = -1;
173
174 /*
175 * Strip leading spaces:
176 */
177 tmp = line;
178 while (*tmp) {
179 if (*tmp != ' ')
180 break;
181 tmp++;
182 }
183
184 if (*tmp) {
185 /*
186 * Parse hexa addresses followed by ':'
187 */
188 line_ip = strtoull(tmp, &tmp2, 16);
189 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
190 line_ip = -1;
191 }
192
193 if (line_ip != -1) {
194 u64 start = map__rip_2objdump(map, sym->start),
195 end = map__rip_2objdump(map, sym->end);
196
197 offset = line_ip - start;
198 if (offset < 0 || (u64)line_ip > end)
199 offset = -1;
200 }
201
202 objdump_line = objdump_line__new(offset, line, privsize);
203 if (objdump_line == NULL) {
204 free(line);
205 return -1;
206 }
207 objdump__add_line(head, objdump_line);
208
209 return 0;
210}
211
212int symbol__annotate(struct symbol *sym, struct map *map,
213 struct list_head *head, size_t privsize)
214{
215 struct dso *dso = map->dso;
216 char *filename = dso__build_id_filename(dso, NULL, 0);
217 bool free_filename = true;
218 char command[PATH_MAX * 2];
219 FILE *file;
220 int err = 0;
221 u64 len;
222 char symfs_filename[PATH_MAX];
223
224 if (filename) {
225 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
226 symbol_conf.symfs, filename);
227 }
228
229 if (filename == NULL) {
230 if (dso->has_build_id) {
231 pr_err("Can't annotate %s: not enough memory\n",
232 sym->name);
233 return -ENOMEM;
234 }
235 goto fallback;
236 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
237 strstr(command, "[kernel.kallsyms]") ||
238 access(symfs_filename, R_OK)) {
239 free(filename);
240fallback:
241 /*
242 * If we don't have build-ids or the build-id file isn't in the
243 * cache, or is just a kallsyms file, well, lets hope that this
244 * DSO is the same as when 'perf record' ran.
245 */
246 filename = dso->long_name;
247 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
248 symbol_conf.symfs, filename);
249 free_filename = false;
250 }
251
252 if (dso->origin == DSO__ORIG_KERNEL) {
253 if (dso->annotate_warned)
254 goto out_free_filename;
255 err = -ENOENT;
256 dso->annotate_warned = 1;
257 pr_err("Can't annotate %s: No vmlinux file was found in the "
258 "path\n", sym->name);
259 goto out_free_filename;
260 }
261
262 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
263 filename, sym->name, map->unmap_ip(map, sym->start),
264 map->unmap_ip(map, sym->end));
265
266 len = sym->end - sym->start;
267
268 pr_debug("annotating [%p] %30s : [%p] %30s\n",
269 dso, dso->long_name, sym, sym->name);
270
271 snprintf(command, sizeof(command),
272 "objdump --start-address=0x%016" PRIx64
273 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
274 map__rip_2objdump(map, sym->start),
275 map__rip_2objdump(map, sym->end),
276 symfs_filename, filename);
277
278 pr_debug("Executing: %s\n", command);
279
280 file = popen(command, "r");
281 if (!file)
282 goto out_free_filename;
283
284 while (!feof(file))
285 if (symbol__parse_objdump_line(sym, map, file, head, privsize) < 0)
286 break;
287
288 pclose(file);
289out_free_filename:
290 if (free_filename)
291 free(filename);
292 return err;
293}
294
295static void insert_source_line(struct rb_root *root, struct source_line *src_line)
296{
297 struct source_line *iter;
298 struct rb_node **p = &root->rb_node;
299 struct rb_node *parent = NULL;
300
301 while (*p != NULL) {
302 parent = *p;
303 iter = rb_entry(parent, struct source_line, node);
304
305 if (src_line->percent > iter->percent)
306 p = &(*p)->rb_left;
307 else
308 p = &(*p)->rb_right;
309 }
310
311 rb_link_node(&src_line->node, parent, p);
312 rb_insert_color(&src_line->node, root);
313}
314
315static void symbol__free_source_line(struct symbol *sym, int len)
316{
317 struct annotation *notes = symbol__annotation(sym);
318 struct source_line *src_line = notes->src_line;
319 int i;
320
321 for (i = 0; i < len; i++)
322 free(src_line[i].path);
323
324 free(src_line);
325 notes->src_line = NULL;
326}
327
328/* Get the filename:line for the colored entries */
329static int symbol__get_source_line(struct symbol *sym, struct map *map,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200330 int evidx, struct rb_root *root, int len,
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200331 const char *filename)
332{
333 u64 start;
334 int i;
335 char cmd[PATH_MAX * 2];
336 struct source_line *src_line;
337 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200338 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200339
340 if (!h->sum)
341 return 0;
342
343 src_line = notes->src_line = calloc(len, sizeof(struct source_line));
344 if (!notes->src_line)
345 return -1;
346
347 start = map->unmap_ip(map, sym->start);
348
349 for (i = 0; i < len; i++) {
350 char *path = NULL;
351 size_t line_len;
352 u64 offset;
353 FILE *fp;
354
355 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
356 if (src_line[i].percent <= 0.5)
357 continue;
358
359 offset = start + i;
360 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
361 fp = popen(cmd, "r");
362 if (!fp)
363 continue;
364
365 if (getline(&path, &line_len, fp) < 0 || !line_len)
366 goto next;
367
368 src_line[i].path = malloc(sizeof(char) * line_len + 1);
369 if (!src_line[i].path)
370 goto next;
371
372 strcpy(src_line[i].path, path);
373 insert_source_line(root, &src_line[i]);
374
375 next:
376 pclose(fp);
377 }
378
379 return 0;
380}
381
382static void print_summary(struct rb_root *root, const char *filename)
383{
384 struct source_line *src_line;
385 struct rb_node *node;
386
387 printf("\nSorted summary for file %s\n", filename);
388 printf("----------------------------------------------\n\n");
389
390 if (RB_EMPTY_ROOT(root)) {
391 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
392 return;
393 }
394
395 node = rb_first(root);
396 while (node) {
397 double percent;
398 const char *color;
399 char *path;
400
401 src_line = rb_entry(node, struct source_line, node);
402 percent = src_line->percent;
403 color = get_percent_color(percent);
404 path = src_line->path;
405
406 color_fprintf(stdout, color, " %7.2f %s", percent, path);
407 node = rb_next(node);
408 }
409}
410
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200411static void symbol__annotate_hits(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200412{
413 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200414 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200415 u64 len = sym->end - sym->start, offset;
416
417 for (offset = 0; offset < len; ++offset)
418 if (h->addr[offset] != 0)
419 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
420 sym->start + offset, h->addr[offset]);
421 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
422}
423
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200424void symbol__annotate_printf(struct symbol *sym, struct map *map,
425 struct list_head *head, int evidx, bool full_paths,
426 int min_pcnt, int max_lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200427{
428 struct dso *dso = map->dso;
429 const char *filename = dso->long_name, *d_filename;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200430 struct objdump_line *pos;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200431 int printed = 2;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200432 u64 len;
433
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200434 if (full_paths)
435 d_filename = filename;
436 else
437 d_filename = basename(filename);
438
439 len = sym->end - sym->start;
440
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200441 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
442 printf("------------------------------------------------\n");
443
444 if (verbose)
445 symbol__annotate_hits(sym, evidx);
446
447 list_for_each_entry(pos, head, node) {
448 objdump_line__print(pos, head, sym, evidx, len, min_pcnt);
449 if (max_lines && ++printed >= max_lines)
450 break;
451
452 }
453}
454
455void objdump_line_list__purge(struct list_head *head)
456{
457 struct objdump_line *pos, *n;
458
459 list_for_each_entry_safe(pos, n, head, node) {
460 list_del(&pos->node);
461 objdump_line__free(pos);
462 }
463}
464
465int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
466 bool print_lines, bool full_paths, int min_pcnt,
467 int max_lines)
468{
469 struct dso *dso = map->dso;
470 const char *filename = dso->long_name;
471 struct rb_root source_line = RB_ROOT;
472 LIST_HEAD(head);
473 u64 len;
474
475 if (symbol__annotate(sym, map, &head, 0) < 0)
476 return -1;
477
478 len = sym->end - sym->start;
479
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200480 if (print_lines) {
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200481 symbol__get_source_line(sym, map, evidx, &source_line,
482 len, filename);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200483 print_summary(&source_line, filename);
484 }
485
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200486 symbol__annotate_printf(sym, map, &head, evidx, full_paths,
487 min_pcnt, max_lines);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200488 if (print_lines)
489 symbol__free_source_line(sym, len);
490
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200491 objdump_line_list__purge(&head);
492
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200493 return 0;
494}