Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[games.git] / usr.sbin / kernbb / kernbb.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD: src/usr.sbin/kernbb/kernbb.c,v 1.11.2.1 2001/07/19 04:17:03 kris Exp $
10  */
11
12 #include <err.h>
13 #include <fcntl.h>
14 #include <kvm.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #define MAXBB 32768
21
22 struct bb {
23         u_long  zero_one;
24         u_long  filename;
25         u_long  counts;
26         u_long  ncounts;
27         u_long  next;
28         u_long  addr;
29         u_long  nwords;
30         u_long  func;
31         u_long  lineno;
32         u_long  file;
33 };
34
35 struct nlist namelist[] = {
36         { .n_name = "bbhead" },
37         { .n_name = NULL }
38 };
39
40 u_long  lineno[MAXBB];
41 u_long  counts[MAXBB];
42 u_long  addr[MAXBB];
43 u_long  func[MAXBB];
44 u_long  file[MAXBB];
45 char    *fn[MAXBB];
46 char    *pn[MAXBB];
47
48 kvm_t   *kv;
49
50 int
51 main(int argc __unused, char **argv __unused)
52 {
53         u_int i,j;
54         u_long l1,l2,l4;
55         struct bb bb;
56         char buf[128];
57         char dash[] = "-";
58
59         kv = kvm_open(NULL,NULL,NULL,O_RDWR,"dnc");
60         if (!kv) 
61                 err(1,"kvm_open");
62         i = kvm_nlist(kv,namelist);
63         if (i)
64                 err(1,"kvm_nlist");
65
66         l1 = namelist[0].n_value;
67         kvm_read(kv,l1,&l2,sizeof l2);
68         while(l2) {
69                 l1 += sizeof l1;
70                 kvm_read(kv,l2,&bb,sizeof bb);
71                 l2 = bb.next;
72                 if (!bb.ncounts)
73                         continue;
74                 if (bb.ncounts > MAXBB)
75                         errx(1, "found %lu counts above limit of %u",
76                                 bb.ncounts, MAXBB);
77                 kvm_read(kv,bb.lineno,lineno, bb.ncounts * sizeof lineno[0]);
78                 kvm_read(kv,bb.counts,counts, bb.ncounts * sizeof counts[0]);
79                 kvm_read(kv,bb.addr,  addr,   bb.ncounts * sizeof addr[0]);
80                 kvm_read(kv,bb.file,  file,   bb.ncounts * sizeof file[0]);
81                 kvm_read(kv,bb.func,  func,   bb.ncounts * sizeof func[0]);
82                 l4 = 0;
83                 for (i=0; i < bb.ncounts; i++) {
84                         if (counts[i])
85                                 l4++;
86                         if (!func[i] && i+1 < bb.ncounts)
87                                 func[i] = func[i+1];
88                 }
89                 if (!l4)
90                         continue;
91                 for (i=0; i < bb.ncounts; i++) {
92
93                         if (0 && !counts[i])
94                                 continue;
95
96                         if (!pn[i] && func[i]) {
97                                 kvm_read(kv,func[i], buf, sizeof buf);
98                                 buf[sizeof buf -1] = 0;
99                                 pn[i] = strdup(buf);
100                                 for(j=i+1;j<bb.ncounts;j++)
101                                         if (func[j] == func[i]) {
102                                                 pn[j] = pn[i];
103                                                 func[j] = 0;
104                                         }
105                         }
106                         if (!pn[i])
107                                 pn[i] = dash;
108                         if (!fn[i] && file[i]) {
109                                 kvm_read(kv,file[i], buf, sizeof buf);
110                                 buf[sizeof buf -1] = 0;
111                                 fn[i] = strdup(buf);
112                                 for(j=i+1;j<bb.ncounts;j++)
113                                         if (file[j] == file[i]) {
114                                                 fn[j] = fn[i];
115                                                 file[j] = 0;
116                                         }
117                         }
118                         if (!fn[i])
119                                 fn[i] = dash;
120                         l4 = 0;
121                         if (i+1 < bb.ncounts)
122                                 l4 = addr[i+1] - addr[i];
123                         printf("%s %5lu %s %lu %lu %lu %lu\n",
124                                 fn[i], lineno[i], pn[i], addr[i], counts[i], l4, counts[i] * l4);
125                 }
126                 for(i=0;i<bb.ncounts;i++) {
127                         if (func[i] && pn[i]) 
128                                 free(pn[i]);
129                         if (file[i] && fn[i])
130                                 free(fn[i]);
131                         pn[i] = NULL;
132                         fn[i] = NULL;
133                 }
134         }
135         return 0;
136 }