builtin-annotate.c 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * 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"

#include "util/util.h"

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

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

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

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

31
static bool		force;
32

33
static bool		full_paths;
34

35
static bool		print_line;
36

37 38
static const char *sym_hist_filter;

39
static int hists__add_entry(struct hists *self, struct addr_location *al)
40
{
41 42 43 44 45 46 47 48 49 50 51 52 53
	struct hist_entry *he;

	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;
	}

54
	he = __hists__add_entry(self, al, NULL, 1);
55
	if (he == NULL)
56
		return -ENOMEM;
57

58
	return hist_entry__inc_addr_samples(he, al->addr);
59 60
}

61
static int process_sample_event(event_t *event, struct perf_session *session)
62
{
63
	struct addr_location al;
64

65 66
	dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
		    event->ip.pid, event->ip.ip);
67

68
	if (event__preprocess_sample(event, session, &al, NULL) < 0) {
69 70
		pr_warning("problem processing %d event, skipping it.\n",
			   event->header.type);
71 72 73
		return -1;
	}

74
	if (!al.filtered && hists__add_entry(&session->hists, &al)) {
75 76
		pr_warning("problem incrementing symbol count, "
			   "skipping event\n");
77
		return -1;
78 79 80 81 82
	}

	return 0;
}

83 84 85 86
static int objdump_line__print(struct objdump_line *self,
			       struct list_head *head,
			       struct hist_entry *he, u64 len)
{
87
	struct symbol *sym = he->ms.sym;
88 89 90 91
	static const char *prev_line;
	static const char *prev_color;

	if (self->offset != -1) {
92
		const char *path = NULL;
93 94
		unsigned int hits = 0;
		double percent = 0.0;
95
		const char *color;
96
		struct sym_priv *priv = symbol__priv(sym);
97 98
		struct sym_ext *sym_ext = priv->ext;
		struct sym_hist *h = priv->hist;
99 100 101 102 103 104 105 106 107 108 109 110 111 112
		s64 offset = self->offset;
		struct objdump_line *next = objdump__get_next_ip_line(head, self);

		while (offset < (s64)len &&
		       (next == NULL || offset < next->offset)) {
			if (sym_ext) {
				if (path == NULL)
					path = sym_ext[offset].path;
				percent += sym_ext[offset].percent;
			} else
				hits += h->ip[offset];

			++offset;
		}
113

114
		if (sym_ext == NULL && h->sum)
115
			percent = 100.0 * hits / h->sum;
116

117
		color = get_percent_color(percent);
118

119 120 121 122 123 124 125 126 127 128 129 130 131 132
		/*
		 * Also color the filename and line if needed, with
		 * the same color than the percentage. Don't print it
		 * twice for close colored ip with the same filename:line
		 */
		if (path) {
			if (!prev_line || strcmp(prev_line, path)
				       || color != prev_color) {
				color_fprintf(stdout, color, " %s", path);
				prev_line = path;
				prev_color = color;
			}
		}

133 134
		color_fprintf(stdout, color, " %7.2f", percent);
		printf(" :	");
135
		color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
136
	} else {
137
		if (!*self->line)
138 139
			printf("         :\n");
		else
140
			printf("         :	%s\n", self->line);
141 142 143 144 145
	}

