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,
83 static int hist_entry__add(struct thread *thread, struct map *map,
84 struct symbol *sym, u64 ip, u64 count, char level)
87 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
97 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
101 struct thread *thread;
102 u64 ip = event->ip.ip;
103 struct map *map = NULL;
104 struct symbol *sym = NULL;
106 thread = threads__findnew(event->ip.pid, &threads, &last_match);
108 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
109 (void *)(offset + head),
110 (void *)(long)(event->header.size),
115 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
117 if (thread == NULL) {
118 fprintf(stderr, "problem processing %d event, skipping it.\n",
123 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
126 sym = kernel_maps__find_symbol(ip, &map);
127 dump_printf(" ...... dso: %s\n",
128 map ? map->dso->long_name : "<not found>");
129 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
132 map = thread__find_map(thread, ip);
135 ip = map->map_ip(map, ip);
136 sym = map->dso->find_symbol(map->dso, ip);
139 * If this is outside of all known maps,
140 * and is a negative address, try to look it
141 * up in the kernel dso, as it might be a
142 * vsyscall or vdso (which executes in user-mode).
144 * XXX This is nasty, we should have a symbol list in
145 * the "[vdso]" dso, but for now lets use the old
146 * trick of looking in the whole kernel symbol list.
148 if ((long long)ip < 0) {
153 dump_printf(" ...... dso: %s\n",
154 map ? map->dso->long_name : "<not found>");
158 dump_printf(" ...... dso: [hypervisor]\n");
161 if (show & show_mask) {
162 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
164 "problem incrementing symbol count, skipping event\n");
174 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
176 struct thread *thread;
177 struct map *map = map__new(&event->mmap, NULL, 0);
179 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
181 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
182 (void *)(offset + head),
183 (void *)(long)(event->header.size),
185 (void *)(long)event->mmap.start,
186 (void *)(long)event->mmap.len,
187 (void *)(long)event->mmap.pgoff,
188 event->mmap.filename);
190 if (thread == NULL || map == NULL) {
191 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
195 thread__insert_map(thread, map);
202 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
204 struct thread *thread;
206 thread = threads__findnew(event->comm.pid, &threads, &last_match);
207 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
208 (void *)(offset + head),
209 (void *)(long)(event->header.size),
210 event->comm.comm, event->comm.pid);
212 if (thread == NULL ||
213 thread__set_comm(thread, event->comm.comm)) {
214 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
223 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
225 struct thread *thread;
226 struct thread *parent;
228 thread = threads__findnew(event->fork.pid, &threads, &last_match);
229 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
230 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
231 (void *)(offset + head),
232 (void *)(long)(event->header.size),
233 event->fork.pid, event->fork.ppid);
236 * A thread clone will have the same PID for both
239 if (thread == parent)
242 if (!thread || !parent || thread__fork(thread, parent)) {
243 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
252 process_event(event_t *event, unsigned long offset, unsigned long head)
254 switch (event->header.type) {
255 case PERF_RECORD_SAMPLE:
256 return process_sample_event(event, offset, head);
258 case PERF_RECORD_MMAP:
259 return process_mmap_event(event, offset, head);
261 case PERF_RECORD_COMM:
262 return process_comm_event(event, offset, head);
264 case PERF_RECORD_FORK:
265 return process_fork_event(event, offset, head);
267 * We dont process them right now but they are fine:
270 case PERF_RECORD_THROTTLE:
271 case PERF_RECORD_UNTHROTTLE:
282 parse_line(FILE *file, struct symbol *sym, u64 len)
284 char *line = NULL, *tmp, *tmp2;
285 static const char *prev_line;
286 static const char *prev_color;
293 if (getline(&line, &line_len, file) < 0)
298 c = strchr(line, '\n');
307 * Strip leading spaces:
318 * Parse hexa addresses followed by ':'
320 line_ip = strtoull(tmp, &tmp2, 16);
326 const char *path = NULL;
327 unsigned int hits = 0;
328 double percent = 0.0;
330 struct sym_ext *sym_ext = sym->priv;
332 offset = line_ip - sym->start;
334 hits = sym->hist[offset];
336 if (offset < len && sym_ext) {
337 path = sym_ext[offset].path;
338 percent = sym_ext[offset].percent;
339 } else if (sym->hist_sum)
340 percent = 100.0 * hits / sym->hist_sum;
342 color = get_percent_color(percent);
345 * Also color the filename and line if needed, with
346 * the same color than the percentage. Don't print it
347 * twice for close colored ip with the same filename:line
350 if (!prev_line || strcmp(prev_line, path)
351 || color != prev_color) {
352 color_fprintf(stdout, color, " %s", path);
358 color_fprintf(stdout, color, " %7.2f", percent);
360 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
365 printf(" : %s\n", line);
371 static struct rb_root root_sym_ext;
373 static void insert_source_line(struct sym_ext *sym_ext)
375 struct sym_ext *iter;
376 struct rb_node **p = &root_sym_ext.rb_node;
377 struct rb_node *parent = NULL;
381 iter = rb_entry(parent, struct sym_ext, node);
383 if (sym_ext->percent > iter->percent)
389 rb_link_node(&sym_ext->node, parent, p);
390 rb_insert_color(&sym_ext->node, &root_sym_ext);
393 static void free_source_line(struct symbol *sym, int len)
395 struct sym_ext *sym_ext = sym->priv;
401 for (i = 0; i < len; i++)
402 free(sym_ext[i].path);
406 root_sym_ext = RB_ROOT;
409 /* Get the filename:line for the colored entries */
411 get_source_line(struct symbol *sym, int len, const char *filename)
414 char cmd[PATH_MAX * 2];
415 struct sym_ext *sym_ext;
420 sym->priv = calloc(len, sizeof(struct sym_ext));
426 for (i = 0; i < len; i++) {
432 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
433 if (sym_ext[i].percent <= 0.5)
436 offset = sym->start + i;
437 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
438 fp = popen(cmd, "r");
442 if (getline(&path, &line_len, fp) < 0 || !line_len)
445 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
446 if (!sym_ext[i].path)
449 strcpy(sym_ext[i].path, path);
450 insert_source_line(&sym_ext[i]);
457 static void print_summary(const char *filename)
459 struct sym_ext *sym_ext;
460 struct rb_node *node;
462 printf("\nSorted summary for file %s\n", filename);
463 printf("----------------------------------------------\n\n");
465 if (RB_EMPTY_ROOT(&root_sym_ext)) {
466 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
470 node = rb_first(&root_sym_ext);
476 sym_ext = rb_entry(node, struct sym_ext, node);
477 percent = sym_ext->percent;
478 color = get_percent_color(percent);
479 path = sym_ext->path;
481 color_fprintf(stdout, color, " %7.2f %s", percent, path);
482 node = rb_next(node);
486 static void annotate_sym(struct dso *dso, struct symbol *sym)
488 const char *filename = dso->long_name, *d_filename;
490 char command[PATH_MAX*2];
497 d_filename = filename;
499 d_filename = basename(filename);
501 len = sym->end - sym->start;
504 get_source_line(sym, len, filename);
505 print_summary(filename);
508 printf("\n\n------------------------------------------------\n");
509 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
510 printf("------------------------------------------------\n");
513 printf("annotating [%p] %30s : [%p] %30s\n",
514 dso, dso->long_name, sym, sym->name);
516 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
517 sym->start, sym->end, filename, filename);
520 printf("doing: %s\n", command);
522 file = popen(command, "r");
526 while (!feof(file)) {
527 if (parse_line(file, sym, len) < 0)
533 free_source_line(sym, len);
536 static void find_annotations(void)
542 list_for_each_entry(dso, &dsos, node) {
544 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
545 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
548 annotate_sym(dso, sym);
555 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
558 static int __cmd_annotate(void)
560 int ret, rc = EXIT_FAILURE;
561 unsigned long offset = 0;
562 unsigned long head = 0;
563 struct stat input_stat;
568 register_idle_thread(&threads, &last_match);
570 input = open(input_name, O_RDONLY);
572 perror("failed to open file");
576 ret = fstat(input, &input_stat);
578 perror("failed to stat file");
582 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
583 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
587 if (!input_stat.st_size) {
588 fprintf(stderr, "zero-sized file, nothing to do!\n");
592 if (load_kernel() < 0) {
593 perror("failed to load kernel symbols");
598 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
599 MAP_SHARED, input, offset);
600 if (buf == MAP_FAILED) {
601 perror("failed to mmap file");
606 event = (event_t *)(buf + head);
608 size = event->header.size;
612 if (head + event->header.size >= page_size * mmap_window) {
613 unsigned long shift = page_size * (head / page_size);
616 munmap_ret = munmap(buf, page_size * mmap_window);
617 assert(munmap_ret == 0);
624 size = event->header.size;
626 dump_printf("%p [%p]: event: %d\n",
627 (void *)(offset + head),
628 (void *)(long)event->header.size,
631 if (!size || process_event(event, offset, head) < 0) {
633 dump_printf("%p [%p]: skipping unknown header type: %d\n",
634 (void *)(offset + head),
635 (void *)(long)(event->header.size),
641 * assume we lost track of the stream, check alignment, and
642 * increment a single u64 in the hope to catch on again 'soon'.
645 if (unlikely(head & 7))
653 if (offset + head < (unsigned long)input_stat.st_size)
659 dump_printf(" IP events: %10ld\n", total);
660 dump_printf(" mmap events: %10ld\n", total_mmap);
661 dump_printf(" comm events: %10ld\n", total_comm);
662 dump_printf(" fork events: %10ld\n", total_fork);
663 dump_printf(" unknown events: %10ld\n", total_unknown);
669 threads__fprintf(stdout, &threads);
672 dsos__fprintf(stdout);
675 output__resort(total);
682 static const char * const annotate_usage[] = {
683 "perf annotate [<options>] <command>",
687 static const struct option options[] = {
688 OPT_STRING('i', "input", &input_name, "file",
690 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
691 "symbol to annotate"),
692 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
693 OPT_BOOLEAN('v', "verbose", &verbose,
694 "be more verbose (show symbol address, etc)"),
695 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
696 "dump raw trace in ASCII"),
697 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
698 OPT_BOOLEAN('m', "modules", &modules,
699 "load module symbols - WARNING: use only with -k and LIVE kernel"),
700 OPT_BOOLEAN('l', "print-line", &print_line,
701 "print matching source lines (may be slow)"),
702 OPT_BOOLEAN('P', "full-paths", &full_paths,
703 "Don't shorten the displayed pathnames"),
707 static void setup_sorting(void)
709 char *tmp, *tok, *str = strdup(sort_order);
711 for (tok = strtok_r(str, ", ", &tmp);
712 tok; tok = strtok_r(NULL, ", ", &tmp)) {
713 if (sort_dimension__add(tok) < 0) {
714 error("Unknown --sort key: `%s'", tok);
715 usage_with_options(annotate_usage, options);
722 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
726 page_size = getpagesize();
728 argc = parse_options(argc, argv, options, annotate_usage, 0);
734 * Special case: if there's an argument left then assume tha
735 * it's a symbol filter:
738 usage_with_options(annotate_usage, options);
740 sym_hist_filter = argv[0];
743 if (!sym_hist_filter)
744 usage_with_options(annotate_usage, options);
748 if (field_sep && *field_sep == '.') {
749 fputs("'.' is the only non valid --field-separator argument\n",
754 return __cmd_annotate();