kernel - swapcache - Fix snocache and cache flags propagation, fix PG_NOTMETA
[dragonfly.git] / test / debug / vnodeinfo.c
CommitLineData
fa12c626
MD
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 *
5c8e2a54
MD
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 *
d1a5d3fc 43 * $DragonFly: src/test/debug/vnodeinfo.c,v 1.13 2007/05/06 20:45:01 dillon Exp $
fa12c626
MD
44 */
45
78465ba9 46#define _KERNEL_STRUCTURES
fa12c626
MD
47#include <sys/param.h>
48#include <sys/user.h>
49#include <sys/malloc.h>
50#include <sys/signalvar.h>
bb87550c 51#include <sys/namecache.h>
fa12c626
MD
52#include <sys/mount.h>
53#include <sys/vnode.h>
af456e64 54#include <sys/buf.h>
fa12c626
MD
55
56#include <vm/vm.h>
57#include <vm/vm_page.h>
58#include <vm/vm_kern.h>
17ba2e5a 59#include <vm/vm_object.h>
fa12c626
MD
60#include <vm/swap_pager.h>
61#include <vm/vnode_pager.h>
62
3661e25d
MD
63#include <vfs/ufs/quota.h>
64#include <vfs/ufs/inode.h>
65
fa12c626
MD
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>
73
74struct nlist Nl[] = {
75 { "_mountlist" },
76 { "_vnode_free_list" },
77 { NULL }
78};
79
80static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
81static struct mount *dumpmount(kvm_t *kd, struct mount *mp);
82static struct vnode *dumpvp(kvm_t *kd, struct vnode *vp, int whichlist);
af456e64 83static void dumpbufs(kvm_t *kd, void *bufp, const char *id);
3661e25d
MD
84static void dumplocks(kvm_t *kd, struct lockf *lockf);
85static void dumplockinfo(kvm_t *kd, struct lockf_range *item);
17ba2e5a 86static int getobjpages(kvm_t *kd, struct vm_object *obj);
ada41611 87static int getobjvnpsize(kvm_t *kd, struct vm_object *obj);
fa12c626 88
af456e64 89int tracebufs = 0;
3661e25d 90int tracelocks = 0;
bb87550c 91int withnames = 0;
af456e64
MD
92
93int
fa12c626
MD
94main(int ac, char **av)
95{
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;
103
3661e25d 104 while ((ch = getopt(ac, av, "alnbM:N:")) != -1) {
fa12c626 105 switch(ch) {
af456e64
MD
106 case 'b':
107 tracebufs = 1;
108 break;
bb87550c
MD
109 case 'n':
110 withnames = 1;
111 break;
3661e25d
MD
112 case 'l':
113 tracelocks = 1;
114 break;
bb87550c
MD
115 case 'a':
116 tracebufs = 1;
3661e25d 117 tracelocks = 1;
bb87550c
MD
118 withnames = 1;
119 break;
fa12c626
MD
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);
129 }
130 }
131
132 if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
133 perror("kvm_open");
134 exit(1);
135 }
136 if (kvm_nlist(kd, Nl) != 0) {
137 perror("kvm_nlist");
138 exit(1);
139 }
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);
149}
150
151static struct mount *
152dumpmount(kvm_t *kd, struct mount *mp)
153{
154 struct mount mnt;
155 struct vnode *vp;
156
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);
7bc16b2d
MD
167 printf(" mnt_stat.f_fsid %08x %08x\n", mnt.mnt_stat.f_fsid.val[0],
168 mnt.mnt_stat.f_fsid.val[1]);
fa12c626
MD
169 vp = mnt.mnt_nvnodelist.tqh_first;
170 while (vp)
171 vp = dumpvp(kd, vp, 1);
172
173 printf("}\n");
174
175 return(mnt.mnt_list.tqe_next);
176}
177
178static const char *
179vtype(enum vtype type)
180{
181 static char buf[32];
182
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;
204 }
205 snprintf(buf, sizeof(buf), "%d", (int)type);
206 return(buf);
207}
208
209static struct vnode *
210dumpvp(kvm_t *kd, struct vnode *vp, int whichlist)
211{
212 struct vnode vn;
213
214 kkread(kd, (u_long)vp, &vn, sizeof(vn));
215
d1a5d3fc
MD
216 printf(" vnode %p usecnt %08x holdcnt %d type=%s flags %08x",
217 vp, vn.v_sysref.refcnt, vn.v_auxrefs, vtype(vn.v_type), vn.v_flag);
17ba2e5a
MD
218
219 if ((vn.v_flag & VOBJBUF) && vn.v_object) {
220 int npages = getobjpages(kd, vn.v_object);
ada41611
MD
221 int vnpsize = getobjvnpsize(kd, vn.v_object);
222 if (npages || vnpsize)
223 printf(" vmobjpgs=%d vnpsize=%d", npages, vnpsize);
17ba2e5a
MD
224 }
225
fa12c626 226 if (vn.v_flag & VROOT)
45136f35 227 printf(" VROOT");
fa12c626 228 if (vn.v_flag & VTEXT)
45136f35 229 printf(" VTEXT");
fa12c626 230 if (vn.v_flag & VSYSTEM)
45136f35 231 printf(" VSYSTEM");
fa12c626 232 if (vn.v_flag & VISTTY)
45136f35
MD
233 printf(" VISTTY");
234#ifdef VXLOCK
fa12c626 235 if (vn.v_flag & VXLOCK)
45136f35 236 printf(" VXLOCK");
fa12c626 237 if (vn.v_flag & VXWANT)
45136f35
MD
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
fa12c626 246 if (vn.v_flag & VOBJBUF)
45136f35 247 printf(" VOBJBUF");
aabd5ce8
MD
248#ifdef VSWAPCACHE
249 if (vn.v_flag & VSWAPCACHE)
250 printf(" VSWAPCACHE");
251#endif
0e8bd897
MD
252 switch(vn.v_flag & (VAGE0 | VAGE1)) {
253 case 0:
254 printf(" VAGE0");
255 break;
256 case VAGE0:
257 printf(" VAGE1");
258 break;
259 case VAGE1:
260 printf(" VAGE2");
261 break;
262 case VAGE0 | VAGE1:
263 printf(" VAGE3");
264 break;
265 }
fa12c626 266 if (vn.v_flag & VOLOCK)
45136f35 267 printf(" VOLOCK");
fa12c626 268 if (vn.v_flag & VOWANT)
45136f35
MD
269 printf(" VOWANT");
270#ifdef VDOOMED
fa12c626 271 if (vn.v_flag & VDOOMED)
45136f35
MD
272 printf(" VDOOMED");
273#endif
fa12c626 274 if (vn.v_flag & VFREE)
45136f35 275 printf(" VFREE");
d1a5d3fc
MD
276 if (vn.v_flag & VCACHED)
277 printf(" VCACHED");
45136f35 278#ifdef VINFREE
fa12c626 279 if (vn.v_flag & VINFREE)
45136f35
MD
280 printf(" VINFREE");
281#endif
fa12c626 282 if (vn.v_flag & VONWORKLST)
45136f35 283 printf(" VONWORKLST");
fa12c626 284 if (vn.v_flag & VMOUNT)
45136f35 285 printf(" VMOUNT");
fa12c626 286 if (vn.v_flag & VOBJDIRTY)
45136f35 287 printf(" VOBJDIRTY");
3661e25d
MD
288 if (vn.v_flag & VMAYHAVELOCKS)
289 printf(" VMAYHAVELOCKS");
17ba2e5a 290
fa12c626
MD
291 printf("\n");
292
293 if (vn.v_lock.lk_sharecount || vn.v_lock.lk_waitcount ||
294 vn.v_lock.lk_exclusivecount || vn.v_lock.lk_lockholder != LK_NOTHREAD) {
295 printf("\tlk_flags %08x share %d wait %d excl %d holder = %p\n",
296 vn.v_lock.lk_flags, vn.v_lock.lk_sharecount,
297 vn.v_lock.lk_waitcount, vn.v_lock.lk_exclusivecount,
298 vn.v_lock.lk_lockholder);
299 }
300
bb87550c
MD
301 if (withnames && TAILQ_FIRST(&vn.v_namecache)) {
302 struct namecache ncp;
303 int nlen;
304 char buf[1024];
305
306 kkread(kd, (u_long)TAILQ_FIRST(&vn.v_namecache), &ncp, sizeof(ncp));
307 if ((nlen = ncp.nc_nlen) >= sizeof(buf))
308 nlen = sizeof(buf) - 1;
309 if (nlen < 0)
310 nlen = 0;
311 if (nlen) {
312 kkread(kd, (u_long)ncp.nc_name, buf, nlen);
313 buf[nlen] = 0;
314 printf("\tfilename %s\n", buf);
315 }
316 }
317
af456e64
MD
318 if (tracebufs) {
319 if (vn.v_rbclean_tree.rbh_root) {
320 printf("\tCLEAN BUFFERS\n");
321 dumpbufs(kd, vn.v_rbclean_tree.rbh_root, "ROOT");
322 }
323 if (vn.v_rbdirty_tree.rbh_root) {
324 printf("\tDIRTY BUFFERS\n");
325 dumpbufs(kd, vn.v_rbdirty_tree.rbh_root, "ROOT");
326 }
327 }
328
3661e25d
MD
329 if (tracelocks) {
330 if (vn.v_tag == VT_UFS && vn.v_data) {
331 struct inode *ip = vn.v_data;
332 struct lockf lockf;
333
334 kkread(kd, (u_long)&ip->i_lockf, &lockf, sizeof(lockf));
335 dumplocks(kd, &lockf);
336 }
337 }
338
339
fa12c626
MD
340 if (whichlist)
341 return(vn.v_nmntvnodes.tqe_next);
342 else
343 return(vn.v_freelist.tqe_next);
344}
345
af456e64
MD
346static void
347dumpbufs(kvm_t *kd, void *bufp, const char *id)
348{
349 struct buf buf;
350
351 kkread(kd, (u_long)bufp, &buf, sizeof(buf));
fa06bda9 352 printf("\t %-8s %p loffset %012llx foffset %08llx",
af456e64 353 id, bufp,
e36e284d
MD
354 buf.b_bio1.bio_offset,
355 buf.b_bio2.bio_offset);
fa06bda9
MD
356 printf(" q=%d lck=%d/%d flags=%08x dep=%p",
357 buf.b_qindex, buf.b_lock.lk_sharecount,
358 buf.b_lock.lk_exclusivecount,
359 buf.b_flags, buf.b_dep.lh_first);
360 printf("\n");
af456e64
MD
361
362 if (buf.b_rbnode.rbe_left)
363 dumpbufs(kd, buf.b_rbnode.rbe_left, "LEFT");
364 if (buf.b_rbnode.rbe_right)
365 dumpbufs(kd, buf.b_rbnode.rbe_right, "RIGHT");
366}
367
3661e25d
MD
368static void
369dumplocks(kvm_t *kd, struct lockf *lockf)
370{
371 struct lockf_range item;
372 struct lockf_range *scan;
373
374 if ((scan = TAILQ_FIRST(&lockf->lf_range)) != NULL) {
375 printf("\tLOCKS\n");
376 do {
377 kkread(kd, (u_long)scan, &item, sizeof(item));
378 dumplockinfo(kd, &item);
379 } while ((scan = TAILQ_NEXT(&item, lf_link)) != NULL);
380 printf("\n");
381 }
382 if ((scan = TAILQ_FIRST(&lockf->lf_blocked)) != NULL) {
383 printf("\tBLKED\n");
384 do {
385 kkread(kd, (u_long)scan, &item, sizeof(item));
386 dumplockinfo(kd, &item);
387 } while ((scan = TAILQ_NEXT(&item, lf_link)) != NULL);
388 printf("\n");
389 }
390
391}
392
393static void
394dumplockinfo(kvm_t *kd, struct lockf_range *item)
395{
396 int ownerpid;
397
398 if (item->lf_owner && (item->lf_flags & F_POSIX)) {
399 kkread(kd, (u_long)&item->lf_owner->p_pid,
400 &ownerpid, sizeof(ownerpid));
401 } else {
402 ownerpid = -1;
403 }
404
405 printf("\t ty=%d flgs=%04x %lld-%lld owner=%d\n",
406 item->lf_type, item->lf_flags,
407 item->lf_start, item->lf_end,
408 ownerpid
409 );
410}
411
17ba2e5a
MD
412static
413int
414getobjpages(kvm_t *kd, struct vm_object *obj)
415{
416 struct vm_object vmobj;
417
418 kkread(kd, (u_long)obj, &vmobj, sizeof(vmobj));
419 return(vmobj.resident_page_count);
420}
421
ada41611
MD
422static
423int
424getobjvnpsize(kvm_t *kd, struct vm_object *obj)
425{
426 struct vm_object vmobj;
427
428 kkread(kd, (u_long)obj, &vmobj, sizeof(vmobj));
e36e284d 429 return ((int)vmobj.size);
ada41611
MD
430}
431
17ba2e5a 432static void
fa12c626
MD
433kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
434{
435 if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
436 perror("kvm_read");
437 exit(1);
438 }
439}
440