blob: 6db435167d74229ed6c1a9fc181a32722bbeb433 [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);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -020025 notes->nr_histograms = nevents;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020026 return notes->histograms == NULL ? -1 : 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020027}
28
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -020029void symbol__annotate_zero_histograms(struct symbol *sym)
30{
31 struct annotation *notes = symbol__annotation(sym);
32
33 if (notes->histograms != NULL)
34 memset(notes->histograms, 0,
35 notes->nr_histograms * notes->sizeof_sym_hist);
36}
37
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020038int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
39 int evidx, u64 addr)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020040{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020041 unsigned offset;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020042 struct annotation *notes;
43 struct sym_hist *h;
44
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020045 notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020046 if (notes->histograms == NULL)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020047 return -ENOMEM;
48
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020049 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
50
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020051 if (addr >= sym->end)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020052 return 0;
53
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020054 offset = addr - sym->start;
55 h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020056 h->sum++;
57 h->addr[offset]++;
58
59 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020060 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
61 addr, addr - sym->start, evidx, h->addr[offset]);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020062 return 0;
63}
64
65static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
66{
67 struct objdump_line *self = malloc(sizeof(*self) + privsize);
68
69 if (self != NULL) {
70 self->offset = offset;
71 self->line = line;
72 }
73
74 return self;
75}
76
77void objdump_line__free(struct objdump_line *self)
78{
79 free(self->line);
80 free(self);
81}
82
83static void objdump__add_line(struct list_head *head, struct objdump_line *line)
84{
85 list_add_tail(&line->node, head);
86}
87
88struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
89 struct objdump_line *pos)
90{
91 list_for_each_entry_continue(pos, head, node)
92 if (pos->offset >= 0)
93 return pos;
94
95 return NULL;
96}
97
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -020098static int objdump_line__print(struct objdump_line *oline,
99 struct list_head *head, struct symbol *sym,
100 int evidx, u64 len, int min_pcnt,
101 int printed, int max_lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200102{
103 static const char *prev_line;
104 static const char *prev_color;
105
106 if (oline->offset != -1) {
107 const char *path = NULL;
108 unsigned int hits = 0;
109 double percent = 0.0;
110 const char *color;
111 struct annotation *notes = symbol__annotation(sym);
112 struct source_line *src_line = notes->src_line;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200113 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200114 s64 offset = oline->offset;
115 struct objdump_line *next = objdump__get_next_ip_line(head, oline);
116
117 while (offset < (s64)len &&
118 (next == NULL || offset < next->offset)) {
119 if (src_line) {
120 if (path == NULL)
121 path = src_line[offset].path;
122 percent += src_line[offset].percent;
123 } else
124 hits += h->addr[offset];
125
126 ++offset;
127 }
128
129 if (src_line == NULL && h->sum)
130 percent = 100.0 * hits / h->sum;
131
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200132 if (percent < min_pcnt)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200133 return -1;
134
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -0200135 if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200136 return 1;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200137
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200138 color = get_percent_color(percent);
139
140 /*
141 * Also color the filename and line if needed, with
142 * the same color than the percentage. Don't print it
143 * twice for close colored addr with the same filename:line
144 */
145 if (path) {
146 if (!prev_line || strcmp(prev_line, path)
147 || color != prev_color) {
148 color_fprintf(stdout, color, " %s", path);
149 prev_line = path;
150 prev_color = color;
151 }
152 }
153
154 color_fprintf(stdout, color, " %7.2f", percent);
155 printf(" : ");
156 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
Arnaldo Carvalho de Meloe3087b82011-02-08 15:01:39 -0200157 } else if (max_lines && printed >= max_lines)
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200158 return 1;
159 else {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200160 if (!*oline->line)
161 printf(" :\n");
162 else
163 printf(" : %s\n", oline->line);
164 }
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200165
166 return 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200167}
168
169static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, FILE *file,
170 struct list_head *head, size_t privsize)
171{
172 struct objdump_line *objdump_line;
173 char *line = NULL, *tmp, *tmp2, *c;
174 size_t line_len;
175 s64 line_ip, offset = -1;
176
177 if (getline(&line, &line_len, file) < 0)
178 return -1;
179
180 if (!line)
181 return -1;
182
183 while (line_len != 0 && isspace(line[line_len - 1]))
184 line[--line_len] = '\0';
185
186 c = strchr(line, '\n');
187 if (c)
188 *c = 0;
189
190 line_ip = -1;
191
192 /*
193 * Strip leading spaces:
194 */
195 tmp = line;
196 while (*tmp) {
197 if (*tmp != ' ')
198 break;
199 tmp++;
200 }
201
202 if (*tmp) {
203 /*
204 * Parse hexa addresses followed by ':'
205 */
206 line_ip = strtoull(tmp, &tmp2, 16);
207 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
208 line_ip = -1;
209 }
210
211 if (line_ip != -1) {
212 u64 start = map__rip_2objdump(map, sym->start),
213 end = map__rip_2objdump(map, sym->end);
214
215 offset = line_ip - start;
216 if (offset < 0 || (u64)line_ip > end)
217 offset = -1;
218 }
219
220 objdump_line = objdump_line__new(offset, line, privsize);
221 if (objdump_line == NULL) {
222 free(line);
223 return -1;
224 }
225 objdump__add_line(head, objdump_line);
226
227 return 0;
228}
229
230int symbol__annotate(struct symbol *sym, struct map *map,
231 struct list_head *head, size_t privsize)
232{
233 struct dso *dso = map->dso;
234 char *filename = dso__build_id_filename(dso, NULL, 0);
235 bool free_filename = true;
236 char command[PATH_MAX * 2];
237 FILE *file;
238 int err = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200239 char symfs_filename[PATH_MAX];
240
241 if (filename) {
242 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
243 symbol_conf.symfs, filename);
244 }
245
246 if (filename == NULL) {
247 if (dso->has_build_id) {
248 pr_err("Can't annotate %s: not enough memory\n",
249 sym->name);
250 return -ENOMEM;
251 }
252 goto fallback;
253 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
254 strstr(command, "[kernel.kallsyms]") ||
255 access(symfs_filename, R_OK)) {
256 free(filename);
257fallback:
258 /*
259 * If we don't have build-ids or the build-id file isn't in the
260 * cache, or is just a kallsyms file, well, lets hope that this
261 * DSO is the same as when 'perf record' ran.
262 */
263 filename = dso->long_name;
264 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
265 symbol_conf.symfs, filename);
266 free_filename = false;
267 }
268
269 if (dso->origin == DSO__ORIG_KERNEL) {
270 if (dso->annotate_warned)
271 goto out_free_filename;
272 err = -ENOENT;
273 dso->annotate_warned = 1;
274 pr_err("Can't annotate %s: No vmlinux file was found in the "
275 "path\n", sym->name);
276 goto out_free_filename;
277 }
278
279 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
280 filename, sym->name, map->unmap_ip(map, sym->start),
281 map->unmap_ip(map, sym->end));
282
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200283 pr_debug("annotating [%p] %30s : [%p] %30s\n",
284 dso, dso->long_name, sym, sym->name);
285
286 snprintf(command, sizeof(command),
287 "objdump --start-address=0x%016" PRIx64
288 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
289 map__rip_2objdump(map, sym->start),
290 map__rip_2objdump(map, sym->end),
291 symfs_filename, filename);
292
293 pr_debug("Executing: %s\n", command);
294
295 file = popen(command, "r");
296 if (!file)
297 goto out_free_filename;
298
299 while (!feof(file))
300 if (symbol__parse_objdump_line(sym, map, file, head, privsize) < 0)
301 break;
302
303 pclose(file);
304out_free_filename:
305 if (free_filename)
306 free(filename);
307 return err;
308}
309
310static void insert_source_line(struct rb_root *root, struct source_line *src_line)
311{
312 struct source_line *iter;
313 struct rb_node **p = &root->rb_node;
314 struct rb_node *parent = NULL;
315
316 while (*p != NULL) {
317 parent = *p;
318 iter = rb_entry(parent, struct source_line, node);
319
320 if (src_line->percent > iter->percent)
321 p = &(*p)->rb_left;
322 else
323 p = &(*p)->rb_right;
324 }
325
326 rb_link_node(&src_line->node, parent, p);
327 rb_insert_color(&src_line->node, root);
328}
329
330static void symbol__free_source_line(struct symbol *sym, int len)
331{
332 struct annotation *notes = symbol__annotation(sym);
333 struct source_line *src_line = notes->src_line;
334 int i;
335
336 for (i = 0; i < len; i++)
337 free(src_line[i].path);
338
339 free(src_line);
340 notes->src_line = NULL;
341}
342
343/* Get the filename:line for the colored entries */
344static int symbol__get_source_line(struct symbol *sym, struct map *map,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200345 int evidx, struct rb_root *root, int len,
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200346 const char *filename)
347{
348 u64 start;
349 int i;
350 char cmd[PATH_MAX * 2];
351 struct source_line *src_line;
352 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200353 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200354
355 if (!h->sum)
356 return 0;
357
358 src_line = notes->src_line = calloc(len, sizeof(struct source_line));
359 if (!notes->src_line)
360 return -1;
361
362 start = map->unmap_ip(map, sym->start);
363
364 for (i = 0; i < len; i++) {
365 char *path = NULL;
366 size_t line_len;
367 u64 offset;
368 FILE *fp;
369
370 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
371 if (src_line[i].percent <= 0.5)
372 continue;
373
374 offset = start + i;
375 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
376 fp = popen(cmd, "r");
377 if (!fp)
378 continue;
379
380 if (getline(&path, &line_len, fp) < 0 || !line_len)
381 goto next;
382
383 src_line[i].path = malloc(sizeof(char) * line_len + 1);
384 if (!src_line[i].path)
385 goto next;
386
387 strcpy(src_line[i].path, path);
388 insert_source_line(root, &src_line[i]);
389
390 next:
391 pclose(fp);
392 }
393
394 return 0;
395}
396
397static void print_summary(struct rb_root *root, const char *filename)
398{
399 struct source_line *src_line;
400 struct rb_node *node;
401
402 printf("\nSorted summary for file %s\n", filename);
403 printf("----------------------------------------------\n\n");
404
405 if (RB_EMPTY_ROOT(root)) {
406 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
407 return;
408 }
409
410 node = rb_first(root);
411 while (node) {
412 double percent;
413 const char *color;
414 char *path;
415
416 src_line = rb_entry(node, struct source_line, node);
417 percent = src_line->percent;
418 color = get_percent_color(percent);
419 path = src_line->path;
420
421 color_fprintf(stdout, color, " %7.2f %s", percent, path);
422 node = rb_next(node);
423 }
424}
425
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200426static void symbol__annotate_hits(struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200427{
428 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200429 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200430 u64 len = sym->end - sym->start, offset;
431
432 for (offset = 0; offset < len; ++offset)
433 if (h->addr[offset] != 0)
434 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
435 sym->start + offset, h->addr[offset]);
436 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
437}
438
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200439int symbol__annotate_printf(struct symbol *sym, struct map *map,
440 struct list_head *head, int evidx, bool full_paths,
441 int min_pcnt, int max_lines)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200442{
443 struct dso *dso = map->dso;
444 const char *filename = dso->long_name, *d_filename;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200445 struct objdump_line *pos;
Arnaldo Carvalho de Melod040bd32011-02-05 15:37:31 -0200446 int printed = 2;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200447 int more = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200448 u64 len;
449
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200450 if (full_paths)
451 d_filename = filename;
452 else
453 d_filename = basename(filename);
454
455 len = sym->end - sym->start;
456
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200457 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
458 printf("------------------------------------------------\n");
459
460 if (verbose)
461 symbol__annotate_hits(sym, evidx);
462
463 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200464 switch (objdump_line__print(pos, head, sym, evidx, len, min_pcnt,
465 printed, max_lines)) {
466 case 0:
467 ++printed;
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200468 break;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200469 case 1:
470 /* filtered by max_lines */
471 ++more;
472 break;
473 case -1:
474 default:
475 /* filtered by min_pcnt */
476 break;
477 }
478 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200479
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200480 return more;
481}
482
483void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
484{
485 struct annotation *notes = symbol__annotation(sym);
486 struct sym_hist *h = annotation__histogram(notes, evidx);
487
488 memset(h, 0, notes->sizeof_sym_hist);
489}
490
491void symbol__annotate_decay_histogram(struct symbol *sym,
492 struct list_head *head, int evidx)
493{
494 struct annotation *notes = symbol__annotation(sym);
495 struct sym_hist *h = annotation__histogram(notes, evidx);
496 struct objdump_line *pos;
497
498 h->sum = 0;
499
500 list_for_each_entry(pos, head, node) {
501 if (pos->offset != -1) {
502 h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8;
503 h->sum += h->addr[pos->offset];
504 }
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200505 }
506}
507
508void objdump_line_list__purge(struct list_head *head)
509{
510 struct objdump_line *pos, *n;
511
512 list_for_each_entry_safe(pos, n, head, node) {
513 list_del(&pos->node);
514 objdump_line__free(pos);
515 }
516}
517
518int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
519 bool print_lines, bool full_paths, int min_pcnt,
520 int max_lines)
521{
522 struct dso *dso = map->dso;
523 const char *filename = dso->long_name;
524 struct rb_root source_line = RB_ROOT;
525 LIST_HEAD(head);
526 u64 len;
527
528 if (symbol__annotate(sym, map, &head, 0) < 0)
529 return -1;
530
531 len = sym->end - sym->start;
532
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200533 if (print_lines) {
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200534 symbol__get_source_line(sym, map, evidx, &source_line,
535 len, filename);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200536 print_summary(&source_line, filename);
537 }
538
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200539 symbol__annotate_printf(sym, map, &head, evidx, full_paths,
540 min_pcnt, max_lines);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200541 if (print_lines)
542 symbol__free_source_line(sym, len);
543
Arnaldo Carvalho de Melof1e27012011-02-05 18:51:38 -0200544 objdump_line_list__purge(&head);
545
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200546 return 0;
547}