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