perf_counter: Implement dso__load using libelf
[linux-2.6] / Documentation / perf_counter / builtin-report.c
1 #define _GNU_SOURCE
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/time.h>
5 #include <unistd.h>
6 #include <stdint.h>
7 #include <stdbool.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <limits.h>
11 #include <gelf.h>
12 #include <elf.h>
13 #include <libelf.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <ctype.h>
18 #include <time.h>
19 #include <getopt.h>
20 #include <assert.h>
21 #include <search.h>
22
23 #include <sys/ioctl.h>
24 #include <sys/poll.h>
25 #include <sys/prctl.h>
26 #include <sys/wait.h>
27 #include <sys/mman.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 #include <linux/unistd.h>
32 #include <linux/types.h>
33
34 #include "../../include/linux/perf_counter.h"
35 #include "list.h"
36
37 #define SHOW_KERNEL     1
38 #define SHOW_USER       2
39 #define SHOW_HV         4
40
41 static char             const *input_name = "output.perf";
42 static int              input;
43 static int              show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
44
45 static unsigned long    page_size;
46 static unsigned long    mmap_window = 32;
47
48 static const char *perf_event_names[] = {
49         [PERF_EVENT_MMAP]   = " PERF_EVENT_MMAP",
50         [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
51         [PERF_EVENT_COMM]   = " PERF_EVENT_COMM",
52 };
53
54 struct ip_event {
55         struct perf_event_header header;
56         __u64 ip;
57         __u32 pid, tid;
58 };
59 struct mmap_event {
60         struct perf_event_header header;
61         __u32 pid, tid;
62         __u64 start;
63         __u64 len;
64         __u64 pgoff;
65         char filename[PATH_MAX];
66 };
67 struct comm_event {
68         struct perf_event_header header;
69         __u32 pid,tid;
70         char comm[16];
71 };
72
73 typedef union event_union {
74         struct perf_event_header header;
75         struct ip_event ip;
76         struct mmap_event mmap;
77         struct comm_event comm;
78 } event_t;
79
80 struct section {
81         struct list_head node;
82         uint64_t         start;
83         uint64_t         end;
84         uint64_t         offset;
85         char             name[0];
86 };
87
88 static struct section *section__new(uint64_t start, uint64_t size,
89                                     uint64_t offset, char *name)
90 {
91         struct section *self = malloc(sizeof(*self) + strlen(name) + 1);
92
93         if (self != NULL) {
94                 self->start  = start;
95                 self->end    = start + size;
96                 self->offset = offset;
97                 strcpy(self->name, name);
98         }
99
100         return self;
101 }
102
103 static void section__delete(struct section *self)
104 {
105         free(self);
106 }
107
108 struct symbol {
109         struct list_head node;
110         uint64_t         start;
111         uint64_t         end;
112         char             name[0];
113 };
114
115 static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
116 {
117         struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
118
119         if (self != NULL) {
120                 self->start = start;
121                 self->end   = start + len;
122                 strcpy(self->name, name);
123         }
124
125         return self;
126 }
127
128 static void symbol__delete(struct symbol *self)
129 {
130         free(self);
131 }
132
133 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
134 {
135         return fprintf(fp, " %lx-%lx %s\n",
136                        self->start, self->end, self->name);
137 }
138
139 struct dso {
140         struct list_head node;
141         struct list_head sections;
142         struct list_head syms;
143         char             name[0];
144 };
145
146 static struct dso *dso__new(const char *name)
147 {
148         struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
149
150         if (self != NULL) {
151                 strcpy(self->name, name);
152                 INIT_LIST_HEAD(&self->sections);
153                 INIT_LIST_HEAD(&self->syms);
154         }
155
156         return self;
157 }
158
159 static void dso__delete_sections(struct dso *self)
160 {
161         struct section *pos, *n;
162
163         list_for_each_entry_safe(pos, n, &self->sections, node)
164                 section__delete(pos);
165 }
166
167 static void dso__delete_symbols(struct dso *self)
168 {
169         struct symbol *pos, *n;
170
171         list_for_each_entry_safe(pos, n, &self->syms, node)
172                 symbol__delete(pos);
173 }
174
175 static void dso__delete(struct dso *self)
176 {
177         dso__delete_sections(self);
178         dso__delete_symbols(self);
179         free(self);
180 }
181
182 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
183 {
184         list_add_tail(&sym->node, &self->syms);
185 }
186
187 static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
188 {
189         if (self == NULL)
190                 return NULL;
191
192         struct symbol *pos;
193
194         list_for_each_entry(pos, &self->syms, node)
195                 if (ip >= pos->start && ip <= pos->end)
196                         return pos;
197
198         return NULL;
199 }
200
201 /**
202  * elf_symtab__for_each_symbol - iterate thru all the symbols
203  *
204  * @self: struct elf_symtab instance to iterate
205  * @index: uint32_t index
206  * @sym: GElf_Sym iterator
207  */
208 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
209         for (index = 0, gelf_getsym(syms, index, &sym);\
210              index < nr_syms; \
211              index++, gelf_getsym(syms, index, &sym))
212
213 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
214 {
215         return GELF_ST_TYPE(sym->st_info);
216 }
217
218 static inline bool elf_sym__is_function(const GElf_Sym *sym)
219 {
220         return elf_sym__type(sym) == STT_FUNC &&
221                sym->st_name != 0 &&
222                sym->st_shndx != SHN_UNDEF;
223 }
224
225 static inline const char *elf_sym__name(const GElf_Sym *sym,
226                                         const Elf_Data *symstrs)
227 {
228         return symstrs->d_buf + sym->st_name;
229 }
230
231 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
232                                     GElf_Shdr *shp, const char *name,
233                                     size_t *index)
234 {
235         Elf_Scn *sec = NULL;
236         size_t cnt = 1;
237
238         while ((sec = elf_nextscn(elf, sec)) != NULL) {
239                 char *str;
240
241                 gelf_getshdr(sec, shp);
242                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
243                 if (!strcmp(name, str)) {
244                         if (index)
245                                 *index = cnt;
246                         break;
247                 }
248                 ++cnt;
249         }
250
251         return sec;
252 }
253
254 static int dso__load(struct dso *self)
255 {
256         int fd = open(self->name, O_RDONLY), err = -1;
257
258         if (fd == -1)
259                 return -1;
260
261         Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
262         if (elf == NULL) {
263                 fprintf(stderr, "%s: cannot read %s ELF file.\n",
264                         __func__, self->name);
265                 goto out_close;
266         }
267
268         GElf_Ehdr ehdr;
269         if (gelf_getehdr(elf, &ehdr) == NULL) {
270                 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
271                 goto out_elf_end;
272         }
273
274         GElf_Shdr shdr;
275         Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
276         if (sec == NULL)
277                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
278
279         if (sec == NULL)
280                 goto out_elf_end;
281
282         if (gelf_getshdr(sec, &shdr) == NULL)
283                 goto out_elf_end;
284
285         Elf_Data *syms = elf_getdata(sec, NULL);
286         if (syms == NULL)
287                 goto out_elf_end;
288
289         sec = elf_getscn(elf, shdr.sh_link);
290         if (sec == NULL)
291                 goto out_elf_end;
292
293         Elf_Data *symstrs = elf_getdata(sec, NULL);
294         if (symstrs == NULL)
295                 goto out_elf_end;
296
297         const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
298
299         GElf_Sym sym;
300         uint32_t index;
301         elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
302                 if (!elf_sym__is_function(&sym))
303                         continue;
304                 struct symbol *f = symbol__new(sym.st_value, sym.st_size,
305                                                elf_sym__name(&sym, symstrs));
306                 if (f == NULL)
307                         goto out_elf_end;
308
309                 dso__insert_symbol(self, f);
310         }
311
312         err = 0;
313 out_elf_end:
314         elf_end(elf);
315 out_close:
316         close(fd);
317         return err;
318 }
319
320 static size_t dso__fprintf(struct dso *self, FILE *fp)
321 {
322         struct symbol *pos;
323         size_t ret = fprintf(fp, "dso: %s\n", self->name);
324
325         list_for_each_entry(pos, &self->syms, node)
326                 ret += symbol__fprintf(pos, fp);
327
328         return ret;
329 }
330
331 static LIST_HEAD(dsos);
332 static struct dso *kernel_dso;
333
334 static void dsos__add(struct dso *dso)
335 {
336         list_add_tail(&dso->node, &dsos);
337 }
338
339 static struct dso *dsos__find(const char *name)
340 {
341         struct dso *pos;
342
343         list_for_each_entry(pos, &dsos, node)
344                 if (strcmp(pos->name, name) == 0)
345                         return pos;
346         return NULL;
347 }
348
349 static struct dso *dsos__findnew(const char *name)
350 {
351         struct dso *dso = dsos__find(name);
352
353         if (dso == NULL) {
354                 dso = dso__new(name);
355                 if (dso != NULL && dso__load(dso) < 0)
356                         goto out_delete_dso;
357
358                 dsos__add(dso);
359         }
360
361         return dso;
362
363 out_delete_dso:
364         dso__delete(dso);
365         return NULL;
366 }
367
368 static void dsos__fprintf(FILE *fp)
369 {
370         struct dso *pos;
371
372         list_for_each_entry(pos, &dsos, node)
373                 dso__fprintf(pos, fp);
374 }
375
376 static int load_kallsyms(void)
377 {
378         kernel_dso = dso__new("[kernel]");
379         if (kernel_dso == NULL)
380                 return -1;
381
382         FILE *file = fopen("/proc/kallsyms", "r");
383
384         if (file == NULL)
385                 goto out_delete_dso;
386
387         char *line = NULL;
388         size_t n;
389
390         while (!feof(file)) {
391                 unsigned long long start;
392                 char c, symbf[4096];
393
394                 if (getline(&line, &n, file) < 0)
395                         break;
396
397                 if (!line)
398                         goto out_delete_dso;
399
400                 if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) {
401                         struct symbol *sym = symbol__new(start, 0x1000000, symbf);
402
403                         if (sym == NULL)
404                                 goto out_delete_dso;
405
406                         dso__insert_symbol(kernel_dso, sym);
407                 }
408         }
409
410         dsos__add(kernel_dso);
411         free(line);
412         fclose(file);
413         return 0;
414
415 out_delete_dso:
416         dso__delete(kernel_dso);
417         return -1;
418 }
419
420 struct map {
421         struct list_head node;
422         uint64_t         start;
423         uint64_t         end;
424         uint64_t         pgoff;
425         struct dso       *dso;
426 };
427
428 static struct map *map__new(struct mmap_event *event)
429 {
430         struct map *self = malloc(sizeof(*self));
431
432         if (self != NULL) {
433                 self->start = event->start;
434                 self->end   = event->start + event->len;
435                 self->pgoff = event->pgoff;
436
437                 self->dso = dsos__findnew(event->filename);
438                 if (self->dso == NULL)
439                         goto out_delete;
440         }
441         return self;
442 out_delete:
443         free(self);
444         return NULL;
445 }
446
447 static size_t map__fprintf(struct map *self, FILE *fp)
448 {
449         return fprintf(fp, " %lx-%lx %lx %s\n",
450                        self->start, self->end, self->pgoff, self->dso->name);
451 }
452
453 struct symhist {
454         struct list_head node;
455         struct dso       *dso;
456         struct symbol    *sym;
457         uint32_t         count;
458         char             level;
459 };
460
461 static struct symhist *symhist__new(struct symbol *sym, struct dso *dso,
462                                     char level)
463 {
464         struct symhist *self = malloc(sizeof(*self));
465
466         if (self != NULL) {
467                 self->sym   = sym;
468                 self->dso   = dso;
469                 self->level = level;
470                 self->count = 0;
471         }
472
473         return self;
474 }
475
476 static void symhist__delete(struct symhist *self)
477 {
478         free(self);
479 }
480
481 static bool symhist__equal(struct symhist *self, struct symbol *sym,
482                            struct dso *dso, char level)
483 {
484         return self->level == level && self->sym == sym && self->dso == dso;
485 }
486
487 static void symhist__inc(struct symhist *self)
488 {
489         ++self->count;
490 }
491
492 static size_t symhist__fprintf(struct symhist *self, FILE *fp)
493 {
494         size_t ret = fprintf(fp, "[%c] ", self->level);
495
496         if (self->level != '.')
497                 ret += fprintf(fp, "%s", self->sym->name);
498         else
499                 ret += fprintf(fp, "%s: %s",
500                                self->dso ? self->dso->name : "<unknown",
501                                self->sym ? self->sym->name : "<unknown>");
502         return ret + fprintf(fp, ": %u\n", self->count);
503 }
504
505 struct thread {
506         struct list_head node;
507         struct list_head maps;
508         struct list_head symhists;
509         pid_t            pid;
510         char             *comm;
511 };
512
513 static struct thread *thread__new(pid_t pid)
514 {
515         struct thread *self = malloc(sizeof(*self));
516
517         if (self != NULL) {
518                 self->pid = pid;
519                 self->comm = NULL;
520                 INIT_LIST_HEAD(&self->maps);
521                 INIT_LIST_HEAD(&self->symhists);
522         }
523
524         return self;
525 }
526
527 static void thread__insert_symhist(struct thread *self,
528                                    struct symhist *symhist)
529 {
530         list_add_tail(&symhist->node, &self->symhists);
531 }
532
533 static struct symhist *thread__symhists_find(struct thread *self,
534                                              struct symbol *sym,
535                                              struct dso *dso, char level)
536 {
537         struct symhist *pos;
538
539         list_for_each_entry(pos, &self->symhists, node)
540                 if (symhist__equal(pos, sym, dso, level))
541                         return pos;
542
543         return NULL;
544 }
545
546 static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
547                                  struct dso *dso, char level)
548 {
549         struct symhist *symhist = thread__symhists_find(self, sym, dso, level);
550
551         if (symhist == NULL) {
552                 symhist = symhist__new(sym, dso, level);
553                 if (symhist == NULL)
554                         goto out_error;
555                 thread__insert_symhist(self, symhist);
556         }
557
558         symhist__inc(symhist);
559         return 0;
560 out_error:
561         return -ENOMEM;
562 }
563
564 static int thread__set_comm(struct thread *self, const char *comm)
565 {
566         self->comm = strdup(comm);
567         return self->comm ? 0 : -ENOMEM;
568 }
569
570 static size_t thread__maps_fprintf(struct thread *self, FILE *fp)
571 {
572         struct map *pos;
573         size_t ret = 0;
574
575         list_for_each_entry(pos, &self->maps, node)
576                 ret += map__fprintf(pos, fp);
577
578         return ret;
579 }
580
581 static size_t thread__fprintf(struct thread *self, FILE *fp)
582 {
583         struct symhist *pos;
584         int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
585
586         list_for_each_entry(pos, &self->symhists, node)
587                 ret += symhist__fprintf(pos, fp);
588
589         return ret;
590 }
591
592 static LIST_HEAD(threads);
593
594 static void threads__add(struct thread *thread)
595 {
596         list_add_tail(&thread->node, &threads);
597 }
598
599 static struct thread *threads__find(pid_t pid)
600 {
601         struct thread *pos;
602
603         list_for_each_entry(pos, &threads, node)
604                 if (pos->pid == pid)
605                         return pos;
606         return NULL;
607 }
608
609 static struct thread *threads__findnew(pid_t pid)
610 {
611         struct thread *thread = threads__find(pid);
612
613         if (thread == NULL) {
614                 thread = thread__new(pid);
615                 if (thread != NULL)
616                         threads__add(thread);
617         }
618
619         return thread;
620 }
621
622 static void thread__insert_map(struct thread *self, struct map *map)
623 {
624         list_add_tail(&map->node, &self->maps);
625 }
626
627 static struct map *thread__find_map(struct thread *self, uint64_t ip)
628 {
629         if (self == NULL)
630                 return NULL;
631
632         struct map *pos;
633
634         list_for_each_entry(pos, &self->maps, node)
635                 if (ip >= pos->start && ip <= pos->end)
636                         return pos;
637
638         return NULL;
639 }
640
641 static void threads__fprintf(FILE *fp)
642 {
643         struct thread *pos;
644
645         list_for_each_entry(pos, &threads, node)
646                 thread__fprintf(pos, fp);
647 }
648
649 #if 0
650 static std::string resolve_user_symbol(int pid, uint64_t ip)
651 {
652         std::string sym = "<unknown>";
653
654         maps_t &m = maps[pid];
655         maps_t::const_iterator mi = m.upper_bound(map(ip));
656         if (mi == m.end())
657                 return sym;
658
659         ip -= mi->start + mi->pgoff;
660
661         symbols_t &s = dsos[mi->dso].syms;
662         symbols_t::const_iterator si = s.upper_bound(symbol(ip));
663
664         sym = mi->dso + ": <unknown>";
665
666         if (si == s.begin())
667                 return sym;
668         si--;
669
670         if (si->start <= ip && ip < si->end)
671                 sym = mi->dso + ": " + si->name;
672 #if 0
673         else if (si->start <= ip)
674                 sym = mi->dso + ": ?" + si->name;
675 #endif
676
677         return sym;
678 }
679 #endif
680
681 static void display_help(void)
682 {
683         printf(
684         "Usage: perf-report [<options>]\n"
685         " -i file   --input=<file>      # input file\n"
686         );
687
688         exit(0);
689 }
690
691 static void process_options(int argc, char *argv[])
692 {
693         int error = 0;
694
695         for (;;) {
696                 int option_index = 0;
697                 /** Options for getopt */
698                 static struct option long_options[] = {
699                         {"input",       required_argument,      NULL, 'i'},
700                         {"no-user",     no_argument,            NULL, 'u'},
701                         {"no-kernel",   no_argument,            NULL, 'k'},
702                         {"no-hv",       no_argument,            NULL, 'h'},
703                         {NULL,          0,                      NULL,  0 }
704                 };
705                 int c = getopt_long(argc, argv, "+:i:kuh",
706                                     long_options, &option_index);
707                 if (c == -1)
708                         break;
709
710                 switch (c) {
711                 case 'i': input_name                    = strdup(optarg); break;
712                 case 'k': show_mask &= ~SHOW_KERNEL; break;
713                 case 'u': show_mask &= ~SHOW_USER; break;
714                 case 'h': show_mask &= ~SHOW_HV; break;
715                 default: error = 1; break;
716                 }
717         }
718
719         if (error)
720                 display_help();
721 }
722
723 int cmd_report(int argc, char **argv)
724 {
725         unsigned long offset = 0;
726         unsigned long head = 0;
727         struct stat stat;
728         char *buf;
729         event_t *event;
730         int ret, rc = EXIT_FAILURE;
731         unsigned long total = 0;
732
733         elf_version(EV_CURRENT);
734
735         page_size = getpagesize();
736
737         process_options(argc, argv);
738
739         input = open(input_name, O_RDONLY);
740         if (input < 0) {
741                 perror("failed to open file");
742                 exit(-1);
743         }
744
745         ret = fstat(input, &stat);
746         if (ret < 0) {
747                 perror("failed to stat file");
748                 exit(-1);
749         }
750
751         if (!stat.st_size) {
752                 fprintf(stderr, "zero-sized file, nothing to do!\n");
753                 exit(0);
754         }
755
756         if (load_kallsyms() < 0) {
757                 perror("failed to open kallsyms");
758                 return EXIT_FAILURE;
759         }
760
761 remap:
762         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
763                            MAP_SHARED, input, offset);
764         if (buf == MAP_FAILED) {
765                 perror("failed to mmap file");
766                 exit(-1);
767         }
768
769 more:
770         event = (event_t *)(buf + head);
771
772         if (head + event->header.size >= page_size * mmap_window) {
773                 unsigned long shift = page_size * (head / page_size);
774                 int ret;
775
776                 ret = munmap(buf, page_size * mmap_window);
777                 assert(ret == 0);
778
779                 offset += shift;
780                 head -= shift;
781                 goto remap;
782         }
783
784
785         if (!event->header.size) {
786                 fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head);
787                 fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head);
788                 goto done;
789         }
790
791         head += event->header.size;
792
793         if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
794                 char level;
795                 int show = 0;
796                 struct dso *dso = NULL;
797                 struct thread *thread = threads__findnew(event->ip.pid);
798
799                 if (thread == NULL)
800                         goto done;
801
802                 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
803                         show = SHOW_KERNEL;
804                         level = 'k';
805                         dso = kernel_dso;
806                 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
807                         show = SHOW_USER;
808                         level = '.';
809                         struct map *map = thread__find_map(thread, event->ip.ip);
810                         if (map != NULL)
811                                 dso = map->dso;
812                 } else {
813                         show = SHOW_HV;
814                         level = 'H';
815                 }
816
817                 if (show & show_mask) {
818                         struct symbol *sym = dso__find_symbol(dso, event->ip.ip);
819
820                         if (thread__symbol_incnew(thread, sym, dso, level))
821                                 goto done;
822                 }
823                 total++;
824         } else switch (event->header.type) {
825         case PERF_EVENT_MMAP: {
826                 struct thread *thread = threads__findnew(event->mmap.pid);
827                 struct map *map = map__new(&event->mmap);
828
829                 if (thread == NULL || map == NULL )
830                         goto done;
831                 thread__insert_map(thread, map);
832                 break;
833         }
834         case PERF_EVENT_COMM: {
835                 struct thread *thread = threads__findnew(event->comm.pid);
836
837                 if (thread == NULL ||
838                     thread__set_comm(thread, event->comm.comm))
839                         goto done;
840                 break;
841         }
842         }
843
844         if (offset + head < stat.st_size)
845                 goto more;
846
847         rc = EXIT_SUCCESS;
848 done:
849         close(input);
850         //dsos__fprintf(stdout);
851         threads__fprintf(stdout);
852 #if 0
853         std::map<std::string, int>::iterator hi = hist.begin();
854
855         while (hi != hist.end()) {
856                 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
857                 hist.erase(hi++);
858         }
859
860         std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
861
862         while (ri != rev_hist.end()) {
863                 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
864                 ri++;
865         }
866 #endif
867         return rc;
868 }
869