Add the standard DragonFly copyright.
[dragonfly.git] / test / debug / vnodeinfo.c
1 /*
2  * VNODEINFO.C
3  *
4  * cc -I/usr/src/sys vnodeinfo.c -o /usr/local/bin/vnodeinfo -lkvm
5  *
6  * vnodeinfo
7  *
8  * Dump the mountlist and related vnodes.
9  *
10  *
11  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
12  * 
13  * This code is derived from software contributed to The DragonFly Project
14  * by Matthew Dillon <dillon@backplane.com>
15  * 
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in
24  *    the documentation and/or other materials provided with the
25  *    distribution.
26  * 3. Neither the name of The DragonFly Project nor the names of its
27  *    contributors may be used to endorse or promote products derived
28  *    from this software without specific, prior written permission.
29  * 
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
34  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
36  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
38  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
40  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  * $DragonFly: src/test/debug/vnodeinfo.c,v 1.3 2004/10/08 18:32:58 dillon Exp $
44  */
45
46 #define _KERNEL_STRUCTURES_
47 #include <sys/param.h>
48 #include <sys/user.h>
49 #include <sys/malloc.h>
50 #include <sys/signalvar.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.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     { "_mountlist" },
70     { "_vnode_free_list" },
71     { NULL }
72 };
73
74 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
75 static struct mount *dumpmount(kvm_t *kd, struct mount *mp);
76 static struct vnode *dumpvp(kvm_t *kd, struct vnode *vp, int whichlist);
77
78 main(int ac, char **av)
79 {
80     struct mount *mp;
81     struct vnode *vp;
82     kvm_t *kd;
83     int i;
84     int ch;
85     const char *corefile = NULL;
86     const char *sysfile = NULL;
87
88     while ((ch = getopt(ac, av, "M:N:")) != -1) {
89         switch(ch) {   
90         case 'M':
91             corefile = optarg;
92             break;
93         case 'N': 
94             sysfile = optarg;
95             break; 
96         default:  
97             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
98             exit(1);
99         }
100     }
101
102     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
103         perror("kvm_open");
104         exit(1);
105     }
106     if (kvm_nlist(kd, Nl) != 0) {
107         perror("kvm_nlist");
108         exit(1);
109     }
110     kkread(kd, Nl[0].n_value, &mp, sizeof(mp));
111     while (mp)
112         mp = dumpmount(kd, mp);
113     kkread(kd, Nl[1].n_value, &vp, sizeof(vp));
114     printf("VNODEFREELIST {\n");
115     while (vp)
116         vp = dumpvp(kd, vp, 0);
117     printf("}\n");
118     return(0);
119 }
120
121 static struct mount *
122 dumpmount(kvm_t *kd, struct mount *mp)
123 {
124     struct mount mnt;
125     struct vnode *vp;
126
127     kkread(kd, (u_long)mp, &mnt, sizeof(mnt));
128     printf("MOUNTPOINT %s on %s {\n", 
129         mnt.mnt_stat.f_mntfromname, mnt.mnt_stat.f_mntonname);
130     printf("    lk_flags %08x share %d wait %d excl %d holder = %p\n",
131         mnt.mnt_lock.lk_flags, mnt.mnt_lock.lk_sharecount,
132         mnt.mnt_lock.lk_waitcount, mnt.mnt_lock.lk_exclusivecount,
133         mnt.mnt_lock.lk_lockholder);
134     printf("    mnt_flag %08x mnt_kern_flag %08x\n", 
135         mnt.mnt_flag, mnt.mnt_kern_flag);
136     printf("    mnt_nvnodelistsize %d\n", mnt.mnt_nvnodelistsize);
137     vp = mnt.mnt_nvnodelist.tqh_first;
138     while (vp)
139         vp = dumpvp(kd, vp, 1);
140
141     printf("}\n");
142
143     return(mnt.mnt_list.tqe_next);
144 }
145
146 static const char *
147 vtype(enum vtype type)
148 {
149     static char buf[32];
150
151     switch(type) {
152     case VNON:
153         return("VNON");
154     case VREG:
155         return("VREG");
156     case VDIR:
157         return("VDIR");
158     case VBLK:
159         return("VBLK");
160     case VCHR:
161         return("VCHR");
162     case VLNK:
163         return("VLNK");
164     case VSOCK:
165         return("VSOCK");
166     case VFIFO:
167         return("VFIFO");
168     case VBAD:
169         return("VBAD");
170     default:
171         break;
172     }
173     snprintf(buf, sizeof(buf), "%d", (int)type);
174     return(buf);
175 }
176
177 static struct vnode *
178 dumpvp(kvm_t *kd, struct vnode *vp, int whichlist)
179 {
180     struct vnode vn;
181
182     kkread(kd, (u_long)vp, &vn, sizeof(vn));
183
184     printf("    vnode %p usecnt %d holdcnt %d type=%s flags %08x",
185         vp, vn.v_usecount, vn.v_holdcnt, vtype(vn.v_type), vn.v_flag);
186     if (vn.v_flag & VROOT)
187         printf(" VROOT");
188     if (vn.v_flag & VTEXT)
189         printf(" VTEXT");
190     if (vn.v_flag & VSYSTEM)
191         printf(" VSYSTEM");
192     if (vn.v_flag & VISTTY)
193         printf(" VISTTY");
194 #ifdef VXLOCK
195     if (vn.v_flag & VXLOCK)
196         printf(" VXLOCK");
197     if (vn.v_flag & VXWANT)
198         printf(" VXWANT");
199 #endif
200 #ifdef VRECLAIMED
201     if (vn.v_flag & VRECLAIMED)
202         printf(" VRECLAIMED");
203     if (vn.v_flag & VINACTIVE)
204         printf(" VINACTIVE");
205 #endif
206     if (vn.v_flag & VBWAIT)
207         printf(" VBWAIT");
208     if (vn.v_flag & VOBJBUF)
209         printf(" VOBJBUF");
210     if (vn.v_flag & VAGE)
211         printf(" VAGE");
212     if (vn.v_flag & VOLOCK)
213         printf(" VOLOCK");
214     if (vn.v_flag & VOWANT)
215         printf(" VOWANT");
216 #ifdef VDOOMED
217     if (vn.v_flag & VDOOMED)
218         printf(" VDOOMED");
219 #endif
220     if (vn.v_flag & VFREE)
221         printf(" VFREE");
222 #ifdef VINFREE
223     if (vn.v_flag & VINFREE)
224         printf(" VINFREE");
225 #endif
226     if (vn.v_flag & VONWORKLST)
227         printf(" VONWORKLST");
228     if (vn.v_flag & VMOUNT)
229         printf(" VMOUNT");
230     if (vn.v_flag & VOBJDIRTY)
231         printf(" VOBJDIRTY");
232     if (vn.v_flag & VPLACEMARKER)
233         printf(" VPLACEMARKER");
234     printf("\n");
235
236     if (vn.v_lock.lk_sharecount || vn.v_lock.lk_waitcount || 
237         vn.v_lock.lk_exclusivecount || vn.v_lock.lk_lockholder != LK_NOTHREAD) {
238         printf("\tlk_flags %08x share %d wait %d excl %d holder = %p\n",
239             vn.v_lock.lk_flags, vn.v_lock.lk_sharecount,
240             vn.v_lock.lk_waitcount, vn.v_lock.lk_exclusivecount,
241             vn.v_lock.lk_lockholder);
242     }
243
244     if (whichlist)
245         return(vn.v_nmntvnodes.tqe_next);
246     else
247         return(vn.v_freelist.tqe_next);
248 }
249
250 void
251 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
252 {
253     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
254         perror("kvm_read");
255         exit(1);
256     }
257 }
258