186470930SIngo Molnar /* 286470930SIngo Molnar * builtin-annotate.c 386470930SIngo Molnar * 486470930SIngo Molnar * Builtin annotate command: Analyze the perf.data input file, 586470930SIngo Molnar * look up and read DSOs and symbol information and display 686470930SIngo Molnar * a histogram of results, along various sorting keys. 786470930SIngo Molnar */ 886470930SIngo Molnar #include "builtin.h" 986470930SIngo Molnar 1086470930SIngo Molnar #include "util/util.h" 1186470930SIngo Molnar #include "util/color.h" 125da50258SArnaldo Carvalho de Melo #include <linux/list.h> 1386470930SIngo Molnar #include "util/cache.h" 1443cbcd8aSArnaldo Carvalho de Melo #include <linux/rbtree.h> 1586470930SIngo Molnar #include "util/symbol.h" 1686470930SIngo Molnar 1786470930SIngo Molnar #include "perf.h" 188f28827aSFrederic Weisbecker #include "util/debug.h" 1986470930SIngo Molnar 20e248de33SArnaldo Carvalho de Melo #include "util/evlist.h" 21e248de33SArnaldo Carvalho de Melo #include "util/evsel.h" 2278f7defeSArnaldo Carvalho de Melo #include "util/annotate.h" 2362daacb5SArnaldo Carvalho de Melo #include "util/event.h" 2486470930SIngo Molnar #include "util/parse-options.h" 2586470930SIngo Molnar #include "util/parse-events.h" 266baa0a5aSFrederic Weisbecker #include "util/thread.h" 27dd68ada2SJohn Kacur #include "util/sort.h" 283d1d07ecSJohn Kacur #include "util/hist.h" 2994c744b6SArnaldo Carvalho de Melo #include "util/session.h" 3086470930SIngo Molnar 315d67be97SAnton Blanchard #include <linux/bitmap.h> 325d67be97SAnton Blanchard 3386470930SIngo Molnar static char const *input_name = "perf.data"; 3486470930SIngo Molnar 358b9e74ebSArnaldo Carvalho de Melo static bool force, use_tui, use_stdio; 3686470930SIngo Molnar 37c0555642SIan Munsie static bool full_paths; 3842976487SMike Galbraith 39c0555642SIan Munsie static bool print_line; 40301406b9SFrederic Weisbecker 41e4204992SArnaldo Carvalho de Melo static const char *sym_hist_filter; 42e4204992SArnaldo Carvalho de Melo 435d67be97SAnton Blanchard static const char *cpu_list; 445d67be97SAnton Blanchard static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 455d67be97SAnton Blanchard 46e248de33SArnaldo Carvalho de Melo static int perf_evlist__add_sample(struct perf_evlist *evlist, 47e248de33SArnaldo Carvalho de Melo struct perf_sample *sample, 489e69c210SArnaldo Carvalho de Melo struct perf_evsel *evsel, 49e248de33SArnaldo Carvalho de Melo struct addr_location *al) 5086470930SIngo Molnar { 51628ada0cSArnaldo Carvalho de Melo struct hist_entry *he; 52e248de33SArnaldo Carvalho de Melo int ret; 53628ada0cSArnaldo Carvalho de Melo 54628ada0cSArnaldo Carvalho de Melo if (sym_hist_filter != NULL && 55628ada0cSArnaldo Carvalho de Melo (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) { 56628ada0cSArnaldo Carvalho de Melo /* We're only interested in a symbol named sym_hist_filter */ 57628ada0cSArnaldo Carvalho de Melo if (al->sym != NULL) { 58628ada0cSArnaldo Carvalho de Melo rb_erase(&al->sym->rb_node, 59628ada0cSArnaldo Carvalho de Melo &al->map->dso->symbols[al->map->type]); 60628ada0cSArnaldo Carvalho de Melo symbol__delete(al->sym); 61628ada0cSArnaldo Carvalho de Melo } 62628ada0cSArnaldo Carvalho de Melo return 0; 63628ada0cSArnaldo Carvalho de Melo } 64628ada0cSArnaldo Carvalho de Melo 65e248de33SArnaldo Carvalho de Melo he = __hists__add_entry(&evsel->hists, al, NULL, 1); 669735abf1SArnaldo Carvalho de Melo if (he == NULL) 6786470930SIngo Molnar return -ENOMEM; 68628ada0cSArnaldo Carvalho de Melo 69e248de33SArnaldo Carvalho de Melo ret = 0; 702f525d01SArnaldo Carvalho de Melo if (he->ms.sym != NULL) { 712f525d01SArnaldo Carvalho de Melo struct annotation *notes = symbol__annotation(he->ms.sym); 72ce6f4fabSArnaldo Carvalho de Melo if (notes->src == NULL && 73e248de33SArnaldo Carvalho de Melo symbol__alloc_hist(he->ms.sym, evlist->nr_entries) < 0) 742f525d01SArnaldo Carvalho de Melo return -ENOMEM; 752f525d01SArnaldo Carvalho de Melo 76e248de33SArnaldo Carvalho de Melo ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr); 772f525d01SArnaldo Carvalho de Melo } 782f525d01SArnaldo Carvalho de Melo 79e248de33SArnaldo Carvalho de Melo evsel->hists.stats.total_period += sample->period; 80e248de33SArnaldo Carvalho de Melo hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE); 81e248de33SArnaldo Carvalho de Melo return ret; 8286470930SIngo Molnar } 8386470930SIngo Molnar 848115d60cSArnaldo Carvalho de Melo static int process_sample_event(union perf_event *event, 858115d60cSArnaldo Carvalho de Melo struct perf_sample *sample, 869e69c210SArnaldo Carvalho de Melo struct perf_evsel *evsel, 87640c03ceSArnaldo Carvalho de Melo struct perf_session *session) 8886470930SIngo Molnar { 891ed091c4SArnaldo Carvalho de Melo struct addr_location al; 906baa0a5aSFrederic Weisbecker 91ce6f4fabSArnaldo Carvalho de Melo if (perf_event__preprocess_sample(event, session, &al, sample, 92ce6f4fabSArnaldo Carvalho de Melo symbol__annotate_init) < 0) { 9329a9f66dSArnaldo Carvalho de Melo pr_warning("problem processing %d event, skipping it.\n", 9486470930SIngo Molnar event->header.type); 9586470930SIngo Molnar return -1; 9686470930SIngo Molnar } 9786470930SIngo Molnar 985d67be97SAnton Blanchard if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) 995d67be97SAnton Blanchard return 0; 1005d67be97SAnton Blanchard 1019e69c210SArnaldo Carvalho de Melo if (!al.filtered && 1029e69c210SArnaldo Carvalho de Melo perf_evlist__add_sample(session->evlist, sample, evsel, &al)) { 10329a9f66dSArnaldo Carvalho de Melo pr_warning("problem incrementing symbol count, " 104ec218fc4SArnaldo Carvalho de Melo "skipping event\n"); 10586470930SIngo Molnar return -1; 10686470930SIngo Molnar } 10786470930SIngo Molnar 10886470930SIngo Molnar return 0; 10986470930SIngo Molnar } 11086470930SIngo Molnar 1112f525d01SArnaldo Carvalho de Melo static int hist_entry__tty_annotate(struct hist_entry *he, int evidx) 11286470930SIngo Molnar { 1132f525d01SArnaldo Carvalho de Melo return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx, 114d040bd36SArnaldo Carvalho de Melo print_line, full_paths, 0, 0); 11586470930SIngo Molnar } 11686470930SIngo Molnar 117*34958544SArnaldo Carvalho de Melo static void hists__find_annotations(struct hists *self, int evidx, 118*34958544SArnaldo Carvalho de Melo int nr_events) 11986470930SIngo Molnar { 120b50e003dSArnaldo Carvalho de Melo struct rb_node *nd = rb_first(&self->entries), *next; 12146e3e055SArnaldo Carvalho de Melo int key = KEY_RIGHT; 12286470930SIngo Molnar 12346e3e055SArnaldo Carvalho de Melo while (nd) { 124ed52ce2eSArnaldo Carvalho de Melo struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 12578f7defeSArnaldo Carvalho de Melo struct annotation *notes; 12686470930SIngo Molnar 12746e3e055SArnaldo Carvalho de Melo if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) 12846e3e055SArnaldo Carvalho de Melo goto find_next; 129e4204992SArnaldo Carvalho de Melo 13078f7defeSArnaldo Carvalho de Melo notes = symbol__annotation(he->ms.sym); 131ce6f4fabSArnaldo Carvalho de Melo if (notes->src == NULL) { 13246e3e055SArnaldo Carvalho de Melo find_next: 13346e3e055SArnaldo Carvalho de Melo if (key == KEY_LEFT) 13446e3e055SArnaldo Carvalho de Melo nd = rb_prev(nd); 13546e3e055SArnaldo Carvalho de Melo else 13646e3e055SArnaldo Carvalho de Melo nd = rb_next(nd); 137e4204992SArnaldo Carvalho de Melo continue; 13846e3e055SArnaldo Carvalho de Melo } 139e4204992SArnaldo Carvalho de Melo 14062e3436bSArnaldo Carvalho de Melo if (use_browser > 0) { 141*34958544SArnaldo Carvalho de Melo key = hist_entry__tui_annotate(he, evidx, nr_events, 142*34958544SArnaldo Carvalho de Melo NULL, NULL, 0); 14346e3e055SArnaldo Carvalho de Melo switch (key) { 14446e3e055SArnaldo Carvalho de Melo case KEY_RIGHT: 145b50e003dSArnaldo Carvalho de Melo next = rb_next(nd); 14646e3e055SArnaldo Carvalho de Melo break; 14746e3e055SArnaldo Carvalho de Melo case KEY_LEFT: 148b50e003dSArnaldo Carvalho de Melo next = rb_prev(nd); 14946e3e055SArnaldo Carvalho de Melo break; 150b50e003dSArnaldo Carvalho de Melo default: 151b50e003dSArnaldo Carvalho de Melo return; 15246e3e055SArnaldo Carvalho de Melo } 153b50e003dSArnaldo Carvalho de Melo 154b50e003dSArnaldo Carvalho de Melo if (next != NULL) 155b50e003dSArnaldo Carvalho de Melo nd = next; 15646e3e055SArnaldo Carvalho de Melo } else { 157e248de33SArnaldo Carvalho de Melo hist_entry__tty_annotate(he, evidx); 15846e3e055SArnaldo Carvalho de Melo nd = rb_next(nd); 159ed52ce2eSArnaldo Carvalho de Melo /* 16046e3e055SArnaldo Carvalho de Melo * Since we have a hist_entry per IP for the same 161ce6f4fabSArnaldo Carvalho de Melo * symbol, free he->ms.sym->src to signal we already 16246e3e055SArnaldo Carvalho de Melo * processed this symbol. 163ed52ce2eSArnaldo Carvalho de Melo */ 164ce6f4fabSArnaldo Carvalho de Melo free(notes->src); 165ce6f4fabSArnaldo Carvalho de Melo notes->src = NULL; 16686470930SIngo Molnar } 16786470930SIngo Molnar } 16846e3e055SArnaldo Carvalho de Melo } 16986470930SIngo Molnar 170301a0b02SArnaldo Carvalho de Melo static struct perf_event_ops event_ops = { 17155aa640fSArnaldo Carvalho de Melo .sample = process_sample_event, 1728115d60cSArnaldo Carvalho de Melo .mmap = perf_event__process_mmap, 1738115d60cSArnaldo Carvalho de Melo .comm = perf_event__process_comm, 1748115d60cSArnaldo Carvalho de Melo .fork = perf_event__process_task, 175eac23d1cSIan Munsie .ordered_samples = true, 176eac23d1cSIan Munsie .ordering_requires_timestamps = true, 177bab81b62SLi Zefan }; 178bab81b62SLi Zefan 17986470930SIngo Molnar static int __cmd_annotate(void) 18086470930SIngo Molnar { 181bab81b62SLi Zefan int ret; 18275be6cf4SArnaldo Carvalho de Melo struct perf_session *session; 183e248de33SArnaldo Carvalho de Melo struct perf_evsel *pos; 184e248de33SArnaldo Carvalho de Melo u64 total_nr_samples; 18586470930SIngo Molnar 18621ef97f0SIan Munsie session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops); 18794c744b6SArnaldo Carvalho de Melo if (session == NULL) 18894c744b6SArnaldo Carvalho de Melo return -ENOMEM; 18994c744b6SArnaldo Carvalho de Melo 1905d67be97SAnton Blanchard if (cpu_list) { 1915d67be97SAnton Blanchard ret = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap); 1925d67be97SAnton Blanchard if (ret) 1935d67be97SAnton Blanchard goto out_delete; 1945d67be97SAnton Blanchard } 1955d67be97SAnton Blanchard 196ec913369SArnaldo Carvalho de Melo ret = perf_session__process_events(session, &event_ops); 197bab81b62SLi Zefan if (ret) 19894c744b6SArnaldo Carvalho de Melo goto out_delete; 19986470930SIngo Molnar 20062daacb5SArnaldo Carvalho de Melo if (dump_trace) { 201c8446b9bSArnaldo Carvalho de Melo perf_session__fprintf_nr_events(session, stdout); 20294c744b6SArnaldo Carvalho de Melo goto out_delete; 20362daacb5SArnaldo Carvalho de Melo } 20486470930SIngo Molnar 205da21d1b5SArnaldo Carvalho de Melo if (verbose > 3) 206b3165f41SArnaldo Carvalho de Melo perf_session__fprintf(session, stdout); 20786470930SIngo Molnar 208da21d1b5SArnaldo Carvalho de Melo if (verbose > 2) 209cbf69680SArnaldo Carvalho de Melo perf_session__fprintf_dsos(session, stdout); 21086470930SIngo Molnar 211e248de33SArnaldo Carvalho de Melo total_nr_samples = 0; 212e248de33SArnaldo Carvalho de Melo list_for_each_entry(pos, &session->evlist->entries, node) { 213e248de33SArnaldo Carvalho de Melo struct hists *hists = &pos->hists; 214e248de33SArnaldo Carvalho de Melo u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE]; 21586470930SIngo Molnar 216e248de33SArnaldo Carvalho de Melo if (nr_samples > 0) { 217e248de33SArnaldo Carvalho de Melo total_nr_samples += nr_samples; 218e248de33SArnaldo Carvalho de Melo hists__collapse_resort(hists); 219e248de33SArnaldo Carvalho de Melo hists__output_resort(hists); 220*34958544SArnaldo Carvalho de Melo hists__find_annotations(hists, pos->idx, 221*34958544SArnaldo Carvalho de Melo session->evlist->nr_entries); 222e248de33SArnaldo Carvalho de Melo } 223e248de33SArnaldo Carvalho de Melo } 224e248de33SArnaldo Carvalho de Melo 225e248de33SArnaldo Carvalho de Melo if (total_nr_samples == 0) { 226e248de33SArnaldo Carvalho de Melo ui__warning("The %s file has no samples!\n", input_name); 227e248de33SArnaldo Carvalho de Melo goto out_delete; 228e248de33SArnaldo Carvalho de Melo } 229e248de33SArnaldo Carvalho de Melo out_delete: 230e248de33SArnaldo Carvalho de Melo /* 231e248de33SArnaldo Carvalho de Melo * Speed up the exit process, for large files this can 232e248de33SArnaldo Carvalho de Melo * take quite a while. 233e248de33SArnaldo Carvalho de Melo * 234e248de33SArnaldo Carvalho de Melo * XXX Enable this when using valgrind or if we ever 235e248de33SArnaldo Carvalho de Melo * librarize this command. 236e248de33SArnaldo Carvalho de Melo * 237e248de33SArnaldo Carvalho de Melo * Also experiment with obstacks to see how much speed 238e248de33SArnaldo Carvalho de Melo * up we'll get here. 239e248de33SArnaldo Carvalho de Melo * 240e248de33SArnaldo Carvalho de Melo * perf_session__delete(session); 241e248de33SArnaldo Carvalho de Melo */ 242bab81b62SLi Zefan return ret; 24386470930SIngo Molnar } 24486470930SIngo Molnar 24586470930SIngo Molnar static const char * const annotate_usage[] = { 24686470930SIngo Molnar "perf annotate [<options>] <command>", 24786470930SIngo Molnar NULL 24886470930SIngo Molnar }; 24986470930SIngo Molnar 25086470930SIngo Molnar static const struct option options[] = { 25186470930SIngo Molnar OPT_STRING('i', "input", &input_name, "file", 25286470930SIngo Molnar "input file name"), 253ac73c5a9SArnaldo Carvalho de Melo OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 254ac73c5a9SArnaldo Carvalho de Melo "only consider symbols in these dsos"), 25523b87116SIngo Molnar OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", 25686470930SIngo Molnar "symbol to annotate"), 257fa6963b2SPeter Zijlstra OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 258c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 25986470930SIngo Molnar "be more verbose (show symbol address, etc)"), 26086470930SIngo Molnar OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 26186470930SIngo Molnar "dump raw trace in ASCII"), 2628b9e74ebSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"), 2638b9e74ebSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"), 264b32d133aSArnaldo Carvalho de Melo OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 265b32d133aSArnaldo Carvalho de Melo "file", "vmlinux pathname"), 266b32d133aSArnaldo Carvalho de Melo OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 26742976487SMike Galbraith "load module symbols - WARNING: use only with -k and LIVE kernel"), 268301406b9SFrederic Weisbecker OPT_BOOLEAN('l', "print-line", &print_line, 269301406b9SFrederic Weisbecker "print matching source lines (may be slow)"), 27042976487SMike Galbraith OPT_BOOLEAN('P', "full-paths", &full_paths, 27142976487SMike Galbraith "Don't shorten the displayed pathnames"), 2725d67be97SAnton Blanchard OPT_STRING('c', "cpu", &cpu_list, "cpu", "list of cpus to profile"), 273e71a0598SStephane Eranian OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", 274e71a0598SStephane Eranian "Look for files with symbols relative to this directory"), 2753e6a2a7fSStephane Eranian OPT_BOOLEAN('0', "source", &symbol_conf.annotate_src, 2763e6a2a7fSStephane Eranian "Interleave source code with assembly code (default)"), 2773e6a2a7fSStephane Eranian OPT_BOOLEAN('0', "asm-raw", &symbol_conf.annotate_asm_raw, 2783e6a2a7fSStephane Eranian "Display raw encoding of assembly instructions (default)"), 279f69b64f7SAndi Kleen OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", 280f69b64f7SAndi Kleen "Specify disassembler style (e.g. -M intel for intel syntax)"), 28186470930SIngo Molnar OPT_END() 28286470930SIngo Molnar }; 28386470930SIngo Molnar 284f37a291cSIngo Molnar int cmd_annotate(int argc, const char **argv, const char *prefix __used) 28586470930SIngo Molnar { 286655000e7SArnaldo Carvalho de Melo argc = parse_options(argc, argv, options, annotate_usage, 0); 287655000e7SArnaldo Carvalho de Melo 2888b9e74ebSArnaldo Carvalho de Melo if (use_stdio) 2898b9e74ebSArnaldo Carvalho de Melo use_browser = 0; 2908b9e74ebSArnaldo Carvalho de Melo else if (use_tui) 2918b9e74ebSArnaldo Carvalho de Melo use_browser = 1; 2928b9e74ebSArnaldo Carvalho de Melo 293229ade9bSArnaldo Carvalho de Melo setup_browser(true); 29446e3e055SArnaldo Carvalho de Melo 29578f7defeSArnaldo Carvalho de Melo symbol_conf.priv_size = sizeof(struct annotation); 29675be6cf4SArnaldo Carvalho de Melo symbol_conf.try_vmlinux_path = true; 29775be6cf4SArnaldo Carvalho de Melo 29875be6cf4SArnaldo Carvalho de Melo if (symbol__init() < 0) 299b32d133aSArnaldo Carvalho de Melo return -1; 30086470930SIngo Molnar 301c8829c7aSArnaldo Carvalho de Melo setup_sorting(annotate_usage, options); 30286470930SIngo Molnar 30386470930SIngo Molnar if (argc) { 30486470930SIngo Molnar /* 30586470930SIngo Molnar * Special case: if there's an argument left then assume tha 30686470930SIngo Molnar * it's a symbol filter: 30786470930SIngo Molnar */ 30886470930SIngo Molnar if (argc > 1) 30986470930SIngo Molnar usage_with_options(annotate_usage, options); 31086470930SIngo Molnar 31186470930SIngo Molnar sym_hist_filter = argv[0]; 31286470930SIngo Molnar } 31386470930SIngo Molnar 314dd68ada2SJohn Kacur if (field_sep && *field_sep == '.') { 31529a9f66dSArnaldo Carvalho de Melo pr_err("'.' is the only non valid --field-separator argument\n"); 31629a9f66dSArnaldo Carvalho de Melo return -1; 317dd68ada2SJohn Kacur } 318dd68ada2SJohn Kacur 31986470930SIngo Molnar return __cmd_annotate(); 32086470930SIngo Molnar } 321