perf tools: Factorize high level dso helpers
[safe/jmp/linux-2.6] / tools / perf / builtin-report.c
1 /*
2  * builtin-report.c
3  *
4  * Builtin report 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 #include "util/callchain.h"
19 #include "util/strlist.h"
20 #include "util/values.h"
21
22 #include "perf.h"
23 #include "util/header.h"
24
25 #include "util/parse-options.h"
26 #include "util/parse-events.h"
27
28 #define SHOW_KERNEL     1
29 #define SHOW_USER       2
30 #define SHOW_HV         4
31
32 static char             const *input_name = "perf.data";
33
34 static char             default_sort_order[] = "comm,dso,symbol";
35 static char             *sort_order = default_sort_order;
36 static char             *dso_list_str, *comm_list_str, *sym_list_str,
37                         *col_width_list_str;
38 static struct strlist   *dso_list, *comm_list, *sym_list;
39 static char             *field_sep;
40
41 static int              input;
42 static int              show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
43
44 static int              dump_trace = 0;
45 #define dprintf(x...)   do { if (dump_trace) printf(x); } while (0)
46 #define cdprintf(x...)  do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
47
48 static int              full_paths;
49 static int              show_nr_samples;
50
51 static int              show_threads;
52 static struct perf_read_values  show_threads_values;
53
54 static char             default_pretty_printing_style[] = "normal";
55 static char             *pretty_printing_style = default_pretty_printing_style;
56
57 static unsigned long    page_size;
58 static unsigned long    mmap_window = 32;
59
60 static char             default_parent_pattern[] = "^sys_|^do_page_fault";
61 static char             *parent_pattern = default_parent_pattern;
62 static regex_t          parent_regex;
63
64 static int              exclude_other = 1;
65
66 static char             callchain_default_opt[] = "fractal,0.5";
67
68 static int              callchain;
69
70 static
71 struct callchain_param  callchain_param = {
72         .mode   = CHAIN_GRAPH_REL,
73         .min_percent = 0.5
74 };
75
76 static u64              sample_type;
77
78 struct ip_event {
79         struct perf_event_header header;
80         u64 ip;
81         u32 pid, tid;
82         unsigned char __more_data[];
83 };
84
85 struct mmap_event {
86         struct perf_event_header header;
87         u32 pid, tid;
88         u64 start;
89         u64 len;
90         u64 pgoff;
91         char filename[PATH_MAX];
92 };
93
94 struct comm_event {
95         struct perf_event_header header;
96         u32 pid, tid;
97         char comm[16];
98 };
99
100 struct fork_event {
101         struct perf_event_header header;
102         u32 pid, ppid;
103         u32 tid, ptid;
104 };
105
106 struct lost_event {
107         struct perf_event_header header;
108         u64 id;
109         u64 lost;
110 };
111
112 struct read_event {
113         struct perf_event_header header;
114         u32 pid,tid;
115         u64 value;
116         u64 time_enabled;
117         u64 time_running;
118         u64 id;
119 };
120
121 typedef union event_union {
122         struct perf_event_header        header;
123         struct ip_event                 ip;
124         struct mmap_event               mmap;
125         struct comm_event               comm;
126         struct fork_event               fork;
127         struct lost_event               lost;
128         struct read_event               read;
129 } event_t;
130
131 static int repsep_fprintf(FILE *fp, const char *fmt, ...)
132 {
133         int n;
134         va_list ap;
135
136         va_start(ap, fmt);
137         if (!field_sep)
138                 n = vfprintf(fp, fmt, ap);
139         else {
140                 char *bf = NULL;
141                 n = vasprintf(&bf, fmt, ap);
142                 if (n > 0) {
143                         char *sep = bf;
144                         while (1) {
145                                 sep = strchr(sep, *field_sep);
146                                 if (sep == NULL)
147                                         break;
148                                 *sep = '.';
149                         }
150                 }
151                 fputs(bf, fp);
152                 free(bf);
153         }
154         va_end(ap);
155         return n;
156 }
157
158
159
160 static char __cwd[PATH_MAX];
161 static char *cwd = __cwd;
162 static int cwdlen;
163
164 static int strcommon(const char *pathname)
165 {
166         int n = 0;
167
168         while (n < cwdlen && pathname[n] == cwd[n])
169                 ++n;
170
171         return n;
172 }
173
174 struct map {
175         struct list_head node;
176         u64      start;
177         u64      end;
178         u64      pgoff;
179         u64      (*map_ip)(struct map *, u64);
180         struct dso       *dso;
181 };
182
183 static u64 map__map_ip(struct map *map, u64 ip)
184 {
185         return ip - map->start + map->pgoff;
186 }
187
188 static u64 vdso__map_ip(struct map *map __used, u64 ip)
189 {
190         return ip;
191 }
192
193 static inline int is_anon_memory(const char *filename)
194 {
195         return strcmp(filename, "//anon") == 0;
196 }
197
198 static struct map *map__new(struct mmap_event *event)
199 {
200         struct map *self = malloc(sizeof(*self));
201
202         if (self != NULL) {
203                 const char *filename = event->filename;
204                 char newfilename[PATH_MAX];
205                 int anon;
206
207                 if (cwd) {
208                         int n = strcommon(filename);
209
210                         if (n == cwdlen) {
211                                 snprintf(newfilename, sizeof(newfilename),
212                                          ".%s", filename + n);
213                                 filename = newfilename;
214                         }
215                 }
216
217                 anon = is_anon_memory(filename);
218
219                 if (anon) {
220                         snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
221                         filename = newfilename;
222                 }
223
224                 self->start = event->start;
225                 self->end   = event->start + event->len;
226                 self->pgoff = event->pgoff;
227
228                 self->dso = dsos__findnew(filename);
229                 if (self->dso == NULL)
230                         goto out_delete;
231
232                 if (self->dso == vdso || anon)
233                         self->map_ip = vdso__map_ip;
234                 else
235                         self->map_ip = map__map_ip;
236         }
237         return self;
238 out_delete:
239         free(self);
240         return NULL;
241 }
242
243 static struct map *map__clone(struct map *self)
244 {
245         struct map *map = malloc(sizeof(*self));
246
247         if (!map)
248                 return NULL;
249
250         memcpy(map, self, sizeof(*self));
251
252         return map;
253 }
254
255 static int map__overlap(struct map *l, struct map *r)
256 {
257         if (l->start > r->start) {
258                 struct map *t = l;
259                 l = r;
260                 r = t;
261         }
262
263         if (l->end > r->start)
264                 return 1;
265
266         return 0;
267 }
268
269 static size_t map__fprintf(struct map *self, FILE *fp)
270 {
271         return fprintf(fp, " %Lx-%Lx %Lx %s\n",
272                        self->start, self->end, self->pgoff, self->dso->name);
273 }
274
275
276 struct thread {
277         struct rb_node   rb_node;
278         struct list_head maps;
279         pid_t            pid;
280         char             *comm;
281 };
282
283 static struct thread *thread__new(pid_t pid)
284 {
285         struct thread *self = malloc(sizeof(*self));
286
287         if (self != NULL) {
288                 self->pid = pid;
289                 self->comm = malloc(32);
290                 if (self->comm)
291                         snprintf(self->comm, 32, ":%d", self->pid);
292                 INIT_LIST_HEAD(&self->maps);
293         }
294
295         return self;
296 }
297
298 static unsigned int dsos__col_width,
299                     comms__col_width,
300                     threads__col_width;
301
302 static int thread__set_comm(struct thread *self, const char *comm)
303 {
304         if (self->comm)
305                 free(self->comm);
306         self->comm = strdup(comm);
307         if (!self->comm)
308                 return -ENOMEM;
309
310         if (!col_width_list_str && !field_sep &&
311             (!comm_list || strlist__has_entry(comm_list, comm))) {
312                 unsigned int slen = strlen(comm);
313                 if (slen > comms__col_width) {
314                         comms__col_width = slen;
315                         threads__col_width = slen + 6;
316                 }
317         }
318
319         return 0;
320 }
321
322 static size_t thread__fprintf(struct thread *self, FILE *fp)
323 {
324         struct map *pos;
325         size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
326
327         list_for_each_entry(pos, &self->maps, node)
328                 ret += map__fprintf(pos, fp);
329
330         return ret;
331 }
332
333
334 static struct rb_root threads;
335 static struct thread *last_match;
336
337 static struct thread *threads__findnew(pid_t pid)
338 {
339         struct rb_node **p = &threads.rb_node;
340         struct rb_node *parent = NULL;
341         struct thread *th;
342
343         /*
344          * Font-end cache - PID lookups come in blocks,
345          * so most of the time we dont have to look up
346          * the full rbtree:
347          */
348         if (last_match && last_match->pid == pid)
349                 return last_match;
350
351         while (*p != NULL) {
352                 parent = *p;
353                 th = rb_entry(parent, struct thread, rb_node);
354
355                 if (th->pid == pid) {
356                         last_match = th;
357                         return th;
358                 }
359
360                 if (pid < th->pid)
361                         p = &(*p)->rb_left;
362                 else
363                         p = &(*p)->rb_right;
364         }
365
366         th = thread__new(pid);
367         if (th != NULL) {
368                 rb_link_node(&th->rb_node, parent, p);
369                 rb_insert_color(&th->rb_node, &threads);
370                 last_match = th;
371         }
372
373         return th;
374 }
375
376 static void thread__insert_map(struct thread *self, struct map *map)
377 {
378         struct map *pos, *tmp;
379
380         list_for_each_entry_safe(pos, tmp, &self->maps, node) {
381                 if (map__overlap(pos, map)) {
382                         if (verbose >= 2) {
383                                 printf("overlapping maps:\n");
384                                 map__fprintf(map, stdout);
385                                 map__fprintf(pos, stdout);
386                         }
387
388                         if (map->start <= pos->start && map->end > pos->start)
389                                 pos->start = map->end;
390
391                         if (map->end >= pos->end && map->start < pos->end)
392                                 pos->end = map->start;
393
394                         if (verbose >= 2) {
395                                 printf("after collision:\n");
396                                 map__fprintf(pos, stdout);
397                         }
398
399                         if (pos->start >= pos->end) {
400                                 list_del_init(&pos->node);
401                                 free(pos);
402                         }
403                 }
404         }
405
406         list_add_tail(&map->node, &self->maps);
407 }
408
409 static int thread__fork(struct thread *self, struct thread *parent)
410 {
411         struct map *map;
412
413         if (self->comm)
414                 free(self->comm);
415         self->comm = strdup(parent->comm);
416         if (!self->comm)
417                 return -ENOMEM;
418
419         list_for_each_entry(map, &parent->maps, node) {
420                 struct map *new = map__clone(map);
421                 if (!new)
422                         return -ENOMEM;
423                 thread__insert_map(self, new);
424         }
425
426         return 0;
427 }
428
429 static struct map *thread__find_map(struct thread *self, u64 ip)
430 {
431         struct map *pos;
432
433         if (self == NULL)
434                 return NULL;
435
436         list_for_each_entry(pos, &self->maps, node)
437                 if (ip >= pos->start && ip <= pos->end)
438                         return pos;
439
440         return NULL;
441 }
442
443 static size_t threads__fprintf(FILE *fp)
444 {
445         size_t ret = 0;
446         struct rb_node *nd;
447
448         for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
449                 struct thread *pos = rb_entry(nd, struct thread, rb_node);
450
451                 ret += thread__fprintf(pos, fp);
452         }
453
454         return ret;
455 }
456
457 /*
458  * histogram, sorted on item, collects counts
459  */
460
461 static struct rb_root hist;
462
463 struct hist_entry {
464         struct rb_node          rb_node;
465
466         struct thread           *thread;
467         struct map              *map;
468         struct dso              *dso;
469         struct symbol           *sym;
470         struct symbol           *parent;
471         u64                     ip;
472         char                    level;
473         struct callchain_node   callchain;
474         struct rb_root          sorted_chain;
475
476         u64                     count;
477 };
478
479 /*
480  * configurable sorting bits
481  */
482
483 struct sort_entry {
484         struct list_head list;
485
486         char *header;
487
488         int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
489         int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
490         size_t  (*print)(FILE *fp, struct hist_entry *, unsigned int width);
491         unsigned int *width;
492         bool    elide;
493 };
494
495 static int64_t cmp_null(void *l, void *r)
496 {
497         if (!l && !r)
498                 return 0;
499         else if (!l)
500                 return -1;
501         else
502                 return 1;
503 }
504
505 /* --sort pid */
506
507 static int64_t
508 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
509 {
510         return right->thread->pid - left->thread->pid;
511 }
512
513 static size_t
514 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
515 {
516         return repsep_fprintf(fp, "%*s:%5d", width - 6,
517                               self->thread->comm ?: "", self->thread->pid);
518 }
519
520 static struct sort_entry sort_thread = {
521         .header = "Command:  Pid",
522         .cmp    = sort__thread_cmp,
523         .print  = sort__thread_print,
524         .width  = &threads__col_width,
525 };
526
527 /* --sort comm */
528
529 static int64_t
530 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
531 {
532         return right->thread->pid - left->thread->pid;
533 }
534
535 static int64_t
536 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
537 {
538         char *comm_l = left->thread->comm;
539         char *comm_r = right->thread->comm;
540
541         if (!comm_l || !comm_r)
542                 return cmp_null(comm_l, comm_r);
543
544         return strcmp(comm_l, comm_r);
545 }
546
547 static size_t
548 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
549 {
550         return repsep_fprintf(fp, "%*s", width, self->thread->comm);
551 }
552
553 static struct sort_entry sort_comm = {
554         .header         = "Command",
555         .cmp            = sort__comm_cmp,
556         .collapse       = sort__comm_collapse,
557         .print          = sort__comm_print,
558         .width          = &comms__col_width,
559 };
560
561 /* --sort dso */
562
563 static int64_t
564 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
565 {
566         struct dso *dso_l = left->dso;
567         struct dso *dso_r = right->dso;
568
569         if (!dso_l || !dso_r)
570                 return cmp_null(dso_l, dso_r);
571
572         return strcmp(dso_l->name, dso_r->name);
573 }
574
575 static size_t
576 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
577 {
578         if (self->dso)
579                 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
580
581         return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
582 }
583
584 static struct sort_entry sort_dso = {
585         .header = "Shared Object",
586         .cmp    = sort__dso_cmp,
587         .print  = sort__dso_print,
588         .width  = &dsos__col_width,
589 };
590
591 /* --sort symbol */
592
593 static int64_t
594 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
595 {
596         u64 ip_l, ip_r;
597
598         if (left->sym == right->sym)
599                 return 0;
600
601         ip_l = left->sym ? left->sym->start : left->ip;
602         ip_r = right->sym ? right->sym->start : right->ip;
603
604         return (int64_t)(ip_r - ip_l);
605 }
606
607 static size_t
608 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
609 {
610         size_t ret = 0;
611
612         if (verbose)
613                 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
614                                       dso__symtab_origin(self->dso));
615
616         ret += repsep_fprintf(fp, "[%c] ", self->level);
617         if (self->sym) {
618                 ret += repsep_fprintf(fp, "%s", self->sym->name);
619
620                 if (self->sym->module)
621                         ret += repsep_fprintf(fp, "\t[%s]",
622                                              self->sym->module->name);
623         } else {
624                 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
625         }
626
627         return ret;
628 }
629
630 static struct sort_entry sort_sym = {
631         .header = "Symbol",
632         .cmp    = sort__sym_cmp,
633         .print  = sort__sym_print,
634 };
635
636 /* --sort parent */
637
638 static int64_t
639 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
640 {
641         struct symbol *sym_l = left->parent;
642         struct symbol *sym_r = right->parent;
643
644         if (!sym_l || !sym_r)
645                 return cmp_null(sym_l, sym_r);
646
647         return strcmp(sym_l->name, sym_r->name);
648 }
649
650 static size_t
651 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
652 {
653         return repsep_fprintf(fp, "%-*s", width,
654                               self->parent ? self->parent->name : "[other]");
655 }
656
657 static unsigned int parent_symbol__col_width;
658
659 static struct sort_entry sort_parent = {
660         .header = "Parent symbol",
661         .cmp    = sort__parent_cmp,
662         .print  = sort__parent_print,
663         .width  = &parent_symbol__col_width,
664 };
665
666 static int sort__need_collapse = 0;
667 static int sort__has_parent = 0;
668
669 struct sort_dimension {
670         char                    *name;
671         struct sort_entry       *entry;
672         int                     taken;
673 };
674
675 static struct sort_dimension sort_dimensions[] = {
676         { .name = "pid",        .entry = &sort_thread,  },
677         { .name = "comm",       .entry = &sort_comm,    },
678         { .name = "dso",        .entry = &sort_dso,     },
679         { .name = "symbol",     .entry = &sort_sym,     },
680         { .name = "parent",     .entry = &sort_parent,  },
681 };
682
683 static LIST_HEAD(hist_entry__sort_list);
684
685 static int sort_dimension__add(char *tok)
686 {
687         unsigned int i;
688
689         for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
690                 struct sort_dimension *sd = &sort_dimensions[i];
691
692                 if (sd->taken)
693                         continue;
694
695                 if (strncasecmp(tok, sd->name, strlen(tok)))
696                         continue;
697
698                 if (sd->entry->collapse)
699                         sort__need_collapse = 1;
700
701                 if (sd->entry == &sort_parent) {
702                         int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
703                         if (ret) {
704                                 char err[BUFSIZ];
705
706                                 regerror(ret, &parent_regex, err, sizeof(err));
707                                 fprintf(stderr, "Invalid regex: %s\n%s",
708                                         parent_pattern, err);
709                                 exit(-1);
710                         }
711                         sort__has_parent = 1;
712                 }
713
714                 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
715                 sd->taken = 1;
716
717                 return 0;
718         }
719
720         return -ESRCH;
721 }
722
723 static int64_t
724 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
725 {
726         struct sort_entry *se;
727         int64_t cmp = 0;
728
729         list_for_each_entry(se, &hist_entry__sort_list, list) {
730                 cmp = se->cmp(left, right);
731                 if (cmp)
732                         break;
733         }
734
735         return cmp;
736 }
737
738 static int64_t
739 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
740 {
741         struct sort_entry *se;
742         int64_t cmp = 0;
743
744         list_for_each_entry(se, &hist_entry__sort_list, list) {
745                 int64_t (*f)(struct hist_entry *, struct hist_entry *);
746
747                 f = se->collapse ?: se->cmp;
748
749                 cmp = f(left, right);
750                 if (cmp)
751                         break;
752         }
753
754         return cmp;
755 }
756
757 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
758 {
759         int i;
760         size_t ret = 0;
761
762         ret += fprintf(fp, "%s", "                ");
763
764         for (i = 0; i < depth; i++)
765                 if (depth_mask & (1 << i))
766                         ret += fprintf(fp, "|          ");
767                 else
768                         ret += fprintf(fp, "           ");
769
770         ret += fprintf(fp, "\n");
771
772         return ret;
773 }
774 static size_t
775 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
776                        int depth_mask, int count, u64 total_samples,
777                        int hits)
778 {
779         int i;
780         size_t ret = 0;
781
782         ret += fprintf(fp, "%s", "                ");
783         for (i = 0; i < depth; i++) {
784                 if (depth_mask & (1 << i))
785                         ret += fprintf(fp, "|");
786                 else
787                         ret += fprintf(fp, " ");
788                 if (!count && i == depth - 1) {
789                         double percent;
790
791                         percent = hits * 100.0 / total_samples;
792                         ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
793                 } else
794                         ret += fprintf(fp, "%s", "          ");
795         }
796         if (chain->sym)
797                 ret += fprintf(fp, "%s\n", chain->sym->name);
798         else
799                 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
800
801         return ret;
802 }
803
804 static struct symbol *rem_sq_bracket;
805 static struct callchain_list rem_hits;
806
807 static void init_rem_hits(void)
808 {
809         rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
810         if (!rem_sq_bracket) {
811                 fprintf(stderr, "Not enough memory to display remaining hits\n");
812                 return;
813         }
814
815         strcpy(rem_sq_bracket->name, "[...]");
816         rem_hits.sym = rem_sq_bracket;
817 }
818
819 static size_t
820 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
821                         u64 total_samples, int depth, int depth_mask)
822 {
823         struct rb_node *node, *next;
824         struct callchain_node *child;
825         struct callchain_list *chain;
826         int new_depth_mask = depth_mask;
827         u64 new_total;
828         u64 remaining;
829         size_t ret = 0;
830         int i;
831
832         if (callchain_param.mode == CHAIN_GRAPH_REL)
833                 new_total = self->children_hit;
834         else
835                 new_total = total_samples;
836
837         remaining = new_total;
838
839         node = rb_first(&self->rb_root);
840         while (node) {
841                 u64 cumul;
842
843                 child = rb_entry(node, struct callchain_node, rb_node);
844                 cumul = cumul_hits(child);
845                 remaining -= cumul;
846
847                 /*
848                  * The depth mask manages the output of pipes that show
849                  * the depth. We don't want to keep the pipes of the current
850                  * level for the last child of this depth.
851                  * Except if we have remaining filtered hits. They will
852                  * supersede the last child
853                  */
854                 next = rb_next(node);
855                 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
856                         new_depth_mask &= ~(1 << (depth - 1));
857
858                 /*
859                  * But we keep the older depth mask for the line seperator
860                  * to keep the level link until we reach the last child
861                  */
862                 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
863                 i = 0;
864                 list_for_each_entry(chain, &child->val, list) {
865                         if (chain->ip >= PERF_CONTEXT_MAX)
866                                 continue;
867                         ret += ipchain__fprintf_graph(fp, chain, depth,
868                                                       new_depth_mask, i++,
869                                                       new_total,
870                                                       cumul);
871                 }
872                 ret += callchain__fprintf_graph(fp, child, new_total,
873                                                 depth + 1,
874                                                 new_depth_mask | (1 << depth));
875                 node = next;
876         }
877
878         if (callchain_param.mode == CHAIN_GRAPH_REL &&
879                 remaining && remaining != new_total) {
880
881                 if (!rem_sq_bracket)
882                         return ret;
883
884                 new_depth_mask &= ~(1 << (depth - 1));
885
886                 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
887                                               new_depth_mask, 0, new_total,
888                                               remaining);
889         }
890
891         return ret;
892 }
893
894 static size_t
895 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
896                         u64 total_samples)
897 {
898         struct callchain_list *chain;
899         size_t ret = 0;
900
901         if (!self)
902                 return 0;
903
904         ret += callchain__fprintf_flat(fp, self->parent, total_samples);
905
906
907         list_for_each_entry(chain, &self->val, list) {
908                 if (chain->ip >= PERF_CONTEXT_MAX)
909                         continue;
910                 if (chain->sym)
911                         ret += fprintf(fp, "                %s\n", chain->sym->name);
912                 else
913                         ret += fprintf(fp, "                %p\n",
914                                         (void *)(long)chain->ip);
915         }
916
917         return ret;
918 }
919
920 static size_t
921 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
922                               u64 total_samples)
923 {
924         struct rb_node *rb_node;
925         struct callchain_node *chain;
926         size_t ret = 0;
927
928         rb_node = rb_first(&self->sorted_chain);
929         while (rb_node) {
930                 double percent;
931
932                 chain = rb_entry(rb_node, struct callchain_node, rb_node);
933                 percent = chain->hit * 100.0 / total_samples;
934                 switch (callchain_param.mode) {
935                 case CHAIN_FLAT:
936                         ret += percent_color_fprintf(fp, "           %6.2f%%\n",
937                                                      percent);
938                         ret += callchain__fprintf_flat(fp, chain, total_samples);
939                         break;
940                 case CHAIN_GRAPH_ABS: /* Falldown */
941                 case CHAIN_GRAPH_REL:
942                         ret += callchain__fprintf_graph(fp, chain,
943                                                         total_samples, 1, 1);
944                 default:
945                         break;
946                 }
947                 ret += fprintf(fp, "\n");
948                 rb_node = rb_next(rb_node);
949         }
950
951         return ret;
952 }
953
954
955 static size_t
956 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
957 {
958         struct sort_entry *se;
959         size_t ret;
960
961         if (exclude_other && !self->parent)
962                 return 0;
963
964         if (total_samples)
965                 ret = percent_color_fprintf(fp,
966                                             field_sep ? "%.2f" : "   %6.2f%%",
967                                         (self->count * 100.0) / total_samples);
968         else
969                 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
970
971         if (show_nr_samples) {
972                 if (field_sep)
973                         fprintf(fp, "%c%lld", *field_sep, self->count);
974                 else
975                         fprintf(fp, "%11lld", self->count);
976         }
977
978         list_for_each_entry(se, &hist_entry__sort_list, list) {
979                 if (se->elide)
980                         continue;
981
982                 fprintf(fp, "%s", field_sep ?: "  ");
983                 ret += se->print(fp, self, se->width ? *se->width : 0);
984         }
985
986         ret += fprintf(fp, "\n");
987
988         if (callchain)
989                 hist_entry_callchain__fprintf(fp, self, total_samples);
990
991         return ret;
992 }
993
994 /*
995  *
996  */
997
998 static void dso__calc_col_width(struct dso *self)
999 {
1000         if (!col_width_list_str && !field_sep &&
1001             (!dso_list || strlist__has_entry(dso_list, self->name))) {
1002                 unsigned int slen = strlen(self->name);
1003                 if (slen > dsos__col_width)
1004                         dsos__col_width = slen;
1005         }
1006
1007         self->slen_calculated = 1;
1008 }
1009
1010 static struct symbol *
1011 resolve_symbol(struct thread *thread, struct map **mapp,
1012                struct dso **dsop, u64 *ipp)
1013 {
1014         struct dso *dso = dsop ? *dsop : NULL;
1015         struct map *map = mapp ? *mapp : NULL;
1016         u64 ip = *ipp;
1017
1018         if (!thread)
1019                 return NULL;
1020
1021         if (dso)
1022                 goto got_dso;
1023
1024         if (map)
1025                 goto got_map;
1026
1027         map = thread__find_map(thread, ip);
1028         if (map != NULL) {
1029                 /*
1030                  * We have to do this here as we may have a dso
1031                  * with no symbol hit that has a name longer than
1032                  * the ones with symbols sampled.
1033                  */
1034                 if (!sort_dso.elide && !map->dso->slen_calculated)
1035                         dso__calc_col_width(map->dso);
1036
1037                 if (mapp)
1038                         *mapp = map;
1039 got_map:
1040                 ip = map->map_ip(map, ip);
1041
1042                 dso = map->dso;
1043         } else {
1044                 /*
1045                  * If this is outside of all known maps,
1046                  * and is a negative address, try to look it
1047                  * up in the kernel dso, as it might be a
1048                  * vsyscall (which executes in user-mode):
1049                  */
1050                 if ((long long)ip < 0)
1051                 dso = kernel_dso;
1052         }
1053         dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1054         dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1055         *ipp  = ip;
1056
1057         if (dsop)
1058                 *dsop = dso;
1059
1060         if (!dso)
1061                 return NULL;
1062 got_dso:
1063         return dso->find_symbol(dso, ip);
1064 }
1065
1066 static int call__match(struct symbol *sym)
1067 {
1068         if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1069                 return 1;
1070
1071         return 0;
1072 }
1073
1074 static struct symbol **
1075 resolve_callchain(struct thread *thread, struct map *map __used,
1076                     struct ip_callchain *chain, struct hist_entry *entry)
1077 {
1078         u64 context = PERF_CONTEXT_MAX;
1079         struct symbol **syms = NULL;
1080         unsigned int i;
1081
1082         if (callchain) {
1083                 syms = calloc(chain->nr, sizeof(*syms));
1084                 if (!syms) {
1085                         fprintf(stderr, "Can't allocate memory for symbols\n");
1086                         exit(-1);
1087                 }
1088         }
1089
1090         for (i = 0; i < chain->nr; i++) {
1091                 u64 ip = chain->ips[i];
1092                 struct dso *dso = NULL;
1093                 struct symbol *sym;
1094
1095                 if (ip >= PERF_CONTEXT_MAX) {
1096                         context = ip;
1097                         continue;
1098                 }
1099
1100                 switch (context) {
1101                 case PERF_CONTEXT_HV:
1102                         dso = hypervisor_dso;
1103                         break;
1104                 case PERF_CONTEXT_KERNEL:
1105                         dso = kernel_dso;
1106                         break;
1107                 default:
1108                         break;
1109                 }
1110
1111                 sym = resolve_symbol(thread, NULL, &dso, &ip);
1112
1113                 if (sym) {
1114                         if (sort__has_parent && call__match(sym) &&
1115                             !entry->parent)
1116                                 entry->parent = sym;
1117                         if (!callchain)
1118                                 break;
1119                         syms[i] = sym;
1120                 }
1121         }
1122
1123         return syms;
1124 }
1125
1126 /*
1127  * collect histogram counts
1128  */
1129
1130 static int
1131 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
1132                 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1133                 char level, u64 count)
1134 {
1135         struct rb_node **p = &hist.rb_node;
1136         struct rb_node *parent = NULL;
1137         struct hist_entry *he;
1138         struct symbol **syms = NULL;
1139         struct hist_entry entry = {
1140                 .thread = thread,
1141                 .map    = map,
1142                 .dso    = dso,
1143                 .sym    = sym,
1144                 .ip     = ip,
1145                 .level  = level,
1146                 .count  = count,
1147                 .parent = NULL,
1148                 .sorted_chain = RB_ROOT
1149         };
1150         int cmp;
1151
1152         if ((sort__has_parent || callchain) && chain)
1153                 syms = resolve_callchain(thread, map, chain, &entry);
1154
1155         while (*p != NULL) {
1156                 parent = *p;
1157                 he = rb_entry(parent, struct hist_entry, rb_node);
1158
1159                 cmp = hist_entry__cmp(&entry, he);
1160
1161                 if (!cmp) {
1162                         he->count += count;
1163                         if (callchain) {
1164                                 append_chain(&he->callchain, chain, syms);
1165                                 free(syms);
1166                         }
1167                         return 0;
1168                 }
1169
1170                 if (cmp < 0)
1171                         p = &(*p)->rb_left;
1172                 else
1173                         p = &(*p)->rb_right;
1174         }
1175
1176         he = malloc(sizeof(*he));
1177         if (!he)
1178                 return -ENOMEM;
1179         *he = entry;
1180         if (callchain) {
1181                 callchain_init(&he->callchain);
1182                 append_chain(&he->callchain, chain, syms);
1183                 free(syms);
1184         }
1185         rb_link_node(&he->rb_node, parent, p);
1186         rb_insert_color(&he->rb_node, &hist);
1187
1188         return 0;
1189 }
1190
1191 static void hist_entry__free(struct hist_entry *he)
1192 {
1193         free(he);
1194 }
1195
1196 /*
1197  * collapse the histogram
1198  */
1199
1200 static struct rb_root collapse_hists;
1201
1202 static void collapse__insert_entry(struct hist_entry *he)
1203 {
1204         struct rb_node **p = &collapse_hists.rb_node;
1205         struct rb_node *parent = NULL;
1206         struct hist_entry *iter;
1207         int64_t cmp;
1208
1209         while (*p != NULL) {
1210                 parent = *p;
1211                 iter = rb_entry(parent, struct hist_entry, rb_node);
1212
1213                 cmp = hist_entry__collapse(iter, he);
1214
1215                 if (!cmp) {
1216                         iter->count += he->count;
1217                         hist_entry__free(he);
1218                         return;
1219                 }
1220
1221                 if (cmp < 0)
1222                         p = &(*p)->rb_left;
1223                 else
1224                         p = &(*p)->rb_right;
1225         }
1226
1227         rb_link_node(&he->rb_node, parent, p);
1228         rb_insert_color(&he->rb_node, &collapse_hists);
1229 }
1230
1231 static void collapse__resort(void)
1232 {
1233         struct rb_node *next;
1234         struct hist_entry *n;
1235
1236         if (!sort__need_collapse)
1237                 return;
1238
1239         next = rb_first(&hist);
1240         while (next) {
1241                 n = rb_entry(next, struct hist_entry, rb_node);
1242                 next = rb_next(&n->rb_node);
1243
1244                 rb_erase(&n->rb_node, &hist);
1245                 collapse__insert_entry(n);
1246         }
1247 }
1248
1249 /*
1250  * reverse the map, sort on count.
1251  */
1252
1253 static struct rb_root output_hists;
1254
1255 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
1256 {
1257         struct rb_node **p = &output_hists.rb_node;
1258         struct rb_node *parent = NULL;
1259         struct hist_entry *iter;
1260
1261         if (callchain)
1262                 callchain_param.sort(&he->sorted_chain, &he->callchain,
1263                                       min_callchain_hits, &callchain_param);
1264
1265         while (*p != NULL) {
1266                 parent = *p;
1267                 iter = rb_entry(parent, struct hist_entry, rb_node);
1268
1269                 if (he->count > iter->count)
1270                         p = &(*p)->rb_left;
1271                 else
1272                         p = &(*p)->rb_right;
1273         }
1274
1275         rb_link_node(&he->rb_node, parent, p);
1276         rb_insert_color(&he->rb_node, &output_hists);
1277 }
1278
1279 static void output__resort(u64 total_samples)
1280 {
1281         struct rb_node *next;
1282         struct hist_entry *n;
1283         struct rb_root *tree = &hist;
1284         u64 min_callchain_hits;
1285
1286         min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
1287
1288         if (sort__need_collapse)
1289                 tree = &collapse_hists;
1290
1291         next = rb_first(tree);
1292
1293         while (next) {
1294                 n = rb_entry(next, struct hist_entry, rb_node);
1295                 next = rb_next(&n->rb_node);
1296
1297                 rb_erase(&n->rb_node, tree);
1298                 output__insert_entry(n, min_callchain_hits);
1299         }
1300 }
1301
1302 static size_t output__fprintf(FILE *fp, u64 total_samples)
1303 {
1304         struct hist_entry *pos;
1305         struct sort_entry *se;
1306         struct rb_node *nd;
1307         size_t ret = 0;
1308         unsigned int width;
1309         char *col_width = col_width_list_str;
1310         int raw_printing_style;
1311
1312         raw_printing_style = !strcmp(pretty_printing_style, "raw");
1313
1314         init_rem_hits();
1315
1316         fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
1317         fprintf(fp, "#\n");
1318
1319         fprintf(fp, "# Overhead");
1320         if (show_nr_samples) {
1321                 if (field_sep)
1322                         fprintf(fp, "%cSamples", *field_sep);
1323                 else
1324                         fputs("  Samples  ", fp);
1325         }
1326         list_for_each_entry(se, &hist_entry__sort_list, list) {
1327                 if (se->elide)
1328                         continue;
1329                 if (field_sep) {
1330                         fprintf(fp, "%c%s", *field_sep, se->header);
1331                         continue;
1332                 }
1333                 width = strlen(se->header);
1334                 if (se->width) {
1335                         if (col_width_list_str) {
1336                                 if (col_width) {
1337                                         *se->width = atoi(col_width);
1338                                         col_width = strchr(col_width, ',');
1339                                         if (col_width)
1340                                                 ++col_width;
1341                                 }
1342                         }
1343                         width = *se->width = max(*se->width, width);
1344                 }
1345                 fprintf(fp, "  %*s", width, se->header);
1346         }
1347         fprintf(fp, "\n");
1348
1349         if (field_sep)
1350                 goto print_entries;
1351
1352         fprintf(fp, "# ........");
1353         if (show_nr_samples)
1354                 fprintf(fp, " ..........");
1355         list_for_each_entry(se, &hist_entry__sort_list, list) {
1356                 unsigned int i;
1357
1358                 if (se->elide)
1359                         continue;
1360
1361                 fprintf(fp, "  ");
1362                 if (se->width)
1363                         width = *se->width;
1364                 else
1365                         width = strlen(se->header);
1366                 for (i = 0; i < width; i++)
1367                         fprintf(fp, ".");
1368         }
1369         fprintf(fp, "\n");
1370
1371         fprintf(fp, "#\n");
1372
1373 print_entries:
1374         for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1375                 pos = rb_entry(nd, struct hist_entry, rb_node);
1376                 ret += hist_entry__fprintf(fp, pos, total_samples);
1377         }
1378
1379         if (sort_order == default_sort_order &&
1380                         parent_pattern == default_parent_pattern) {
1381                 fprintf(fp, "#\n");
1382                 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
1383                 fprintf(fp, "#\n");
1384         }
1385         fprintf(fp, "\n");
1386
1387         free(rem_sq_bracket);
1388
1389         if (show_threads)
1390                 perf_read_values_display(fp, &show_threads_values,
1391                                          raw_printing_style);
1392
1393         return ret;
1394 }
1395
1396 static void register_idle_thread(void)
1397 {
1398         struct thread *thread = threads__findnew(0);
1399
1400         if (thread == NULL ||
1401                         thread__set_comm(thread, "[idle]")) {
1402                 fprintf(stderr, "problem inserting idle task.\n");
1403                 exit(-1);
1404         }
1405 }
1406
1407 static unsigned long total = 0,
1408                      total_mmap = 0,
1409                      total_comm = 0,
1410                      total_fork = 0,
1411                      total_unknown = 0,
1412                      total_lost = 0;
1413
1414 static int validate_chain(struct ip_callchain *chain, event_t *event)
1415 {
1416         unsigned int chain_size;
1417
1418         chain_size = event->header.size;
1419         chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1420
1421         if (chain->nr*sizeof(u64) > chain_size)
1422                 return -1;
1423
1424         return 0;
1425 }
1426
1427 static int
1428 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1429 {
1430         char level;
1431         int show = 0;
1432         struct dso *dso = NULL;
1433         struct thread *thread = threads__findnew(event->ip.pid);
1434         u64 ip = event->ip.ip;
1435         u64 period = 1;
1436         struct map *map = NULL;
1437         void *more_data = event->ip.__more_data;
1438         struct ip_callchain *chain = NULL;
1439         int cpumode;
1440
1441         if (sample_type & PERF_SAMPLE_PERIOD) {
1442                 period = *(u64 *)more_data;
1443                 more_data += sizeof(u64);
1444         }
1445
1446         dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1447                 (void *)(offset + head),
1448                 (void *)(long)(event->header.size),
1449                 event->header.misc,
1450                 event->ip.pid,
1451                 (void *)(long)ip,
1452                 (long long)period);
1453
1454         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1455                 unsigned int i;
1456
1457                 chain = (void *)more_data;
1458
1459                 dprintf("... chain: nr:%Lu\n", chain->nr);
1460
1461                 if (validate_chain(chain, event) < 0) {
1462                         eprintf("call-chain problem with event, skipping it.\n");
1463                         return 0;
1464                 }
1465
1466                 if (dump_trace) {
1467                         for (i = 0; i < chain->nr; i++)
1468                                 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1469                 }
1470         }
1471
1472         dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1473
1474         if (thread == NULL) {
1475                 eprintf("problem processing %d event, skipping it.\n",
1476                         event->header.type);
1477                 return -1;
1478         }
1479
1480         if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1481                 return 0;
1482
1483         cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1484
1485         if (cpumode == PERF_EVENT_MISC_KERNEL) {
1486                 show = SHOW_KERNEL;
1487                 level = 'k';
1488
1489                 dso = kernel_dso;
1490
1491                 dprintf(" ...... dso: %s\n", dso->name);
1492
1493         } else if (cpumode == PERF_EVENT_MISC_USER) {
1494
1495                 show = SHOW_USER;
1496                 level = '.';
1497
1498         } else {
1499                 show = SHOW_HV;
1500                 level = 'H';
1501
1502                 dso = hypervisor_dso;
1503
1504                 dprintf(" ...... dso: [hypervisor]\n");
1505         }
1506
1507         if (show & show_mask) {
1508                 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1509
1510                 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1511                         return 0;
1512
1513                 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1514                         return 0;
1515
1516                 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1517                         eprintf("problem incrementing symbol count, skipping event\n");
1518                         return -1;
1519                 }
1520         }
1521         total += period;
1522
1523         return 0;
1524 }
1525
1526 static int
1527 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1528 {
1529         struct thread *thread = threads__findnew(event->mmap.pid);
1530         struct map *map = map__new(&event->mmap);
1531
1532         dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1533                 (void *)(offset + head),
1534                 (void *)(long)(event->header.size),
1535                 event->mmap.pid,
1536                 (void *)(long)event->mmap.start,
1537                 (void *)(long)event->mmap.len,
1538                 (void *)(long)event->mmap.pgoff,
1539                 event->mmap.filename);
1540
1541         if (thread == NULL || map == NULL) {
1542                 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1543                 return 0;
1544         }
1545
1546         thread__insert_map(thread, map);
1547         total_mmap++;
1548
1549         return 0;
1550 }
1551
1552 static int
1553 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1554 {
1555         struct thread *thread = threads__findnew(event->comm.pid);
1556
1557         dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1558                 (void *)(offset + head),
1559                 (void *)(long)(event->header.size),
1560                 event->comm.comm, event->comm.pid);
1561
1562         if (thread == NULL ||
1563             thread__set_comm(thread, event->comm.comm)) {
1564                 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1565                 return -1;
1566         }
1567         total_comm++;
1568
1569         return 0;
1570 }
1571
1572 static int
1573 process_task_event(event_t *event, unsigned long offset, unsigned long head)
1574 {
1575         struct thread *thread = threads__findnew(event->fork.pid);
1576         struct thread *parent = threads__findnew(event->fork.ppid);
1577
1578         dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
1579                 (void *)(offset + head),
1580                 (void *)(long)(event->header.size),
1581                 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1582                 event->fork.pid, event->fork.tid,
1583                 event->fork.ppid, event->fork.ptid);
1584
1585         /*
1586          * A thread clone will have the same PID for both
1587          * parent and child.
1588          */
1589         if (thread == parent)
1590                 return 0;
1591
1592         if (event->header.type == PERF_EVENT_EXIT)
1593                 return 0;
1594
1595         if (!thread || !parent || thread__fork(thread, parent)) {
1596                 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1597                 return -1;
1598         }
1599         total_fork++;
1600
1601         return 0;
1602 }
1603
1604 static int
1605 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1606 {
1607         dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1608                 (void *)(offset + head),
1609                 (void *)(long)(event->header.size),
1610                 event->lost.id,
1611                 event->lost.lost);
1612
1613         total_lost += event->lost.lost;
1614
1615         return 0;
1616 }
1617
1618 static void trace_event(event_t *event)
1619 {
1620         unsigned char *raw_event = (void *)event;
1621         char *color = PERF_COLOR_BLUE;
1622         int i, j;
1623
1624         if (!dump_trace)
1625                 return;
1626
1627         dprintf(".");
1628         cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1629
1630         for (i = 0; i < event->header.size; i++) {
1631                 if ((i & 15) == 0) {
1632                         dprintf(".");
1633                         cdprintf("  %04x: ", i);
1634                 }
1635
1636                 cdprintf(" %02x", raw_event[i]);
1637
1638                 if (((i & 15) == 15) || i == event->header.size-1) {
1639                         cdprintf("  ");
1640                         for (j = 0; j < 15-(i & 15); j++)
1641                                 cdprintf("   ");
1642                         for (j = 0; j < (i & 15); j++) {
1643                                 if (isprint(raw_event[i-15+j]))
1644                                         cdprintf("%c", raw_event[i-15+j]);
1645                                 else
1646                                         cdprintf(".");
1647                         }
1648                         cdprintf("\n");
1649                 }
1650         }
1651         dprintf(".\n");
1652 }
1653
1654 static struct perf_header       *header;
1655
1656 static struct perf_counter_attr *perf_header__find_attr(u64 id)
1657 {
1658         int i;
1659
1660         for (i = 0; i < header->attrs; i++) {
1661                 struct perf_header_attr *attr = header->attr[i];
1662                 int j;
1663
1664                 for (j = 0; j < attr->ids; j++) {
1665                         if (attr->id[j] == id)
1666                                 return &attr->attr;
1667                 }
1668         }
1669
1670         return NULL;
1671 }
1672
1673 static int
1674 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1675 {
1676         struct perf_counter_attr *attr = perf_header__find_attr(event->read.id);
1677
1678         if (show_threads) {
1679                 char *name = attr ? __event_name(attr->type, attr->config)
1680                                    : "unknown";
1681                 perf_read_values_add_value(&show_threads_values,
1682                                            event->read.pid, event->read.tid,
1683                                            event->read.id,
1684                                            name,
1685                                            event->read.value);
1686         }
1687
1688         dprintf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
1689                         (void *)(offset + head),
1690                         (void *)(long)(event->header.size),
1691                         event->read.pid,
1692                         event->read.tid,
1693                         attr ? __event_name(attr->type, attr->config)
1694                              : "FAIL",
1695                         event->read.value);
1696
1697         return 0;
1698 }
1699
1700 static int
1701 process_event(event_t *event, unsigned long offset, unsigned long head)
1702 {
1703         trace_event(event);
1704
1705         switch (event->header.type) {
1706         case PERF_EVENT_SAMPLE:
1707                 return process_sample_event(event, offset, head);
1708
1709         case PERF_EVENT_MMAP:
1710                 return process_mmap_event(event, offset, head);
1711
1712         case PERF_EVENT_COMM:
1713                 return process_comm_event(event, offset, head);
1714
1715         case PERF_EVENT_FORK:
1716         case PERF_EVENT_EXIT:
1717                 return process_task_event(event, offset, head);
1718
1719         case PERF_EVENT_LOST:
1720                 return process_lost_event(event, offset, head);
1721
1722         case PERF_EVENT_READ:
1723                 return process_read_event(event, offset, head);
1724
1725         /*
1726          * We dont process them right now but they are fine:
1727          */
1728
1729         case PERF_EVENT_THROTTLE:
1730         case PERF_EVENT_UNTHROTTLE:
1731                 return 0;
1732
1733         default:
1734                 return -1;
1735         }
1736
1737         return 0;
1738 }
1739
1740 static u64 perf_header__sample_type(void)
1741 {
1742         u64 sample_type = 0;
1743         int i;
1744
1745         for (i = 0; i < header->attrs; i++) {
1746                 struct perf_header_attr *attr = header->attr[i];
1747
1748                 if (!sample_type)
1749                         sample_type = attr->attr.sample_type;
1750                 else if (sample_type != attr->attr.sample_type)
1751                         die("non matching sample_type");
1752         }
1753
1754         return sample_type;
1755 }
1756
1757 static int __cmd_report(void)
1758 {
1759         int ret, rc = EXIT_FAILURE;
1760         unsigned long offset = 0;
1761         unsigned long head, shift;
1762         struct stat stat;
1763         event_t *event;
1764         uint32_t size;
1765         char *buf;
1766
1767         register_idle_thread();
1768
1769         if (show_threads)
1770                 perf_read_values_init(&show_threads_values);
1771
1772         input = open(input_name, O_RDONLY);
1773         if (input < 0) {
1774                 fprintf(stderr, " failed to open file: %s", input_name);
1775                 if (!strcmp(input_name, "perf.data"))
1776                         fprintf(stderr, "  (try 'perf record' first)");
1777                 fprintf(stderr, "\n");
1778                 exit(-1);
1779         }
1780
1781         ret = fstat(input, &stat);
1782         if (ret < 0) {
1783                 perror("failed to stat file");
1784                 exit(-1);
1785         }
1786
1787         if (!stat.st_size) {
1788                 fprintf(stderr, "zero-sized file, nothing to do!\n");
1789                 exit(0);
1790         }
1791
1792         header = perf_header__read(input);
1793         head = header->data_offset;
1794
1795         sample_type = perf_header__sample_type();
1796
1797         if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1798                 if (sort__has_parent) {
1799                         fprintf(stderr, "selected --sort parent, but no"
1800                                         " callchain data. Did you call"
1801                                         " perf record without -g?\n");
1802                         exit(-1);
1803                 }
1804                 if (callchain) {
1805                         fprintf(stderr, "selected -c but no callchain data."
1806                                         " Did you call perf record without"
1807                                         " -g?\n");
1808                         exit(-1);
1809                 }
1810         } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1811                         callchain = 1;
1812                         if (register_callchain_param(&callchain_param) < 0) {
1813                                 fprintf(stderr, "Can't register callchain"
1814                                                 " params\n");
1815                                 exit(-1);
1816                         }
1817         }
1818
1819         if (load_kernel() < 0) {
1820                 perror("failed to load kernel symbols");
1821                 return EXIT_FAILURE;
1822         }
1823
1824         if (!full_paths) {
1825                 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1826                         perror("failed to get the current directory");
1827                         return EXIT_FAILURE;
1828                 }
1829                 cwdlen = strlen(cwd);
1830         } else {
1831                 cwd = NULL;
1832                 cwdlen = 0;
1833         }
1834
1835         shift = page_size * (head / page_size);
1836         offset += shift;
1837         head -= shift;
1838
1839 remap:
1840         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1841                            MAP_SHARED, input, offset);
1842         if (buf == MAP_FAILED) {
1843                 perror("failed to mmap file");
1844                 exit(-1);
1845         }
1846
1847 more:
1848         event = (event_t *)(buf + head);
1849
1850         size = event->header.size;
1851         if (!size)
1852                 size = 8;
1853
1854         if (head + event->header.size >= page_size * mmap_window) {
1855                 int ret;
1856
1857                 shift = page_size * (head / page_size);
1858
1859                 ret = munmap(buf, page_size * mmap_window);
1860                 assert(ret == 0);
1861
1862                 offset += shift;
1863                 head -= shift;
1864                 goto remap;
1865         }
1866
1867         size = event->header.size;
1868
1869         dprintf("\n%p [%p]: event: %d\n",
1870                         (void *)(offset + head),
1871                         (void *)(long)event->header.size,
1872                         event->header.type);
1873
1874         if (!size || process_event(event, offset, head) < 0) {
1875
1876                 dprintf("%p [%p]: skipping unknown header type: %d\n",
1877                         (void *)(offset + head),
1878                         (void *)(long)(event->header.size),
1879                         event->header.type);
1880
1881                 total_unknown++;
1882
1883                 /*
1884                  * assume we lost track of the stream, check alignment, and
1885                  * increment a single u64 in the hope to catch on again 'soon'.
1886                  */
1887
1888                 if (unlikely(head & 7))
1889                         head &= ~7ULL;
1890
1891                 size = 8;
1892         }
1893
1894         head += size;
1895
1896         if (offset + head >= header->data_offset + header->data_size)
1897                 goto done;
1898
1899         if (offset + head < (unsigned long)stat.st_size)
1900                 goto more;
1901
1902 done:
1903         rc = EXIT_SUCCESS;
1904         close(input);
1905
1906         dprintf("      IP events: %10ld\n", total);
1907         dprintf("    mmap events: %10ld\n", total_mmap);
1908         dprintf("    comm events: %10ld\n", total_comm);
1909         dprintf("    fork events: %10ld\n", total_fork);
1910         dprintf("    lost events: %10ld\n", total_lost);
1911         dprintf(" unknown events: %10ld\n", total_unknown);
1912
1913         if (dump_trace)
1914                 return 0;
1915
1916         if (verbose >= 3)
1917                 threads__fprintf(stdout);
1918
1919         if (verbose >= 2)
1920                 dsos__fprintf(stdout);
1921
1922         collapse__resort();
1923         output__resort(total);
1924         output__fprintf(stdout, total);
1925
1926         if (show_threads)
1927                 perf_read_values_destroy(&show_threads_values);
1928
1929         return rc;
1930 }
1931
1932 static int
1933 parse_callchain_opt(const struct option *opt __used, const char *arg,
1934                     int unset __used)
1935 {
1936         char *tok;
1937         char *endptr;
1938
1939         callchain = 1;
1940
1941         if (!arg)
1942                 return 0;
1943
1944         tok = strtok((char *)arg, ",");
1945         if (!tok)
1946                 return -1;
1947
1948         /* get the output mode */
1949         if (!strncmp(tok, "graph", strlen(arg)))
1950                 callchain_param.mode = CHAIN_GRAPH_ABS;
1951
1952         else if (!strncmp(tok, "flat", strlen(arg)))
1953                 callchain_param.mode = CHAIN_FLAT;
1954
1955         else if (!strncmp(tok, "fractal", strlen(arg)))
1956                 callchain_param.mode = CHAIN_GRAPH_REL;
1957
1958         else if (!strncmp(tok, "none", strlen(arg))) {
1959                 callchain_param.mode = CHAIN_NONE;
1960                 callchain = 0;
1961
1962                 return 0;
1963         }
1964
1965         else
1966                 return -1;
1967
1968         /* get the min percentage */
1969         tok = strtok(NULL, ",");
1970         if (!tok)
1971                 goto setup;
1972
1973         callchain_param.min_percent = strtod(tok, &endptr);
1974         if (tok == endptr)
1975                 return -1;
1976
1977 setup:
1978         if (register_callchain_param(&callchain_param) < 0) {
1979                 fprintf(stderr, "Can't register callchain params\n");
1980                 return -1;
1981         }
1982         return 0;
1983 }
1984
1985 static const char * const report_usage[] = {
1986         "perf report [<options>] <command>",
1987         NULL
1988 };
1989
1990 static const struct option options[] = {
1991         OPT_STRING('i', "input", &input_name, "file",
1992                     "input file name"),
1993         OPT_BOOLEAN('v', "verbose", &verbose,
1994                     "be more verbose (show symbol address, etc)"),
1995         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1996                     "dump raw trace in ASCII"),
1997         OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1998         OPT_BOOLEAN('m', "modules", &modules,
1999                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
2000         OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
2001                     "Show a column with the number of samples"),
2002         OPT_BOOLEAN('T', "threads", &show_threads,
2003                     "Show per-thread event counters"),
2004         OPT_STRING(0, "pretty", &pretty_printing_style, "key",
2005                    "pretty printing style key: normal raw"),
2006         OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
2007                    "sort by key(s): pid, comm, dso, symbol, parent"),
2008         OPT_BOOLEAN('P', "full-paths", &full_paths,
2009                     "Don't shorten the pathnames taking into account the cwd"),
2010         OPT_STRING('p', "parent", &parent_pattern, "regex",
2011                    "regex filter to identify parent, see: '--sort parent'"),
2012         OPT_BOOLEAN('x', "exclude-other", &exclude_other,
2013                     "Only display entries with parent-match"),
2014         OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
2015                      "Display callchains using output_type and min percent threshold. "
2016                      "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
2017         OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
2018                    "only consider symbols in these dsos"),
2019         OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
2020                    "only consider symbols in these comms"),
2021         OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
2022                    "only consider these symbols"),
2023         OPT_STRING('w', "column-widths", &col_width_list_str,
2024                    "width[,width...]",
2025                    "don't try to adjust column width, use these fixed values"),
2026         OPT_STRING('t', "field-separator", &field_sep, "separator",
2027                    "separator for columns, no spaces will be added between "
2028                    "columns '.' is reserved."),
2029         OPT_END()
2030 };
2031
2032 static void setup_sorting(void)
2033 {
2034         char *tmp, *tok, *str = strdup(sort_order);
2035
2036         for (tok = strtok_r(str, ", ", &tmp);
2037                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
2038                 if (sort_dimension__add(tok) < 0) {
2039                         error("Unknown --sort key: `%s'", tok);
2040                         usage_with_options(report_usage, options);
2041                 }
2042         }
2043
2044         free(str);
2045 }
2046
2047 static void setup_list(struct strlist **list, const char *list_str,
2048                        struct sort_entry *se, const char *list_name,
2049                        FILE *fp)
2050 {
2051         if (list_str) {
2052                 *list = strlist__new(true, list_str);
2053                 if (!*list) {
2054                         fprintf(stderr, "problems parsing %s list\n",
2055                                 list_name);
2056                         exit(129);
2057                 }
2058                 if (strlist__nr_entries(*list) == 1) {
2059                         fprintf(fp, "# %s: %s\n", list_name,
2060                                 strlist__entry(*list, 0)->s);
2061                         se->elide = true;
2062                 }
2063         }
2064 }
2065
2066 int cmd_report(int argc, const char **argv, const char *prefix __used)
2067 {
2068         symbol__init();
2069
2070         page_size = getpagesize();
2071
2072         argc = parse_options(argc, argv, options, report_usage, 0);
2073
2074         setup_sorting();
2075
2076         if (parent_pattern != default_parent_pattern) {
2077                 sort_dimension__add("parent");
2078                 sort_parent.elide = 1;
2079         } else
2080                 exclude_other = 0;
2081
2082         /*
2083          * Any (unrecognized) arguments left?
2084          */
2085         if (argc)
2086                 usage_with_options(report_usage, options);
2087
2088         setup_pager();
2089
2090         setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
2091         setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
2092         setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
2093
2094         if (field_sep && *field_sep == '.') {
2095                 fputs("'.' is the only non valid --field-separator argument\n",
2096                       stderr);
2097                 exit(129);
2098         }
2099
2100         return __cmd_report();
2101 }