7d39bd2b19b83d4b59973f68a04d7531c0249967
[deliverable/linux.git] / tools / perf / builtin-annotate.c
1 /*
2 * builtin-annotate.c
3 *
4 * Builtin annotate command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
8 #include "builtin.h"
9
10 #include "util/util.h"
11
12 #include "util/color.h"
13 #include <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/string.h"
18
19 #include "perf.h"
20 #include "util/debug.h"
21
22 #include "util/event.h"
23 #include "util/parse-options.h"
24 #include "util/parse-events.h"
25 #include "util/thread.h"
26 #include "util/sort.h"
27 #include "util/hist.h"
28
29 static char const *input_name = "perf.data";
30
31 static int force;
32 static int input;
33
34 static int full_paths;
35
36 static int print_line;
37
38 static unsigned long page_size;
39 static unsigned long mmap_window = 32;
40
41 struct sym_hist {
42 u64 sum;
43 u64 ip[0];
44 };
45
46 struct sym_ext {
47 struct rb_node node;
48 double percent;
49 char *path;
50 };
51
52 struct sym_priv {
53 struct sym_hist *hist;
54 struct sym_ext *ext;
55 };
56
57 static struct symbol_conf symbol_conf = {
58 .priv_size = sizeof(struct sym_priv),
59 .try_vmlinux_path = true,
60 };
61
62 static const char *sym_hist_filter;
63
64 static int symbol_filter(struct map *map __used, struct symbol *sym)
65 {
66 if (sym_hist_filter == NULL ||
67 strcmp(sym->name, sym_hist_filter) == 0) {
68 struct sym_priv *priv = symbol__priv(sym);
69 const int size = (sizeof(*priv->hist) +
70 (sym->end - sym->start) * sizeof(u64));
71
72 priv->hist = malloc(size);
73 if (priv->hist)
74 memset(priv->hist, 0, size);
75 return 0;
76 }
77 /*
78 * FIXME: We should really filter it out, as we don't want to go thru symbols
79 * we're not interested, and if a DSO ends up with no symbols, delete it too,
80 * but right now the kernel loading routines in symbol.c bail out if no symbols
81 * are found, fix it later.
82 */
83 return 0;
84 }
85
86 /*
87 * collect histogram counts
88 */
89 static void hist_hit(struct hist_entry *he, u64 ip)
90 {
91 unsigned int sym_size, offset;
92 struct symbol *sym = he->sym;
93 struct sym_priv *priv;
94 struct sym_hist *h;
95
96 he->count++;
97
98 if (!sym || !he->map)
99 return;
100
101 priv = symbol__priv(sym);
102 if (!priv->hist)
103 return;
104
105 sym_size = sym->end - sym->start;
106 offset = ip - sym->start;
107
108 if (verbose)
109 fprintf(stderr, "%s: ip=%Lx\n", __func__,
110 he->map->unmap_ip(he->map, ip));
111
112 if (offset >= sym_size)
113 return;
114
115 h = priv->hist;
116 h->sum++;
117 h->ip[offset]++;
118
119 if (verbose >= 3)
120 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
121 (void *)(unsigned long)he->sym->start,
122 he->sym->name,
123 (void *)(unsigned long)ip, ip - he->sym->start,
124 h->ip[offset]);
125 }
126
127 static int hist_entry__add(struct thread *thread, struct map *map,
128 struct symbol *sym, u64 ip, u64 count, char level)
129 {
130 bool hit;
131 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
132 count, level, &hit);
133 if (he == NULL)
134 return -ENOMEM;
135 hist_hit(he, ip);
136 return 0;
137 }
138
139 static int process_sample_event(event_t *event)
140 {
141 char level;
142 u64 ip = event->ip.ip;
143 struct map *map = NULL;
144 struct symbol *sym = NULL;
145 struct thread *thread = threads__findnew(event->ip.pid);
146
147 dump_printf("(IP, %d): %d: %p\n", event->header.misc,
148 event->ip.pid, (void *)(long)ip);
149
150 if (thread == NULL) {
151 fprintf(stderr, "problem processing %d event, skipping it.\n",
152 event->header.type);
153 return -1;
154 }
155
156 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
157
158 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
159 level = 'k';
160 sym = kernel_maps__find_function(ip, &map, symbol_filter);
161 dump_printf(" ...... dso: %s\n",
162 map ? map->dso->long_name : "<not found>");
163 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
164 level = '.';
165 map = thread__find_map(thread, MAP__FUNCTION, ip);
166 if (map != NULL) {
167 ip = map->map_ip(map, ip);
168 sym = map__find_symbol(map, ip, symbol_filter);
169 } else {
170 /*
171 * If this is outside of all known maps,
172 * and is a negative address, try to look it
173 * up in the kernel dso, as it might be a
174 * vsyscall or vdso (which executes in user-mode).
175 *
176 * XXX This is nasty, we should have a symbol list in
177 * the "[vdso]" dso, but for now lets use the old
178 * trick of looking in the whole kernel symbol list.
179 */
180 if ((long long)ip < 0)
181 sym = kernel_maps__find_function(ip, &map,
182 symbol_filter);
183 }
184 dump_printf(" ...... dso: %s\n",
185 map ? map->dso->long_name : "<not found>");
186 } else {
187 level = 'H';
188 dump_printf(" ...... dso: [hypervisor]\n");
189 }
190
191 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
192 fprintf(stderr, "problem incrementing symbol count, "
193 "skipping event\n");
194 return -1;
195 }
196
197 return 0;
198 }
199
200 static int event__process(event_t *self)
201 {
202 switch (self->header.type) {
203 case PERF_RECORD_SAMPLE:
204 return process_sample_event(self);
205
206 case PERF_RECORD_MMAP:
207 return event__process_mmap(self);
208
209 case PERF_RECORD_COMM:
210 return event__process_comm(self);
211
212 case PERF_RECORD_FORK:
213 return event__process_task(self);
214 /*
215 * We dont process them right now but they are fine:
216 */
217
218 case PERF_RECORD_THROTTLE:
219 case PERF_RECORD_UNTHROTTLE:
220 return 0;
221
222 default:
223 return -1;
224 }
225
226 return 0;
227 }
228
229 static int parse_line(FILE *file, struct hist_entry *he, u64 len)
230 {
231 struct symbol *sym = he->sym;
232 char *line = NULL, *tmp, *tmp2;
233 static const char *prev_line;
234 static const char *prev_color;
235 unsigned int offset;
236 size_t line_len;
237 u64 start;
238 s64 line_ip;
239 int ret;
240 char *c;
241
242 if (getline(&line, &line_len, file) < 0)
243 return -1;
244 if (!line)
245 return -1;
246
247 c = strchr(line, '\n');
248 if (c)
249 *c = 0;
250
251 line_ip = -1;
252 offset = 0;
253 ret = -2;
254
255 /*
256 * Strip leading spaces:
257 */
258 tmp = line;
259 while (*tmp) {
260 if (*tmp != ' ')
261 break;
262 tmp++;
263 }
264
265 if (*tmp) {
266 /*
267 * Parse hexa addresses followed by ':'
268 */
269 line_ip = strtoull(tmp, &tmp2, 16);
270 if (*tmp2 != ':')
271 line_ip = -1;
272 }
273
274 start = he->map->unmap_ip(he->map, sym->start);
275
276 if (line_ip != -1) {
277 const char *path = NULL;
278 unsigned int hits = 0;
279 double percent = 0.0;
280 const char *color;
281 struct sym_priv *priv = symbol__priv(sym);
282 struct sym_ext *sym_ext = priv->ext;
283 struct sym_hist *h = priv->hist;
284
285 offset = line_ip - start;
286 if (offset < len)
287 hits = h->ip[offset];
288
289 if (offset < len && sym_ext) {
290 path = sym_ext[offset].path;
291 percent = sym_ext[offset].percent;
292 } else if (h->sum)
293 percent = 100.0 * hits / h->sum;
294
295 color = get_percent_color(percent);
296
297 /*
298 * Also color the filename and line if needed, with
299 * the same color than the percentage. Don't print it
300 * twice for close colored ip with the same filename:line
301 */
302 if (path) {
303 if (!prev_line || strcmp(prev_line, path)
304 || color != prev_color) {
305 color_fprintf(stdout, color, " %s", path);
306 prev_line = path;
307 prev_color = color;
308 }
309 }
310
311 color_fprintf(stdout, color, " %7.2f", percent);
312 printf(" : ");
313 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
314 } else {
315 if (!*line)
316 printf(" :\n");
317 else
318 printf(" : %s\n", line);
319 }
320
321 return 0;
322 }
323
324 static struct rb_root root_sym_ext;
325
326 static void insert_source_line(struct sym_ext *sym_ext)
327 {
328 struct sym_ext *iter;
329 struct rb_node **p = &root_sym_ext.rb_node;
330 struct rb_node *parent = NULL;
331
332 while (*p != NULL) {
333 parent = *p;
334 iter = rb_entry(parent, struct sym_ext, node);
335
336 if (sym_ext->percent > iter->percent)
337 p = &(*p)->rb_left;
338 else
339 p = &(*p)->rb_right;
340 }
341
342 rb_link_node(&sym_ext->node, parent, p);
343 rb_insert_color(&sym_ext->node, &root_sym_ext);
344 }
345
346 static void free_source_line(struct hist_entry *he, int len)
347 {
348 struct sym_priv *priv = symbol__priv(he->sym);
349 struct sym_ext *sym_ext = priv->ext;
350 int i;
351
352 if (!sym_ext)
353 return;
354
355 for (i = 0; i < len; i++)
356 free(sym_ext[i].path);
357 free(sym_ext);
358
359 priv->ext = NULL;
360 root_sym_ext = RB_ROOT;
361 }
362
363 /* Get the filename:line for the colored entries */
364 static void
365 get_source_line(struct hist_entry *he, int len, const char *filename)
366 {
367 struct symbol *sym = he->sym;
368 u64 start;
369 int i;
370 char cmd[PATH_MAX * 2];
371 struct sym_ext *sym_ext;
372 struct sym_priv *priv = symbol__priv(sym);
373 struct sym_hist *h = priv->hist;
374
375 if (!h->sum)
376 return;
377
378 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
379 if (!priv->ext)
380 return;
381
382 start = he->map->unmap_ip(he->map, sym->start);
383
384 for (i = 0; i < len; i++) {
385 char *path = NULL;
386 size_t line_len;
387 u64 offset;
388 FILE *fp;
389
390 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
391 if (sym_ext[i].percent <= 0.5)
392 continue;
393
394 offset = start + i;
395 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
396 fp = popen(cmd, "r");
397 if (!fp)
398 continue;
399
400 if (getline(&path, &line_len, fp) < 0 || !line_len)
401 goto next;
402
403 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
404 if (!sym_ext[i].path)
405 goto next;
406
407 strcpy(sym_ext[i].path, path);
408 insert_source_line(&sym_ext[i]);
409
410 next:
411 pclose(fp);
412 }
413 }
414
415 static void print_summary(const char *filename)
416 {
417 struct sym_ext *sym_ext;
418 struct rb_node *node;
419
420 printf("\nSorted summary for file %s\n", filename);
421 printf("----------------------------------------------\n\n");
422
423 if (RB_EMPTY_ROOT(&root_sym_ext)) {
424 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
425 return;
426 }
427
428 node = rb_first(&root_sym_ext);
429 while (node) {
430 double percent;
431 const char *color;
432 char *path;
433
434 sym_ext = rb_entry(node, struct sym_ext, node);
435 percent = sym_ext->percent;
436 color = get_percent_color(percent);
437 path = sym_ext->path;
438
439 color_fprintf(stdout, color, " %7.2f %s", percent, path);
440 node = rb_next(node);
441 }
442 }
443
444 static void annotate_sym(struct hist_entry *he)
445 {
446 struct map *map = he->map;
447 struct dso *dso = map->dso;
448 struct symbol *sym = he->sym;
449 const char *filename = dso->long_name, *d_filename;
450 u64 len;
451 char command[PATH_MAX*2];
452 FILE *file;
453
454 if (!filename)
455 return;
456
457 if (verbose)
458 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
459 __func__, filename, sym->name,
460 map->unmap_ip(map, sym->start),
461 map->unmap_ip(map, sym->end));
462
463 if (full_paths)
464 d_filename = filename;
465 else
466 d_filename = basename(filename);
467
468 len = sym->end - sym->start;
469
470 if (print_line) {
471 get_source_line(he, len, filename);
472 print_summary(filename);
473 }
474
475 printf("\n\n------------------------------------------------\n");
476 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
477 printf("------------------------------------------------\n");
478
479 if (verbose >= 2)
480 printf("annotating [%p] %30s : [%p] %30s\n",
481 dso, dso->long_name, sym, sym->name);
482
483 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
484 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
485 filename, filename);
486
487 if (verbose >= 3)
488 printf("doing: %s\n", command);
489
490 file = popen(command, "r");
491 if (!file)
492 return;
493
494 while (!feof(file)) {
495 if (parse_line(file, he, len) < 0)
496 break;
497 }
498
499 pclose(file);
500 if (print_line)
501 free_source_line(he, len);
502 }
503
504 static void find_annotations(void)
505 {
506 struct rb_node *nd;
507
508 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
509 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
510 struct sym_priv *priv;
511
512 if (he->sym == NULL)
513 continue;
514
515 priv = symbol__priv(he->sym);
516 if (priv->hist == NULL)
517 continue;
518
519 annotate_sym(he);
520 /*
521 * Since we have a hist_entry per IP for the same symbol, free
522 * he->sym->hist to signal we already processed this symbol.
523 */
524 free(priv->hist);
525 priv->hist = NULL;
526 }
527 }
528
529 static int __cmd_annotate(void)
530 {
531 int ret, rc = EXIT_FAILURE;
532 unsigned long offset = 0;
533 unsigned long head = 0;
534 struct stat input_stat;
535 event_t *event;
536 uint32_t size;
537 char *buf;
538
539 register_idle_thread();
540
541 input = open(input_name, O_RDONLY);
542 if (input < 0) {
543 perror("failed to open file");
544 exit(-1);
545 }
546
547 ret = fstat(input, &input_stat);
548 if (ret < 0) {
549 perror("failed to stat file");
550 exit(-1);
551 }
552
553 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
554 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
555 exit(-1);
556 }
557
558 if (!input_stat.st_size) {
559 fprintf(stderr, "zero-sized file, nothing to do!\n");
560 exit(0);
561 }
562
563 remap:
564 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
565 MAP_SHARED, input, offset);
566 if (buf == MAP_FAILED) {
567 perror("failed to mmap file");
568 exit(-1);
569 }
570
571 more:
572 event = (event_t *)(buf + head);
573
574 size = event->header.size;
575 if (!size)
576 size = 8;
577
578 if (head + event->header.size >= page_size * mmap_window) {
579 unsigned long shift = page_size * (head / page_size);
580 int munmap_ret;
581
582 munmap_ret = munmap(buf, page_size * mmap_window);
583 assert(munmap_ret == 0);
584
585 offset += shift;
586 head -= shift;
587 goto remap;
588 }
589
590 size = event->header.size;
591
592 dump_printf("%p [%p]: event: %d\n",
593 (void *)(offset + head),
594 (void *)(long)event->header.size,
595 event->header.type);
596
597 if (!size || event__process(event) < 0) {
598
599 dump_printf("%p [%p]: skipping unknown header type: %d\n",
600 (void *)(offset + head),
601 (void *)(long)(event->header.size),
602 event->header.type);
603 /*
604 * assume we lost track of the stream, check alignment, and
605 * increment a single u64 in the hope to catch on again 'soon'.
606 */
607
608 if (unlikely(head & 7))
609 head &= ~7ULL;
610
611 size = 8;
612 }
613
614 head += size;
615
616 if (offset + head < (unsigned long)input_stat.st_size)
617 goto more;
618
619 rc = EXIT_SUCCESS;
620 close(input);
621
622
623 if (dump_trace) {
624 event__print_totals();
625 return 0;
626 }
627
628 if (verbose > 3)
629 threads__fprintf(stdout);
630
631 if (verbose > 2)
632 dsos__fprintf(stdout);
633
634 collapse__resort();
635 output__resort(event__total[0]);
636
637 find_annotations();
638
639 return rc;
640 }
641
642 static const char * const annotate_usage[] = {
643 "perf annotate [<options>] <command>",
644 NULL
645 };
646
647 static const struct option options[] = {
648 OPT_STRING('i', "input", &input_name, "file",
649 "input file name"),
650 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
651 "symbol to annotate"),
652 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
653 OPT_BOOLEAN('v', "verbose", &verbose,
654 "be more verbose (show symbol address, etc)"),
655 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
656 "dump raw trace in ASCII"),
657 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
658 "file", "vmlinux pathname"),
659 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
660 "load module symbols - WARNING: use only with -k and LIVE kernel"),
661 OPT_BOOLEAN('l', "print-line", &print_line,
662 "print matching source lines (may be slow)"),
663 OPT_BOOLEAN('P', "full-paths", &full_paths,
664 "Don't shorten the displayed pathnames"),
665 OPT_END()
666 };
667
668 static void setup_sorting(void)
669 {
670 char *tmp, *tok, *str = strdup(sort_order);
671
672 for (tok = strtok_r(str, ", ", &tmp);
673 tok; tok = strtok_r(NULL, ", ", &tmp)) {
674 if (sort_dimension__add(tok) < 0) {
675 error("Unknown --sort key: `%s'", tok);
676 usage_with_options(annotate_usage, options);
677 }
678 }
679
680 free(str);
681 }
682
683 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
684 {
685 if (symbol__init(&symbol_conf) < 0)
686 return -1;
687
688 page_size = getpagesize();
689
690 argc = parse_options(argc, argv, options, annotate_usage, 0);
691
692 setup_sorting();
693
694 if (argc) {
695 /*
696 * Special case: if there's an argument left then assume tha
697 * it's a symbol filter:
698 */
699 if (argc > 1)
700 usage_with_options(annotate_usage, options);
701
702 sym_hist_filter = argv[0];
703 }
704
705 setup_pager();
706
707 if (field_sep && *field_sep == '.') {
708 fputs("'.' is the only non valid --field-separator argument\n",
709 stderr);
710 exit(129);
711 }
712
713 return __cmd_annotate();
714 }
This page took 0.055753 seconds and 4 git commands to generate.