6467d3e1cfe515103fbb90a1de8fa7d26b3290a5
[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  */
44
45 #define _KERNEL_STRUCTURES
46 #include <sys/param.h>
47 #include <sys/user.h>
48 #include <sys/malloc.h>
49 #include <sys/signalvar.h>
50 #include <sys/namecache.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/buf.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_object.h>
59 #include <vm/swap_pager.h>
60 #include <vm/vnode_pager.h>
61
62 #include <vfs/ufs/quota.h>
63 #include <vfs/ufs/inode.h>
64
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <fcntl.h>
69 #include <kvm.h>
70 #include <nlist.h>
71 #include <getopt.h>
72
73 struct nlist Nl[] = {
74     { "_mountlist" },
75     { "_vnode_list_hash" },
76     { NULL }
77 };
78
79 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
80 static struct mount *dumpmount(kvm_t *kd, struct mount *mp);
81 static struct vnode *dumpvp(kvm_t *kd, struct vnode *vp, int whichlist, char *vfc_name);
82 static void dumpbufs(kvm_t *kd, void *bufp, const char *id);
83 static void dumplocks(kvm_t *kd, struct lockf *lockf);
84 static void dumplockinfo(kvm_t *kd, struct lockf_range *item);
85 static int getobjpages(kvm_t *kd, struct vm_object *obj);
86 static int getobjvnpsize(kvm_t *kd, struct vm_object *obj);
87
88 static const struct dump_private_data {
89         char vfc_name[MFSNAMELEN];
90         void (*dumpfn)(kvm_t *, void *);
91 } dumplist[] = {
92         { "", NULL }
93 };
94
95 int tracebufs = 0;
96 int tracelocks = 0;
97 int withnames = 0;
98 int fsprivate = 0;
99
100 int
101 main(int ac, char **av)
102 {
103     struct mount *mp;
104     struct vnode *vp_inactive;
105     struct vnode *vp_active;
106     struct vnode_index *vi;
107     kvm_t *kd;
108     int ch;
109     const char *corefile = NULL;
110     const char *sysfile = NULL;
111
112     while ((ch = getopt(ac, av, "alnbM:N:p")) != -1) {
113         switch(ch) {
114         case 'b':
115             tracebufs = 1;
116             break;
117         case 'n':
118             withnames = 1;
119             break;
120         case 'l':
121             tracelocks = 1;
122             break;
123         case 'a':
124             tracebufs = 1;
125             tracelocks = 1;
126             withnames = 1;
127             fsprivate = 1;
128             break;
129         case 'p':
130             fsprivate = 1;
131             break;
132         case 'M':
133             corefile = optarg;
134             break;
135         case 'N':
136             sysfile = optarg;
137             break;
138         default:
139             fprintf(stderr, "%s [-pbnla] [-M core] [-N system]\n", av[0]);
140             exit(1);
141         }
142     }
143
144     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
145         perror("kvm_open");
146         exit(1);
147     }
148     if (kvm_nlist(kd, Nl) != 0) {
149         perror("kvm_nlist");
150         exit(1);
151     }
152     kkread(kd, Nl[0].n_value, &mp, sizeof(mp));
153     while (mp)
154         mp = dumpmount(kd, mp);
155     kkread(kd, Nl[1].n_value, &vi, sizeof(struct vnode_index));
156     printf("INACTIVELIST {\n");
157     while (vp_inactive)
158             vp_inactive = dumpvp(kd, vi->inactive_list.tqh_first,
159                 0, NULL);
160     printf("}\n");
161     printf("ACTIVELIST {\n");
162     while (vp_active)
163             vp_active = dumpvp(kd, vi->active_list.tqh_first, 0, NULL);
164     printf("}\n");
165     return(0);
166 }
167
168 static struct mount *
169 dumpmount(kvm_t *kd, struct mount *mp)
170 {
171     struct mount mnt;
172     struct vnode *vp;
173     struct vfsconf vfc;
174
175     kkread(kd, (u_long)mp, &mnt, sizeof(mnt));
176     printf("MOUNTPOINT %s on %s {\n",
177         mnt.mnt_stat.f_mntfromname, mnt.mnt_stat.f_mntonname);
178     printf("    lk_flags %08x count %016jx holder = %p\n",
179         mnt.mnt_lock.lk_flags, mnt.mnt_lock.lk_count,
180         mnt.mnt_lock.lk_lockholder);
181     printf("    mnt_flag %08x mnt_kern_flag %08x\n",
182         mnt.mnt_flag, mnt.mnt_kern_flag);
183     printf("    mnt_nvnodelistsize %d\n", mnt.mnt_nvnodelistsize);
184     printf("    mnt_stat.f_fsid %08x %08x\n", mnt.mnt_stat.f_fsid.val[0],
185         mnt.mnt_stat.f_fsid.val[1]);
186
187     /* Dump fs private node data */
188     kkread(kd, (u_long)mnt.mnt_vfc, &vfc, sizeof(vfc));
189     vp = mnt.mnt_nvnodelist.tqh_first;
190     while (vp)
191             vp = dumpvp(kd, vp, 1, vfc.vfc_name);
192
193     printf("}\n");
194
195     return(mnt.mnt_list.tqe_next);
196 }
197
198 static const char *
199 vtype(enum vtype type)
200 {
201     static char buf[32];
202
203     switch(type) {
204     case VNON:
205         return("VNON");
206     case VREG:
207         return("VREG");
208     case VDIR:
209         return("VDIR");
210     case VBLK:
211         return("VBLK");
212     case VCHR:
213         return("VCHR");
214     case VLNK:
215         return("VLNK");
216     case VSOCK:
217         return("VSOCK");
218     case VFIFO:
219         return("VFIFO");
220     case VBAD:
221         return("VBAD");
222     default:
223         break;
224     }
225     snprintf(buf, sizeof(buf), "%d", (int)type);
226     return(buf);
227 }
228
229 static struct vnode *
230 dumpvp(kvm_t *kd, struct vnode *vp, int whichlist, char *vfc_name)
231 {
232     struct vnode vn;
233
234     kkread(kd, (u_long)vp, &vn, sizeof(vn));
235
236     printf("    vnode %p.%d refcnt %08x auxcnt %d type=%s flags %08x",
237         vp, vn.v_state, vn.v_refcnt, vn.v_auxrefs, vtype(vn.v_type), vn.v_flag);
238
239     if ((vn.v_flag & VOBJBUF) && vn.v_object) {
240         int npages = getobjpages(kd, vn.v_object);
241         int vnpsize = getobjvnpsize(kd, vn.v_object);
242         if (npages || vnpsize)
243             printf(" vmobjpgs=%d vnpsize=%d", npages, vnpsize);
244     }
245
246     if (vn.v_flag & VROOT)
247         printf(" VROOT");
248     if (vn.v_flag & VTEXT)
249         printf(" VTEXT");
250     if (vn.v_flag & VSYSTEM)
251         printf(" VSYSTEM");
252     if (vn.v_flag & VISTTY)
253         printf(" VISTTY");
254 #ifdef VXLOCK
255     if (vn.v_flag & VXLOCK)
256         printf(" VXLOCK");
257     if (vn.v_flag & VXWANT)
258         printf(" VXWANT");
259 #endif
260 #ifdef VRECLAIMED
261     if (vn.v_flag & VRECLAIMED)
262         printf(" VRECLAIMED");
263     if (vn.v_flag & VINACTIVE)
264         printf(" VINACTIVE");
265 #endif
266     if (vn.v_flag & VOBJBUF)
267         printf(" VOBJBUF");
268 #ifdef VSWAPCACHE
269     if (vn.v_flag & VSWAPCACHE)
270         printf(" VSWAPCACHE");
271 #endif
272     switch(vn.v_flag & (VAGE0 | VAGE1)) {
273     case 0:
274         printf(" VAGE0");
275         break;
276     case VAGE0:
277         printf(" VAGE1");
278         break;
279     case VAGE1:
280         printf(" VAGE2");
281         break;
282     case VAGE0 | VAGE1:
283         printf(" VAGE3");
284         break;
285     }
286 #ifdef VDOOMED
287     if (vn.v_flag & VDOOMED)
288         printf(" VDOOMED");
289 #endif
290 #ifdef VINFREE
291     if (vn.v_flag & VINFREE)
292         printf(" VINFREE");
293 #endif
294     if (vn.v_flag & VONWORKLST)
295         printf(" VONWORKLST");
296     if (vn.v_flag & VOBJDIRTY)
297         printf(" VOBJDIRTY");
298     if (vn.v_flag & VMAYHAVELOCKS)
299         printf(" VMAYHAVELOCKS");
300
301     printf("\n");
302
303     if (vn.v_lock.lk_count || vn.v_lock.lk_lockholder != NULL) {
304         printf("\tlk_flags %08x count %016jx holder = %p\n",
305             vn.v_lock.lk_flags, vn.v_lock.lk_count,
306             vn.v_lock.lk_lockholder);
307     }
308
309     if (withnames && TAILQ_FIRST(&vn.v_namecache)) {
310         struct namecache ncp;
311         int nlen;
312         char buf[1024];
313
314         kkread(kd, (u_long)TAILQ_FIRST(&vn.v_namecache), &ncp, sizeof(ncp));
315         if ((nlen = ncp.nc_nlen) >= sizeof(buf))
316                 nlen = sizeof(buf) - 1;
317         if (nlen < 0)
318                 nlen = 0;
319         if (nlen) {
320                 kkread(kd, (u_long)ncp.nc_name, buf, nlen);
321                 buf[nlen] = 0;
322                 printf("\tfilename %s\n", buf);
323         }
324     }
325
326     if (tracebufs) {
327         if (vn.v_rbclean_tree.rbh_root) {
328             printf("\tCLEAN BUFFERS\n");
329             dumpbufs(kd, vn.v_rbclean_tree.rbh_root, "ROOT");
330         }
331         if (vn.v_rbdirty_tree.rbh_root) {
332             printf("\tDIRTY BUFFERS\n");
333             dumpbufs(kd, vn.v_rbdirty_tree.rbh_root, "ROOT");
334         }
335     }
336
337     if (tracelocks) {
338         if (vn.v_tag == VT_UFS && vn.v_data) {
339             struct inode *ip = vn.v_data;
340             struct lockf lockf;
341
342             kkread(kd, (u_long)&ip->i_lockf, &lockf, sizeof(lockf));
343             dumplocks(kd, &lockf);
344         }
345     }
346
347     if (fsprivate && vfc_name) {
348             /*
349              * Actually find whether the filesystem can dump
350              * detailed inode information out of the vnode
351              */
352             const struct dump_private_data *dpd;
353
354             for (dpd = dumplist; dpd->dumpfn != NULL; dpd++) {
355                     if ((strcmp(dpd->vfc_name, vfc_name) == 0) &&
356                         vn.v_data != NULL)
357                             dpd->dumpfn(kd, vn.v_data);
358             }
359     }
360
361     if (whichlist)
362         return(vn.v_nmntvnodes.tqe_next);
363     else
364         return(vn.v_list.tqe_next);
365 }
366
367 static void
368 dumpbufs(kvm_t *kd, void *bufp, const char *id)
369 {
370         struct buf buf;
371
372         kkread(kd, (u_long)bufp, &buf, sizeof(buf));
373         printf("\t    %-8s %p loffset %012lx/%05x foffset %08lx",
374                 id, bufp,
375                 buf.b_bio1.bio_offset,
376                 buf.b_bufsize,
377                 buf.b_bio2.bio_offset);
378         printf(" q=%d count=%016jx flags=%08x refs=%08x dep=%p",
379                 buf.b_qindex, buf.b_lock.lk_count,
380                 buf.b_flags, buf.b_refs, buf.b_dep.lh_first);
381         printf("\n");
382
383         if (buf.b_rbnode.rbe_left)
384             dumpbufs(kd, buf.b_rbnode.rbe_left, "LEFT");
385         if (buf.b_rbnode.rbe_right)
386             dumpbufs(kd, buf.b_rbnode.rbe_right, "RIGHT");
387 }
388
389 static void
390 dumplocks(kvm_t *kd, struct lockf *lockf)
391 {
392         struct lockf_range item;
393         struct lockf_range *scan;
394
395         if ((scan = TAILQ_FIRST(&lockf->lf_range)) != NULL) {
396                 printf("\tLOCKS\n");
397                 do {
398                         kkread(kd, (u_long)scan, &item, sizeof(item));
399                         dumplockinfo(kd, &item);
400                 } while ((scan = TAILQ_NEXT(&item, lf_link)) != NULL);
401                 printf("\n");
402         }
403         if ((scan = TAILQ_FIRST(&lockf->lf_blocked)) != NULL) {
404                 printf("\tBLKED\n");
405                 do {
406                         kkread(kd, (u_long)scan, &item, sizeof(item));
407                         dumplockinfo(kd, &item);
408                 } while ((scan = TAILQ_NEXT(&item, lf_link)) != NULL);
409                 printf("\n");
410         }
411
412 }
413
414 static void
415 dumplockinfo(kvm_t *kd, struct lockf_range *item)
416 {
417         int ownerpid;
418
419         if (item->lf_owner && (item->lf_flags & F_POSIX)) {
420                 kkread(kd, (u_long)&item->lf_owner->p_pid,
421                         &ownerpid, sizeof(ownerpid));
422         } else {
423                 ownerpid = -1;
424         }
425
426         printf("\t    ty=%d flgs=%04x %ld-%ld owner=%d\n",
427                 item->lf_type, item->lf_flags,
428                 item->lf_start, item->lf_end,
429                 ownerpid
430         );
431 }
432
433 static
434 int
435 getobjpages(kvm_t *kd, struct vm_object *obj)
436 {
437         struct vm_object vmobj;
438
439         kkread(kd, (u_long)obj, &vmobj, sizeof(vmobj));
440         return(vmobj.resident_page_count);
441 }
442
443 static
444 int
445 getobjvnpsize(kvm_t *kd, struct vm_object *obj)
446 {
447         struct vm_object vmobj;
448
449         kkread(kd, (u_long)obj, &vmobj, sizeof(vmobj));
450         return ((int)vmobj.size);
451 }
452
453 static void
454 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
455 {
456     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
457         perror("kvm_read");
458         exit(1);
459     }
460 }