	return 0;
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static struct rb_root root_sym_ext;

static void insert_source_line(struct sym_ext *sym_ext)
{
	struct sym_ext *iter;
	struct rb_node **p = &root_sym_ext.rb_node;
	struct rb_node *parent = NULL;

	while (*p != NULL) {
		parent = *p;
		iter = rb_entry(parent, struct sym_ext, node);

		if (sym_ext->percent > iter->percent)
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}

	rb_link_node(&sym_ext->node, parent, p);
	rb_insert_color(&sym_ext->node, &root_sym_ext);
}

168
static void free_source_line(struct hist_entry *he, int len)
169
{
170
	struct sym_priv *priv = symbol__priv(he->ms.sym);
171
	struct sym_ext *sym_ext = priv->ext;
172 173 174 175 176 177 178 179 180
	int i;

	if (!sym_ext)
		return;

	for (i = 0; i < len; i++)
		free(sym_ext[i].path);
	free(sym_ext);

181
	priv->ext = NULL;
182
	root_sym_ext = RB_ROOT;
183 184 185
}

/* Get the filename:line for the colored entries */
186
static void
187
get_source_line(struct hist_entry *he, int len, const char *filename)
188
{
189
	struct symbol *sym = he->ms.sym;
190
	u64 start;
191 192 193
	int i;
	char cmd[PATH_MAX * 2];
	struct sym_ext *sym_ext;
194
	struct sym_priv *priv = symbol__priv(sym);
195
	struct sym_hist *h = priv->hist;
196

197
	if (!h->sum)
198 199
		return;

200 201
	sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
	if (!priv->ext)
202 203
		return;

204
	start = he->ms.map->unmap_ip(he->ms.map, sym->start);
205 206 207 208

	for (i = 0; i < len; i++) {
		char *path = NULL;
		size_t line_len;
209
		u64 offset;
210 211
		FILE *fp;

212
		sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
213 214 215
		if (sym_ext[i].percent <= 0.5)
			continue;

216
		offset = start + i;
217
		sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
218 219 220 221 222 223 224
		fp = popen(cmd, "r");
		if (!fp)
			continue;

		if (getline(&path, &line_len, fp) < 0 || !line_len)
			goto next;

225
		sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
226 227 228 229
		if (!sym_ext[i].path)
			goto next;

		strcpy(sym_ext[i].path, path);
230
		insert_source_line(&sym_ext[i]);
231 232 233 234 235 236

	next:
		pclose(fp);
	}
}

237
static void print_summary(const char *filename)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
{
	struct sym_ext *sym_ext;
	struct rb_node *node;

	printf("\nSorted summary for file %s\n", filename);
	printf("----------------------------------------------\n\n");

	if (RB_EMPTY_ROOT(&root_sym_ext)) {
		printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
		return;
	}

	node = rb_first(&root_sym_ext);
	while (node) {
		double percent;
253
		const char *color;
254 255 256 257
		char *path;

		sym_ext = rb_entry(node, struct sym_ext, node);
		percent = sym_ext->percent;
258
		color = get_percent_color(percent);
259 260 261 262 263 264 265
		path = sym_ext->path;

		color_fprintf(stdout, color, " %7.2f %s", percent, path);
		node = rb_next(node);
	}
}

266 267
static void hist_entry__print_hits(struct hist_entry *self)
{
268
	struct symbol *sym = self->ms.sym;
269 270 271 272 273 274 275 276 277 278 279
	struct sym_priv *priv = symbol__priv(sym);
	struct sym_hist *h = priv->hist;
	u64 len = sym->end - sym->start, offset;

	for (offset = 0; offset < len; ++offset)
		if (h->ip[offset] != 0)
			printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
			       sym->start + offset, h->ip[offset]);
	printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
}

280
static void annotate_sym(struct hist_entry *he)
281
{
282
	struct map *map = he->ms.map;
283
	struct dso *dso = map->dso;
284
	struct symbol *sym = he->ms.sym;
285 286
	const char *filename = dso->long_name, *d_filename;
	u64 len;
287 288
	LIST_HEAD(head);
	struct objdump_line *pos, *n;
289

290
	if (hist_entry__annotate(he, &head) < 0)
291
		return;
292

293 294 295 296
	if (full_paths)
		d_filename = filename;
	else
		d_filename = basename(filename);
297 298 299

	len = sym->end - sym->start;

300
	if (print_line) {
301
		get_source_line(he, len, filename);
302 303 304 305
		print_summary(filename);
	}

	printf("\n\n------------------------------------------------\n");
306
	printf(" Percent |	Source code & Disassembly of %s\n", d_filename);
307 308
	printf("------------------------------------------------\n");

309 310 311 312 313 314 315 316 317
	if (verbose)
		hist_entry__print_hits(he);

	list_for_each_entry_safe(pos, n, &head, node) {
		objdump_line__print(pos, &head, he, len);
		list_del(&pos->node);
		objdump_line__free(pos);
	}

318
	if (print_line)
319
		free_source_line(he, len);
320 321
}

