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"
19 #include "util/debug.h"
21 #include "util/event.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/session.h"
29 static char const *input_name = "perf.data";
33 static bool full_paths;
35 static bool print_line;
49 struct sym_hist *hist;
53 static const char *sym_hist_filter;
55 static int sym__alloc_hist(struct symbol *self)
57 struct sym_priv *priv = symbol__priv(self);
58 const int size = (sizeof(*priv->hist) +
59 (self->end - self->start) * sizeof(u64));
61 priv->hist = zalloc(size);
62 return priv->hist == NULL ? -1 : 0;
66 * collect histogram counts
68 static int annotate__hist_hit(struct hist_entry *he, u64 ip)
70 unsigned int sym_size, offset;
71 struct symbol *sym = he->ms.sym;
72 struct sym_priv *priv;
77 if (!sym || !he->ms.map)
80 priv = symbol__priv(sym);
81 if (priv->hist == NULL && sym__alloc_hist(sym) < 0)
84 sym_size = sym->end - sym->start;
85 offset = ip - sym->start;
87 pr_debug3("%s: ip=%#Lx\n", __func__, he->ms.map->unmap_ip(he->ms.map, ip));
89 if (offset >= sym_size)
96 pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->ms.sym->start,
97 he->ms.sym->name, ip, ip - he->ms.sym->start, h->ip[offset]);
101 static int perf_session__add_hist_entry(struct perf_session *self,
102 struct addr_location *al, u64 count)
105 struct hist_entry *he;
107 if (sym_hist_filter != NULL &&
108 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
109 /* We're only interested in a symbol named sym_hist_filter */
110 if (al->sym != NULL) {
111 rb_erase(&al->sym->rb_node,
112 &al->map->dso->symbols[al->map->type]);
113 symbol__delete(al->sym);
118 he = __perf_session__add_hist_entry(&self->hists, al, NULL, count, &hit);
122 return annotate__hist_hit(he, al->addr);
125 static int process_sample_event(event_t *event, struct perf_session *session)
127 struct addr_location al;
129 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
130 event->ip.pid, event->ip.ip);
132 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
133 pr_warning("problem processing %d event, skipping it.\n",
138 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) {
139 pr_warning("problem incrementing symbol count, "
147 struct objdump_line {
148 struct list_head node;
153 static struct objdump_line *objdump_line__new(s64 offset, char *line)
155 struct objdump_line *self = malloc(sizeof(*self));
158 self->offset = offset;
165 static void objdump_line__free(struct objdump_line *self)
171 static void objdump__add_line(struct list_head *head, struct objdump_line *line)
173 list_add_tail(&line->node, head);
176 static struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
177 struct objdump_line *pos)
179 list_for_each_entry_continue(pos, head, node)
180 if (pos->offset >= 0)
186 static int parse_line(FILE *file, struct hist_entry *he,
187 struct list_head *head)
189 struct symbol *sym = he->ms.sym;
190 struct objdump_line *objdump_line;
191 char *line = NULL, *tmp, *tmp2;
193 s64 line_ip, offset = -1;
196 if (getline(&line, &line_len, file) < 0)
202 c = strchr(line, '\n');
209 * Strip leading spaces:
220 * Parse hexa addresses followed by ':'
222 line_ip = strtoull(tmp, &tmp2, 16);
228 u64 start = map__rip_2objdump(he->ms.map, sym->start);
229 offset = line_ip - start;
232 objdump_line = objdump_line__new(offset, line);
233 if (objdump_line == NULL) {
237 objdump__add_line(head, objdump_line);
242 static int objdump_line__print(struct objdump_line *self,
243 struct list_head *head,
244 struct hist_entry *he, u64 len)
246 struct symbol *sym = he->ms.sym;
247 static const char *prev_line;
248 static const char *prev_color;
250 if (self->offset != -1) {
251 const char *path = NULL;
252 unsigned int hits = 0;
253 double percent = 0.0;
255 struct sym_priv *priv = symbol__priv(sym);
256 struct sym_ext *sym_ext = priv->ext;
257 struct sym_hist *h = priv->hist;
258 s64 offset = self->offset;
259 struct objdump_line *next = objdump__get_next_ip_line(head, self);
261 while (offset < (s64)len &&
262 (next == NULL || offset < next->offset)) {
265 path = sym_ext[offset].path;
266 percent += sym_ext[offset].percent;
268 hits += h->ip[offset];
273 if (sym_ext == NULL && h->sum)
274 percent = 100.0 * hits / h->sum;
276 color = get_percent_color(percent);
279 * Also color the filename and line if needed, with
280 * the same color than the percentage. Don't print it
281 * twice for close colored ip with the same filename:line
284 if (!prev_line || strcmp(prev_line, path)
285 || color != prev_color) {
286 color_fprintf(stdout, color, " %s", path);
292 color_fprintf(stdout, color, " %7.2f", percent);
294 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
299 printf(" : %s\n", self->line);
305 static struct rb_root root_sym_ext;
307 static void insert_source_line(struct sym_ext *sym_ext)
309 struct sym_ext *iter;
310 struct rb_node **p = &root_sym_ext.rb_node;
311 struct rb_node *parent = NULL;
315 iter = rb_entry(parent, struct sym_ext, node);
317 if (sym_ext->percent > iter->percent)
323 rb_link_node(&sym_ext->node, parent, p);
324 rb_insert_color(&sym_ext->node, &root_sym_ext);
327 static void free_source_line(struct hist_entry *he, int len)
329 struct sym_priv *priv = symbol__priv(he->ms.sym);
330 struct sym_ext *sym_ext = priv->ext;
336 for (i = 0; i < len; i++)
337 free(sym_ext[i].path);
341 root_sym_ext = RB_ROOT;
344 /* Get the filename:line for the colored entries */
346 get_source_line(struct hist_entry *he, int len, const char *filename)
348 struct symbol *sym = he->ms.sym;
351 char cmd[PATH_MAX * 2];
352 struct sym_ext *sym_ext;
353 struct sym_priv *priv = symbol__priv(sym);
354 struct sym_hist *h = priv->hist;
359 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
363 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
365 for (i = 0; i < len; i++) {
371 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
372 if (sym_ext[i].percent <= 0.5)
376 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
377 fp = popen(cmd, "r");
381 if (getline(&path, &line_len, fp) < 0 || !line_len)
384 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
385 if (!sym_ext[i].path)
388 strcpy(sym_ext[i].path, path);
389 insert_source_line(&sym_ext[i]);
396 static void print_summary(const char *filename)
398 struct sym_ext *sym_ext;
399 struct rb_node *node;
401 printf("\nSorted summary for file %s\n", filename);
402 printf("----------------------------------------------\n\n");
404 if (RB_EMPTY_ROOT(&root_sym_ext)) {
405 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
409 node = rb_first(&root_sym_ext);
415 sym_ext = rb_entry(node, struct sym_ext, node);
416 percent = sym_ext->percent;
417 color = get_percent_color(percent);
418 path = sym_ext->path;
420 color_fprintf(stdout, color, " %7.2f %s", percent, path);
421 node = rb_next(node);
425 static void hist_entry__print_hits(struct hist_entry *self)
427 struct symbol *sym = self->ms.sym;
428 struct sym_priv *priv = symbol__priv(sym);
429 struct sym_hist *h = priv->hist;
430 u64 len = sym->end - sym->start, offset;
432 for (offset = 0; offset < len; ++offset)
433 if (h->ip[offset] != 0)
434 printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
435 sym->start + offset, h->ip[offset]);
436 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
439 static void annotate_sym(struct hist_entry *he)
441 struct map *map = he->ms.map;
442 struct dso *dso = map->dso;
443 struct symbol *sym = he->ms.sym;
444 const char *filename = dso->long_name, *d_filename;
446 char command[PATH_MAX*2];
449 struct objdump_line *pos, *n;
454 if (dso->origin == DSO__ORIG_KERNEL) {
455 if (dso->annotate_warned)
457 dso->annotate_warned = 1;
458 pr_err("Can't annotate %s: No vmlinux file was found in the "
459 "path:\n", sym->name);
460 vmlinux_path__fprintf(stderr);
464 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
465 filename, sym->name, map->unmap_ip(map, sym->start),
466 map->unmap_ip(map, sym->end));
469 d_filename = filename;
471 d_filename = basename(filename);
473 len = sym->end - sym->start;
476 get_source_line(he, len, filename);
477 print_summary(filename);
480 printf("\n\n------------------------------------------------\n");
481 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
482 printf("------------------------------------------------\n");
485 printf("annotating [%p] %30s : [%p] %30s\n",
486 dso, dso->long_name, sym, sym->name);
488 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
489 map__rip_2objdump(map, sym->start),
490 map__rip_2objdump(map, sym->end),
494 printf("doing: %s\n", command);
496 file = popen(command, "r");
500 while (!feof(file)) {
501 if (parse_line(file, he, &head) < 0)
508 hist_entry__print_hits(he);
510 list_for_each_entry_safe(pos, n, &head, node) {
511 objdump_line__print(pos, &head, he, len);
512 list_del(&pos->node);
513 objdump_line__free(pos);
517 free_source_line(he, len);
520 static void perf_session__find_annotations(struct perf_session *self)
524 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
525 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
526 struct sym_priv *priv;
528 if (he->ms.sym == NULL)
531 priv = symbol__priv(he->ms.sym);
532 if (priv->hist == NULL)
537 * Since we have a hist_entry per IP for the same symbol, free
538 * he->ms.sym->hist to signal we already processed this symbol.
545 static struct perf_event_ops event_ops = {
546 .sample = process_sample_event,
547 .mmap = event__process_mmap,
548 .comm = event__process_comm,
549 .fork = event__process_task,
552 static int __cmd_annotate(void)
555 struct perf_session *session;
557 session = perf_session__new(input_name, O_RDONLY, force, false);
561 ret = perf_session__process_events(session, &event_ops);
566 event__print_totals();
571 perf_session__fprintf(session, stdout);
574 perf_session__fprintf_dsos(session, stdout);
576 perf_session__collapse_resort(&session->hists);
577 perf_session__output_resort(&session->hists, session->event_total[0]);
578 perf_session__find_annotations(session);
580 perf_session__delete(session);
585 static const char * const annotate_usage[] = {
586 "perf annotate [<options>] <command>",
590 static const struct option options[] = {
591 OPT_STRING('i', "input", &input_name, "file",
593 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
594 "only consider symbols in these dsos"),
595 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
596 "symbol to annotate"),
597 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
598 OPT_INCR('v', "verbose", &verbose,
599 "be more verbose (show symbol address, etc)"),
600 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
601 "dump raw trace in ASCII"),
602 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
603 "file", "vmlinux pathname"),
604 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
605 "load module symbols - WARNING: use only with -k and LIVE kernel"),
606 OPT_BOOLEAN('l', "print-line", &print_line,
607 "print matching source lines (may be slow)"),
608 OPT_BOOLEAN('P', "full-paths", &full_paths,
609 "Don't shorten the displayed pathnames"),
613 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
615 argc = parse_options(argc, argv, options, annotate_usage, 0);
617 symbol_conf.priv_size = sizeof(struct sym_priv);
618 symbol_conf.try_vmlinux_path = true;
620 if (symbol__init() < 0)
623 setup_sorting(annotate_usage, options);
627 * Special case: if there's an argument left then assume tha
628 * it's a symbol filter:
631 usage_with_options(annotate_usage, options);
633 sym_hist_filter = argv[0];
638 if (field_sep && *field_sep == '.') {
639 pr_err("'.' is the only non valid --field-separator argument\n");
643 return __cmd_annotate();