// SPDX-License-Identifier: GPL-2.0 /* * builtin-annotate.c * * Builtin annotate command: Analyze the perf.data input file, * look up and read DSOs and symbol information and display * a histogram of results, along various sorting keys.
*/ #include"builtin.h" #include"perf.h"
/* * Given one basic block: * * from to branch_i * * ----> * * | * | block * v * * ----> * * from to branch_i+1 * * where the horizontal are the branches and the vertical is the executed * block of instructions. * * We count, for each 'instruction', the number of blocks that covered it as * well as count the ratio each branch is taken. * * We can do this without knowing the actual instruction stream by keeping * track of the address ranges. We break down ranges such that there is no * overlap and iterate from the start until the end. * * @acme: once we parse the objdump output _before_ processing the samples, * we can easily fold the branch.cycles IPC bits in.
*/ staticvoid process_basic_block(struct addr_map_symbol *start, struct addr_map_symbol *end, struct branch_flags *flags)
{ struct symbol *sym = start->ms.sym; struct annotation *notes = sym ? symbol__annotation(sym) : NULL; struct block_range_iter iter; struct block_range *entry; struct annotated_branch *branch;
/* * Sanity; NULL isn't executable and the CPU cannot execute backwards
*/ if (!start->addr || start->addr > end->addr) return;
iter = block_range__create(start->addr, end->addr); if (!block_range_iter__valid(&iter)) return;
branch = annotation__get_branch(notes);
/* * First block in range is a branch target.
*/
entry = block_range_iter(&iter);
assert(entry->is_target);
entry->entry++;
do {
entry = block_range_iter(&iter);
entry->coverage++;
entry->sym = sym;
if (branch)
branch->max_coverage = max(branch->max_coverage, entry->coverage);
} while (block_range_iter__next(&iter));
/* * Last block in rage is a branch.
*/
entry = block_range_iter(&iter);
assert(entry->is_branch);
entry->taken++; if (flags->predicted)
entry->pred++;
}
bi = sample__resolve_bstack(sample, al); if (!bi) return;
for (i = bs->nr - 1; i >= 0; i--) { /* * XXX filter against symbol
*/ if (prev)
process_basic_block(prev, &bi[i].from, &bi[i].flags);
prev = &bi[i].to;
}
if ((!ann->has_br_stack || !has_annotation(ann)) &&
ann->sym_hist_filter != NULL &&
(al->sym == NULL ||
strcmp(ann->sym_hist_filter, al->sym->name) != 0)) { /* We're only interested in a symbol named sym_hist_filter */ /* * FIXME: why isn't this done in the symbol_filter when loading * the DSO?
*/ if (al->sym != NULL) { struct dso *dso = map__dso(al->map);
/* * XXX filtered samples can still have branch entries pointing into our * symbol and are missed.
*/
process_branch_stack(sample->branch_stack, al, sample);
if (ann->has_br_stack && has_annotation(ann)) return process_branch_callback(evsel, sample, al, ann, machine);
he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true); if (he == NULL) return -ENOMEM;
switch (key) { case -1: if (!ann->skip_missing) return; /* fall through */ case K_RIGHT: case'>':
next = rb_next(nd); break; case K_LEFT: case'<':
next = rb_prev(nd); break; default: return;
}
if (use_browser == 0 || next != NULL)
nd = next;
continue;
}
if (use_browser == 2) { int ret; int (*annotate)(struct hist_entry *he, struct evsel *evsel, struct hist_browser_timer *hbt);
annotate = dlsym(perf_gtk_handle, "hist_entry__gtk_annotate"); if (annotate == NULL) {
ui__error("GTK browser not found!\n"); return;
}
ret = annotate(he, evsel, NULL); if (!ret || !ann->skip_missing) return;
switch (key) { case -1: if (!ann->skip_missing) return; /* fall through */ case K_RIGHT: case'>':
next = rb_next(nd); break; case K_LEFT: case'<':
next = rb_prev(nd); break; default: return;
}
ui_progress__init(&prog, nr_samples, "Sorting events for output...");
evsel__output_resort(pos, &prog);
ui_progress__finish();
/* * An event group needs to display other events too. * Let's delay printing until other events are processed.
*/ if (symbol_conf.event_group) { if (!evsel__is_group_leader(pos)) { struct hists *leader_hists;
int cmd_annotate(int argc, constchar **argv)
{ struct perf_annotate annotate = {}; struct perf_data data = {
.mode = PERF_DATA_MODE_READ,
}; struct itrace_synth_opts itrace_synth_opts = {
.set = 0,
}; constchar *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL; struct option options[] = {
OPT_STRING('i', "input", &input_name, "file", "input file name"),
OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", "only consider symbols in these dsos"),
OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol", "symbol to annotate"),
OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"),
OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"), #ifdef HAVE_GTK2_SUPPORT
OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"), #endif #ifdef HAVE_SLANG_SUPPORT
OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"), #endif
OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux, "don't load vmlinux even if found"),
OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, "file", "vmlinux pathname"),
OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, "load module symbols - WARNING: use only with -k and LIVE kernel"),
OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines, "print matching source lines (may be slow)"),
OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path, "Don't shorten the displayed pathnames"),
OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing, "Skip symbols that cannot be annotated"),
OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
&annotate.group_set, "Show event group information together"),
OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
OPT_CALLBACK(0, "symfs", NULL, "directory", "Look for files with symbols relative to this directory",
symbol__config_symfs),
OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src, "Interleave source code with assembly code (default)"),
OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw, "Display raw encoding of assembly instructions (default)"),
OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", "Specify disassembler style (e.g. -M intel for intel syntax)"),
OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix", "Add prefix to source file path names in programs (with --prefix-strip)"),
OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N", "Strip first N entries of source file path name in programs (with --prefix)"),
OPT_STRING(0, "objdump", &objdump_path, "path", "objdump binary to use for disassembly and annotations"),
OPT_STRING(0, "addr2line", &addr2line_path, "path", "addr2line binary to use for line numbers"),
OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, "Enable symbol demangling"),
OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, "Enable kernel symbol demangling"),
OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period, "Show a column with the sum of periods"),
OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, "Show a column with the number of samples"),
OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode", "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
stdio__config_color, "always"),
OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period", "Set percent type local/global-period/hits",
annotate_parse_percent_type),
OPT_CALLBACK(0, "percent-limit", &annotate, "percent", "Don't show entries under that percent", parse_percent_limit),
OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", "Instruction Tracing options\n" ITRACE_HELP,
itrace_parse_synth_opts),
OPT_CALLBACK_OPTARG(0, "data-type", &annotate, NULL, "name", "Show data type annotate for the memory accesses",
parse_data_type),
OPT_BOOLEAN(0, "type-stat", &annotate.type_stat, "Show stats for the data type annotation"),
OPT_BOOLEAN(0, "insn-stat", &annotate.insn_stat, "Show instruction stats for the data type annotation"),
OPT_BOOLEAN(0, "skip-empty", &symbol_conf.skip_empty, "Do not display empty (or dummy) events in the output"),
OPT_BOOLEAN(0, "code-with-type", &annotate_opts.code_with_type, "Show data type info in code annotation (memory instructions only)"),
OPT_END()
}; int ret;
argc = parse_options(argc, argv, options, annotate_usage, 0); if (argc) { /* * Special case: if there's an argument left then assume that * it's a symbol filter:
*/ if (argc > 1)
usage_with_options(annotate_usage, options);
annotate.sym_hist_filter = argv[0];
}
if (disassembler_style) {
annotate_opts.disassembler_style = strdup(disassembler_style); if (!annotate_opts.disassembler_style) return -ENOMEM;
} if (objdump_path) {
annotate_opts.objdump_path = strdup(objdump_path); if (!annotate_opts.objdump_path) return -ENOMEM;
} if (addr2line_path) {
symbol_conf.addr2line_path = strdup(addr2line_path); if (!symbol_conf.addr2line_path) return -ENOMEM;
}
if (annotate_check_args() < 0) return -EINVAL;
#ifdef HAVE_GTK2_SUPPORT if (symbol_conf.show_nr_samples && annotate.use_gtk) {
pr_err("--show-nr-samples is not available in --gtk mode at this time\n"); return ret;
} #endif
#ifndef HAVE_LIBDW_SUPPORT if (annotate.data_type) {
pr_err("Error: Data type profiling is disabled due to missing DWARF support\n"); return -ENOTSUP;
} #endif
ret = symbol__validate_sym_arguments(); if (ret) return ret;
if (!annotate.use_stdio) {
pr_err("--code-with-type only works with --stdio.\n"); goto out_delete;
}
}
setup_browser(true);
/* * Events of different processes may correspond to the same * symbol, we do not care about the processes in annotate, * set sort order to avoid repeated output.
*/ if (annotate.data_type)
sort_order = "dso,type"; else
sort_order = "dso,symbol";
/* * Set SORT_MODE__BRANCH so that annotate displays IPC/Cycle and * branch counters, if the corresponding branch info is available * in the perf data in the TUI mode.
*/ if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
sort__mode = SORT_MODE__BRANCH; if (annotate.session->evlist->nr_br_cntr > 0)
annotate_opts.show_br_cntr = true;
}
if (setup_sorting(/*evlist=*/NULL, perf_session__env(annotate.session)) < 0)
usage_with_options(annotate_usage, options);
ret = __cmd_annotate(&annotate);
out_delete: /* * Speed up the exit process by only deleting for debug builds. For * large files this can save time.
*/ #ifndef NDEBUG
perf_session__delete(annotate.session); #endif
annotation_options__exit();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.