Add missing -s to usage string.
[dragonfly.git] / usr.sbin / kernbb / kernbb.c
blob09c5b2da5ee2897f24505378087df5f343b5538e
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 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/usr.sbin/kernbb/kernbb.c,v 1.11.2.1 2001/07/19 04:17:03 kris Exp $
10 * $DragonFly: src/usr.sbin/kernbb/kernbb.c,v 1.3 2003/11/16 15:17:36 eirikn Exp $
13 #include <err.h>
14 #include <fcntl.h>
15 #include <kvm.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
21 #define MAXBB 32768
23 struct bb {
24 u_long zero_one;
25 u_long filename;
26 u_long counts;
27 u_long ncounts;
28 u_long next;
29 u_long addr;
30 u_long nwords;
31 u_long func;
32 u_long lineno;
33 u_long file;
36 struct nlist namelist[] = {
37 { "bbhead" },
38 { NULL }
41 u_long lineno[MAXBB];
42 u_long counts[MAXBB];
43 u_long addr[MAXBB];
44 u_long func[MAXBB];
45 u_long file[MAXBB];
46 char *fn[MAXBB];
47 char *pn[MAXBB];
49 kvm_t *kv;
51 int
52 main(int argc __unused, char **argv __unused)
54 int i,j;
55 u_long l1,l2,l4;
56 struct bb bb;
57 char buf[128];
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");
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];
89 if (!l4)
90 continue;
91 for (i=0; i < bb.ncounts; i++) {
93 if (0 && !counts[i])
94 continue;
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;
106 if (!pn[i])
107 pn[i] = "-";
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;
118 if (!fn[i])
119 fn[i] = "-";
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);
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] = 0;
132 fn[i] = 0;
135 return 0;