]> git.karo-electronics.de Git - mv-sheeva.git/blob - Documentation/perf_counter/builtin-report.c
perf report: Use hex2long instead of sscanf
[mv-sheeva.git] / Documentation / perf_counter / builtin-report.c
1 #include "util/util.h"
2
3 #include <libelf.h>
4 #include <gelf.h>
5 #include <elf.h>
6
7 #include "util/list.h"
8 #include "util/rbtree.h"
9
10 #include "perf.h"
11
12 #include "util/parse-options.h"
13 #include "util/parse-events.h"
14
15 #define SHOW_KERNEL     1
16 #define SHOW_USER       2
17 #define SHOW_HV         4
18
19 static char             const *input_name = "output.perf";
20 static int              input;
21 static int              show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
22
23 static int              dump_trace = 0;
24
25 static unsigned long    page_size;
26 static unsigned long    mmap_window = 32;
27
28 const char *perf_event_names[] = {
29         [PERF_EVENT_MMAP]   = " PERF_EVENT_MMAP",
30         [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
31         [PERF_EVENT_COMM]   = " PERF_EVENT_COMM",
32 };
33
34 struct ip_event {
35         struct perf_event_header header;
36         __u64 ip;
37         __u32 pid, tid;
38 };
39 struct mmap_event {
40         struct perf_event_header header;
41         __u32 pid, tid;
42         __u64 start;
43         __u64 len;
44         __u64 pgoff;
45         char filename[PATH_MAX];
46 };
47 struct comm_event {
48         struct perf_event_header header;
49         __u32 pid,tid;
50         char comm[16];
51 };
52
53 typedef union event_union {
54         struct perf_event_header header;
55         struct ip_event ip;
56         struct mmap_event mmap;
57         struct comm_event comm;
58 } event_t;
59
60 struct symbol {
61         struct rb_node rb_node;
62         uint64_t       start;
63         uint64_t       end;
64         char           name[0];
65 };
66
67 static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
68 {
69         struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
70
71         if (self != NULL) {
72                 self->start = start;
73                 self->end   = start + len;
74                 strcpy(self->name, name);
75         }
76
77         return self;
78 }
79
80 static void symbol__delete(struct symbol *self)
81 {
82         free(self);
83 }
84
85 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
86 {
87         return fprintf(fp, " %lx-%lx %s\n",
88                        self->start, self->end, self->name);
89 }
90
91 struct dso {
92         struct list_head node;
93         struct rb_root   syms;
94         char             name[0];
95 };
96
97 static struct dso *dso__new(const char *name)
98 {
99         struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
100
101         if (self != NULL) {
102                 strcpy(self->name, name);
103                 self->syms = RB_ROOT;
104         }
105
106         return self;
107 }
108
109 static void dso__delete_symbols(struct dso *self)
110 {
111         struct symbol *pos;
112         struct rb_node *next = rb_first(&self->syms);
113
114         while (next) {
115                 pos = rb_entry(next, struct symbol, rb_node);
116                 next = rb_next(&pos->rb_node);
117                 symbol__delete(pos);
118         }
119 }
120
121 static void dso__delete(struct dso *self)
122 {
123         dso__delete_symbols(self);
124         free(self);
125 }
126
127 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
128 {
129         struct rb_node **p = &self->syms.rb_node;
130         struct rb_node *parent = NULL;
131         const uint64_t ip = sym->start;
132         struct symbol *s;
133
134         while (*p != NULL) {
135                 parent = *p;
136                 s = rb_entry(parent, struct symbol, rb_node);
137                 if (ip < s->start)
138                         p = &(*p)->rb_left;
139                 else
140                         p = &(*p)->rb_right;
141         }
142         rb_link_node(&sym->rb_node, parent, p);
143         rb_insert_color(&sym->rb_node, &self->syms);
144 }
145
146 static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
147 {
148         if (self == NULL)
149                 return NULL;
150
151         struct rb_node *n = self->syms.rb_node;
152
153         while (n) {
154                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
155
156                 if (ip < s->start)
157                         n = n->rb_left;
158                 else if (ip > s->end)
159                         n = n->rb_right;
160                 else
161                         return s;
162         }
163
164         return NULL;
165 }
166
167 /**
168  * elf_symtab__for_each_symbol - iterate thru all the symbols
169  *
170  * @self: struct elf_symtab instance to iterate
171  * @index: uint32_t index
172  * @sym: GElf_Sym iterator
173  */
174 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
175         for (index = 0, gelf_getsym(syms, index, &sym);\
176              index < nr_syms; \
177              index++, gelf_getsym(syms, index, &sym))
178
179 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
180 {
181         return GELF_ST_TYPE(sym->st_info);
182 }
183
184 static inline int elf_sym__is_function(const GElf_Sym *sym)
185 {
186         return elf_sym__type(sym) == STT_FUNC &&
187                sym->st_name != 0 &&
188                sym->st_shndx != SHN_UNDEF;
189 }
190
191 static inline const char *elf_sym__name(const GElf_Sym *sym,
192                                         const Elf_Data *symstrs)
193 {
194         return symstrs->d_buf + sym->st_name;
195 }
196
197 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
198                                     GElf_Shdr *shp, const char *name,
199                                     size_t *index)
200 {
201         Elf_Scn *sec = NULL;
202         size_t cnt = 1;
203
204         while ((sec = elf_nextscn(elf, sec)) != NULL) {
205                 char *str;
206
207                 gelf_getshdr(sec, shp);
208                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
209                 if (!strcmp(name, str)) {
210                         if (index)
211                                 *index = cnt;
212                         break;
213                 }
214                 ++cnt;
215         }
216
217         return sec;
218 }
219
220 static int dso__load(struct dso *self)
221 {
222         int fd = open(self->name, O_RDONLY), err = -1;
223
224         if (fd == -1)
225                 return -1;
226
227         Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
228         if (elf == NULL) {
229                 fprintf(stderr, "%s: cannot read %s ELF file.\n",
230                         __func__, self->name);
231                 goto out_close;
232         }
233
234         GElf_Ehdr ehdr;
235         if (gelf_getehdr(elf, &ehdr) == NULL) {
236                 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
237                 goto out_elf_end;
238         }
239
240         GElf_Shdr shdr;
241         Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
242         if (sec == NULL)
243                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
244
245         if (sec == NULL)
246                 goto out_elf_end;
247
248         Elf_Data *syms = elf_getdata(sec, NULL);
249         if (syms == NULL)
250                 goto out_elf_end;
251
252         sec = elf_getscn(elf, shdr.sh_link);
253         if (sec == NULL)
254                 goto out_elf_end;
255
256         Elf_Data *symstrs = elf_getdata(sec, NULL);
257         if (symstrs == NULL)
258                 goto out_elf_end;
259
260         const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
261
262         GElf_Sym sym;
263         uint32_t index;
264         elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
265                 struct symbol *f;
266
267                 if (!elf_sym__is_function(&sym))
268                         continue;
269
270                 sec = elf_getscn(elf, sym.st_shndx);
271                 if (!sec)
272                         goto out_elf_end;
273
274                 gelf_getshdr(sec, &shdr);
275                 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
276
277                 f = symbol__new(sym.st_value, sym.st_size,
278                                 elf_sym__name(&sym, symstrs));
279                 if (!f)
280                         goto out_elf_end;
281
282                 dso__insert_symbol(self, f);
283         }
284
285         err = 0;
286 out_elf_end:
287         elf_end(elf);
288 out_close:
289         close(fd);
290         return err;
291 }
292
293 static size_t dso__fprintf(struct dso *self, FILE *fp)
294 {
295         size_t ret = fprintf(fp, "dso: %s\n", self->name);
296
297         struct rb_node *nd;
298         for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
299                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
300                 ret += symbol__fprintf(pos, fp);
301         }
302
303         return ret;
304 }
305
306 static LIST_HEAD(dsos);
307 static struct dso *kernel_dso;
308
309 static void dsos__add(struct dso *dso)
310 {
311         list_add_tail(&dso->node, &dsos);
312 }
313
314 static struct dso *dsos__find(const char *name)
315 {
316         struct dso *pos;
317
318         list_for_each_entry(pos, &dsos, node)
319                 if (strcmp(pos->name, name) == 0)
320                         return pos;
321         return NULL;
322 }
323
324 static struct dso *dsos__findnew(const char *name)
325 {
326         struct dso *dso = dsos__find(name);
327
328         if (dso == NULL) {
329                 dso = dso__new(name);
330                 if (dso != NULL && dso__load(dso) < 0)
331                         goto out_delete_dso;
332
333                 dsos__add(dso);
334         }
335
336         return dso;
337
338 out_delete_dso:
339         dso__delete(dso);
340         return NULL;
341 }
342
343 void dsos__fprintf(FILE *fp)
344 {
345         struct dso *pos;
346
347         list_for_each_entry(pos, &dsos, node)
348                 dso__fprintf(pos, fp);
349 }
350
351 static int hex(char ch)
352 {
353         if ((ch >= '0') && (ch <= '9'))
354                 return ch - '0';
355         if ((ch >= 'a') && (ch <= 'f'))
356                 return ch - 'a' + 10;
357         if ((ch >= 'A') && (ch <= 'F'))
358                 return ch - 'A' + 10;
359         return -1;
360 }
361
362 /*
363  * While we find nice hex chars, build a long_val.
364  * Return number of chars processed.
365  */
366 int hex2long(char *ptr, unsigned long *long_val)
367 {
368         const char *p = ptr;
369         *long_val = 0;
370
371         while (*p) {
372                 const int hex_val = hex(*p);
373
374                 if (hex_val < 0)
375                         break;
376
377                 *long_val = (*long_val << 4) | hex_val;
378                 p++;
379         }
380
381         return p - ptr;
382 }
383
384 static int load_kallsyms(void)
385 {
386         kernel_dso = dso__new("[kernel]");
387         if (kernel_dso == NULL)
388                 return -1;
389
390         FILE *file = fopen("/proc/kallsyms", "r");
391
392         if (file == NULL)
393                 goto out_delete_dso;
394
395         char *line = NULL;
396         size_t n;
397
398         while (!feof(file)) {
399                 unsigned long start;
400                 int line_len = getline(&line, &n, file);
401                 if (line_len < 0)
402                         break;
403
404                 if (!line)
405                         goto out_delete_dso;
406
407                 line[--line_len] = '\0'; /* \n */
408
409                 int len = hex2long(line, &start);
410                 
411                 len += 3; /* ' t ' */
412                 if (len >= line_len)
413                         continue;
414                 /*
415                  * Well fix up the end later, when we have all sorted.
416                  */
417                 struct symbol *sym = symbol__new(start, 0xdead, line + len);
418
419                 if (sym == NULL)
420                         goto out_delete_dso;
421
422                 dso__insert_symbol(kernel_dso, sym);
423         }
424
425         /*
426          * Now that we have all sorted out, just set the ->end of all
427          * symbols
428          */
429         struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms);
430
431         if (prevnd == NULL)
432                 goto out_delete_line;
433
434         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
435                 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
436                               *curr = rb_entry(nd, struct symbol, rb_node);
437
438                 prev->end = curr->start - 1;
439                 prevnd = nd;
440         }
441
442         dsos__add(kernel_dso);
443         free(line);
444         fclose(file);
445         return 0;
446
447 out_delete_line:
448         free(line);
449 out_delete_dso:
450         dso__delete(kernel_dso);
451         return -1;
452 }
453
454 struct map {
455         struct list_head node;
456         uint64_t         start;
457         uint64_t         end;
458         uint64_t         pgoff;
459         struct dso       *dso;
460 };
461
462 static struct map *map__new(struct mmap_event *event)
463 {
464         struct map *self = malloc(sizeof(*self));
465
466         if (self != NULL) {
467                 self->start = event->start;
468                 self->end   = event->start + event->len;
469                 self->pgoff = event->pgoff;
470
471                 self->dso = dsos__findnew(event->filename);
472                 if (self->dso == NULL)
473                         goto out_delete;
474         }
475         return self;
476 out_delete:
477         free(self);
478         return NULL;
479 }
480
481 static size_t map__fprintf(struct map *self, FILE *fp)
482 {
483         return fprintf(fp, " %lx-%lx %lx %s\n",
484                        self->start, self->end, self->pgoff, self->dso->name);
485 }
486
487 struct thread;
488
489 static const char *thread__name(struct thread *self, char *bf, size_t size);
490
491 struct symhist {
492         struct rb_node   rb_node;
493         struct dso       *dso;
494         struct symbol    *sym;
495         struct thread    *thread;
496         uint64_t         ip;
497         uint32_t         count;
498         char             level;
499 };
500
501 static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
502                                     struct thread *thread, struct dso *dso,
503                                     char level)
504 {
505         struct symhist *self = malloc(sizeof(*self));
506
507         if (self != NULL) {
508                 self->sym    = sym;
509                 self->thread = thread;
510                 self->ip     = ip;
511                 self->dso    = dso;
512                 self->level  = level;
513                 self->count  = 1;
514         }
515
516         return self;
517 }
518
519 void symhist__delete(struct symhist *self)
520 {
521         free(self);
522 }
523
524 static void symhist__inc(struct symhist *self)
525 {
526         ++self->count;
527 }
528
529 static size_t
530 symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp)
531 {
532         char bf[32];
533         size_t ret;
534
535         if (total_samples)
536                 ret = fprintf(fp, "%5.2f", (self->count * 100.0) / total_samples);
537         else
538                 ret = fprintf(fp, "%12d", self->count);
539
540         ret += fprintf(fp, "%14s [%c] %#018llx ",
541                        thread__name(self->thread, bf, sizeof(bf)),
542                        self->level, (unsigned long long)self->ip);
543
544         if (self->level != '.')
545                 ret += fprintf(fp, "%s\n",
546                                self->sym ? self->sym->name : "<unknown>");
547         else
548                 ret += fprintf(fp, "%s: %s\n",
549                                self->dso ? self->dso->name : "<unknown>",
550                                self->sym ? self->sym->name : "<unknown>");
551         return ret;
552 }
553
554 struct thread {
555         struct rb_node   rb_node;
556         struct list_head maps;
557         struct rb_root   symhists;
558         pid_t            pid;
559         char             *comm;
560 };
561
562 static const char *thread__name(struct thread *self, char *bf, size_t size)
563 {
564         if (self->comm)
565                 return self->comm;
566
567         snprintf(bf, sizeof(bf), ":%u", self->pid);
568         return bf;
569 }
570
571 static struct thread *thread__new(pid_t pid)
572 {
573         struct thread *self = malloc(sizeof(*self));
574
575         if (self != NULL) {
576                 self->pid = pid;
577                 self->comm = NULL;
578                 INIT_LIST_HEAD(&self->maps);
579                 self->symhists = RB_ROOT;
580         }
581
582         return self;
583 }
584
585 static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
586                                  uint64_t ip, struct dso *dso, char level)
587 {
588         struct rb_node **p = &self->symhists.rb_node;
589         struct rb_node *parent = NULL;
590         struct symhist *sh;
591
592         while (*p != NULL) {
593                 parent = *p;
594                 sh = rb_entry(parent, struct symhist, rb_node);
595
596                 if (sh->sym == sym || ip == sh->ip) {
597                         symhist__inc(sh);
598                         return 0;
599                 }
600
601                 /* Handle unresolved symbols too */
602                 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
603
604                 if (ip < start)
605                         p = &(*p)->rb_left;
606                 else
607                         p = &(*p)->rb_right;
608         }
609
610         sh = symhist__new(sym, ip, self, dso, level);
611         if (sh == NULL)
612                 return -ENOMEM;
613         rb_link_node(&sh->rb_node, parent, p);
614         rb_insert_color(&sh->rb_node, &self->symhists);
615         return 0;
616 }
617
618 static int thread__set_comm(struct thread *self, const char *comm)
619 {
620         self->comm = strdup(comm);
621         return self->comm ? 0 : -ENOMEM;
622 }
623
624 size_t thread__maps_fprintf(struct thread *self, FILE *fp)
625 {
626         struct map *pos;
627         size_t ret = 0;
628
629         list_for_each_entry(pos, &self->maps, node)
630                 ret += map__fprintf(pos, fp);
631
632         return ret;
633 }
634
635 static size_t thread__fprintf(struct thread *self, FILE *fp)
636 {
637         int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
638         struct rb_node *nd;
639
640         for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
641                 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
642                 ret += symhist__fprintf(pos, 0, fp);
643         }
644
645         return ret;
646 }
647
648 static struct rb_root threads = RB_ROOT;
649
650 static struct thread *threads__findnew(pid_t pid)
651 {
652         struct rb_node **p = &threads.rb_node;
653         struct rb_node *parent = NULL;
654         struct thread *th;
655
656         while (*p != NULL) {
657                 parent = *p;
658                 th = rb_entry(parent, struct thread, rb_node);
659
660                 if (th->pid == pid)
661                         return th;
662
663                 if (pid < th->pid)
664                         p = &(*p)->rb_left;
665                 else
666                         p = &(*p)->rb_right;
667         }
668
669         th = thread__new(pid);
670         if (th != NULL) {
671                 rb_link_node(&th->rb_node, parent, p);
672                 rb_insert_color(&th->rb_node, &threads);
673         }
674         return th;
675 }
676
677 static void thread__insert_map(struct thread *self, struct map *map)
678 {
679         list_add_tail(&map->node, &self->maps);
680 }
681
682 static struct map *thread__find_map(struct thread *self, uint64_t ip)
683 {
684         if (self == NULL)
685                 return NULL;
686
687         struct map *pos;
688
689         list_for_each_entry(pos, &self->maps, node)
690                 if (ip >= pos->start && ip <= pos->end)
691                         return pos;
692
693         return NULL;
694 }
695
696 void threads__fprintf(FILE *fp)
697 {
698         struct rb_node *nd;
699         for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
700                 struct thread *pos = rb_entry(nd, struct thread, rb_node);
701                 thread__fprintf(pos, fp);
702         }
703 }
704
705 static struct rb_root global_symhists = RB_ROOT;
706
707 static void threads__insert_symhist(struct symhist *sh)
708 {
709         struct rb_node **p = &global_symhists.rb_node;
710         struct rb_node *parent = NULL;
711         struct symhist *iter;
712
713         while (*p != NULL) {
714                 parent = *p;
715                 iter = rb_entry(parent, struct symhist, rb_node);
716
717                 /* Reverse order */
718                 if (sh->count > iter->count)
719                         p = &(*p)->rb_left;
720                 else
721                         p = &(*p)->rb_right;
722         }
723
724         rb_link_node(&sh->rb_node, parent, p);
725         rb_insert_color(&sh->rb_node, &global_symhists);
726 }
727
728 static void threads__sort_symhists(void)
729 {
730         struct rb_node *nd;
731
732         for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
733                 struct thread *thread = rb_entry(nd, struct thread, rb_node);
734                 struct rb_node *next = rb_first(&thread->symhists);
735
736                 while (next) {
737                         struct symhist *n = rb_entry(next, struct symhist,
738                                                      rb_node);
739                         next = rb_next(&n->rb_node);
740                         rb_erase(&n->rb_node, &thread->symhists);
741                         threads__insert_symhist(n);
742                 }
743
744         }
745 }
746
747 static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
748 {
749         struct rb_node *nd;
750         size_t ret = 0;
751
752         for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
753                 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
754                 ret += symhist__fprintf(pos, total_samples, fp);
755         }
756
757         return ret;
758 }
759
760 static int __cmd_report(void)
761 {
762         unsigned long offset = 0;
763         unsigned long head = 0;
764         struct stat stat;
765         char *buf;
766         event_t *event;
767         int ret, rc = EXIT_FAILURE;
768         uint32_t size;
769         unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
770
771         input = open(input_name, O_RDONLY);
772         if (input < 0) {
773                 perror("failed to open file");
774                 exit(-1);
775         }
776
777         ret = fstat(input, &stat);
778         if (ret < 0) {
779                 perror("failed to stat file");
780                 exit(-1);
781         }
782
783         if (!stat.st_size) {
784                 fprintf(stderr, "zero-sized file, nothing to do!\n");
785                 exit(0);
786         }
787
788         if (load_kallsyms() < 0) {
789                 perror("failed to open kallsyms");
790                 return EXIT_FAILURE;
791         }
792
793 remap:
794         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
795                            MAP_SHARED, input, offset);
796         if (buf == MAP_FAILED) {
797                 perror("failed to mmap file");
798                 exit(-1);
799         }
800
801 more:
802         event = (event_t *)(buf + head);
803
804         size = event->header.size;
805         if (!size)
806                 size = 8;
807
808         if (head + event->header.size >= page_size * mmap_window) {
809                 unsigned long shift = page_size * (head / page_size);
810                 int ret;
811
812                 ret = munmap(buf, page_size * mmap_window);
813                 assert(ret == 0);
814
815                 offset += shift;
816                 head -= shift;
817                 goto remap;
818         }
819
820         size = event->header.size;
821         if (!size)
822                 goto broken_event;
823
824         if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
825                 char level;
826                 int show = 0;
827                 struct dso *dso = NULL;
828                 struct thread *thread = threads__findnew(event->ip.pid);
829                 uint64_t ip = event->ip.ip;
830
831                 if (dump_trace) {
832                         fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
833                                 (void *)(offset + head),
834                                 (void *)(long)(event->header.size),
835                                 event->header.misc,
836                                 event->ip.pid,
837                                 (void *)event->ip.ip);
838                 }
839
840                 if (thread == NULL) {
841                         fprintf(stderr, "problem processing %d event, bailing out\n",
842                                 event->header.type);
843                         goto done;
844                 }
845
846                 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
847                         show = SHOW_KERNEL;
848                         level = 'k';
849                         dso = kernel_dso;
850                 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
851                         show = SHOW_USER;
852                         level = '.';
853                         struct map *map = thread__find_map(thread, ip);
854                         if (map != NULL) {
855                                 dso = map->dso;
856                                 ip -= map->start + map->pgoff;
857                         }
858                 } else {
859                         show = SHOW_HV;
860                         level = 'H';
861                 }
862
863                 if (show & show_mask) {
864                         struct symbol *sym = dso__find_symbol(dso, ip);
865
866                         if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
867                                 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
868                                 goto done;
869                         }
870                 }
871                 total++;
872         } else switch (event->header.type) {
873         case PERF_EVENT_MMAP: {
874                 struct thread *thread = threads__findnew(event->mmap.pid);
875                 struct map *map = map__new(&event->mmap);
876
877                 if (dump_trace) {
878                         fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
879                                 (void *)(offset + head),
880                                 (void *)(long)(event->header.size),
881                                 (void *)event->mmap.start,
882                                 (void *)event->mmap.len,
883                                 (void *)event->mmap.pgoff,
884                                 event->mmap.filename);
885                 }
886                 if (thread == NULL || map == NULL) {
887                         fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
888                         goto done;
889                 }
890                 thread__insert_map(thread, map);
891                 total_mmap++;
892                 break;
893         }
894         case PERF_EVENT_COMM: {
895                 struct thread *thread = threads__findnew(event->comm.pid);
896
897                 if (dump_trace) {
898                         fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
899                                 (void *)(offset + head),
900                                 (void *)(long)(event->header.size),
901                                 event->comm.comm, event->comm.pid);
902                 }
903                 if (thread == NULL ||
904                     thread__set_comm(thread, event->comm.comm)) {
905                         fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
906                         goto done;
907                 }
908                 total_comm++;
909                 break;
910         }
911         default: {
912 broken_event:
913                 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
914                         (void *)(offset + head),
915                         (void *)(long)(event->header.size),
916                         event->header.type);
917                 total_unknown++;
918
919                 /*
920                  * assume we lost track of the stream, check alignment, and
921                  * increment a single u64 in the hope to catch on again 'soon'.
922                  */
923
924                 if (unlikely(head & 7))
925                         head &= ~7ULL;
926
927                 size = 8;
928         }
929         }
930
931         head += size;
932
933         if (offset + head < stat.st_size)
934                 goto more;
935
936         rc = EXIT_SUCCESS;
937 done:
938         close(input);
939
940         if (dump_trace) {
941                 fprintf(stderr, "      IP events: %10ld\n", total);
942                 fprintf(stderr, "    mmap events: %10ld\n", total_mmap);
943                 fprintf(stderr, "    comm events: %10ld\n", total_comm);
944                 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
945
946                 return 0;
947         }
948
949         threads__sort_symhists();
950         threads__symhists_fprintf(total, stdout);
951
952         return rc;
953 }
954
955 static const char * const report_usage[] = {
956         "perf report [<options>] <command>",
957         NULL
958 };
959
960 static const struct option options[] = {
961         OPT_STRING('i', "input", &input_name, "file",
962                     "input file name"),
963         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
964                     "dump raw trace in ASCII"),
965         OPT_END()
966 };
967
968 int cmd_report(int argc, const char **argv, const char *prefix)
969 {
970         elf_version(EV_CURRENT);
971
972         page_size = getpagesize();
973
974         parse_options(argc, argv, options, report_usage, 0);
975
976         return __cmd_report();
977 }