aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/perf_counter/builtin-report.c
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2009-05-27 03:10:38 -0400
committerIngo Molnar <mingo@elte.hu>2009-05-27 02:10:35 -0400
commit16f762a2ac5ecf8a11f6f0332e46cc3459220da5 (patch)
treea54f013d037637a91ceac237b2b0455b29cf45d4 /Documentation/perf_counter/builtin-report.c
parent815e777f913ed54ddb449d2854015c65b4ecbfe3 (diff)
perf_counter tools: Introduce stricter C code checking
Tighten up our C code requirements: - disallow warnings - disallow declarations-mixed-with-statements - require proper prototypes - require C99 (with gcc extensions) Fix up a ton of problems these measures unearth: - unused functions - needlessly global functions - missing prototypes - code mixed with declarations Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Mike Galbraith <efault@gmx.de> Cc: Paul Mackerras <paulus@samba.org> Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: John Kacur <jkacur@redhat.com> Cc: Steven Rostedt <rostedt@goodmis.org> LKML-Reference: <20090526222155.GJ4424@ghostprotocols.net> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'Documentation/perf_counter/builtin-report.c')
-rw-r--r--Documentation/perf_counter/builtin-report.c103
1 files changed, 51 insertions, 52 deletions
diff --git a/Documentation/perf_counter/builtin-report.c b/Documentation/perf_counter/builtin-report.c
index 2d65d9c12aad..7f1255dcd222 100644
--- a/Documentation/perf_counter/builtin-report.c
+++ b/Documentation/perf_counter/builtin-report.c
@@ -1,4 +1,5 @@
1#include "util/util.h" 1#include "util/util.h"
2#include "builtin.h"
2 3
3#include <libelf.h> 4#include <libelf.h>
4#include <gelf.h> 5#include <gelf.h>
@@ -22,7 +23,7 @@ static int input;
22static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; 23static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
23 24
24static int dump_trace = 0; 25static int dump_trace = 0;
25static int verbose; 26static int verbose;
26 27
27static unsigned long page_size; 28static unsigned long page_size;
28static unsigned long mmap_window = 32; 29static unsigned long mmap_window = 32;
@@ -60,10 +61,10 @@ typedef union event_union {
60} event_t; 61} event_t;
61 62
62struct symbol { 63struct symbol {
63 struct rb_node rb_node; 64 struct rb_node rb_node;
64 uint64_t start; 65 __u64 start;
65 uint64_t end; 66 __u64 end;
66 char name[0]; 67 char name[0];
67}; 68};
68 69
69static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name) 70static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
@@ -86,7 +87,7 @@ static void symbol__delete(struct symbol *self)
86 87
87static size_t symbol__fprintf(struct symbol *self, FILE *fp) 88static size_t symbol__fprintf(struct symbol *self, FILE *fp)
88{ 89{
89 return fprintf(fp, " %lx-%lx %s\n", 90 return fprintf(fp, " %llx-%llx %s\n",
90 self->start, self->end, self->name); 91 self->start, self->end, self->name);
91} 92}
92 93
@@ -147,10 +148,12 @@ static void dso__insert_symbol(struct dso *self, struct symbol *sym)
147 148
148static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip) 149static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
149{ 150{
151 struct rb_node *n;
152
150 if (self == NULL) 153 if (self == NULL)
151 return NULL; 154 return NULL;
152 155
153 struct rb_node *n = self->syms.rb_node; 156 n = self->syms.rb_node;
154 157
155 while (n) { 158 while (n) {
156 struct symbol *s = rb_entry(n, struct symbol, rb_node); 159 struct symbol *s = rb_entry(n, struct symbol, rb_node);
@@ -221,33 +224,42 @@ static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
221 224
222static int dso__load(struct dso *self) 225static int dso__load(struct dso *self)
223{ 226{
224 int fd = open(self->name, O_RDONLY), err = -1; 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;
225 237
238
239 fd = open(self->name, O_RDONLY);
226 if (fd == -1) 240 if (fd == -1)
227 return -1; 241 return -1;
228 242
229 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); 243 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
230 if (elf == NULL) { 244 if (elf == NULL) {
231 fprintf(stderr, "%s: cannot read %s ELF file.\n", 245 fprintf(stderr, "%s: cannot read %s ELF file.\n",
232 __func__, self->name); 246 __func__, self->name);
233 goto out_close; 247 goto out_close;
234 } 248 }
235 249
236 GElf_Ehdr ehdr;
237 if (gelf_getehdr(elf, &ehdr) == NULL) { 250 if (gelf_getehdr(elf, &ehdr) == NULL) {
238 fprintf(stderr, "%s: cannot get elf header.\n", __func__); 251 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
239 goto out_elf_end; 252 goto out_elf_end;
240 } 253 }
241 254
242 GElf_Shdr shdr; 255 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
243 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
244 if (sec == NULL) 256 if (sec == NULL)
245 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); 257 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
246 258
247 if (sec == NULL) 259 if (sec == NULL)
248 goto out_elf_end; 260 goto out_elf_end;
249 261
250 Elf_Data *syms = elf_getdata(sec, NULL); 262 syms = elf_getdata(sec, NULL);
251 if (syms == NULL) 263 if (syms == NULL)
252 goto out_elf_end; 264 goto out_elf_end;
253 265
@@ -255,14 +267,12 @@ static int dso__load(struct dso *self)
255 if (sec == NULL) 267 if (sec == NULL)
256 goto out_elf_end; 268 goto out_elf_end;
257 269
258 Elf_Data *symstrs = elf_getdata(sec, NULL); 270 symstrs = elf_getdata(sec, NULL);
259 if (symstrs == NULL) 271 if (symstrs == NULL)
260 goto out_elf_end; 272 goto out_elf_end;
261 273
262 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize; 274 nr_syms = shdr.sh_size / shdr.sh_entsize;
263 275
264 GElf_Sym sym;
265 uint32_t index;
266 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { 276 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
267 struct symbol *f; 277 struct symbol *f;
268 278
@@ -342,7 +352,7 @@ out_delete_dso:
342 return NULL; 352 return NULL;
343} 353}
344 354
345void dsos__fprintf(FILE *fp) 355static void dsos__fprintf(FILE *fp)
346{ 356{
347 struct dso *pos; 357 struct dso *pos;
348 358
@@ -365,7 +375,7 @@ static int hex(char ch)
365 * While we find nice hex chars, build a long_val. 375 * While we find nice hex chars, build a long_val.
366 * Return number of chars processed. 376 * Return number of chars processed.
367 */ 377 */
368int hex2long(char *ptr, unsigned long *long_val) 378static int hex2long(char *ptr, unsigned long *long_val)
369{ 379{
370 const char *p = ptr; 380 const char *p = ptr;
371 *long_val = 0; 381 *long_val = 0;
@@ -493,12 +503,6 @@ out_delete:
493 return NULL; 503 return NULL;
494} 504}
495 505
496static size_t map__fprintf(struct map *self, FILE *fp)
497{
498 return fprintf(fp, " %lx-%lx %lx %s\n",
499 self->start, self->end, self->pgoff, self->dso->name);
500}
501
502struct thread; 506struct thread;
503 507
504static const char *thread__name(struct thread *self, char *bf, size_t size); 508static const char *thread__name(struct thread *self, char *bf, size_t size);
@@ -531,11 +535,6 @@ static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
531 return self; 535 return self;
532} 536}
533 537
534void symhist__delete(struct symhist *self)
535{
536 free(self);
537}
538
539static void symhist__inc(struct symhist *self) 538static void symhist__inc(struct symhist *self)
540{ 539{
541 ++self->count; 540 ++self->count;
@@ -608,6 +607,8 @@ static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
608 struct symhist *sh; 607 struct symhist *sh;
609 608
610 while (*p != NULL) { 609 while (*p != NULL) {
610 uint64_t start;
611
611 parent = *p; 612 parent = *p;
612 sh = rb_entry(parent, struct symhist, rb_node); 613 sh = rb_entry(parent, struct symhist, rb_node);
613 614
@@ -617,7 +618,7 @@ static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
617 } 618 }
618 619
619 /* Handle unresolved symbols too */ 620 /* Handle unresolved symbols too */
620 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start; 621 start = !sh->sym ? sh->ip : sh->sym->start;
621 622
622 if (ip < start) 623 if (ip < start)
623 p = &(*p)->rb_left; 624 p = &(*p)->rb_left;
@@ -639,17 +640,6 @@ static int thread__set_comm(struct thread *self, const char *comm)
639 return self->comm ? 0 : -ENOMEM; 640 return self->comm ? 0 : -ENOMEM;
640} 641}
641 642
642size_t thread__maps_fprintf(struct thread *self, FILE *fp)
643{
644 struct map *pos;
645 size_t ret = 0;
646
647 list_for_each_entry(pos, &self->maps, node)
648 ret += map__fprintf(pos, fp);
649
650 return ret;
651}
652
653static size_t thread__fprintf(struct thread *self, FILE *fp) 643static size_t thread__fprintf(struct thread *self, FILE *fp)
654{ 644{
655 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm); 645 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
@@ -657,13 +647,14 @@ static size_t thread__fprintf(struct thread *self, FILE *fp)
657 647
658 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) { 648 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
659 struct symhist *pos = rb_entry(nd, struct symhist, rb_node); 649 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
650
660 ret += symhist__fprintf(pos, 0, fp); 651 ret += symhist__fprintf(pos, 0, fp);
661 } 652 }
662 653
663 return ret; 654 return ret;
664} 655}
665 656
666static struct rb_root threads = RB_ROOT; 657static struct rb_root threads;
667 658
668static struct thread *threads__findnew(pid_t pid) 659static struct thread *threads__findnew(pid_t pid)
669{ 660{
@@ -699,11 +690,11 @@ static void thread__insert_map(struct thread *self, struct map *map)
699 690
700static struct map *thread__find_map(struct thread *self, uint64_t ip) 691static struct map *thread__find_map(struct thread *self, uint64_t ip)
701{ 692{
693 struct map *pos;
694
702 if (self == NULL) 695 if (self == NULL)
703 return NULL; 696 return NULL;
704 697
705 struct map *pos;
706
707 list_for_each_entry(pos, &self->maps, node) 698 list_for_each_entry(pos, &self->maps, node)
708 if (ip >= pos->start && ip <= pos->end) 699 if (ip >= pos->start && ip <= pos->end)
709 return pos; 700 return pos;
@@ -711,7 +702,7 @@ static struct map *thread__find_map(struct thread *self, uint64_t ip)
711 return NULL; 702 return NULL;
712} 703}
713 704
714void threads__fprintf(FILE *fp) 705static void threads__fprintf(FILE *fp)
715{ 706{
716 struct rb_node *nd; 707 struct rb_node *nd;
717 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { 708 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
@@ -720,7 +711,7 @@ void threads__fprintf(FILE *fp)
720 } 711 }
721} 712}
722 713
723static struct rb_root global_symhists = RB_ROOT; 714static struct rb_root global_symhists;
724 715
725static void threads__insert_symhist(struct symhist *sh) 716static void threads__insert_symhist(struct symhist *sh)
726{ 717{
@@ -852,7 +843,7 @@ more:
852 (void *)(long)(event->header.size), 843 (void *)(long)(event->header.size),
853 event->header.misc, 844 event->header.misc,
854 event->ip.pid, 845 event->ip.pid,
855 (void *)event->ip.ip); 846 (void *)(long)ip);
856 } 847 }
857 848
858 if (thread == NULL) { 849 if (thread == NULL) {
@@ -866,9 +857,12 @@ more:
866 level = 'k'; 857 level = 'k';
867 dso = kernel_dso; 858 dso = kernel_dso;
868 } else if (event->header.misc & PERF_EVENT_MISC_USER) { 859 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
860 struct map *map;
861
869 show = SHOW_USER; 862 show = SHOW_USER;
870 level = '.'; 863 level = '.';
871 struct map *map = thread__find_map(thread, ip); 864
865 map = thread__find_map(thread, ip);
872 if (map != NULL) { 866 if (map != NULL) {
873 dso = map->dso; 867 dso = map->dso;
874 ip -= map->start + map->pgoff; 868 ip -= map->start + map->pgoff;
@@ -896,9 +890,9 @@ more:
896 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", 890 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
897 (void *)(offset + head), 891 (void *)(offset + head),
898 (void *)(long)(event->header.size), 892 (void *)(long)(event->header.size),
899 (void *)event->mmap.start, 893 (void *)(long)event->mmap.start,
900 (void *)event->mmap.len, 894 (void *)(long)event->mmap.len,
901 (void *)event->mmap.pgoff, 895 (void *)(long)event->mmap.pgoff,
902 event->mmap.filename); 896 event->mmap.filename);
903 } 897 }
904 if (thread == NULL || map == NULL) { 898 if (thread == NULL || map == NULL) {
@@ -964,6 +958,11 @@ done:
964 return 0; 958 return 0;
965 } 959 }
966 960
961 if (verbose >= 2) {
962 dsos__fprintf(stdout);
963 threads__fprintf(stdout);
964 }
965
967 threads__sort_symhists(); 966 threads__sort_symhists();
968 threads__symhists_fprintf(total, stdout); 967 threads__symhists_fprintf(total, stdout);
969 968