fee663adeea2c2577b674ebda972f1cf7cf6ba45
[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
21 #include "util/parse-options.h"
22 #include "util/parse-events.h"
23
24 #define SHOW_KERNEL     1
25 #define SHOW_USER       2
26 #define SHOW_HV         4
27
28 static char             const *input_name = "perf.data";
29
30 static char             default_sort_order[] = "comm,symbol";
31 static char             *sort_order = default_sort_order;
32
33 static int              input;
34 static int              show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
35
36 static int              dump_trace = 0;
37 #define dprintf(x...)   do { if (dump_trace) printf(x); } while (0)
38
39
40 static int              full_paths;
41
42 static int              print_line;
43
44 static unsigned long    page_size;
45 static unsigned long    mmap_window = 32;
46
47
48 struct sym_ext {
49         struct rb_node  node;
50         double          percent;
51         char            *path;
52 };
53
54 struct map {
55         struct list_head node;
56         u64      start;
57         u64      end;
58         u64      pgoff;
59         u64      (*map_ip)(struct map *, u64);
60         struct dso       *dso;
61 };
62
63 static u64 map__map_ip(struct map *map, u64 ip)
64 {
65         return ip - map->start + map->pgoff;
66 }
67
68 static u64 vdso__map_ip(struct map *map __used, u64 ip)
69 {
70         return ip;
71 }
72
73 static struct map *map__new(struct mmap_event *event)
74 {
75         struct map *self = malloc(sizeof(*self));
76
77         if (self != NULL) {
78                 const char *filename = event->filename;
79
80                 self->start = event->start;
81                 self->end   = event->start + event->len;
82                 self->pgoff = event->pgoff;
83
84                 self->dso = dsos__findnew(filename);
85                 if (self->dso == NULL)
86                         goto out_delete;
87
88                 if (self->dso == vdso)
89                         self->map_ip = vdso__map_ip;
90                 else
91                         self->map_ip = map__map_ip;
92         }
93         return self;
94 out_delete:
95         free(self);
96         return NULL;
97 }
98
99 static struct map *map__clone(struct map *self)
100 {
101         struct map *map = malloc(sizeof(*self));
102
103         if (!map)
104                 return NULL;
105
106         memcpy(map, self, sizeof(*self));
107
108         return map;
109 }
110
111 static int map__overlap(struct map *l, struct map *r)
112 {
113         if (l->start > r->start) {
114                 struct map *t = l;
115                 l = r;
116                 r = t;
117         }
118
119         if (l->end > r->start)
120                 return 1;
121
122         return 0;
123 }
124
125 static size_t map__fprintf(struct map *self, FILE *fp)
126 {
127         return fprintf(fp, " %Lx-%Lx %Lx %s\n",
128                        self->start, self->end, self->pgoff, self->dso->name);
129 }
130
131
132 struct thread {
133         struct rb_node   rb_node;
134         struct list_head maps;
135         pid_t            pid;
136         char             *comm;
137 };
138
139 static struct thread *thread__new(pid_t pid)
140 {
141         struct thread *self = malloc(sizeof(*self));
142
143         if (self != NULL) {
144                 self->pid = pid;
145                 self->comm = malloc(32);
146                 if (self->comm)
147                         snprintf(self->comm, 32, ":%d", self->pid);
148                 INIT_LIST_HEAD(&self->maps);
149         }
150
151         return self;
152 }
153
154 static int thread__set_comm(struct thread *self, const char *comm)
155 {
156         if (self->comm)
157                 free(self->comm);
158         self->comm = strdup(comm);
159         return self->comm ? 0 : -ENOMEM;
160 }
161
162 static size_t thread__fprintf(struct thread *self, FILE *fp)
163 {
164         struct map *pos;
165         size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
166
167         list_for_each_entry(pos, &self->maps, node)
168                 ret += map__fprintf(pos, fp);
169
170         return ret;
171 }
172
173
174 static struct rb_root threads;
175 static struct thread *last_match;
176
177 static struct thread *threads__findnew(pid_t pid)
178 {
179         struct rb_node **p = &threads.rb_node;
180         struct rb_node *parent = NULL;
181         struct thread *th;
182
183         /*
184          * Font-end cache - PID lookups come in blocks,
185          * so most of the time we dont have to look up
186          * the full rbtree:
187          */
188         if (last_match && last_match->pid == pid)
189                 return last_match;
190
191         while (*p != NULL) {
192                 parent = *p;
193                 th = rb_entry(parent, struct thread, rb_node);
194
195                 if (th->pid == pid) {
196                         last_match = th;
197                         return th;
198                 }
199
200                 if (pid < th->pid)
201                         p = &(*p)->rb_left;
202                 else
203                         p = &(*p)->rb_right;
204         }
205
206         th = thread__new(pid);
207         if (th != NULL) {
208                 rb_link_node(&th->rb_node, parent, p);
209                 rb_insert_color(&th->rb_node, &threads);
210                 last_match = th;
211         }
212
213         return th;
214 }
215
216 static void thread__insert_map(struct thread *self, struct map *map)
217 {
218         struct map *pos, *tmp;
219
220         list_for_each_entry_safe(pos, tmp, &self->maps, node) {
221                 if (map__overlap(pos, map)) {
222                         list_del_init(&pos->node);
223                         /* XXX leaks dsos */
224                         free(pos);
225                 }
226         }
227
228         list_add_tail(&map->node, &self->maps);
229 }
230
231 static int thread__fork(struct thread *self, struct thread *parent)
232 {
233         struct map *map;
234
235         if (self->comm)
236                 free(self->comm);
237         self->comm = strdup(parent->comm);
238         if (!self->comm)
239                 return -ENOMEM;
240
241         list_for_each_entry(map, &parent->maps, node) {
242                 struct map *new = map__clone(map);
243                 if (!new)
244                         return -ENOMEM;
245                 thread__insert_map(self, new);
246         }
247
248         return 0;
249 }
250
251 static struct map *thread__find_map(struct thread *self, u64 ip)
252 {
253         struct map *pos;
254
255         if (self == NULL)
256                 return NULL;
257
258         list_for_each_entry(pos, &self->maps, node)
259                 if (ip >= pos->start && ip <= pos->end)
260                         return pos;
261
262         return NULL;
263 }
264
265 static size_t threads__fprintf(FILE *fp)
266 {
267         size_t ret = 0;
268         struct rb_node *nd;
269
270         for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
271                 struct thread *pos = rb_entry(nd, struct thread, rb_node);
272
273                 ret += thread__fprintf(pos, fp);
274         }
275
276         return ret;
277 }
278
279 /*
280  * histogram, sorted on item, collects counts
281  */
282
283 static struct rb_root hist;
284
285 struct hist_entry {
286         struct rb_node   rb_node;
287
288         struct thread    *thread;
289         struct map       *map;
290         struct dso       *dso;
291         struct symbol    *sym;
292         u64      ip;
293         char             level;
294
295         uint32_t         count;
296 };
297
298 /*
299  * configurable sorting bits
300  */
301
302 struct sort_entry {
303         struct list_head list;
304
305         char *header;
306
307         int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
308         int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
309         size_t  (*print)(FILE *fp, struct hist_entry *);
310 };
311
312 /* --sort pid */
313
314 static int64_t
315 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
316 {
317         return right->thread->pid - left->thread->pid;
318 }
319
320 static size_t
321 sort__thread_print(FILE *fp, struct hist_entry *self)
322 {
323         return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
324 }
325
326 static struct sort_entry sort_thread = {
327         .header = "         Command:  Pid",
328         .cmp    = sort__thread_cmp,
329         .print  = sort__thread_print,
330 };
331
332 /* --sort comm */
333
334 static int64_t
335 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
336 {
337         return right->thread->pid - left->thread->pid;
338 }
339
340 static int64_t
341 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
342 {
343         char *comm_l = left->thread->comm;
344         char *comm_r = right->thread->comm;
345
346         if (!comm_l || !comm_r) {
347                 if (!comm_l && !comm_r)
348                         return 0;
349                 else if (!comm_l)
350                         return -1;
351                 else
352                         return 1;
353         }
354
355         return strcmp(comm_l, comm_r);
356 }
357
358 static size_t
359 sort__comm_print(FILE *fp, struct hist_entry *self)
360 {
361         return fprintf(fp, "%16s", self->thread->comm);
362 }
363
364 static struct sort_entry sort_comm = {
365         .header         = "         Command",
366         .cmp            = sort__comm_cmp,
367         .collapse       = sort__comm_collapse,
368         .print          = sort__comm_print,
369 };
370
371 /* --sort dso */
372
373 static int64_t
374 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
375 {
376         struct dso *dso_l = left->dso;
377         struct dso *dso_r = right->dso;
378
379         if (!dso_l || !dso_r) {
380                 if (!dso_l && !dso_r)
381                         return 0;
382                 else if (!dso_l)
383                         return -1;
384                 else
385                         return 1;
386         }
387
388         return strcmp(dso_l->name, dso_r->name);
389 }
390
391 static size_t
392 sort__dso_print(FILE *fp, struct hist_entry *self)
393 {
394         if (self->dso)
395                 return fprintf(fp, "%-25s", self->dso->name);
396
397         return fprintf(fp, "%016llx         ", (u64)self->ip);
398 }
399
400 static struct sort_entry sort_dso = {
401         .header = "Shared Object            ",
402         .cmp    = sort__dso_cmp,
403         .print  = sort__dso_print,
404 };
405
406 /* --sort symbol */
407
408 static int64_t
409 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
410 {
411         u64 ip_l, ip_r;
412
413         if (left->sym == right->sym)
414                 return 0;
415
416         ip_l = left->sym ? left->sym->start : left->ip;
417         ip_r = right->sym ? right->sym->start : right->ip;
418
419         return (int64_t)(ip_r - ip_l);
420 }
421
422 static size_t
423 sort__sym_print(FILE *fp, struct hist_entry *self)
424 {
425         size_t ret = 0;
426
427         if (verbose)
428                 ret += fprintf(fp, "%#018llx  ", (u64)self->ip);
429
430         if (self->sym) {
431                 ret += fprintf(fp, "[%c] %s",
432                         self->dso == kernel_dso ? 'k' : '.', self->sym->name);
433         } else {
434                 ret += fprintf(fp, "%#016llx", (u64)self->ip);
435         }
436
437         return ret;
438 }
439
440 static struct sort_entry sort_sym = {
441         .header = "Symbol",
442         .cmp    = sort__sym_cmp,
443         .print  = sort__sym_print,
444 };
445
446 static int sort__need_collapse = 0;
447
448 struct sort_dimension {
449         char                    *name;
450         struct sort_entry       *entry;
451         int                     taken;
452 };
453
454 static struct sort_dimension sort_dimensions[] = {
455         { .name = "pid",        .entry = &sort_thread,  },
456         { .name = "comm",       .entry = &sort_comm,    },
457         { .name = "dso",        .entry = &sort_dso,     },
458         { .name = "symbol",     .entry = &sort_sym,     },
459 };
460
461 static LIST_HEAD(hist_entry__sort_list);
462
463 static int sort_dimension__add(char *tok)
464 {
465         unsigned int i;
466
467         for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
468                 struct sort_dimension *sd = &sort_dimensions[i];
469
470                 if (sd->taken)
471                         continue;
472
473                 if (strncasecmp(tok, sd->name, strlen(tok)))
474                         continue;
475
476                 if (sd->entry->collapse)
477                         sort__need_collapse = 1;
478
479                 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
480                 sd->taken = 1;
481
482                 return 0;
483         }
484
485         return -ESRCH;
486 }
487
488 static int64_t
489 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
490 {
491         struct sort_entry *se;
492         int64_t cmp = 0;
493
494         list_for_each_entry(se, &hist_entry__sort_list, list) {
495                 cmp = se->cmp(left, right);
496                 if (cmp)
497                         break;
498         }
499
500         return cmp;
501 }
502
503 static int64_t
504 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
505 {
506         struct sort_entry *se;
507         int64_t cmp = 0;
508
509         list_for_each_entry(se, &hist_entry__sort_list, list) {
510                 int64_t (*f)(struct hist_entry *, struct hist_entry *);
511
512                 f = se->collapse ?: se->cmp;
513
514                 cmp = f(left, right);
515                 if (cmp)
516                         break;
517         }
518
519         return cmp;
520 }
521
522 /*
523  * collect histogram counts
524  */
525 static void hist_hit(struct hist_entry *he, u64 ip)
526 {
527         unsigned int sym_size, offset;
528         struct symbol *sym = he->sym;
529
530         he->count++;
531
532         if (!sym || !sym->hist)
533                 return;
534
535         sym_size = sym->end - sym->start;
536         offset = ip - sym->start;
537
538         if (offset >= sym_size)
539                 return;
540
541         sym->hist_sum++;
542         sym->hist[offset]++;
543
544         if (verbose >= 3)
545                 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
546                         (void *)(unsigned long)he->sym->start,
547                         he->sym->name,
548                         (void *)(unsigned long)ip, ip - he->sym->start,
549                         sym->hist[offset]);
550 }
551
552 static int
553 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
554                 struct symbol *sym, u64 ip, char level)
555 {
556         struct rb_node **p = &hist.rb_node;
557         struct rb_node *parent = NULL;
558         struct hist_entry *he;
559         struct hist_entry entry = {
560                 .thread = thread,
561                 .map    = map,
562                 .dso    = dso,
563                 .sym    = sym,
564                 .ip     = ip,
565                 .level  = level,
566                 .count  = 1,
567         };
568         int cmp;
569
570         while (*p != NULL) {
571                 parent = *p;
572                 he = rb_entry(parent, struct hist_entry, rb_node);
573
574                 cmp = hist_entry__cmp(&entry, he);
575
576                 if (!cmp) {
577                         hist_hit(he, ip);
578
579                         return 0;
580                 }
581
582                 if (cmp < 0)
583                         p = &(*p)->rb_left;
584                 else
585                         p = &(*p)->rb_right;
586         }
587
588         he = malloc(sizeof(*he));
589         if (!he)
590                 return -ENOMEM;
591         *he = entry;
592         rb_link_node(&he->rb_node, parent, p);
593         rb_insert_color(&he->rb_node, &hist);
594
595         return 0;
596 }
597
598 static void hist_entry__free(struct hist_entry *he)
599 {
600         free(he);
601 }
602
603 /*
604  * collapse the histogram
605  */
606
607 static struct rb_root collapse_hists;
608
609 static void collapse__insert_entry(struct hist_entry *he)
610 {
611         struct rb_node **p = &collapse_hists.rb_node;
612         struct rb_node *parent = NULL;
613         struct hist_entry *iter;
614         int64_t cmp;
615
616         while (*p != NULL) {
617                 parent = *p;
618                 iter = rb_entry(parent, struct hist_entry, rb_node);
619
620                 cmp = hist_entry__collapse(iter, he);
621
622                 if (!cmp) {
623                         iter->count += he->count;
624                         hist_entry__free(he);
625                         return;
626                 }
627
628                 if (cmp < 0)
629                         p = &(*p)->rb_left;
630                 else
631                         p = &(*p)->rb_right;
632         }
633
634         rb_link_node(&he->rb_node, parent, p);
635         rb_insert_color(&he->rb_node, &collapse_hists);
636 }
637
638 static void collapse__resort(void)
639 {
640         struct rb_node *next;
641         struct hist_entry *n;
642
643         if (!sort__need_collapse)
644                 return;
645
646         next = rb_first(&hist);
647         while (next) {
648                 n = rb_entry(next, struct hist_entry, rb_node);
649                 next = rb_next(&n->rb_node);
650
651                 rb_erase(&n->rb_node, &hist);
652                 collapse__insert_entry(n);
653         }
654 }
655
656 /*
657  * reverse the map, sort on count.
658  */
659
660 static struct rb_root output_hists;
661
662 static void output__insert_entry(struct hist_entry *he)
663 {
664         struct rb_node **p = &output_hists.rb_node;
665         struct rb_node *parent = NULL;
666         struct hist_entry *iter;
667
668         while (*p != NULL) {
669                 parent = *p;
670                 iter = rb_entry(parent, struct hist_entry, rb_node);
671
672                 if (he->count > iter->count)
673                         p = &(*p)->rb_left;
674                 else
675                         p = &(*p)->rb_right;
676         }
677
678         rb_link_node(&he->rb_node, parent, p);
679         rb_insert_color(&he->rb_node, &output_hists);
680 }
681
682 static void output__resort(void)
683 {
684         struct rb_node *next;
685         struct hist_entry *n;
686         struct rb_root *tree = &hist;
687
688         if (sort__need_collapse)
689                 tree = &collapse_hists;
690
691         next = rb_first(tree);
692
693         while (next) {
694                 n = rb_entry(next, struct hist_entry, rb_node);
695                 next = rb_next(&n->rb_node);
696
697                 rb_erase(&n->rb_node, tree);
698                 output__insert_entry(n);
699         }
700 }
701
702 static void register_idle_thread(void)
703 {
704         struct thread *thread = threads__findnew(0);
705
706         if (thread == NULL ||
707                         thread__set_comm(thread, "[idle]")) {
708                 fprintf(stderr, "problem inserting idle task.\n");
709                 exit(-1);
710         }
711 }
712
713 static unsigned long total = 0,
714                      total_mmap = 0,
715                      total_comm = 0,
716                      total_fork = 0,
717                      total_unknown = 0;
718
719 static int
720 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
721 {
722         char level;
723         int show = 0;
724         struct dso *dso = NULL;
725         struct thread *thread = threads__findnew(event->ip.pid);
726         u64 ip = event->ip.ip;
727         struct map *map = NULL;
728
729         dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
730                 (void *)(offset + head),
731                 (void *)(long)(event->header.size),
732                 event->header.misc,
733                 event->ip.pid,
734                 (void *)(long)ip);
735
736         dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
737
738         if (thread == NULL) {
739                 fprintf(stderr, "problem processing %d event, skipping it.\n",
740                         event->header.type);
741                 return -1;
742         }
743
744         if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
745                 show = SHOW_KERNEL;
746                 level = 'k';
747
748                 dso = kernel_dso;
749
750                 dprintf(" ...... dso: %s\n", dso->name);
751
752         } else if (event->header.misc & PERF_EVENT_MISC_USER) {
753
754                 show = SHOW_USER;
755                 level = '.';
756
757                 map = thread__find_map(thread, ip);
758                 if (map != NULL) {
759                         ip = map->map_ip(map, ip);
760                         dso = map->dso;
761                 } else {
762                         /*
763                          * If this is outside of all known maps,
764                          * and is a negative address, try to look it
765                          * up in the kernel dso, as it might be a
766                          * vsyscall (which executes in user-mode):
767                          */
768                         if ((long long)ip < 0)
769                                 dso = kernel_dso;
770                 }
771                 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
772
773         } else {
774                 show = SHOW_HV;
775                 level = 'H';
776                 dprintf(" ...... dso: [hypervisor]\n");
777         }
778
779         if (show & show_mask) {
780                 struct symbol *sym = NULL;
781
782                 if (dso)
783                         sym = dso->find_symbol(dso, ip);
784
785                 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
786                         fprintf(stderr,
787                 "problem incrementing symbol count, skipping event\n");
788                         return -1;
789                 }
790         }
791         total++;
792
793         return 0;
794 }
795
796 static int
797 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
798 {
799         struct thread *thread = threads__findnew(event->mmap.pid);
800         struct map *map = map__new(&event->mmap);
801
802         dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
803                 (void *)(offset + head),
804                 (void *)(long)(event->header.size),
805                 event->mmap.pid,
806                 (void *)(long)event->mmap.start,
807                 (void *)(long)event->mmap.len,
808                 (void *)(long)event->mmap.pgoff,
809                 event->mmap.filename);
810
811         if (thread == NULL || map == NULL) {
812                 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
813                 return 0;
814         }
815
816         thread__insert_map(thread, map);
817         total_mmap++;
818
819         return 0;
820 }
821
822 static int
823 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
824 {
825         struct thread *thread = threads__findnew(event->comm.pid);
826
827         dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
828                 (void *)(offset + head),
829                 (void *)(long)(event->header.size),
830                 event->comm.comm, event->comm.pid);
831
832         if (thread == NULL ||
833             thread__set_comm(thread, event->comm.comm)) {
834                 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
835                 return -1;
836         }
837         total_comm++;
838
839         return 0;
840 }
841
842 static int
843 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
844 {
845         struct thread *thread = threads__findnew(event->fork.pid);
846         struct thread *parent = threads__findnew(event->fork.ppid);
847
848         dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
849                 (void *)(offset + head),
850                 (void *)(long)(event->header.size),
851                 event->fork.pid, event->fork.ppid);
852
853         if (!thread || !parent || thread__fork(thread, parent)) {
854                 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
855                 return -1;
856         }
857         total_fork++;
858
859         return 0;
860 }
861
862 static int
863 process_event(event_t *event, unsigned long offset, unsigned long head)
864 {
865         switch (event->header.type) {
866         case PERF_EVENT_SAMPLE:
867                 return process_sample_event(event, offset, head);
868
869         case PERF_EVENT_MMAP:
870                 return process_mmap_event(event, offset, head);
871
872         case PERF_EVENT_COMM:
873                 return process_comm_event(event, offset, head);
874
875         case PERF_EVENT_FORK:
876                 return process_fork_event(event, offset, head);
877         /*
878          * We dont process them right now but they are fine:
879          */
880
881         case PERF_EVENT_THROTTLE:
882         case PERF_EVENT_UNTHROTTLE:
883                 return 0;
884
885         default:
886                 return -1;
887         }
888
889         return 0;
890 }
891
892 static int
893 parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
894 {
895         char *line = NULL, *tmp, *tmp2;
896         static const char *prev_line;
897         static const char *prev_color;
898         unsigned int offset;
899         size_t line_len;
900         s64 line_ip;
901         int ret;
902         char *c;
903
904         if (getline(&line, &line_len, file) < 0)
905                 return -1;
906         if (!line)
907                 return -1;
908
909         c = strchr(line, '\n');
910         if (c)
911                 *c = 0;
912
913         line_ip = -1;
914         offset = 0;
915         ret = -2;
916
917         /*
918          * Strip leading spaces:
919          */
920         tmp = line;
921         while (*tmp) {
922                 if (*tmp != ' ')
923                         break;
924                 tmp++;
925         }
926
927         if (*tmp) {
928                 /*
929                  * Parse hexa addresses followed by ':'
930                  */
931                 line_ip = strtoull(tmp, &tmp2, 16);
932                 if (*tmp2 != ':')
933                         line_ip = -1;
934         }
935
936         if (line_ip != -1) {
937                 const char *path = NULL;
938                 unsigned int hits = 0;
939                 double percent = 0.0;
940                 char *color;
941                 struct sym_ext *sym_ext = sym->priv;
942
943                 offset = line_ip - start;
944                 if (offset < len)
945                         hits = sym->hist[offset];
946
947                 if (offset < len && sym_ext) {
948                         path = sym_ext[offset].path;
949                         percent = sym_ext[offset].percent;
950                 } else if (sym->hist_sum)
951                         percent = 100.0 * hits / sym->hist_sum;
952
953                 color = get_percent_color(percent);
954
955                 /*
956                  * Also color the filename and line if needed, with
957                  * the same color than the percentage. Don't print it
958                  * twice for close colored ip with the same filename:line
959                  */
960                 if (path) {
961                         if (!prev_line || strcmp(prev_line, path)
962                                        || color != prev_color) {
963                                 color_fprintf(stdout, color, " %s", path);
964                                 prev_line = path;
965                                 prev_color = color;
966                         }
967                 }
968
969                 color_fprintf(stdout, color, " %7.2f", percent);
970                 printf(" :      ");
971                 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
972         } else {
973                 if (!*line)
974                         printf("         :\n");
975                 else
976                         printf("         :      %s\n", line);
977         }
978
979         return 0;
980 }
981
982 static struct rb_root root_sym_ext;
983
984 static void insert_source_line(struct sym_ext *sym_ext)
985 {
986         struct sym_ext *iter;
987         struct rb_node **p = &root_sym_ext.rb_node;
988         struct rb_node *parent = NULL;
989
990         while (*p != NULL) {
991                 parent = *p;
992                 iter = rb_entry(parent, struct sym_ext, node);
993
994                 if (sym_ext->percent > iter->percent)
995                         p = &(*p)->rb_left;
996                 else
997                         p = &(*p)->rb_right;
998         }
999
1000         rb_link_node(&sym_ext->node, parent, p);
1001         rb_insert_color(&sym_ext->node, &root_sym_ext);
1002 }
1003
1004 static void free_source_line(struct symbol *sym, int len)
1005 {
1006         struct sym_ext *sym_ext = sym->priv;
1007         int i;
1008
1009         if (!sym_ext)
1010                 return;
1011
1012         for (i = 0; i < len; i++)
1013                 free(sym_ext[i].path);
1014         free(sym_ext);
1015
1016         sym->priv = NULL;
1017         root_sym_ext = RB_ROOT;
1018 }
1019
1020 /* Get the filename:line for the colored entries */
1021 static void
1022 get_source_line(struct symbol *sym, u64 start, int len, char *filename)
1023 {
1024         int i;
1025         char cmd[PATH_MAX * 2];
1026         struct sym_ext *sym_ext;
1027
1028         if (!sym->hist_sum)
1029                 return;
1030
1031         sym->priv = calloc(len, sizeof(struct sym_ext));
1032         if (!sym->priv)
1033                 return;
1034
1035         sym_ext = sym->priv;
1036
1037         for (i = 0; i < len; i++) {
1038                 char *path = NULL;
1039                 size_t line_len;
1040                 u64 offset;
1041                 FILE *fp;
1042
1043                 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
1044                 if (sym_ext[i].percent <= 0.5)
1045                         continue;
1046
1047                 offset = start + i;
1048                 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
1049                 fp = popen(cmd, "r");
1050                 if (!fp)
1051                         continue;
1052
1053                 if (getline(&path, &line_len, fp) < 0 || !line_len)
1054                         goto next;
1055
1056                 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
1057                 if (!sym_ext[i].path)
1058                         goto next;
1059
1060                 strcpy(sym_ext[i].path, path);
1061                 insert_source_line(&sym_ext[i]);
1062
1063         next:
1064                 pclose(fp);
1065         }
1066 }
1067
1068 static void print_summary(char *filename)
1069 {
1070         struct sym_ext *sym_ext;
1071         struct rb_node *node;
1072
1073         printf("\nSorted summary for file %s\n", filename);
1074         printf("----------------------------------------------\n\n");
1075
1076         if (RB_EMPTY_ROOT(&root_sym_ext)) {
1077                 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1078                 return;
1079         }
1080
1081         node = rb_first(&root_sym_ext);
1082         while (node) {
1083                 double percent;
1084                 char *color;
1085                 char *path;
1086
1087                 sym_ext = rb_entry(node, struct sym_ext, node);
1088                 percent = sym_ext->percent;
1089                 color = get_percent_color(percent);
1090                 path = sym_ext->path;
1091
1092                 color_fprintf(stdout, color, " %7.2f %s", percent, path);
1093                 node = rb_next(node);
1094         }
1095 }
1096
1097 static void annotate_sym(struct dso *dso, struct symbol *sym)
1098 {
1099         char *filename = dso->name, *d_filename;
1100         u64 start, end, len;
1101         char command[PATH_MAX*2];
1102         FILE *file;
1103
1104         if (!filename)
1105                 return;
1106         if (sym->module)
1107                 filename = sym->module->path;
1108         else if (dso == kernel_dso)
1109                 filename = vmlinux;
1110
1111         start = sym->obj_start;
1112         if (!start)
1113                 start = sym->start;
1114         if (full_paths)
1115                 d_filename = filename;
1116         else
1117                 d_filename = basename(filename);
1118
1119         end = start + sym->end - sym->start + 1;
1120         len = sym->end - sym->start;
1121
1122         if (print_line) {
1123                 get_source_line(sym, start, len, filename);
1124                 print_summary(filename);
1125         }
1126
1127         printf("\n\n------------------------------------------------\n");
1128         printf(" Percent |      Source code & Disassembly of %s\n", d_filename);
1129         printf("------------------------------------------------\n");
1130
1131         if (verbose >= 2)
1132                 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
1133
1134         sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
1135                         (u64)start, (u64)end, filename, filename);
1136
1137         if (verbose >= 3)
1138                 printf("doing: %s\n", command);
1139
1140         file = popen(command, "r");
1141         if (!file)
1142                 return;
1143
1144         while (!feof(file)) {
1145                 if (parse_line(file, sym, start, len) < 0)
1146                         break;
1147         }
1148
1149         pclose(file);
1150         if (print_line)
1151                 free_source_line(sym, len);
1152 }
1153
1154 static void find_annotations(void)
1155 {
1156         struct rb_node *nd;
1157         struct dso *dso;
1158         int count = 0;
1159
1160         list_for_each_entry(dso, &dsos, node) {
1161
1162                 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
1163                         struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
1164
1165                         if (sym->hist) {
1166                                 annotate_sym(dso, sym);
1167                                 count++;
1168                         }
1169                 }
1170         }
1171
1172         if (!count)
1173                 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
1174 }
1175
1176 static int __cmd_annotate(void)
1177 {
1178         int ret, rc = EXIT_FAILURE;
1179         unsigned long offset = 0;
1180         unsigned long head = 0;
1181         struct stat stat;
1182         event_t *event;
1183         uint32_t size;
1184         char *buf;
1185
1186         register_idle_thread();
1187
1188         input = open(input_name, O_RDONLY);
1189         if (input < 0) {
1190                 perror("failed to open file");
1191                 exit(-1);
1192         }
1193
1194         ret = fstat(input, &stat);
1195         if (ret < 0) {
1196                 perror("failed to stat file");
1197                 exit(-1);
1198         }
1199
1200         if (!stat.st_size) {
1201                 fprintf(stderr, "zero-sized file, nothing to do!\n");
1202                 exit(0);
1203         }
1204
1205         if (load_kernel() < 0) {
1206                 perror("failed to load kernel symbols");
1207                 return EXIT_FAILURE;
1208         }
1209
1210 remap:
1211         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1212                            MAP_SHARED, input, offset);
1213         if (buf == MAP_FAILED) {
1214                 perror("failed to mmap file");
1215                 exit(-1);
1216         }
1217
1218 more:
1219         event = (event_t *)(buf + head);
1220
1221         size = event->header.size;
1222         if (!size)
1223                 size = 8;
1224
1225         if (head + event->header.size >= page_size * mmap_window) {
1226                 unsigned long shift = page_size * (head / page_size);
1227                 int ret;
1228
1229                 ret = munmap(buf, page_size * mmap_window);
1230                 assert(ret == 0);
1231
1232                 offset += shift;
1233                 head -= shift;
1234                 goto remap;
1235         }
1236
1237         size = event->header.size;
1238
1239         dprintf("%p [%p]: event: %d\n",
1240                         (void *)(offset + head),
1241                         (void *)(long)event->header.size,
1242                         event->header.type);
1243
1244         if (!size || process_event(event, offset, head) < 0) {
1245
1246                 dprintf("%p [%p]: skipping unknown header type: %d\n",
1247                         (void *)(offset + head),
1248                         (void *)(long)(event->header.size),
1249                         event->header.type);
1250
1251                 total_unknown++;
1252
1253                 /*
1254                  * assume we lost track of the stream, check alignment, and
1255                  * increment a single u64 in the hope to catch on again 'soon'.
1256                  */
1257
1258                 if (unlikely(head & 7))
1259                         head &= ~7ULL;
1260
1261                 size = 8;
1262         }
1263
1264         head += size;
1265
1266         if (offset + head < (unsigned long)stat.st_size)
1267                 goto more;
1268
1269         rc = EXIT_SUCCESS;
1270         close(input);
1271
1272         dprintf("      IP events: %10ld\n", total);
1273         dprintf("    mmap events: %10ld\n", total_mmap);
1274         dprintf("    comm events: %10ld\n", total_comm);
1275         dprintf("    fork events: %10ld\n", total_fork);
1276         dprintf(" unknown events: %10ld\n", total_unknown);
1277
1278         if (dump_trace)
1279                 return 0;
1280
1281         if (verbose >= 3)
1282                 threads__fprintf(stdout);
1283
1284         if (verbose >= 2)
1285                 dsos__fprintf(stdout);
1286
1287         collapse__resort();
1288         output__resort();
1289
1290         find_annotations();
1291
1292         return rc;
1293 }
1294
1295 static const char * const annotate_usage[] = {
1296         "perf annotate [<options>] <command>",
1297         NULL
1298 };
1299
1300 static const struct option options[] = {
1301         OPT_STRING('i', "input", &input_name, "file",
1302                     "input file name"),
1303         OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
1304                     "symbol to annotate"),
1305         OPT_BOOLEAN('v', "verbose", &verbose,
1306                     "be more verbose (show symbol address, etc)"),
1307         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1308                     "dump raw trace in ASCII"),
1309         OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1310         OPT_BOOLEAN('m', "modules", &modules,
1311                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
1312         OPT_BOOLEAN('l', "print-line", &print_line,
1313                     "print matching source lines (may be slow)"),
1314         OPT_BOOLEAN('P', "full-paths", &full_paths,
1315                     "Don't shorten the displayed pathnames"),
1316         OPT_END()
1317 };
1318
1319 static void setup_sorting(void)
1320 {
1321         char *tmp, *tok, *str = strdup(sort_order);
1322
1323         for (tok = strtok_r(str, ", ", &tmp);
1324                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
1325                 if (sort_dimension__add(tok) < 0) {
1326                         error("Unknown --sort key: `%s'", tok);
1327                         usage_with_options(annotate_usage, options);
1328                 }
1329         }
1330
1331         free(str);
1332 }
1333
1334 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1335 {
1336         symbol__init();
1337
1338         page_size = getpagesize();
1339
1340         argc = parse_options(argc, argv, options, annotate_usage, 0);
1341
1342         setup_sorting();
1343
1344         if (argc) {
1345                 /*
1346                  * Special case: if there's an argument left then assume tha
1347                  * it's a symbol filter:
1348                  */
1349                 if (argc > 1)
1350                         usage_with_options(annotate_usage, options);
1351
1352                 sym_hist_filter = argv[0];
1353         }
1354
1355         if (!sym_hist_filter)
1356                 usage_with_options(annotate_usage, options);
1357
1358         setup_pager();
1359
1360         return __cmd_annotate();
1361 }