Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs...
[linux-2.6-block.git] / tools / perf / builtin-annotate.c
CommitLineData
8035e428
IM
1/*
2 * builtin-annotate.c
3 *
4 * Builtin annotate command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
8#include "builtin.h"
9
10#include "util/util.h"
11
12#include "util/color.h"
5da50258 13#include <linux/list.h>
8035e428 14#include "util/cache.h"
43cbcd8a 15#include <linux/rbtree.h>
8035e428
IM
16#include "util/symbol.h"
17#include "util/string.h"
18
19#include "perf.h"
8f28827a 20#include "util/debug.h"
8035e428 21
62daacb5 22#include "util/event.h"
8035e428
IM
23#include "util/parse-options.h"
24#include "util/parse-events.h"
6baa0a5a 25#include "util/thread.h"
dd68ada2 26#include "util/sort.h"
3d1d07ec 27#include "util/hist.h"
94c744b6 28#include "util/session.h"
8035e428 29
8035e428 30static char const *input_name = "perf.data";
8035e428 31
fa6963b2 32static int force;
8035e428 33
42976487
MG
34static int full_paths;
35
301406b9
FW
36static int print_line;
37
e4204992
ACM
38struct sym_hist {
39 u64 sum;
40 u64 ip[0];
41};
42
301406b9 43struct sym_ext {
971738f3 44 struct rb_node node;
301406b9
FW
45 double percent;
46 char *path;
47};
48
e4204992
ACM
49struct sym_priv {
50 struct sym_hist *hist;
51 struct sym_ext *ext;
52};
53
54static const char *sym_hist_filter;
55
00a192b3 56static int symbol_filter(struct map *map __used, struct symbol *sym)
e4204992 57{
8f0b0373
ACM
58 if (sym_hist_filter == NULL ||
59 strcmp(sym->name, sym_hist_filter) == 0) {
00a192b3 60 struct sym_priv *priv = symbol__priv(sym);
e4204992
ACM
61 const int size = (sizeof(*priv->hist) +
62 (sym->end - sym->start) * sizeof(u64));
63
64 priv->hist = malloc(size);
65 if (priv->hist)
66 memset(priv->hist, 0, size);
67 return 0;
68 }
69 /*
70 * FIXME: We should really filter it out, as we don't want to go thru symbols
71 * we're not interested, and if a DSO ends up with no symbols, delete it too,
72 * but right now the kernel loading routines in symbol.c bail out if no symbols
73 * are found, fix it later.
74 */
75 return 0;
76}
8035e428 77
0b73da3f
IM
78/*
79 * collect histogram counts
80 */
9cffa8d5 81static void hist_hit(struct hist_entry *he, u64 ip)
8035e428 82{
0b73da3f
IM
83 unsigned int sym_size, offset;
84 struct symbol *sym = he->sym;
e4204992
ACM
85 struct sym_priv *priv;
86 struct sym_hist *h;
8035e428 87
0b73da3f 88 he->count++;
8035e428 89
e4204992
ACM
90 if (!sym || !he->map)
91 return;
92
00a192b3 93 priv = symbol__priv(sym);
e4204992 94 if (!priv->hist)
0b73da3f 95 return;
8035e428 96
0b73da3f
IM
97 sym_size = sym->end - sym->start;
98 offset = ip - sym->start;
8035e428 99
ed52ce2e
ACM
100 if (verbose)
101 fprintf(stderr, "%s: ip=%Lx\n", __func__,
102 he->map->unmap_ip(he->map, ip));
103
0b73da3f
IM
104 if (offset >= sym_size)
105 return;
8035e428 106
e4204992
ACM
107 h = priv->hist;
108 h->sum++;
109 h->ip[offset]++;
8035e428 110
0b73da3f
IM
111 if (verbose >= 3)
112 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
7d37a0cb 113 (void *)(unsigned long)he->sym->start,
0b73da3f 114 he->sym->name,
7d37a0cb 115 (void *)(unsigned long)ip, ip - he->sym->start,
e4204992 116 h->ip[offset]);
8035e428
IM
117}
118
4e4f06e4
ACM
119static int perf_session__add_hist_entry(struct perf_session *self,
120 struct addr_location *al, u64 count)
8035e428 121{
9735abf1 122 bool hit;
4e4f06e4
ACM
123 struct hist_entry *he = __perf_session__add_hist_entry(self, al, NULL,
124 count, &hit);
9735abf1 125 if (he == NULL)
8035e428 126 return -ENOMEM;
1ed091c4 127 hist_hit(he, al->addr);
8035e428
IM
128 return 0;
129}
130
b3165f41 131static int process_sample_event(event_t *event, struct perf_session *session)
8035e428 132{
1ed091c4 133 struct addr_location al;
6baa0a5a 134
62daacb5 135 dump_printf("(IP, %d): %d: %p\n", event->header.misc,
1ed091c4 136 event->ip.pid, (void *)(long)event->ip.ip);
8035e428 137
b3165f41 138 if (event__preprocess_sample(event, session, &al, symbol_filter) < 0) {
8035e428
IM
139 fprintf(stderr, "problem processing %d event, skipping it.\n",
140 event->header.type);
141 return -1;
142 }
143
c410a338 144 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) {
ec218fc4
ACM
145 fprintf(stderr, "problem incrementing symbol count, "
146 "skipping event\n");
147 return -1;
8035e428 148 }
8035e428
IM
149
150 return 0;
151}
152
ed52ce2e 153static int parse_line(FILE *file, struct hist_entry *he, u64 len)
0b73da3f 154{
ed52ce2e 155 struct symbol *sym = he->sym;
0b73da3f 156 char *line = NULL, *tmp, *tmp2;
301406b9
FW
157 static const char *prev_line;
158 static const char *prev_color;
0b73da3f
IM
159 unsigned int offset;
160 size_t line_len;
ed52ce2e 161 u64 start;
f37a291c 162 s64 line_ip;
0b73da3f
IM
163 int ret;
164 char *c;
165
166 if (getline(&line, &line_len, file) < 0)
167 return -1;
168 if (!line)
169 return -1;
170
171 c = strchr(line, '\n');
172 if (c)
173 *c = 0;
174
175 line_ip = -1;
176 offset = 0;
177 ret = -2;
178
179 /*
180 * Strip leading spaces:
181 */
182 tmp = line;
183 while (*tmp) {
184 if (*tmp != ' ')
185 break;
186 tmp++;
187 }
188
189 if (*tmp) {
190 /*
191 * Parse hexa addresses followed by ':'
192 */
193 line_ip = strtoull(tmp, &tmp2, 16);
194 if (*tmp2 != ':')
195 line_ip = -1;
196 }
197
ed52ce2e
ACM
198 start = he->map->unmap_ip(he->map, sym->start);
199
0b73da3f 200 if (line_ip != -1) {
301406b9 201 const char *path = NULL;
0b73da3f
IM
202 unsigned int hits = 0;
203 double percent = 0.0;
83a0944f 204 const char *color;
00a192b3 205 struct sym_priv *priv = symbol__priv(sym);
e4204992
ACM
206 struct sym_ext *sym_ext = priv->ext;
207 struct sym_hist *h = priv->hist;
0b73da3f 208
ed52ce2e 209 offset = line_ip - start;
0b73da3f 210 if (offset < len)
e4204992 211 hits = h->ip[offset];
0b73da3f 212
c17c2db1 213 if (offset < len && sym_ext) {
301406b9
FW
214 path = sym_ext[offset].path;
215 percent = sym_ext[offset].percent;
e4204992
ACM
216 } else if (h->sum)
217 percent = 100.0 * hits / h->sum;
0b73da3f 218
1e11fd82 219 color = get_percent_color(percent);
0b73da3f 220
301406b9
FW
221 /*
222 * Also color the filename and line if needed, with
223 * the same color than the percentage. Don't print it
224 * twice for close colored ip with the same filename:line
225 */
226 if (path) {
227 if (!prev_line || strcmp(prev_line, path)
228 || color != prev_color) {
229 color_fprintf(stdout, color, " %s", path);
230 prev_line = path;
231 prev_color = color;
232 }
233 }
234
0b73da3f
IM
235 color_fprintf(stdout, color, " %7.2f", percent);
236 printf(" : ");
237 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
238 } else {
239 if (!*line)
240 printf(" :\n");
241 else
242 printf(" : %s\n", line);
243 }
244
245 return 0;
246}
247
971738f3
FW
248static struct rb_root root_sym_ext;
249
250static void insert_source_line(struct sym_ext *sym_ext)
251{
252 struct sym_ext *iter;
253 struct rb_node **p = &root_sym_ext.rb_node;
254 struct rb_node *parent = NULL;
255
256 while (*p != NULL) {
257 parent = *p;
258 iter = rb_entry(parent, struct sym_ext, node);
259
260 if (sym_ext->percent > iter->percent)
261 p = &(*p)->rb_left;
262 else
263 p = &(*p)->rb_right;
264 }
265
266 rb_link_node(&sym_ext->node, parent, p);
267 rb_insert_color(&sym_ext->node, &root_sym_ext);
268}
269
e4204992 270static void free_source_line(struct hist_entry *he, int len)
301406b9 271{
00a192b3 272 struct sym_priv *priv = symbol__priv(he->sym);
e4204992 273 struct sym_ext *sym_ext = priv->ext;
301406b9
FW
274 int i;
275
276 if (!sym_ext)
277 return;
278
279 for (i = 0; i < len; i++)
280 free(sym_ext[i].path);
281 free(sym_ext);
282
e4204992 283 priv->ext = NULL;
971738f3 284 root_sym_ext = RB_ROOT;
301406b9
FW
285}
286
287/* Get the filename:line for the colored entries */
c17c2db1 288static void
ed52ce2e 289get_source_line(struct hist_entry *he, int len, const char *filename)
301406b9 290{
ed52ce2e
ACM
291 struct symbol *sym = he->sym;
292 u64 start;
301406b9
FW
293 int i;
294 char cmd[PATH_MAX * 2];
295 struct sym_ext *sym_ext;
00a192b3 296 struct sym_priv *priv = symbol__priv(sym);
e4204992 297 struct sym_hist *h = priv->hist;
301406b9 298
e4204992 299 if (!h->sum)
301406b9
FW
300 return;
301
e4204992
ACM
302 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
303 if (!priv->ext)
301406b9
FW
304 return;
305
ed52ce2e 306 start = he->map->unmap_ip(he->map, sym->start);
301406b9
FW
307
308 for (i = 0; i < len; i++) {
309 char *path = NULL;
310 size_t line_len;
9cffa8d5 311 u64 offset;
301406b9
FW
312 FILE *fp;
313
e4204992 314 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
301406b9
FW
315 if (sym_ext[i].percent <= 0.5)
316 continue;
317
ed52ce2e 318 offset = start + i;
c17c2db1 319 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
301406b9
FW
320 fp = popen(cmd, "r");
321 if (!fp)
322 continue;
323
324 if (getline(&path, &line_len, fp) < 0 || !line_len)
325 goto next;
326
c17c2db1 327 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
301406b9
FW
328 if (!sym_ext[i].path)
329 goto next;
330
331 strcpy(sym_ext[i].path, path);
971738f3 332 insert_source_line(&sym_ext[i]);
301406b9
FW
333
334 next:
335 pclose(fp);
336 }
337}
338
83a0944f 339static void print_summary(const char *filename)
971738f3
FW
340{
341 struct sym_ext *sym_ext;
342 struct rb_node *node;
343
344 printf("\nSorted summary for file %s\n", filename);
345 printf("----------------------------------------------\n\n");
346
347 if (RB_EMPTY_ROOT(&root_sym_ext)) {
348 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
349 return;
350 }
351
352 node = rb_first(&root_sym_ext);
353 while (node) {
354 double percent;
83a0944f 355 const char *color;
971738f3
FW
356 char *path;
357
358 sym_ext = rb_entry(node, struct sym_ext, node);
359 percent = sym_ext->percent;
1e11fd82 360 color = get_percent_color(percent);
971738f3
FW
361 path = sym_ext->path;
362
363 color_fprintf(stdout, color, " %7.2f %s", percent, path);
364 node = rb_next(node);
365 }
366}
367
ed52ce2e 368static void annotate_sym(struct hist_entry *he)
0b73da3f 369{
ed52ce2e
ACM
370 struct map *map = he->map;
371 struct dso *dso = map->dso;
372 struct symbol *sym = he->sym;
439d473b
ACM
373 const char *filename = dso->long_name, *d_filename;
374 u64 len;
0b73da3f
IM
375 char command[PATH_MAX*2];
376 FILE *file;
377
378 if (!filename)
379 return;
439d473b 380
ed52ce2e
ACM
381 if (verbose)
382 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
383 __func__, filename, sym->name,
384 map->unmap_ip(map, sym->start),
385 map->unmap_ip(map, sym->end));
386
42976487
MG
387 if (full_paths)
388 d_filename = filename;
389 else
390 d_filename = basename(filename);
0b73da3f 391
0b73da3f
IM
392 len = sym->end - sym->start;
393
971738f3 394 if (print_line) {
ed52ce2e 395 get_source_line(he, len, filename);
971738f3
FW
396 print_summary(filename);
397 }
398
399 printf("\n\n------------------------------------------------\n");
42976487 400 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
971738f3
FW
401 printf("------------------------------------------------\n");
402
403 if (verbose >= 2)
439d473b
ACM
404 printf("annotating [%p] %30s : [%p] %30s\n",
405 dso, dso->long_name, sym, sym->name);
301406b9 406
42976487 407 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
ed52ce2e
ACM
408 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
409 filename, filename);
0b73da3f
IM
410
411 if (verbose >= 3)
412 printf("doing: %s\n", command);
413
414 file = popen(command, "r");
415 if (!file)
416 return;
417
418 while (!feof(file)) {
ed52ce2e 419 if (parse_line(file, he, len) < 0)
0b73da3f
IM
420 break;
421 }
422
423 pclose(file);
971738f3 424 if (print_line)
e4204992 425 free_source_line(he, len);
0b73da3f
IM
426}
427
4e4f06e4 428static void perf_session__find_annotations(struct perf_session *self)
0b73da3f
IM
429{
430 struct rb_node *nd;
0b73da3f 431
4e4f06e4 432 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
ed52ce2e 433 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
e4204992 434 struct sym_priv *priv;
0b73da3f 435
e4204992
ACM
436 if (he->sym == NULL)
437 continue;
0b73da3f 438
00a192b3 439 priv = symbol__priv(he->sym);
e4204992
ACM
440 if (priv->hist == NULL)
441 continue;
442
443 annotate_sym(he);
e4204992
ACM
444 /*
445 * Since we have a hist_entry per IP for the same symbol, free
446 * he->sym->hist to signal we already processed this symbol.
447 */
448 free(priv->hist);
449 priv->hist = NULL;
0b73da3f 450 }
0b73da3f
IM
451}
452
301a0b02 453static struct perf_event_ops event_ops = {
bab81b62
LZ
454 .process_sample_event = process_sample_event,
455 .process_mmap_event = event__process_mmap,
456 .process_comm_event = event__process_comm,
457 .process_fork_event = event__process_task,
458};
459
8035e428
IM
460static int __cmd_annotate(void)
461{
bab81b62 462 int ret;
75be6cf4 463 struct perf_session *session;
8035e428 464
75be6cf4 465 session = perf_session__new(input_name, O_RDONLY, force);
94c744b6
ACM
466 if (session == NULL)
467 return -ENOMEM;
468
ec913369 469 ret = perf_session__process_events(session, &event_ops);
bab81b62 470 if (ret)
94c744b6 471 goto out_delete;
8035e428 472
62daacb5
ACM
473 if (dump_trace) {
474 event__print_totals();
94c744b6 475 goto out_delete;
62daacb5 476 }
8035e428 477
da21d1b5 478 if (verbose > 3)
b3165f41 479 perf_session__fprintf(session, stdout);
8035e428 480
da21d1b5 481 if (verbose > 2)
8035e428
IM
482 dsos__fprintf(stdout);
483
4e4f06e4 484 perf_session__collapse_resort(session);
f823e441 485 perf_session__output_resort(session, session->event_total[0]);
4e4f06e4 486 perf_session__find_annotations(session);
94c744b6
ACM
487out_delete:
488 perf_session__delete(session);
8035e428 489
bab81b62 490 return ret;
8035e428
IM
491}
492
493static const char * const annotate_usage[] = {
494 "perf annotate [<options>] <command>",
495 NULL
496};
497
498static const struct option options[] = {
499 OPT_STRING('i', "input", &input_name, "file",
500 "input file name"),
23b87116 501 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
0b73da3f 502 "symbol to annotate"),
fa6963b2 503 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
8035e428
IM
504 OPT_BOOLEAN('v', "verbose", &verbose,
505 "be more verbose (show symbol address, etc)"),
506 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
507 "dump raw trace in ASCII"),
b32d133a
ACM
508 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
509 "file", "vmlinux pathname"),
510 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 511 "load module symbols - WARNING: use only with -k and LIVE kernel"),
301406b9
FW
512 OPT_BOOLEAN('l', "print-line", &print_line,
513 "print matching source lines (may be slow)"),
42976487
MG
514 OPT_BOOLEAN('P', "full-paths", &full_paths,
515 "Don't shorten the displayed pathnames"),
8035e428
IM
516 OPT_END()
517};
518
f37a291c 519int cmd_annotate(int argc, const char **argv, const char *prefix __used)
8035e428 520{
655000e7
ACM
521 argc = parse_options(argc, argv, options, annotate_usage, 0);
522
75be6cf4
ACM
523 symbol_conf.priv_size = sizeof(struct sym_priv);
524 symbol_conf.try_vmlinux_path = true;
525
526 if (symbol__init() < 0)
b32d133a 527 return -1;
8035e428 528
c8829c7a 529 setup_sorting(annotate_usage, options);
8035e428 530
0b73da3f
IM
531 if (argc) {
532 /*
533 * Special case: if there's an argument left then assume tha
534 * it's a symbol filter:
535 */
536 if (argc > 1)
537 usage_with_options(annotate_usage, options);
538
539 sym_hist_filter = argv[0];
540 }
541
8035e428
IM
542 setup_pager();
543
dd68ada2
JK
544 if (field_sep && *field_sep == '.') {
545 fputs("'.' is the only non valid --field-separator argument\n",
546 stderr);
547 exit(129);
548 }
549
8035e428
IM
550 return __cmd_annotate();
551}