builtin-annotate.c 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * builtin-annotate.c
 *
 * Builtin annotate command: Analyze the perf.data input file,
 * look up and read DSOs and symbol information and display
 * a histogram of results, along various sorting keys.
 */
#include "builtin.h"

10
#include "util/util.h"
11
#include "util/color.h"
12
#include <linux/list.h>
13
#include "util/cache.h"
14
#include <linux/rbtree.h>
15 16 17
#include "util/symbol.h"

#include "perf.h"
18
#include "util/debug.h"
19

20 21
#include "util/evlist.h"
#include "util/evsel.h"
22
#include "util/annotate.h"
23
#include "util/event.h"
24 25
#include "util/parse-options.h"
#include "util/parse-events.h"
26
#include "util/thread.h"
27
#include "util/sort.h"
28
#include "util/hist.h"
29
#include "util/session.h"
30 31 32

static char		const *input_name = "perf.data";

33
static bool		force, use_tui, use_stdio;
34

35
static bool		full_paths;
36

37
static bool		print_line;
38

39 40
static const char *sym_hist_filter;

41 42
static int perf_evlist__add_sample(struct perf_evlist *evlist,
				   struct perf_sample *sample,
43
				   struct perf_evsel *evsel,
44
				   struct addr_location *al)
45
{
46
	struct hist_entry *he;
47
	int ret;
48 49 50 51 52 53 54 55 56 57 58 59

	if (sym_hist_filter != NULL &&
	    (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
		/* We're only interested in a symbol named sym_hist_filter */
		if (al->sym != NULL) {
			rb_erase(&al->sym->rb_node,
				 &al->map->dso->symbols[al->map->type]);
			symbol__delete(al->sym);
		}
		return 0;
	}

60
	he = __hists__add_entry(&evsel->hists, al, NULL, 1);
61
	if (he == NULL)
62
		return -ENOMEM;
63

64
	ret = 0;
65 66
	if (he->ms.sym != NULL) {
		struct annotation *notes = symbol__annotation(he->ms.sym);
67
		if (notes->src == NULL &&
68
		    symbol__alloc_hist(he->ms.sym, evlist->nr_entries) < 0)
69 70
			return -ENOMEM;

71
		ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
72 73
	}

74 75 76
	evsel->hists.stats.total_period += sample->period;
	hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
	return ret;
77 78
}

79 80
static int process_sample_event(union perf_event *event,
				struct perf_sample *sample,
81
				struct perf_evsel *evsel,
82
				struct perf_session *session)
83
{
84
	struct addr_location al;
85

86 87
	if (perf_event__preprocess_sample(event, session, &al, sample,
					  symbol__annotate_init) < 0) {
88 89
		pr_warning("problem processing %d event, skipping it.\n",
			   event->header.type);
90 91 92
		return -1;
	}

93 94
	if (!al.filtered &&
	    perf_evlist__add_sample(session->evlist, sample, evsel, &al)) {
95 96
		pr_warning("problem incrementing symbol count, "
			   "skipping event\n");
97
		return -1;
98 99 100 101 102
	}

	return 0;
}

103
static int hist_entry__tty_annotate(struct hist_entry *he, int evidx)
104
{
105
	return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx,
106
				    print_line, full_paths, 0, 0);
107 108
}

109
static void hists__find_annotations(struct hists *self, int evidx)
110
{
111
	struct rb_node *nd = rb_first(&self->entries), *next;
112
	int key = KEY_RIGHT;
113

114
	while (nd) {
115
		struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
116
		struct annotation *notes;
117

118 119
		if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
			goto find_next;
120

121
		notes = symbol__annotation(he->ms.sym);
122
		if (notes->src == NULL) {
123 124 125 126 127
find_next:
			if (key == KEY_LEFT)
				nd = rb_prev(nd);
			else
				nd = rb_next(nd);
128
			continue;
129
		}
130

131
		if (use_browser > 0) {
132
			key = hist_entry__tui_annotate(he, evidx);
133 134
			switch (key) {
			case KEY_RIGHT:
135
				next = rb_next(nd);
136 137
				break;
			case KEY_LEFT:
138
				next = rb_prev(nd);
139
				break;
140 141
			default:
				return;
142
			}
143 144 145

			if (next != NULL)
				nd = next;
146
		} else {
147
			hist_entry__tty_annotate(he, evidx);
148 149 150
			nd = rb_next(nd);
			/*
			 * Since we have a hist_entry per IP for the same
151
			 * symbol, free he->ms.sym->src to signal we already
152 153
			 * processed this symbol.
			 */
154 155
			free(notes->src);
			notes->src = NULL;
156
		}
157 158 159
	}
}

