Introduce globals: KvaStart, KvaEnd, and KvaSize. Used by the kernel
[dfdiff.git] / test / debug / vnodeinfo.c
blobbfaa937fdeac3b1733bb1103d165da44b55555b9
1 /*
2 * VNODEINFO.C
4 * cc -I/usr/src/sys vnodeinfo.c -o /usr/local/bin/vnodeinfo -lkvm
6 * vnodeinfo
8 * Dump the mountlist and related vnodes.
11 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
13 * This code is derived from software contributed to The DragonFly Project
14 * by Matthew Dillon <dillon@backplane.com>
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
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.
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.
43 * $DragonFly: src/test/debug/vnodeinfo.c,v 1.12 2006/07/28 02:11:53 dillon Exp $
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/namecache.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/buf.h>
56 #include <vm/vm.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_object.h>
60 #include <vm/swap_pager.h>
61 #include <vm/vnode_pager.h>
63 #include <vfs/ufs/quota.h>
64 #include <vfs/ufs/inode.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <fcntl.h>
70 #include <kvm.h>
71 #include <nlist.h>
72 #include <getopt.h>
74 struct nlist Nl[] = {
75 { "_mountlist" },
76 { "_vnode_free_list" },
77 { NULL }
80 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
81 static struct mount *dumpmount(kvm_t *kd, struct mount *mp);
82 static struct vnode *dumpvp(kvm_t *kd, struct vnode *vp, int whichlist);
83 static void dumpbufs(kvm_t *kd, void *bufp, const char *id);
84 static void dumplocks(kvm_t *kd, struct lockf *lockf);
85 static void dumplockinfo(kvm_t *kd, struct lockf_range *item);
86 static int getobjpages(kvm_t *kd, struct vm_object *obj);
87 static int getobjvnpsize(kvm_t *kd, struct vm_object *obj);
89 int tracebufs = 0;
90 int tracelocks = 0;
91 int withnames = 0;
93 int
94 main(int ac, char **av)
96 struct mount *mp;
97 struct vnode *vp;
98 kvm_t *kd;
99 int i;
100 int ch;
101 const char *corefile = NULL;
102 const char *sysfile = NULL;
104 while ((ch = getopt(ac, av, "alnbM:N:")) != -1) {
105 switch(ch) {
106 case 'b':
107 tracebufs = 1;
108 break;
109 case 'n':
110 withnames = 1;
111 break;
112 case 'l':
113 tracelocks = 1;
114 break;
115 case 'a':
116 tracebufs = 1;
117 tracelocks = 1;
118 withnames = 1;
119 break;
120 case 'M':
121 corefile = optarg;
122 break;
123 case 'N':
124 sysfile = optarg;
125 break;
126 default:
127 fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
128 exit(1);
132 if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
133 perror("kvm_open");
134 exit(1);
136 if (kvm_nlist(kd, Nl) != 0) {
137 perror("kvm_nlist");
138 exit(1);
140 kkread(kd, Nl[0].n_value, &mp, sizeof(mp));
141 while (mp)
142 mp = dumpmount(kd, mp);
143 kkread(kd, Nl[1].n_value, &vp, sizeof(vp));
144 printf("VNODEFREELIST {\n");
145 while (vp)
146 vp = dumpvp(kd, vp, 0);
147 printf("}\n");
148 return(0);
151 static struct mount *
152 dumpmount(kvm_t *kd, struct mount *mp)
154 struct mount mnt;
155 struct vnode *vp;
157 kkread(kd, (u_long)mp, &mnt, sizeof(mnt));
158 printf("MOUNTPOINT %s on %s {\n",
159 mnt.mnt_stat.f_mntfromname, mnt.mnt_stat.f_mntonname);
160 printf(" lk_flags %08x share %d wait %d excl %d holder = %p\n",
161 mnt.mnt_lock.lk_flags, mnt.mnt_lock.lk_sharecount,
162 mnt.mnt_lock.lk_waitcount, mnt.mnt_lock.lk_exclusivecount,
163 mnt.mnt_lock.lk_lockholder);
164 printf(" mnt_flag %08x mnt_kern_flag %08x\n",
165 mnt.mnt_flag, mnt.mnt_kern_flag);
166 printf(" mnt_nvnodelistsize %d\n", mnt.mnt_nvnodelistsize);
167 printf(" mnt_stat.f_fsid %08x %08x\n", mnt.mnt_stat.f_fsid.val[0],
168 mnt.mnt_stat.f_fsid.val[1]);
169 vp = mnt.mnt_nvnodelist.tqh_first;
170 while (vp)
171 vp = dumpvp(kd, vp, 1);
173 printf("}\n");
175 return(mnt.mnt_list.tqe_next);
178 static const char *
179 vtype(enum vtype type)
181 static char buf[32];
183 switch(type) {
184 case VNON:
185 return("VNON");
186 case VREG:
187 return("VREG");
188 case VDIR:
189 return("VDIR");
190 case VBLK:
191 return("VBLK");
192 case VCHR:
193 return("VCHR");
194 case VLNK:
195 return("VLNK");
196 case VSOCK:
197 return("VSOCK");
198 case VFIFO:
199 return("VFIFO");
200 case VBAD:
201 return("VBAD");
202 default:
203 break;
205 snprintf(buf, sizeof(buf), "%d", (int)type);
206 return(buf);
209 static struct vnode *
210 dumpvp(kvm_t *kd, struct vnode *vp, int whichlist)
212 struct vnode vn;
214 kkread(kd, (u_long)vp, &vn, sizeof(vn));
216 printf(" vnode %p usecnt %d holdcnt %d type=%s flags %08x",
217 vp, vn.v_usecount, vn.v_holdcnt, vtype(vn.v_type), vn.v_flag);
219 if ((vn.v_flag & VOBJBUF) && vn.v_object) {
220 int npages = getobjpages(kd, vn.v_object);
221 int vnpsize = getobjvnpsize(kd, vn.v_object);
222 if (npages || vnpsize)
223 printf(" vmobjpgs=%d vnpsize=%d", npages, vnpsize);
226 if (vn.v_flag & VROOT)
227 printf(" VROOT");
228 if (vn.v_flag & VTEXT)
229 printf(" VTEXT");
230 if (vn.v_flag & VSYSTEM)
231 printf(" VSYSTEM");
232 if (vn.v_flag & VISTTY)
233 printf(" VISTTY");
234 #ifdef VXLOCK
235 if (vn.v_flag & VXLOCK)
236 printf(" VXLOCK");
237 if (vn.v_flag & VXWANT)
238 printf(" VXWANT");
239 #endif
240 #ifdef VRECLAIMED
241 if (vn.v_flag & VRECLAIMED)
242 printf(" VRECLAIMED");
243 if (vn.v_flag & VINACTIVE)
244 printf(" VINACTIVE");
245 #endif
246 if (vn.v_flag & VOBJBUF)
247 printf(" VOBJBUF");
248 if (vn.v_flag & VAGE)
249 printf(" VAGE");
250 if (vn.v_flag & VOLOCK)
251 printf(" VOLOCK");
252 if (vn.v_flag & VOWANT)
253 printf(" VOWANT");
254 #ifdef VDOOMED
255 if (vn.v_flag & VDOOMED)
256 printf(" VDOOMED");
257 #endif
258 if (vn.v_flag & VFREE)
259 printf(" VFREE");
260 #ifdef VINFREE
261 if (vn.v_flag & VINFREE)
262 printf(" VINFREE");
263 #endif
264 if (vn.v_flag & VONWORKLST)
265 printf(" VONWORKLST");
266 if (vn.v_flag & VMOUNT)
267 printf(" VMOUNT");
268 if (vn.v_flag & VOBJDIRTY)
269 printf(" VOBJDIRTY");
270 if (vn.v_flag & VMAYHAVELOCKS)
271 printf(" VMAYHAVELOCKS");
273 printf("\n");
275 if (vn.v_lock.lk_sharecount || vn.v_lock.lk_waitcount ||
276 vn.v_lock.lk_exclusivecount || vn.v_lock.lk_lockholder != LK_NOTHREAD) {
277 printf("\tlk_flags %08x share %d wait %d excl %d holder = %p\n",
278 vn.v_lock.lk_flags, vn.v_lock.lk_sharecount,
279 vn.v_lock.lk_waitcount, vn.v_lock.lk_exclusivecount,
280 vn.v_lock.lk_lockholder);
283 if (withnames && TAILQ_FIRST(&vn.v_namecache)) {
284 struct namecache ncp;
285 int nlen;
286 char buf[1024];
288 kkread(kd, (u_long)TAILQ_FIRST(&vn.v_namecache), &ncp, sizeof(ncp));
289 if ((nlen = ncp.nc_nlen) >= sizeof(buf))
290 nlen = sizeof(buf) - 1;
291 if (nlen < 0)
292 nlen = 0;
293 if (nlen) {
294 kkread(kd, (u_long)ncp.nc_name, buf, nlen);
295 buf[nlen] = 0;
296 printf("\tfilename %s\n", buf);
300 if (tracebufs) {
301 if (vn.v_rbclean_tree.rbh_root) {
302 printf("\tCLEAN BUFFERS\n");
303 dumpbufs(kd, vn.v_rbclean_tree.rbh_root, "ROOT");
305 if (vn.v_rbdirty_tree.rbh_root) {
306 printf("\tDIRTY BUFFERS\n");
307 dumpbufs(kd, vn.v_rbdirty_tree.rbh_root, "ROOT");
311 if (tracelocks) {
312 if (vn.v_tag == VT_UFS && vn.v_data) {
313 struct inode *ip = vn.v_data;
314 struct lockf lockf;
316 kkread(kd, (u_long)&ip->i_lockf, &lockf, sizeof(lockf));
317 dumplocks(kd, &lockf);
322 if (whichlist)
323 return(vn.v_nmntvnodes.tqe_next);
324 else
325 return(vn.v_freelist.tqe_next);
328 static void
329 dumpbufs(kvm_t *kd, void *bufp, const char *id)
331 struct buf buf;
333 kkread(kd, (u_long)bufp, &buf, sizeof(buf));
334 printf("\t %-8s %p loffset %08llx foffset %08llx\n",
335 id, bufp,
336 buf.b_bio1.bio_offset,
337 buf.b_bio2.bio_offset);
339 if (buf.b_rbnode.rbe_left)
340 dumpbufs(kd, buf.b_rbnode.rbe_left, "LEFT");
341 if (buf.b_rbnode.rbe_right)
342 dumpbufs(kd, buf.b_rbnode.rbe_right, "RIGHT");
345 static void
346 dumplocks(kvm_t *kd, struct lockf *lockf)
348 struct lockf_range item;
349 struct lockf_range *scan;
351 if ((scan = TAILQ_FIRST(&lockf->lf_range)) != NULL) {
352 printf("\tLOCKS\n");
353 do {
354 kkread(kd, (u_long)scan, &item, sizeof(item));
355 dumplockinfo(kd, &item);
356 } while ((scan = TAILQ_NEXT(&item, lf_link)) != NULL);
357 printf("\n");
359 if ((scan = TAILQ_FIRST(&lockf->lf_blocked)) != NULL) {
360 printf("\tBLKED\n");
361 do {
362 kkread(kd, (u_long)scan, &item, sizeof(item));
363 dumplockinfo(kd, &item);
364 } while ((scan = TAILQ_NEXT(&item, lf_link)) != NULL);
365 printf("\n");
370 static void
371 dumplockinfo(kvm_t *kd, struct lockf_range *item)
373 int ownerpid;
375 if (item->lf_owner && (item->lf_flags & F_POSIX)) {
376 kkread(kd, (u_long)&item->lf_owner->p_pid,
377 &ownerpid, sizeof(ownerpid));
378 } else {
379 ownerpid = -1;
382 printf("\t ty=%d flgs=%04x %lld-%lld owner=%d\n",
383 item->lf_type, item->lf_flags,
384 item->lf_start, item->lf_end,
385 ownerpid
389 static
391 getobjpages(kvm_t *kd, struct vm_object *obj)
393 struct vm_object vmobj;
395 kkread(kd, (u_long)obj, &vmobj, sizeof(vmobj));
396 return(vmobj.resident_page_count);
399 static
401 getobjvnpsize(kvm_t *kd, struct vm_object *obj)
403 struct vm_object vmobj;
405 kkread(kd, (u_long)obj, &vmobj, sizeof(vmobj));
406 return ((int)vmobj.size);
409 static void
410 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
412 if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
413 perror("kvm_read");
414 exit(1);