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_real; 77 vm_offset_t total_used_byid[VM_SUBSYS_LIMIT]; 78 79 static const char *formatnum(int64_t value); 80 static const char *entryid(vm_subsys_t id, int *realmemp); 81 static void kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes); 82 static void mapscan(kvm_t *kd, vm_map_entry_t kptr, vm_map_entry_t ken, 83 vm_offset_t *lastp); 84 85 int 86 main(int ac, char **av) 87 { 88 const char *corefile = NULL; 89 const char *sysfile = NULL; 90 kvm_t *kd; 91 int ch; 92 int i; 93 vm_offset_t last; 94 struct vm_map_entry entry; 95 struct vm_map_entry *kptr; 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.min_addr; 129 130 kptr = kvm_vm_map_entry_first(kd, &kmap, &entry); 131 while (kptr) { 132 mapscan(kd, kptr, &entry, &last); 133 kptr = kvm_vm_map_entry_next(kd, kptr, &entry); 134 } 135 136 printf("%4ldM 0x%016jx %08lx-%08lx (%6s) EMPTY\n", 137 total_used / 1024 / 1024, 138 (intmax_t)NULL, 139 last, kmap.max_addr, 140 formatnum(kmap.max_addr - last)); 141 total_empty += kmap.max_addr - last; 142 143 printf("-----------------------------------------------\n"); 144 for (i = 0; i < VM_SUBSYS_LIMIT; ++i) { 145 int realmem; 146 const char *id = entryid(i, &realmem); 147 148 printf("Total-id: %9s %s%s\n", 149 id, 150 formatnum(total_used_byid[i]), 151 (realmem ? " (real memory)" : "")); 152 } 153 154 printf("-----------------------------------------------\n"); 155 printf("Total empty space: %s\n", formatnum(total_empty)); 156 printf("Total used space: %s\n", formatnum(total_used)); 157 printf("Total real space: %s\n", formatnum(total_real)); 158 } 159 160 static const char * 161 formatnum(int64_t value) 162 { 163 static char buf[64]; 164 const char *neg; 165 const char *suffix; 166 int64_t div = 1; 167 168 if (value < 0) { 169 value = -value; 170 neg = "-"; 171 } else { 172 neg = ""; 173 } 174 if (value < 100000) { 175 div = 1; 176 suffix = ""; 177 } else if (value < 1 * 1024 * 1024) { 178 div = 1024; 179 suffix = "K"; 180 } else if (value < 1024 * 1024 * 1024) { 181 div = 1024 * 1024; 182 suffix = "M"; 183 } else if (value < 1024LL * 1024 * 1024 * 1024) { 184 div = 1024 * 1024 * 1024; 185 suffix = "G"; 186 } else { 187 div = 1024LL * 1024 * 1024 * 1024; 188 suffix = "T"; 189 } 190 if (value == 0) { 191 snprintf(buf, sizeof(buf), ""); 192 } else if (div == 1) { 193 snprintf(buf, sizeof(buf), "%s%7.0f%s", 194 neg, 195 (double)value / (double)div, 196 suffix); 197 } else { 198 snprintf(buf, sizeof(buf), "%s%6.2f%s", 199 neg, 200 (double)value / (double)div, 201 suffix); 202 } 203 return buf; 204 } 205 206 static void 207 mapscan(kvm_t *kd, vm_map_entry_t kptr, vm_map_entry_t ken, vm_offset_t *lastp) 208 { 209 int realmem; 210 211 if (*lastp != ken->ba.start) { 212 printf("%4ldM %p %08lx-%08lx (%s) EMPTY\n", 213 total_used / 1024 / 1024, 214 kptr, 215 *lastp, ken->ba.start, 216 formatnum(ken->ba.start - *lastp)); 217 total_empty += ken->ba.start - *lastp; 218 } 219 printf("%4ldM %p %08lx-%08lx (%6ldK) id=%-8s object=%p\n", 220 total_used / 1024 / 1024, 221 kptr, 222 ken->ba.start, ken->ba.end, 223 (ken->ba.end - ken->ba.start) / 1024, 224 entryid(ken->id, &realmem), 225 ken->ba.map_object); 226 total_used += ken->ba.end - ken->ba.start; 227 228 if (ken->id < VM_SUBSYS_LIMIT) 229 total_used_byid[ken->id] += ken->ba.end - ken->ba.start; 230 else 231 total_used_byid[0] += ken->ba.end - ken->ba.start; 232 233 if (realmem) 234 total_real += ken->ba.end - ken->ba.start; 235 236 *lastp = ken->ba.end; 237 } 238 239 static 240 const char * 241 entryid(vm_subsys_t id, int *realmemp) 242 { 243 static char buf[32]; 244 int dummy = 0; 245 int *realmem = (realmemp ? realmemp : &dummy); 246 247 *realmem = 0; 248 249 switch(id) { 250 case VM_SUBSYS_UNKNOWN: 251 return("UNKNOWN"); 252 case VM_SUBSYS_KMALLOC: 253 *realmem = 1; 254 return("KMALLOC"); 255 case VM_SUBSYS_STACK: 256 *realmem = 1; 257 return("STACK"); 258 case VM_SUBSYS_IMGACT: 259 return("IMGACT"); 260 case VM_SUBSYS_EFI: 261 return("EFI"); 262 case VM_SUBSYS_RESERVED: 263 *realmem = 1; 264 return("BOOT+KERN"); 265 case VM_SUBSYS_INIT: 266 return("INIT"); 267 case VM_SUBSYS_PIPE: 268 return("PIPE"); 269 case VM_SUBSYS_PROC: 270 return("PROC"); 271 case VM_SUBSYS_SHMEM: 272 return("SHMEM"); 273 case VM_SUBSYS_SYSMAP: 274 return("SYSMAP"); 275 case VM_SUBSYS_MMAP: 276 return("MMAP"); 277 case VM_SUBSYS_BRK: 278 return("BRK"); 279 case VM_SUBSYS_BOGUS: 280 return("BOGUS"); 281 case VM_SUBSYS_BUF: 282 *realmem = 1; 283 return("BUF"); 284 case VM_SUBSYS_BUFDATA: 285 return("BUFDATA"); 286 case VM_SUBSYS_GD: 287 *realmem = 1; 288 return("GD"); 289 case VM_SUBSYS_IPIQ: 290 *realmem = 1; 291 return("IPIQ"); 292 case VM_SUBSYS_PVENTRY: 293 return("PVENTRY"); 294 case VM_SUBSYS_PML4: 295 *realmem = 1; 296 return("PML4"); 297 case VM_SUBSYS_MAPDEV: 298 return("MAPDEV"); 299 case VM_SUBSYS_ZALLOC: 300 return("ZALLOC"); 301 302 case VM_SUBSYS_DM: 303 return("DM"); 304 case VM_SUBSYS_CONTIG: 305 return("CONTIG"); 306 case VM_SUBSYS_DRM: 307 return("DRM"); 308 case VM_SUBSYS_DRM_GEM: 309 return("DRM_GEM"); 310 case VM_SUBSYS_DRM_SCAT: 311 return("DRM_SCAT"); 312 case VM_SUBSYS_DRM_VMAP: 313 *realmem = 1; 314 return("DRM_VMAP"); 315 case VM_SUBSYS_DRM_TTM: 316 return("DRM_TTM"); 317 case VM_SUBSYS_HAMMER: 318 return("HAMMER"); 319 case VM_SUBSYS_VMPGHASH: 320 *realmem = 1; 321 return("VMPGHASH"); 322 default: 323 break; 324 } 325 snprintf(buf, sizeof(buf), "%d", (int)id); 326 327 return buf; 328 } 329 330 331 static void 332 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes) 333 { 334 if (kvm_read(kd, addr, buf, nbytes) != nbytes) { 335 perror("kvm_read"); 336 exit(1); 337 } 338 } 339