]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - tools/perf/builtin-annotate.c
perf tools: Pass tool context in the the perf_event_ops functions
[mirror_ubuntu-eoan-kernel.git] / tools / perf / builtin-annotate.c
CommitLineData
8035e428
IM
1/*
2 * builtin-annotate.c
3 *
4 * Builtin annotate command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
8#include "builtin.h"
9
78f7defe 10#include "util/util.h"
8035e428 11#include "util/color.h"
5da50258 12#include <linux/list.h>
8035e428 13#include "util/cache.h"
43cbcd8a 14#include <linux/rbtree.h>
8035e428 15#include "util/symbol.h"
8035e428
IM
16
17#include "perf.h"
8f28827a 18#include "util/debug.h"
8035e428 19
e248de33
ACM
20#include "util/evlist.h"
21#include "util/evsel.h"
78f7defe 22#include "util/annotate.h"
62daacb5 23#include "util/event.h"
8035e428
IM
24#include "util/parse-options.h"
25#include "util/parse-events.h"
6baa0a5a 26#include "util/thread.h"
dd68ada2 27#include "util/sort.h"
3d1d07ec 28#include "util/hist.h"
94c744b6 29#include "util/session.h"
8035e428 30
5d67be97
AB
31#include <linux/bitmap.h>
32
d20deb64
ACM
33struct perf_annotate {
34 struct perf_event_ops ops;
7009cc34
ACM
35 char const *input_name;
36 bool force, use_tui, use_stdio;
37 bool full_paths;
38 bool print_line;
39 const char *sym_hist_filter;
40 const char *cpu_list;
41 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
d20deb64 42};
5d67be97 43
d04b35f8
ACM
44static int perf_evsel__add_sample(struct perf_evsel *evsel,
45 struct perf_sample *sample,
d20deb64
ACM
46 struct addr_location *al,
47 struct perf_annotate *ann)
8035e428 48{
628ada0c 49 struct hist_entry *he;
e248de33 50 int ret;
628ada0c 51
7009cc34
ACM
52 if (ann->sym_hist_filter != NULL &&
53 (al->sym == NULL ||
54 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
628ada0c
ACM
55 /* We're only interested in a symbol named sym_hist_filter */
56 if (al->sym != NULL) {
57 rb_erase(&al->sym->rb_node,
58 &al->map->dso->symbols[al->map->type]);
59 symbol__delete(al->sym);
60 }
61 return 0;
62 }
63
e248de33 64 he = __hists__add_entry(&evsel->hists, al, NULL, 1);
9735abf1 65 if (he == NULL)
8035e428 66 return -ENOMEM;
628ada0c 67
e248de33 68 ret = 0;
2f525d01 69 if (he->ms.sym != NULL) {
2f525d01 70 struct annotation *notes = symbol__annotation(he->ms.sym);
d04b35f8 71 if (notes->src == NULL && symbol__alloc_hist(he->ms.sym) < 0)
2f525d01
ACM
72 return -ENOMEM;
73
e248de33 74 ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
2f525d01
ACM
75 }
76
e248de33
ACM
77 evsel->hists.stats.total_period += sample->period;
78 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
79 return ret;
8035e428
IM
80}
81
d20deb64
ACM
82static int process_sample_event(struct perf_event_ops *ops,
83 union perf_event *event,
8115d60c 84 struct perf_sample *sample,
9e69c210 85 struct perf_evsel *evsel,
640c03ce 86 struct perf_session *session)
8035e428 87{
d20deb64 88 struct perf_annotate *ann = container_of(ops, struct perf_annotate, ops);
1ed091c4 89 struct addr_location al;
6baa0a5a 90
ce6f4fab
ACM
91 if (perf_event__preprocess_sample(event, session, &al, sample,
92 symbol__annotate_init) < 0) {
29a9f66d
ACM
93 pr_warning("problem processing %d event, skipping it.\n",
94 event->header.type);
8035e428
IM
95 return -1;
96 }
97
7009cc34 98 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
5d67be97
AB
99 return 0;
100
d20deb64 101 if (!al.filtered && perf_evsel__add_sample(evsel, sample, &al, ann)) {
29a9f66d
ACM
102 pr_warning("problem incrementing symbol count, "
103 "skipping event\n");
ec218fc4 104 return -1;
8035e428 105 }
8035e428
IM
106
107 return 0;
108}
109
d20deb64
ACM
110static int hist_entry__tty_annotate(struct hist_entry *he, int evidx,
111 struct perf_annotate *ann)
0b73da3f 112{
2f525d01 113 return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx,
7009cc34 114 ann->print_line, ann->full_paths, 0, 0);
0b73da3f
IM
115}
116
d20deb64
ACM
117static void hists__find_annotations(struct hists *self, int evidx,
118 struct perf_annotate *ann)
0b73da3f 119{
b50e003d 120 struct rb_node *nd = rb_first(&self->entries), *next;
cf958003 121 int key = K_RIGHT;
0b73da3f 122
46e3e055 123 while (nd) {
ed52ce2e 124 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
78f7defe 125 struct annotation *notes;
0b73da3f 126
46e3e055
ACM
127 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
128 goto find_next;
0b73da3f 129
78f7defe 130 notes = symbol__annotation(he->ms.sym);
ce6f4fab 131 if (notes->src == NULL) {
46e3e055 132find_next:
cf958003 133 if (key == K_LEFT)
46e3e055
ACM
134 nd = rb_prev(nd);
135 else
136 nd = rb_next(nd);
e4204992 137 continue;
46e3e055 138 }
e4204992 139
62e3436b 140 if (use_browser > 0) {
d04b35f8 141 key = hist_entry__tui_annotate(he, evidx, NULL, NULL, 0);
46e3e055 142 switch (key) {
cf958003 143 case K_RIGHT:
b50e003d 144 next = rb_next(nd);
46e3e055 145 break;
cf958003 146 case K_LEFT:
b50e003d 147 next = rb_prev(nd);
46e3e055 148 break;
b50e003d
ACM
149 default:
150 return;
46e3e055 151 }
b50e003d
ACM
152
153 if (next != NULL)
154 nd = next;
46e3e055 155 } else {
d20deb64 156 hist_entry__tty_annotate(he, evidx, ann);
46e3e055
ACM
157 nd = rb_next(nd);
158 /*
159 * Since we have a hist_entry per IP for the same
ce6f4fab 160 * symbol, free he->ms.sym->src to signal we already
46e3e055
ACM
161 * processed this symbol.
162 */
ce6f4fab
ACM
163 free(notes->src);
164 notes->src = NULL;
46e3e055 165 }
0b73da3f 166 }
0b73da3f
IM
167}
168
d20deb64 169static int __cmd_annotate(struct perf_annotate *ann)
8035e428 170{
bab81b62 171 int ret;
75be6cf4 172 struct perf_session *session;
e248de33
ACM
173 struct perf_evsel *pos;
174 u64 total_nr_samples;
8035e428 175
7009cc34 176 session = perf_session__new(ann->input_name, O_RDONLY,
d20deb64 177 ann->force, false, &ann->ops);
94c744b6
ACM
178 if (session == NULL)
179 return -ENOMEM;
180
7009cc34
ACM
181 if (ann->cpu_list) {
182 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
183 ann->cpu_bitmap);
5d67be97
AB
184 if (ret)
185 goto out_delete;
186 }
187
d20deb64 188 ret = perf_session__process_events(session, &ann->ops);
bab81b62 189 if (ret)
94c744b6 190 goto out_delete;
8035e428 191
62daacb5 192 if (dump_trace) {
c8446b9b 193 perf_session__fprintf_nr_events(session, stdout);
94c744b6 194 goto out_delete;
62daacb5 195 }
8035e428 196
da21d1b5 197 if (verbose > 3)
b3165f41 198 perf_session__fprintf(session, stdout);
8035e428 199
da21d1b5 200 if (verbose > 2)
cbf69680 201 perf_session__fprintf_dsos(session, stdout);
8035e428 202
e248de33
ACM
203 total_nr_samples = 0;
204 list_for_each_entry(pos, &session->evlist->entries, node) {
205 struct hists *hists = &pos->hists;
206 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
207
208 if (nr_samples > 0) {
209 total_nr_samples += nr_samples;
210 hists__collapse_resort(hists);
211 hists__output_resort(hists);
d20deb64 212 hists__find_annotations(hists, pos->idx, ann);
e248de33
ACM
213 }
214 }
8035e428 215
e248de33 216 if (total_nr_samples == 0) {
7009cc34 217 ui__warning("The %s file has no samples!\n", ann->input_name);
e248de33
ACM
218 goto out_delete;
219 }
220out_delete:
221 /*
222 * Speed up the exit process, for large files this can
223 * take quite a while.
224 *
225 * XXX Enable this when using valgrind or if we ever
226 * librarize this command.
227 *
228 * Also experiment with obstacks to see how much speed
229 * up we'll get here.
230 *
231 * perf_session__delete(session);
232 */
bab81b62 233 return ret;
8035e428
IM
234}
235
236static const char * const annotate_usage[] = {
237 "perf annotate [<options>] <command>",
238 NULL
239};
240
d20deb64
ACM
241int cmd_annotate(int argc, const char **argv, const char *prefix __used)
242{
243 struct perf_annotate annotate = {
244 .ops = {
245 .sample = process_sample_event,
246 .mmap = perf_event__process_mmap,
247 .comm = perf_event__process_comm,
248 .fork = perf_event__process_task,
249 .ordered_samples = true,
250 .ordering_requires_timestamps = true,
251 },
252 .input_name = "perf.data",
253 };
254 const struct option options[] = {
7009cc34 255 OPT_STRING('i', "input", &annotate.input_name, "file",
8035e428 256 "input file name"),
ac73c5a9
ACM
257 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
258 "only consider symbols in these dsos"),
7009cc34 259 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
0b73da3f 260 "symbol to annotate"),
7009cc34 261 OPT_BOOLEAN('f', "force", &annotate.force, "don't complain, do it"),
c0555642 262 OPT_INCR('v', "verbose", &verbose,
8035e428
IM
263 "be more verbose (show symbol address, etc)"),
264 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
265 "dump raw trace in ASCII"),
7009cc34
ACM
266 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
267 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
b32d133a
ACM
268 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
269 "file", "vmlinux pathname"),
270 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 271 "load module symbols - WARNING: use only with -k and LIVE kernel"),
7009cc34 272 OPT_BOOLEAN('l', "print-line", &annotate.print_line,
301406b9 273 "print matching source lines (may be slow)"),
7009cc34 274 OPT_BOOLEAN('P', "full-paths", &annotate.full_paths,
42976487 275 "Don't shorten the displayed pathnames"),
7009cc34 276 OPT_STRING('c', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
e71a0598
SE
277 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
278 "Look for files with symbols relative to this directory"),
64c6f0c7 279 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
3e6a2a7f 280 "Interleave source code with assembly code (default)"),
64c6f0c7 281 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
3e6a2a7f 282 "Display raw encoding of assembly instructions (default)"),
f69b64f7
AK
283 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
284 "Specify disassembler style (e.g. -M intel for intel syntax)"),
8035e428 285 OPT_END()
d20deb64 286 };
8035e428 287
655000e7
ACM
288 argc = parse_options(argc, argv, options, annotate_usage, 0);
289
7009cc34 290 if (annotate.use_stdio)
8b9e74eb 291 use_browser = 0;
7009cc34 292 else if (annotate.use_tui)
8b9e74eb
ACM
293 use_browser = 1;
294
229ade9b 295 setup_browser(true);
46e3e055 296
78f7defe 297 symbol_conf.priv_size = sizeof(struct annotation);
75be6cf4
ACM
298 symbol_conf.try_vmlinux_path = true;
299
300 if (symbol__init() < 0)
b32d133a 301 return -1;
8035e428 302
c8829c7a 303 setup_sorting(annotate_usage, options);
8035e428 304
0b73da3f
IM
305 if (argc) {
306 /*
307 * Special case: if there's an argument left then assume tha
308 * it's a symbol filter:
309 */
310 if (argc > 1)
311 usage_with_options(annotate_usage, options);
312
7009cc34 313 annotate.sym_hist_filter = argv[0];
0b73da3f
IM
314 }
315
dd68ada2 316 if (field_sep && *field_sep == '.') {
29a9f66d
ACM
317 pr_err("'.' is the only non valid --field-separator argument\n");
318 return -1;
dd68ada2
JK
319 }
320
d20deb64 321 return __cmd_annotate(&annotate);
8035e428 322}