xref: /dragonfly/test/debug/kmapinfo.c (revision 1b1d847f)
1 /*
2  * KMAPINFO.C
3  *
4  * cc -I/usr/src/sys kmapinfo.c -o ~/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 vm_offset_t total_used_byid[VM_SUBSYS_LIMIT];
77 
78 static const char *formatnum(int64_t value);
79 static const char *entryid(vm_subsys_t id);
80 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes);
81 static void mapscan(kvm_t *kd, vm_map_entry_t kptr, vm_map_entry_t ken,
82 		    vm_offset_t *lastp);
83 
84 int
85 main(int ac, char **av)
86 {
87     const char *corefile = NULL;
88     const char *sysfile = NULL;
89     kvm_t *kd;
90     int ch;
91     int i;
92     vm_offset_t last;
93     struct vm_map_entry entry;
94     struct vm_map_entry *kptr;
95 
96     while ((ch = getopt(ac, av, "M:N:dv")) != -1) {
97 	switch(ch) {
98 	case 'd':
99 	    ++debugopt;
100 	    break;
101 	case 'v':
102 	    ++verboseopt;
103 	    break;
104 	case 'M':
105 	    corefile = optarg;
106 	    break;
107 	case 'N':
108 	    sysfile = optarg;
109 	    break;
110 	default:
111 	    fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
112 	    exit(1);
113 	}
114     }
115     ac -= optind;
116     av += optind;
117 
118     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
119 	perror("kvm_open");
120 	exit(1);
121     }
122     if (kvm_nlist(kd, Nl) != 0) {
123 	perror("kvm_nlist");
124 	exit(1);
125     }
126     kkread(kd, Nl[0].n_value, &kmap, sizeof(kmap));
127     last = kmap.min_addr;
128 
129     kptr = kvm_vm_map_entry_first(kd, &kmap, &entry);
130     while (kptr) {
131 	mapscan(kd, kptr, &entry, &last);
132 	kptr = kvm_vm_map_entry_next(kd, kptr, &entry);
133     }
134 
135     printf("%4ldM 0x%016jx %08lx-%08lx (%6s) EMPTY\n",
136 	total_used / 1024 / 1024,
137 	(intmax_t)NULL,
138 	last, kmap.max_addr,
139 	formatnum(kmap.max_addr - last));
140     total_empty += kmap.max_addr - last;
141 
142     printf("-----------------------------------------------\n");
143     for (i = 0; i < VM_SUBSYS_LIMIT; ++i)
144 	printf("Total-id: %9s %s\n", entryid(i), formatnum(total_used_byid[i]));
145 
146     printf("-----------------------------------------------\n");
147     printf("Total empty space: %s\n", formatnum(total_empty));
148     printf("Total used  space: %s\n", formatnum(total_used));
149 }
150 
151 static const char *
152 formatnum(int64_t value)
153 {
154 	static char buf[64];
155 	const char *neg;
156 	const char *suffix;
157 	int64_t div = 1;
158 
159 	if (value < 0) {
160 		value = -value;
161 		neg = "-";
162 	} else {
163 		neg = "";
164 	}
165 	if (value < 100000) {
166 		div = 1;
167 		suffix = "";
168 	} else if (value < 1 * 1024 * 1024) {
169 		div = 1024;
170 		suffix = "K";
171 	} else if (value < 1024 * 1024 * 1024) {
172 		div = 1024 * 1024;
173 		suffix = "M";
174 	} else if (value < 1024LL * 1024 * 1024 * 1024) {
175 		div = 1024 * 1024 * 1024;
176 		suffix = "G";
177 	} else {
178 		div = 1024LL * 1024 * 1024 * 1024;
179 		suffix = "T";
180 	}
181 	if (value == 0) {
182 		snprintf(buf, sizeof(buf), "");
183 	} else if (div == 1) {
184 		snprintf(buf, sizeof(buf), "%s%7.0f%s",
185 			 neg,
186 			 (double)value / (double)div,
187 			 suffix);
188 	} else {
189 		snprintf(buf, sizeof(buf), "%s%6.2f%s",
190 			 neg,
191 			 (double)value / (double)div,
192 			 suffix);
193 	}
194 	return buf;
195 }
196 
197 static void
198 mapscan(kvm_t *kd, vm_map_entry_t kptr, vm_map_entry_t ken, vm_offset_t *lastp)
199 {
200     if (*lastp != ken->start) {
201 	    printf("%4ldM %p %08lx-%08lx (%s) EMPTY\n",
202 		total_used / 1024 / 1024,
203 		kptr,
204 		*lastp, ken->start,
205 		formatnum(ken->start - *lastp));
206 	    total_empty += ken->start - *lastp;
207     }
208     printf("%4ldM %p %08lx-%08lx (%6ldK) id=%-8s object=%p\n",
209 	total_used / 1024 / 1024,
210 	kptr,
211 	ken->start, ken->end,
212 	(ken->end - ken->start) / 1024,
213 	entryid(ken->id),
214 	ken->object.map_object);
215     total_used += ken->end - ken->start;
216     if (ken->id < VM_SUBSYS_LIMIT)
217 	total_used_byid[ken->id] += ken->end - ken->start;
218     else
219 	total_used_byid[0] += ken->end - ken->start;
220     *lastp = ken->end;
221 }
222 
223 static
224 const char *
225 entryid(vm_subsys_t id)
226 {
227 	static char buf[32];
228 
229 	switch(id) {
230 	case VM_SUBSYS_UNKNOWN:
231 		return("UNKNOWN");
232 	case VM_SUBSYS_KMALLOC:
233 		return("KMALLOC");
234 	case VM_SUBSYS_STACK:
235 		return("STACK");
236 	case VM_SUBSYS_IMGACT:
237 		return("IMGACT");
238 	case VM_SUBSYS_EFI:
239 		return("EFI");
240 	case VM_SUBSYS_RESERVED:
241 		return("RESERVED");
242 	case VM_SUBSYS_INIT:
243 		return("INIT");
244 	case VM_SUBSYS_PIPE:
245 		return("PIPE");
246 	case VM_SUBSYS_PROC:
247 		return("PROC");
248 	case VM_SUBSYS_SHMEM:
249 		return("SHMEM");
250 	case VM_SUBSYS_SYSMAP:
251 		return("SYSMAP");
252 	case VM_SUBSYS_MMAP:
253 		return("MMAP");
254 	case VM_SUBSYS_BRK:
255 		return("BRK");
256 	case VM_SUBSYS_BOGUS:
257 		return("BOGUS");
258 	case VM_SUBSYS_BUF:
259 		return("BUF");
260 	case VM_SUBSYS_BUFDATA:
261 		return("BUFDATA");
262 	case VM_SUBSYS_GD:
263 		return("GD");
264 	case VM_SUBSYS_IPIQ:
265 		return("IPIQ");
266 	case VM_SUBSYS_PVENTRY:
267 		return("PVENTRY");
268 	case VM_SUBSYS_PML4:
269 		return("PML4");
270 	case VM_SUBSYS_MAPDEV:
271 		return("MAPDEV");
272 	case VM_SUBSYS_ZALLOC:
273 		return("ZALLOC");
274 
275 	case VM_SUBSYS_DM:
276 		return("DM");
277 	case VM_SUBSYS_CONTIG:
278 		return("CONTIG");
279 	case VM_SUBSYS_DRM:
280 		return("DRM");
281 	case VM_SUBSYS_DRM_GEM:
282 		return("DRM_GEM");
283 	case VM_SUBSYS_DRM_SCAT:
284 		return("DRM_SCAT");
285 	case VM_SUBSYS_DRM_VMAP:
286 		return("DRM_VMAP");
287 	case VM_SUBSYS_DRM_TTM:
288 		return("DRM_TTM");
289 	case VM_SUBSYS_HAMMER:
290 		return("HAMMER");
291 	case VM_SUBSYS_VMPGHASH:
292 		return("VMPGHASH");
293 	default:
294 		break;
295 	}
296 	snprintf(buf, sizeof(buf), "%d", (int)id);
297 
298 	return buf;
299 }
300 
301 
302 static void
303 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes)
304 {
305     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
306         perror("kvm_read");
307         exit(1);
308     }
309 }
310