File Index
Files and directories
Sign in
FILE

builtin-annotate.c

File preview with syntax highlighting.

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