]> bbs.cooldavid.org Git - net-next-2.6.git/blame - tools/perf/builtin-annotate.c
perf machine: Adopt some map_groups functions
[net-next-2.6.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 16#include "util/symbol.h"
8035e428
IM
17
18#include "perf.h"
8f28827a 19#include "util/debug.h"
8035e428 20
62daacb5 21#include "util/event.h"
8035e428
IM
22#include "util/parse-options.h"
23#include "util/parse-events.h"
6baa0a5a 24#include "util/thread.h"
dd68ada2 25#include "util/sort.h"
3d1d07ec 26#include "util/hist.h"
94c744b6 27#include "util/session.h"
8035e428 28
8035e428 29static char const *input_name = "perf.data";
8035e428 30
c0555642 31static bool force;
8035e428 32
c0555642 33static bool full_paths;
42976487 34
c0555642 35static bool print_line;
301406b9 36
e4204992
ACM
37struct sym_hist {
38 u64 sum;
39 u64 ip[0];
40};
41
301406b9 42struct sym_ext {
971738f3 43 struct rb_node node;
301406b9
FW
44 double percent;
45 char *path;
46};
47
e4204992
ACM
48struct sym_priv {
49 struct sym_hist *hist;
50 struct sym_ext *ext;
51};
52
53static const char *sym_hist_filter;
54
628ada0c 55static int sym__alloc_hist(struct symbol *self)
e4204992 56{
628ada0c
ACM
57 struct sym_priv *priv = symbol__priv(self);
58 const int size = (sizeof(*priv->hist) +
59 (self->end - self->start) * sizeof(u64));
60
61 priv->hist = zalloc(size);
62 return priv->hist == NULL ? -1 : 0;
e4204992 63}
8035e428 64
0b73da3f
IM
65/*
66 * collect histogram counts
67 */
628ada0c 68static int annotate__hist_hit(struct hist_entry *he, u64 ip)
8035e428 69{
0b73da3f 70 unsigned int sym_size, offset;
59fd5306 71 struct symbol *sym = he->ms.sym;
e4204992
ACM
72 struct sym_priv *priv;
73 struct sym_hist *h;
8035e428 74
0b73da3f 75 he->count++;
8035e428 76
59fd5306 77 if (!sym || !he->ms.map)
628ada0c 78 return 0;
e4204992 79
00a192b3 80 priv = symbol__priv(sym);
628ada0c
ACM
81 if (priv->hist == NULL && sym__alloc_hist(sym) < 0)
82 return -ENOMEM;
8035e428 83
0b73da3f
IM
84 sym_size = sym->end - sym->start;
85 offset = ip - sym->start;
8035e428 86
59fd5306 87 pr_debug3("%s: ip=%#Lx\n", __func__, he->ms.map->unmap_ip(he->ms.map, ip));
ed52ce2e 88
0b73da3f 89 if (offset >= sym_size)
628ada0c 90 return 0;
8035e428 91
e4204992
ACM
92 h = priv->hist;
93 h->sum++;
94 h->ip[offset]++;
8035e428 95
59fd5306
ACM
96 pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->ms.sym->start,
97 he->ms.sym->name, ip, ip - he->ms.sym->start, h->ip[offset]);
628ada0c 98 return 0;
8035e428
IM
99}
100
4e4f06e4
ACM
101static int perf_session__add_hist_entry(struct perf_session *self,
102 struct addr_location *al, u64 count)
8035e428 103{
9735abf1 104 bool hit;
628ada0c
ACM
105 struct hist_entry *he;
106
107 if (sym_hist_filter != NULL &&
108 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
109 /* We're only interested in a symbol named sym_hist_filter */
110 if (al->sym != NULL) {
111 rb_erase(&al->sym->rb_node,
112 &al->map->dso->symbols[al->map->type]);
113 symbol__delete(al->sym);
114 }
115 return 0;
116 }
117
d403d0ac 118 he = __perf_session__add_hist_entry(&self->hists, al, NULL, count, &hit);
9735abf1 119 if (he == NULL)
8035e428 120 return -ENOMEM;
628ada0c
ACM
121
122 return annotate__hist_hit(he, al->addr);
8035e428
IM
123}
124
b3165f41 125static int process_sample_event(event_t *event, struct perf_session *session)
8035e428 126{
1ed091c4 127 struct addr_location al;
6baa0a5a 128
0d755034
ACM
129 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
130 event->ip.pid, event->ip.ip);
8035e428 131
628ada0c 132 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
29a9f66d
ACM
133 pr_warning("problem processing %d event, skipping it.\n",
134 event->header.type);
8035e428
IM
135 return -1;
136 }
137
c410a338 138 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) {
29a9f66d
ACM
139 pr_warning("problem incrementing symbol count, "
140 "skipping event\n");
ec218fc4 141 return -1;
8035e428 142 }
8035e428
IM
143
144 return 0;
145}
146
48fb4fdd
ACM
147struct objdump_line {
148 struct list_head node;
149 s64 offset;
150 char *line;
151};
152
153static struct objdump_line *objdump_line__new(s64 offset, char *line)
154{
155 struct objdump_line *self = malloc(sizeof(*self));
156
157 if (self != NULL) {
158 self->offset = offset;
159 self->line = line;
160 }
161
162 return self;
163}
164
165static void objdump_line__free(struct objdump_line *self)
166{
167 free(self->line);
168 free(self);
169}
170
171static void objdump__add_line(struct list_head *head, struct objdump_line *line)
172{
173 list_add_tail(&line->node, head);
174}
175
176static struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
177 struct objdump_line *pos)
178{
179 list_for_each_entry_continue(pos, head, node)
180 if (pos->offset >= 0)
181 return pos;
182
183 return NULL;
184}
185
186static int parse_line(FILE *file, struct hist_entry *he,
187 struct list_head *head)
0b73da3f 188{
59fd5306 189 struct symbol *sym = he->ms.sym;
48fb4fdd 190 struct objdump_line *objdump_line;
0b73da3f 191 char *line = NULL, *tmp, *tmp2;
0b73da3f 192 size_t line_len;
48fb4fdd 193 s64 line_ip, offset = -1;
0b73da3f
IM
194 char *c;
195
196 if (getline(&line, &line_len, file) < 0)
197 return -1;
48fb4fdd 198
0b73da3f
IM
199 if (!line)
200 return -1;
201
202 c = strchr(line, '\n');
203 if (c)
204 *c = 0;
205
206 line_ip = -1;
0b73da3f
IM
207
208 /*
209 * Strip leading spaces:
210 */
211 tmp = line;
212 while (*tmp) {
213 if (*tmp != ' ')
214 break;
215 tmp++;
216 }
217
218 if (*tmp) {
219 /*
220 * Parse hexa addresses followed by ':'
221 */
222 line_ip = strtoull(tmp, &tmp2, 16);
223 if (*tmp2 != ':')
224 line_ip = -1;
225 }
226
227 if (line_ip != -1) {
59fd5306 228 u64 start = map__rip_2objdump(he->ms.map, sym->start);
48fb4fdd
ACM
229 offset = line_ip - start;
230 }
231
232 objdump_line = objdump_line__new(offset, line);
233 if (objdump_line == NULL) {
234 free(line);
235 return -1;
236 }
237 objdump__add_line(head, objdump_line);
238
239 return 0;
240}
241
242static int objdump_line__print(struct objdump_line *self,
243 struct list_head *head,
244 struct hist_entry *he, u64 len)
245{
59fd5306 246 struct symbol *sym = he->ms.sym;
48fb4fdd
ACM
247 static const char *prev_line;
248 static const char *prev_color;
249
250 if (self->offset != -1) {
301406b9 251 const char *path = NULL;
0b73da3f
IM
252 unsigned int hits = 0;
253 double percent = 0.0;
83a0944f 254 const char *color;
00a192b3 255 struct sym_priv *priv = symbol__priv(sym);
e4204992
ACM
256 struct sym_ext *sym_ext = priv->ext;
257 struct sym_hist *h = priv->hist;
48fb4fdd
ACM
258 s64 offset = self->offset;
259 struct objdump_line *next = objdump__get_next_ip_line(head, self);
260
261 while (offset < (s64)len &&
262 (next == NULL || offset < next->offset)) {
263 if (sym_ext) {
264 if (path == NULL)
265 path = sym_ext[offset].path;
266 percent += sym_ext[offset].percent;
267 } else
268 hits += h->ip[offset];
269
270 ++offset;
271 }
0b73da3f 272
48fb4fdd 273 if (sym_ext == NULL && h->sum)
e4204992 274 percent = 100.0 * hits / h->sum;
0b73da3f 275
1e11fd82 276 color = get_percent_color(percent);
0b73da3f 277
301406b9
FW
278 /*
279 * Also color the filename and line if needed, with
280 * the same color than the percentage. Don't print it
281 * twice for close colored ip with the same filename:line
282 */
283 if (path) {
284 if (!prev_line || strcmp(prev_line, path)
285 || color != prev_color) {
286 color_fprintf(stdout, color, " %s", path);
287 prev_line = path;
288 prev_color = color;
289 }
290 }
291
0b73da3f
IM
292 color_fprintf(stdout, color, " %7.2f", percent);
293 printf(" : ");
48fb4fdd 294 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
0b73da3f 295 } else {
48fb4fdd 296 if (!*self->line)
0b73da3f
IM
297 printf(" :\n");
298 else
48fb4fdd 299 printf(" : %s\n", self->line);
0b73da3f
IM
300 }
301
302 return 0;
303}
304
971738f3
FW
305static struct rb_root root_sym_ext;
306
307static void insert_source_line(struct sym_ext *sym_ext)
308{
309 struct sym_ext *iter;
310 struct rb_node **p = &root_sym_ext.rb_node;
311 struct rb_node *parent = NULL;
312
313 while (*p != NULL) {
314 parent = *p;
315 iter = rb_entry(parent, struct sym_ext, node);
316
317 if (sym_ext->percent > iter->percent)
318 p = &(*p)->rb_left;
319 else
320 p = &(*p)->rb_right;
321 }
322
323 rb_link_node(&sym_ext->node, parent, p);
324 rb_insert_color(&sym_ext->node, &root_sym_ext);
325}
326
e4204992 327static void free_source_line(struct hist_entry *he, int len)
301406b9 328{
59fd5306 329 struct sym_priv *priv = symbol__priv(he->ms.sym);
e4204992 330 struct sym_ext *sym_ext = priv->ext;
301406b9
FW
331 int i;
332
333 if (!sym_ext)
334 return;
335
336 for (i = 0; i < len; i++)
337 free(sym_ext[i].path);
338 free(sym_ext);
339
e4204992 340 priv->ext = NULL;
971738f3 341 root_sym_ext = RB_ROOT;
301406b9
FW
342}
343
344/* Get the filename:line for the colored entries */
c17c2db1 345static void
ed52ce2e 346get_source_line(struct hist_entry *he, int len, const char *filename)
301406b9 347{
59fd5306 348 struct symbol *sym = he->ms.sym;
ed52ce2e 349 u64 start;
301406b9
FW
350 int i;
351 char cmd[PATH_MAX * 2];
352 struct sym_ext *sym_ext;
00a192b3 353 struct sym_priv *priv = symbol__priv(sym);
e4204992 354 struct sym_hist *h = priv->hist;
301406b9 355
e4204992 356 if (!h->sum)
301406b9
FW
357 return;
358
e4204992
ACM
359 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
360 if (!priv->ext)
301406b9
FW
361 return;
362
59fd5306 363 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
301406b9
FW
364
365 for (i = 0; i < len; i++) {
366 char *path = NULL;
367 size_t line_len;
9cffa8d5 368 u64 offset;
301406b9
FW
369 FILE *fp;
370
e4204992 371 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
301406b9
FW
372 if (sym_ext[i].percent <= 0.5)
373 continue;
374
ed52ce2e 375 offset = start + i;
c17c2db1 376 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
301406b9
FW
377 fp = popen(cmd, "r");
378 if (!fp)
379 continue;
380
381 if (getline(&path, &line_len, fp) < 0 || !line_len)
382 goto next;
383
c17c2db1 384 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
301406b9
FW
385 if (!sym_ext[i].path)
386 goto next;
387
388 strcpy(sym_ext[i].path, path);
971738f3 389 insert_source_line(&sym_ext[i]);
301406b9
FW
390
391 next:
392 pclose(fp);
393 }
394}
395
83a0944f 396static void print_summary(const char *filename)
971738f3
FW
397{
398 struct sym_ext *sym_ext;
399 struct rb_node *node;
400
401 printf("\nSorted summary for file %s\n", filename);
402 printf("----------------------------------------------\n\n");
403
404 if (RB_EMPTY_ROOT(&root_sym_ext)) {
405 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
406 return;
407 }
408
409 node = rb_first(&root_sym_ext);
410 while (node) {
411 double percent;
83a0944f 412 const char *color;
971738f3
FW
413 char *path;
414
415 sym_ext = rb_entry(node, struct sym_ext, node);
416 percent = sym_ext->percent;
1e11fd82 417 color = get_percent_color(percent);
971738f3
FW
418 path = sym_ext->path;
419
420 color_fprintf(stdout, color, " %7.2f %s", percent, path);
421 node = rb_next(node);
422 }
423}
424
48fb4fdd
ACM
425static void hist_entry__print_hits(struct hist_entry *self)
426{
59fd5306 427 struct symbol *sym = self->ms.sym;
48fb4fdd
ACM
428 struct sym_priv *priv = symbol__priv(sym);
429 struct sym_hist *h = priv->hist;
430 u64 len = sym->end - sym->start, offset;
431
432 for (offset = 0; offset < len; ++offset)
433 if (h->ip[offset] != 0)
434 printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
435 sym->start + offset, h->ip[offset]);
436 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
437}
438
ed52ce2e 439static void annotate_sym(struct hist_entry *he)
0b73da3f 440{
59fd5306 441 struct map *map = he->ms.map;
ed52ce2e 442 struct dso *dso = map->dso;
59fd5306 443 struct symbol *sym = he->ms.sym;
439d473b
ACM
444 const char *filename = dso->long_name, *d_filename;
445 u64 len;
0b73da3f
IM
446 char command[PATH_MAX*2];
447 FILE *file;
48fb4fdd
ACM
448 LIST_HEAD(head);
449 struct objdump_line *pos, *n;
0b73da3f
IM
450
451 if (!filename)
452 return;
439d473b 453
d06d92b7
ACM
454 if (dso->origin == DSO__ORIG_KERNEL) {
455 if (dso->annotate_warned)
456 return;
457 dso->annotate_warned = 1;
458 pr_err("Can't annotate %s: No vmlinux file was found in the "
459 "path:\n", sym->name);
460 vmlinux_path__fprintf(stderr);
461 return;
462 }
463
29a9f66d
ACM
464 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
465 filename, sym->name, map->unmap_ip(map, sym->start),
466 map->unmap_ip(map, sym->end));
ed52ce2e 467
42976487
MG
468 if (full_paths)
469 d_filename = filename;
470 else
471 d_filename = basename(filename);
0b73da3f 472
0b73da3f
IM
473 len = sym->end - sym->start;
474
971738f3 475 if (print_line) {
ed52ce2e 476 get_source_line(he, len, filename);
971738f3
FW
477 print_summary(filename);
478 }
479
480 printf("\n\n------------------------------------------------\n");
42976487 481 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
971738f3
FW
482 printf("------------------------------------------------\n");
483
484 if (verbose >= 2)
439d473b
ACM
485 printf("annotating [%p] %30s : [%p] %30s\n",
486 dso, dso->long_name, sym, sym->name);
301406b9 487
42976487 488 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
7a2b6209
KS
489 map__rip_2objdump(map, sym->start),
490 map__rip_2objdump(map, sym->end),
ed52ce2e 491 filename, filename);
0b73da3f
IM
492
493 if (verbose >= 3)
494 printf("doing: %s\n", command);
495
496 file = popen(command, "r");
497 if (!file)
498 return;
499
500 while (!feof(file)) {
48fb4fdd 501 if (parse_line(file, he, &head) < 0)
0b73da3f
IM
502 break;
503 }
504
505 pclose(file);
48fb4fdd
ACM
506
507 if (verbose)
508 hist_entry__print_hits(he);
509
510 list_for_each_entry_safe(pos, n, &head, node) {
511 objdump_line__print(pos, &head, he, len);
512 list_del(&pos->node);
513 objdump_line__free(pos);
514 }
515
971738f3 516 if (print_line)
e4204992 517 free_source_line(he, len);
0b73da3f
IM
518}
519
4e4f06e4 520static void perf_session__find_annotations(struct perf_session *self)
0b73da3f
IM
521{
522 struct rb_node *nd;
0b73da3f 523
4e4f06e4 524 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
ed52ce2e 525 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
e4204992 526 struct sym_priv *priv;
0b73da3f 527
59fd5306 528 if (he->ms.sym == NULL)
e4204992 529 continue;
0b73da3f 530
59fd5306 531 priv = symbol__priv(he->ms.sym);
e4204992
ACM
532 if (priv->hist == NULL)
533 continue;
534
535 annotate_sym(he);
e4204992
ACM
536 /*
537 * Since we have a hist_entry per IP for the same symbol, free
59fd5306 538 * he->ms.sym->hist to signal we already processed this symbol.
e4204992
ACM
539 */
540 free(priv->hist);
541 priv->hist = NULL;
0b73da3f 542 }
0b73da3f
IM
543}
544
301a0b02 545static struct perf_event_ops event_ops = {
55aa640f
ACM
546 .sample = process_sample_event,
547 .mmap = event__process_mmap,
548 .comm = event__process_comm,
549 .fork = event__process_task,
bab81b62
LZ
550};
551
8035e428
IM
552static int __cmd_annotate(void)
553{
bab81b62 554 int ret;
75be6cf4 555 struct perf_session *session;
8035e428 556
75be6cf4 557 session = perf_session__new(input_name, O_RDONLY, force);
94c744b6
ACM
558 if (session == NULL)
559 return -ENOMEM;
560
ec913369 561 ret = perf_session__process_events(session, &event_ops);
bab81b62 562 if (ret)
94c744b6 563 goto out_delete;
8035e428 564
62daacb5
ACM
565 if (dump_trace) {
566 event__print_totals();
94c744b6 567 goto out_delete;
62daacb5 568 }
8035e428 569
da21d1b5 570 if (verbose > 3)
b3165f41 571 perf_session__fprintf(session, stdout);
8035e428 572
da21d1b5 573 if (verbose > 2)
23346f21 574 dsos__fprintf(&session->machines, stdout);
8035e428 575
eefc465c
EM
576 perf_session__collapse_resort(&session->hists);
577 perf_session__output_resort(&session->hists, session->event_total[0]);
4e4f06e4 578 perf_session__find_annotations(session);
94c744b6
ACM
579out_delete:
580 perf_session__delete(session);
8035e428 581
bab81b62 582 return ret;
8035e428
IM
583}
584
585static const char * const annotate_usage[] = {
586 "perf annotate [<options>] <command>",
587 NULL
588};
589
590static const struct option options[] = {
591 OPT_STRING('i', "input", &input_name, "file",
592 "input file name"),
ac73c5a9
ACM
593 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
594 "only consider symbols in these dsos"),
23b87116 595 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
0b73da3f 596 "symbol to annotate"),
fa6963b2 597 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
c0555642 598 OPT_INCR('v', "verbose", &verbose,
8035e428
IM
599 "be more verbose (show symbol address, etc)"),
600 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
601 "dump raw trace in ASCII"),
b32d133a
ACM
602 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
603 "file", "vmlinux pathname"),
604 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 605 "load module symbols - WARNING: use only with -k and LIVE kernel"),
301406b9
FW
606 OPT_BOOLEAN('l', "print-line", &print_line,
607 "print matching source lines (may be slow)"),
42976487
MG
608 OPT_BOOLEAN('P', "full-paths", &full_paths,
609 "Don't shorten the displayed pathnames"),
8035e428
IM
610 OPT_END()
611};
612
f37a291c 613int cmd_annotate(int argc, const char **argv, const char *prefix __used)
8035e428 614{
655000e7
ACM
615 argc = parse_options(argc, argv, options, annotate_usage, 0);
616
75be6cf4
ACM
617 symbol_conf.priv_size = sizeof(struct sym_priv);
618 symbol_conf.try_vmlinux_path = true;
619
620 if (symbol__init() < 0)
b32d133a 621 return -1;
8035e428 622
c8829c7a 623 setup_sorting(annotate_usage, options);
8035e428 624
0b73da3f
IM
625 if (argc) {
626 /*
627 * Special case: if there's an argument left then assume tha
628 * it's a symbol filter:
629 */
630 if (argc > 1)
631 usage_with_options(annotate_usage, options);
632
633 sym_hist_filter = argv[0];
634 }
635
8035e428
IM
636 setup_pager();
637
dd68ada2 638 if (field_sep && *field_sep == '.') {
29a9f66d
ACM
639 pr_err("'.' is the only non valid --field-separator argument\n");
640 return -1;
dd68ada2
JK
641 }
642
8035e428
IM
643 return __cmd_annotate();
644}