perf tools: Consolidate symbol resolving across all tools
[linux-2.6.git] / tools / perf / builtin-annotate.c
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"
13 #include <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/string.h"
18
19 #include "perf.h"
20 #include "util/debug.h"
21
22 #include "util/event.h"
23 #include "util/parse-options.h"
24 #include "util/parse-events.h"
25 #include "util/thread.h"
26 #include "util/sort.h"
27 #include "util/hist.h"
28
29 static char             const *input_name = "perf.data";
30
31 static int              force;
32 static int              input;
33
34 static int              full_paths;
35
36 static int              print_line;
37
38 static unsigned long    page_size;
39 static unsigned long    mmap_window = 32;
40
41 struct sym_hist {
42         u64             sum;
43         u64             ip[0];
44 };
45
46 struct sym_ext {
47         struct rb_node  node;
48         double          percent;
49         char            *path;
50 };
51
52 struct sym_priv {
53         struct sym_hist *hist;
54         struct sym_ext  *ext;
55 };
56
57 static struct symbol_conf symbol_conf = {
58         .priv_size        = sizeof(struct sym_priv),
59         .try_vmlinux_path = true,
60 };
61
62 static const char *sym_hist_filter;
63
64 static int symbol_filter(struct map *map __used, struct symbol *sym)
65 {
66         if (sym_hist_filter == NULL ||
67             strcmp(sym->name, sym_hist_filter) == 0) {
68                 struct sym_priv *priv = symbol__priv(sym);
69                 const int size = (sizeof(*priv->hist) +
70                                   (sym->end - sym->start) * sizeof(u64));
71
72                 priv->hist = malloc(size);
73                 if (priv->hist)
74                         memset(priv->hist, 0, size);
75                 return 0;
76         }
77         /*
78          * FIXME: We should really filter it out, as we don't want to go thru symbols
79          * we're not interested, and if a DSO ends up with no symbols, delete it too,
80          * but right now the kernel loading routines in symbol.c bail out if no symbols
81          * are found, fix it later.
82          */
83         return 0;
84 }
85
86 /*
87  * collect histogram counts
88  */
89 static void hist_hit(struct hist_entry *he, u64 ip)
90 {
91         unsigned int sym_size, offset;
92         struct symbol *sym = he->sym;
93         struct sym_priv *priv;
94         struct sym_hist *h;
95
96         he->count++;
97
98         if (!sym || !he->map)
99                 return;
100
101         priv = symbol__priv(sym);
102         if (!priv->hist)
103                 return;
104
105         sym_size = sym->end - sym->start;
106         offset = ip - sym->start;
107
108         if (verbose)
109                 fprintf(stderr, "%s: ip=%Lx\n", __func__,
110                         he->map->unmap_ip(he->map, ip));
111
112         if (offset >= sym_size)
113                 return;
114
115         h = priv->hist;
116         h->sum++;
117         h->ip[offset]++;
118
119         if (verbose >= 3)
120                 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
121                         (void *)(unsigned long)he->sym->start,
122                         he->sym->name,
123                         (void *)(unsigned long)ip, ip - he->sym->start,
124                         h->ip[offset]);
125 }
126
127 static int hist_entry__add(struct addr_location *al, u64 count)
128 {
129         bool hit;
130         struct hist_entry *he = __hist_entry__add(al, NULL, count, &hit);
131         if (he == NULL)
132                 return -ENOMEM;
133         hist_hit(he, al->addr);
134         return 0;
135 }
136
137 static int process_sample_event(event_t *event)
138 {
139         struct addr_location al;
140
141         dump_printf("(IP, %d): %d: %p\n", event->header.misc,
142                     event->ip.pid, (void *)(long)event->ip.ip);
143
144         if (event__preprocess_sample(event, &al, symbol_filter) < 0) {
145                 fprintf(stderr, "problem processing %d event, skipping it.\n",
146                         event->header.type);
147                 return -1;
148         }
149
150         if (hist_entry__add(&al, 1)) {
151                 fprintf(stderr, "problem incrementing symbol count, "
152                                 "skipping event\n");
153                 return -1;
154         }
155
156         return 0;
157 }
158
159 static int event__process(event_t *self)
160 {
161         switch (self->header.type) {
162         case PERF_RECORD_SAMPLE:
163                 return process_sample_event(self);
164
165         case PERF_RECORD_MMAP:
166                 return event__process_mmap(self);
167
168         case PERF_RECORD_COMM:
169                 return event__process_comm(self);
170
171         case PERF_RECORD_FORK:
172                 return event__process_task(self);
173         /*
174          * We dont process them right now but they are fine:
175          */
176
177         case PERF_RECORD_THROTTLE:
178         case PERF_RECORD_UNTHROTTLE:
179                 return 0;
180
181         default:
182                 return -1;
183         }
184
185         return 0;
186 }
187
188 static int parse_line(FILE *file, struct hist_entry *he, u64 len)
189 {
190         struct symbol *sym = he->sym;
191         char *line = NULL, *tmp, *tmp2;
192         static const char *prev_line;
193         static const char *prev_color;
194         unsigned int offset;
195         size_t line_len;
196         u64 start;
197         s64 line_ip;
198         int ret;
199         char *c;
200
201         if (getline(&line, &line_len, file) < 0)
202                 return -1;
203         if (!line)
204                 return -1;
205
206         c = strchr(line, '\n');
207         if (c)
208                 *c = 0;
209
210         line_ip = -1;
211         offset = 0;
212         ret = -2;
213
214         /*
215          * Strip leading spaces:
216          */
217         tmp = line;
218         while (*tmp) {
219                 if (*tmp != ' ')
220                         break;
221                 tmp++;
222         }
223
224         if (*tmp) {
225                 /*
226                  * Parse hexa addresses followed by ':'
227                  */
228                 line_ip = strtoull(tmp, &tmp2, 16);
229                 if (*tmp2 != ':')
230                         line_ip = -1;
231         }
232
233         start = he->map->unmap_ip(he->map, sym->start);
234
235         if (line_ip != -1) {
236                 const char *path = NULL;
237                 unsigned int hits = 0;
238                 double percent = 0.0;
239                 const char *color;
240                 struct sym_priv *priv = symbol__priv(sym);
241                 struct sym_ext *sym_ext = priv->ext;
242                 struct sym_hist *h = priv->hist;
243
244                 offset = line_ip - start;
245                 if (offset < len)
246                         hits = h->ip[offset];
247
248                 if (offset < len && sym_ext) {
249                         path = sym_ext[offset].path;
250                         percent = sym_ext[offset].percent;
251                 } else if (h->sum)
252                         percent = 100.0 * hits / h->sum;
253
254                 color = get_percent_color(percent);
255
256                 /*
257                  * Also color the filename and line if needed, with
258                  * the same color than the percentage. Don't print it
259                  * twice for close colored ip with the same filename:line
260                  */
261                 if (path) {
262                         if (!prev_line || strcmp(prev_line, path)
263                                        || color != prev_color) {
264                                 color_fprintf(stdout, color, " %s", path);
265                                 prev_line = path;
266                                 prev_color = color;
267                         }
268                 }
269
270                 color_fprintf(stdout, color, " %7.2f", percent);
271                 printf(" :      ");
272                 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
273         } else {
274                 if (!*line)
275                         printf("         :\n");
276                 else
277                         printf("         :      %s\n", line);
278         }
279
280         return 0;
281 }
282
283 static struct rb_root root_sym_ext;
284
285 static void insert_source_line(struct sym_ext *sym_ext)
286 {
287         struct sym_ext *iter;
288         struct rb_node **p = &root_sym_ext.rb_node;
289         struct rb_node *parent = NULL;
290
291         while (*p != NULL) {
292                 parent = *p;
293                 iter = rb_entry(parent, struct sym_ext, node);
294
295                 if (sym_ext->percent > iter->percent)
296                         p = &(*p)->rb_left;
297                 else
298                         p = &(*p)->rb_right;
299         }
300
301         rb_link_node(&sym_ext->node, parent, p);
302         rb_insert_color(&sym_ext->node, &root_sym_ext);
303 }
304
305 static void free_source_line(struct hist_entry *he, int len)
306 {
307         struct sym_priv *priv = symbol__priv(he->sym);
308         struct sym_ext *sym_ext = priv->ext;
309         int i;
310
311         if (!sym_ext)
312                 return;
313
314         for (i = 0; i < len; i++)
315                 free(sym_ext[i].path);
316         free(sym_ext);
317
318         priv->ext = NULL;
319         root_sym_ext = RB_ROOT;
320 }
321
322 /* Get the filename:line for the colored entries */
323 static void
324 get_source_line(struct hist_entry *he, int len, const char *filename)
325 {
326         struct symbol *sym = he->sym;
327         u64 start;
328         int i;
329         char cmd[PATH_MAX * 2];
330         struct sym_ext *sym_ext;
331         struct sym_priv *priv = symbol__priv(sym);
332         struct sym_hist *h = priv->hist;
333
334         if (!h->sum)
335                 return;
336
337         sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
338         if (!priv->ext)
339                 return;
340
341         start = he->map->unmap_ip(he->map, sym->start);
342
343         for (i = 0; i < len; i++) {
344                 char *path = NULL;
345                 size_t line_len;
346                 u64 offset;
347                 FILE *fp;
348
349                 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
350                 if (sym_ext[i].percent <= 0.5)
351                         continue;
352
353                 offset = start + i;
354                 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
355                 fp = popen(cmd, "r");
356                 if (!fp)
357                         continue;
358
359                 if (getline(&path, &line_len, fp) < 0 || !line_len)
360                         goto next;
361
362                 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
363                 if (!sym_ext[i].path)
364                         goto next;
365
366                 strcpy(sym_ext[i].path, path);
367                 insert_source_line(&sym_ext[i]);
368
369         next:
370                 pclose(fp);
371         }
372 }
373
374 static void print_summary(const char *filename)
375 {
376         struct sym_ext *sym_ext;
377         struct rb_node *node;
378
379         printf("\nSorted summary for file %s\n", filename);
380         printf("----------------------------------------------\n\n");
381
382         if (RB_EMPTY_ROOT(&root_sym_ext)) {
383                 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
384                 return;
385         }
386
387         node = rb_first(&root_sym_ext);
388         while (node) {
389                 double percent;
390                 const char *color;
391                 char *path;
392
393                 sym_ext = rb_entry(node, struct sym_ext, node);
394                 percent = sym_ext->percent;
395                 color = get_percent_color(percent);
396                 path = sym_ext->path;
397
398                 color_fprintf(stdout, color, " %7.2f %s", percent, path);
399                 node = rb_next(node);
400         }
401 }
402
403 static void annotate_sym(struct hist_entry *he)
404 {
405         struct map *map = he->map;
406         struct dso *dso = map->dso;
407         struct symbol *sym = he->sym;
408         const char *filename = dso->long_name, *d_filename;
409         u64 len;
410         char command[PATH_MAX*2];
411         FILE *file;
412
413         if (!filename)
414                 return;
415
416         if (verbose)
417                 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
418                         __func__, filename, sym->name,
419                         map->unmap_ip(map, sym->start),
420                         map->unmap_ip(map, sym->end));
421
422         if (full_paths)
423                 d_filename = filename;
424         else
425                 d_filename = basename(filename);
426
427         len = sym->end - sym->start;
428
429         if (print_line) {
430                 get_source_line(he, len, filename);
431                 print_summary(filename);
432         }
433
434         printf("\n\n------------------------------------------------\n");
435         printf(" Percent |      Source code & Disassembly of %s\n", d_filename);
436         printf("------------------------------------------------\n");
437
438         if (verbose >= 2)
439                 printf("annotating [%p] %30s : [%p] %30s\n",
440                        dso, dso->long_name, sym, sym->name);
441
442         sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
443                 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
444                 filename, filename);
445
446         if (verbose >= 3)
447                 printf("doing: %s\n", command);
448
449         file = popen(command, "r");
450         if (!file)
451                 return;
452
453         while (!feof(file)) {
454                 if (parse_line(file, he, len) < 0)
455                         break;
456         }
457
458         pclose(file);
459         if (print_line)
460                 free_source_line(he, len);
461 }
462
463 static void find_annotations(void)
464 {
465         struct rb_node *nd;
466
467         for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
468                 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
469                 struct sym_priv *priv;
470
471                 if (he->sym == NULL)
472                         continue;
473
474                 priv = symbol__priv(he->sym);
475                 if (priv->hist == NULL)
476                         continue;
477
478                 annotate_sym(he);
479                 /*
480                  * Since we have a hist_entry per IP for the same symbol, free
481                  * he->sym->hist to signal we already processed this symbol.
482                  */
483                 free(priv->hist);
484                 priv->hist = NULL;
485         }
486 }
487
488 static int __cmd_annotate(void)
489 {
490         int ret, rc = EXIT_FAILURE;
491         unsigned long offset = 0;
492         unsigned long head = 0;
493         struct stat input_stat;
494         event_t *event;
495         uint32_t size;
496         char *buf;
497
498         register_idle_thread();
499
500         input = open(input_name, O_RDONLY);
501         if (input < 0) {
502                 perror("failed to open file");
503                 exit(-1);
504         }
505
506         ret = fstat(input, &input_stat);
507         if (ret < 0) {
508                 perror("failed to stat file");
509                 exit(-1);
510         }
511
512         if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
513                 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
514                 exit(-1);
515         }
516
517         if (!input_stat.st_size) {
518                 fprintf(stderr, "zero-sized file, nothing to do!\n");
519                 exit(0);
520         }
521
522 remap:
523         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
524                            MAP_SHARED, input, offset);
525         if (buf == MAP_FAILED) {
526                 perror("failed to mmap file");
527                 exit(-1);
528         }
529
530 more:
531         event = (event_t *)(buf + head);
532
533         size = event->header.size;
534         if (!size)
535                 size = 8;
536
537         if (head + event->header.size >= page_size * mmap_window) {
538                 unsigned long shift = page_size * (head / page_size);
539                 int munmap_ret;
540
541                 munmap_ret = munmap(buf, page_size * mmap_window);
542                 assert(munmap_ret == 0);
543
544                 offset += shift;
545                 head -= shift;
546                 goto remap;
547         }
548
549         size = event->header.size;
550
551         dump_printf("%p [%p]: event: %d\n",
552                         (void *)(offset + head),
553                         (void *)(long)event->header.size,
554                         event->header.type);
555
556         if (!size || event__process(event) < 0) {
557
558                 dump_printf("%p [%p]: skipping unknown header type: %d\n",
559                         (void *)(offset + head),
560                         (void *)(long)(event->header.size),
561                         event->header.type);
562                 /*
563                  * assume we lost track of the stream, check alignment, and
564                  * increment a single u64 in the hope to catch on again 'soon'.
565                  */
566
567                 if (unlikely(head & 7))
568                         head &= ~7ULL;
569
570                 size = 8;
571         }
572
573         head += size;
574
575         if (offset + head < (unsigned long)input_stat.st_size)
576                 goto more;
577
578         rc = EXIT_SUCCESS;
579         close(input);
580
581
582         if (dump_trace) {
583                 event__print_totals();
584                 return 0;
585         }
586
587         if (verbose > 3)
588                 threads__fprintf(stdout);
589
590         if (verbose > 2)
591                 dsos__fprintf(stdout);
592
593         collapse__resort();
594         output__resort(event__total[0]);
595
596         find_annotations();
597
598         return rc;
599 }
600
601 static const char * const annotate_usage[] = {
602         "perf annotate [<options>] <command>",
603         NULL
604 };
605
606 static const struct option options[] = {
607         OPT_STRING('i', "input", &input_name, "file",
608                     "input file name"),
609         OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
610                     "symbol to annotate"),
611         OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
612         OPT_BOOLEAN('v', "verbose", &verbose,
613                     "be more verbose (show symbol address, etc)"),
614         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
615                     "dump raw trace in ASCII"),
616         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
617                    "file", "vmlinux pathname"),
618         OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
619                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
620         OPT_BOOLEAN('l', "print-line", &print_line,
621                     "print matching source lines (may be slow)"),
622         OPT_BOOLEAN('P', "full-paths", &full_paths,
623                     "Don't shorten the displayed pathnames"),
624         OPT_END()
625 };
626
627 static void setup_sorting(void)
628 {
629         char *tmp, *tok, *str = strdup(sort_order);
630
631         for (tok = strtok_r(str, ", ", &tmp);
632                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
633                 if (sort_dimension__add(tok) < 0) {
634                         error("Unknown --sort key: `%s'", tok);
635                         usage_with_options(annotate_usage, options);
636                 }
637         }
638
639         free(str);
640 }
641
642 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
643 {
644         if (symbol__init(&symbol_conf) < 0)
645                 return -1;
646
647         page_size = getpagesize();
648
649         argc = parse_options(argc, argv, options, annotate_usage, 0);
650
651         setup_sorting();
652
653         if (argc) {
654                 /*
655                  * Special case: if there's an argument left then assume tha
656                  * it's a symbol filter:
657                  */
658                 if (argc > 1)
659                         usage_with_options(annotate_usage, options);
660
661                 sym_hist_filter = argv[0];
662         }
663
664         setup_pager();
665
666         if (field_sep && *field_sep == '.') {
667                 fputs("'.' is the only non valid --field-separator argument\n",
668                                 stderr);
669                 exit(129);
670         }
671
672         return __cmd_annotate();
673 }