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_object.h> 53 #include <vm/vm_map.h> 54 #include <vm/swap_pager.h> 55 #include <vm/vnode_pager.h> 56 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <fcntl.h> 61 #include <kvm.h> 62 #include <nlist.h> 63 #include <getopt.h> 64 65 struct nlist Nl[] = { 66 { "_kernel_map" }, 67 { NULL } 68 }; 69 70 int debugopt; 71 int verboseopt; 72 struct vm_map kmap; 73 vm_offset_t total_empty; 74 vm_offset_t total_used; 75 vm_offset_t total_real; 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, int *realmemp); 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 int realmem; 145 const char *id = entryid(i, &realmem); 146 147 printf("Total-id: %9s %s%s\n", 148 id, 149 formatnum(total_used_byid[i]), 150 (realmem ? " (real memory)" : "")); 151 } 152 153 printf("-----------------------------------------------\n"); 154 printf("Total empty space: %s\n", formatnum(total_empty)); 155 printf("Total used space: %s\n", formatnum(total_used)); 156 printf("Total real space: %s\n", formatnum(total_real)); 157 } 158 159 static const char * 160 formatnum(int64_t value) 161 { 162 static char buf[64]; 163 const char *neg; 164 const char *suffix; 165 int64_t div = 1; 166 167 if (value < 0) { 168 value = -value; 169 neg = "-"; 170 } else { 171 neg = ""; 172 } 173 if (value < 100000) { 174 div = 1; 175 suffix = ""; 176 } else if (value < 1 * 1024 * 1024) { 177 div = 1024; 178 suffix = "K"; 179 } else if (value < 1024 * 1024 * 1024) { 180 div = 1024 * 1024; 181 suffix = "M"; 182 } else if (value < 1024LL * 1024 * 1024 * 1024) { 183 div = 1024 * 1024 * 1024; 184 suffix = "G"; 185 } else { 186 div = 1024LL * 1024 * 1024 * 1024; 187 suffix = "T"; 188 } 189 if (value == 0) { 190 snprintf(buf, sizeof(buf), ""); 191 } else if (div == 1) { 192 snprintf(buf, sizeof(buf), "%s%7.0f%s", 193 neg, 194 (double)value / (double)div, 195 suffix); 196 } else { 197 snprintf(buf, sizeof(buf), "%s%6.2f%s", 198 neg, 199 (double)value / (double)div, 200 suffix); 201 } 202 return buf; 203 } 204 205 static void 206 mapscan(kvm_t *kd, vm_map_entry_t kptr, vm_map_entry_t ken, vm_offset_t *lastp) 207 { 208 int realmem; 209 210 if (*lastp != ken->ba.start) { 211 printf("%4ldM %p %08lx-%08lx (%s) EMPTY\n", 212 total_used / 1024 / 1024, 213 kptr, 214 *lastp, ken->ba.start, 215 formatnum(ken->ba.start - *lastp)); 216 total_empty += ken->ba.start - *lastp; 217 } 218 printf("%4ldM %p %08lx-%08lx (%6ldK) id=%-8s object=%p\n", 219 total_used / 1024 / 1024, 220 kptr, 221 ken->ba.start, ken->ba.end, 222 (ken->ba.end - ken->ba.start) / 1024, 223 entryid(ken->id, &realmem), 224 ken->ba.map_object); 225 total_used += ken->ba.end - ken->ba.start; 226 227 if (ken->id < VM_SUBSYS_LIMIT) 228 total_used_byid[ken->id] += ken->ba.end - ken->ba.start; 229 else 230 total_used_byid[0] += ken->ba.end - ken->ba.start; 231 232 if (realmem) 233 total_real += ken->ba.end - ken->ba.start; 234 235 *lastp = ken->ba.end; 236 } 237 238 static 239 const char * 240 entryid(vm_subsys_t id, int *realmemp) 241 { 242 static char buf[32]; 243 int dummy = 0; 244 int *realmem = (realmemp ? realmemp : &dummy); 245 246 *realmem = 0; 247 248 switch(id) { 249 case VM_SUBSYS_UNKNOWN: 250 return("UNKNOWN"); 251 case VM_SUBSYS_KMALLOC: 252 *realmem = 1; 253 return("KMALLOC"); 254 case VM_SUBSYS_STACK: 255 *realmem = 1; 256 return("STACK"); 257 case VM_SUBSYS_IMGACT: 258 return("IMGACT"); 259 case VM_SUBSYS_EFI: 260 return("EFI"); 261 case VM_SUBSYS_RESERVED: 262 *realmem = 1; 263 return("BOOT+KERN"); 264 case VM_SUBSYS_INIT: 265 return("INIT"); 266 case VM_SUBSYS_PIPE: 267 return("PIPE"); 268 case VM_SUBSYS_PROC: 269 return("PROC"); 270 case VM_SUBSYS_SHMEM: 271 return("SHMEM"); 272 case VM_SUBSYS_SYSMAP: 273 return("SYSMAP"); 274 case VM_SUBSYS_MMAP: 275 return("MMAP"); 276 case VM_SUBSYS_BRK: 277 return("BRK"); 278 case VM_SUBSYS_BOGUS: 279 return("BOGUS"); 280 case VM_SUBSYS_BUF: 281 *realmem = 1; 282 return("BUF"); 283 case VM_SUBSYS_BUFDATA: 284 return("BUFDATA"); 285 case VM_SUBSYS_GD: 286 *realmem = 1; 287 return("GD"); 288 case VM_SUBSYS_IPIQ: 289 *realmem = 1; 290 return("IPIQ"); 291 case VM_SUBSYS_PVENTRY: 292 return("PVENTRY"); 293 case VM_SUBSYS_PML4: 294 *realmem = 1; 295 return("PML4"); 296 case VM_SUBSYS_MAPDEV: 297 return("MAPDEV"); 298 case VM_SUBSYS_ZALLOC: 299 return("ZALLOC"); 300 301 case VM_SUBSYS_DM: 302 return("DM"); 303 case VM_SUBSYS_CONTIG: 304 return("CONTIG"); 305 case VM_SUBSYS_DRM: 306 return("DRM"); 307 case VM_SUBSYS_DRM_GEM: 308 return("DRM_GEM"); 309 case VM_SUBSYS_DRM_SCAT: 310 return("DRM_SCAT"); 311 case VM_SUBSYS_DRM_VMAP: 312 *realmem = 1; 313 return("DRM_VMAP"); 314 case VM_SUBSYS_DRM_TTM: 315 return("DRM_TTM"); 316 case VM_SUBSYS_HAMMER: 317 return("HAMMER"); 318 case VM_SUBSYS_VMPGHASH: 319 *realmem = 1; 320 return("VMPGHASH"); 321 default: 322 break; 323 } 324 snprintf(buf, sizeof(buf), "%d", (int)id); 325 326 return buf; 327 } 328 329 330 static void 331 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes) 332 { 333 if (kvm_read(kd, addr, buf, nbytes) != nbytes) { 334 perror("kvm_read"); 335 exit(1); 336 } 337 } 338