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"
30 #include "util/machine.h"
32 #include "util/session.h"
33 #include "util/tool.h"
34 #include "util/data.h"
35 #include "arch/common.h"
36 #include "util/block-range.h"
37 #include "util/map_symbol.h"
38 #include "util/branch.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;
58 const char *sym_hist_filter;
60 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
64 * Given one basic block:
74 * where the horizontal are the branches and the vertical is the executed
75 * block of instructions.
77 * We count, for each 'instruction', the number of blocks that covered it as
78 * well as count the ratio each branch is taken.
80 * We can do this without knowing the actual instruction stream by keeping
81 * track of the address ranges. We break down ranges such that there is no
82 * overlap and iterate from the start until the end.
84 * @acme: once we parse the objdump output _before_ processing the samples,
85 * we can easily fold the branch.cycles IPC bits in.
87 static void process_basic_block(struct addr_map_symbol *start,
88 struct addr_map_symbol *end,
89 struct branch_flags *flags)
91 struct symbol *sym = start->ms.sym;
92 struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
93 struct block_range_iter iter;
94 struct block_range *entry;
97 * Sanity; NULL isn't executable and the CPU cannot execute backwards
99 if (!start->addr || start->addr > end->addr)
102 iter = block_range__create(start->addr, end->addr);
103 if (!block_range_iter__valid(&iter))
107 * First block in range is a branch target.
109 entry = block_range_iter(&iter);
110 assert(entry->is_target);
114 entry = block_range_iter(&iter);
120 notes->max_coverage = max(notes->max_coverage, entry->coverage);
122 } while (block_range_iter__next(&iter));
125 * Last block in rage is a branch.
127 entry = block_range_iter(&iter);
128 assert(entry->is_branch);
130 if (flags->predicted)
134 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
135 struct perf_sample *sample)
137 struct addr_map_symbol *prev = NULL;
138 struct branch_info *bi;
144 bi = sample__resolve_bstack(sample, al);
148 for (i = bs->nr - 1; i >= 0; i--) {
150 * XXX filter against symbol
153 process_basic_block(prev, &bi[i].from, &bi[i].flags);
160 static int hist_iter__branch_callback(struct hist_entry_iter *iter,
161 struct addr_location *al __maybe_unused,
162 bool single __maybe_unused,
163 void *arg __maybe_unused)
165 struct hist_entry *he = iter->he;
166 struct branch_info *bi;
167 struct perf_sample *sample = iter->sample;
168 struct evsel *evsel = iter->evsel;
171 bi = he->branch_info;
172 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
177 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
183 static int process_branch_callback(struct evsel *evsel,
184 struct perf_sample *sample,
185 struct addr_location *al __maybe_unused,
186 struct perf_annotate *ann,
187 struct machine *machine)
189 struct hist_entry_iter iter = {
192 .add_entry_cb = hist_iter__branch_callback,
193 .hide_unresolved = symbol_conf.hide_unresolved,
194 .ops = &hist_iter_branch,
197 struct addr_location a;
199 if (machine__resolve(machine, &a, sample) < 0)
208 hist__account_cycles(sample->branch_stack, al, sample, false, NULL);
210 return hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
213 static bool has_annotation(struct perf_annotate *ann)
215 return ui__has_annotation() || ann->use_stdio2;
218 static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
219 struct addr_location *al, struct perf_annotate *ann,
220 struct machine *machine)
222 struct hists *hists = evsel__hists(evsel);
223 struct hist_entry *he;
226 if ((!ann->has_br_stack || !has_annotation(ann)) &&
227 ann->sym_hist_filter != NULL &&
229 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
230 /* We're only interested in a symbol named sym_hist_filter */
232 * FIXME: why isn't this done in the symbol_filter when loading
235 if (al->sym != NULL) {
236 rb_erase_cached(&al->sym->rb_node,
237 &al->map->dso->symbols);
238 symbol__delete(al->sym);
239 dso__reset_find_symbol_cache(al->map->dso);
245 * XXX filtered samples can still have branch entries pointing into our
246 * symbol and are missed.
248 process_branch_stack(sample->branch_stack, al, sample);
250 if (ann->has_br_stack && has_annotation(ann))
251 return process_branch_callback(evsel, sample, al, ann, machine);
253 he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
257 ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
258 hists__inc_nr_samples(hists, true);
262 static int process_sample_event(struct perf_tool *tool,
263 union perf_event *event,
264 struct perf_sample *sample,
266 struct machine *machine)
268 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
269 struct addr_location al;
272 if (machine__resolve(machine, &al, sample) < 0) {
273 pr_warning("problem processing %d event, skipping it.\n",
278 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
282 evsel__add_sample(evsel, sample, &al, ann, machine)) {
283 pr_warning("problem incrementing symbol count, "
288 addr_location__put(&al);
292 static int process_feature_event(struct perf_session *session,
293 union perf_event *event)
295 if (event->feat.feat_id < HEADER_LAST_FEATURE)
296 return perf_event__process_feature(session, event);
300 static int hist_entry__tty_annotate(struct hist_entry *he,
302 struct perf_annotate *ann)
304 if (!ann->use_stdio2)
305 return symbol__tty_annotate(&he->ms, evsel, &ann->opts);
307 return symbol__tty_annotate2(&he->ms, evsel, &ann->opts);
310 static void hists__find_annotations(struct hists *hists,
312 struct perf_annotate *ann)
314 struct rb_node *nd = rb_first_cached(&hists->entries), *next;
318 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
319 struct annotation *notes;
321 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
324 if (ann->sym_hist_filter &&
325 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
328 if (ann->min_percent) {
330 u64 total = hists__total_period(hists);
333 percent = 100.0 * he->stat.period / total;
335 if (percent < ann->min_percent)
339 notes = symbol__annotation(he->ms.sym);
340 if (notes->src == NULL) {
349 if (use_browser == 2) {
351 int (*annotate)(struct hist_entry *he,
353 struct hist_browser_timer *hbt);
355 annotate = dlsym(perf_gtk_handle,
356 "hist_entry__gtk_annotate");
357 if (annotate == NULL) {
358 ui__error("GTK browser not found!\n");
362 ret = annotate(he, evsel, NULL);
363 if (!ret || !ann->skip_missing)
366 /* skip missing symbols */
368 } else if (use_browser == 1) {
369 key = hist_entry__tui_annotate(he, evsel, NULL, &ann->opts);
373 if (!ann->skip_missing)
389 hist_entry__tty_annotate(he, evsel, ann);
395 static int __cmd_annotate(struct perf_annotate *ann)
398 struct perf_session *session = ann->session;
400 u64 total_nr_samples;
403 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
409 if (!ann->opts.objdump_path) {
410 ret = perf_env__lookup_objdump(&session->header.env,
411 &ann->opts.objdump_path);
416 ret = perf_session__process_events(session);
421 perf_session__fprintf_nr_events(session, stdout, false);
422 evlist__fprintf_nr_events(session->evlist, stdout, false);
427 perf_session__fprintf(session, stdout);
430 perf_session__fprintf_dsos(session, stdout);
432 total_nr_samples = 0;
433 evlist__for_each_entry(session->evlist, pos) {
434 struct hists *hists = evsel__hists(pos);
435 u32 nr_samples = hists->stats.nr_samples;
437 if (nr_samples > 0) {
438 total_nr_samples += nr_samples;
439 hists__collapse_resort(hists, NULL);
440 /* Don't sort callchain */
441 evsel__reset_sample_bit(pos, CALLCHAIN);
442 evsel__output_resort(pos, NULL);
444 if (symbol_conf.event_group && !evsel__is_group_leader(pos))
447 hists__find_annotations(hists, pos, ann);
451 if (total_nr_samples == 0) {
452 ui__error("The %s data has no samples!\n", session->data->path);
456 if (use_browser == 2) {
457 void (*show_annotations)(void);
459 show_annotations = dlsym(perf_gtk_handle,
460 "perf_gtk__show_annotations");
461 if (show_annotations == NULL) {
462 ui__error("GTK browser not found!\n");
472 static int parse_percent_limit(const struct option *opt, const char *str,
473 int unset __maybe_unused)
475 struct perf_annotate *ann = opt->value;
476 double pcnt = strtof(str, NULL);
478 ann->min_percent = pcnt;
482 static const char * const annotate_usage[] = {
483 "perf annotate [<options>]",
487 int cmd_annotate(int argc, const char **argv)
489 struct perf_annotate annotate = {
491 .sample = process_sample_event,
492 .mmap = perf_event__process_mmap,
493 .mmap2 = perf_event__process_mmap2,
494 .comm = perf_event__process_comm,
495 .exit = perf_event__process_exit,
496 .fork = perf_event__process_fork,
497 .namespaces = perf_event__process_namespaces,
498 .attr = perf_event__process_attr,
499 .build_id = perf_event__process_build_id,
500 .tracing_data = perf_event__process_tracing_data,
501 .id_index = perf_event__process_id_index,
502 .auxtrace_info = perf_event__process_auxtrace_info,
503 .auxtrace = perf_event__process_auxtrace,
504 .feature = process_feature_event,
505 .ordered_events = true,
506 .ordering_requires_timestamps = true,
508 .opts = annotation__default_options,
510 struct perf_data data = {
511 .mode = PERF_DATA_MODE_READ,
513 struct itrace_synth_opts itrace_synth_opts = {
516 struct option options[] = {
517 OPT_STRING('i', "input", &input_name, "file",
519 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
520 "only consider symbols in these dsos"),
521 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
522 "symbol to annotate"),
523 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
524 OPT_INCR('v', "verbose", &verbose,
525 "be more verbose (show symbol address, etc)"),
526 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
527 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
528 "dump raw trace in ASCII"),
529 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
530 #ifdef HAVE_SLANG_SUPPORT
531 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
533 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
534 OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
535 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
536 "don't load vmlinux even if found"),
537 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
538 "file", "vmlinux pathname"),
539 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
540 "load module symbols - WARNING: use only with -k and LIVE kernel"),
541 OPT_BOOLEAN('l', "print-line", &annotate.opts.print_lines,
542 "print matching source lines (may be slow)"),
543 OPT_BOOLEAN('P', "full-paths", &annotate.opts.full_path,
544 "Don't shorten the displayed pathnames"),
545 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
546 "Skip symbols that cannot be annotated"),
547 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
549 "Show event group information together"),
550 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
551 OPT_CALLBACK(0, "symfs", NULL, "directory",
552 "Look for files with symbols relative to this directory",
553 symbol__config_symfs),
554 OPT_BOOLEAN(0, "source", &annotate.opts.annotate_src,
555 "Interleave source code with assembly code (default)"),
556 OPT_BOOLEAN(0, "asm-raw", &annotate.opts.show_asm_raw,
557 "Display raw encoding of assembly instructions (default)"),
558 OPT_STRING('M', "disassembler-style", &annotate.opts.disassembler_style, "disassembler style",
559 "Specify disassembler style (e.g. -M intel for intel syntax)"),
560 OPT_STRING(0, "prefix", &annotate.opts.prefix, "prefix",
561 "Add prefix to source file path names in programs (with --prefix-strip)"),
562 OPT_STRING(0, "prefix-strip", &annotate.opts.prefix_strip, "N",
563 "Strip first N entries of source file path name in programs (with --prefix)"),
564 OPT_STRING(0, "objdump", &annotate.opts.objdump_path, "path",
565 "objdump binary to use for disassembly and annotations"),
566 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
567 "Enable symbol demangling"),
568 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
569 "Enable kernel symbol demangling"),
570 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
571 "Show event group information together"),
572 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
573 "Show a column with the sum of periods"),
574 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
575 "Show a column with the number of samples"),
576 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
577 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
578 stdio__config_color, "always"),
579 OPT_CALLBACK(0, "percent-type", &annotate.opts, "local-period",
580 "Set percent type local/global-period/hits",
581 annotate_parse_percent_type),
582 OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
583 "Don't show entries under that percent", parse_percent_limit),
584 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
585 "Instruction Tracing options\n" ITRACE_HELP,
586 itrace_parse_synth_opts),
592 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
593 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
600 annotation_config__init(&annotate.opts);
602 argc = parse_options(argc, argv, options, annotate_usage, 0);
605 * Special case: if there's an argument left then assume that
606 * it's a symbol filter:
609 usage_with_options(annotate_usage, options);
611 annotate.sym_hist_filter = argv[0];
614 if (annotate_check_args(&annotate.opts) < 0)
617 if (symbol_conf.show_nr_samples && annotate.use_gtk) {
618 pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
622 ret = symbol__validate_sym_arguments();
629 data.path = input_name;
631 annotate.session = perf_session__new(&data, &annotate.tool);
632 if (IS_ERR(annotate.session))
633 return PTR_ERR(annotate.session);
635 annotate.session->itrace_synth_opts = &itrace_synth_opts;
637 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
638 HEADER_BRANCH_STACK);
640 if (annotate.group_set)
641 evlist__force_leader(annotate.session->evlist);
643 ret = symbol__annotation_init();
647 symbol_conf.try_vmlinux_path = true;
649 ret = symbol__init(&annotate.session->header.env);
653 if (annotate.use_stdio || annotate.use_stdio2)
655 #ifdef HAVE_SLANG_SUPPORT
656 else if (annotate.use_tui)
659 else if (annotate.use_gtk)
665 * Events of different processes may correspond to the same
666 * symbol, we do not care about the processes in annotate,
667 * set sort order to avoid repeated output.
669 sort_order = "dso,symbol";
672 * Set SORT_MODE__BRANCH so that annotate display IPC/Cycle
673 * if branch info is in perf data in TUI mode.
675 if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack)
676 sort__mode = SORT_MODE__BRANCH;
678 if (setup_sorting(NULL) < 0)
679 usage_with_options(annotate_usage, options);
681 ret = __cmd_annotate(&annotate);
685 * Speed up the exit process, for large files this can
686 * take quite a while.
688 * XXX Enable this when using valgrind or if we ever
689 * librarize this command.
691 * Also experiment with obstacks to see how much speed
694 * perf_session__delete(session);