160
static struct perf_event_ops event_ops = {
161
	.sample	= process_sample_event,
162 163 164
	.mmap	= perf_event__process_mmap,
	.comm	= perf_event__process_comm,
	.fork	= perf_event__process_task,
165 166
	.ordered_samples = true,
	.ordering_requires_timestamps = true,
L
Li Zefan 已提交
167 168
};

169 170
static int __cmd_annotate(void)
{
L
Li Zefan 已提交
171
	int ret;
172
	struct perf_session *session;
173 174
	struct perf_evsel *pos;
	u64 total_nr_samples;
175

176
	session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops);
177 178 179
	if (session == NULL)
		return -ENOMEM;

180
	ret = perf_session__process_events(session, &event_ops);
L
Li Zefan 已提交
181
	if (ret)
182
		goto out_delete;
183

184
	if (dump_trace) {
185
		perf_session__fprintf_nr_events(session, stdout);
186
		goto out_delete;
187
	}
188

189
	if (verbose > 3)
190
		perf_session__fprintf(session, stdout);
191

192
	if (verbose > 2)
193
		perf_session__fprintf_dsos(session, stdout);
194

195 196 197 198 199 200 201 202 203 204 205 206
	total_nr_samples = 0;
	list_for_each_entry(pos, &session->evlist->entries, node) {
		struct hists *hists = &pos->hists;
		u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];

		if (nr_samples > 0) {
			total_nr_samples += nr_samples;
			hists__collapse_resort(hists);
			hists__output_resort(hists);
			hists__find_annotations(hists, pos->idx);
		}
	}
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	if (total_nr_samples == 0) {
		ui__warning("The %s file has no samples!\n", input_name);
		goto out_delete;
	}
out_delete:
	/*
	 * Speed up the exit process, for large files this can
	 * take quite a while.
	 *
	 * XXX Enable this when using valgrind or if we ever
	 * librarize this command.
	 *
	 * Also experiment with obstacks to see how much speed
	 * up we'll get here.
	 *
	 * perf_session__delete(session);
	 */
L
Li Zefan 已提交
225
	return ret;
226 227 228 229 230 231 232 233 234 235
}

static const char * const annotate_usage[] = {
	"perf annotate [<options>] <command>",
	NULL
};

static const struct option options[] = {
	OPT_STRING('i', "input", &input_name, "file",
		    "input file name"),
236 237
	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
		   "only consider symbols in these dsos"),
238
	OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
239
		    "symbol to annotate"),
240
	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
241
	OPT_INCR('v', "verbose", &verbose,
242 243 244
		    "be more verbose (show symbol address, etc)"),
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
245 246
	OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
	OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
247 248 249
	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
		   "file", "vmlinux pathname"),
	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
250
		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
251 252
	OPT_BOOLEAN('l', "print-line", &print_line,
		    "print matching source lines (may be slow)"),
253 254
	OPT_BOOLEAN('P', "full-paths", &full_paths,
		    "Don't shorten the displayed pathnames"),
255 256 257
	OPT_END()
};

258
int cmd_annotate(int argc, const char **argv, const char *prefix __used)
259
{
260 261
	argc = parse_options(argc, argv, options, annotate_usage, 0);

262 263 264 265 266
	if (use_stdio)
		use_browser = 0;
	else if (use_tui)
		use_browser = 1;

267
	setup_browser(true);
268

269
	symbol_conf.priv_size = sizeof(struct annotation);
270 271 272
	symbol_conf.try_vmlinux_path = true;

	if (symbol__init() < 0)
273
		return -1;
274

275
	setup_sorting(annotate_usage, options);
276

277 278 279 280 281 282 283 284 285 286 287
	if (argc) {
		/*
		 * Special case: if there's an argument left then assume tha
		 * it's a symbol filter:
		 */
		if (argc > 1)
			usage_with_options(annotate_usage, options);

		sym_hist_filter = argv[0];
	}

288
	if (field_sep && *field_sep == '.') {
289 290
		pr_err("'.' is the only non valid --field-separator argument\n");
		return -1;
291 292
	}

293 294
	return __cmd_annotate();
}