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