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/event.h"
23 #include "util/parse-options.h"
24 #include "util/parse-events.h"
25 #include "util/thread.h"
26 #include "util/sort.h"
27 #include "util/hist.h"
28 #include "util/session.h"
29 #include "util/data_map.h"
31 static char const *input_name = "perf.data";
35 static int full_paths;
37 static int print_line;
51 struct sym_hist *hist;
55 static struct symbol_conf symbol_conf = {
56 .priv_size = sizeof(struct sym_priv),
57 .try_vmlinux_path = true,
60 static const char *sym_hist_filter;
62 static int symbol_filter(struct map *map __used, struct symbol *sym)
64 if (sym_hist_filter == NULL ||
65 strcmp(sym->name, sym_hist_filter) == 0) {
66 struct sym_priv *priv = symbol__priv(sym);
67 const int size = (sizeof(*priv->hist) +
68 (sym->end - sym->start) * sizeof(u64));
70 priv->hist = malloc(size);
72 memset(priv->hist, 0, size);
76 * FIXME: We should really filter it out, as we don't want to go thru symbols
77 * we're not interested, and if a DSO ends up with no symbols, delete it too,
78 * but right now the kernel loading routines in symbol.c bail out if no symbols
79 * are found, fix it later.
85 * collect histogram counts
87 static void hist_hit(struct hist_entry *he, u64 ip)
89 unsigned int sym_size, offset;
90 struct symbol *sym = he->sym;
91 struct sym_priv *priv;
99 priv = symbol__priv(sym);
103 sym_size = sym->end - sym->start;
104 offset = ip - sym->start;
107 fprintf(stderr, "%s: ip=%Lx\n", __func__,
108 he->map->unmap_ip(he->map, ip));
110 if (offset >= sym_size)
118 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
119 (void *)(unsigned long)he->sym->start,
121 (void *)(unsigned long)ip, ip - he->sym->start,
125 static int hist_entry__add(struct addr_location *al, u64 count)
128 struct hist_entry *he = __hist_entry__add(al, NULL, count, &hit);
131 hist_hit(he, al->addr);
135 static int process_sample_event(event_t *event)
137 struct addr_location al;
139 dump_printf("(IP, %d): %d: %p\n", event->header.misc,
140 event->ip.pid, (void *)(long)event->ip.ip);
142 if (event__preprocess_sample(event, &al, symbol_filter) < 0) {
143 fprintf(stderr, "problem processing %d event, skipping it.\n",
148 if (hist_entry__add(&al, 1)) {
149 fprintf(stderr, "problem incrementing symbol count, "
157 static int parse_line(FILE *file, struct hist_entry *he, u64 len)
159 struct symbol *sym = he->sym;
160 char *line = NULL, *tmp, *tmp2;
161 static const char *prev_line;
162 static const char *prev_color;
170 if (getline(&line, &line_len, file) < 0)
175 c = strchr(line, '\n');
184 * Strip leading spaces:
195 * Parse hexa addresses followed by ':'
197 line_ip = strtoull(tmp, &tmp2, 16);
202 start = he->map->unmap_ip(he->map, sym->start);
205 const char *path = NULL;
206 unsigned int hits = 0;
207 double percent = 0.0;
209 struct sym_priv *priv = symbol__priv(sym);
210 struct sym_ext *sym_ext = priv->ext;
211 struct sym_hist *h = priv->hist;
213 offset = line_ip - start;
215 hits = h->ip[offset];
217 if (offset < len && sym_ext) {
218 path = sym_ext[offset].path;
219 percent = sym_ext[offset].percent;
221 percent = 100.0 * hits / h->sum;
223 color = get_percent_color(percent);
226 * Also color the filename and line if needed, with
227 * the same color than the percentage. Don't print it
228 * twice for close colored ip with the same filename:line
231 if (!prev_line || strcmp(prev_line, path)
232 || color != prev_color) {
233 color_fprintf(stdout, color, " %s", path);
239 color_fprintf(stdout, color, " %7.2f", percent);
241 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
246 printf(" : %s\n", line);
252 static struct rb_root root_sym_ext;
254 static void insert_source_line(struct sym_ext *sym_ext)
256 struct sym_ext *iter;
257 struct rb_node **p = &root_sym_ext.rb_node;
258 struct rb_node *parent = NULL;
262 iter = rb_entry(parent, struct sym_ext, node);
264 if (sym_ext->percent > iter->percent)
270 rb_link_node(&sym_ext->node, parent, p);
271 rb_insert_color(&sym_ext->node, &root_sym_ext);
274 static void free_source_line(struct hist_entry *he, int len)
276 struct sym_priv *priv = symbol__priv(he->sym);
277 struct sym_ext *sym_ext = priv->ext;
283 for (i = 0; i < len; i++)
284 free(sym_ext[i].path);
288 root_sym_ext = RB_ROOT;
291 /* Get the filename:line for the colored entries */
293 get_source_line(struct hist_entry *he, int len, const char *filename)
295 struct symbol *sym = he->sym;
298 char cmd[PATH_MAX * 2];
299 struct sym_ext *sym_ext;
300 struct sym_priv *priv = symbol__priv(sym);
301 struct sym_hist *h = priv->hist;
306 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
310 start = he->map->unmap_ip(he->map, sym->start);
312 for (i = 0; i < len; i++) {
318 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
319 if (sym_ext[i].percent <= 0.5)
323 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
324 fp = popen(cmd, "r");
328 if (getline(&path, &line_len, fp) < 0 || !line_len)
331 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
332 if (!sym_ext[i].path)
335 strcpy(sym_ext[i].path, path);
336 insert_source_line(&sym_ext[i]);
343 static void print_summary(const char *filename)
345 struct sym_ext *sym_ext;
346 struct rb_node *node;
348 printf("\nSorted summary for file %s\n", filename);
349 printf("----------------------------------------------\n\n");
351 if (RB_EMPTY_ROOT(&root_sym_ext)) {
352 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
356 node = rb_first(&root_sym_ext);
362 sym_ext = rb_entry(node, struct sym_ext, node);
363 percent = sym_ext->percent;
364 color = get_percent_color(percent);
365 path = sym_ext->path;
367 color_fprintf(stdout, color, " %7.2f %s", percent, path);
368 node = rb_next(node);
372 static void annotate_sym(struct hist_entry *he)
374 struct map *map = he->map;
375 struct dso *dso = map->dso;
376 struct symbol *sym = he->sym;
377 const char *filename = dso->long_name, *d_filename;
379 char command[PATH_MAX*2];
386 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
387 __func__, filename, sym->name,
388 map->unmap_ip(map, sym->start),
389 map->unmap_ip(map, sym->end));
392 d_filename = filename;
394 d_filename = basename(filename);
396 len = sym->end - sym->start;
399 get_source_line(he, len, filename);
400 print_summary(filename);
403 printf("\n\n------------------------------------------------\n");
404 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
405 printf("------------------------------------------------\n");
408 printf("annotating [%p] %30s : [%p] %30s\n",
409 dso, dso->long_name, sym, sym->name);
411 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
412 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
416 printf("doing: %s\n", command);
418 file = popen(command, "r");
422 while (!feof(file)) {
423 if (parse_line(file, he, len) < 0)
429 free_source_line(he, len);
432 static void find_annotations(void)
436 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
437 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
438 struct sym_priv *priv;
443 priv = symbol__priv(he->sym);
444 if (priv->hist == NULL)
449 * Since we have a hist_entry per IP for the same symbol, free
450 * he->sym->hist to signal we already processed this symbol.
457 static struct perf_file_handler file_handler = {
458 .process_sample_event = process_sample_event,
459 .process_mmap_event = event__process_mmap,
460 .process_comm_event = event__process_comm,
461 .process_fork_event = event__process_task,
464 static int __cmd_annotate(void)
466 struct perf_session *session = perf_session__new(input_name, O_RDONLY, force);
473 idle = register_idle_thread();
474 register_perf_file_handler(&file_handler);
476 ret = perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
481 event__print_totals();
486 threads__fprintf(stdout);
489 dsos__fprintf(stdout);
492 output__resort(event__total[0]);
496 perf_session__delete(session);
501 static const char * const annotate_usage[] = {
502 "perf annotate [<options>] <command>",
506 static const struct option options[] = {
507 OPT_STRING('i', "input", &input_name, "file",
509 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
510 "symbol to annotate"),
511 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
512 OPT_BOOLEAN('v', "verbose", &verbose,
513 "be more verbose (show symbol address, etc)"),
514 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
515 "dump raw trace in ASCII"),
516 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
517 "file", "vmlinux pathname"),
518 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
519 "load module symbols - WARNING: use only with -k and LIVE kernel"),
520 OPT_BOOLEAN('l', "print-line", &print_line,
521 "print matching source lines (may be slow)"),
522 OPT_BOOLEAN('P', "full-paths", &full_paths,
523 "Don't shorten the displayed pathnames"),
527 static void setup_sorting(void)
529 char *tmp, *tok, *str = strdup(sort_order);
531 for (tok = strtok_r(str, ", ", &tmp);
532 tok; tok = strtok_r(NULL, ", ", &tmp)) {
533 if (sort_dimension__add(tok) < 0) {
534 error("Unknown --sort key: `%s'", tok);
535 usage_with_options(annotate_usage, options);
542 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
544 if (symbol__init(&symbol_conf) < 0)
547 argc = parse_options(argc, argv, options, annotate_usage, 0);
553 * Special case: if there's an argument left then assume tha
554 * it's a symbol filter:
557 usage_with_options(annotate_usage, options);
559 sym_hist_filter = argv[0];
564 if (field_sep && *field_sep == '.') {
565 fputs("'.' is the only non valid --field-separator argument\n",
570 return __cmd_annotate();