xref: /linux/tools/perf/builtin-annotate.c (revision 046fd8206d820b71e7870f7b894b46f8a15ae974)
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 #include "perf.h"
11 
12 #include "util/color.h"
13 #include <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include <linux/zalloc.h>
17 #include "util/symbol.h"
18 
19 #include "util/debug.h"
20 
21 #include "util/evlist.h"
22 #include "util/evsel.h"
23 #include "util/annotate.h"
24 #include "util/annotate-data.h"
25 #include "util/event.h"
26 #include <subcmd/parse-options.h>
27 #include "util/parse-events.h"
28 #include "util/sort.h"
29 #include "util/hist.h"
30 #include "util/dso.h"
31 #include "util/machine.h"
32 #include "util/map.h"
33 #include "util/session.h"
34 #include "util/tool.h"
35 #include "util/data.h"
36 #include "arch/common.h"
37 #include "util/block-range.h"
38 #include "util/map_symbol.h"
39 #include "util/branch.h"
40 #include "util/util.h"
41 #include "ui/progress.h"
42 
43 #include <dlfcn.h>
44 #include <errno.h>
45 #include <linux/bitmap.h>
46 #include <linux/err.h>
47 #include <inttypes.h>
48 
49 struct perf_annotate {
50 	struct perf_tool tool;
51 	struct perf_session *session;
52 #ifdef HAVE_SLANG_SUPPORT
53 	bool	   use_tui;
54 #endif
55 	bool	   use_stdio, use_stdio2;
56 #ifdef HAVE_GTK2_SUPPORT
57 	bool	   use_gtk;
58 #endif
59 	bool	   skip_missing;
60 	bool	   has_br_stack;
61 	bool	   group_set;
62 	bool	   data_type;
63 	bool	   type_stat;
64 	bool	   insn_stat;
65 	float	   min_percent;
66 	const char *sym_hist_filter;
67 	const char *cpu_list;
68 	const char *target_data_type;
69 	DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
70 };
71 
72 /*
73  * Given one basic block:
74  *
75  *	from	to		branch_i
76  *	* ----> *
77  *		|
78  *		| block
79  *		v
80  *		* ----> *
81  *		from	to	branch_i+1
82  *
83  * where the horizontal are the branches and the vertical is the executed
84  * block of instructions.
85  *
86  * We count, for each 'instruction', the number of blocks that covered it as
87  * well as count the ratio each branch is taken.
88  *
89  * We can do this without knowing the actual instruction stream by keeping
90  * track of the address ranges. We break down ranges such that there is no
91  * overlap and iterate from the start until the end.
92  *
93  * @acme: once we parse the objdump output _before_ processing the samples,
94  * we can easily fold the branch.cycles IPC bits in.
95  */
96 static void process_basic_block(struct addr_map_symbol *start,
97 				struct addr_map_symbol *end,
98 				struct branch_flags *flags)
99 {
100 	struct symbol *sym = start->ms.sym;
101 	struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
102 	struct block_range_iter iter;
103 	struct block_range *entry;
104 	struct annotated_branch *branch;
105 
106 	/*
107 	 * Sanity; NULL isn't executable and the CPU cannot execute backwards
108 	 */
109 	if (!start->addr || start->addr > end->addr)
110 		return;
111 
112 	iter = block_range__create(start->addr, end->addr);
113 	if (!block_range_iter__valid(&iter))
114 		return;
115 
116 	branch = annotation__get_branch(notes);
117 
118 	/*
119 	 * First block in range is a branch target.
120 	 */
121 	entry = block_range_iter(&iter);
122 	assert(entry->is_target);
123 	entry->entry++;
124 
125 	do {
126 		entry = block_range_iter(&iter);
127 
128 		entry->coverage++;
129 		entry->sym = sym;
130 
131 		if (branch)
132 			branch->max_coverage = max(branch->max_coverage, entry->coverage);
133 
134 	} while (block_range_iter__next(&iter));
135 
136 	/*
137 	 * Last block in rage is a branch.
138 	 */
139 	entry = block_range_iter(&iter);
140 	assert(entry->is_branch);
141 	entry->taken++;
142 	if (flags->predicted)
143 		entry->pred++;
144 }
145 
146 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
147 				 struct perf_sample *sample)
148 {
149 	struct addr_map_symbol *prev = NULL;
150 	struct branch_info *bi;
151 	int i;
152 
153 	if (!bs || !bs->nr)
154 		return;
155 
156 	bi = sample__resolve_bstack(sample, al);
157 	if (!bi)
158 		return;
159 
160 	for (i = bs->nr - 1; i >= 0; i--) {
161 		/*
162 		 * XXX filter against symbol
163 		 */
164 		if (prev)
165 			process_basic_block(prev, &bi[i].from, &bi[i].flags);
166 		prev = &bi[i].to;
167 	}
168 
169 	free(bi);
170 }
171 
172 static int hist_iter__branch_callback(struct hist_entry_iter *iter,
173 				      struct addr_location *al __maybe_unused,
174 				      bool single __maybe_unused,
175 				      void *arg __maybe_unused)
176 {
177 	struct hist_entry *he = iter->he;
178 	struct branch_info *bi;
179 	struct perf_sample *sample = iter->sample;
180 	struct evsel *evsel = iter->evsel;
181 	int err;
182 
183 	bi = he->branch_info;
184 	err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
185 
186 	if (err)
187 		goto out;
188 
189 	err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
190 
191 out:
192 	return err;
193 }
194 
195 static int process_branch_callback(struct evsel *evsel,
196 				   struct perf_sample *sample,
197 				   struct addr_location *al,
198 				   struct perf_annotate *ann,
199 				   struct machine *machine)
200 {
201 	struct hist_entry_iter iter = {
202 		.evsel		= evsel,
203 		.sample		= sample,
204 		.add_entry_cb	= hist_iter__branch_callback,
205 		.hide_unresolved	= symbol_conf.hide_unresolved,
206 		.ops		= &hist_iter_branch,
207 	};
208 	struct addr_location a;
209 	int ret;
210 
211 	addr_location__init(&a);
212 	if (machine__resolve(machine, &a, sample) < 0) {
213 		ret = -1;
214 		goto out;
215 	}
216 
217 	if (a.sym == NULL) {
218 		ret = 0;
219 		goto out;
220 	}
221 
222 	if (a.map != NULL)
223 		dso__set_hit(map__dso(a.map));
224 
225 	hist__account_cycles(sample->branch_stack, al, sample, false,
226 			     NULL, evsel);
227 
228 	ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
229 out:
230 	addr_location__exit(&a);
231 	return ret;
232 }
233 
234 static bool has_annotation(struct perf_annotate *ann)
235 {
236 	return ui__has_annotation() || ann->use_stdio2;
237 }
238 
239 static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
240 			     struct addr_location *al, struct perf_annotate *ann,
241 			     struct machine *machine)
242 {
243 	struct hists *hists = evsel__hists(evsel);
244 	struct hist_entry *he;
245 	int ret;
246 
247 	if ((!ann->has_br_stack || !has_annotation(ann)) &&
248 	    ann->sym_hist_filter != NULL &&
249 	    (al->sym == NULL ||
250 	     strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
251 		/* We're only interested in a symbol named sym_hist_filter */
252 		/*
253 		 * FIXME: why isn't this done in the symbol_filter when loading
254 		 * the DSO?
255 		 */
256 		if (al->sym != NULL) {
257 			struct dso *dso = map__dso(al->map);
258 
259 			rb_erase_cached(&al->sym->rb_node, dso__symbols(dso));
260 			symbol__delete(al->sym);
261 			dso__reset_find_symbol_cache(dso);
262 		}
263 		return 0;
264 	}
265 
266 	/*
267 	 * XXX filtered samples can still have branch entries pointing into our
268 	 * symbol and are missed.
269 	 */
270 	process_branch_stack(sample->branch_stack, al, sample);
271 
272 	if (ann->has_br_stack && has_annotation(ann))
273 		return process_branch_callback(evsel, sample, al, ann, machine);
274 
275 	he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true);
276 	if (he == NULL)
277 		return -ENOMEM;
278 
279 	ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
280 	hists__inc_nr_samples(hists, true);
281 	return ret;
282 }
283 
284 static int process_sample_event(const struct perf_tool *tool,
285 				union perf_event *event,
286 				struct perf_sample *sample,
287 				struct evsel *evsel,
288 				struct machine *machine)
289 {
290 	struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
291 	struct addr_location al;
292 	int ret = 0;
293 
294 	addr_location__init(&al);
295 	if (machine__resolve(machine, &al, sample) < 0) {
296 		pr_warning("problem processing %d event, skipping it.\n",
297 			   event->header.type);
298 		ret = -1;
299 		goto out_put;
300 	}
301 
302 	if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
303 		goto out_put;
304 
305 	if (!al.filtered &&
306 	    evsel__add_sample(evsel, sample, &al, ann, machine)) {
307 		pr_warning("problem incrementing symbol count, "
308 			   "skipping event\n");
309 		ret = -1;
310 	}
311 out_put:
312 	addr_location__exit(&al);
313 	return ret;
314 }
315 
316 static int hist_entry__stdio_annotate(struct hist_entry *he,
317 				    struct evsel *evsel,
318 				    struct perf_annotate *ann)
319 {
320 	if (ann->use_stdio2)
321 		return hist_entry__tty_annotate2(he, evsel);
322 
323 	return hist_entry__tty_annotate(he, evsel);
324 }
325 
326 static void print_annotate_data_stat(struct annotated_data_stat *s)
327 {
328 #define PRINT_STAT(fld) if (s->fld) printf("%10d : %s\n", s->fld, #fld)
329 
330 	int bad = s->no_sym +
331 			s->no_insn +
332 			s->no_insn_ops +
333 			s->no_mem_ops +
334 			s->no_reg +
335 			s->no_dbginfo +
336 			s->no_cuinfo +
337 			s->no_var +
338 			s->no_typeinfo +
339 			s->invalid_size +
340 			s->bad_offset;
341 	int ok = s->total - bad;
342 
343 	printf("Annotate data type stats:\n");
344 	printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n",
345 		s->total, ok, 100.0 * ok / (s->total ?: 1), bad, 100.0 * bad / (s->total ?: 1));
346 	printf("-----------------------------------------------------------\n");
347 	PRINT_STAT(no_sym);
348 	PRINT_STAT(no_insn);
349 	PRINT_STAT(no_insn_ops);
350 	PRINT_STAT(no_mem_ops);
351 	PRINT_STAT(no_reg);
352 	PRINT_STAT(no_dbginfo);
353 	PRINT_STAT(no_cuinfo);
354 	PRINT_STAT(no_var);
355 	PRINT_STAT(no_typeinfo);
356 	PRINT_STAT(invalid_size);
357 	PRINT_STAT(bad_offset);
358 	PRINT_STAT(insn_track);
359 	printf("\n");
360 
361 #undef PRINT_STAT
362 }
363 
364 static void print_annotate_item_stat(struct list_head *head, const char *title)
365 {
366 	struct annotated_item_stat *istat, *pos, *iter;
367 	int total_good, total_bad, total;
368 	int sum1, sum2;
369 	LIST_HEAD(tmp);
370 
371 	/* sort the list by count */
372 	list_splice_init(head, &tmp);
373 	total_good = total_bad = 0;
374 
375 	list_for_each_entry_safe(istat, pos, &tmp, list) {
376 		total_good += istat->good;
377 		total_bad += istat->bad;
378 		sum1 = istat->good + istat->bad;
379 
380 		list_for_each_entry(iter, head, list) {
381 			sum2 = iter->good + iter->bad;
382 			if (sum1 > sum2)
383 				break;
384 		}
385 		list_move_tail(&istat->list, &iter->list);
386 	}
387 	total = total_good + total_bad;
388 
389 	printf("Annotate %s stats\n", title);
390 	printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n\n", total,
391 	       total_good, 100.0 * total_good / (total ?: 1),
392 	       total_bad, 100.0 * total_bad / (total ?: 1));
393 	printf("  %-20s: %5s %5s\n", "Name/opcode", "Good", "Bad");
394 	printf("-----------------------------------------------------------\n");
395 	list_for_each_entry(istat, head, list)
396 		printf("  %-20s: %5d %5d\n", istat->name, istat->good, istat->bad);
397 	printf("\n");
398 }
399 
400 static void hists__find_annotations(struct hists *hists,
401 				    struct evsel *evsel,
402 				    struct perf_annotate *ann)
403 {
404 	struct rb_node *nd = rb_first_cached(&hists->entries), *next;
405 	int key = K_RIGHT;
406 
407 	if (ann->type_stat)
408 		print_annotate_data_stat(&ann_data_stat);
409 	if (ann->insn_stat)
410 		print_annotate_item_stat(&ann_insn_stat, "Instruction");
411 
412 	while (nd) {
413 		struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
414 		struct annotation *notes;
415 
416 		if (he->ms.sym == NULL || dso__annotate_warned(map__dso(he->ms.map)))
417 			goto find_next;
418 
419 		if (ann->sym_hist_filter &&
420 		    (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
421 			goto find_next;
422 
423 		if (ann->min_percent) {
424 			float percent = 0;
425 			u64 total = hists__total_period(hists);
426 
427 			if (total)
428 				percent = 100.0 * he->stat.period / total;
429 
430 			if (percent < ann->min_percent)
431 				goto find_next;
432 		}
433 
434 		notes = symbol__annotation(he->ms.sym);
435 		if (notes->src == NULL) {
436 find_next:
437 			if (key == K_LEFT || key == '<')
438 				nd = rb_prev(nd);
439 			else
440 				nd = rb_next(nd);
441 			continue;
442 		}
443 
444 		if (ann->data_type) {
445 			/* skip unknown type */
446 			if (he->mem_type->histograms == NULL)
447 				goto find_next;
448 
449 			if (ann->target_data_type) {
450 				const char *type_name = he->mem_type->self.type_name;
451 
452 				/* skip 'struct ' prefix in the type name */
453 				if (strncmp(ann->target_data_type, "struct ", 7) &&
454 				    !strncmp(type_name, "struct ", 7))
455 					type_name += 7;
456 
457 				/* skip 'union ' prefix in the type name */
458 				if (strncmp(ann->target_data_type, "union ", 6) &&
459 				    !strncmp(type_name, "union ", 6))
460 					type_name += 6;
461 
462 				if (strcmp(ann->target_data_type, type_name))
463 					goto find_next;
464 			}
465 
466 			if (use_browser == 1)
467 				key = hist_entry__annotate_data_tui(he, evsel, NULL);
468 			else
469 				key = hist_entry__annotate_data_tty(he, evsel);
470 
471 			switch (key) {
472 			case -1:
473 				if (!ann->skip_missing)
474 					return;
475 				/* fall through */
476 			case K_RIGHT:
477 			case '>':
478 				next = rb_next(nd);
479 				break;
480 			case K_LEFT:
481 			case '<':
482 				next = rb_prev(nd);
483 				break;
484 			default:
485 				return;
486 			}
487 
488 			if (use_browser == 0 || next != NULL)
489 				nd = next;
490 
491 			continue;
492 		}
493 
494 		if (use_browser == 2) {
495 			int ret;
496 			int (*annotate)(struct hist_entry *he,
497 					struct evsel *evsel,
498 					struct hist_browser_timer *hbt);
499 
500 			annotate = dlsym(perf_gtk_handle,
501 					 "hist_entry__gtk_annotate");
502 			if (annotate == NULL) {
503 				ui__error("GTK browser not found!\n");
504 				return;
505 			}
506 
507 			ret = annotate(he, evsel, NULL);
508 			if (!ret || !ann->skip_missing)
509 				return;
510 
511 			/* skip missing symbols */
512 			nd = rb_next(nd);
513 		} else if (use_browser == 1) {
514 			key = hist_entry__tui_annotate(he, evsel, NULL, NO_ADDR);
515 
516 			switch (key) {
517 			case -1:
518 				if (!ann->skip_missing)
519 					return;
520 				/* fall through */
521 			case K_RIGHT:
522 			case '>':
523 				next = rb_next(nd);
524 				break;
525 			case K_LEFT:
526 			case '<':
527 				next = rb_prev(nd);
528 				break;
529 			default:
530 				return;
531 			}
532 
533 			if (next != NULL)
534 				nd = next;
535 		} else {
536 			hist_entry__stdio_annotate(he, evsel, ann);
537 			nd = rb_next(nd);
538 		}
539 	}
540 }
541 
542 static int __cmd_annotate(struct perf_annotate *ann)
543 {
544 	int ret;
545 	struct perf_session *session = ann->session;
546 	struct evsel *pos;
547 	u64 total_nr_samples;
548 
549 	if (ann->cpu_list) {
550 		ret = perf_session__cpu_bitmap(session, ann->cpu_list,
551 					       ann->cpu_bitmap);
552 		if (ret)
553 			goto out;
554 	}
555 
556 	if (!annotate_opts.objdump_path) {
557 		ret = perf_env__lookup_objdump(perf_session__env(session),
558 					       &annotate_opts.objdump_path);
559 		if (ret)
560 			goto out;
561 	}
562 
563 	ret = perf_session__process_events(session);
564 	if (ret)
565 		goto out;
566 
567 	if (dump_trace) {
568 		perf_session__fprintf_nr_events(session, stdout);
569 		evlist__fprintf_nr_events(session->evlist, stdout);
570 		goto out;
571 	}
572 
573 	if (verbose > 3)
574 		perf_session__fprintf(session, stdout);
575 
576 	if (verbose > 2)
577 		perf_session__fprintf_dsos(session, stdout);
578 
579 	total_nr_samples = 0;
580 	evlist__for_each_entry(session->evlist, pos) {
581 		struct hists *hists = evsel__hists(pos);
582 		u32 nr_samples = hists->stats.nr_samples;
583 		struct ui_progress prog;
584 
585 		if (nr_samples > 0) {
586 			total_nr_samples += nr_samples;
587 
588 			ui_progress__init(&prog, nr_samples,
589 					  "Merging related events...");
590 			hists__collapse_resort(hists, &prog);
591 			ui_progress__finish();
592 
593 			/* Don't sort callchain */
594 			evsel__reset_sample_bit(pos, CALLCHAIN);
595 
596 			ui_progress__init(&prog, nr_samples,
597 					  "Sorting events for output...");
598 			evsel__output_resort(pos, &prog);
599 			ui_progress__finish();
600 
601 			/*
602 			 * An event group needs to display other events too.
603 			 * Let's delay printing until other events are processed.
604 			 */
605 			if (symbol_conf.event_group) {
606 				if (!evsel__is_group_leader(pos)) {
607 					struct hists *leader_hists;
608 
609 					leader_hists = evsel__hists(evsel__leader(pos));
610 					hists__match(leader_hists, hists);
611 					hists__link(leader_hists, hists);
612 				}
613 				continue;
614 			}
615 
616 			hists__find_annotations(hists, pos, ann);
617 		}
618 	}
619 
620 	if (total_nr_samples == 0) {
621 		ui__error("The %s data has no samples!\n", session->data->path);
622 		goto out;
623 	}
624 
625 	/* Display group events together */
626 	evlist__for_each_entry(session->evlist, pos) {
627 		struct hists *hists = evsel__hists(pos);
628 		u32 nr_samples = hists->stats.nr_samples;
629 		struct ui_progress prog;
630 		struct evsel *evsel;
631 
632 		if (!symbol_conf.event_group || !evsel__is_group_leader(pos))
633 			continue;
634 
635 		for_each_group_member(evsel, pos)
636 			nr_samples += evsel__hists(evsel)->stats.nr_samples;
637 
638 		if (nr_samples == 0)
639 			continue;
640 
641 		ui_progress__init(&prog, nr_samples,
642 				  "Sorting group events for output...");
643 		evsel__output_resort(pos, &prog);
644 		ui_progress__finish();
645 
646 		hists__find_annotations(hists, pos, ann);
647 	}
648 
649 	if (use_browser == 2) {
650 		void (*show_annotations)(void);
651 
652 		show_annotations = dlsym(perf_gtk_handle,
653 					 "perf_gtk__show_annotations");
654 		if (show_annotations == NULL) {
655 			ui__error("GTK browser not found!\n");
656 			goto out;
657 		}
658 		show_annotations();
659 	}
660 
661 out:
662 	return ret;
663 }
664 
665 static int parse_percent_limit(const struct option *opt, const char *str,
666 			       int unset __maybe_unused)
667 {
668 	struct perf_annotate *ann = opt->value;
669 	double pcnt = strtof(str, NULL);
670 
671 	ann->min_percent = pcnt;
672 	return 0;
673 }
674 
675 static int parse_data_type(const struct option *opt, const char *str, int unset)
676 {
677 	struct perf_annotate *ann = opt->value;
678 
679 	ann->data_type = !unset;
680 	if (str)
681 		ann->target_data_type = strdup(str);
682 
683 	return 0;
684 }
685 
686 static const char * const annotate_usage[] = {
687 	"perf annotate [<options>]",
688 	NULL
689 };
690 
691 int cmd_annotate(int argc, const char **argv)
692 {
693 	struct perf_annotate annotate = {};
694 	struct perf_data data = {
695 		.mode  = PERF_DATA_MODE_READ,
696 	};
697 	struct itrace_synth_opts itrace_synth_opts = {
698 		.set = 0,
699 	};
700 	const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
701 	struct option options[] = {
702 	OPT_STRING('i', "input", &input_name, "file",
703 		    "input file name"),
704 	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
705 		   "only consider symbols in these dsos"),
706 	OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
707 		    "symbol to annotate"),
708 	OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
709 	OPT_INCR('v', "verbose", &verbose,
710 		    "be more verbose (show symbol address, etc)"),
711 	OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"),
712 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
713 		    "dump raw trace in ASCII"),
714 #ifdef HAVE_GTK2_SUPPORT
715 	OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
716 #endif
717 #ifdef HAVE_SLANG_SUPPORT
718 	OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
719 #endif
720 	OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
721 	OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
722 	OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
723                     "don't load vmlinux even if found"),
724 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
725 		   "file", "vmlinux pathname"),
726 	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
727 		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
728 	OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines,
729 		    "print matching source lines (may be slow)"),
730 	OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path,
731 		    "Don't shorten the displayed pathnames"),
732 	OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
733 		    "Skip symbols that cannot be annotated"),
734 	OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
735 			&annotate.group_set,
736 			"Show event group information together"),
737 	OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
738 	OPT_CALLBACK(0, "symfs", NULL, "directory[,layout]", SYMFS_HELP,
739 		     symbol__config_symfs),
740 	OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src,
741 		    "Interleave source code with assembly code (default)"),
742 	OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw,
743 		    "Display raw encoding of assembly instructions (default)"),
744 	OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
745 		   "Specify disassembler style (e.g. -M intel for intel syntax)"),
746 	OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix",
747 		    "Add prefix to source file path names in programs (with --prefix-strip)"),
748 	OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
749 		    "Strip first N entries of source file path name in programs (with --prefix)"),
750 	OPT_STRING(0, "objdump", &objdump_path, "path",
751 		   "objdump binary to use for disassembly and annotations"),
752 	OPT_STRING(0, "addr2line", &addr2line_path, "path",
753 		   "addr2line binary to use for line numbers"),
754 	OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
755 		    "Enable symbol demangling"),
756 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
757 		    "Enable kernel symbol demangling"),
758 	OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
759 		    "Show a column with the sum of periods"),
760 	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
761 		    "Show a column with the number of samples"),
762 	OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
763 			     "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
764 			     stdio__config_color, "always"),
765 	OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period",
766 		     "Set percent type local/global-period/hits",
767 		     annotate_parse_percent_type),
768 	OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
769 		     "Don't show entries under that percent", parse_percent_limit),
770 	OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
771 			    "Instruction Tracing options\n" ITRACE_HELP,
772 			    itrace_parse_synth_opts),
773 	OPT_CALLBACK_OPTARG(0, "data-type", &annotate, NULL, "name",
774 			    "Show data type annotate for the memory accesses",
775 			    parse_data_type),
776 	OPT_BOOLEAN(0, "type-stat", &annotate.type_stat,
777 		    "Show stats for the data type annotation"),
778 	OPT_BOOLEAN(0, "insn-stat", &annotate.insn_stat,
779 		    "Show instruction stats for the data type annotation"),
780 	OPT_BOOLEAN(0, "skip-empty", &symbol_conf.skip_empty,
781 		    "Do not display empty (or dummy) events in the output"),
782 	OPT_BOOLEAN(0, "code-with-type", &annotate_opts.code_with_type,
783 		    "Show data type info in code annotation (memory instructions only)"),
784 	OPT_END()
785 	};
786 	int ret;
787 
788 	set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
789 	set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
790 
791 	annotation_options__init();
792 
793 	ret = hists__init();
794 	if (ret < 0)
795 		return ret;
796 
797 	annotation_config__init();
798 
799 	argc = parse_options(argc, argv, options, annotate_usage, 0);
800 	if (argc) {
801 		/*
802 		 * Special case: if there's an argument left then assume that
803 		 * it's a symbol filter:
804 		 */
805 		if (argc > 1)
806 			usage_with_options(annotate_usage, options);
807 
808 		annotate.sym_hist_filter = argv[0];
809 	}
810 
811 	if (disassembler_style) {
812 		annotate_opts.disassembler_style = strdup(disassembler_style);
813 		if (!annotate_opts.disassembler_style)
814 			return -ENOMEM;
815 	}
816 	if (objdump_path) {
817 		annotate_opts.objdump_path = strdup(objdump_path);
818 		if (!annotate_opts.objdump_path)
819 			return -ENOMEM;
820 	}
821 	if (addr2line_path) {
822 		symbol_conf.addr2line_path = strdup(addr2line_path);
823 		if (!symbol_conf.addr2line_path)
824 			return -ENOMEM;
825 	}
826 
827 	if (annotate_check_args() < 0)
828 		return -EINVAL;
829 
830 #ifdef HAVE_GTK2_SUPPORT
831 	if (symbol_conf.show_nr_samples && annotate.use_gtk) {
832 		pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
833 		return ret;
834 	}
835 #endif
836 
837 #ifndef HAVE_LIBDW_SUPPORT
838 	if (annotate.data_type) {
839 		pr_err("Error: Data type profiling is disabled due to missing DWARF support\n");
840 		return -ENOTSUP;
841 	}
842 #endif
843 
844 	ret = symbol__validate_sym_arguments();
845 	if (ret)
846 		return ret;
847 
848 	if (quiet)
849 		perf_quiet_option();
850 
851 	data.path = input_name;
852 
853 	perf_tool__init(&annotate.tool, /*ordered_events=*/true);
854 	annotate.tool.sample	= process_sample_event;
855 	annotate.tool.mmap	= perf_event__process_mmap;
856 	annotate.tool.mmap2	= perf_event__process_mmap2;
857 	annotate.tool.comm	= perf_event__process_comm;
858 	annotate.tool.exit	= perf_event__process_exit;
859 	annotate.tool.fork	= perf_event__process_fork;
860 	annotate.tool.namespaces = perf_event__process_namespaces;
861 	annotate.tool.attr	= perf_event__process_attr;
862 	annotate.tool.build_id = perf_event__process_build_id;
863 #ifdef HAVE_LIBTRACEEVENT
864 	annotate.tool.tracing_data   = perf_event__process_tracing_data;
865 #endif
866 	annotate.tool.id_index	= perf_event__process_id_index;
867 	annotate.tool.auxtrace_info	= perf_event__process_auxtrace_info;
868 	annotate.tool.auxtrace	= perf_event__process_auxtrace;
869 	annotate.tool.feature	= perf_event__process_feature;
870 	annotate.tool.ordering_requires_timestamps = true;
871 
872 	annotate.session = perf_session__new(&data, &annotate.tool);
873 	if (IS_ERR(annotate.session))
874 		return PTR_ERR(annotate.session);
875 
876 	annotate.session->itrace_synth_opts = &itrace_synth_opts;
877 
878 	annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
879 						      HEADER_BRANCH_STACK);
880 
881 	if (annotate.group_set)
882 		evlist__force_leader(annotate.session->evlist);
883 
884 	ret = symbol__annotation_init();
885 	if (ret < 0)
886 		goto out_delete;
887 
888 	symbol_conf.try_vmlinux_path = true;
889 
890 	ret = symbol__init(perf_session__env(annotate.session));
891 	if (ret < 0)
892 		goto out_delete;
893 
894 	if (annotate.use_stdio || annotate.use_stdio2)
895 		use_browser = 0;
896 #ifdef HAVE_SLANG_SUPPORT
897 	else if (annotate.use_tui)
898 		use_browser = 1;
899 #endif
900 #ifdef HAVE_GTK2_SUPPORT
901 	else if (annotate.use_gtk)
902 		use_browser = 2;
903 #endif
904 
905 	if (annotate.data_type) {
906 		annotate_opts.annotate_src = false;
907 		symbol_conf.annotate_data_member = true;
908 		symbol_conf.annotate_data_sample = true;
909 	} else if (annotate_opts.code_with_type) {
910 		symbol_conf.annotate_data_member = true;
911 	}
912 
913 	setup_browser(true);
914 
915 	/*
916 	 * Events of different processes may correspond to the same
917 	 * symbol, we do not care about the processes in annotate,
918 	 * set sort order to avoid repeated output.
919 	 */
920 	if (annotate.data_type)
921 		sort_order = "dso,type";
922 	else
923 		sort_order = "dso,symbol";
924 
925 	/*
926 	 * Set SORT_MODE__BRANCH so that annotate displays IPC/Cycle and
927 	 * branch counters, if the corresponding branch info is available
928 	 * in the perf data in the TUI mode.
929 	 */
930 	if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
931 		sort__mode = SORT_MODE__BRANCH;
932 		if (annotate.session->evlist->nr_br_cntr > 0)
933 			annotate_opts.show_br_cntr = true;
934 	}
935 
936 	if (setup_sorting(/*evlist=*/NULL, perf_session__env(annotate.session)) < 0)
937 		usage_with_options(annotate_usage, options);
938 
939 	ret = __cmd_annotate(&annotate);
940 
941 out_delete:
942 	/*
943 	 * Speed up the exit process by only deleting for debug builds. For
944 	 * large files this can save time.
945 	 */
946 #ifndef NDEBUG
947 	perf_session__delete(annotate.session);
948 #endif
949 	annotation_options__exit();
950 
951 	return ret;
952 }
953