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