kernel - revamp mtx_spinlock()
[dragonfly.git] / usr.sbin / kernbb / kernbb.c
blob279efa93829e17072ffba00be69d5422fa667868
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 { .n_name = "bbhead" },
38 { .n_name = 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 u_int i,j;
55 u_long l1,l2,l4;
56 struct bb bb;
57 char buf[128];
58 char dash[] = "-";
60 kv = kvm_open(NULL,NULL,NULL,O_RDWR,"dnc");
61 if (!kv)
62 err(1,"kvm_open");
63 i = kvm_nlist(kv,namelist);
64 if (i)
65 err(1,"kvm_nlist");
67 l1 = namelist[0].n_value;
68 kvm_read(kv,l1,&l2,sizeof l2);
69 while(l2) {
70 l1 += sizeof l1;
71 kvm_read(kv,l2,&bb,sizeof bb);
72 l2 = bb.next;
73 if (!bb.ncounts)
74 continue;
75 if (bb.ncounts > MAXBB)
76 errx(1, "found %lu counts above limit of %u",
77 bb.ncounts, MAXBB);
78 kvm_read(kv,bb.lineno,lineno, bb.ncounts * sizeof lineno[0]);
79 kvm_read(kv,bb.counts,counts, bb.ncounts * sizeof counts[0]);
80 kvm_read(kv,bb.addr, addr, bb.ncounts * sizeof addr[0]);
81 kvm_read(kv,bb.file, file, bb.ncounts * sizeof file[0]);
82 kvm_read(kv,bb.func, func, bb.ncounts * sizeof func[0]);
83 l4 = 0;
84 for (i=0; i < bb.ncounts; i++) {
85 if (counts[i])
86 l4++;
87 if (!func[i] && i+1 < bb.ncounts)
88 func[i] = func[i+1];
90 if (!l4)
91 continue;
92 for (i=0; i < bb.ncounts; i++) {
94 if (0 && !counts[i])
95 continue;
97 if (!pn[i] && func[i]) {
98 kvm_read(kv,func[i], buf, sizeof buf);
99 buf[sizeof buf -1] = 0;
100 pn[i] = strdup(buf);
101 for(j=i+1;j<bb.ncounts;j++)
102 if (func[j] == func[i]) {
103 pn[j] = pn[i];
104 func[j] = 0;
107 if (!pn[i])
108 pn[i] = dash;
109 if (!fn[i] && file[i]) {
110 kvm_read(kv,file[i], buf, sizeof buf);
111 buf[sizeof buf -1] = 0;
112 fn[i] = strdup(buf);
113 for(j=i+1;j<bb.ncounts;j++)
114 if (file[j] == file[i]) {
115 fn[j] = fn[i];
116 file[j] = 0;
119 if (!fn[i])
120 fn[i] = dash;
121 l4 = 0;
122 if (i+1 < bb.ncounts)
123 l4 = addr[i+1] - addr[i];
124 printf("%s %5lu %s %lu %lu %lu %lu\n",
125 fn[i], lineno[i], pn[i], addr[i], counts[i], l4, counts[i] * l4);
127 for(i=0;i<bb.ncounts;i++) {
128 if (func[i] && pn[i])
129 free(pn[i]);
130 if (file[i] && fn[i])
131 free(fn[i]);
132 pn[i] = 0;
133 fn[i] = 0;
136 return 0;