Merge branch 'vendor/GREP'
[dragonfly.git] / test / debug / kmapinfo.c
1 /*
2  * KMAPINFO.C
3  *
4  * cc -I/usr/src/sys kmapinfo.c -o /usr/local/bin/kmapinfo -lkvm
5  *
6  * Dump the kernel_map
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/vnode.h>
47 #include <sys/namecache.h>
48
49 #include <vm/vm.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_map.h>
55 #include <vm/swap_pager.h>
56 #include <vm/vnode_pager.h>
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <fcntl.h>
62 #include <kvm.h>
63 #include <nlist.h>
64 #include <getopt.h>
65
66 struct nlist Nl[] = {
67     { "_kernel_map" },
68     { NULL }
69 };
70
71 int debugopt;
72 int verboseopt;
73 struct vm_map kmap;
74 vm_offset_t total_empty;
75 vm_offset_t total_used;
76
77 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
78 static void mapscan(kvm_t *kd, vm_map_entry_t entry,
79                     vm_offset_t *lastp);
80
81 int
82 main(int ac, char **av)
83 {
84     const char *corefile = NULL;
85     const char *sysfile = NULL;
86     vm_page_t mptr;
87     struct vm_page m;
88     struct vm_object obj;
89     kvm_t *kd;
90     int ch;
91     int hv;
92     int i;
93     const char *qstr;
94     const char *ostr;
95     vm_offset_t last;
96
97     while ((ch = getopt(ac, av, "M:N:dv")) != -1) {
98         switch(ch) {
99         case 'd':
100             ++debugopt;
101             break;
102         case 'v':
103             ++verboseopt;
104             break;
105         case 'M':
106             corefile = optarg;
107             break;
108         case 'N':
109             sysfile = optarg;
110             break;
111         default:
112             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
113             exit(1);
114         }
115     }
116     ac -= optind;
117     av += optind;
118
119     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
120         perror("kvm_open");
121         exit(1);
122     }
123     if (kvm_nlist(kd, Nl) != 0) {
124         perror("kvm_nlist");
125         exit(1);
126     }
127     kkread(kd, Nl[0].n_value, &kmap, sizeof(kmap));
128     last = kmap.header.start;
129     mapscan(kd, kmap.rb_root.rbh_root, &last);
130
131     printf("%4ldM 0x%016jx %08lx-%08lx (%6ldK) EMPTY\n",
132         total_used / 1024 / 1024,
133         (intmax_t)NULL,
134         last, kmap.header.end,
135         (kmap.header.end - last) / 1024);
136     total_empty += kmap.header.end - last;
137
138     printf("-----------------------------------------------\n");
139     printf("Total empty space: %7ldK\n", total_empty / 1024);
140     printf("Total used  space: %7ldK\n", total_used / 1024);
141 }
142
143 static void
144 mapscan(kvm_t *kd, vm_map_entry_t entryp, vm_offset_t *lastp)
145 {
146     struct vm_map_entry entry;
147
148     if (entryp == NULL)
149         return;
150     kkread(kd, (u_long)entryp, &entry, sizeof(entry));
151     mapscan(kd, entry.rb_entry.rbe_left, lastp);
152     if (*lastp != entry.start) {
153             printf("%4ldM %p %08lx-%08lx (%6ldK) EMPTY\n",
154                 total_used / 1024 / 1024,
155                 entryp,
156                 *lastp, entry.start,
157                 (entry.start - *lastp) / 1024);
158             total_empty += entry.start - *lastp;
159     }
160     printf("%4ldM %p %08lx-%08lx (%6ldK) type=%d object=%p\n",
161         total_used / 1024 / 1024,
162         entryp,
163         entry.start, entry.end,
164         (entry.end - entry.start) / 1024,
165         entry.maptype,
166         entry.object);
167     total_used += entry.end - entry.start;
168     *lastp = entry.end;
169     mapscan(kd, entry.rb_entry.rbe_right, lastp);
170 }
171
172 static void
173 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
174 {
175     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
176         perror("kvm_read");
177         exit(1);
178     }
179 }