1 // SPDX-License-Identifier: GPL-2.0
5 * Builtin annotate command: Analyze the perf.data input file,
6 * look up and read DSOs and symbol information and display
7 * a histogram of results, along various sorting keys.
11 #include "util/color.h"
12 #include <linux/list.h>
13 #include "util/cache.h"
14 #include <linux/rbtree.h>
15 #include <linux/zalloc.h>
16 #include "util/symbol.h"
19 #include "util/debug.h"
21 #include "util/evlist.h"
22 #include "util/evsel.h"
23 #include "util/annotate.h"
24 #include "util/event.h"
25 #include <subcmd/parse-options.h>
26 #include "util/parse-events.h"
27 #include "util/sort.h"
28 #include "util/hist.h"
31 #include "util/session.h"
32 #include "util/tool.h"
33 #include "util/data.h"
34 #include "arch/common.h"
35 #include "util/block-range.h"
36 #include "util/map_symbol.h"
37 #include "util/branch.h"
41 #include <linux/bitmap.h>
43 struct perf_annotate {
44 struct perf_tool tool;
45 struct perf_session *session;
46 struct annotation_options opts;
47 bool use_tui, use_stdio, use_stdio2, use_gtk;
51 const char *sym_hist_filter;
53 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
57 * Given one basic block:
67 * where the horizontal are the branches and the vertical is the executed
68 * block of instructions.
70 * We count, for each 'instruction', the number of blocks that covered it as
71 * well as count the ratio each branch is taken.
73 * We can do this without knowing the actual instruction stream by keeping
74 * track of the address ranges. We break down ranges such that there is no
75 * overlap and iterate from the start until the end.
77 * @acme: once we parse the objdump output _before_ processing the samples,
78 * we can easily fold the branch.cycles IPC bits in.
80 static void process_basic_block(struct addr_map_symbol *start,
81 struct addr_map_symbol *end,
82 struct branch_flags *flags)
84 struct symbol *sym = start->sym;
85 struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
86 struct block_range_iter iter;
87 struct block_range *entry;
90 * Sanity; NULL isn't executable and the CPU cannot execute backwards
92 if (!start->addr || start->addr > end->addr)
95 iter = block_range__create(start->addr, end->addr);
96 if (!block_range_iter__valid(&iter))
100 * First block in range is a branch target.
102 entry = block_range_iter(&iter);
103 assert(entry->is_target);
107 entry = block_range_iter(&iter);
113 notes->max_coverage = max(notes->max_coverage, entry->coverage);
115 } while (block_range_iter__next(&iter));
118 * Last block in rage is a branch.
120 entry = block_range_iter(&iter);
121 assert(entry->is_branch);
123 if (flags->predicted)
127 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
128 struct perf_sample *sample)
130 struct addr_map_symbol *prev = NULL;
131 struct branch_info *bi;
137 bi = sample__resolve_bstack(sample, al);
141 for (i = bs->nr - 1; i >= 0; i--) {
143 * XXX filter against symbol
146 process_basic_block(prev, &bi[i].from, &bi[i].flags);
153 static int hist_iter__branch_callback(struct hist_entry_iter *iter,
154 struct addr_location *al __maybe_unused,
155 bool single __maybe_unused,
156 void *arg __maybe_unused)
158 struct hist_entry *he = iter->he;
159 struct branch_info *bi;
160 struct perf_sample *sample = iter->sample;
161 struct evsel *evsel = iter->evsel;
164 bi = he->branch_info;
165 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
170 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
176 static int process_branch_callback(struct evsel *evsel,
177 struct perf_sample *sample,
178 struct addr_location *al __maybe_unused,
179 struct perf_annotate *ann,
180 struct machine *machine)
182 struct hist_entry_iter iter = {
185 .add_entry_cb = hist_iter__branch_callback,
186 .hide_unresolved = symbol_conf.hide_unresolved,
187 .ops = &hist_iter_branch,
190 struct addr_location a;
193 if (machine__resolve(machine, &a, sample) < 0)
202 hist__account_cycles(sample->branch_stack, al, sample, false);
204 ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
208 static bool has_annotation(struct perf_annotate *ann)
210 return ui__has_annotation() || ann->use_stdio2;
213 static int perf_evsel__add_sample(struct evsel *evsel,
214 struct perf_sample *sample,
215 struct addr_location *al,
216 struct perf_annotate *ann,
217 struct machine *machine)
219 struct hists *hists = evsel__hists(evsel);
220 struct hist_entry *he;
223 if ((!ann->has_br_stack || !has_annotation(ann)) &&
224 ann->sym_hist_filter != NULL &&
226 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
227 /* We're only interested in a symbol named sym_hist_filter */
229 * FIXME: why isn't this done in the symbol_filter when loading
232 if (al->sym != NULL) {
233 rb_erase_cached(&al->sym->rb_node,
234 &al->map->dso->symbols);
235 symbol__delete(al->sym);
236 dso__reset_find_symbol_cache(al->map->dso);
242 * XXX filtered samples can still have branch entires pointing into our
243 * symbol and are missed.
245 process_branch_stack(sample->branch_stack, al, sample);
247 if (ann->has_br_stack && has_annotation(ann))
248 return process_branch_callback(evsel, sample, al, ann, machine);
250 he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
254 ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
255 hists__inc_nr_samples(hists, true);
259 static int process_sample_event(struct perf_tool *tool,
260 union perf_event *event,
261 struct perf_sample *sample,
263 struct machine *machine)
265 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
266 struct addr_location al;
269 if (machine__resolve(machine, &al, sample) < 0) {
270 pr_warning("problem processing %d event, skipping it.\n",
275 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
279 perf_evsel__add_sample(evsel, sample, &al, ann, machine)) {
280 pr_warning("problem incrementing symbol count, "
285 addr_location__put(&al);
289 static int process_feature_event(struct perf_session *session,
290 union perf_event *event)
292 if (event->feat.feat_id < HEADER_LAST_FEATURE)
293 return perf_event__process_feature(session, event);
297 static int hist_entry__tty_annotate(struct hist_entry *he,
299 struct perf_annotate *ann)
301 if (!ann->use_stdio2)
302 return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel, &ann->opts);
304 return symbol__tty_annotate2(he->ms.sym, he->ms.map, evsel, &ann->opts);
307 static void hists__find_annotations(struct hists *hists,
309 struct perf_annotate *ann)
311 struct rb_node *nd = rb_first_cached(&hists->entries), *next;
315 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
316 struct annotation *notes;
318 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
321 if (ann->sym_hist_filter &&
322 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
325 notes = symbol__annotation(he->ms.sym);
326 if (notes->src == NULL) {
335 if (use_browser == 2) {
337 int (*annotate)(struct hist_entry *he,
339 struct hist_browser_timer *hbt);
341 annotate = dlsym(perf_gtk_handle,
342 "hist_entry__gtk_annotate");
343 if (annotate == NULL) {
344 ui__error("GTK browser not found!\n");
348 ret = annotate(he, evsel, NULL);
349 if (!ret || !ann->skip_missing)
352 /* skip missing symbols */
354 } else if (use_browser == 1) {
355 key = hist_entry__tui_annotate(he, evsel, NULL, &ann->opts);
359 if (!ann->skip_missing)
375 hist_entry__tty_annotate(he, evsel, ann);
378 * Since we have a hist_entry per IP for the same
379 * symbol, free he->ms.sym->src to signal we already
380 * processed this symbol.
382 zfree(¬es->src->cycles_hist);
388 static int __cmd_annotate(struct perf_annotate *ann)
391 struct perf_session *session = ann->session;
393 u64 total_nr_samples;
396 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
402 if (!ann->opts.objdump_path) {
403 ret = perf_env__lookup_objdump(&session->header.env,
404 &ann->opts.objdump_path);
409 ret = perf_session__process_events(session);
414 perf_session__fprintf_nr_events(session, stdout);
415 perf_evlist__fprintf_nr_events(session->evlist, stdout);
420 perf_session__fprintf(session, stdout);
423 perf_session__fprintf_dsos(session, stdout);
425 total_nr_samples = 0;
426 evlist__for_each_entry(session->evlist, pos) {
427 struct hists *hists = evsel__hists(pos);
428 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
430 if (nr_samples > 0) {
431 total_nr_samples += nr_samples;
432 hists__collapse_resort(hists, NULL);
433 /* Don't sort callchain */
434 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
435 perf_evsel__output_resort(pos, NULL);
437 if (symbol_conf.event_group &&
438 !perf_evsel__is_group_leader(pos))
441 hists__find_annotations(hists, pos, ann);
445 if (total_nr_samples == 0) {
446 ui__error("The %s data has no samples!\n", session->data->path);
450 if (use_browser == 2) {
451 void (*show_annotations)(void);
453 show_annotations = dlsym(perf_gtk_handle,
454 "perf_gtk__show_annotations");
455 if (show_annotations == NULL) {
456 ui__error("GTK browser not found!\n");
466 static const char * const annotate_usage[] = {
467 "perf annotate [<options>]",
471 int cmd_annotate(int argc, const char **argv)
473 struct perf_annotate annotate = {
475 .sample = process_sample_event,
476 .mmap = perf_event__process_mmap,
477 .mmap2 = perf_event__process_mmap2,
478 .comm = perf_event__process_comm,
479 .exit = perf_event__process_exit,
480 .fork = perf_event__process_fork,
481 .namespaces = perf_event__process_namespaces,
482 .attr = perf_event__process_attr,
483 .build_id = perf_event__process_build_id,
484 .tracing_data = perf_event__process_tracing_data,
485 .feature = process_feature_event,
486 .ordered_events = true,
487 .ordering_requires_timestamps = true,
489 .opts = annotation__default_options,
491 struct perf_data data = {
492 .mode = PERF_DATA_MODE_READ,
494 struct option options[] = {
495 OPT_STRING('i', "input", &input_name, "file",
497 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
498 "only consider symbols in these dsos"),
499 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
500 "symbol to annotate"),
501 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
502 OPT_INCR('v', "verbose", &verbose,
503 "be more verbose (show symbol address, etc)"),
504 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
505 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
506 "dump raw trace in ASCII"),
507 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
508 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
509 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
510 OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
511 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
512 "don't load vmlinux even if found"),
513 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
514 "file", "vmlinux pathname"),
515 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
516 "load module symbols - WARNING: use only with -k and LIVE kernel"),
517 OPT_BOOLEAN('l', "print-line", &annotate.opts.print_lines,
518 "print matching source lines (may be slow)"),
519 OPT_BOOLEAN('P', "full-paths", &annotate.opts.full_path,
520 "Don't shorten the displayed pathnames"),
521 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
522 "Skip symbols that cannot be annotated"),
523 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
525 "Show event group information together"),
526 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
527 OPT_CALLBACK(0, "symfs", NULL, "directory",
528 "Look for files with symbols relative to this directory",
529 symbol__config_symfs),
530 OPT_BOOLEAN(0, "source", &annotate.opts.annotate_src,
531 "Interleave source code with assembly code (default)"),
532 OPT_BOOLEAN(0, "asm-raw", &annotate.opts.show_asm_raw,
533 "Display raw encoding of assembly instructions (default)"),
534 OPT_STRING('M', "disassembler-style", &annotate.opts.disassembler_style, "disassembler style",
535 "Specify disassembler style (e.g. -M intel for intel syntax)"),
536 OPT_STRING(0, "objdump", &annotate.opts.objdump_path, "path",
537 "objdump binary to use for disassembly and annotations"),
538 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
539 "Show event group information together"),
540 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
541 "Show a column with the sum of periods"),
542 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
543 "Show a column with the number of samples"),
544 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
545 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
546 stdio__config_color, "always"),
547 OPT_CALLBACK(0, "percent-type", &annotate.opts, "local-period",
548 "Set percent type local/global-period/hits",
549 annotate_parse_percent_type),
555 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
556 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
563 argc = parse_options(argc, argv, options, annotate_usage, 0);
566 * Special case: if there's an argument left then assume that
567 * it's a symbol filter:
570 usage_with_options(annotate_usage, options);
572 annotate.sym_hist_filter = argv[0];
575 if (symbol_conf.show_nr_samples && annotate.use_gtk) {
576 pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
583 data.path = input_name;
585 annotate.session = perf_session__new(&data, false, &annotate.tool);
586 if (annotate.session == NULL)
589 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
590 HEADER_BRANCH_STACK);
592 if (annotate.group_set)
593 perf_evlist__force_leader(annotate.session->evlist);
595 ret = symbol__annotation_init();
599 annotation_config__init();
601 symbol_conf.try_vmlinux_path = true;
603 ret = symbol__init(&annotate.session->header.env);
607 if (annotate.use_stdio || annotate.use_stdio2)
609 else if (annotate.use_tui)
611 else if (annotate.use_gtk)
616 if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
617 sort__mode = SORT_MODE__BRANCH;
618 if (setup_sorting(annotate.session->evlist) < 0)
619 usage_with_options(annotate_usage, options);
621 if (setup_sorting(NULL) < 0)
622 usage_with_options(annotate_usage, options);
625 ret = __cmd_annotate(&annotate);
629 * Speed up the exit process, for large files this can
630 * take quite a while.
632 * XXX Enable this when using valgrind or if we ever
633 * librarize this command.
635 * Also experiment with obstacks to see how much speed
638 * perf_session__delete(session);