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"
27 #include "util/process_events.h"
29 static char const *input_name = "perf.data";
34 static int full_paths;
36 static int print_line;
37 static bool use_modules;
39 static unsigned long page_size;
40 static unsigned long mmap_window = 32;
41 const char *vmlinux_name;
55 struct sym_hist *hist;
59 static const char *sym_hist_filter;
61 static int symbol_filter(struct map *map __used, struct symbol *sym)
63 if (sym_hist_filter == NULL ||
64 strcmp(sym->name, sym_hist_filter) == 0) {
65 struct sym_priv *priv = symbol__priv(sym);
66 const int size = (sizeof(*priv->hist) +
67 (sym->end - sym->start) * sizeof(u64));
69 priv->hist = malloc(size);
71 memset(priv->hist, 0, size);
75 * FIXME: We should really filter it out, as we don't want to go thru symbols
76 * we're not interested, and if a DSO ends up with no symbols, delete it too,
77 * but right now the kernel loading routines in symbol.c bail out if no symbols
78 * are found, fix it later.
84 * collect histogram counts
86 static void hist_hit(struct hist_entry *he, u64 ip)
88 unsigned int sym_size, offset;
89 struct symbol *sym = he->sym;
90 struct sym_priv *priv;
98 priv = symbol__priv(sym);
102 sym_size = sym->end - sym->start;
103 offset = ip - sym->start;
106 fprintf(stderr, "%s: ip=%Lx\n", __func__,
107 he->map->unmap_ip(he->map, ip));
109 if (offset >= sym_size)
117 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
118 (void *)(unsigned long)he->sym->start,
120 (void *)(unsigned long)ip, ip - he->sym->start,
124 static int hist_entry__add(struct thread *thread, struct map *map,
125 struct symbol *sym, u64 ip, u64 count, char level)
128 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
137 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
140 u64 ip = event->ip.ip;
141 struct map *map = NULL;
142 struct symbol *sym = NULL;
143 struct thread *thread = threads__findnew(event->ip.pid);
145 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
146 (void *)(offset + head),
147 (void *)(long)(event->header.size),
152 if (thread == NULL) {
153 fprintf(stderr, "problem processing %d event, skipping it.\n",
158 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
160 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
162 sym = kernel_maps__find_symbol(ip, &map, symbol_filter);
163 dump_printf(" ...... dso: %s\n",
164 map ? map->dso->long_name : "<not found>");
165 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
167 map = thread__find_map(thread, ip);
170 ip = map->map_ip(map, ip);
171 sym = map__find_symbol(map, ip, symbol_filter);
174 * If this is outside of all known maps,
175 * and is a negative address, try to look it
176 * up in the kernel dso, as it might be a
177 * vsyscall or vdso (which executes in user-mode).
179 * XXX This is nasty, we should have a symbol list in
180 * the "[vdso]" dso, but for now lets use the old
181 * trick of looking in the whole kernel symbol list.
183 if ((long long)ip < 0) {
188 dump_printf(" ...... dso: %s\n",
189 map ? map->dso->long_name : "<not found>");
192 dump_printf(" ...... dso: [hypervisor]\n");
195 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
196 fprintf(stderr, "problem incrementing symbol count, "
206 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
208 struct thread *thread = threads__findnew(event->comm.pid);
210 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
211 (void *)(offset + head),
212 (void *)(long)(event->header.size),
213 event->comm.comm, event->comm.pid);
215 if (thread == NULL ||
216 thread__set_comm(thread, event->comm.comm)) {
217 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
226 process_event(event_t *event, unsigned long offset, unsigned long head)
228 switch (event->header.type) {
229 case PERF_RECORD_SAMPLE:
230 return process_sample_event(event, offset, head);
232 case PERF_RECORD_MMAP:
233 return process_mmap_event(event, offset, head);
235 case PERF_RECORD_COMM:
236 return process_comm_event(event, offset, head);
238 case PERF_RECORD_FORK:
239 return process_task_event(event, offset, head);
241 * We dont process them right now but they are fine:
244 case PERF_RECORD_THROTTLE:
245 case PERF_RECORD_UNTHROTTLE:
255 static int parse_line(FILE *file, struct hist_entry *he, u64 len)
257 struct symbol *sym = he->sym;
258 char *line = NULL, *tmp, *tmp2;
259 static const char *prev_line;
260 static const char *prev_color;
268 if (getline(&line, &line_len, file) < 0)
273 c = strchr(line, '\n');
282 * Strip leading spaces:
293 * Parse hexa addresses followed by ':'
295 line_ip = strtoull(tmp, &tmp2, 16);
300 start = he->map->unmap_ip(he->map, sym->start);
303 const char *path = NULL;
304 unsigned int hits = 0;
305 double percent = 0.0;
307 struct sym_priv *priv = symbol__priv(sym);
308 struct sym_ext *sym_ext = priv->ext;
309 struct sym_hist *h = priv->hist;
311 offset = line_ip - start;
313 hits = h->ip[offset];
315 if (offset < len && sym_ext) {
316 path = sym_ext[offset].path;
317 percent = sym_ext[offset].percent;
319 percent = 100.0 * hits / h->sum;
321 color = get_percent_color(percent);
324 * Also color the filename and line if needed, with
325 * the same color than the percentage. Don't print it
326 * twice for close colored ip with the same filename:line
329 if (!prev_line || strcmp(prev_line, path)
330 || color != prev_color) {
331 color_fprintf(stdout, color, " %s", path);
337 color_fprintf(stdout, color, " %7.2f", percent);
339 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
344 printf(" : %s\n", line);
350 static struct rb_root root_sym_ext;
352 static void insert_source_line(struct sym_ext *sym_ext)
354 struct sym_ext *iter;
355 struct rb_node **p = &root_sym_ext.rb_node;
356 struct rb_node *parent = NULL;
360 iter = rb_entry(parent, struct sym_ext, node);
362 if (sym_ext->percent > iter->percent)
368 rb_link_node(&sym_ext->node, parent, p);
369 rb_insert_color(&sym_ext->node, &root_sym_ext);
372 static void free_source_line(struct hist_entry *he, int len)
374 struct sym_priv *priv = symbol__priv(he->sym);
375 struct sym_ext *sym_ext = priv->ext;
381 for (i = 0; i < len; i++)
382 free(sym_ext[i].path);
386 root_sym_ext = RB_ROOT;
389 /* Get the filename:line for the colored entries */
391 get_source_line(struct hist_entry *he, int len, const char *filename)
393 struct symbol *sym = he->sym;
396 char cmd[PATH_MAX * 2];
397 struct sym_ext *sym_ext;
398 struct sym_priv *priv = symbol__priv(sym);
399 struct sym_hist *h = priv->hist;
404 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
408 start = he->map->unmap_ip(he->map, sym->start);
410 for (i = 0; i < len; i++) {
416 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
417 if (sym_ext[i].percent <= 0.5)
421 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
422 fp = popen(cmd, "r");
426 if (getline(&path, &line_len, fp) < 0 || !line_len)
429 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
430 if (!sym_ext[i].path)
433 strcpy(sym_ext[i].path, path);
434 insert_source_line(&sym_ext[i]);
441 static void print_summary(const char *filename)
443 struct sym_ext *sym_ext;
444 struct rb_node *node;
446 printf("\nSorted summary for file %s\n", filename);
447 printf("----------------------------------------------\n\n");
449 if (RB_EMPTY_ROOT(&root_sym_ext)) {
450 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
454 node = rb_first(&root_sym_ext);
460 sym_ext = rb_entry(node, struct sym_ext, node);
461 percent = sym_ext->percent;
462 color = get_percent_color(percent);
463 path = sym_ext->path;
465 color_fprintf(stdout, color, " %7.2f %s", percent, path);
466 node = rb_next(node);
470 static void annotate_sym(struct hist_entry *he)
472 struct map *map = he->map;
473 struct dso *dso = map->dso;
474 struct symbol *sym = he->sym;
475 const char *filename = dso->long_name, *d_filename;
477 char command[PATH_MAX*2];
484 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
485 __func__, filename, sym->name,
486 map->unmap_ip(map, sym->start),
487 map->unmap_ip(map, sym->end));
490 d_filename = filename;
492 d_filename = basename(filename);
494 len = sym->end - sym->start;
497 get_source_line(he, 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 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
514 printf("doing: %s\n", command);
516 file = popen(command, "r");
520 while (!feof(file)) {
521 if (parse_line(file, he, len) < 0)
527 free_source_line(he, len);
530 static void find_annotations(void)
534 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
535 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
536 struct sym_priv *priv;
541 priv = symbol__priv(he->sym);
542 if (priv->hist == NULL)
547 * Since we have a hist_entry per IP for the same symbol, free
548 * he->sym->hist to signal we already processed this symbol.
555 static int __cmd_annotate(void)
557 int ret, rc = EXIT_FAILURE;
558 unsigned long offset = 0;
559 unsigned long head = 0;
560 struct stat input_stat;
565 register_idle_thread();
567 input = open(input_name, O_RDONLY);
569 perror("failed to open file");
573 ret = fstat(input, &input_stat);
575 perror("failed to stat file");
579 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
580 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
584 if (!input_stat.st_size) {
585 fprintf(stderr, "zero-sized file, nothing to do!\n");
589 if (kernel_maps__init(vmlinux_name, true, use_modules) < 0) {
590 pr_err("failed to create kernel maps for symbol resolution\b");
595 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
596 MAP_SHARED, input, offset);
597 if (buf == MAP_FAILED) {
598 perror("failed to mmap file");
603 event = (event_t *)(buf + head);
605 size = event->header.size;
609 if (head + event->header.size >= page_size * mmap_window) {
610 unsigned long shift = page_size * (head / page_size);
613 munmap_ret = munmap(buf, page_size * mmap_window);
614 assert(munmap_ret == 0);
621 size = event->header.size;
623 dump_printf("%p [%p]: event: %d\n",
624 (void *)(offset + head),
625 (void *)(long)event->header.size,
628 if (!size || process_event(event, offset, head) < 0) {
630 dump_printf("%p [%p]: skipping unknown header type: %d\n",
631 (void *)(offset + head),
632 (void *)(long)(event->header.size),
638 * assume we lost track of the stream, check alignment, and
639 * increment a single u64 in the hope to catch on again 'soon'.
642 if (unlikely(head & 7))
650 if (offset + head < (unsigned long)input_stat.st_size)
656 dump_printf(" IP events: %10ld\n", total);
657 dump_printf(" mmap events: %10ld\n", total_mmap);
658 dump_printf(" comm events: %10ld\n", total_comm);
659 dump_printf(" fork events: %10ld\n", total_fork);
660 dump_printf(" unknown events: %10ld\n", total_unknown);
666 threads__fprintf(stdout);
669 dsos__fprintf(stdout);
672 output__resort(total);
679 static const char * const annotate_usage[] = {
680 "perf annotate [<options>] <command>",
684 static const struct option options[] = {
685 OPT_STRING('i', "input", &input_name, "file",
687 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
688 "symbol to annotate"),
689 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
690 OPT_BOOLEAN('v', "verbose", &verbose,
691 "be more verbose (show symbol address, etc)"),
692 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
693 "dump raw trace in ASCII"),
694 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
695 OPT_BOOLEAN('m', "modules", &use_modules,
696 "load module symbols - WARNING: use only with -k and LIVE kernel"),
697 OPT_BOOLEAN('l', "print-line", &print_line,
698 "print matching source lines (may be slow)"),
699 OPT_BOOLEAN('P', "full-paths", &full_paths,
700 "Don't shorten the displayed pathnames"),
704 static void setup_sorting(void)
706 char *tmp, *tok, *str = strdup(sort_order);
708 for (tok = strtok_r(str, ", ", &tmp);
709 tok; tok = strtok_r(NULL, ", ", &tmp)) {
710 if (sort_dimension__add(tok) < 0) {
711 error("Unknown --sort key: `%s'", tok);
712 usage_with_options(annotate_usage, options);
719 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
721 symbol__init(sizeof(struct sym_priv));
723 page_size = getpagesize();
725 argc = parse_options(argc, argv, options, annotate_usage, 0);
731 * Special case: if there's an argument left then assume tha
732 * it's a symbol filter:
735 usage_with_options(annotate_usage, options);
737 sym_hist_filter = argv[0];
742 if (field_sep && *field_sep == '.') {
743 fputs("'.' is the only non valid --field-separator argument\n",
748 return __cmd_annotate();