xref: /openbmc/linux/tools/perf/builtin-annotate.c (revision d040bd36)
186470930SIngo Molnar /*
286470930SIngo Molnar  * builtin-annotate.c
386470930SIngo Molnar  *
486470930SIngo Molnar  * Builtin annotate command: Analyze the perf.data input file,
586470930SIngo Molnar  * look up and read DSOs and symbol information and display
686470930SIngo Molnar  * a histogram of results, along various sorting keys.
786470930SIngo Molnar  */
886470930SIngo Molnar #include "builtin.h"
986470930SIngo Molnar 
1086470930SIngo Molnar #include "util/util.h"
1186470930SIngo Molnar 
1278f7defeSArnaldo Carvalho de Melo #include "util/util.h"
1386470930SIngo Molnar #include "util/color.h"
145da50258SArnaldo Carvalho de Melo #include <linux/list.h>
1586470930SIngo Molnar #include "util/cache.h"
1643cbcd8aSArnaldo Carvalho de Melo #include <linux/rbtree.h>
1786470930SIngo Molnar #include "util/symbol.h"
1886470930SIngo Molnar 
1986470930SIngo Molnar #include "perf.h"
208f28827aSFrederic Weisbecker #include "util/debug.h"
2186470930SIngo Molnar 
2278f7defeSArnaldo Carvalho de Melo #include "util/annotate.h"
2362daacb5SArnaldo Carvalho de Melo #include "util/event.h"
2486470930SIngo Molnar #include "util/parse-options.h"
2586470930SIngo Molnar #include "util/parse-events.h"
266baa0a5aSFrederic Weisbecker #include "util/thread.h"
27dd68ada2SJohn Kacur #include "util/sort.h"
283d1d07ecSJohn Kacur #include "util/hist.h"
2994c744b6SArnaldo Carvalho de Melo #include "util/session.h"
3086470930SIngo Molnar 
3186470930SIngo Molnar static char		const *input_name = "perf.data";
3286470930SIngo Molnar 
338b9e74ebSArnaldo Carvalho de Melo static bool		force, use_tui, use_stdio;
3486470930SIngo Molnar 
35c0555642SIan Munsie static bool		full_paths;
3642976487SMike Galbraith 
37c0555642SIan Munsie static bool		print_line;
38301406b9SFrederic Weisbecker 
39e4204992SArnaldo Carvalho de Melo static const char *sym_hist_filter;
40e4204992SArnaldo Carvalho de Melo 
411c02c4d2SArnaldo Carvalho de Melo static int hists__add_entry(struct hists *self, struct addr_location *al)
4286470930SIngo Molnar {
43628ada0cSArnaldo Carvalho de Melo 	struct hist_entry *he;
44628ada0cSArnaldo Carvalho de Melo 
45628ada0cSArnaldo Carvalho de Melo 	if (sym_hist_filter != NULL &&
46628ada0cSArnaldo Carvalho de Melo 	    (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
47628ada0cSArnaldo Carvalho de Melo 		/* We're only interested in a symbol named sym_hist_filter */
48628ada0cSArnaldo Carvalho de Melo 		if (al->sym != NULL) {
49628ada0cSArnaldo Carvalho de Melo 			rb_erase(&al->sym->rb_node,
50628ada0cSArnaldo Carvalho de Melo 				 &al->map->dso->symbols[al->map->type]);
51628ada0cSArnaldo Carvalho de Melo 			symbol__delete(al->sym);
52628ada0cSArnaldo Carvalho de Melo 		}
53628ada0cSArnaldo Carvalho de Melo 		return 0;
54628ada0cSArnaldo Carvalho de Melo 	}
55628ada0cSArnaldo Carvalho de Melo 
561c02c4d2SArnaldo Carvalho de Melo 	he = __hists__add_entry(self, al, NULL, 1);
579735abf1SArnaldo Carvalho de Melo 	if (he == NULL)
5886470930SIngo Molnar 		return -ENOMEM;
59628ada0cSArnaldo Carvalho de Melo 
602f525d01SArnaldo Carvalho de Melo 	if (he->ms.sym != NULL) {
612f525d01SArnaldo Carvalho de Melo 		/*
622f525d01SArnaldo Carvalho de Melo 		 * All aggregated on the first sym_hist.
632f525d01SArnaldo Carvalho de Melo 		 */
642f525d01SArnaldo Carvalho de Melo 		struct annotation *notes = symbol__annotation(he->ms.sym);
652f525d01SArnaldo Carvalho de Melo 		if (notes->histograms == NULL && symbol__alloc_hist(he->ms.sym, 1) < 0)
662f525d01SArnaldo Carvalho de Melo 			return -ENOMEM;
672f525d01SArnaldo Carvalho de Melo 
682f525d01SArnaldo Carvalho de Melo 		return hist_entry__inc_addr_samples(he, 0, al->addr);
692f525d01SArnaldo Carvalho de Melo 	}
702f525d01SArnaldo Carvalho de Melo 
712f525d01SArnaldo Carvalho de Melo 	return 0;
7286470930SIngo Molnar }
7386470930SIngo Molnar 
748115d60cSArnaldo Carvalho de Melo static int process_sample_event(union perf_event *event,
758115d60cSArnaldo Carvalho de Melo 				struct perf_sample *sample,
76640c03ceSArnaldo Carvalho de Melo 				struct perf_session *session)
7786470930SIngo Molnar {
781ed091c4SArnaldo Carvalho de Melo 	struct addr_location al;
796baa0a5aSFrederic Weisbecker 
808115d60cSArnaldo Carvalho de Melo 	if (perf_event__preprocess_sample(event, session, &al, sample, NULL) < 0) {
8129a9f66dSArnaldo Carvalho de Melo 		pr_warning("problem processing %d event, skipping it.\n",
8286470930SIngo Molnar 			   event->header.type);
8386470930SIngo Molnar 		return -1;
8486470930SIngo Molnar 	}
8586470930SIngo Molnar 
861c02c4d2SArnaldo Carvalho de Melo 	if (!al.filtered && hists__add_entry(&session->hists, &al)) {
8729a9f66dSArnaldo Carvalho de Melo 		pr_warning("problem incrementing symbol count, "
88ec218fc4SArnaldo Carvalho de Melo 			   "skipping event\n");
8986470930SIngo Molnar 		return -1;
9086470930SIngo Molnar 	}
9186470930SIngo Molnar 
9286470930SIngo Molnar 	return 0;
9386470930SIngo Molnar }
9486470930SIngo Molnar 
952f525d01SArnaldo Carvalho de Melo static int hist_entry__tty_annotate(struct hist_entry *he, int evidx)
9686470930SIngo Molnar {
972f525d01SArnaldo Carvalho de Melo 	return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx,
98*d040bd36SArnaldo Carvalho de Melo 				    print_line, full_paths, 0, 0);
9986470930SIngo Molnar }
10086470930SIngo Molnar 
1011c02c4d2SArnaldo Carvalho de Melo static void hists__find_annotations(struct hists *self)
10286470930SIngo Molnar {
103b50e003dSArnaldo Carvalho de Melo 	struct rb_node *nd = rb_first(&self->entries), *next;
10446e3e055SArnaldo Carvalho de Melo 	int key = KEY_RIGHT;
10586470930SIngo Molnar 
10646e3e055SArnaldo Carvalho de Melo 	while (nd) {
107ed52ce2eSArnaldo Carvalho de Melo 		struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
10878f7defeSArnaldo Carvalho de Melo 		struct annotation *notes;
10986470930SIngo Molnar 
11046e3e055SArnaldo Carvalho de Melo 		if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
11146e3e055SArnaldo Carvalho de Melo 			goto find_next;
112e4204992SArnaldo Carvalho de Melo 
11378f7defeSArnaldo Carvalho de Melo 		notes = symbol__annotation(he->ms.sym);
1142f525d01SArnaldo Carvalho de Melo 		if (notes->histograms == NULL) {
11546e3e055SArnaldo Carvalho de Melo find_next:
11646e3e055SArnaldo Carvalho de Melo 			if (key == KEY_LEFT)
11746e3e055SArnaldo Carvalho de Melo 				nd = rb_prev(nd);
11846e3e055SArnaldo Carvalho de Melo 			else
11946e3e055SArnaldo Carvalho de Melo 				nd = rb_next(nd);
120e4204992SArnaldo Carvalho de Melo 			continue;
12146e3e055SArnaldo Carvalho de Melo 		}
122e4204992SArnaldo Carvalho de Melo 
12362e3436bSArnaldo Carvalho de Melo 		if (use_browser > 0) {
1242f525d01SArnaldo Carvalho de Melo 			/* For now all is aggregated on the first */
1252f525d01SArnaldo Carvalho de Melo 			key = hist_entry__tui_annotate(he, 0);
12646e3e055SArnaldo Carvalho de Melo 			switch (key) {
12746e3e055SArnaldo Carvalho de Melo 			case KEY_RIGHT:
128b50e003dSArnaldo Carvalho de Melo 				next = rb_next(nd);
12946e3e055SArnaldo Carvalho de Melo 				break;
13046e3e055SArnaldo Carvalho de Melo 			case KEY_LEFT:
131b50e003dSArnaldo Carvalho de Melo 				next = rb_prev(nd);
13246e3e055SArnaldo Carvalho de Melo 				break;
133b50e003dSArnaldo Carvalho de Melo 			default:
134b50e003dSArnaldo Carvalho de Melo 				return;
13546e3e055SArnaldo Carvalho de Melo 			}
136b50e003dSArnaldo Carvalho de Melo 
137b50e003dSArnaldo Carvalho de Melo 			if (next != NULL)
138b50e003dSArnaldo Carvalho de Melo 				nd = next;
13946e3e055SArnaldo Carvalho de Melo 		} else {
1402f525d01SArnaldo Carvalho de Melo 			/* For now all is aggregated on the first */
1412f525d01SArnaldo Carvalho de Melo 			hist_entry__tty_annotate(he, 0);
14246e3e055SArnaldo Carvalho de Melo 			nd = rb_next(nd);
143ed52ce2eSArnaldo Carvalho de Melo 			/*
14446e3e055SArnaldo Carvalho de Melo 			 * Since we have a hist_entry per IP for the same
14578f7defeSArnaldo Carvalho de Melo 			 * symbol, free he->ms.sym->histogram to signal we already
14646e3e055SArnaldo Carvalho de Melo 			 * processed this symbol.
147ed52ce2eSArnaldo Carvalho de Melo 			 */
1482f525d01SArnaldo Carvalho de Melo 			free(notes->histograms);
1492f525d01SArnaldo Carvalho de Melo 			notes->histograms = NULL;
15086470930SIngo Molnar 		}
15186470930SIngo Molnar 	}
15246e3e055SArnaldo Carvalho de Melo }
15386470930SIngo Molnar 
154301a0b02SArnaldo Carvalho de Melo static struct perf_event_ops event_ops = {
15555aa640fSArnaldo Carvalho de Melo 	.sample	= process_sample_event,
1568115d60cSArnaldo Carvalho de Melo 	.mmap	= perf_event__process_mmap,
1578115d60cSArnaldo Carvalho de Melo 	.comm	= perf_event__process_comm,
1588115d60cSArnaldo Carvalho de Melo 	.fork	= perf_event__process_task,
159eac23d1cSIan Munsie 	.ordered_samples = true,
160eac23d1cSIan Munsie 	.ordering_requires_timestamps = true,
161bab81b62SLi Zefan };
162bab81b62SLi Zefan 
16386470930SIngo Molnar static int __cmd_annotate(void)
16486470930SIngo Molnar {
165bab81b62SLi Zefan 	int ret;
16675be6cf4SArnaldo Carvalho de Melo 	struct perf_session *session;
16786470930SIngo Molnar 
16821ef97f0SIan Munsie 	session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops);
16994c744b6SArnaldo Carvalho de Melo 	if (session == NULL)
17094c744b6SArnaldo Carvalho de Melo 		return -ENOMEM;
17194c744b6SArnaldo Carvalho de Melo 
172ec913369SArnaldo Carvalho de Melo 	ret = perf_session__process_events(session, &event_ops);
173bab81b62SLi Zefan 	if (ret)
17494c744b6SArnaldo Carvalho de Melo 		goto out_delete;
17586470930SIngo Molnar 
17662daacb5SArnaldo Carvalho de Melo 	if (dump_trace) {
177c8446b9bSArnaldo Carvalho de Melo 		perf_session__fprintf_nr_events(session, stdout);
17894c744b6SArnaldo Carvalho de Melo 		goto out_delete;
17962daacb5SArnaldo Carvalho de Melo 	}
18086470930SIngo Molnar 
181da21d1b5SArnaldo Carvalho de Melo 	if (verbose > 3)
182b3165f41SArnaldo Carvalho de Melo 		perf_session__fprintf(session, stdout);
18386470930SIngo Molnar 
184da21d1b5SArnaldo Carvalho de Melo 	if (verbose > 2)
185cbf69680SArnaldo Carvalho de Melo 		perf_session__fprintf_dsos(session, stdout);
18686470930SIngo Molnar 
1871c02c4d2SArnaldo Carvalho de Melo 	hists__collapse_resort(&session->hists);
1881c02c4d2SArnaldo Carvalho de Melo 	hists__output_resort(&session->hists);
1891c02c4d2SArnaldo Carvalho de Melo 	hists__find_annotations(&session->hists);
19094c744b6SArnaldo Carvalho de Melo out_delete:
19194c744b6SArnaldo Carvalho de Melo 	perf_session__delete(session);
19286470930SIngo Molnar 
193bab81b62SLi Zefan 	return ret;
19486470930SIngo Molnar }
19586470930SIngo Molnar 
19686470930SIngo Molnar static const char * const annotate_usage[] = {
19786470930SIngo Molnar 	"perf annotate [<options>] <command>",
19886470930SIngo Molnar 	NULL
19986470930SIngo Molnar };
20086470930SIngo Molnar 
20186470930SIngo Molnar static const struct option options[] = {
20286470930SIngo Molnar 	OPT_STRING('i', "input", &input_name, "file",
20386470930SIngo Molnar 		    "input file name"),
204ac73c5a9SArnaldo Carvalho de Melo 	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
205ac73c5a9SArnaldo Carvalho de Melo 		   "only consider symbols in these dsos"),
20623b87116SIngo Molnar 	OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
20786470930SIngo Molnar 		    "symbol to annotate"),
208fa6963b2SPeter Zijlstra 	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
209c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
21086470930SIngo Molnar 		    "be more verbose (show symbol address, etc)"),
21186470930SIngo Molnar 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
21286470930SIngo Molnar 		    "dump raw trace in ASCII"),
2138b9e74ebSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
2148b9e74ebSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
215b32d133aSArnaldo Carvalho de Melo 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
216b32d133aSArnaldo Carvalho de Melo 		   "file", "vmlinux pathname"),
217b32d133aSArnaldo Carvalho de Melo 	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
21842976487SMike Galbraith 		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
219301406b9SFrederic Weisbecker 	OPT_BOOLEAN('l', "print-line", &print_line,
220301406b9SFrederic Weisbecker 		    "print matching source lines (may be slow)"),
22142976487SMike Galbraith 	OPT_BOOLEAN('P', "full-paths", &full_paths,
22242976487SMike Galbraith 		    "Don't shorten the displayed pathnames"),
22386470930SIngo Molnar 	OPT_END()
22486470930SIngo Molnar };
22586470930SIngo Molnar 
226f37a291cSIngo Molnar int cmd_annotate(int argc, const char **argv, const char *prefix __used)
22786470930SIngo Molnar {
228655000e7SArnaldo Carvalho de Melo 	argc = parse_options(argc, argv, options, annotate_usage, 0);
229655000e7SArnaldo Carvalho de Melo 
2308b9e74ebSArnaldo Carvalho de Melo 	if (use_stdio)
2318b9e74ebSArnaldo Carvalho de Melo 		use_browser = 0;
2328b9e74ebSArnaldo Carvalho de Melo 	else if (use_tui)
2338b9e74ebSArnaldo Carvalho de Melo 		use_browser = 1;
2348b9e74ebSArnaldo Carvalho de Melo 
235229ade9bSArnaldo Carvalho de Melo 	setup_browser(true);
23646e3e055SArnaldo Carvalho de Melo 
23778f7defeSArnaldo Carvalho de Melo 	symbol_conf.priv_size = sizeof(struct annotation);
23875be6cf4SArnaldo Carvalho de Melo 	symbol_conf.try_vmlinux_path = true;
23975be6cf4SArnaldo Carvalho de Melo 
24075be6cf4SArnaldo Carvalho de Melo 	if (symbol__init() < 0)
241b32d133aSArnaldo Carvalho de Melo 		return -1;
24286470930SIngo Molnar 
243c8829c7aSArnaldo Carvalho de Melo 	setup_sorting(annotate_usage, options);
24486470930SIngo Molnar 
24586470930SIngo Molnar 	if (argc) {
24686470930SIngo Molnar 		/*
24786470930SIngo Molnar 		 * Special case: if there's an argument left then assume tha
24886470930SIngo Molnar 		 * it's a symbol filter:
24986470930SIngo Molnar 		 */
25086470930SIngo Molnar 		if (argc > 1)
25186470930SIngo Molnar 			usage_with_options(annotate_usage, options);
25286470930SIngo Molnar 
25386470930SIngo Molnar 		sym_hist_filter = argv[0];
25486470930SIngo Molnar 	}
25586470930SIngo Molnar 
256dd68ada2SJohn Kacur 	if (field_sep && *field_sep == '.') {
25729a9f66dSArnaldo Carvalho de Melo 		pr_err("'.' is the only non valid --field-separator argument\n");
25829a9f66dSArnaldo Carvalho de Melo 		return -1;
259dd68ada2SJohn Kacur 	}
260dd68ada2SJohn Kacur 
26186470930SIngo Molnar 	return __cmd_annotate();
26286470930SIngo Molnar }
263