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