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  */
process_basic_block(struct addr_map_symbol * start,struct addr_map_symbol * end,struct branch_flags * flags)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 
process_branch_stack(struct branch_stack * bs,struct addr_location * al,struct perf_sample * sample)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 
hist_iter__branch_callback(struct hist_entry_iter * iter,struct addr_location * al __maybe_unused,bool single __maybe_unused,void * arg __maybe_unused)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 
process_branch_callback(struct evsel * evsel,struct perf_sample * sample,struct addr_location * al,struct perf_annotate * ann,struct machine * machine)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 
has_annotation(struct perf_annotate * ann)234 static bool has_annotation(struct perf_annotate *ann)
235 {
236 	return ui__has_annotation() || ann->use_stdio2;
237 }
238 
evsel__add_sample(struct evsel * evsel,struct perf_sample * sample,struct addr_location * al,struct perf_annotate * ann,struct machine * machine)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 
process_sample_event(const struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct evsel * evsel,struct machine * machine)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 
process_feature_event(struct perf_session * session,union perf_event * event)316 static int process_feature_event(struct perf_session *session,
317 				 union perf_event *event)
318 {
319 	if (event->feat.feat_id < HEADER_LAST_FEATURE)
320 		return perf_event__process_feature(session, event);
321 	return 0;
322 }
323 
hist_entry__tty_annotate(struct hist_entry * he,struct evsel * evsel,struct perf_annotate * ann)324 static int hist_entry__tty_annotate(struct hist_entry *he,
325 				    struct evsel *evsel,
326 				    struct perf_annotate *ann)
327 {
328 	if (!ann->use_stdio2)
329 		return symbol__tty_annotate(&he->ms, evsel);
330 
331 	return symbol__tty_annotate2(&he->ms, evsel);
332 }
333 
print_annotate_data_stat(struct annotated_data_stat * s)334 static void print_annotate_data_stat(struct annotated_data_stat *s)
335 {
336 #define PRINT_STAT(fld) if (s->fld) printf("%10d : %s\n", s->fld, #fld)
337 
338 	int bad = s->no_sym +
339 			s->no_insn +
340 			s->no_insn_ops +
341 			s->no_mem_ops +
342 			s->no_reg +
343 			s->no_dbginfo +
344 			s->no_cuinfo +
345 			s->no_var +
346 			s->no_typeinfo +
347 			s->invalid_size +
348 			s->bad_offset;
349 	int ok = s->total - bad;
350 
351 	printf("Annotate data type stats:\n");
352 	printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n",
353 		s->total, ok, 100.0 * ok / (s->total ?: 1), bad, 100.0 * bad / (s->total ?: 1));
354 	printf("-----------------------------------------------------------\n");
355 	PRINT_STAT(no_sym);
356 	PRINT_STAT(no_insn);
357 	PRINT_STAT(no_insn_ops);
358 	PRINT_STAT(no_mem_ops);
359 	PRINT_STAT(no_reg);
360 	PRINT_STAT(no_dbginfo);
361 	PRINT_STAT(no_cuinfo);
362 	PRINT_STAT(no_var);
363 	PRINT_STAT(no_typeinfo);
364 	PRINT_STAT(invalid_size);
365 	PRINT_STAT(bad_offset);
366 	PRINT_STAT(insn_track);
367 	printf("\n");
368 
369 #undef PRINT_STAT
370 }
371 
print_annotate_item_stat(struct list_head * head,const char * title)372 static void print_annotate_item_stat(struct list_head *head, const char *title)
373 {
374 	struct annotated_item_stat *istat, *pos, *iter;
375 	int total_good, total_bad, total;
376 	int sum1, sum2;
377 	LIST_HEAD(tmp);
378 
379 	/* sort the list by count */
380 	list_splice_init(head, &tmp);
381 	total_good = total_bad = 0;
382 
383 	list_for_each_entry_safe(istat, pos, &tmp, list) {
384 		total_good += istat->good;
385 		total_bad += istat->bad;
386 		sum1 = istat->good + istat->bad;
387 
388 		list_for_each_entry(iter, head, list) {
389 			sum2 = iter->good + iter->bad;
390 			if (sum1 > sum2)
391 				break;
392 		}
393 		list_move_tail(&istat->list, &iter->list);
394 	}
395 	total = total_good + total_bad;
396 
397 	printf("Annotate %s stats\n", title);
398 	printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n\n", total,
399 	       total_good, 100.0 * total_good / (total ?: 1),
400 	       total_bad, 100.0 * total_bad / (total ?: 1));
401 	printf("  %-20s: %5s %5s\n", "Name/opcode", "Good", "Bad");
402 	printf("-----------------------------------------------------------\n");
403 	list_for_each_entry(istat, head, list)
404 		printf("  %-20s: %5d %5d\n", istat->name, istat->good, istat->bad);
405 	printf("\n");
406 }
407 
hists__find_annotations(struct hists * hists,struct evsel * evsel,struct perf_annotate * ann)408 static void hists__find_annotations(struct hists *hists,
409 				    struct evsel *evsel,
410 				    struct perf_annotate *ann)
411 {
412 	struct rb_node *nd = rb_first_cached(&hists->entries), *next;
413 	int key = K_RIGHT;
414 
415 	if (ann->type_stat)
416 		print_annotate_data_stat(&ann_data_stat);
417 	if (ann->insn_stat)
418 		print_annotate_item_stat(&ann_insn_stat, "Instruction");
419 
420 	while (nd) {
421 		struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
422 		struct annotation *notes;
423 
424 		if (he->ms.sym == NULL || dso__annotate_warned(map__dso(he->ms.map)))
425 			goto find_next;
426 
427 		if (ann->sym_hist_filter &&
428 		    (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
429 			goto find_next;
430 
431 		if (ann->min_percent) {
432 			float percent = 0;
433 			u64 total = hists__total_period(hists);
434 
435 			if (total)
436 				percent = 100.0 * he->stat.period / total;
437 
438 			if (percent < ann->min_percent)
439 				goto find_next;
440 		}
441 
442 		notes = symbol__annotation(he->ms.sym);
443 		if (notes->src == NULL) {
444 find_next:
445 			if (key == K_LEFT || key == '<')
446 				nd = rb_prev(nd);
447 			else
448 				nd = rb_next(nd);
449 			continue;
450 		}
451 
452 		if (ann->data_type) {
453 			/* skip unknown type */
454 			if (he->mem_type->histograms == NULL)
455 				goto find_next;
456 
457 			if (ann->target_data_type) {
458 				const char *type_name = he->mem_type->self.type_name;
459 
460 				/* skip 'struct ' prefix in the type name */
461 				if (strncmp(ann->target_data_type, "struct ", 7) &&
462 				    !strncmp(type_name, "struct ", 7))
463 					type_name += 7;
464 
465 				/* skip 'union ' prefix in the type name */
466 				if (strncmp(ann->target_data_type, "union ", 6) &&
467 				    !strncmp(type_name, "union ", 6))
468 					type_name += 6;
469 
470 				if (strcmp(ann->target_data_type, type_name))
471 					goto find_next;
472 			}
473 
474 			if (use_browser == 1)
475 				key = hist_entry__annotate_data_tui(he, evsel, NULL);
476 			else
477 				key = hist_entry__annotate_data_tty(he, evsel);
478 
479 			switch (key) {
480 			case -1:
481 				if (!ann->skip_missing)
482 					return;
483 				/* fall through */
484 			case K_RIGHT:
485 			case '>':
486 				next = rb_next(nd);
487 				break;
488 			case K_LEFT:
489 			case '<':
490 				next = rb_prev(nd);
491 				break;
492 			default:
493 				return;
494 			}
495 
496 			if (use_browser == 0 || next != NULL)
497 				nd = next;
498 
499 			continue;
500 		}
501 
502 		if (use_browser == 2) {
503 			int ret;
504 			int (*annotate)(struct hist_entry *he,
505 					struct evsel *evsel,
506 					struct hist_browser_timer *hbt);
507 
508 			annotate = dlsym(perf_gtk_handle,
509 					 "hist_entry__gtk_annotate");
510 			if (annotate == NULL) {
511 				ui__error("GTK browser not found!\n");
512 				return;
513 			}
514 
515 			ret = annotate(he, evsel, NULL);
516 			if (!ret || !ann->skip_missing)
517 				return;
518 
519 			/* skip missing symbols */
520 			nd = rb_next(nd);
521 		} else if (use_browser == 1) {
522 			key = hist_entry__tui_annotate(he, evsel, NULL);
523 
524 			switch (key) {
525 			case -1:
526 				if (!ann->skip_missing)
527 					return;
528 				/* fall through */
529 			case K_RIGHT:
530 			case '>':
531 				next = rb_next(nd);
532 				break;
533 			case K_LEFT:
534 			case '<':
535 				next = rb_prev(nd);
536 				break;
537 			default:
538 				return;
539 			}
540 
541 			if (next != NULL)
542 				nd = next;
543 		} else {
544 			hist_entry__tty_annotate(he, evsel, ann);
545 			nd = rb_next(nd);
546 		}
547 	}
548 }
549 
__cmd_annotate(struct perf_annotate * ann)550 static int __cmd_annotate(struct perf_annotate *ann)
551 {
552 	int ret;
553 	struct perf_session *session = ann->session;
554 	struct evsel *pos;
555 	u64 total_nr_samples;
556 
557 	if (ann->cpu_list) {
558 		ret = perf_session__cpu_bitmap(session, ann->cpu_list,
559 					       ann->cpu_bitmap);
560 		if (ret)
561 			goto out;
562 	}
563 
564 	if (!annotate_opts.objdump_path) {
565 		ret = perf_env__lookup_objdump(&session->header.env,
566 					       &annotate_opts.objdump_path);
567 		if (ret)
568 			goto out;
569 	}
570 
571 	ret = perf_session__process_events(session);
572 	if (ret)
573 		goto out;
574 
575 	if (dump_trace) {
576 		perf_session__fprintf_nr_events(session, stdout);
577 		evlist__fprintf_nr_events(session->evlist, stdout);
578 		goto out;
579 	}
580 
581 	if (verbose > 3)
582 		perf_session__fprintf(session, stdout);
583 
584 	if (verbose > 2)
585 		perf_session__fprintf_dsos(session, stdout);
586 
587 	total_nr_samples = 0;
588 	evlist__for_each_entry(session->evlist, pos) {
589 		struct hists *hists = evsel__hists(pos);
590 		u32 nr_samples = hists->stats.nr_samples;
591 		struct ui_progress prog;
592 
593 		if (nr_samples > 0) {
594 			total_nr_samples += nr_samples;
595 
596 			ui_progress__init(&prog, nr_samples,
597 					  "Merging related events...");
598 			hists__collapse_resort(hists, &prog);
599 			ui_progress__finish();
600 
601 			/* Don't sort callchain */
602 			evsel__reset_sample_bit(pos, CALLCHAIN);
603 
604 			ui_progress__init(&prog, nr_samples,
605 					  "Sorting events for output...");
606 			evsel__output_resort(pos, &prog);
607 			ui_progress__finish();
608 
609 			/*
610 			 * An event group needs to display other events too.
611 			 * Let's delay printing until other events are processed.
612 			 */
613 			if (symbol_conf.event_group) {
614 				if (!evsel__is_group_leader(pos)) {
615 					struct hists *leader_hists;
616 
617 					leader_hists = evsel__hists(evsel__leader(pos));
618 					hists__match(leader_hists, hists);
619 					hists__link(leader_hists, hists);
620 				}
621 				continue;
622 			}
623 
624 			hists__find_annotations(hists, pos, ann);
625 		}
626 	}
627 
628 	if (total_nr_samples == 0) {
629 		ui__error("The %s data has no samples!\n", session->data->path);
630 		goto out;
631 	}
632 
633 	/* Display group events together */
634 	evlist__for_each_entry(session->evlist, pos) {
635 		struct hists *hists = evsel__hists(pos);
636 		u32 nr_samples = hists->stats.nr_samples;
637 		struct ui_progress prog;
638 		struct evsel *evsel;
639 
640 		if (!symbol_conf.event_group || !evsel__is_group_leader(pos))
641 			continue;
642 
643 		for_each_group_member(evsel, pos)
644 			nr_samples += evsel__hists(evsel)->stats.nr_samples;
645 
646 		if (nr_samples == 0)
647 			continue;
648 
649 		ui_progress__init(&prog, nr_samples,
650 				  "Sorting group events for output...");
651 		evsel__output_resort(pos, &prog);
652 		ui_progress__finish();
653 
654 		hists__find_annotations(hists, pos, ann);
655 	}
656 
657 	if (use_browser == 2) {
658 		void (*show_annotations)(void);
659 
660 		show_annotations = dlsym(perf_gtk_handle,
661 					 "perf_gtk__show_annotations");
662 		if (show_annotations == NULL) {
663 			ui__error("GTK browser not found!\n");
664 			goto out;
665 		}
666 		show_annotations();
667 	}
668 
669 out:
670 	return ret;
671 }
672 
parse_percent_limit(const struct option * opt,const char * str,int unset __maybe_unused)673 static int parse_percent_limit(const struct option *opt, const char *str,
674 			       int unset __maybe_unused)
675 {
676 	struct perf_annotate *ann = opt->value;
677 	double pcnt = strtof(str, NULL);
678 
679 	ann->min_percent = pcnt;
680 	return 0;
681 }
682 
parse_data_type(const struct option * opt,const char * str,int unset)683 static int parse_data_type(const struct option *opt, const char *str, int unset)
684 {
685 	struct perf_annotate *ann = opt->value;
686 
687 	ann->data_type = !unset;
688 	if (str)
689 		ann->target_data_type = strdup(str);
690 
691 	return 0;
692 }
693 
694 static const char * const annotate_usage[] = {
695 	"perf annotate [<options>]",
696 	NULL
697 };
698 
cmd_annotate(int argc,const char ** argv)699 int cmd_annotate(int argc, const char **argv)
700 {
701 	struct perf_annotate annotate = {};
702 	struct perf_data data = {
703 		.mode  = PERF_DATA_MODE_READ,
704 	};
705 	struct itrace_synth_opts itrace_synth_opts = {
706 		.set = 0,
707 	};
708 	const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
709 	struct option options[] = {
710 	OPT_STRING('i', "input", &input_name, "file",
711 		    "input file name"),
712 	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
713 		   "only consider symbols in these dsos"),
714 	OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
715 		    "symbol to annotate"),
716 	OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
717 	OPT_INCR('v', "verbose", &verbose,
718 		    "be more verbose (show symbol address, etc)"),
719 	OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"),
720 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
721 		    "dump raw trace in ASCII"),
722 #ifdef HAVE_GTK2_SUPPORT
723 	OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
724 #endif
725 #ifdef HAVE_SLANG_SUPPORT
726 	OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
727 #endif
728 	OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
729 	OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
730 	OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
731                     "don't load vmlinux even if found"),
732 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
733 		   "file", "vmlinux pathname"),
734 	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
735 		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
736 	OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines,
737 		    "print matching source lines (may be slow)"),
738 	OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path,
739 		    "Don't shorten the displayed pathnames"),
740 	OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
741 		    "Skip symbols that cannot be annotated"),
742 	OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
743 			&annotate.group_set,
744 			"Show event group information together"),
745 	OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
746 	OPT_CALLBACK(0, "symfs", NULL, "directory",
747 		     "Look for files with symbols relative to this directory",
748 		     symbol__config_symfs),
749 	OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src,
750 		    "Interleave source code with assembly code (default)"),
751 	OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw,
752 		    "Display raw encoding of assembly instructions (default)"),
753 	OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
754 		   "Specify disassembler style (e.g. -M intel for intel syntax)"),
755 	OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix",
756 		    "Add prefix to source file path names in programs (with --prefix-strip)"),
757 	OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
758 		    "Strip first N entries of source file path name in programs (with --prefix)"),
759 	OPT_STRING(0, "objdump", &objdump_path, "path",
760 		   "objdump binary to use for disassembly and annotations"),
761 	OPT_STRING(0, "addr2line", &addr2line_path, "path",
762 		   "addr2line binary to use for line numbers"),
763 	OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
764 		    "Enable symbol demangling"),
765 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
766 		    "Enable kernel symbol demangling"),
767 	OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
768 		    "Show a column with the sum of periods"),
769 	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
770 		    "Show a column with the number of samples"),
771 	OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
772 			     "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
773 			     stdio__config_color, "always"),
774 	OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period",
775 		     "Set percent type local/global-period/hits",
776 		     annotate_parse_percent_type),
777 	OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
778 		     "Don't show entries under that percent", parse_percent_limit),
779 	OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
780 			    "Instruction Tracing options\n" ITRACE_HELP,
781 			    itrace_parse_synth_opts),
782 	OPT_CALLBACK_OPTARG(0, "data-type", &annotate, NULL, "name",
783 			    "Show data type annotate for the memory accesses",
784 			    parse_data_type),
785 	OPT_BOOLEAN(0, "type-stat", &annotate.type_stat,
786 		    "Show stats for the data type annotation"),
787 	OPT_BOOLEAN(0, "insn-stat", &annotate.insn_stat,
788 		    "Show instruction stats for the data type annotation"),
789 	OPT_BOOLEAN(0, "skip-empty", &symbol_conf.skip_empty,
790 		    "Do not display empty (or dummy) events in the output"),
791 	OPT_END()
792 	};
793 	int ret;
794 
795 	set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
796 	set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
797 
798 	annotation_options__init();
799 
800 	ret = hists__init();
801 	if (ret < 0)
802 		return ret;
803 
804 	annotation_config__init();
805 
806 	argc = parse_options(argc, argv, options, annotate_usage, 0);
807 	if (argc) {
808 		/*
809 		 * Special case: if there's an argument left then assume that
810 		 * it's a symbol filter:
811 		 */
812 		if (argc > 1)
813 			usage_with_options(annotate_usage, options);
814 
815 		annotate.sym_hist_filter = argv[0];
816 	}
817 
818 	if (disassembler_style) {
819 		annotate_opts.disassembler_style = strdup(disassembler_style);
820 		if (!annotate_opts.disassembler_style)
821 			return -ENOMEM;
822 	}
823 	if (objdump_path) {
824 		annotate_opts.objdump_path = strdup(objdump_path);
825 		if (!annotate_opts.objdump_path)
826 			return -ENOMEM;
827 	}
828 	if (addr2line_path) {
829 		symbol_conf.addr2line_path = strdup(addr2line_path);
830 		if (!symbol_conf.addr2line_path)
831 			return -ENOMEM;
832 	}
833 
834 	if (annotate_check_args() < 0)
835 		return -EINVAL;
836 
837 #ifdef HAVE_GTK2_SUPPORT
838 	if (symbol_conf.show_nr_samples && annotate.use_gtk) {
839 		pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
840 		return ret;
841 	}
842 #endif
843 
844 #ifndef HAVE_LIBDW_SUPPORT
845 	if (annotate.data_type) {
846 		pr_err("Error: Data type profiling is disabled due to missing DWARF support\n");
847 		return -ENOTSUP;
848 	}
849 #endif
850 
851 	ret = symbol__validate_sym_arguments();
852 	if (ret)
853 		return ret;
854 
855 	if (quiet)
856 		perf_quiet_option();
857 
858 	data.path = input_name;
859 
860 	perf_tool__init(&annotate.tool, /*ordered_events=*/true);
861 	annotate.tool.sample	= process_sample_event;
862 	annotate.tool.mmap	= perf_event__process_mmap;
863 	annotate.tool.mmap2	= perf_event__process_mmap2;
864 	annotate.tool.comm	= perf_event__process_comm;
865 	annotate.tool.exit	= perf_event__process_exit;
866 	annotate.tool.fork	= perf_event__process_fork;
867 	annotate.tool.namespaces = perf_event__process_namespaces;
868 	annotate.tool.attr	= perf_event__process_attr;
869 	annotate.tool.build_id = perf_event__process_build_id;
870 #ifdef HAVE_LIBTRACEEVENT
871 	annotate.tool.tracing_data   = perf_event__process_tracing_data;
872 #endif
873 	annotate.tool.id_index	= perf_event__process_id_index;
874 	annotate.tool.auxtrace_info	= perf_event__process_auxtrace_info;
875 	annotate.tool.auxtrace	= perf_event__process_auxtrace;
876 	annotate.tool.feature	= process_feature_event;
877 	annotate.tool.ordering_requires_timestamps = true;
878 
879 	annotate.session = perf_session__new(&data, &annotate.tool);
880 	if (IS_ERR(annotate.session))
881 		return PTR_ERR(annotate.session);
882 
883 	annotate.session->itrace_synth_opts = &itrace_synth_opts;
884 
885 	annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
886 						      HEADER_BRANCH_STACK);
887 
888 	if (annotate.group_set)
889 		evlist__force_leader(annotate.session->evlist);
890 
891 	ret = symbol__annotation_init();
892 	if (ret < 0)
893 		goto out_delete;
894 
895 	symbol_conf.try_vmlinux_path = true;
896 
897 	ret = symbol__init(&annotate.session->header.env);
898 	if (ret < 0)
899 		goto out_delete;
900 
901 	if (annotate.use_stdio || annotate.use_stdio2)
902 		use_browser = 0;
903 #ifdef HAVE_SLANG_SUPPORT
904 	else if (annotate.use_tui)
905 		use_browser = 1;
906 #endif
907 #ifdef HAVE_GTK2_SUPPORT
908 	else if (annotate.use_gtk)
909 		use_browser = 2;
910 #endif
911 
912 	if (annotate.data_type) {
913 		annotate_opts.annotate_src = false;
914 		symbol_conf.annotate_data_member = true;
915 		symbol_conf.annotate_data_sample = true;
916 	}
917 
918 	setup_browser(true);
919 
920 	/*
921 	 * Events of different processes may correspond to the same
922 	 * symbol, we do not care about the processes in annotate,
923 	 * set sort order to avoid repeated output.
924 	 */
925 	if (annotate.data_type)
926 		sort_order = "dso,type";
927 	else
928 		sort_order = "dso,symbol";
929 
930 	/*
931 	 * Set SORT_MODE__BRANCH so that annotate displays IPC/Cycle and
932 	 * branch counters, if the corresponding branch info is available
933 	 * in the perf data in the TUI mode.
934 	 */
935 	if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
936 		sort__mode = SORT_MODE__BRANCH;
937 		if (annotate.session->evlist->nr_br_cntr > 0)
938 			annotate_opts.show_br_cntr = true;
939 	}
940 
941 	if (setup_sorting(NULL) < 0)
942 		usage_with_options(annotate_usage, options);
943 
944 	ret = __cmd_annotate(&annotate);
945 
946 out_delete:
947 	/*
948 	 * Speed up the exit process by only deleting for debug builds. For
949 	 * large files this can save time.
950 	 */
951 #ifndef NDEBUG
952 	perf_session__delete(annotate.session);
953 #endif
954 	annotation_options__exit();
955 
956 	return ret;
957 }
958