Merge tag 'zonefs-6.5-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/dlemoal...
[linux-2.6-block.git] / tools / perf / builtin-annotate.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
8035e428
IM
2/*
3 * builtin-annotate.c
4 *
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.
8 */
9#include "builtin.h"
10
8035e428 11#include "util/color.h"
5da50258 12#include <linux/list.h>
8035e428 13#include "util/cache.h"
43cbcd8a 14#include <linux/rbtree.h>
7f7c536f 15#include <linux/zalloc.h>
8035e428 16#include "util/symbol.h"
8035e428 17
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"
4b6ab94e 24#include <subcmd/parse-options.h>
8035e428 25#include "util/parse-events.h"
dd68ada2 26#include "util/sort.h"
3d1d07ec 27#include "util/hist.h"
4a3cec84 28#include "util/dso.h"
3f79132a 29#include "util/machine.h"
1101f69a 30#include "util/map.h"
94c744b6 31#include "util/session.h"
45694aa7 32#include "util/tool.h"
f5fc1412 33#include "util/data.h"
68e94f4e 34#include "arch/common.h"
70fbe057 35#include "util/block-range.h"
d3300a3c
ACM
36#include "util/map_symbol.h"
37#include "util/branch.h"
f12ad272 38#include "util/util.h"
8035e428 39
fc67297b 40#include <dlfcn.h>
a43783ae 41#include <errno.h>
5d67be97 42#include <linux/bitmap.h>
6ef81c55 43#include <linux/err.h>
5d67be97 44
d20deb64 45struct perf_annotate {
45694aa7 46 struct perf_tool tool;
fa10f316 47 struct perf_session *session;
982d410b 48 struct annotation_options opts;
3402ae0a
IR
49#ifdef HAVE_SLANG_SUPPORT
50 bool use_tui;
51#endif
52 bool use_stdio, use_stdio2;
557cc18e 53#ifdef HAVE_GTK2_SUPPORT
3402ae0a 54 bool use_gtk;
557cc18e 55#endif
18c9e5c5 56 bool skip_missing;
bb848c14 57 bool has_br_stack;
7ebaf489 58 bool group_set;
cad10ce3 59 float min_percent;
7009cc34
ACM
60 const char *sym_hist_filter;
61 const char *cpu_list;
62 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
d20deb64 63};
5d67be97 64
70fbe057
PZ
65/*
66 * Given one basic block:
67 *
68 * from to branch_i
69 * * ----> *
70 * |
71 * | block
72 * v
73 * * ----> *
74 * from to branch_i+1
75 *
76 * where the horizontal are the branches and the vertical is the executed
77 * block of instructions.
78 *
79 * We count, for each 'instruction', the number of blocks that covered it as
80 * well as count the ratio each branch is taken.
81 *
82 * We can do this without knowing the actual instruction stream by keeping
83 * track of the address ranges. We break down ranges such that there is no
84 * overlap and iterate from the start until the end.
85 *
86 * @acme: once we parse the objdump output _before_ processing the samples,
87 * we can easily fold the branch.cycles IPC bits in.
88 */
89static void process_basic_block(struct addr_map_symbol *start,
90 struct addr_map_symbol *end,
91 struct branch_flags *flags)
92{
d46a4cdf 93 struct symbol *sym = start->ms.sym;
70fbe057
PZ
94 struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
95 struct block_range_iter iter;
96 struct block_range *entry;
97
98 /*
99 * Sanity; NULL isn't executable and the CPU cannot execute backwards
100 */
101 if (!start->addr || start->addr > end->addr)
102 return;
103
104 iter = block_range__create(start->addr, end->addr);
105 if (!block_range_iter__valid(&iter))
106 return;
107
108 /*
109 * First block in range is a branch target.
110 */
111 entry = block_range_iter(&iter);
112 assert(entry->is_target);
113 entry->entry++;
114
115 do {
116 entry = block_range_iter(&iter);
117
118 entry->coverage++;
119 entry->sym = sym;
120
121 if (notes)
122 notes->max_coverage = max(notes->max_coverage, entry->coverage);
123
124 } while (block_range_iter__next(&iter));
125
126 /*
127 * Last block in rage is a branch.
128 */
129 entry = block_range_iter(&iter);
130 assert(entry->is_branch);
131 entry->taken++;
132 if (flags->predicted)
133 entry->pred++;
134}
135
136static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
137 struct perf_sample *sample)
138{
139 struct addr_map_symbol *prev = NULL;
140 struct branch_info *bi;
141 int i;
142
143 if (!bs || !bs->nr)
144 return;
145
146 bi = sample__resolve_bstack(sample, al);
147 if (!bi)
148 return;
149
150 for (i = bs->nr - 1; i >= 0; i--) {
151 /*
152 * XXX filter against symbol
153 */
154 if (prev)
155 process_basic_block(prev, &bi[i].from, &bi[i].flags);
156 prev = &bi[i].to;
157 }
158
159 free(bi);
160}
161
bb848c14
JY
162static int hist_iter__branch_callback(struct hist_entry_iter *iter,
163 struct addr_location *al __maybe_unused,
164 bool single __maybe_unused,
165 void *arg __maybe_unused)
166{
167 struct hist_entry *he = iter->he;
168 struct branch_info *bi;
169 struct perf_sample *sample = iter->sample;
32dcd021 170 struct evsel *evsel = iter->evsel;
bb848c14
JY
171 int err;
172
bb848c14 173 bi = he->branch_info;
e345f3bd 174 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
bb848c14
JY
175
176 if (err)
177 goto out;
178
e345f3bd 179 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
bb848c14
JY
180
181out:
182 return err;
183}
184
32dcd021 185static int process_branch_callback(struct evsel *evsel,
bb848c14 186 struct perf_sample *sample,
0dd5041c 187 struct addr_location *al,
bb848c14
JY
188 struct perf_annotate *ann,
189 struct machine *machine)
190{
191 struct hist_entry_iter iter = {
192 .evsel = evsel,
193 .sample = sample,
194 .add_entry_cb = hist_iter__branch_callback,
195 .hide_unresolved = symbol_conf.hide_unresolved,
196 .ops = &hist_iter_branch,
197 };
bb848c14 198 struct addr_location a;
0dd5041c 199 int ret;
bb848c14 200
0dd5041c
IR
201 addr_location__init(&a);
202 if (machine__resolve(machine, &a, sample) < 0) {
203 ret = -1;
204 goto out;
205 }
bb848c14 206
0dd5041c
IR
207 if (a.sym == NULL) {
208 ret = 0;
209 goto out;
210 }
bb848c14
JY
211
212 if (a.map != NULL)
63df0e4b 213 map__dso(a.map)->hit = 1;
bb848c14 214
7841f40a 215 hist__account_cycles(sample->branch_stack, al, sample, false, NULL);
bdd1666b 216
0dd5041c
IR
217 ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
218out:
219 addr_location__exit(&a);
220 return ret;
bb848c14
JY
221}
222
befd2a38
ACM
223static bool has_annotation(struct perf_annotate *ann)
224{
225 return ui__has_annotation() || ann->use_stdio2;
226}
227
74aa90e8
ACM
228static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
229 struct addr_location *al, struct perf_annotate *ann,
230 struct machine *machine)
8035e428 231{
4ea062ed 232 struct hists *hists = evsel__hists(evsel);
628ada0c 233 struct hist_entry *he;
e248de33 234 int ret;
628ada0c 235
befd2a38 236 if ((!ann->has_br_stack || !has_annotation(ann)) &&
bb848c14 237 ann->sym_hist_filter != NULL &&
7009cc34
ACM
238 (al->sym == NULL ||
239 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
628ada0c 240 /* We're only interested in a symbol named sym_hist_filter */
facf3f06
ACM
241 /*
242 * FIXME: why isn't this done in the symbol_filter when loading
243 * the DSO?
244 */
628ada0c 245 if (al->sym != NULL) {
63df0e4b
IR
246 struct dso *dso = map__dso(al->map);
247
248 rb_erase_cached(&al->sym->rb_node, &dso->symbols);
628ada0c 249 symbol__delete(al->sym);
63df0e4b 250 dso__reset_find_symbol_cache(dso);
628ada0c
ACM
251 }
252 return 0;
253 }
254
70fbe057 255 /*
4d39c89f 256 * XXX filtered samples can still have branch entries pointing into our
70fbe057
PZ
257 * symbol and are missed.
258 */
259 process_branch_stack(sample->branch_stack, al, sample);
260
befd2a38 261 if (ann->has_br_stack && has_annotation(ann))
bb848c14
JY
262 return process_branch_callback(evsel, sample, al, ann, machine);
263
ebf39d29 264 he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true);
9735abf1 265 if (he == NULL)
8035e428 266 return -ENOMEM;
628ada0c 267
e345f3bd 268 ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
4ea062ed 269 hists__inc_nr_samples(hists, true);
e248de33 270 return ret;
8035e428
IM
271}
272
45694aa7 273static int process_sample_event(struct perf_tool *tool,
d20deb64 274 union perf_event *event,
8115d60c 275 struct perf_sample *sample,
32dcd021 276 struct evsel *evsel,
743eb868 277 struct machine *machine)
8035e428 278{
45694aa7 279 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
1ed091c4 280 struct addr_location al;
b91fc39f 281 int ret = 0;
6baa0a5a 282
0dd5041c 283 addr_location__init(&al);
bb3eb566 284 if (machine__resolve(machine, &al, sample) < 0) {
29a9f66d
ACM
285 pr_warning("problem processing %d event, skipping it.\n",
286 event->header.type);
0dd5041c
IR
287 ret = -1;
288 goto out_put;
8035e428
IM
289 }
290
7009cc34 291 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
b91fc39f 292 goto out_put;
5d67be97 293
bb848c14 294 if (!al.filtered &&
74aa90e8 295 evsel__add_sample(evsel, sample, &al, ann, machine)) {
29a9f66d
ACM
296 pr_warning("problem incrementing symbol count, "
297 "skipping event\n");
b91fc39f 298 ret = -1;
8035e428 299 }
b91fc39f 300out_put:
0dd5041c 301 addr_location__exit(&al);
b91fc39f 302 return ret;
8035e428
IM
303}
304
89f1688a
JO
305static int process_feature_event(struct perf_session *session,
306 union perf_event *event)
92ead7ee
RB
307{
308 if (event->feat.feat_id < HEADER_LAST_FEATURE)
89f1688a 309 return perf_event__process_feature(session, event);
92ead7ee
RB
310 return 0;
311}
312
db8fd07a 313static int hist_entry__tty_annotate(struct hist_entry *he,
32dcd021 314 struct evsel *evsel,
d20deb64 315 struct perf_annotate *ann)
0b73da3f 316{
befd2a38 317 if (!ann->use_stdio2)
29754894 318 return symbol__tty_annotate(&he->ms, evsel, &ann->opts);
982d410b 319
29754894 320 return symbol__tty_annotate2(&he->ms, evsel, &ann->opts);
0b73da3f
IM
321}
322
c824c433 323static void hists__find_annotations(struct hists *hists,
32dcd021 324 struct evsel *evsel,
d20deb64 325 struct perf_annotate *ann)
0b73da3f 326{
2eb3d689 327 struct rb_node *nd = rb_first_cached(&hists->entries), *next;
cf958003 328 int key = K_RIGHT;
0b73da3f 329
46e3e055 330 while (nd) {
ed52ce2e 331 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
78f7defe 332 struct annotation *notes;
0b73da3f 333
63df0e4b 334 if (he->ms.sym == NULL || map__dso(he->ms.map)->annotate_warned)
46e3e055 335 goto find_next;
0b73da3f 336
bb848c14
JY
337 if (ann->sym_hist_filter &&
338 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
339 goto find_next;
340
cad10ce3
NK
341 if (ann->min_percent) {
342 float percent = 0;
343 u64 total = hists__total_period(hists);
344
345 if (total)
346 percent = 100.0 * he->stat.period / total;
347
348 if (percent < ann->min_percent)
349 goto find_next;
350 }
351
78f7defe 352 notes = symbol__annotation(he->ms.sym);
ce6f4fab 353 if (notes->src == NULL) {
46e3e055 354find_next:
6d491b37 355 if (key == K_LEFT || key == '<')
46e3e055
ACM
356 nd = rb_prev(nd);
357 else
358 nd = rb_next(nd);
e4204992 359 continue;
46e3e055 360 }
e4204992 361
2b676bf0 362 if (use_browser == 2) {
18c9e5c5 363 int ret;
fc67297b 364 int (*annotate)(struct hist_entry *he,
32dcd021 365 struct evsel *evsel,
217b7d41 366 struct annotation_options *options,
fc67297b
NK
367 struct hist_browser_timer *hbt);
368
369 annotate = dlsym(perf_gtk_handle,
370 "hist_entry__gtk_annotate");
371 if (annotate == NULL) {
372 ui__error("GTK browser not found!\n");
373 return;
374 }
18c9e5c5 375
217b7d41 376 ret = annotate(he, evsel, &ann->opts, NULL);
18c9e5c5
NK
377 if (!ret || !ann->skip_missing)
378 return;
379
380 /* skip missing symbols */
381 nd = rb_next(nd);
2b676bf0 382 } else if (use_browser == 1) {
cd0cccba 383 key = hist_entry__tui_annotate(he, evsel, NULL, &ann->opts);
bb848c14 384
46e3e055 385 switch (key) {
18c9e5c5
NK
386 case -1:
387 if (!ann->skip_missing)
388 return;
389 /* fall through */
cf958003 390 case K_RIGHT:
6d491b37 391 case '>':
b50e003d 392 next = rb_next(nd);
46e3e055 393 break;
cf958003 394 case K_LEFT:
6d491b37 395 case '<':
b50e003d 396 next = rb_prev(nd);
46e3e055 397 break;
b50e003d
ACM
398 default:
399 return;
46e3e055 400 }
b50e003d
ACM
401
402 if (next != NULL)
403 nd = next;
46e3e055 404 } else {
db8fd07a 405 hist_entry__tty_annotate(he, evsel, ann);
46e3e055 406 nd = rb_next(nd);
46e3e055 407 }
0b73da3f 408 }
0b73da3f
IM
409}
410
d20deb64 411static int __cmd_annotate(struct perf_annotate *ann)
8035e428 412{
bab81b62 413 int ret;
fa10f316 414 struct perf_session *session = ann->session;
32dcd021 415 struct evsel *pos;
e248de33 416 u64 total_nr_samples;
94c744b6 417
7009cc34
ACM
418 if (ann->cpu_list) {
419 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
420 ann->cpu_bitmap);
5d67be97 421 if (ret)
fa10f316 422 goto out;
5d67be97
AB
423 }
424
f178fd2d
ACM
425 if (!ann->opts.objdump_path) {
426 ret = perf_env__lookup_objdump(&session->header.env,
427 &ann->opts.objdump_path);
68e94f4e 428 if (ret)
fa10f316 429 goto out;
68e94f4e
IT
430 }
431
b7b61cbe 432 ret = perf_session__process_events(session);
bab81b62 433 if (ret)
fa10f316 434 goto out;
8035e428 435
62daacb5 436 if (dump_trace) {
2775de0b
NK
437 perf_session__fprintf_nr_events(session, stdout, false);
438 evlist__fprintf_nr_events(session->evlist, stdout, false);
fa10f316 439 goto out;
62daacb5 440 }
8035e428 441
da21d1b5 442 if (verbose > 3)
b3165f41 443 perf_session__fprintf(session, stdout);
8035e428 444
da21d1b5 445 if (verbose > 2)
cbf69680 446 perf_session__fprintf_dsos(session, stdout);
8035e428 447
e248de33 448 total_nr_samples = 0;
e5cadb93 449 evlist__for_each_entry(session->evlist, pos) {
4ea062ed 450 struct hists *hists = evsel__hists(pos);
0f0abbac 451 u32 nr_samples = hists->stats.nr_samples;
e248de33
ACM
452
453 if (nr_samples > 0) {
454 total_nr_samples += nr_samples;
c1fb5651 455 hists__collapse_resort(hists, NULL);
f9db0d0f 456 /* Don't sort callchain */
862b2f8f 457 evsel__reset_sample_bit(pos, CALLCHAIN);
10c513f7 458 evsel__output_resort(pos, NULL);
b1dd4432 459
c754c382 460 if (symbol_conf.event_group && !evsel__is_group_leader(pos))
b1dd4432
NK
461 continue;
462
db8fd07a 463 hists__find_annotations(hists, pos, ann);
e248de33
ACM
464 }
465 }
8035e428 466
e248de33 467 if (total_nr_samples == 0) {
2d4f2799 468 ui__error("The %s data has no samples!\n", session->data->path);
fa10f316 469 goto out;
e248de33 470 }
7a60ba94 471
fc67297b
NK
472 if (use_browser == 2) {
473 void (*show_annotations)(void);
474
475 show_annotations = dlsym(perf_gtk_handle,
476 "perf_gtk__show_annotations");
477 if (show_annotations == NULL) {
478 ui__error("GTK browser not found!\n");
fa10f316 479 goto out;
fc67297b
NK
480 }
481 show_annotations();
482 }
7a60ba94 483
fa10f316 484out:
bab81b62 485 return ret;
8035e428
IM
486}
487
cad10ce3
NK
488static int parse_percent_limit(const struct option *opt, const char *str,
489 int unset __maybe_unused)
490{
491 struct perf_annotate *ann = opt->value;
492 double pcnt = strtof(str, NULL);
493
494 ann->min_percent = pcnt;
495 return 0;
496}
497
8035e428 498static const char * const annotate_usage[] = {
99345254 499 "perf annotate [<options>]",
8035e428
IM
500 NULL
501};
502
b0ad8ea6 503int cmd_annotate(int argc, const char **argv)
d20deb64
ACM
504{
505 struct perf_annotate annotate = {
45694aa7 506 .tool = {
d20deb64
ACM
507 .sample = process_sample_event,
508 .mmap = perf_event__process_mmap,
5c5e854b 509 .mmap2 = perf_event__process_mmap2,
d20deb64 510 .comm = perf_event__process_comm,
ec4622f5 511 .exit = perf_event__process_exit,
f62d3f0f 512 .fork = perf_event__process_fork,
f3b3614a 513 .namespaces = perf_event__process_namespaces,
6ab11f3a
DCC
514 .attr = perf_event__process_attr,
515 .build_id = perf_event__process_build_id,
378ef0f5 516#ifdef HAVE_LIBTRACEEVENT
f4849599 517 .tracing_data = perf_event__process_tracing_data,
378ef0f5 518#endif
4bcbe438
YJ
519 .id_index = perf_event__process_id_index,
520 .auxtrace_info = perf_event__process_auxtrace_info,
521 .auxtrace = perf_event__process_auxtrace,
92ead7ee 522 .feature = process_feature_event,
0a8cb85c 523 .ordered_events = true,
d20deb64
ACM
524 .ordering_requires_timestamps = true,
525 },
d20deb64 526 };
8ceb41d7 527 struct perf_data data = {
fa10f316
NK
528 .mode = PERF_DATA_MODE_READ,
529 };
4bcbe438
YJ
530 struct itrace_synth_opts itrace_synth_opts = {
531 .set = 0,
532 };
57594454 533 const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
1ac39372 534 struct option options[] = {
70cb4e96 535 OPT_STRING('i', "input", &input_name, "file",
8035e428 536 "input file name"),
ac73c5a9
ACM
537 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
538 "only consider symbols in these dsos"),
7009cc34 539 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
0b73da3f 540 "symbol to annotate"),
8ceb41d7 541 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
c0555642 542 OPT_INCR('v', "verbose", &verbose,
8035e428 543 "be more verbose (show symbol address, etc)"),
a527c2c1 544 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"),
8035e428
IM
545 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
546 "dump raw trace in ASCII"),
557cc18e 547#ifdef HAVE_GTK2_SUPPORT
2b676bf0 548 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
557cc18e 549#endif
3402ae0a 550#ifdef HAVE_SLANG_SUPPORT
7009cc34 551 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
3402ae0a 552#endif
7009cc34 553 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
befd2a38 554 OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
be316409
ACM
555 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
556 "don't load vmlinux even if found"),
b32d133a
ACM
557 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
558 "file", "vmlinux pathname"),
559 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 560 "load module symbols - WARNING: use only with -k and LIVE kernel"),
982d410b 561 OPT_BOOLEAN('l', "print-line", &annotate.opts.print_lines,
301406b9 562 "print matching source lines (may be slow)"),
982d410b 563 OPT_BOOLEAN('P', "full-paths", &annotate.opts.full_path,
42976487 564 "Don't shorten the displayed pathnames"),
18c9e5c5
NK
565 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
566 "Skip symbols that cannot be annotated"),
7ebaf489
JY
567 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
568 &annotate.group_set,
569 "Show event group information together"),
c8e66720 570 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
a7066709
HK
571 OPT_CALLBACK(0, "symfs", NULL, "directory",
572 "Look for files with symbols relative to this directory",
573 symbol__config_symfs),
1eddd9e4 574 OPT_BOOLEAN(0, "source", &annotate.opts.annotate_src,
3e6a2a7f 575 "Interleave source code with assembly code (default)"),
1eddd9e4 576 OPT_BOOLEAN(0, "asm-raw", &annotate.opts.show_asm_raw,
3e6a2a7f 577 "Display raw encoding of assembly instructions (default)"),
56d9117c 578 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
f69b64f7 579 "Specify disassembler style (e.g. -M intel for intel syntax)"),
3b0b16bf
AK
580 OPT_STRING(0, "prefix", &annotate.opts.prefix, "prefix",
581 "Add prefix to source file path names in programs (with --prefix-strip)"),
582 OPT_STRING(0, "prefix-strip", &annotate.opts.prefix_strip, "N",
583 "Strip first N entries of source file path name in programs (with --prefix)"),
56d9117c 584 OPT_STRING(0, "objdump", &objdump_path, "path",
7a4ec938 585 "objdump binary to use for disassembly and annotations"),
57594454
IR
586 OPT_STRING(0, "addr2line", &addr2line_path, "path",
587 "addr2line binary to use for line numbers"),
3406ac53
ML
588 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
589 "Enable symbol demangling"),
590 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
591 "Enable kernel symbol demangling"),
b1dd4432
NK
592 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
593 "Show event group information together"),
0c4a5bce
ML
594 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
595 "Show a column with the sum of periods"),
1ac39372
TS
596 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
597 "Show a column with the number of samples"),
53fe4ba1
ACM
598 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
599 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
600 stdio__config_color, "always"),
88c21190
JO
601 OPT_CALLBACK(0, "percent-type", &annotate.opts, "local-period",
602 "Set percent type local/global-period/hits",
603 annotate_parse_percent_type),
cad10ce3
NK
604 OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
605 "Don't show entries under that percent", parse_percent_limit),
4bcbe438
YJ
606 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
607 "Instruction Tracing options\n" ITRACE_HELP,
608 itrace_parse_synth_opts),
88c21190 609
8035e428 610 OPT_END()
d20deb64 611 };
1ac39372
TS
612 int ret;
613
614 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
615 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
616
217b7d41 617 annotation_options__init(&annotate.opts);
a635fc51 618
1ac39372 619 ret = hists__init();
a635fc51
ACM
620 if (ret < 0)
621 return ret;
8035e428 622
812b0f52
RB
623 annotation_config__init(&annotate.opts);
624
655000e7 625 argc = parse_options(argc, argv, options, annotate_usage, 0);
50e19ef9
NK
626 if (argc) {
627 /*
628 * Special case: if there's an argument left then assume that
629 * it's a symbol filter:
630 */
631 if (argc > 1)
632 usage_with_options(annotate_usage, options);
633
634 annotate.sym_hist_filter = argv[0];
635 }
655000e7 636
56d9117c
IR
637 if (disassembler_style) {
638 annotate.opts.disassembler_style = strdup(disassembler_style);
639 if (!annotate.opts.disassembler_style)
640 return -ENOMEM;
641 }
642 if (objdump_path) {
643 annotate.opts.objdump_path = strdup(objdump_path);
644 if (!annotate.opts.objdump_path)
645 return -ENOMEM;
646 }
57594454
IR
647 if (addr2line_path) {
648 symbol_conf.addr2line_path = strdup(addr2line_path);
649 if (!symbol_conf.addr2line_path)
650 return -ENOMEM;
651 }
56d9117c 652
3b0b16bf
AK
653 if (annotate_check_args(&annotate.opts) < 0)
654 return -EINVAL;
655
557cc18e 656#ifdef HAVE_GTK2_SUPPORT
9cef4b0b
TS
657 if (symbol_conf.show_nr_samples && annotate.use_gtk) {
658 pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
1ac39372
TS
659 return ret;
660 }
557cc18e 661#endif
1ac39372 662
7cc72553
JC
663 ret = symbol__validate_sym_arguments();
664 if (ret)
665 return ret;
666
eddaef88
NK
667 if (quiet)
668 perf_quiet_option();
669
2d4f2799 670 data.path = input_name;
44848cdb 671
2681bd85 672 annotate.session = perf_session__new(&data, &annotate.tool);
6ef81c55
MI
673 if (IS_ERR(annotate.session))
674 return PTR_ERR(annotate.session);
fa10f316 675
4bcbe438
YJ
676 annotate.session->itrace_synth_opts = &itrace_synth_opts;
677
bb848c14
JY
678 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
679 HEADER_BRANCH_STACK);
680
7ebaf489 681 if (annotate.group_set)
64b4778b 682 evlist__force_leader(annotate.session->evlist);
7ebaf489 683
b01141f4
ACM
684 ret = symbol__annotation_init();
685 if (ret < 0)
686 goto out_delete;
687
75be6cf4
ACM
688 symbol_conf.try_vmlinux_path = true;
689
0a7e6d1b 690 ret = symbol__init(&annotate.session->header.env);
fa10f316
NK
691 if (ret < 0)
692 goto out_delete;
8035e428 693
befd2a38 694 if (annotate.use_stdio || annotate.use_stdio2)
3df668e7 695 use_browser = 0;
3402ae0a 696#ifdef HAVE_SLANG_SUPPORT
3df668e7
NK
697 else if (annotate.use_tui)
698 use_browser = 1;
3402ae0a 699#endif
557cc18e 700#ifdef HAVE_GTK2_SUPPORT
3df668e7
NK
701 else if (annotate.use_gtk)
702 use_browser = 2;
557cc18e 703#endif
3df668e7
NK
704
705 setup_browser(true);
706
5676dba7
YJ
707 /*
708 * Events of different processes may correspond to the same
709 * symbol, we do not care about the processes in annotate,
710 * set sort order to avoid repeated output.
711 */
712 sort_order = "dso,symbol";
713
714 /*
715 * Set SORT_MODE__BRANCH so that annotate display IPC/Cycle
716 * if branch info is in perf data in TUI mode.
717 */
718 if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack)
bb848c14 719 sort__mode = SORT_MODE__BRANCH;
5676dba7
YJ
720
721 if (setup_sorting(NULL) < 0)
722 usage_with_options(annotate_usage, options);
bb848c14 723
fa10f316
NK
724 ret = __cmd_annotate(&annotate);
725
726out_delete:
727 /*
333b1b11
IR
728 * Speed up the exit process by only deleting for debug builds. For
729 * large files this can save time.
fa10f316 730 */
333b1b11
IR
731#ifndef NDEBUG
732 perf_session__delete(annotate.session);
733#endif
217b7d41 734 annotation_options__exit(&annotate.opts);
333b1b11 735
fa10f316 736 return ret;
8035e428 737}