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.
10 #include "util/util.h"
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"
20 #include "util/debug.h"
22 #include "util/parse-options.h"
23 #include "util/parse-events.h"
24 #include "util/thread.h"
25 #include "util/sort.h"
26 #include "util/hist.h"
28 static char const *input_name = "perf.data";
33 static int full_paths;
35 static int print_line;
37 static unsigned long page_size;
38 static unsigned long mmap_window = 32;
40 static struct rb_root threads;
41 static struct thread *last_match;
52 * collect histogram counts
54 static void hist_hit(struct hist_entry *he, u64 ip)
56 unsigned int sym_size, offset;
57 struct symbol *sym = he->sym;
61 if (!sym || !sym->hist)
64 sym_size = sym->end - sym->start;
65 ip = he->map->map_ip(he->map, ip);
66 offset = ip - sym->start;
68 if (offset >= sym_size)
75 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
76 (void *)(unsigned long)he->sym->start,
78 (void *)(unsigned long)ip, ip - he->sym->start,
82 static int hist_entry__add(struct thread *thread, struct map *map,
83 struct symbol *sym, u64 ip, u64 count, char level)
86 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
96 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
99 struct thread *thread;
100 u64 ip = event->ip.ip;
101 struct map *map = NULL;
102 struct symbol *sym = NULL;
104 thread = threads__findnew(event->ip.pid, &threads, &last_match);
106 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
107 (void *)(offset + head),
108 (void *)(long)(event->header.size),
113 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
115 if (thread == NULL) {
116 fprintf(stderr, "problem processing %d event, skipping it.\n",
121 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
123 sym = kernel_maps__find_symbol(ip, &map);
124 dump_printf(" ...... dso: %s\n",
125 map ? map->dso->long_name : "<not found>");
126 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
128 map = thread__find_map(thread, ip);
131 ip = map->map_ip(map, ip);
132 sym = map->dso->find_symbol(map->dso, ip);
135 * If this is outside of all known maps,
136 * and is a negative address, try to look it
137 * up in the kernel dso, as it might be a
138 * vsyscall or vdso (which executes in user-mode).
140 * XXX This is nasty, we should have a symbol list in
141 * the "[vdso]" dso, but for now lets use the old
142 * trick of looking in the whole kernel symbol list.
144 if ((long long)ip < 0) {
149 dump_printf(" ...... dso: %s\n",
150 map ? map->dso->long_name : "<not found>");
153 dump_printf(" ...... dso: [hypervisor]\n");
156 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
157 fprintf(stderr, "problem incrementing symbol count, "
167 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
169 struct thread *thread;
170 struct map *map = map__new(&event->mmap, NULL, 0);
172 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
174 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
175 (void *)(offset + head),
176 (void *)(long)(event->header.size),
178 (void *)(long)event->mmap.start,
179 (void *)(long)event->mmap.len,
180 (void *)(long)event->mmap.pgoff,
181 event->mmap.filename);
183 if (thread == NULL || map == NULL) {
184 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
188 thread__insert_map(thread, map);
195 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
197 struct thread *thread;
199 thread = threads__findnew(event->comm.pid, &threads, &last_match);
200 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
201 (void *)(offset + head),
202 (void *)(long)(event->header.size),
203 event->comm.comm, event->comm.pid);
205 if (thread == NULL ||
206 thread__set_comm(thread, event->comm.comm)) {
207 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
216 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
218 struct thread *thread;
219 struct thread *parent;
221 thread = threads__findnew(event->fork.pid, &threads, &last_match);
222 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
223 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
224 (void *)(offset + head),
225 (void *)(long)(event->header.size),
226 event->fork.pid, event->fork.ppid);
229 * A thread clone will have the same PID for both
232 if (thread == parent)
235 if (!thread || !parent || thread__fork(thread, parent)) {
236 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
245 process_event(event_t *event, unsigned long offset, unsigned long head)
247 switch (event->header.type) {
248 case PERF_RECORD_SAMPLE:
249 return process_sample_event(event, offset, head);
251 case PERF_RECORD_MMAP:
252 return process_mmap_event(event, offset, head);
254 case PERF_RECORD_COMM:
255 return process_comm_event(event, offset, head);
257 case PERF_RECORD_FORK:
258 return process_fork_event(event, offset, head);
260 * We dont process them right now but they are fine:
263 case PERF_RECORD_THROTTLE:
264 case PERF_RECORD_UNTHROTTLE:
275 parse_line(FILE *file, struct symbol *sym, u64 len)
277 char *line = NULL, *tmp, *tmp2;
278 static const char *prev_line;
279 static const char *prev_color;
286 if (getline(&line, &line_len, file) < 0)
291 c = strchr(line, '\n');
300 * Strip leading spaces:
311 * Parse hexa addresses followed by ':'
313 line_ip = strtoull(tmp, &tmp2, 16);
319 const char *path = NULL;
320 unsigned int hits = 0;
321 double percent = 0.0;
323 struct sym_ext *sym_ext = sym->priv;
325 offset = line_ip - sym->start;
327 hits = sym->hist[offset];
329 if (offset < len && sym_ext) {
330 path = sym_ext[offset].path;
331 percent = sym_ext[offset].percent;
332 } else if (sym->hist_sum)
333 percent = 100.0 * hits / sym->hist_sum;
335 color = get_percent_color(percent);
338 * Also color the filename and line if needed, with
339 * the same color than the percentage. Don't print it
340 * twice for close colored ip with the same filename:line
343 if (!prev_line || strcmp(prev_line, path)
344 || color != prev_color) {
345 color_fprintf(stdout, color, " %s", path);
351 color_fprintf(stdout, color, " %7.2f", percent);
353 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
358 printf(" : %s\n", line);
364 static struct rb_root root_sym_ext;
366 static void insert_source_line(struct sym_ext *sym_ext)
368 struct sym_ext *iter;
369 struct rb_node **p = &root_sym_ext.rb_node;
370 struct rb_node *parent = NULL;
374 iter = rb_entry(parent, struct sym_ext, node);
376 if (sym_ext->percent > iter->percent)
382 rb_link_node(&sym_ext->node, parent, p);
383 rb_insert_color(&sym_ext->node, &root_sym_ext);
386 static void free_source_line(struct symbol *sym, int len)
388 struct sym_ext *sym_ext = sym->priv;
394 for (i = 0; i < len; i++)
395 free(sym_ext[i].path);
399 root_sym_ext = RB_ROOT;
402 /* Get the filename:line for the colored entries */
404 get_source_line(struct symbol *sym, int len, const char *filename)
407 char cmd[PATH_MAX * 2];
408 struct sym_ext *sym_ext;
413 sym->priv = calloc(len, sizeof(struct sym_ext));
419 for (i = 0; i < len; i++) {
425 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
426 if (sym_ext[i].percent <= 0.5)
429 offset = sym->start + i;
430 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
431 fp = popen(cmd, "r");
435 if (getline(&path, &line_len, fp) < 0 || !line_len)
438 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
439 if (!sym_ext[i].path)
442 strcpy(sym_ext[i].path, path);
443 insert_source_line(&sym_ext[i]);
450 static void print_summary(const char *filename)
452 struct sym_ext *sym_ext;
453 struct rb_node *node;
455 printf("\nSorted summary for file %s\n", filename);
456 printf("----------------------------------------------\n\n");
458 if (RB_EMPTY_ROOT(&root_sym_ext)) {
459 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
463 node = rb_first(&root_sym_ext);
469 sym_ext = rb_entry(node, struct sym_ext, node);
470 percent = sym_ext->percent;
471 color = get_percent_color(percent);
472 path = sym_ext->path;
474 color_fprintf(stdout, color, " %7.2f %s", percent, path);
475 node = rb_next(node);
479 static void annotate_sym(struct dso *dso, struct symbol *sym)
481 const char *filename = dso->long_name, *d_filename;
483 char command[PATH_MAX*2];
490 d_filename = filename;
492 d_filename = basename(filename);
494 len = sym->end - sym->start;
497 get_source_line(sym, len, filename);
498 print_summary(filename);
501 printf("\n\n------------------------------------------------\n");
502 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
503 printf("------------------------------------------------\n");
506 printf("annotating [%p] %30s : [%p] %30s\n",
507 dso, dso->long_name, sym, sym->name);
509 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
510 sym->start, sym->end, filename, filename);
513 printf("doing: %s\n", command);
515 file = popen(command, "r");
519 while (!feof(file)) {
520 if (parse_line(file, sym, len) < 0)
526 free_source_line(sym, len);
529 static void find_annotations(void)
535 list_for_each_entry(dso, &dsos, node) {
537 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
538 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
541 annotate_sym(dso, sym);
548 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
551 static int __cmd_annotate(void)
553 int ret, rc = EXIT_FAILURE;
554 unsigned long offset = 0;
555 unsigned long head = 0;
556 struct stat input_stat;
561 register_idle_thread(&threads, &last_match);
563 input = open(input_name, O_RDONLY);
565 perror("failed to open file");
569 ret = fstat(input, &input_stat);
571 perror("failed to stat file");
575 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
576 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
580 if (!input_stat.st_size) {
581 fprintf(stderr, "zero-sized file, nothing to do!\n");
585 if (load_kernel() < 0) {
586 perror("failed to load kernel symbols");
591 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
592 MAP_SHARED, input, offset);
593 if (buf == MAP_FAILED) {
594 perror("failed to mmap file");
599 event = (event_t *)(buf + head);
601 size = event->header.size;
605 if (head + event->header.size >= page_size * mmap_window) {
606 unsigned long shift = page_size * (head / page_size);
609 munmap_ret = munmap(buf, page_size * mmap_window);
610 assert(munmap_ret == 0);
617 size = event->header.size;
619 dump_printf("%p [%p]: event: %d\n",
620 (void *)(offset + head),
621 (void *)(long)event->header.size,
624 if (!size || process_event(event, offset, head) < 0) {
626 dump_printf("%p [%p]: skipping unknown header type: %d\n",
627 (void *)(offset + head),
628 (void *)(long)(event->header.size),
634 * assume we lost track of the stream, check alignment, and
635 * increment a single u64 in the hope to catch on again 'soon'.
638 if (unlikely(head & 7))
646 if (offset + head < (unsigned long)input_stat.st_size)
652 dump_printf(" IP events: %10ld\n", total);
653 dump_printf(" mmap events: %10ld\n", total_mmap);
654 dump_printf(" comm events: %10ld\n", total_comm);
655 dump_printf(" fork events: %10ld\n", total_fork);
656 dump_printf(" unknown events: %10ld\n", total_unknown);
662 threads__fprintf(stdout, &threads);
665 dsos__fprintf(stdout);
668 output__resort(total);
675 static const char * const annotate_usage[] = {
676 "perf annotate [<options>] <command>",
680 static const struct option options[] = {
681 OPT_STRING('i', "input", &input_name, "file",
683 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
684 "symbol to annotate"),
685 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
686 OPT_BOOLEAN('v', "verbose", &verbose,
687 "be more verbose (show symbol address, etc)"),
688 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
689 "dump raw trace in ASCII"),
690 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
691 OPT_BOOLEAN('m', "modules", &modules,
692 "load module symbols - WARNING: use only with -k and LIVE kernel"),
693 OPT_BOOLEAN('l', "print-line", &print_line,
694 "print matching source lines (may be slow)"),
695 OPT_BOOLEAN('P', "full-paths", &full_paths,
696 "Don't shorten the displayed pathnames"),
700 static void setup_sorting(void)
702 char *tmp, *tok, *str = strdup(sort_order);
704 for (tok = strtok_r(str, ", ", &tmp);
705 tok; tok = strtok_r(NULL, ", ", &tmp)) {
706 if (sort_dimension__add(tok) < 0) {
707 error("Unknown --sort key: `%s'", tok);
708 usage_with_options(annotate_usage, options);
715 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
719 page_size = getpagesize();
721 argc = parse_options(argc, argv, options, annotate_usage, 0);
727 * Special case: if there's an argument left then assume tha
728 * it's a symbol filter:
731 usage_with_options(annotate_usage, options);
733 sym_hist_filter = argv[0];
736 if (!sym_hist_filter)
737 usage_with_options(annotate_usage, options);
741 if (field_sep && *field_sep == '.') {
742 fputs("'.' is the only non valid --field-separator argument\n",
747 return __cmd_annotate();