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"
18 #include "util/debug.h"
20 #include "util/evlist.h"
21 #include "util/evsel.h"
22 #include "util/annotate.h"
23 #include "util/event.h"
24 #include <subcmd/parse-options.h>
25 #include "util/parse-events.h"
26 #include "util/sort.h"
27 #include "util/hist.h"
29 #include "util/machine.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"
38 #include "util/util.h"
42 #include <linux/bitmap.h>
43 #include <linux/err.h>
45 struct perf_annotate {
46 struct perf_tool tool;
47 struct perf_session *session;
48 struct annotation_options opts;
49 #ifdef HAVE_SLANG_SUPPORT
52 bool use_stdio, use_stdio2;
53 #ifdef HAVE_GTK2_SUPPORT
60 const char *sym_hist_filter;
62 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
66 * Given one basic block:
76 * where the horizontal are the branches and the vertical is the executed
77 * block of instructions.
79 * We count, for each 'instruction', the number of blocks that covered it as
80 * well as count the ratio each branch is taken.
82 * We can do this without knowing the actual instruction stream by keeping
83 * track of the address ranges. We break down ranges such that there is no
84 * overlap and iterate from the start until the end.
86 * @acme: once we parse the objdump output _before_ processing the samples,
87 * we can easily fold the branch.cycles IPC bits in.
89 static void process_basic_block(struct addr_map_symbol *start,
90 struct addr_map_symbol *end,
91 struct branch_flags *flags)
93 struct symbol *sym = start->ms.sym;
94 struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
95 struct block_range_iter iter;
96 struct block_range *entry;
97 struct annotated_branch *branch;
100 * Sanity; NULL isn't executable and the CPU cannot execute backwards
102 if (!start->addr || start->addr > end->addr)
105 iter = block_range__create(start->addr, end->addr);
106 if (!block_range_iter__valid(&iter))
109 branch = annotation__get_branch(notes);
112 * First block in range is a branch target.
114 entry = block_range_iter(&iter);
115 assert(entry->is_target);
119 entry = block_range_iter(&iter);
125 branch->max_coverage = max(branch->max_coverage, entry->coverage);
127 } while (block_range_iter__next(&iter));
130 * Last block in rage is a branch.
132 entry = block_range_iter(&iter);
133 assert(entry->is_branch);
135 if (flags->predicted)
139 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
140 struct perf_sample *sample)
142 struct addr_map_symbol *prev = NULL;
143 struct branch_info *bi;
149 bi = sample__resolve_bstack(sample, al);
153 for (i = bs->nr - 1; i >= 0; i--) {
155 * XXX filter against symbol
158 process_basic_block(prev, &bi[i].from, &bi[i].flags);
165 static int hist_iter__branch_callback(struct hist_entry_iter *iter,
166 struct addr_location *al __maybe_unused,
167 bool single __maybe_unused,
168 void *arg __maybe_unused)
170 struct hist_entry *he = iter->he;
171 struct branch_info *bi;
172 struct perf_sample *sample = iter->sample;
173 struct evsel *evsel = iter->evsel;
176 bi = he->branch_info;
177 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
182 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
188 static int process_branch_callback(struct evsel *evsel,
189 struct perf_sample *sample,
190 struct addr_location *al,
191 struct perf_annotate *ann,
192 struct machine *machine)
194 struct hist_entry_iter iter = {
197 .add_entry_cb = hist_iter__branch_callback,
198 .hide_unresolved = symbol_conf.hide_unresolved,
199 .ops = &hist_iter_branch,
201 struct addr_location a;
204 addr_location__init(&a);
205 if (machine__resolve(machine, &a, sample) < 0) {
216 map__dso(a.map)->hit = 1;
218 hist__account_cycles(sample->branch_stack, al, sample, false, NULL);
220 ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
222 addr_location__exit(&a);
226 static bool has_annotation(struct perf_annotate *ann)
228 return ui__has_annotation() || ann->use_stdio2;
231 static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
232 struct addr_location *al, struct perf_annotate *ann,
233 struct machine *machine)
235 struct hists *hists = evsel__hists(evsel);
236 struct hist_entry *he;
239 if ((!ann->has_br_stack || !has_annotation(ann)) &&
240 ann->sym_hist_filter != NULL &&
242 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
243 /* We're only interested in a symbol named sym_hist_filter */
245 * FIXME: why isn't this done in the symbol_filter when loading
248 if (al->sym != NULL) {
249 struct dso *dso = map__dso(al->map);
251 rb_erase_cached(&al->sym->rb_node, &dso->symbols);
252 symbol__delete(al->sym);
253 dso__reset_find_symbol_cache(dso);
259 * XXX filtered samples can still have branch entries pointing into our
260 * symbol and are missed.
262 process_branch_stack(sample->branch_stack, al, sample);
264 if (ann->has_br_stack && has_annotation(ann))
265 return process_branch_callback(evsel, sample, al, ann, machine);
267 he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true);
271 ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
272 hists__inc_nr_samples(hists, true);
276 static int process_sample_event(struct perf_tool *tool,
277 union perf_event *event,
278 struct perf_sample *sample,
280 struct machine *machine)
282 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
283 struct addr_location al;
286 addr_location__init(&al);
287 if (machine__resolve(machine, &al, sample) < 0) {
288 pr_warning("problem processing %d event, skipping it.\n",
294 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
298 evsel__add_sample(evsel, sample, &al, ann, machine)) {
299 pr_warning("problem incrementing symbol count, "
304 addr_location__exit(&al);
308 static int process_feature_event(struct perf_session *session,
309 union perf_event *event)
311 if (event->feat.feat_id < HEADER_LAST_FEATURE)
312 return perf_event__process_feature(session, event);
316 static int hist_entry__tty_annotate(struct hist_entry *he,
318 struct perf_annotate *ann)
320 if (!ann->use_stdio2)
321 return symbol__tty_annotate(&he->ms, evsel, &ann->opts);
323 return symbol__tty_annotate2(&he->ms, evsel, &ann->opts);
326 static void hists__find_annotations(struct hists *hists,
328 struct perf_annotate *ann)
330 struct rb_node *nd = rb_first_cached(&hists->entries), *next;
334 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
335 struct annotation *notes;
337 if (he->ms.sym == NULL || map__dso(he->ms.map)->annotate_warned)
340 if (ann->sym_hist_filter &&
341 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
344 if (ann->min_percent) {
346 u64 total = hists__total_period(hists);
349 percent = 100.0 * he->stat.period / total;
351 if (percent < ann->min_percent)
355 notes = symbol__annotation(he->ms.sym);
356 if (notes->src == NULL) {
358 if (key == K_LEFT || key == '<')
365 if (use_browser == 2) {
367 int (*annotate)(struct hist_entry *he,
369 struct annotation_options *options,
370 struct hist_browser_timer *hbt);
372 annotate = dlsym(perf_gtk_handle,
373 "hist_entry__gtk_annotate");
374 if (annotate == NULL) {
375 ui__error("GTK browser not found!\n");
379 ret = annotate(he, evsel, &ann->opts, NULL);
380 if (!ret || !ann->skip_missing)
383 /* skip missing symbols */
385 } else if (use_browser == 1) {
386 key = hist_entry__tui_annotate(he, evsel, NULL, &ann->opts);
390 if (!ann->skip_missing)
408 hist_entry__tty_annotate(he, evsel, ann);
414 static int __cmd_annotate(struct perf_annotate *ann)
417 struct perf_session *session = ann->session;
419 u64 total_nr_samples;
422 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
428 if (!ann->opts.objdump_path) {
429 ret = perf_env__lookup_objdump(&session->header.env,
430 &ann->opts.objdump_path);
435 ret = perf_session__process_events(session);
440 perf_session__fprintf_nr_events(session, stdout, false);
441 evlist__fprintf_nr_events(session->evlist, stdout, false);
446 perf_session__fprintf(session, stdout);
449 perf_session__fprintf_dsos(session, stdout);
451 total_nr_samples = 0;
452 evlist__for_each_entry(session->evlist, pos) {
453 struct hists *hists = evsel__hists(pos);
454 u32 nr_samples = hists->stats.nr_samples;
456 if (nr_samples > 0) {
457 total_nr_samples += nr_samples;
458 hists__collapse_resort(hists, NULL);
459 /* Don't sort callchain */
460 evsel__reset_sample_bit(pos, CALLCHAIN);
461 evsel__output_resort(pos, NULL);
463 if (symbol_conf.event_group && !evsel__is_group_leader(pos))
466 hists__find_annotations(hists, pos, ann);
470 if (total_nr_samples == 0) {
471 ui__error("The %s data has no samples!\n", session->data->path);
475 if (use_browser == 2) {
476 void (*show_annotations)(void);
478 show_annotations = dlsym(perf_gtk_handle,
479 "perf_gtk__show_annotations");
480 if (show_annotations == NULL) {
481 ui__error("GTK browser not found!\n");
491 static int parse_percent_limit(const struct option *opt, const char *str,
492 int unset __maybe_unused)
494 struct perf_annotate *ann = opt->value;
495 double pcnt = strtof(str, NULL);
497 ann->min_percent = pcnt;
501 static const char * const annotate_usage[] = {
502 "perf annotate [<options>]",
506 int cmd_annotate(int argc, const char **argv)
508 struct perf_annotate annotate = {
510 .sample = process_sample_event,
511 .mmap = perf_event__process_mmap,
512 .mmap2 = perf_event__process_mmap2,
513 .comm = perf_event__process_comm,
514 .exit = perf_event__process_exit,
515 .fork = perf_event__process_fork,
516 .namespaces = perf_event__process_namespaces,
517 .attr = perf_event__process_attr,
518 .build_id = perf_event__process_build_id,
519 #ifdef HAVE_LIBTRACEEVENT
520 .tracing_data = perf_event__process_tracing_data,
522 .id_index = perf_event__process_id_index,
523 .auxtrace_info = perf_event__process_auxtrace_info,
524 .auxtrace = perf_event__process_auxtrace,
525 .feature = process_feature_event,
526 .ordered_events = true,
527 .ordering_requires_timestamps = true,
530 struct perf_data data = {
531 .mode = PERF_DATA_MODE_READ,
533 struct itrace_synth_opts itrace_synth_opts = {
536 const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
537 struct option options[] = {
538 OPT_STRING('i', "input", &input_name, "file",
540 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
541 "only consider symbols in these dsos"),
542 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
543 "symbol to annotate"),
544 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
545 OPT_INCR('v', "verbose", &verbose,
546 "be more verbose (show symbol address, etc)"),
547 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"),
548 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
549 "dump raw trace in ASCII"),
550 #ifdef HAVE_GTK2_SUPPORT
551 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
553 #ifdef HAVE_SLANG_SUPPORT
554 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
556 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
557 OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
558 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
559 "don't load vmlinux even if found"),
560 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
561 "file", "vmlinux pathname"),
562 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
563 "load module symbols - WARNING: use only with -k and LIVE kernel"),
564 OPT_BOOLEAN('l', "print-line", &annotate.opts.print_lines,
565 "print matching source lines (may be slow)"),
566 OPT_BOOLEAN('P', "full-paths", &annotate.opts.full_path,
567 "Don't shorten the displayed pathnames"),
568 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
569 "Skip symbols that cannot be annotated"),
570 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
572 "Show event group information together"),
573 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
574 OPT_CALLBACK(0, "symfs", NULL, "directory",
575 "Look for files with symbols relative to this directory",
576 symbol__config_symfs),
577 OPT_BOOLEAN(0, "source", &annotate.opts.annotate_src,
578 "Interleave source code with assembly code (default)"),
579 OPT_BOOLEAN(0, "asm-raw", &annotate.opts.show_asm_raw,
580 "Display raw encoding of assembly instructions (default)"),
581 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
582 "Specify disassembler style (e.g. -M intel for intel syntax)"),
583 OPT_STRING(0, "prefix", &annotate.opts.prefix, "prefix",
584 "Add prefix to source file path names in programs (with --prefix-strip)"),
585 OPT_STRING(0, "prefix-strip", &annotate.opts.prefix_strip, "N",
586 "Strip first N entries of source file path name in programs (with --prefix)"),
587 OPT_STRING(0, "objdump", &objdump_path, "path",
588 "objdump binary to use for disassembly and annotations"),
589 OPT_STRING(0, "addr2line", &addr2line_path, "path",
590 "addr2line binary to use for line numbers"),
591 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
592 "Enable symbol demangling"),
593 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
594 "Enable kernel symbol demangling"),
595 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
596 "Show event group information together"),
597 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
598 "Show a column with the sum of periods"),
599 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
600 "Show a column with the number of samples"),
601 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
602 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
603 stdio__config_color, "always"),
604 OPT_CALLBACK(0, "percent-type", &annotate.opts, "local-period",
605 "Set percent type local/global-period/hits",
606 annotate_parse_percent_type),
607 OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
608 "Don't show entries under that percent", parse_percent_limit),
609 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
610 "Instruction Tracing options\n" ITRACE_HELP,
611 itrace_parse_synth_opts),
617 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
618 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
620 annotation_options__init(&annotate.opts);
626 annotation_config__init(&annotate.opts);
628 argc = parse_options(argc, argv, options, annotate_usage, 0);
631 * Special case: if there's an argument left then assume that
632 * it's a symbol filter:
635 usage_with_options(annotate_usage, options);
637 annotate.sym_hist_filter = argv[0];
640 if (disassembler_style) {
641 annotate.opts.disassembler_style = strdup(disassembler_style);
642 if (!annotate.opts.disassembler_style)
646 annotate.opts.objdump_path = strdup(objdump_path);
647 if (!annotate.opts.objdump_path)
650 if (addr2line_path) {
651 symbol_conf.addr2line_path = strdup(addr2line_path);
652 if (!symbol_conf.addr2line_path)
656 if (annotate_check_args(&annotate.opts) < 0)
659 #ifdef HAVE_GTK2_SUPPORT
660 if (symbol_conf.show_nr_samples && annotate.use_gtk) {
661 pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
666 ret = symbol__validate_sym_arguments();
673 data.path = input_name;
675 annotate.session = perf_session__new(&data, &annotate.tool);
676 if (IS_ERR(annotate.session))
677 return PTR_ERR(annotate.session);
679 annotate.session->itrace_synth_opts = &itrace_synth_opts;
681 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
682 HEADER_BRANCH_STACK);
684 if (annotate.group_set)
685 evlist__force_leader(annotate.session->evlist);
687 ret = symbol__annotation_init();
691 symbol_conf.try_vmlinux_path = true;
693 ret = symbol__init(&annotate.session->header.env);
697 if (annotate.use_stdio || annotate.use_stdio2)
699 #ifdef HAVE_SLANG_SUPPORT
700 else if (annotate.use_tui)
703 #ifdef HAVE_GTK2_SUPPORT
704 else if (annotate.use_gtk)
711 * Events of different processes may correspond to the same
712 * symbol, we do not care about the processes in annotate,
713 * set sort order to avoid repeated output.
715 sort_order = "dso,symbol";
718 * Set SORT_MODE__BRANCH so that annotate display IPC/Cycle
719 * if branch info is in perf data in TUI mode.
721 if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack)
722 sort__mode = SORT_MODE__BRANCH;
724 if (setup_sorting(NULL) < 0)
725 usage_with_options(annotate_usage, options);
727 ret = __cmd_annotate(&annotate);
731 * Speed up the exit process by only deleting for debug builds. For
732 * large files this can save time.
735 perf_session__delete(annotate.session);
737 annotation_options__exit(&annotate.opts);