perf symbols: Remove unrelated actions from dso__load_kernel_sym
[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/debug.h"
24 #include "util/header.h"
25
26 #include "util/parse-options.h"
27 #include "util/parse-events.h"
28
29 #include "util/data_map.h"
30 #include "util/thread.h"
31 #include "util/sort.h"
32 #include "util/hist.h"
33
34 static char             const *input_name = "perf.data";
35
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
40 static int              force;
41 static bool             use_modules;
42
43 static int              full_paths;
44 static int              show_nr_samples;
45
46 static int              show_threads;
47 static struct perf_read_values  show_threads_values;
48
49 static char             default_pretty_printing_style[] = "normal";
50 static char             *pretty_printing_style = default_pretty_printing_style;
51
52 static int              exclude_other = 1;
53
54 static char             callchain_default_opt[] = "fractal,0.5";
55
56 static char             *cwd;
57 static int              cwdlen;
58
59 static struct perf_header *header;
60
61 static u64              sample_type;
62
63
64 static size_t
65 callchain__fprintf_left_margin(FILE *fp, int left_margin)
66 {
67         int i;
68         int ret;
69
70         ret = fprintf(fp, "            ");
71
72         for (i = 0; i < left_margin; i++)
73                 ret += fprintf(fp, " ");
74
75         return ret;
76 }
77
78 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
79                                           int left_margin)
80 {
81         int i;
82         size_t ret = 0;
83
84         ret += callchain__fprintf_left_margin(fp, left_margin);
85
86         for (i = 0; i < depth; i++)
87                 if (depth_mask & (1 << i))
88                         ret += fprintf(fp, "|          ");
89                 else
90                         ret += fprintf(fp, "           ");
91
92         ret += fprintf(fp, "\n");
93
94         return ret;
95 }
96 static size_t
97 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
98                        int depth_mask, int count, u64 total_samples,
99                        int hits, int left_margin)
100 {
101         int i;
102         size_t ret = 0;
103
104         ret += callchain__fprintf_left_margin(fp, left_margin);
105         for (i = 0; i < depth; i++) {
106                 if (depth_mask & (1 << i))
107                         ret += fprintf(fp, "|");
108                 else
109                         ret += fprintf(fp, " ");
110                 if (!count && i == depth - 1) {
111                         double percent;
112
113                         percent = hits * 100.0 / total_samples;
114                         ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
115                 } else
116                         ret += fprintf(fp, "%s", "          ");
117         }
118         if (chain->sym)
119                 ret += fprintf(fp, "%s\n", chain->sym->name);
120         else
121                 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
122
123         return ret;
124 }
125
126 static struct symbol *rem_sq_bracket;
127 static struct callchain_list rem_hits;
128
129 static void init_rem_hits(void)
130 {
131         rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
132         if (!rem_sq_bracket) {
133                 fprintf(stderr, "Not enough memory to display remaining hits\n");
134                 return;
135         }
136
137         strcpy(rem_sq_bracket->name, "[...]");
138         rem_hits.sym = rem_sq_bracket;
139 }
140
141 static size_t
142 __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
143                            u64 total_samples, int depth, int depth_mask,
144                            int left_margin)
145 {
146         struct rb_node *node, *next;
147         struct callchain_node *child;
148         struct callchain_list *chain;
149         int new_depth_mask = depth_mask;
150         u64 new_total;
151         u64 remaining;
152         size_t ret = 0;
153         int i;
154
155         if (callchain_param.mode == CHAIN_GRAPH_REL)
156                 new_total = self->children_hit;
157         else
158                 new_total = total_samples;
159
160         remaining = new_total;
161
162         node = rb_first(&self->rb_root);
163         while (node) {
164                 u64 cumul;
165
166                 child = rb_entry(node, struct callchain_node, rb_node);
167                 cumul = cumul_hits(child);
168                 remaining -= cumul;
169
170                 /*
171                  * The depth mask manages the output of pipes that show
172                  * the depth. We don't want to keep the pipes of the current
173                  * level for the last child of this depth.
174                  * Except if we have remaining filtered hits. They will
175                  * supersede the last child
176                  */
177                 next = rb_next(node);
178                 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
179                         new_depth_mask &= ~(1 << (depth - 1));
180
181                 /*
182                  * But we keep the older depth mask for the line seperator
183                  * to keep the level link until we reach the last child
184                  */
185                 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
186                                                    left_margin);
187                 i = 0;
188                 list_for_each_entry(chain, &child->val, list) {
189                         if (chain->ip >= PERF_CONTEXT_MAX)
190                                 continue;
191                         ret += ipchain__fprintf_graph(fp, chain, depth,
192                                                       new_depth_mask, i++,
193                                                       new_total,
194                                                       cumul,
195                                                       left_margin);
196                 }
197                 ret += __callchain__fprintf_graph(fp, child, new_total,
198                                                   depth + 1,
199                                                   new_depth_mask | (1 << depth),
200                                                   left_margin);
201                 node = next;
202         }
203
204         if (callchain_param.mode == CHAIN_GRAPH_REL &&
205                 remaining && remaining != new_total) {
206
207                 if (!rem_sq_bracket)
208                         return ret;
209
210                 new_depth_mask &= ~(1 << (depth - 1));
211
212                 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
213                                               new_depth_mask, 0, new_total,
214                                               remaining, left_margin);
215         }
216
217         return ret;
218 }
219
220
221 static size_t
222 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
223                          u64 total_samples, int left_margin)
224 {
225         struct callchain_list *chain;
226         bool printed = false;
227         int i = 0;
228         int ret = 0;
229
230         list_for_each_entry(chain, &self->val, list) {
231                 if (chain->ip >= PERF_CONTEXT_MAX)
232                         continue;
233
234                 if (!i++ && sort__first_dimension == SORT_SYM)
235                         continue;
236
237                 if (!printed) {
238                         ret += callchain__fprintf_left_margin(fp, left_margin);
239                         ret += fprintf(fp, "|\n");
240                         ret += callchain__fprintf_left_margin(fp, left_margin);
241                         ret += fprintf(fp, "---");
242
243                         left_margin += 3;
244                         printed = true;
245                 } else
246                         ret += callchain__fprintf_left_margin(fp, left_margin);
247
248                 if (chain->sym)
249                         ret += fprintf(fp, " %s\n", chain->sym->name);
250                 else
251                         ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
252         }
253
254         ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
255
256         return ret;
257 }
258
259 static size_t
260 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
261                         u64 total_samples)
262 {
263         struct callchain_list *chain;
264         size_t ret = 0;
265
266         if (!self)
267                 return 0;
268
269         ret += callchain__fprintf_flat(fp, self->parent, total_samples);
270
271
272         list_for_each_entry(chain, &self->val, list) {
273                 if (chain->ip >= PERF_CONTEXT_MAX)
274                         continue;
275                 if (chain->sym)
276                         ret += fprintf(fp, "                %s\n", chain->sym->name);
277                 else
278                         ret += fprintf(fp, "                %p\n",
279                                         (void *)(long)chain->ip);
280         }
281
282         return ret;
283 }
284
285 static size_t
286 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
287                               u64 total_samples, int left_margin)
288 {
289         struct rb_node *rb_node;
290         struct callchain_node *chain;
291         size_t ret = 0;
292
293         rb_node = rb_first(&self->sorted_chain);
294         while (rb_node) {
295                 double percent;
296
297                 chain = rb_entry(rb_node, struct callchain_node, rb_node);
298                 percent = chain->hit * 100.0 / total_samples;
299                 switch (callchain_param.mode) {
300                 case CHAIN_FLAT:
301                         ret += percent_color_fprintf(fp, "           %6.2f%%\n",
302                                                      percent);
303                         ret += callchain__fprintf_flat(fp, chain, total_samples);
304                         break;
305                 case CHAIN_GRAPH_ABS: /* Falldown */
306                 case CHAIN_GRAPH_REL:
307                         ret += callchain__fprintf_graph(fp, chain, total_samples,
308                                                         left_margin);
309                 case CHAIN_NONE:
310                 default:
311                         break;
312                 }
313                 ret += fprintf(fp, "\n");
314                 rb_node = rb_next(rb_node);
315         }
316
317         return ret;
318 }
319
320 static size_t
321 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
322 {
323         struct sort_entry *se;
324         size_t ret;
325
326         if (exclude_other && !self->parent)
327                 return 0;
328
329         if (total_samples)
330                 ret = percent_color_fprintf(fp,
331                                             field_sep ? "%.2f" : "   %6.2f%%",
332                                         (self->count * 100.0) / total_samples);
333         else
334                 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
335
336         if (show_nr_samples) {
337                 if (field_sep)
338                         fprintf(fp, "%c%lld", *field_sep, self->count);
339                 else
340                         fprintf(fp, "%11lld", self->count);
341         }
342
343         list_for_each_entry(se, &hist_entry__sort_list, list) {
344                 if (se->elide)
345                         continue;
346
347                 fprintf(fp, "%s", field_sep ?: "  ");
348                 ret += se->print(fp, self, se->width ? *se->width : 0);
349         }
350
351         ret += fprintf(fp, "\n");
352
353         if (callchain) {
354                 int left_margin = 0;
355
356                 if (sort__first_dimension == SORT_COMM) {
357                         se = list_first_entry(&hist_entry__sort_list, typeof(*se),
358                                                 list);
359                         left_margin = se->width ? *se->width : 0;
360                         left_margin -= thread__comm_len(self->thread);
361                 }
362
363                 hist_entry_callchain__fprintf(fp, self, total_samples,
364                                               left_margin);
365         }
366
367         return ret;
368 }
369
370 /*
371  *
372  */
373
374 static void dso__calc_col_width(struct dso *self)
375 {
376         if (!col_width_list_str && !field_sep &&
377             (!dso_list || strlist__has_entry(dso_list, self->name))) {
378                 unsigned int slen = strlen(self->name);
379                 if (slen > dsos__col_width)
380                         dsos__col_width = slen;
381         }
382
383         self->slen_calculated = 1;
384 }
385
386 static void thread__comm_adjust(struct thread *self)
387 {
388         char *comm = self->comm;
389
390         if (!col_width_list_str && !field_sep &&
391             (!comm_list || strlist__has_entry(comm_list, comm))) {
392                 unsigned int slen = strlen(comm);
393
394                 if (slen > comms__col_width) {
395                         comms__col_width = slen;
396                         threads__col_width = slen + 6;
397                 }
398         }
399 }
400
401 static int thread__set_comm_adjust(struct thread *self, const char *comm)
402 {
403         int ret = thread__set_comm(self, comm);
404
405         if (ret)
406                 return ret;
407
408         thread__comm_adjust(self);
409
410         return 0;
411 }
412
413
414 static struct symbol *
415 resolve_symbol(struct thread *thread, struct map **mapp, u64 *ipp)
416 {
417         struct map *map = mapp ? *mapp : NULL;
418         u64 ip = *ipp;
419
420         if (map)
421                 goto got_map;
422
423         if (!thread)
424                 return NULL;
425
426         map = thread__find_map(thread, ip);
427         if (map != NULL) {
428                 /*
429                  * We have to do this here as we may have a dso
430                  * with no symbol hit that has a name longer than
431                  * the ones with symbols sampled.
432                  */
433                 if (!sort_dso.elide && !map->dso->slen_calculated)
434                         dso__calc_col_width(map->dso);
435
436                 if (mapp)
437                         *mapp = map;
438 got_map:
439                 ip = map->map_ip(map, ip);
440         } else {
441                 /*
442                  * If this is outside of all known maps,
443                  * and is a negative address, try to look it
444                  * up in the kernel dso, as it might be a
445                  * vsyscall or vdso (which executes in user-mode).
446                  *
447                  * XXX This is nasty, we should have a symbol list in
448                  * the "[vdso]" dso, but for now lets use the old
449                  * trick of looking in the whole kernel symbol list.
450                  */
451                 if ((long long)ip < 0)
452                         return kernel_maps__find_symbol(ip, mapp);
453         }
454         dump_printf(" ...... dso: %s\n",
455                     map ? map->dso->long_name : "<not found>");
456         dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
457         *ipp  = ip;
458
459         return map ? map__find_symbol(map, ip, NULL) : NULL;
460 }
461
462 static int call__match(struct symbol *sym)
463 {
464         if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
465                 return 1;
466
467         return 0;
468 }
469
470 static struct symbol **resolve_callchain(struct thread *thread, struct map *map,
471                                          struct ip_callchain *chain,
472                                          struct symbol **parent)
473 {
474         u64 context = PERF_CONTEXT_MAX;
475         struct symbol **syms = NULL;
476         unsigned int i;
477
478         if (callchain) {
479                 syms = calloc(chain->nr, sizeof(*syms));
480                 if (!syms) {
481                         fprintf(stderr, "Can't allocate memory for symbols\n");
482                         exit(-1);
483                 }
484         }
485
486         for (i = 0; i < chain->nr; i++) {
487                 u64 ip = chain->ips[i];
488                 struct symbol *sym = NULL;
489
490                 if (ip >= PERF_CONTEXT_MAX) {
491                         context = ip;
492                         continue;
493                 }
494
495                 switch (context) {
496                 case PERF_CONTEXT_HV:
497                         break;
498                 case PERF_CONTEXT_KERNEL:
499                         sym = kernel_maps__find_symbol(ip, &map);
500                         break;
501                 default:
502                         sym = resolve_symbol(thread, &map, &ip);
503                         break;
504                 }
505
506                 if (sym) {
507                         if (sort__has_parent && !*parent && call__match(sym))
508                                 *parent = sym;
509                         if (!callchain)
510                                 break;
511                         syms[i] = sym;
512                 }
513         }
514
515         return syms;
516 }
517
518 /*
519  * collect histogram counts
520  */
521
522 static int
523 hist_entry__add(struct thread *thread, struct map *map,
524                 struct symbol *sym, u64 ip, struct ip_callchain *chain,
525                 char level, u64 count)
526 {
527         struct symbol **syms = NULL, *parent = NULL;
528         bool hit;
529         struct hist_entry *he;
530
531         if ((sort__has_parent || callchain) && chain)
532                 syms = resolve_callchain(thread, map, chain, &parent);
533
534         he = __hist_entry__add(thread, map, sym, parent,
535                                ip, count, level, &hit);
536         if (he == NULL)
537                 return -ENOMEM;
538
539         if (hit)
540                 he->count += count;
541
542         if (callchain) {
543                 if (!hit)
544                         callchain_init(&he->callchain);
545                 append_chain(&he->callchain, chain, syms);
546                 free(syms);
547         }
548
549         return 0;
550 }
551
552 static size_t output__fprintf(FILE *fp, u64 total_samples)
553 {
554         struct hist_entry *pos;
555         struct sort_entry *se;
556         struct rb_node *nd;
557         size_t ret = 0;
558         unsigned int width;
559         char *col_width = col_width_list_str;
560         int raw_printing_style;
561
562         raw_printing_style = !strcmp(pretty_printing_style, "raw");
563
564         init_rem_hits();
565
566         fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
567         fprintf(fp, "#\n");
568
569         fprintf(fp, "# Overhead");
570         if (show_nr_samples) {
571                 if (field_sep)
572                         fprintf(fp, "%cSamples", *field_sep);
573                 else
574                         fputs("  Samples  ", fp);
575         }
576         list_for_each_entry(se, &hist_entry__sort_list, list) {
577                 if (se->elide)
578                         continue;
579                 if (field_sep) {
580                         fprintf(fp, "%c%s", *field_sep, se->header);
581                         continue;
582                 }
583                 width = strlen(se->header);
584                 if (se->width) {
585                         if (col_width_list_str) {
586                                 if (col_width) {
587                                         *se->width = atoi(col_width);
588                                         col_width = strchr(col_width, ',');
589                                         if (col_width)
590                                                 ++col_width;
591                                 }
592                         }
593                         width = *se->width = max(*se->width, width);
594                 }
595                 fprintf(fp, "  %*s", width, se->header);
596         }
597         fprintf(fp, "\n");
598
599         if (field_sep)
600                 goto print_entries;
601
602         fprintf(fp, "# ........");
603         if (show_nr_samples)
604                 fprintf(fp, " ..........");
605         list_for_each_entry(se, &hist_entry__sort_list, list) {
606                 unsigned int i;
607
608                 if (se->elide)
609                         continue;
610
611                 fprintf(fp, "  ");
612                 if (se->width)
613                         width = *se->width;
614                 else
615                         width = strlen(se->header);
616                 for (i = 0; i < width; i++)
617                         fprintf(fp, ".");
618         }
619         fprintf(fp, "\n");
620
621         fprintf(fp, "#\n");
622
623 print_entries:
624         for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
625                 pos = rb_entry(nd, struct hist_entry, rb_node);
626                 ret += hist_entry__fprintf(fp, pos, total_samples);
627         }
628
629         if (sort_order == default_sort_order &&
630                         parent_pattern == default_parent_pattern) {
631                 fprintf(fp, "#\n");
632                 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
633                 fprintf(fp, "#\n");
634         }
635         fprintf(fp, "\n");
636
637         free(rem_sq_bracket);
638
639         if (show_threads)
640                 perf_read_values_display(fp, &show_threads_values,
641                                          raw_printing_style);
642
643         return ret;
644 }
645
646 static int validate_chain(struct ip_callchain *chain, event_t *event)
647 {
648         unsigned int chain_size;
649
650         chain_size = event->header.size;
651         chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
652
653         if (chain->nr*sizeof(u64) > chain_size)
654                 return -1;
655
656         return 0;
657 }
658
659 static int
660 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
661 {
662         char level;
663         struct symbol *sym = NULL;
664         u64 ip = event->ip.ip;
665         u64 period = 1;
666         struct map *map = NULL;
667         void *more_data = event->ip.__more_data;
668         struct ip_callchain *chain = NULL;
669         int cpumode;
670         struct thread *thread = threads__findnew(event->ip.pid);
671
672         if (sample_type & PERF_SAMPLE_PERIOD) {
673                 period = *(u64 *)more_data;
674                 more_data += sizeof(u64);
675         }
676
677         dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
678                 (void *)(offset + head),
679                 (void *)(long)(event->header.size),
680                 event->header.misc,
681                 event->ip.pid, event->ip.tid,
682                 (void *)(long)ip,
683                 (long long)period);
684
685         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
686                 unsigned int i;
687
688                 chain = (void *)more_data;
689
690                 dump_printf("... chain: nr:%Lu\n", chain->nr);
691
692                 if (validate_chain(chain, event) < 0) {
693                         pr_debug("call-chain problem with event, "
694                                  "skipping it.\n");
695                         return 0;
696                 }
697
698                 if (dump_trace) {
699                         for (i = 0; i < chain->nr; i++)
700                                 dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
701                 }
702         }
703
704         if (thread == NULL) {
705                 pr_debug("problem processing %d event, skipping it.\n",
706                         event->header.type);
707                 return -1;
708         }
709
710         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
711
712         if (comm_list && !strlist__has_entry(comm_list, thread->comm))
713                 return 0;
714
715         cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
716
717         if (cpumode == PERF_RECORD_MISC_KERNEL) {
718                 level = 'k';
719                 sym = kernel_maps__find_symbol(ip, &map);
720                 dump_printf(" ...... dso: %s\n",
721                             map ? map->dso->long_name : "<not found>");
722         } else if (cpumode == PERF_RECORD_MISC_USER) {
723                 level = '.';
724                 sym = resolve_symbol(thread, &map, &ip);
725
726         } else {
727                 level = 'H';
728                 dump_printf(" ...... dso: [hypervisor]\n");
729         }
730
731         if (dso_list &&
732             (!map || !map->dso ||
733              !(strlist__has_entry(dso_list, map->dso->short_name) ||
734                (map->dso->short_name != map->dso->long_name &&
735                 strlist__has_entry(dso_list, map->dso->long_name)))))
736                 return 0;
737
738         if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
739                 return 0;
740
741         if (hist_entry__add(thread, map, sym, ip,
742                             chain, level, period)) {
743                 pr_debug("problem incrementing symbol count, skipping event\n");
744                 return -1;
745         }
746
747         total += period;
748
749         return 0;
750 }
751
752 static int
753 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
754 {
755         struct map *map = map__new(&event->mmap, cwd, cwdlen);
756         struct thread *thread = threads__findnew(event->mmap.pid);
757
758         dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
759                 (void *)(offset + head),
760                 (void *)(long)(event->header.size),
761                 event->mmap.pid,
762                 event->mmap.tid,
763                 (void *)(long)event->mmap.start,
764                 (void *)(long)event->mmap.len,
765                 (void *)(long)event->mmap.pgoff,
766                 event->mmap.filename);
767
768         if (thread == NULL || map == NULL) {
769                 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
770                 return 0;
771         }
772
773         thread__insert_map(thread, map);
774         total_mmap++;
775
776         return 0;
777 }
778
779 static int
780 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
781 {
782         struct thread *thread = threads__findnew(event->comm.pid);
783
784         dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
785                 (void *)(offset + head),
786                 (void *)(long)(event->header.size),
787                 event->comm.comm, event->comm.pid);
788
789         if (thread == NULL ||
790             thread__set_comm_adjust(thread, event->comm.comm)) {
791                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
792                 return -1;
793         }
794         total_comm++;
795
796         return 0;
797 }
798
799 static int
800 process_task_event(event_t *event, unsigned long offset, unsigned long head)
801 {
802         struct thread *thread = threads__findnew(event->fork.pid);
803         struct thread *parent = threads__findnew(event->fork.ppid);
804
805         dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
806                 (void *)(offset + head),
807                 (void *)(long)(event->header.size),
808                 event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
809                 event->fork.pid, event->fork.tid,
810                 event->fork.ppid, event->fork.ptid);
811
812         /*
813          * A thread clone will have the same PID for both
814          * parent and child.
815          */
816         if (thread == parent)
817                 return 0;
818
819         if (event->header.type == PERF_RECORD_EXIT)
820                 return 0;
821
822         if (!thread || !parent || thread__fork(thread, parent)) {
823                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
824                 return -1;
825         }
826         total_fork++;
827
828         return 0;
829 }
830
831 static int
832 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
833 {
834         dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
835                 (void *)(offset + head),
836                 (void *)(long)(event->header.size),
837                 event->lost.id,
838                 event->lost.lost);
839
840         total_lost += event->lost.lost;
841
842         return 0;
843 }
844
845 static int
846 process_read_event(event_t *event, unsigned long offset, unsigned long head)
847 {
848         struct perf_event_attr *attr;
849
850         attr = perf_header__find_attr(event->read.id, header);
851
852         if (show_threads) {
853                 const char *name = attr ? __event_name(attr->type, attr->config)
854                                    : "unknown";
855                 perf_read_values_add_value(&show_threads_values,
856                                            event->read.pid, event->read.tid,
857                                            event->read.id,
858                                            name,
859                                            event->read.value);
860         }
861
862         dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
863                         (void *)(offset + head),
864                         (void *)(long)(event->header.size),
865                         event->read.pid,
866                         event->read.tid,
867                         attr ? __event_name(attr->type, attr->config)
868                              : "FAIL",
869                         event->read.value);
870
871         return 0;
872 }
873
874 static int sample_type_check(u64 type)
875 {
876         sample_type = type;
877
878         if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
879                 if (sort__has_parent) {
880                         fprintf(stderr, "selected --sort parent, but no"
881                                         " callchain data. Did you call"
882                                         " perf record without -g?\n");
883                         return -1;
884                 }
885                 if (callchain) {
886                         fprintf(stderr, "selected -g but no callchain data."
887                                         " Did you call perf record without"
888                                         " -g?\n");
889                         return -1;
890                 }
891         } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
892                         callchain = 1;
893                         if (register_callchain_param(&callchain_param) < 0) {
894                                 fprintf(stderr, "Can't register callchain"
895                                                 " params\n");
896                                 return -1;
897                         }
898         }
899
900         return 0;
901 }
902
903 static struct perf_file_handler file_handler = {
904         .process_sample_event   = process_sample_event,
905         .process_mmap_event     = process_mmap_event,
906         .process_comm_event     = process_comm_event,
907         .process_exit_event     = process_task_event,
908         .process_fork_event     = process_task_event,
909         .process_lost_event     = process_lost_event,
910         .process_read_event     = process_read_event,
911         .sample_type_check      = sample_type_check,
912 };
913
914
915 static int __cmd_report(void)
916 {
917         struct thread *idle;
918         int ret;
919
920         idle = register_idle_thread();
921         thread__comm_adjust(idle);
922
923         if (show_threads)
924                 perf_read_values_init(&show_threads_values);
925
926         register_perf_file_handler(&file_handler);
927
928         ret = mmap_dispatch_perf_file(&header, input_name, force, full_paths,
929                                       &cwdlen, &cwd);
930         if (ret)
931                 return ret;
932
933         dump_printf("      IP events: %10ld\n", total);
934         dump_printf("    mmap events: %10ld\n", total_mmap);
935         dump_printf("    comm events: %10ld\n", total_comm);
936         dump_printf("    fork events: %10ld\n", total_fork);
937         dump_printf("    lost events: %10ld\n", total_lost);
938         dump_printf(" unknown events: %10ld\n", file_handler.total_unknown);
939
940         if (dump_trace)
941                 return 0;
942
943         if (verbose > 3)
944                 threads__fprintf(stdout);
945
946         if (verbose > 2)
947                 dsos__fprintf(stdout);
948
949         collapse__resort();
950         output__resort(total);
951         output__fprintf(stdout, total);
952
953         if (show_threads)
954                 perf_read_values_destroy(&show_threads_values);
955
956         return ret;
957 }
958
959 static int
960 parse_callchain_opt(const struct option *opt __used, const char *arg,
961                     int unset __used)
962 {
963         char *tok;
964         char *endptr;
965
966         callchain = 1;
967
968         if (!arg)
969                 return 0;
970
971         tok = strtok((char *)arg, ",");
972         if (!tok)
973                 return -1;
974
975         /* get the output mode */
976         if (!strncmp(tok, "graph", strlen(arg)))
977                 callchain_param.mode = CHAIN_GRAPH_ABS;
978
979         else if (!strncmp(tok, "flat", strlen(arg)))
980                 callchain_param.mode = CHAIN_FLAT;
981
982         else if (!strncmp(tok, "fractal", strlen(arg)))
983                 callchain_param.mode = CHAIN_GRAPH_REL;
984
985         else if (!strncmp(tok, "none", strlen(arg))) {
986                 callchain_param.mode = CHAIN_NONE;
987                 callchain = 0;
988
989                 return 0;
990         }
991
992         else
993                 return -1;
994
995         /* get the min percentage */
996         tok = strtok(NULL, ",");
997         if (!tok)
998                 goto setup;
999
1000         callchain_param.min_percent = strtod(tok, &endptr);
1001         if (tok == endptr)
1002                 return -1;
1003
1004 setup:
1005         if (register_callchain_param(&callchain_param) < 0) {
1006                 fprintf(stderr, "Can't register callchain params\n");
1007                 return -1;
1008         }
1009         return 0;
1010 }
1011
1012 //static const char * const report_usage[] = {
1013 const char * const report_usage[] = {
1014         "perf report [<options>] <command>",
1015         NULL
1016 };
1017
1018 static const struct option options[] = {
1019         OPT_STRING('i', "input", &input_name, "file",
1020                     "input file name"),
1021         OPT_BOOLEAN('v', "verbose", &verbose,
1022                     "be more verbose (show symbol address, etc)"),
1023         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1024                     "dump raw trace in ASCII"),
1025         OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1026         OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1027         OPT_BOOLEAN('m', "modules", &use_modules,
1028                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
1029         OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1030                     "Show a column with the number of samples"),
1031         OPT_BOOLEAN('T', "threads", &show_threads,
1032                     "Show per-thread event counters"),
1033         OPT_STRING(0, "pretty", &pretty_printing_style, "key",
1034                    "pretty printing style key: normal raw"),
1035         OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1036                    "sort by key(s): pid, comm, dso, symbol, parent"),
1037         OPT_BOOLEAN('P', "full-paths", &full_paths,
1038                     "Don't shorten the pathnames taking into account the cwd"),
1039         OPT_STRING('p', "parent", &parent_pattern, "regex",
1040                    "regex filter to identify parent, see: '--sort parent'"),
1041         OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1042                     "Only display entries with parent-match"),
1043         OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
1044                      "Display callchains using output_type and min percent threshold. "
1045                      "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
1046         OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1047                    "only consider symbols in these dsos"),
1048         OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1049                    "only consider symbols in these comms"),
1050         OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1051                    "only consider these symbols"),
1052         OPT_STRING('w', "column-widths", &col_width_list_str,
1053                    "width[,width...]",
1054                    "don't try to adjust column width, use these fixed values"),
1055         OPT_STRING('t', "field-separator", &field_sep, "separator",
1056                    "separator for columns, no spaces will be added between "
1057                    "columns '.' is reserved."),
1058         OPT_END()
1059 };
1060
1061 static void setup_sorting(void)
1062 {
1063         char *tmp, *tok, *str = strdup(sort_order);
1064
1065         for (tok = strtok_r(str, ", ", &tmp);
1066                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
1067                 if (sort_dimension__add(tok) < 0) {
1068                         error("Unknown --sort key: `%s'", tok);
1069                         usage_with_options(report_usage, options);
1070                 }
1071         }
1072
1073         free(str);
1074 }
1075
1076 static void setup_list(struct strlist **list, const char *list_str,
1077                        struct sort_entry *se, const char *list_name,
1078                        FILE *fp)
1079 {
1080         if (list_str) {
1081                 *list = strlist__new(true, list_str);
1082                 if (!*list) {
1083                         fprintf(stderr, "problems parsing %s list\n",
1084                                 list_name);
1085                         exit(129);
1086                 }
1087                 if (strlist__nr_entries(*list) == 1) {
1088                         fprintf(fp, "# %s: %s\n", list_name,
1089                                 strlist__entry(*list, 0)->s);
1090                         se->elide = true;
1091                 }
1092         }
1093 }
1094
1095 int cmd_report(int argc, const char **argv, const char *prefix __used)
1096 {
1097         symbol__init(0);
1098
1099         argc = parse_options(argc, argv, options, report_usage, 0);
1100
1101         setup_sorting();
1102
1103         if (parent_pattern != default_parent_pattern) {
1104                 sort_dimension__add("parent");
1105                 sort_parent.elide = 1;
1106         } else
1107                 exclude_other = 0;
1108
1109         /*
1110          * Any (unrecognized) arguments left?
1111          */
1112         if (argc)
1113                 usage_with_options(report_usage, options);
1114
1115         setup_pager();
1116
1117         setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
1118         setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
1119         setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
1120
1121         if (field_sep && *field_sep == '.') {
1122                 fputs("'.' is the only non valid --field-separator argument\n",
1123                       stderr);
1124                 exit(129);
1125         }
1126
1127         return __cmd_report();
1128 }