fixes for Power64
[tridge/junkcode.git] / kgprof.c
1 #include <stdio.h>
2 #include <string.h>
3
4 #define MAX_MAPSIZE 10000
5
6 static struct {
7         unsigned addr;
8         char *name;
9 } sysmap[MAX_MAPSIZE];
10
11 static int mapsize;
12
13 static void load_map(void)
14 {
15         FILE *f;
16         unsigned addr;
17         char dum[100];
18         char fn[100];
19
20         f = fopen("/boot/System.map", "r");
21         while (!feof(f) && mapsize < MAX_MAPSIZE) {
22                 if (fscanf(f,"%x %s %s", &addr, dum, fn) == 3) {
23                         sysmap[mapsize].addr = addr;
24                         sysmap[mapsize].name = strdup(fn);
25                         mapsize++;
26                 }
27         }
28         fclose(f);
29 }
30
31 static char *find_map(unsigned addr)
32 {
33         int low, high, i;
34
35         low = 0;
36         high = mapsize-1;
37
38         while (low != high) {
39                 i = (low+high)/2;
40                 if (addr >= sysmap[i].addr) {
41                         low = i;
42                 }
43                 if (addr < sysmap[i].addr) {
44                         high = i-1;
45                 } 
46                 if (addr >= sysmap[i+1].addr) {
47                         low = i+1;
48                 }
49                 if (addr < sysmap[i+1].addr) {
50                         high = i;
51                 }
52         }
53
54         return sysmap[i].name;
55 }
56
57 static void disp_one(char *line)
58 {
59         unsigned addr[6];
60         unsigned t, count;
61         char fname[30];
62         int i;
63
64         sscanf(line,"%s %u %u %x %x %x %x %x %x",
65                fname, &count, &t, 
66                &addr[0], &addr[1], &addr[2], 
67                &addr[3], &addr[4], &addr[5]);
68
69         printf("%s %u %u:", fname, count, t);
70         for (i=0;i<6;i++) {
71                 printf(" %s", find_map(addr[i]));
72         }
73         printf("\n");
74 }
75
76 int main()
77 {
78         char line[1000];
79         int enabled = 0;
80         FILE *f;
81
82         load_map();
83
84         printf("loaded map\n");
85
86         f = fopen("/proc/cpuinfo", "r");
87
88         while (fgets(line, sizeof(line)-1, f)) {
89                 if (enabled) {
90                         disp_one(line);
91                 }
92                 if (strncmp(line,"kgprof", 6) == 0) enabled = 1;
93         }
94
95 }