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>
44 struct perf_annotate {
45 struct perf_tool tool;
46 struct perf_session *session;
47 struct annotation_options opts;
48 bool use_tui, use_stdio, use_stdio2, use_gtk;
52 const char *sym_hist_filter;
54 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
58 * Given one basic block:
68 * where the horizontal are the branches and the vertical is the executed
69 * block of instructions.
71 * We count, for each 'instruction', the number of blocks that covered it as
72 * well as count the ratio each branch is taken.
74 * We can do this without knowing the actual instruction stream by keeping
75 * track of the address ranges. We break down ranges such that there is no
76 * overlap and iterate from the start until the end.
78 * @acme: once we parse the objdump output _before_ processing the samples,
79 * we can easily fold the branch.cycles IPC bits in.
81 static void process_basic_block(struct addr_map_symbol *start,
82 struct addr_map_symbol *end,
83 struct branch_flags *flags)
85 struct symbol *sym = start->sym;
86 struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
87 struct block_range_iter iter;
88 struct block_range *entry;
91 * Sanity; NULL isn't executable and the CPU cannot execute backwards
93 if (!start->addr || start->addr > end->addr)
96 iter = block_range__create(start->addr, end->addr);
97 if (!block_range_iter__valid(&iter))
101 * First block in range is a branch target.
103 entry = block_range_iter(&iter);
104 assert(entry->is_target);
108 entry = block_range_iter(&iter);
114 notes->max_coverage = max(notes->max_coverage, entry->coverage);
116 } while (block_range_iter__next(&iter));
119 * Last block in rage is a branch.
121 entry = block_range_iter(&iter);
122 assert(entry->is_branch);
124 if (flags->predicted)
128 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
129 struct perf_sample *sample)
131 struct addr_map_symbol *prev = NULL;
132 struct branch_info *bi;
138 bi = sample__resolve_bstack(sample, al);
142 for (i = bs->nr - 1; i >= 0; i--) {
144 * XXX filter against symbol
147 process_basic_block(prev, &bi[i].from, &bi[i].flags);
154 static int hist_iter__branch_callback(struct hist_entry_iter *iter,
155 struct addr_location *al __maybe_unused,
156 bool single __maybe_unused,
157 void *arg __maybe_unused)
159 struct hist_entry *he = iter->he;
160 struct branch_info *bi;
161 struct perf_sample *sample = iter->sample;
162 struct evsel *evsel = iter->evsel;
165 bi = he->branch_info;
166 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
171 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
177 static int process_branch_callback(struct evsel *evsel,
178 struct perf_sample *sample,
179 struct addr_location *al __maybe_unused,
180 struct perf_annotate *ann,
181 struct machine *machine)
183 struct hist_entry_iter iter = {
186 .add_entry_cb = hist_iter__branch_callback,
187 .hide_unresolved = symbol_conf.hide_unresolved,
188 .ops = &hist_iter_branch,
191 struct addr_location a;
194 if (machine__resolve(machine, &a, sample) < 0)
203 hist__account_cycles(sample->branch_stack, al, sample, false);
205 ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
209 static bool has_annotation(struct perf_annotate *ann)
211 return ui__has_annotation() || ann->use_stdio2;
214 static int perf_evsel__add_sample(struct evsel *evsel,
215 struct perf_sample *sample,
216 struct addr_location *al,
217 struct perf_annotate *ann,
218 struct machine *machine)
220 struct hists *hists = evsel__hists(evsel);
221 struct hist_entry *he;
224 if ((!ann->has_br_stack || !has_annotation(ann)) &&
225 ann->sym_hist_filter != NULL &&
227 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
228 /* We're only interested in a symbol named sym_hist_filter */
230 * FIXME: why isn't this done in the symbol_filter when loading
233 if (al->sym != NULL) {
234 rb_erase_cached(&al->sym->rb_node,
235 &al->map->dso->symbols);
236 symbol__delete(al->sym);
237 dso__reset_find_symbol_cache(al->map->dso);
243 * XXX filtered samples can still have branch entires pointing into our
244 * symbol and are missed.
246 process_branch_stack(sample->branch_stack, al, sample);
248 if (ann->has_br_stack && has_annotation(ann))
249 return process_branch_callback(evsel, sample, al, ann, machine);
251 he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
255 ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
256 hists__inc_nr_samples(hists, true);
260 static int process_sample_event(struct perf_tool *tool,
261 union perf_event *event,
262 struct perf_sample *sample,
264 struct machine *machine)
266 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
267 struct addr_location al;
270 if (machine__resolve(machine, &al, sample) < 0) {
271 pr_warning("problem processing %d event, skipping it.\n",
276 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
280 perf_evsel__add_sample(evsel, sample, &al, ann, machine)) {
281 pr_warning("problem incrementing symbol count, "
286 addr_location__put(&al);
290 static int process_feature_event(struct perf_session *session,
291 union perf_event *event)
293 if (event->feat.feat_id < HEADER_LAST_FEATURE)
294 return perf_event__process_feature(session, event);
298 static int hist_entry__tty_annotate(struct hist_entry *he,
300 struct perf_annotate *ann)
302 if (!ann->use_stdio2)
303 return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel, &ann->opts);
305 return symbol__tty_annotate2(he->ms.sym, he->ms.map, evsel, &ann->opts);
308 static void hists__find_annotations(struct hists *hists,
310 struct perf_annotate *ann)
312 struct rb_node *nd = rb_first_cached(&hists->entries), *next;
316 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
317 struct annotation *notes;
319 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
322 if (ann->sym_hist_filter &&
323 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
326 notes = symbol__annotation(he->ms.sym);
327 if (notes->src == NULL) {
336 if (use_browser == 2) {
338 int (*annotate)(struct hist_entry *he,
340 struct hist_browser_timer *hbt);
342 annotate = dlsym(perf_gtk_handle,
343 "hist_entry__gtk_annotate");
344 if (annotate == NULL) {
345 ui__error("GTK browser not found!\n");
349 ret = annotate(he, evsel, NULL);
350 if (!ret || !ann->skip_missing)
353 /* skip missing symbols */
355 } else if (use_browser == 1) {
356 key = hist_entry__tui_annotate(he, evsel, NULL, &ann->opts);
360 if (!ann->skip_missing)
376 hist_entry__tty_annotate(he, evsel, ann);
379 * Since we have a hist_entry per IP for the same
380 * symbol, free he->ms.sym->src to signal we already
381 * processed this symbol.
383 zfree(¬es->src->cycles_hist);
389 static int __cmd_annotate(struct perf_annotate *ann)
392 struct perf_session *session = ann->session;
394 u64 total_nr_samples;
397 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
403 if (!ann->opts.objdump_path) {
404 ret = perf_env__lookup_objdump(&session->header.env,
405 &ann->opts.objdump_path);
410 ret = perf_session__process_events(session);
415 perf_session__fprintf_nr_events(session, stdout);
416 perf_evlist__fprintf_nr_events(session->evlist, stdout);
421 perf_session__fprintf(session, stdout);
424 perf_session__fprintf_dsos(session, stdout);
426 total_nr_samples = 0;
427 evlist__for_each_entry(session->evlist, pos) {
428 struct hists *hists = evsel__hists(pos);
429 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
431 if (nr_samples > 0) {
432 total_nr_samples += nr_samples;
433 hists__collapse_resort(hists, NULL);
434 /* Don't sort callchain */
435 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
436 perf_evsel__output_resort(pos, NULL);
438 if (symbol_conf.event_group &&
439 !perf_evsel__is_group_leader(pos))
442 hists__find_annotations(hists, pos, ann);
446 if (total_nr_samples == 0) {
447 ui__error("The %s data has no samples!\n", session->data->path);
451 if (use_browser == 2) {
452 void (*show_annotations)(void);
454 show_annotations = dlsym(perf_gtk_handle,
455 "perf_gtk__show_annotations");
456 if (show_annotations == NULL) {
457 ui__error("GTK browser not found!\n");
467 static const char * const annotate_usage[] = {
468 "perf annotate [<options>]",
472 int cmd_annotate(int argc, const char **argv)
474 struct perf_annotate annotate = {
476 .sample = process_sample_event,
477 .mmap = perf_event__process_mmap,
478 .mmap2 = perf_event__process_mmap2,
479 .comm = perf_event__process_comm,
480 .exit = perf_event__process_exit,
481 .fork = perf_event__process_fork,
482 .namespaces = perf_event__process_namespaces,
483 .attr = perf_event__process_attr,
484 .build_id = perf_event__process_build_id,
485 .tracing_data = perf_event__process_tracing_data,
486 .feature = process_feature_event,
487 .ordered_events = true,
488 .ordering_requires_timestamps = true,
490 .opts = annotation__default_options,
492 struct perf_data data = {
493 .mode = PERF_DATA_MODE_READ,
495 struct option options[] = {
496 OPT_STRING('i', "input", &input_name, "file",
498 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
499 "only consider symbols in these dsos"),
500 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
501 "symbol to annotate"),
502 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
503 OPT_INCR('v', "verbose", &verbose,
504 "be more verbose (show symbol address, etc)"),
505 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
506 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
507 "dump raw trace in ASCII"),
508 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
509 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
510 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
511 OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
512 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
513 "don't load vmlinux even if found"),
514 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
515 "file", "vmlinux pathname"),
516 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
517 "load module symbols - WARNING: use only with -k and LIVE kernel"),
518 OPT_BOOLEAN('l', "print-line", &annotate.opts.print_lines,
519 "print matching source lines (may be slow)"),
520 OPT_BOOLEAN('P', "full-paths", &annotate.opts.full_path,
521 "Don't shorten the displayed pathnames"),
522 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
523 "Skip symbols that cannot be annotated"),
524 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
526 "Show event group information together"),
527 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
528 OPT_CALLBACK(0, "symfs", NULL, "directory",
529 "Look for files with symbols relative to this directory",
530 symbol__config_symfs),
531 OPT_BOOLEAN(0, "source", &annotate.opts.annotate_src,
532 "Interleave source code with assembly code (default)"),
533 OPT_BOOLEAN(0, "asm-raw", &annotate.opts.show_asm_raw,
534 "Display raw encoding of assembly instructions (default)"),
535 OPT_STRING('M', "disassembler-style", &annotate.opts.disassembler_style, "disassembler style",
536 "Specify disassembler style (e.g. -M intel for intel syntax)"),
537 OPT_STRING(0, "objdump", &annotate.opts.objdump_path, "path",
538 "objdump binary to use for disassembly and annotations"),
539 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
540 "Show event group information together"),
541 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
542 "Show a column with the sum of periods"),
543 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
544 "Show a column with the number of samples"),
545 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
546 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
547 stdio__config_color, "always"),
548 OPT_CALLBACK(0, "percent-type", &annotate.opts, "local-period",
549 "Set percent type local/global-period/hits",
550 annotate_parse_percent_type),
556 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
557 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
564 argc = parse_options(argc, argv, options, annotate_usage, 0);
567 * Special case: if there's an argument left then assume that
568 * it's a symbol filter:
571 usage_with_options(annotate_usage, options);
573 annotate.sym_hist_filter = argv[0];
576 if (symbol_conf.show_nr_samples && annotate.use_gtk) {
577 pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
584 data.path = input_name;
586 annotate.session = perf_session__new(&data, false, &annotate.tool);
587 if (annotate.session == NULL)
590 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
591 HEADER_BRANCH_STACK);
593 if (annotate.group_set)
594 perf_evlist__force_leader(annotate.session->evlist);
596 ret = symbol__annotation_init();
600 annotation_config__init();
602 symbol_conf.try_vmlinux_path = true;
604 ret = symbol__init(&annotate.session->header.env);
608 if (annotate.use_stdio || annotate.use_stdio2)
610 else if (annotate.use_tui)
612 else if (annotate.use_gtk)
617 if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
618 sort__mode = SORT_MODE__BRANCH;
619 if (setup_sorting(annotate.session->evlist) < 0)
620 usage_with_options(annotate_usage, options);
622 if (setup_sorting(NULL) < 0)
623 usage_with_options(annotate_usage, options);
626 ret = __cmd_annotate(&annotate);
630 * Speed up the exit process, for large files this can
631 * take quite a while.
633 * XXX Enable this when using valgrind or if we ever
634 * librarize this command.
636 * Also experiment with obstacks to see how much speed
639 * perf_session__delete(session);