Instead of using the non-standard conforming %+ format string,
[dragonfly.git] / test / debug / ttyinfo.c
1 /*
2  * TTYINFO.C
3  *
4  * cc -I/usr/src/sys ttyinfo.c -o /usr/local/bin/ttyinfo -lkvm
5  *
6  * ttyinfo
7  *
8  *
9  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
10  * 
11  * This code is derived from software contributed to The DragonFly Project
12  * by Matthew Dillon <dillon@backplane.com>
13  * 
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in
22  *    the documentation and/or other materials provided with the
23  *    distribution.
24  * 3. Neither the name of The DragonFly Project nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific, prior written permission.
27  * 
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
32  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
34  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  * $DragonFly: src/test/debug/ttyinfo.c,v 1.2 2004/10/08 18:32:58 dillon Exp $
42  */
43
44 #define _KERNEL_STRUCTURES_
45 #include <sys/param.h>
46 #include <sys/user.h>
47 #include <sys/malloc.h>
48 #include <sys/signalvar.h>
49 #include <sys/vnode.h>
50 #include <sys/namecache.h>
51 #include <sys/tty.h>
52 #include <sys/clist.h>
53
54 #include <vm/vm.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_kern.h>
57 #include <vm/swap_pager.h>
58 #include <vm/vnode_pager.h>
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <fcntl.h>
64 #include <kvm.h>
65 #include <nlist.h>
66 #include <getopt.h>
67
68 struct nlist Nl[] = {
69     { "_cfreelist" },
70     { "_cfreecount" },
71     { "_cslushcount" },
72     { "_ctotcount" },
73     { NULL }
74 };
75
76 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
77 static int scanfree(kvm_t *kd, struct cblock *cfree);
78
79 main(int ac, char **av)
80 {
81     struct cblock *cfree;
82     int cbytes;
83     int count;
84     int slush;
85     int totalcnt;
86     int ch;
87     kvm_t *kd;
88     const char *corefile = NULL;
89     const char *sysfile = NULL;
90
91     while ((ch = getopt(ac, av, "M:N:")) != -1) {
92         switch(ch) {
93         case 'M':
94             corefile = optarg;
95             break;
96         case 'N':
97             sysfile = optarg;
98             break;
99         default:
100             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
101             exit(1);
102         }
103     }
104
105     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
106         perror("kvm_open");
107         exit(1);
108     }
109     if (kvm_nlist(kd, Nl) != 0) {
110         perror("kvm_nlist");
111         exit(1);
112     }
113     kkread(kd, Nl[0].n_value, &cfree, sizeof(cfree));
114     kkread(kd, Nl[1].n_value, &cbytes, sizeof(cbytes));
115     kkread(kd, Nl[2].n_value, &slush, sizeof(slush));
116     kkread(kd, Nl[3].n_value, &totalcnt, sizeof(totalcnt));
117     count = scanfree(kd, cfree);
118     printf("blksize %d, freespc %d bytes, %d blks (%d total), %d slush",
119         CBSIZE, cbytes, cbytes / CBSIZE, totalcnt, slush);
120     if (cbytes % CBSIZE)
121         printf(" [unaligned]\n");
122     else
123         printf(" [aligned]\n");
124     printf("freelist found to have %d blocks\n", count);
125     return(0);
126 }
127
128 static int
129 scanfree(kvm_t *kd, struct cblock *cfree)
130 {
131     int count = 0;
132     struct cblock cb;
133
134     while (cfree) {
135         kkread(kd, (u_long)cfree, &cb, sizeof(cb));
136         cfree = cb.c_head.ch_next;
137         ++count;
138     }
139     return(count);
140 }
141
142 static void
143 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
144 {
145     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
146         perror("kvm_read");
147         exit(1);
148     }
149 }
150