Merge branch 'vendor/OPENSSH'
[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 int
80 main(int ac, char **av)
81 {
82     struct cblock *cfree;
83     int cbytes;
84     int count;
85     int slush;
86     int totalcnt;
87     int ch;
88     kvm_t *kd;
89     const char *corefile = NULL;
90     const char *sysfile = NULL;
91
92     while ((ch = getopt(ac, av, "M:N:")) != -1) {
93         switch(ch) {
94         case 'M':
95             corefile = optarg;
96             break;
97         case 'N':
98             sysfile = optarg;
99             break;
100         default:
101             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
102             exit(1);
103         }
104     }
105
106     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
107         perror("kvm_open");
108         exit(1);
109     }
110     if (kvm_nlist(kd, Nl) != 0) {
111         perror("kvm_nlist");
112         exit(1);
113     }
114     kkread(kd, Nl[0].n_value, &cfree, sizeof(cfree));
115     kkread(kd, Nl[1].n_value, &cbytes, sizeof(cbytes));
116     kkread(kd, Nl[2].n_value, &slush, sizeof(slush));
117     kkread(kd, Nl[3].n_value, &totalcnt, sizeof(totalcnt));
118     count = scanfree(kd, cfree);
119     printf("blksize %zd, freespc %d bytes, %zd blks (%d total), %d slush",
120         CBSIZE, cbytes, cbytes / CBSIZE, totalcnt, slush);
121     if (cbytes % CBSIZE)
122         printf(" [unaligned]\n");
123     else
124         printf(" [aligned]\n");
125     printf("freelist found to have %d blocks\n", count);
126     return(0);
127 }
128
129 static int
130 scanfree(kvm_t *kd, struct cblock *cfree)
131 {
132     int count = 0;
133     struct cblock cb;
134
135     while (cfree) {
136         kkread(kd, (u_long)cfree, &cb, sizeof(cb));
137         cfree = cb.c_head.ch_next;
138         ++count;
139     }
140     return(count);
141 }
142
143 static void
144 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
145 {
146     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
147         perror("kvm_read");
148         exit(1);
149     }
150 }
151