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