^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * builtin-annotate.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Builtin annotate command: Analyze the perf.data input file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * look up and read DSOs and symbol information and display
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * a histogram of results, along various sorting keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "util/color.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "util/cache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "util/symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "perf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "util/evlist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "util/evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "util/annotate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "util/event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "util/parse-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "util/sort.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "util/hist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "util/dso.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "util/machine.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "util/map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "util/session.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "util/tool.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "util/data.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "arch/common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "util/block-range.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "util/map_symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include "util/branch.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <dlfcn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/bitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct perf_annotate {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct perf_tool tool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct perf_session *session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct annotation_options opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) bool use_tui, use_stdio, use_stdio2, use_gtk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) bool skip_missing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) bool has_br_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) bool group_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) const char *sym_hist_filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) const char *cpu_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Given one basic block:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * from to branch_i
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * * ----> *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * | block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * v
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * * ----> *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * from to branch_i+1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * where the horizontal are the branches and the vertical is the executed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * block of instructions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * We count, for each 'instruction', the number of blocks that covered it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * well as count the ratio each branch is taken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * We can do this without knowing the actual instruction stream by keeping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * track of the address ranges. We break down ranges such that there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * overlap and iterate from the start until the end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @acme: once we parse the objdump output _before_ processing the samples,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * we can easily fold the branch.cycles IPC bits in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static void process_basic_block(struct addr_map_symbol *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct addr_map_symbol *end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct branch_flags *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct symbol *sym = start->ms.sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct block_range_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct block_range *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Sanity; NULL isn't executable and the CPU cannot execute backwards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!start->addr || start->addr > end->addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) iter = block_range__create(start->addr, end->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!block_range_iter__valid(&iter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * First block in range is a branch target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) entry = block_range_iter(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) assert(entry->is_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) entry->entry++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) entry = block_range_iter(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) entry->coverage++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) entry->sym = sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (notes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) notes->max_coverage = max(notes->max_coverage, entry->coverage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) } while (block_range_iter__next(&iter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Last block in rage is a branch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) entry = block_range_iter(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) assert(entry->is_branch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) entry->taken++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (flags->predicted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) entry->pred++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct perf_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct addr_map_symbol *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct branch_info *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!bs || !bs->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) bi = sample__resolve_bstack(sample, al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!bi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) for (i = bs->nr - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * XXX filter against symbol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) process_basic_block(prev, &bi[i].from, &bi[i].flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) prev = &bi[i].to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) free(bi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int hist_iter__branch_callback(struct hist_entry_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct addr_location *al __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) bool single __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) void *arg __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct hist_entry *he = iter->he;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct branch_info *bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct perf_sample *sample = iter->sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct evsel *evsel = iter->evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) bi = he->branch_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int process_branch_callback(struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct addr_location *al __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct perf_annotate *ann,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct hist_entry_iter iter = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .evsel = evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .sample = sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .add_entry_cb = hist_iter__branch_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .hide_unresolved = symbol_conf.hide_unresolved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .ops = &hist_iter_branch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct addr_location a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (machine__resolve(machine, &a, sample) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (a.sym == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (a.map != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) a.map->dso->hit = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) hist__account_cycles(sample->branch_stack, al, sample, false, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static bool has_annotation(struct perf_annotate *ann)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return ui__has_annotation() || ann->use_stdio2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct addr_location *al, struct perf_annotate *ann,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct hists *hists = evsel__hists(evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct hist_entry *he;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if ((!ann->has_br_stack || !has_annotation(ann)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ann->sym_hist_filter != NULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) (al->sym == NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /* We're only interested in a symbol named sym_hist_filter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * FIXME: why isn't this done in the symbol_filter when loading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * the DSO?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (al->sym != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) rb_erase_cached(&al->sym->rb_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) &al->map->dso->symbols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) symbol__delete(al->sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) dso__reset_find_symbol_cache(al->map->dso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * XXX filtered samples can still have branch entires pointing into our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * symbol and are missed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) process_branch_stack(sample->branch_stack, al, sample);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (ann->has_br_stack && has_annotation(ann))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return process_branch_callback(evsel, sample, al, ann, machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (he == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) hists__inc_nr_samples(hists, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int process_sample_event(struct perf_tool *tool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (machine__resolve(machine, &al, sample) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) pr_warning("problem processing %d event, skipping it.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) event->header.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!al.filtered &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) evsel__add_sample(evsel, sample, &al, ann, machine)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) pr_warning("problem incrementing symbol count, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) "skipping event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) addr_location__put(&al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int process_feature_event(struct perf_session *session,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (event->feat.feat_id < HEADER_LAST_FEATURE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return perf_event__process_feature(session, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static int hist_entry__tty_annotate(struct hist_entry *he,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct perf_annotate *ann)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!ann->use_stdio2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return symbol__tty_annotate(&he->ms, evsel, &ann->opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return symbol__tty_annotate2(&he->ms, evsel, &ann->opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static void hists__find_annotations(struct hists *hists,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct perf_annotate *ann)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct rb_node *nd = rb_first_cached(&hists->entries), *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) int key = K_RIGHT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) while (nd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct annotation *notes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) goto find_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (ann->sym_hist_filter &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) goto find_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) notes = symbol__annotation(he->ms.sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (notes->src == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) find_next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (key == K_LEFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) nd = rb_prev(nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) nd = rb_next(nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (use_browser == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int (*annotate)(struct hist_entry *he,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct evsel *evsel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct hist_browser_timer *hbt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) annotate = dlsym(perf_gtk_handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) "hist_entry__gtk_annotate");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (annotate == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) ui__error("GTK browser not found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ret = annotate(he, evsel, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (!ret || !ann->skip_missing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* skip missing symbols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) nd = rb_next(nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) } else if (use_browser == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) key = hist_entry__tui_annotate(he, evsel, NULL, &ann->opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) switch (key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) case -1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (!ann->skip_missing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) case K_RIGHT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) next = rb_next(nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) case K_LEFT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) next = rb_prev(nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (next != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) nd = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) hist_entry__tty_annotate(he, evsel, ann);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) nd = rb_next(nd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * Since we have a hist_entry per IP for the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * symbol, free he->ms.sym->src to signal we already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * processed this symbol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) zfree(¬es->src->cycles_hist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) zfree(¬es->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static int __cmd_annotate(struct perf_annotate *ann)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) struct perf_session *session = ann->session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) struct evsel *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) u64 total_nr_samples;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (ann->cpu_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ret = perf_session__cpu_bitmap(session, ann->cpu_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) ann->cpu_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (!ann->opts.objdump_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ret = perf_env__lookup_objdump(&session->header.env,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) &ann->opts.objdump_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ret = perf_session__process_events(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (dump_trace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) perf_session__fprintf_nr_events(session, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) perf_evlist__fprintf_nr_events(session->evlist, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (verbose > 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) perf_session__fprintf(session, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (verbose > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) perf_session__fprintf_dsos(session, stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) total_nr_samples = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) evlist__for_each_entry(session->evlist, pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct hists *hists = evsel__hists(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (nr_samples > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) total_nr_samples += nr_samples;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) hists__collapse_resort(hists, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /* Don't sort callchain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) evsel__reset_sample_bit(pos, CALLCHAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) evsel__output_resort(pos, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (symbol_conf.event_group && !evsel__is_group_leader(pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) hists__find_annotations(hists, pos, ann);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (total_nr_samples == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) ui__error("The %s data has no samples!\n", session->data->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (use_browser == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) void (*show_annotations)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) show_annotations = dlsym(perf_gtk_handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) "perf_gtk__show_annotations");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (show_annotations == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ui__error("GTK browser not found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) show_annotations();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static const char * const annotate_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) "perf annotate [<options>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) int cmd_annotate(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct perf_annotate annotate = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) .tool = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) .sample = process_sample_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) .mmap = perf_event__process_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) .mmap2 = perf_event__process_mmap2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) .comm = perf_event__process_comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) .exit = perf_event__process_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) .fork = perf_event__process_fork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) .namespaces = perf_event__process_namespaces,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) .attr = perf_event__process_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) .build_id = perf_event__process_build_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) .tracing_data = perf_event__process_tracing_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) .feature = process_feature_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) .ordered_events = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) .ordering_requires_timestamps = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) .opts = annotation__default_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) struct perf_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) .mode = PERF_DATA_MODE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct option options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) OPT_STRING('i', "input", &input_name, "file",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) "input file name"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) "only consider symbols in these dsos"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) "symbol to annotate"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) OPT_INCR('v', "verbose", &verbose,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) "be more verbose (show symbol address, etc)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) "dump raw trace in ASCII"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) "don't load vmlinux even if found"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) "file", "vmlinux pathname"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) "load module symbols - WARNING: use only with -k and LIVE kernel"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) OPT_BOOLEAN('l', "print-line", &annotate.opts.print_lines,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) "print matching source lines (may be slow)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) OPT_BOOLEAN('P', "full-paths", &annotate.opts.full_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) "Don't shorten the displayed pathnames"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) "Skip symbols that cannot be annotated"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) &annotate.group_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) "Show event group information together"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) OPT_CALLBACK(0, "symfs", NULL, "directory",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) "Look for files with symbols relative to this directory",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) symbol__config_symfs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) OPT_BOOLEAN(0, "source", &annotate.opts.annotate_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) "Interleave source code with assembly code (default)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) OPT_BOOLEAN(0, "asm-raw", &annotate.opts.show_asm_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) "Display raw encoding of assembly instructions (default)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) OPT_STRING('M', "disassembler-style", &annotate.opts.disassembler_style, "disassembler style",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) "Specify disassembler style (e.g. -M intel for intel syntax)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) OPT_STRING(0, "prefix", &annotate.opts.prefix, "prefix",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) "Add prefix to source file path names in programs (with --prefix-strip)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) OPT_STRING(0, "prefix-strip", &annotate.opts.prefix_strip, "N",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) "Strip first N entries of source file path name in programs (with --prefix)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) OPT_STRING(0, "objdump", &annotate.opts.objdump_path, "path",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) "objdump binary to use for disassembly and annotations"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) "Show event group information together"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) "Show a column with the sum of periods"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) "Show a column with the number of samples"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) stdio__config_color, "always"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) OPT_CALLBACK(0, "percent-type", &annotate.opts, "local-period",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) "Set percent type local/global-period/hits",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) annotate_parse_percent_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) ret = hists__init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) annotation_config__init(&annotate.opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) argc = parse_options(argc, argv, options, annotate_usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * Special case: if there's an argument left then assume that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * it's a symbol filter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (argc > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) usage_with_options(annotate_usage, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) annotate.sym_hist_filter = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (annotate_check_args(&annotate.opts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (symbol_conf.show_nr_samples && annotate.use_gtk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) perf_quiet_option();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) data.path = input_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) annotate.session = perf_session__new(&data, false, &annotate.tool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (IS_ERR(annotate.session))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return PTR_ERR(annotate.session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) HEADER_BRANCH_STACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (annotate.group_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) perf_evlist__force_leader(annotate.session->evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) ret = symbol__annotation_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) symbol_conf.try_vmlinux_path = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) ret = symbol__init(&annotate.session->header.env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (annotate.use_stdio || annotate.use_stdio2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) use_browser = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) else if (annotate.use_tui)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) use_browser = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) else if (annotate.use_gtk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) use_browser = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) setup_browser(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) sort__mode = SORT_MODE__BRANCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (setup_sorting(annotate.session->evlist) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) usage_with_options(annotate_usage, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) if (setup_sorting(NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) usage_with_options(annotate_usage, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) ret = __cmd_annotate(&annotate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) out_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * Speed up the exit process, for large files this can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * take quite a while.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * XXX Enable this when using valgrind or if we ever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * librarize this command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * Also experiment with obstacks to see how much speed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * up we'll get here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * perf_session__delete(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }