perf tools: Factorize the event structure definitions in a single file
[linux-2.6/kvm.git] / tools / perf / builtin-annotate.c
blobfee663adeea2c2577b674ebda972f1cf7cf6ba45
1 /*
2 * builtin-annotate.c
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"
10 #include "util/util.h"
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"
19 #include "perf.h"
21 #include "util/parse-options.h"
22 #include "util/parse-events.h"
24 #define SHOW_KERNEL 1
25 #define SHOW_USER 2
26 #define SHOW_HV 4
28 static char const *input_name = "perf.data";
30 static char default_sort_order[] = "comm,symbol";
31 static char *sort_order = default_sort_order;
33 static int input;
34 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
36 static int dump_trace = 0;
37 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
40 static int full_paths;
42 static int print_line;
44 static unsigned long page_size;
45 static unsigned long mmap_window = 32;
48 struct sym_ext {
49 struct rb_node node;
50 double percent;
51 char *path;
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;
63 static u64 map__map_ip(struct map *map, u64 ip)
65 return ip - map->start + map->pgoff;
68 static u64 vdso__map_ip(struct map *map __used, u64 ip)
70 return ip;
73 static struct map *map__new(struct mmap_event *event)
75 struct map *self = malloc(sizeof(*self));
77 if (self != NULL) {
78 const char *filename = event->filename;
80 self->start = event->start;
81 self->end = event->start + event->len;
82 self->pgoff = event->pgoff;
84 self->dso = dsos__findnew(filename);
85 if (self->dso == NULL)
86 goto out_delete;
88 if (self->dso == vdso)
89 self->map_ip = vdso__map_ip;
90 else
91 self->map_ip = map__map_ip;
93 return self;
94 out_delete:
95 free(self);
96 return NULL;
99 static struct map *map__clone(struct map *self)
101 struct map *map = malloc(sizeof(*self));
103 if (!map)
104 return NULL;
106 memcpy(map, self, sizeof(*self));
108 return map;
111 static int map__overlap(struct map *l, struct map *r)
113 if (l->start > r->start) {
114 struct map *t = l;
115 l = r;
116 r = t;
119 if (l->end > r->start)
120 return 1;
122 return 0;
125 static size_t map__fprintf(struct map *self, FILE *fp)
127 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
128 self->start, self->end, self->pgoff, self->dso->name);
132 struct thread {
133 struct rb_node rb_node;
134 struct list_head maps;
135 pid_t pid;
136 char *comm;
139 static struct thread *thread__new(pid_t pid)
141 struct thread *self = malloc(sizeof(*self));
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);
151 return self;
154 static int thread__set_comm(struct thread *self, const char *comm)
156 if (self->comm)
157 free(self->comm);
158 self->comm = strdup(comm);
159 return self->comm ? 0 : -ENOMEM;
162 static size_t thread__fprintf(struct thread *self, FILE *fp)
164 struct map *pos;
165 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
167 list_for_each_entry(pos, &self->maps, node)
168 ret += map__fprintf(pos, fp);
170 return ret;
174 static struct rb_root threads;
175 static struct thread *last_match;
177 static struct thread *threads__findnew(pid_t pid)
179 struct rb_node **p = &threads.rb_node;
180 struct rb_node *parent = NULL;
181 struct thread *th;
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:
188 if (last_match && last_match->pid == pid)
189 return last_match;
191 while (*p != NULL) {
192 parent = *p;
193 th = rb_entry(parent, struct thread, rb_node);
195 if (th->pid == pid) {
196 last_match = th;
197 return th;
200 if (pid < th->pid)
201 p = &(*p)->rb_left;
202 else
203 p = &(*p)->rb_right;
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;
213 return th;
216 static void thread__insert_map(struct thread *self, struct map *map)
218 struct map *pos, *tmp;
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);
228 list_add_tail(&map->node, &self->maps);
231 static int thread__fork(struct thread *self, struct thread *parent)
233 struct map *map;
235 if (self->comm)
236 free(self->comm);
237 self->comm = strdup(parent->comm);
238 if (!self->comm)
239 return -ENOMEM;
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);
248 return 0;
251 static struct map *thread__find_map(struct thread *self, u64 ip)
253 struct map *pos;
255 if (self == NULL)
256 return NULL;
258 list_for_each_entry(pos, &self->maps, node)
259 if (ip >= pos->start && ip <= pos->end)
260 return pos;
262 return NULL;
265 static size_t threads__fprintf(FILE *fp)
267 size_t ret = 0;
268 struct rb_node *nd;
270 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
271 struct thread *pos = rb_entry(nd, struct thread, rb_node);
273 ret += thread__fprintf(pos, fp);
276 return ret;
280 * histogram, sorted on item, collects counts
283 static struct rb_root hist;
285 struct hist_entry {
286 struct rb_node rb_node;
288 struct thread *thread;
289 struct map *map;
290 struct dso *dso;
291 struct symbol *sym;
292 u64 ip;
293 char level;
295 uint32_t count;
299 * configurable sorting bits
302 struct sort_entry {
303 struct list_head list;
305 char *header;
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 *);
312 /* --sort pid */
314 static int64_t
315 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
317 return right->thread->pid - left->thread->pid;
320 static size_t
321 sort__thread_print(FILE *fp, struct hist_entry *self)
323 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
326 static struct sort_entry sort_thread = {
327 .header = " Command: Pid",
328 .cmp = sort__thread_cmp,
329 .print = sort__thread_print,
332 /* --sort comm */
334 static int64_t
335 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
337 return right->thread->pid - left->thread->pid;
340 static int64_t
341 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
343 char *comm_l = left->thread->comm;
344 char *comm_r = right->thread->comm;
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;
355 return strcmp(comm_l, comm_r);
358 static size_t
359 sort__comm_print(FILE *fp, struct hist_entry *self)
361 return fprintf(fp, "%16s", self->thread->comm);
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,
371 /* --sort dso */
373 static int64_t
374 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
376 struct dso *dso_l = left->dso;
377 struct dso *dso_r = right->dso;
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;
388 return strcmp(dso_l->name, dso_r->name);
391 static size_t
392 sort__dso_print(FILE *fp, struct hist_entry *self)
394 if (self->dso)
395 return fprintf(fp, "%-25s", self->dso->name);
397 return fprintf(fp, "%016llx ", (u64)self->ip);
400 static struct sort_entry sort_dso = {
401 .header = "Shared Object ",
402 .cmp = sort__dso_cmp,
403 .print = sort__dso_print,
406 /* --sort symbol */
408 static int64_t
409 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
411 u64 ip_l, ip_r;
413 if (left->sym == right->sym)
414 return 0;
416 ip_l = left->sym ? left->sym->start : left->ip;
417 ip_r = right->sym ? right->sym->start : right->ip;
419 return (int64_t)(ip_r - ip_l);
422 static size_t
423 sort__sym_print(FILE *fp, struct hist_entry *self)
425 size_t ret = 0;
427 if (verbose)
428 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
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);
437 return ret;
440 static struct sort_entry sort_sym = {
441 .header = "Symbol",
442 .cmp = sort__sym_cmp,
443 .print = sort__sym_print,
446 static int sort__need_collapse = 0;
448 struct sort_dimension {
449 char *name;
450 struct sort_entry *entry;
451 int taken;
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, },
461 static LIST_HEAD(hist_entry__sort_list);
463 static int sort_dimension__add(char *tok)
465 unsigned int i;
467 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
468 struct sort_dimension *sd = &sort_dimensions[i];
470 if (sd->taken)
471 continue;
473 if (strncasecmp(tok, sd->name, strlen(tok)))
474 continue;
476 if (sd->entry->collapse)
477 sort__need_collapse = 1;
479 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
480 sd->taken = 1;
482 return 0;
485 return -ESRCH;
488 static int64_t
489 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
491 struct sort_entry *se;
492 int64_t cmp = 0;
494 list_for_each_entry(se, &hist_entry__sort_list, list) {
495 cmp = se->cmp(left, right);
496 if (cmp)
497 break;
500 return cmp;
503 static int64_t
504 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
506 struct sort_entry *se;
507 int64_t cmp = 0;
509 list_for_each_entry(se, &hist_entry__sort_list, list) {
510 int64_t (*f)(struct hist_entry *, struct hist_entry *);
512 f = se->collapse ?: se->cmp;
514 cmp = f(left, right);
515 if (cmp)
516 break;
519 return cmp;
523 * collect histogram counts
525 static void hist_hit(struct hist_entry *he, u64 ip)
527 unsigned int sym_size, offset;
528 struct symbol *sym = he->sym;
530 he->count++;
532 if (!sym || !sym->hist)
533 return;
535 sym_size = sym->end - sym->start;
536 offset = ip - sym->start;
538 if (offset >= sym_size)
539 return;
541 sym->hist_sum++;
542 sym->hist[offset]++;
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]);
552 static int
553 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
554 struct symbol *sym, u64 ip, char level)
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,
568 int cmp;
570 while (*p != NULL) {
571 parent = *p;
572 he = rb_entry(parent, struct hist_entry, rb_node);
574 cmp = hist_entry__cmp(&entry, he);
576 if (!cmp) {
577 hist_hit(he, ip);
579 return 0;
582 if (cmp < 0)
583 p = &(*p)->rb_left;
584 else
585 p = &(*p)->rb_right;
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);
595 return 0;
598 static void hist_entry__free(struct hist_entry *he)
600 free(he);
604 * collapse the histogram
607 static struct rb_root collapse_hists;
609 static void collapse__insert_entry(struct hist_entry *he)
611 struct rb_node **p = &collapse_hists.rb_node;
612 struct rb_node *parent = NULL;
613 struct hist_entry *iter;
614 int64_t cmp;
616 while (*p != NULL) {
617 parent = *p;
618 iter = rb_entry(parent, struct hist_entry, rb_node);
620 cmp = hist_entry__collapse(iter, he);
622 if (!cmp) {
623 iter->count += he->count;
624 hist_entry__free(he);
625 return;
628 if (cmp < 0)
629 p = &(*p)->rb_left;
630 else
631 p = &(*p)->rb_right;
634 rb_link_node(&he->rb_node, parent, p);
635 rb_insert_color(&he->rb_node, &collapse_hists);
638 static void collapse__resort(void)
640 struct rb_node *next;
641 struct hist_entry *n;
643 if (!sort__need_collapse)
644 return;
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);
651 rb_erase(&n->rb_node, &hist);
652 collapse__insert_entry(n);
657 * reverse the map, sort on count.
660 static struct rb_root output_hists;
662 static void output__insert_entry(struct hist_entry *he)
664 struct rb_node **p = &output_hists.rb_node;
665 struct rb_node *parent = NULL;
666 struct hist_entry *iter;
668 while (*p != NULL) {
669 parent = *p;
670 iter = rb_entry(parent, struct hist_entry, rb_node);
672 if (he->count > iter->count)
673 p = &(*p)->rb_left;
674 else
675 p = &(*p)->rb_right;
678 rb_link_node(&he->rb_node, parent, p);
679 rb_insert_color(&he->rb_node, &output_hists);
682 static void output__resort(void)
684 struct rb_node *next;
685 struct hist_entry *n;
686 struct rb_root *tree = &hist;
688 if (sort__need_collapse)
689 tree = &collapse_hists;
691 next = rb_first(tree);
693 while (next) {
694 n = rb_entry(next, struct hist_entry, rb_node);
695 next = rb_next(&n->rb_node);
697 rb_erase(&n->rb_node, tree);
698 output__insert_entry(n);
702 static void register_idle_thread(void)
704 struct thread *thread = threads__findnew(0);
706 if (thread == NULL ||
707 thread__set_comm(thread, "[idle]")) {
708 fprintf(stderr, "problem inserting idle task.\n");
709 exit(-1);
713 static unsigned long total = 0,
714 total_mmap = 0,
715 total_comm = 0,
716 total_fork = 0,
717 total_unknown = 0;
719 static int
720 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
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;
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);
736 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
738 if (thread == NULL) {
739 fprintf(stderr, "problem processing %d event, skipping it.\n",
740 event->header.type);
741 return -1;
744 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
745 show = SHOW_KERNEL;
746 level = 'k';
748 dso = kernel_dso;
750 dprintf(" ...... dso: %s\n", dso->name);
752 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
754 show = SHOW_USER;
755 level = '.';
757 map = thread__find_map(thread, ip);
758 if (map != NULL) {
759 ip = map->map_ip(map, ip);
760 dso = map->dso;
761 } else {
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):
768 if ((long long)ip < 0)
769 dso = kernel_dso;
771 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
773 } else {
774 show = SHOW_HV;
775 level = 'H';
776 dprintf(" ...... dso: [hypervisor]\n");
779 if (show & show_mask) {
780 struct symbol *sym = NULL;
782 if (dso)
783 sym = dso->find_symbol(dso, ip);
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;
791 total++;
793 return 0;
796 static int
797 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
799 struct thread *thread = threads__findnew(event->mmap.pid);
800 struct map *map = map__new(&event->mmap);
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);
811 if (thread == NULL || map == NULL) {
812 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
813 return 0;
816 thread__insert_map(thread, map);
817 total_mmap++;
819 return 0;
822 static int
823 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
825 struct thread *thread = threads__findnew(event->comm.pid);
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);
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;
837 total_comm++;
839 return 0;
842 static int
843 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
845 struct thread *thread = threads__findnew(event->fork.pid);
846 struct thread *parent = threads__findnew(event->fork.ppid);
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);
853 if (!thread || !parent || thread__fork(thread, parent)) {
854 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
855 return -1;
857 total_fork++;
859 return 0;
862 static int
863 process_event(event_t *event, unsigned long offset, unsigned long head)
865 switch (event->header.type) {
866 case PERF_EVENT_SAMPLE:
867 return process_sample_event(event, offset, head);
869 case PERF_EVENT_MMAP:
870 return process_mmap_event(event, offset, head);
872 case PERF_EVENT_COMM:
873 return process_comm_event(event, offset, head);
875 case PERF_EVENT_FORK:
876 return process_fork_event(event, offset, head);
878 * We dont process them right now but they are fine:
881 case PERF_EVENT_THROTTLE:
882 case PERF_EVENT_UNTHROTTLE:
883 return 0;
885 default:
886 return -1;
889 return 0;
892 static int
893 parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
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;
904 if (getline(&line, &line_len, file) < 0)
905 return -1;
906 if (!line)
907 return -1;
909 c = strchr(line, '\n');
910 if (c)
911 *c = 0;
913 line_ip = -1;
914 offset = 0;
915 ret = -2;
918 * Strip leading spaces:
920 tmp = line;
921 while (*tmp) {
922 if (*tmp != ' ')
923 break;
924 tmp++;
927 if (*tmp) {
929 * Parse hexa addresses followed by ':'
931 line_ip = strtoull(tmp, &tmp2, 16);
932 if (*tmp2 != ':')
933 line_ip = -1;
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;
943 offset = line_ip - start;
944 if (offset < len)
945 hits = sym->hist[offset];
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;
953 color = get_percent_color(percent);
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
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;
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);
979 return 0;
982 static struct rb_root root_sym_ext;
984 static void insert_source_line(struct sym_ext *sym_ext)
986 struct sym_ext *iter;
987 struct rb_node **p = &root_sym_ext.rb_node;
988 struct rb_node *parent = NULL;
990 while (*p != NULL) {
991 parent = *p;
992 iter = rb_entry(parent, struct sym_ext, node);
994 if (sym_ext->percent > iter->percent)
995 p = &(*p)->rb_left;
996 else
997 p = &(*p)->rb_right;
1000 rb_link_node(&sym_ext->node, parent, p);
1001 rb_insert_color(&sym_ext->node, &root_sym_ext);
1004 static void free_source_line(struct symbol *sym, int len)
1006 struct sym_ext *sym_ext = sym->priv;
1007 int i;
1009 if (!sym_ext)
1010 return;
1012 for (i = 0; i < len; i++)
1013 free(sym_ext[i].path);
1014 free(sym_ext);
1016 sym->priv = NULL;
1017 root_sym_ext = RB_ROOT;
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)
1024 int i;
1025 char cmd[PATH_MAX * 2];
1026 struct sym_ext *sym_ext;
1028 if (!sym->hist_sum)
1029 return;
1031 sym->priv = calloc(len, sizeof(struct sym_ext));
1032 if (!sym->priv)
1033 return;
1035 sym_ext = sym->priv;
1037 for (i = 0; i < len; i++) {
1038 char *path = NULL;
1039 size_t line_len;
1040 u64 offset;
1041 FILE *fp;
1043 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
1044 if (sym_ext[i].percent <= 0.5)
1045 continue;
1047 offset = start + i;
1048 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
1049 fp = popen(cmd, "r");
1050 if (!fp)
1051 continue;
1053 if (getline(&path, &line_len, fp) < 0 || !line_len)
1054 goto next;
1056 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
1057 if (!sym_ext[i].path)
1058 goto next;
1060 strcpy(sym_ext[i].path, path);
1061 insert_source_line(&sym_ext[i]);
1063 next:
1064 pclose(fp);
1068 static void print_summary(char *filename)
1070 struct sym_ext *sym_ext;
1071 struct rb_node *node;
1073 printf("\nSorted summary for file %s\n", filename);
1074 printf("----------------------------------------------\n\n");
1076 if (RB_EMPTY_ROOT(&root_sym_ext)) {
1077 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1078 return;
1081 node = rb_first(&root_sym_ext);
1082 while (node) {
1083 double percent;
1084 char *color;
1085 char *path;
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;
1092 color_fprintf(stdout, color, " %7.2f %s", percent, path);
1093 node = rb_next(node);
1097 static void annotate_sym(struct dso *dso, struct symbol *sym)
1099 char *filename = dso->name, *d_filename;
1100 u64 start, end, len;
1101 char command[PATH_MAX*2];
1102 FILE *file;
1104 if (!filename)
1105 return;
1106 if (sym->module)
1107 filename = sym->module->path;
1108 else if (dso == kernel_dso)
1109 filename = vmlinux;
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);
1119 end = start + sym->end - sym->start + 1;
1120 len = sym->end - sym->start;
1122 if (print_line) {
1123 get_source_line(sym, start, len, filename);
1124 print_summary(filename);
1127 printf("\n\n------------------------------------------------\n");
1128 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
1129 printf("------------------------------------------------\n");
1131 if (verbose >= 2)
1132 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
1134 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
1135 (u64)start, (u64)end, filename, filename);
1137 if (verbose >= 3)
1138 printf("doing: %s\n", command);
1140 file = popen(command, "r");
1141 if (!file)
1142 return;
1144 while (!feof(file)) {
1145 if (parse_line(file, sym, start, len) < 0)
1146 break;
1149 pclose(file);
1150 if (print_line)
1151 free_source_line(sym, len);
1154 static void find_annotations(void)
1156 struct rb_node *nd;
1157 struct dso *dso;
1158 int count = 0;
1160 list_for_each_entry(dso, &dsos, node) {
1162 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
1163 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
1165 if (sym->hist) {
1166 annotate_sym(dso, sym);
1167 count++;
1172 if (!count)
1173 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
1176 static int __cmd_annotate(void)
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;
1186 register_idle_thread();
1188 input = open(input_name, O_RDONLY);
1189 if (input < 0) {
1190 perror("failed to open file");
1191 exit(-1);
1194 ret = fstat(input, &stat);
1195 if (ret < 0) {
1196 perror("failed to stat file");
1197 exit(-1);
1200 if (!stat.st_size) {
1201 fprintf(stderr, "zero-sized file, nothing to do!\n");
1202 exit(0);
1205 if (load_kernel() < 0) {
1206 perror("failed to load kernel symbols");
1207 return EXIT_FAILURE;
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);
1218 more:
1219 event = (event_t *)(buf + head);
1221 size = event->header.size;
1222 if (!size)
1223 size = 8;
1225 if (head + event->header.size >= page_size * mmap_window) {
1226 unsigned long shift = page_size * (head / page_size);
1227 int ret;
1229 ret = munmap(buf, page_size * mmap_window);
1230 assert(ret == 0);
1232 offset += shift;
1233 head -= shift;
1234 goto remap;
1237 size = event->header.size;
1239 dprintf("%p [%p]: event: %d\n",
1240 (void *)(offset + head),
1241 (void *)(long)event->header.size,
1242 event->header.type);
1244 if (!size || process_event(event, offset, head) < 0) {
1246 dprintf("%p [%p]: skipping unknown header type: %d\n",
1247 (void *)(offset + head),
1248 (void *)(long)(event->header.size),
1249 event->header.type);
1251 total_unknown++;
1254 * assume we lost track of the stream, check alignment, and
1255 * increment a single u64 in the hope to catch on again 'soon'.
1258 if (unlikely(head & 7))
1259 head &= ~7ULL;
1261 size = 8;
1264 head += size;
1266 if (offset + head < (unsigned long)stat.st_size)
1267 goto more;
1269 rc = EXIT_SUCCESS;
1270 close(input);
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);
1278 if (dump_trace)
1279 return 0;
1281 if (verbose >= 3)
1282 threads__fprintf(stdout);
1284 if (verbose >= 2)
1285 dsos__fprintf(stdout);
1287 collapse__resort();
1288 output__resort();
1290 find_annotations();
1292 return rc;
1295 static const char * const annotate_usage[] = {
1296 "perf annotate [<options>] <command>",
1297 NULL
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()
1319 static void setup_sorting(void)
1321 char *tmp, *tok, *str = strdup(sort_order);
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);
1331 free(str);
1334 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1336 symbol__init();
1338 page_size = getpagesize();
1340 argc = parse_options(argc, argv, options, annotate_usage, 0);
1342 setup_sorting();
1344 if (argc) {
1346 * Special case: if there's an argument left then assume tha
1347 * it's a symbol filter:
1349 if (argc > 1)
1350 usage_with_options(annotate_usage, options);
1352 sym_hist_filter = argv[0];
1355 if (!sym_hist_filter)
1356 usage_with_options(annotate_usage, options);
1358 setup_pager();
1360 return __cmd_annotate();