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";
32 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
34 static int full_paths;
36 static int print_line;
38 static unsigned long page_size;
39 static unsigned long mmap_window = 32;
41 static struct rb_root threads;
42 static struct thread *last_match;
53 * collect histogram counts
55 static void hist_hit(struct hist_entry *he, u64 ip)
57 unsigned int sym_size, offset;
58 struct symbol *sym = he->sym;
62 if (!sym || !sym->hist)
65 sym_size = sym->end - sym->start;
66 ip = he->map->map_ip(he->map, ip);
67 offset = ip - sym->start;
69 if (offset >= sym_size)
76 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
77 (void *)(unsigned long)he->sym->start,
79 (void *)(unsigned long)ip, ip - he->sym->start,
84 hist_entry__add(struct thread *thread, struct map *map,
85 struct symbol *sym, u64 ip, char level)
87 struct rb_node **p = &hist.rb_node;
88 struct rb_node *parent = NULL;
89 struct hist_entry *he;
90 struct hist_entry entry = {
102 he = rb_entry(parent, struct hist_entry, rb_node);
104 cmp = hist_entry__cmp(&entry, he);
118 he = malloc(sizeof(*he));
122 rb_link_node(&he->rb_node, parent, p);
123 rb_insert_color(&he->rb_node, &hist);
129 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
133 struct thread *thread;
134 u64 ip = event->ip.ip;
135 struct map *map = NULL;
136 struct symbol *sym = NULL;
138 thread = threads__findnew(event->ip.pid, &threads, &last_match);
140 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
141 (void *)(offset + head),
142 (void *)(long)(event->header.size),
147 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
149 if (thread == NULL) {
150 fprintf(stderr, "problem processing %d event, skipping it.\n",
155 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
158 sym = kernel_maps__find_symbol(ip, &map);
159 dump_printf(" ...... dso: %s\n",
160 map ? map->dso->long_name : "<not found>");
161 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
164 map = thread__find_map(thread, ip);
167 ip = map->map_ip(map, ip);
168 sym = map->dso->find_symbol(map->dso, ip);
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).
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.
180 if ((long long)ip < 0) {
185 dump_printf(" ...... dso: %s\n",
186 map ? map->dso->long_name : "<not found>");
190 dump_printf(" ...... dso: [hypervisor]\n");
193 if (show & show_mask) {
194 if (hist_entry__add(thread, map, sym, ip, level)) {
196 "problem incrementing symbol count, skipping event\n");
206 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
208 struct thread *thread;
209 struct map *map = map__new(&event->mmap, NULL, 0);
211 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
213 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
214 (void *)(offset + head),
215 (void *)(long)(event->header.size),
217 (void *)(long)event->mmap.start,
218 (void *)(long)event->mmap.len,
219 (void *)(long)event->mmap.pgoff,
220 event->mmap.filename);
222 if (thread == NULL || map == NULL) {
223 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
227 thread__insert_map(thread, map);
234 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
236 struct thread *thread;
238 thread = threads__findnew(event->comm.pid, &threads, &last_match);
239 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
240 (void *)(offset + head),
241 (void *)(long)(event->header.size),
242 event->comm.comm, event->comm.pid);
244 if (thread == NULL ||
245 thread__set_comm(thread, event->comm.comm)) {
246 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
255 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
257 struct thread *thread;
258 struct thread *parent;
260 thread = threads__findnew(event->fork.pid, &threads, &last_match);
261 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
262 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
263 (void *)(offset + head),
264 (void *)(long)(event->header.size),
265 event->fork.pid, event->fork.ppid);
268 * A thread clone will have the same PID for both
271 if (thread == parent)
274 if (!thread || !parent || thread__fork(thread, parent)) {
275 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
284 process_event(event_t *event, unsigned long offset, unsigned long head)
286 switch (event->header.type) {
287 case PERF_RECORD_SAMPLE:
288 return process_sample_event(event, offset, head);
290 case PERF_RECORD_MMAP:
291 return process_mmap_event(event, offset, head);
293 case PERF_RECORD_COMM:
294 return process_comm_event(event, offset, head);
296 case PERF_RECORD_FORK:
297 return process_fork_event(event, offset, head);
299 * We dont process them right now but they are fine:
302 case PERF_RECORD_THROTTLE:
303 case PERF_RECORD_UNTHROTTLE:
314 parse_line(FILE *file, struct symbol *sym, u64 len)
316 char *line = NULL, *tmp, *tmp2;
317 static const char *prev_line;
318 static const char *prev_color;
325 if (getline(&line, &line_len, file) < 0)
330 c = strchr(line, '\n');
339 * Strip leading spaces:
350 * Parse hexa addresses followed by ':'
352 line_ip = strtoull(tmp, &tmp2, 16);
358 const char *path = NULL;
359 unsigned int hits = 0;
360 double percent = 0.0;
362 struct sym_ext *sym_ext = sym->priv;
364 offset = line_ip - sym->start;
366 hits = sym->hist[offset];
368 if (offset < len && sym_ext) {
369 path = sym_ext[offset].path;
370 percent = sym_ext[offset].percent;
371 } else if (sym->hist_sum)
372 percent = 100.0 * hits / sym->hist_sum;
374 color = get_percent_color(percent);
377 * Also color the filename and line if needed, with
378 * the same color than the percentage. Don't print it
379 * twice for close colored ip with the same filename:line
382 if (!prev_line || strcmp(prev_line, path)
383 || color != prev_color) {
384 color_fprintf(stdout, color, " %s", path);
390 color_fprintf(stdout, color, " %7.2f", percent);
392 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
397 printf(" : %s\n", line);
403 static struct rb_root root_sym_ext;
405 static void insert_source_line(struct sym_ext *sym_ext)
407 struct sym_ext *iter;
408 struct rb_node **p = &root_sym_ext.rb_node;
409 struct rb_node *parent = NULL;
413 iter = rb_entry(parent, struct sym_ext, node);
415 if (sym_ext->percent > iter->percent)
421 rb_link_node(&sym_ext->node, parent, p);
422 rb_insert_color(&sym_ext->node, &root_sym_ext);
425 static void free_source_line(struct symbol *sym, int len)
427 struct sym_ext *sym_ext = sym->priv;
433 for (i = 0; i < len; i++)
434 free(sym_ext[i].path);
438 root_sym_ext = RB_ROOT;
441 /* Get the filename:line for the colored entries */
443 get_source_line(struct symbol *sym, int len, const char *filename)
446 char cmd[PATH_MAX * 2];
447 struct sym_ext *sym_ext;
452 sym->priv = calloc(len, sizeof(struct sym_ext));
458 for (i = 0; i < len; i++) {
464 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
465 if (sym_ext[i].percent <= 0.5)
468 offset = sym->start + i;
469 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
470 fp = popen(cmd, "r");
474 if (getline(&path, &line_len, fp) < 0 || !line_len)
477 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
478 if (!sym_ext[i].path)
481 strcpy(sym_ext[i].path, path);
482 insert_source_line(&sym_ext[i]);
489 static void print_summary(const char *filename)
491 struct sym_ext *sym_ext;
492 struct rb_node *node;
494 printf("\nSorted summary for file %s\n", filename);
495 printf("----------------------------------------------\n\n");
497 if (RB_EMPTY_ROOT(&root_sym_ext)) {
498 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
502 node = rb_first(&root_sym_ext);
508 sym_ext = rb_entry(node, struct sym_ext, node);
509 percent = sym_ext->percent;
510 color = get_percent_color(percent);
511 path = sym_ext->path;
513 color_fprintf(stdout, color, " %7.2f %s", percent, path);
514 node = rb_next(node);
518 static void annotate_sym(struct dso *dso, struct symbol *sym)
520 const char *filename = dso->long_name, *d_filename;
522 char command[PATH_MAX*2];
529 d_filename = filename;
531 d_filename = basename(filename);
533 len = sym->end - sym->start;
536 get_source_line(sym, len, filename);
537 print_summary(filename);
540 printf("\n\n------------------------------------------------\n");
541 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
542 printf("------------------------------------------------\n");
545 printf("annotating [%p] %30s : [%p] %30s\n",
546 dso, dso->long_name, sym, sym->name);
548 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
549 sym->start, sym->end, filename, filename);
552 printf("doing: %s\n", command);
554 file = popen(command, "r");
558 while (!feof(file)) {
559 if (parse_line(file, sym, len) < 0)
565 free_source_line(sym, len);
568 static void find_annotations(void)
574 list_for_each_entry(dso, &dsos, node) {
576 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
577 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
580 annotate_sym(dso, sym);
587 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
590 static int __cmd_annotate(void)
592 int ret, rc = EXIT_FAILURE;
593 unsigned long offset = 0;
594 unsigned long head = 0;
595 struct stat input_stat;
600 register_idle_thread(&threads, &last_match);
602 input = open(input_name, O_RDONLY);
604 perror("failed to open file");
608 ret = fstat(input, &input_stat);
610 perror("failed to stat file");
614 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
615 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
619 if (!input_stat.st_size) {
620 fprintf(stderr, "zero-sized file, nothing to do!\n");
624 if (load_kernel() < 0) {
625 perror("failed to load kernel symbols");
630 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
631 MAP_SHARED, input, offset);
632 if (buf == MAP_FAILED) {
633 perror("failed to mmap file");
638 event = (event_t *)(buf + head);
640 size = event->header.size;
644 if (head + event->header.size >= page_size * mmap_window) {
645 unsigned long shift = page_size * (head / page_size);
648 munmap_ret = munmap(buf, page_size * mmap_window);
649 assert(munmap_ret == 0);
656 size = event->header.size;
658 dump_printf("%p [%p]: event: %d\n",
659 (void *)(offset + head),
660 (void *)(long)event->header.size,
663 if (!size || process_event(event, offset, head) < 0) {
665 dump_printf("%p [%p]: skipping unknown header type: %d\n",
666 (void *)(offset + head),
667 (void *)(long)(event->header.size),
673 * assume we lost track of the stream, check alignment, and
674 * increment a single u64 in the hope to catch on again 'soon'.
677 if (unlikely(head & 7))
685 if (offset + head < (unsigned long)input_stat.st_size)
691 dump_printf(" IP events: %10ld\n", total);
692 dump_printf(" mmap events: %10ld\n", total_mmap);
693 dump_printf(" comm events: %10ld\n", total_comm);
694 dump_printf(" fork events: %10ld\n", total_fork);
695 dump_printf(" unknown events: %10ld\n", total_unknown);
701 threads__fprintf(stdout, &threads);
704 dsos__fprintf(stdout);
707 output__resort(total);
714 static const char * const annotate_usage[] = {
715 "perf annotate [<options>] <command>",
719 static const struct option options[] = {
720 OPT_STRING('i', "input", &input_name, "file",
722 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
723 "symbol to annotate"),
724 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
725 OPT_BOOLEAN('v', "verbose", &verbose,
726 "be more verbose (show symbol address, etc)"),
727 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
728 "dump raw trace in ASCII"),
729 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
730 OPT_BOOLEAN('m', "modules", &modules,
731 "load module symbols - WARNING: use only with -k and LIVE kernel"),
732 OPT_BOOLEAN('l', "print-line", &print_line,
733 "print matching source lines (may be slow)"),
734 OPT_BOOLEAN('P', "full-paths", &full_paths,
735 "Don't shorten the displayed pathnames"),
739 static void setup_sorting(void)
741 char *tmp, *tok, *str = strdup(sort_order);
743 for (tok = strtok_r(str, ", ", &tmp);
744 tok; tok = strtok_r(NULL, ", ", &tmp)) {
745 if (sort_dimension__add(tok) < 0) {
746 error("Unknown --sort key: `%s'", tok);
747 usage_with_options(annotate_usage, options);
754 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
758 page_size = getpagesize();
760 argc = parse_options(argc, argv, options, annotate_usage, 0);
766 * Special case: if there's an argument left then assume tha
767 * it's a symbol filter:
770 usage_with_options(annotate_usage, options);
772 sym_hist_filter = argv[0];
775 if (!sym_hist_filter)
776 usage_with_options(annotate_usage, options);
780 if (field_sep && *field_sep == '.') {
781 fputs("'.' is the only non valid --field-separator argument\n",
786 return __cmd_annotate();