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