1 /* $OpenBSD: procmap.c,v 1.64 2018/03/31 17:26:13 otto Exp $ */ 2 /* $NetBSD: pmap.c,v 1.1 2002/09/01 20:32:44 atatat Exp $ */ 3 4 /* 5 * Copyright (c) 2002 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Andrew Brown. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #define _KERNEL 34 #include <sys/tree.h> 35 #undef _KERNEL 36 37 #include <sys/param.h> /* MAXCOMLEN */ 38 #include <sys/types.h> 39 #include <sys/time.h> 40 #include <sys/exec.h> 41 #include <sys/proc.h> 42 #include <sys/vnode.h> 43 #include <sys/mount.h> 44 #include <sys/uio.h> 45 #include <sys/namei.h> 46 #include <sys/sysctl.h> 47 48 /* XXX until uvm gets cleaned up */ 49 typedef int boolean_t; 50 51 #include <uvm/uvm.h> 52 #include <uvm/uvm_device.h> 53 #include <uvm/uvm_amap.h> 54 #include <uvm/uvm_vnode.h> 55 56 #include <ufs/ufs/quota.h> 57 #include <ufs/ufs/inode.h> 58 #undef doff_t 59 #undef IN_ACCESS 60 #undef i_size 61 #undef i_devvp 62 #include <isofs/cd9660/iso.h> 63 #include <isofs/cd9660/cd9660_node.h> 64 65 #include <kvm.h> 66 #include <fcntl.h> 67 #include <errno.h> 68 #include <err.h> 69 #include <stdlib.h> 70 #include <stddef.h> 71 #include <unistd.h> 72 #include <stdio.h> 73 #include <limits.h> 74 #include <string.h> 75 76 /* 77 * stolen (and munged) from #include <uvm/uvm_object.h> 78 */ 79 #define UVM_OBJ_IS_VNODE(uobj) ((uobj)->pgops == uvm_vnodeops) 80 #define UVM_OBJ_IS_AOBJ(uobj) ((uobj)->pgops == aobj_pager) 81 #define UVM_OBJ_IS_DEVICE(uobj) ((uobj)->pgops == uvm_deviceops) 82 83 #define PRINT_VMSPACE 0x00000001 84 #define PRINT_VM_MAP 0x00000002 85 #define PRINT_VM_MAP_HEADER 0x00000004 86 #define PRINT_VM_MAP_ENTRY 0x00000008 87 #define DUMP_NAMEI_CACHE 0x00000010 88 89 struct cache_entry { 90 LIST_ENTRY(cache_entry) ce_next; 91 struct vnode *ce_vp, *ce_pvp; 92 u_long ce_cid, ce_pcid; 93 unsigned int ce_nlen; 94 char ce_name[256]; 95 }; 96 97 LIST_HEAD(cache_head, cache_entry) lcache; 98 TAILQ_HEAD(namecache_head, namecache) nclruhead; 99 int namecache_loaded; 100 void *uvm_vnodeops, *uvm_deviceops, *aobj_pager; 101 u_long kernel_map_addr, nclruhead_addr; 102 int debug, verbose; 103 int print_all, print_map, print_maps, print_solaris, print_ddb, print_amap; 104 int rwx = PROT_READ | PROT_WRITE | PROT_EXEC; 105 rlim_t maxssiz; 106 107 struct sum { 108 unsigned long s_am_nslots; 109 unsigned long s_am_nusedslots; 110 }; 111 112 struct kbit { 113 /* 114 * size of data chunk 115 */ 116 size_t k_size; 117 118 /* 119 * something for printf() and something for kvm_read() 120 */ 121 union { 122 void *k_addr_p; 123 u_long k_addr_ul; 124 } k_addr; 125 126 /* 127 * where we actually put the "stuff" 128 */ 129 union { 130 char data[1]; 131 struct vmspace vmspace; 132 struct vm_map vm_map; 133 struct vm_map_entry vm_map_entry; 134 struct uvm_vnode uvm_vnode; 135 struct vnode vnode; 136 struct uvm_object uvm_object; 137 struct mount mount; 138 struct inode inode; 139 struct iso_node iso_node; 140 struct uvm_device uvm_device; 141 struct vm_amap vm_amap; 142 } k_data; 143 }; 144 145 /* the size of the object in the kernel */ 146 #define S(x) ((x)->k_size) 147 /* the address of the object in kernel, two forms */ 148 #define A(x) ((x)->k_addr.k_addr_ul) 149 #define P(x) ((x)->k_addr.k_addr_p) 150 /* the data from the kernel */ 151 #define D(x,d) (&((x)->k_data.d)) 152 153 /* suck the data from the kernel */ 154 #define _KDEREF(kd, addr, dst, sz) do { \ 155 ssize_t len; \ 156 len = kvm_read((kd), (addr), (dst), (sz)); \ 157 if (len != (sz)) \ 158 errx(1, "%s == %ld vs. %lu @ %lx", \ 159 kvm_geterr(kd), (long)len, (unsigned long)(sz), (addr)); \ 160 } while (0/*CONSTCOND*/) 161 162 /* suck the data using the structure */ 163 #define KDEREF(kd, item) _KDEREF((kd), A(item), D(item, data), S(item)) 164 165 struct nlist nl[] = { 166 { "_maxsmap" }, 167 #define NL_MAXSSIZ 0 168 { "_uvm_vnodeops" }, 169 #define NL_UVM_VNODEOPS 1 170 { "_uvm_deviceops" }, 171 #define NL_UVM_DEVICEOPS 2 172 { "_aobj_pager" }, 173 #define NL_AOBJ_PAGER 3 174 { "_kernel_map" }, 175 #define NL_KERNEL_MAP 4 176 { "_nclruhead" }, 177 #define NL_NCLRUHEAD 5 178 { NULL } 179 }; 180 181 void load_symbols(kvm_t *); 182 void process_map(kvm_t *, pid_t, struct kinfo_proc *, struct sum *); 183 struct vm_map_entry *load_vm_map_entries(kvm_t *, struct vm_map_entry *, 184 struct vm_map_entry *); 185 void unload_vm_map_entries(struct vm_map_entry *); 186 size_t dump_vm_map_entry(kvm_t *, struct kbit *, struct vm_map_entry *, 187 struct sum *); 188 char *findname(kvm_t *, struct kbit *, struct vm_map_entry *, struct kbit *, 189 struct kbit *, struct kbit *); 190 int search_cache(kvm_t *, struct kbit *, char **, char *, size_t); 191 void load_name_cache(kvm_t *); 192 void cache_enter(struct namecache *); 193 static void __dead usage(void); 194 static pid_t strtopid(const char *); 195 void print_sum(struct sum *, struct sum *); 196 197 /* 198 * uvm_map address tree implementation. 199 */ 200 static int no_impl(const void *, const void *); 201 static int 202 no_impl(const void *p, const void *q) 203 { 204 errx(1, "uvm_map address comparison not implemented"); 205 return 0; 206 } 207 208 RBT_PROTOTYPE(uvm_map_addr, vm_map_entry, daddrs.addr_entry, no_impl); 209 RBT_GENERATE(uvm_map_addr, vm_map_entry, daddrs.addr_entry, no_impl); 210 211 int 212 main(int argc, char *argv[]) 213 { 214 const char *errstr; 215 char errbuf[_POSIX2_LINE_MAX], *kmem = NULL, *kernel = NULL; 216 struct kinfo_proc *kproc; 217 struct sum total_sum; 218 int many, ch, rc; 219 kvm_t *kd; 220 pid_t pid = -1; 221 gid_t gid; 222 223 while ((ch = getopt(argc, argv, "AaD:dlmM:N:p:Prsvx")) != -1) { 224 switch (ch) { 225 case 'A': 226 print_amap = 1; 227 break; 228 case 'a': 229 print_all = 1; 230 break; 231 case 'd': 232 print_ddb = 1; 233 break; 234 case 'D': 235 debug = strtonum(optarg, 0, 0x1f, &errstr); 236 if (errstr) 237 errx(1, "invalid debug mask"); 238 break; 239 case 'l': 240 print_maps = 1; 241 break; 242 case 'm': 243 print_map = 1; 244 break; 245 case 'M': 246 kmem = optarg; 247 break; 248 case 'N': 249 kernel = optarg; 250 break; 251 case 'p': 252 pid = strtopid(optarg); 253 break; 254 case 'P': 255 pid = getpid(); 256 break; 257 case 's': 258 print_solaris = 1; 259 break; 260 case 'v': 261 verbose = 1; 262 break; 263 case 'r': 264 case 'x': 265 errx(1, "-%c option not implemented, sorry", ch); 266 /*NOTREACHED*/ 267 default: 268 usage(); 269 } 270 } 271 272 /* 273 * Discard setgid privileges if not the running kernel so that bad 274 * guys can't print interesting stuff from kernel memory. 275 */ 276 gid = getgid(); 277 if (kernel != NULL || kmem != NULL) 278 if (setresgid(gid, gid, gid) == -1) 279 err(1, "setresgid"); 280 281 argc -= optind; 282 argv += optind; 283 284 /* more than one "process" to dump? */ 285 many = (argc > 1 - (pid == -1 ? 0 : 1)) ? 1 : 0; 286 287 /* apply default */ 288 if (print_all + print_map + print_maps + print_solaris + 289 print_ddb == 0) 290 print_solaris = 1; 291 292 /* start by opening libkvm */ 293 kd = kvm_openfiles(kernel, kmem, NULL, O_RDONLY, errbuf); 294 295 if (kernel == NULL && kmem == NULL) 296 if (setresgid(gid, gid, gid) == -1) 297 err(1, "setresgid"); 298 299 if (kd == NULL) 300 errx(1, "%s", errbuf); 301 302 /* get "bootstrap" addresses from kernel */ 303 load_symbols(kd); 304 305 memset(&total_sum, 0, sizeof(total_sum)); 306 307 do { 308 struct sum sum; 309 310 memset(&sum, 0, sizeof(sum)); 311 312 if (pid == -1) { 313 if (argc == 0) 314 pid = getppid(); 315 else { 316 pid = strtopid(argv[0]); 317 argv++; 318 argc--; 319 } 320 } 321 322 /* find the process id */ 323 if (pid == 0) 324 kproc = NULL; 325 else { 326 kproc = kvm_getprocs(kd, KERN_PROC_PID, pid, 327 sizeof(struct kinfo_proc), &rc); 328 if (kproc == NULL || rc == 0) { 329 warnc(ESRCH, "%d", pid); 330 pid = -1; 331 continue; 332 } 333 } 334 335 /* dump it */ 336 if (many) { 337 if (kproc) 338 printf("process %d:\n", pid); 339 else 340 printf("kernel:\n"); 341 } 342 343 process_map(kd, pid, kproc, &sum); 344 if (print_amap) 345 print_sum(&sum, &total_sum); 346 pid = -1; 347 } while (argc > 0); 348 349 if (print_amap) 350 print_sum(&total_sum, NULL); 351 352 /* done. go away. */ 353 rc = kvm_close(kd); 354 if (rc == -1) 355 err(1, "kvm_close"); 356 357 return (0); 358 } 359 360 void 361 print_sum(struct sum *sum, struct sum *total_sum) 362 { 363 const char *t = total_sum == NULL ? "total " : ""; 364 printf("%samap mapped slots: %lu\n", t, sum->s_am_nslots); 365 printf("%samap used slots: %lu\n", t, sum->s_am_nusedslots); 366 367 if (total_sum) { 368 total_sum->s_am_nslots += sum->s_am_nslots; 369 total_sum->s_am_nusedslots += sum->s_am_nusedslots; 370 } 371 } 372 373 void 374 process_map(kvm_t *kd, pid_t pid, struct kinfo_proc *proc, struct sum *sum) 375 { 376 struct kbit kbit[3], *vmspace, *vm_map; 377 struct vm_map_entry *vm_map_entry; 378 size_t total = 0; 379 char *thing; 380 uid_t uid; 381 int vmmap_flags; 382 383 if ((uid = getuid())) { 384 if (pid == 0) { 385 warnx("kernel map is restricted"); 386 return; 387 } 388 if (uid != proc->p_uid) { 389 warnx("other users' process maps are restricted"); 390 return; 391 } 392 } 393 394 vmspace = &kbit[0]; 395 vm_map = &kbit[1]; 396 397 A(vmspace) = 0; 398 A(vm_map) = 0; 399 400 if (pid > 0) { 401 A(vmspace) = (u_long)proc->p_vmspace; 402 S(vmspace) = sizeof(struct vmspace); 403 KDEREF(kd, vmspace); 404 thing = "proc->p_vmspace.vm_map"; 405 } else { 406 A(vmspace) = 0; 407 S(vmspace) = 0; 408 thing = "kernel_map"; 409 } 410 411 if (pid > 0 && (debug & PRINT_VMSPACE)) { 412 printf("proc->p_vmspace %p = {", P(vmspace)); 413 printf(" vm_refcnt = %d,", D(vmspace, vmspace)->vm_refcnt); 414 printf(" vm_shm = %p,\n", D(vmspace, vmspace)->vm_shm); 415 printf(" vm_rssize = %d,", D(vmspace, vmspace)->vm_rssize); 416 #if 0 417 printf(" vm_swrss = %d,", D(vmspace, vmspace)->vm_swrss); 418 #endif 419 printf(" vm_tsize = %d,", D(vmspace, vmspace)->vm_tsize); 420 printf(" vm_dsize = %d,\n", D(vmspace, vmspace)->vm_dsize); 421 printf(" vm_ssize = %d,", D(vmspace, vmspace)->vm_ssize); 422 printf(" vm_taddr = %p,", D(vmspace, vmspace)->vm_taddr); 423 printf(" vm_daddr = %p,\n", D(vmspace, vmspace)->vm_daddr); 424 printf(" vm_maxsaddr = %p,", 425 D(vmspace, vmspace)->vm_maxsaddr); 426 printf(" vm_minsaddr = %p }\n", 427 D(vmspace, vmspace)->vm_minsaddr); 428 } 429 430 S(vm_map) = sizeof(struct vm_map); 431 if (pid > 0) { 432 A(vm_map) = A(vmspace); 433 memcpy(D(vm_map, vm_map), &D(vmspace, vmspace)->vm_map, 434 S(vm_map)); 435 } else { 436 A(vm_map) = kernel_map_addr; 437 KDEREF(kd, vm_map); 438 } 439 if (debug & PRINT_VM_MAP) { 440 printf("%s %p = {", thing, P(vm_map)); 441 442 printf(" pmap = %p,\n", D(vm_map, vm_map)->pmap); 443 printf(" lock = <struct lock>\n"); 444 printf(" size = %lx,", D(vm_map, vm_map)->size); 445 printf(" ref_count = %d,", D(vm_map, vm_map)->ref_count); 446 printf(" ref_lock = <struct simplelock>,\n"); 447 printf(" min_offset-max_offset = 0x%lx-0x%lx\n", 448 D(vm_map, vm_map)->min_offset, 449 D(vm_map, vm_map)->max_offset); 450 printf(" b_start-b_end = 0x%lx-0x%lx\n", 451 D(vm_map, vm_map)->b_start, 452 D(vm_map, vm_map)->b_end); 453 printf(" s_start-s_end = 0x%lx-0x%lx\n", 454 D(vm_map, vm_map)->s_start, 455 D(vm_map, vm_map)->s_end); 456 vmmap_flags = D(vm_map, vm_map)->flags; 457 printf(" flags = %x <%s%s%s%s%s%s >,\n", 458 vmmap_flags, 459 vmmap_flags & VM_MAP_PAGEABLE ? " PAGEABLE" : "", 460 vmmap_flags & VM_MAP_INTRSAFE ? " INTRSAFE" : "", 461 vmmap_flags & VM_MAP_WIREFUTURE ? " WIREFUTURE" : "", 462 vmmap_flags & VM_MAP_BUSY ? " BUSY" : "", 463 vmmap_flags & VM_MAP_WANTLOCK ? " WANTLOCK" : "", 464 #if VM_MAP_TOPDOWN > 0 465 vmmap_flags & VM_MAP_TOPDOWN ? " TOPDOWN" : 466 #endif 467 ""); 468 printf(" timestamp = %u }\n", D(vm_map, vm_map)->timestamp); 469 } 470 if (print_ddb) { 471 printf("MAP %p: [0x%lx->0x%lx]\n", P(vm_map), 472 D(vm_map, vm_map)->min_offset, 473 D(vm_map, vm_map)->max_offset); 474 printf("\tsz=%ld, ref=%d, version=%d, flags=0x%x\n", 475 D(vm_map, vm_map)->size, 476 D(vm_map, vm_map)->ref_count, 477 D(vm_map, vm_map)->timestamp, 478 D(vm_map, vm_map)->flags); 479 printf("\tpmap=%p(resident=<unknown>)\n", 480 D(vm_map, vm_map)->pmap); 481 } 482 483 /* headers */ 484 #ifdef DISABLED_HEADERS 485 if (print_map) 486 printf("%-*s %-*s rwx RWX CPY NCP I W A\n", 487 (int)sizeof(long) * 2 + 2, "Start", 488 (int)sizeof(long) * 2 + 2, "End"); 489 if (print_maps) 490 printf("%-*s %-*s rwxp %-*s Dev Inode File\n", 491 (int)sizeof(long) * 2 + 0, "Start", 492 (int)sizeof(long) * 2 + 0, "End", 493 (int)sizeof(long) * 2 + 0, "Offset"); 494 if (print_solaris) 495 printf("%-*s %*s Protection File\n", 496 (int)sizeof(long) * 2 + 0, "Start", 497 (int)sizeof(int) * 2 - 1, "Size "); 498 #endif 499 if (print_all) 500 printf("%-*s %-*s %*s %-*s rwxpc RWX I/W/A Dev %*s - File\n", 501 (int)sizeof(long) * 2, "Start", 502 (int)sizeof(long) * 2, "End", 503 (int)sizeof(int) * 2, "Size ", 504 (int)sizeof(long) * 2, "Offset", 505 (int)sizeof(int) * 2, "Inode"); 506 507 /* these are the "sub entries" */ 508 vm_map_entry = load_vm_map_entries(kd, 509 RBT_ROOT(uvm_map_addr, &D(vm_map, vm_map)->addr), NULL); 510 if (vm_map_entry != NULL) { 511 /* RBTs point at rb_entries inside nodes */ 512 D(vm_map, vm_map)->addr.rbh_root.rbt_root = 513 &vm_map_entry->daddrs.addr_entry; 514 } else 515 RBT_INIT(uvm_map_addr, &D(vm_map, vm_map)->addr); 516 517 RBT_FOREACH(vm_map_entry, uvm_map_addr, &D(vm_map, vm_map)->addr) 518 total += dump_vm_map_entry(kd, vmspace, vm_map_entry, sum); 519 unload_vm_map_entries(RBT_ROOT(uvm_map_addr, &D(vm_map, vm_map)->addr)); 520 521 if (print_solaris) 522 printf("%-*s %8luK\n", 523 (int)sizeof(void *) * 2 - 2, " total", 524 (unsigned long)total); 525 if (print_all) 526 printf("%-*s %9luk\n", 527 (int)sizeof(void *) * 4 - 1, " total", 528 (unsigned long)total); 529 } 530 531 void 532 load_symbols(kvm_t *kd) 533 { 534 int rc, i; 535 536 rc = kvm_nlist(kd, &nl[0]); 537 if (rc == -1) 538 errx(1, "%s == %d", kvm_geterr(kd), rc); 539 for (i = 0; i < sizeof(nl)/sizeof(nl[0]); i++) 540 if (nl[i].n_value == 0 && nl[i].n_name) 541 printf("%s not found\n", nl[i].n_name); 542 543 uvm_vnodeops = (void*)nl[NL_UVM_VNODEOPS].n_value; 544 uvm_deviceops = (void*)nl[NL_UVM_DEVICEOPS].n_value; 545 aobj_pager = (void*)nl[NL_AOBJ_PAGER].n_value; 546 547 nclruhead_addr = nl[NL_NCLRUHEAD].n_value; 548 549 _KDEREF(kd, nl[NL_MAXSSIZ].n_value, &maxssiz, 550 sizeof(maxssiz)); 551 _KDEREF(kd, nl[NL_KERNEL_MAP].n_value, &kernel_map_addr, 552 sizeof(kernel_map_addr)); 553 } 554 555 /* 556 * Recreate the addr tree of vm_map in local memory. 557 */ 558 struct vm_map_entry * 559 load_vm_map_entries(kvm_t *kd, struct vm_map_entry *kptr, 560 struct vm_map_entry *parent) 561 { 562 static struct kbit map_ent; 563 struct vm_map_entry *result, *ld; 564 565 if (kptr == NULL) 566 return NULL; 567 568 A(&map_ent) = (u_long)kptr; 569 S(&map_ent) = sizeof(struct vm_map_entry); 570 KDEREF(kd, &map_ent); 571 572 result = malloc(sizeof(*result)); 573 if (result == NULL) 574 err(1, "malloc"); 575 memcpy(result, D(&map_ent, vm_map_entry), sizeof(struct vm_map_entry)); 576 577 /* 578 * Recurse to download rest of the tree. 579 */ 580 581 /* RBTs point at rb_entries inside nodes */ 582 ld = load_vm_map_entries(kd, RBT_LEFT(uvm_map_addr, result), result); 583 result->daddrs.addr_entry.rbt_left = &ld->daddrs.addr_entry; 584 ld = load_vm_map_entries(kd, RBT_RIGHT(uvm_map_addr, result), result); 585 result->daddrs.addr_entry.rbt_right = &ld->daddrs.addr_entry; 586 result->daddrs.addr_entry.rbt_parent = &parent->daddrs.addr_entry; 587 588 return result; 589 } 590 591 /* 592 * Release the addr tree of vm_map. 593 */ 594 void 595 unload_vm_map_entries(struct vm_map_entry *ent) 596 { 597 if (ent == NULL) 598 return; 599 600 unload_vm_map_entries(RBT_LEFT(uvm_map_addr, ent)); 601 unload_vm_map_entries(RBT_RIGHT(uvm_map_addr, ent)); 602 free(ent); 603 } 604 605 size_t 606 dump_vm_map_entry(kvm_t *kd, struct kbit *vmspace, 607 struct vm_map_entry *vme, struct sum *sum) 608 { 609 struct kbit kbit[5], *uvm_obj, *vp, *vfs, *amap, *uvn; 610 ino_t inode = 0; 611 dev_t dev = 0; 612 size_t sz = 0; 613 char *name; 614 static u_long prevend; 615 616 uvm_obj = &kbit[0]; 617 vp = &kbit[1]; 618 vfs = &kbit[2]; 619 amap = &kbit[3]; 620 uvn = &kbit[4]; 621 622 A(uvm_obj) = 0; 623 A(vp) = 0; 624 A(vfs) = 0; 625 A(uvn) = 0; 626 627 if (debug & PRINT_VM_MAP_ENTRY) { 628 printf("%s = {", "vm_map_entry"); 629 printf(" start = %lx,", vme->start); 630 printf(" end = %lx,", vme->end); 631 printf(" fspace = %lx,\n", vme->fspace); 632 printf(" object.uvm_obj/sub_map = %p,\n", 633 vme->object.uvm_obj); 634 printf(" offset = %lx,", (unsigned long)vme->offset); 635 printf(" etype = %x <%s%s%s%s%s >,", vme->etype, 636 vme->etype & UVM_ET_OBJ ? " OBJ" : "", 637 vme->etype & UVM_ET_SUBMAP ? " SUBMAP" : "", 638 vme->etype & UVM_ET_COPYONWRITE ? " COW" : "", 639 vme->etype & UVM_ET_NEEDSCOPY ? " NEEDSCOPY" : "", 640 vme->etype & UVM_ET_HOLE ? " HOLE" : ""); 641 printf(" protection = %x,\n", vme->protection); 642 printf(" max_protection = %x,", vme->max_protection); 643 printf(" inheritance = %d,", vme->inheritance); 644 printf(" wired_count = %d,\n", vme->wired_count); 645 printf(" aref = <struct vm_aref>,"); 646 printf(" advice = %d,", vme->advice); 647 printf(" flags = %x <%s%s > }\n", vme->flags, 648 vme->flags & UVM_MAP_STATIC ? " STATIC" : "", 649 vme->flags & UVM_MAP_KMEM ? " KMEM" : ""); 650 } 651 652 A(vp) = 0; 653 A(uvm_obj) = 0; 654 655 if (vme->object.uvm_obj != NULL) { 656 P(uvm_obj) = vme->object.uvm_obj; 657 S(uvm_obj) = sizeof(struct uvm_object); 658 KDEREF(kd, uvm_obj); 659 if (UVM_ET_ISOBJ(vme) && 660 UVM_OBJ_IS_VNODE(D(uvm_obj, uvm_object))) { 661 P(uvn) = P(uvm_obj); 662 S(uvn) = sizeof(struct uvm_vnode); 663 KDEREF(kd, uvn); 664 665 P(vp) = D(uvn, uvm_vnode)->u_vnode; 666 S(vp) = sizeof(struct vnode); 667 KDEREF(kd, vp); 668 } 669 } 670 671 if (vme->aref.ar_amap != NULL) { 672 P(amap) = vme->aref.ar_amap; 673 S(amap) = sizeof(struct vm_amap); 674 KDEREF(kd, amap); 675 } 676 677 A(vfs) = 0; 678 679 if (P(vp) != NULL && D(vp, vnode)->v_mount != NULL) { 680 P(vfs) = D(vp, vnode)->v_mount; 681 S(vfs) = sizeof(struct mount); 682 KDEREF(kd, vfs); 683 D(vp, vnode)->v_mount = D(vfs, mount); 684 } 685 686 /* 687 * dig out the device number and inode number from certain 688 * file system types. 689 */ 690 #define V_DATA_IS(vp, type, d, i) do { \ 691 struct kbit data; \ 692 P(&data) = D(vp, vnode)->v_data; \ 693 S(&data) = sizeof(*D(&data, type)); \ 694 KDEREF(kd, &data); \ 695 dev = D(&data, type)->d; \ 696 inode = D(&data, type)->i; \ 697 } while (0/*CONSTCOND*/) 698 699 if (A(vp) && 700 D(vp, vnode)->v_type == VREG && 701 D(vp, vnode)->v_data != NULL) { 702 switch (D(vp, vnode)->v_tag) { 703 case VT_UFS: 704 case VT_EXT2FS: 705 V_DATA_IS(vp, inode, i_dev, i_number); 706 break; 707 case VT_ISOFS: 708 V_DATA_IS(vp, iso_node, i_dev, i_number); 709 break; 710 case VT_NON: 711 case VT_NFS: 712 case VT_MFS: 713 case VT_MSDOSFS: 714 default: 715 break; 716 } 717 } 718 719 name = findname(kd, vmspace, vme, vp, vfs, uvm_obj); 720 721 if (print_map) { 722 printf("0x%lx 0x%lx %c%c%c %c%c%c %s %s %d %d %d", 723 vme->start, vme->end, 724 (vme->protection & PROT_READ) ? 'r' : '-', 725 (vme->protection & PROT_WRITE) ? 'w' : '-', 726 (vme->protection & PROT_EXEC) ? 'x' : '-', 727 (vme->max_protection & PROT_READ) ? 'r' : '-', 728 (vme->max_protection & PROT_WRITE) ? 'w' : '-', 729 (vme->max_protection & PROT_EXEC) ? 'x' : '-', 730 (vme->etype & UVM_ET_COPYONWRITE) ? "COW" : "NCOW", 731 (vme->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC", 732 vme->inheritance, vme->wired_count, 733 vme->advice); 734 if (verbose) { 735 if (inode) 736 printf(" %d,%d %llu", 737 major(dev), minor(dev), 738 (unsigned long long)inode); 739 if (name[0]) 740 printf(" %s", name); 741 } 742 printf("\n"); 743 } 744 745 if (print_maps) 746 printf("%0*lx-%0*lx %c%c%c%c %0*lx %02x:%02x %llu %s\n", 747 (int)sizeof(void *) * 2, vme->start, 748 (int)sizeof(void *) * 2, vme->end, 749 (vme->protection & PROT_READ) ? 'r' : '-', 750 (vme->protection & PROT_WRITE) ? 'w' : '-', 751 (vme->protection & PROT_EXEC) ? 'x' : '-', 752 (vme->etype & UVM_ET_COPYONWRITE) ? 'p' : 's', 753 (int)sizeof(void *) * 2, 754 (unsigned long)vme->offset, 755 major(dev), minor(dev), (unsigned long long)inode, 756 inode ? name : ""); 757 758 if (print_ddb) { 759 printf(" - <lost address>: 0x%lx->0x%lx: " 760 "obj=%p/0x%lx, amap=%p/%d\n", 761 vme->start, vme->end, 762 vme->object.uvm_obj, (unsigned long)vme->offset, 763 vme->aref.ar_amap, vme->aref.ar_pageoff); 764 printf("\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, " 765 "wc=%d, adv=%d\n", 766 (vme->etype & UVM_ET_SUBMAP) ? 'T' : 'F', 767 (vme->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F', 768 (vme->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F', 769 vme->protection, vme->max_protection, 770 vme->inheritance, vme->wired_count, vme->advice); 771 if (inode && verbose) 772 printf("\t(dev=%d,%d ino=%llu [%s] [%p])\n", 773 major(dev), minor(dev), (unsigned long long)inode, 774 inode ? name : "", P(vp)); 775 else if (name[0] == ' ' && verbose) 776 printf("\t(%s)\n", &name[2]); 777 } 778 779 if (print_solaris) { 780 char prot[30]; 781 782 prot[0] = '\0'; 783 prot[1] = '\0'; 784 if (vme->protection & PROT_READ) 785 strlcat(prot, "/read", sizeof(prot)); 786 if (vme->protection & PROT_WRITE) 787 strlcat(prot, "/write", sizeof(prot)); 788 if (vme->protection & PROT_EXEC) 789 strlcat(prot, "/exec", sizeof(prot)); 790 791 sz = (size_t)((vme->end - vme->start) / 1024); 792 printf("%0*lX %6luK %-15s %s\n", 793 (int)sizeof(void *) * 2, (unsigned long)vme->start, 794 (unsigned long)sz, &prot[1], name); 795 } 796 797 if (print_all) { 798 if (verbose) { 799 if (prevend < vme->start) 800 printf("%0*lx-%0*lx %7luk *\n", 801 (int)sizeof(void *) * 2, prevend, 802 (int)sizeof(void *) * 2, vme->start - 1, 803 (vme->start - prevend) / 1024); 804 prevend = vme->end; 805 } 806 807 sz = (size_t)((vme->end - vme->start) / 1024); 808 printf("%0*lx-%0*lx %7luk %0*lx %c%c%c%c%c (%c%c%c) %d/%d/%d %02d:%02d %7llu - %s", 809 (int)sizeof(void *) * 2, vme->start, (int)sizeof(void *) * 2, 810 vme->end - (vme->start != vme->end ? 1 : 0), (unsigned long)sz, 811 (int)sizeof(void *) * 2, (unsigned long)vme->offset, 812 (vme->protection & PROT_READ) ? 'r' : '-', 813 (vme->protection & PROT_WRITE) ? 'w' : '-', 814 (vme->protection & PROT_EXEC) ? 'x' : '-', 815 (vme->etype & UVM_ET_COPYONWRITE) ? 'p' : 's', 816 (vme->etype & UVM_ET_NEEDSCOPY) ? '+' : '-', 817 (vme->max_protection & PROT_READ) ? 'r' : '-', 818 (vme->max_protection & PROT_WRITE) ? 'w' : '-', 819 (vme->max_protection & PROT_EXEC) ? 'x' : '-', 820 vme->inheritance, vme->wired_count, vme->advice, 821 major(dev), minor(dev), (unsigned long long)inode, name); 822 if (A(vp)) 823 printf(" [%p]", P(vp)); 824 printf("\n"); 825 } 826 827 if (print_amap && vme->aref.ar_amap) { 828 printf(" amap - ref: %d fl: 0x%x nsl: %d nuse: %d\n", 829 D(amap, vm_amap)->am_ref, 830 D(amap, vm_amap)->am_flags, 831 D(amap, vm_amap)->am_nslot, 832 D(amap, vm_amap)->am_nused); 833 if (sum) { 834 sum->s_am_nslots += D(amap, vm_amap)->am_nslot; 835 sum->s_am_nusedslots += D(amap, vm_amap)->am_nused; 836 } 837 } 838 839 /* no access allowed, don't count space */ 840 if ((vme->protection & rwx) == 0) 841 sz = 0; 842 843 return (sz); 844 } 845 846 char * 847 findname(kvm_t *kd, struct kbit *vmspace, 848 struct vm_map_entry *vme, struct kbit *vp, 849 struct kbit *vfs, struct kbit *uvm_obj) 850 { 851 static char buf[1024], *name; 852 size_t l; 853 854 if (UVM_ET_ISOBJ(vme)) { 855 if (A(vfs)) { 856 l = strlen(D(vfs, mount)->mnt_stat.f_mntonname); 857 switch (search_cache(kd, vp, &name, buf, sizeof(buf))) { 858 case 0: /* found something */ 859 if (name - (1 + 11 + l) < buf) 860 break; 861 name--; 862 *name = '/'; 863 /*FALLTHROUGH*/ 864 case 2: /* found nothing */ 865 name -= 11; 866 memcpy(name, " -unknown- ", (size_t)11); 867 name -= l; 868 memcpy(name, 869 D(vfs, mount)->mnt_stat.f_mntonname, l); 870 break; 871 case 1: /* all is well */ 872 if (name - (1 + l) < buf) 873 break; 874 name--; 875 *name = '/'; 876 if (l != 1) { 877 name -= l; 878 memcpy(name, 879 D(vfs, mount)->mnt_stat.f_mntonname, l); 880 } 881 break; 882 } 883 } else if (UVM_OBJ_IS_DEVICE(D(uvm_obj, uvm_object))) { 884 struct kbit kdev; 885 dev_t dev; 886 887 P(&kdev) = P(uvm_obj); 888 S(&kdev) = sizeof(struct uvm_device); 889 KDEREF(kd, &kdev); 890 dev = D(&kdev, uvm_device)->u_device; 891 name = devname(dev, S_IFCHR); 892 if (name != NULL) 893 snprintf(buf, sizeof(buf), "/dev/%s", name); 894 else 895 snprintf(buf, sizeof(buf), " [ device %d,%d ]", 896 major(dev), minor(dev)); 897 name = buf; 898 } else if (UVM_OBJ_IS_AOBJ(D(uvm_obj, uvm_object))) 899 name = " [ uvm_aobj ]"; 900 else if (UVM_OBJ_IS_VNODE(D(uvm_obj, uvm_object))) 901 name = " [ ?VNODE? ]"; 902 else { 903 snprintf(buf, sizeof(buf), " [ unknown (%p) ]", 904 D(uvm_obj, uvm_object)->pgops); 905 name = buf; 906 } 907 } else if (D(vmspace, vmspace)->vm_maxsaddr <= (caddr_t)vme->start && 908 (D(vmspace, vmspace)->vm_maxsaddr + (size_t)maxssiz) >= 909 (caddr_t)vme->end) { 910 name = " [ stack ]"; 911 } else if (UVM_ET_ISHOLE(vme)) 912 name = " [ hole ]"; 913 else 914 name = " [ anon ]"; 915 916 return (name); 917 } 918 919 int 920 search_cache(kvm_t *kd, struct kbit *vp, char **name, char *buf, size_t blen) 921 { 922 struct cache_entry *ce; 923 struct kbit svp; 924 char *o, *e; 925 u_long cid; 926 927 if (!namecache_loaded) 928 load_name_cache(kd); 929 930 P(&svp) = P(vp); 931 S(&svp) = sizeof(struct vnode); 932 cid = D(vp, vnode)->v_id; 933 934 e = &buf[blen - 1]; 935 o = e; 936 do { 937 LIST_FOREACH(ce, &lcache, ce_next) 938 if (ce->ce_vp == P(&svp) && ce->ce_cid == cid) 939 break; 940 if (ce && ce->ce_vp == P(&svp) && ce->ce_cid == cid) { 941 if (o != e) { 942 if (o <= buf) 943 break; 944 *(--o) = '/'; 945 } 946 if (o - ce->ce_nlen <= buf) 947 break; 948 o -= ce->ce_nlen; 949 memcpy(o, ce->ce_name, ce->ce_nlen); 950 P(&svp) = ce->ce_pvp; 951 cid = ce->ce_pcid; 952 } else 953 break; 954 } while (1/*CONSTCOND*/); 955 *e = '\0'; 956 *name = o; 957 958 if (e == o) 959 return (2); 960 961 KDEREF(kd, &svp); 962 return (D(&svp, vnode)->v_flag & VROOT); 963 } 964 965 void 966 load_name_cache(kvm_t *kd) 967 { 968 struct namecache n, *tmp; 969 struct namecache_head nchead; 970 971 LIST_INIT(&lcache); 972 _KDEREF(kd, nclruhead_addr, &nchead, sizeof(nchead)); 973 tmp = TAILQ_FIRST(&nchead); 974 while (tmp != NULL) { 975 _KDEREF(kd, (u_long)tmp, &n, sizeof(n)); 976 977 if (n.nc_nlen > 0) { 978 if (n.nc_nlen > 2 || 979 n.nc_name[0] != '.' || 980 (n.nc_nlen != 1 && n.nc_name[1] != '.')) 981 cache_enter(&n); 982 } 983 tmp = TAILQ_NEXT(&n, nc_lru); 984 } 985 986 namecache_loaded = 1; 987 } 988 989 void 990 cache_enter(struct namecache *ncp) 991 { 992 struct cache_entry *ce; 993 994 if (debug & DUMP_NAMEI_CACHE) 995 printf("ncp->nc_vp %10p, ncp->nc_dvp %10p, ncp->nc_nlen " 996 "%3d [%.*s] (nc_dvpid=%lu, nc_vpid=%lu)\n", 997 ncp->nc_vp, ncp->nc_dvp, 998 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name, 999 ncp->nc_dvpid, ncp->nc_vpid); 1000 1001 ce = malloc(sizeof(struct cache_entry)); 1002 if (ce == NULL) 1003 err(1, "cache_enter"); 1004 1005 ce->ce_vp = ncp->nc_vp; 1006 ce->ce_pvp = ncp->nc_dvp; 1007 ce->ce_cid = ncp->nc_vpid; 1008 ce->ce_pcid = ncp->nc_dvpid; 1009 ce->ce_nlen = (unsigned)ncp->nc_nlen; 1010 strlcpy(ce->ce_name, ncp->nc_name, sizeof(ce->ce_name)); 1011 1012 LIST_INSERT_HEAD(&lcache, ce, ce_next); 1013 } 1014 1015 static void __dead 1016 usage(void) 1017 { 1018 extern char *__progname; 1019 fprintf(stderr, "usage: %s [-AadlmPsv] [-D number] " 1020 "[-M core] [-N system] [-p pid] [pid ...]\n", 1021 __progname); 1022 exit(1); 1023 } 1024 1025 static pid_t 1026 strtopid(const char *str) 1027 { 1028 pid_t pid; 1029 1030 errno = 0; 1031 pid = (pid_t)strtonum(str, 0, INT_MAX, NULL); 1032 if (errno != 0) 1033 usage(); 1034 return (pid); 1035 } 1036