322
static void hists__find_annotations(struct hists *self)
323 324 325
{
	struct rb_node *nd;

326
	for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
327
		struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
328
		struct sym_priv *priv;
329

330
		if (he->ms.sym == NULL)
331
			continue;
332

333
		priv = symbol__priv(he->ms.sym);
334 335 336 337 338 339
		if (priv->hist == NULL)
			continue;

		annotate_sym(he);
		/*
		 * Since we have a hist_entry per IP for the same symbol, free
340
		 * he->ms.sym->hist to signal we already processed this symbol.
341 342 343
		 */
		free(priv->hist);
		priv->hist = NULL;
344 345 346
	}
}

347
static struct perf_event_ops event_ops = {
348 349 350 351
	.sample	= process_sample_event,
	.mmap	= event__process_mmap,
	.comm	= event__process_comm,
	.fork	= event__process_task,
L
Li Zefan 已提交
352 353
};

354 355
static int __cmd_annotate(void)
{
L
Li Zefan 已提交
356
	int ret;
357
	struct perf_session *session;
358

T
Tom Zanussi 已提交
359
	session = perf_session__new(input_name, O_RDONLY, force, false);
360 361 362
	if (session == NULL)
		return -ENOMEM;

363
	ret = perf_session__process_events(session, &event_ops);
L
Li Zefan 已提交
364
	if (ret)
365
		goto out_delete;
366

367 368
	if (dump_trace) {
		event__print_totals();
369
		goto out_delete;
370
	}
371

372
	if (verbose > 3)
373
		perf_session__fprintf(session, stdout);
374

375
	if (verbose > 2)
376
		perf_session__fprintf_dsos(session, stdout);
377

378 379 380
	hists__collapse_resort(&session->hists);
	hists__output_resort(&session->hists);
	hists__find_annotations(&session->hists);
381 382
out_delete:
	perf_session__delete(session);
383

L
Li Zefan 已提交
384
	return ret;
385 386 387 388 389 390 391 392 393 394
}

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"),
395 396
	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
		   "only consider symbols in these dsos"),
397
	OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
398
		    "symbol to annotate"),
399
	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
400
	OPT_INCR('v', "verbose", &verbose,
401 402 403
		    "be more verbose (show symbol address, etc)"),
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
404 405 406
	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
		   "file", "vmlinux pathname"),
	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
407
		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
408 409
	OPT_BOOLEAN('l', "print-line", &print_line,
		    "print matching source lines (may be slow)"),
410 411
	OPT_BOOLEAN('P', "full-paths", &full_paths,
		    "Don't shorten the displayed pathnames"),
412 413 414
	OPT_END()
};

415
int cmd_annotate(int argc, const char **argv, const char *prefix __used)
416
{
417 418
	argc = parse_options(argc, argv, options, annotate_usage, 0);

419 420 421 422
	symbol_conf.priv_size = sizeof(struct sym_priv);
	symbol_conf.try_vmlinux_path = true;

	if (symbol__init() < 0)
423
		return -1;
424

425
	setup_sorting(annotate_usage, options);
426

427 428 429 430 431 432 433 434 435 436 437
	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];
	}

438 439
	setup_pager();

440
	if (field_sep && *field_sep == '.') {
441 442
		pr_err("'.' is the only non valid --field-separator argument\n");
		return -1;
443 444
	}

445 446
	return __cmd_annotate();
}