Merge branch 'vendor/BYACC'
[dragonfly.git] / test / debug / vmobjinfo.c
1 /*
2  * VMOBJINFO.C
3  *
4  * cc -I/usr/src/sys vmobjinfo.c -o /usr/local/bin/vmobjinfo -lkvm
5  *
6  * Dump all vm_object's in the system
7  *
8  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
9  *
10  * This code is derived from software contributed to The DragonFly Project
11  * by Matthew Dillon <dillon@backplane.com>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in
21  *    the documentation and/or other materials provided with the
22  *    distribution.
23  * 3. Neither the name of The DragonFly Project nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific, prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
31  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
35  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
37  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40
41 #define _KERNEL_STRUCTURES
42 #include <sys/param.h>
43 #include <sys/user.h>
44 #include <sys/malloc.h>
45 #include <sys/signalvar.h>
46 #include <sys/namecache.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/buf.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_kern.h>
54 #include <vm/vm_object.h>
55 #include <vm/swap_pager.h>
56 #include <vm/vnode_pager.h>
57
58 #include <vfs/ufs/quota.h>
59 #include <vfs/ufs/inode.h>
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <fcntl.h>
65 #include <kvm.h>
66 #include <nlist.h>
67 #include <getopt.h>
68
69 TAILQ_HEAD(object_q, vm_object);
70
71 struct nlist Nl[] = {
72     { "_vm_object_list" },
73     { NULL }
74 };
75
76 static void scan_vmobjs(kvm_t *kd, struct object_q *obj_list);
77 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
78
79 int
80 main(int ac, char **av)
81 {
82     struct object_q obj_list;
83     kvm_t *kd;
84     int i;
85     int ch;
86     const char *corefile = NULL;
87     const char *sysfile = NULL;
88
89     while ((ch = getopt(ac, av, "M:N:")) != -1) {
90         switch(ch) {
91         case 'M':
92             corefile = optarg;
93             break;
94         case 'N':
95             sysfile = optarg;
96             break;
97         default:
98             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
99             exit(1);
100         }
101     }
102
103     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
104         perror("kvm_open");
105         exit(1);
106     }
107     if (kvm_nlist(kd, Nl) != 0) {
108         perror("kvm_nlist");
109         exit(1);
110     }
111     kkread(kd, Nl[0].n_value, &obj_list, sizeof(obj_list));
112     scan_vmobjs(kd, &obj_list);
113     return(0);
114 }
115
116 static void
117 scan_vmobjs(kvm_t *kd, struct object_q *obj_list)
118 {
119     struct vm_object *op;
120     struct vm_object obj;
121
122     op = TAILQ_FIRST(obj_list);
123     while (op) {
124         kkread(kd, (long)op, &obj, sizeof(obj));
125
126         printf("%p type=%d size=%016jx handle=%p swblocks=%d\n",
127                 op, obj.type, (intmax_t)obj.size, obj.handle,
128                 obj.swblock_count);
129
130         op = TAILQ_NEXT(&obj, object_list);
131     }
132 }
133
134 static void
135 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
136 {
137     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
138         perror("kvm_read");
139         exit(1);
140     }
141 }