xref: /openbsd/usr.sbin/procmap/procmap.c (revision 82ba26c4)
1 /*	$OpenBSD: procmap.c,v 1.74 2024/10/20 11:21:24 claudio 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/types.h>
38 #include <sys/time.h>
39 #include <sys/exec.h>
40 #include <sys/signal.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
no_impl(const void * p,const void * q)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
main(int argc,char * argv[])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_all = 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
print_sum(struct sum * sum,struct sum * total_sum)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
process_map(kvm_t * kd,pid_t pid,struct kinfo_proc * proc,struct sum * sum)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 #ifdef VM_MAP_BUSY
463 		    vmmap_flags & VM_MAP_BUSY ? " BUSY" :
464 #endif
465 		    "",
466 #ifdef VM_MAP_WANTLOCK
467 		    vmmap_flags & VM_MAP_WANTLOCK ? " WANTLOCK" :
468 #endif
469 		    "",
470 #if VM_MAP_TOPDOWN > 0
471 		    vmmap_flags & VM_MAP_TOPDOWN ? " TOPDOWN" :
472 #endif
473 		    "");
474 		printf("    timestamp = %u }\n", D(vm_map, vm_map)->timestamp);
475 	}
476 	if (print_ddb) {
477 		printf("MAP %p: [0x%lx->0x%lx]\n", P(vm_map),
478 		    D(vm_map, vm_map)->min_offset,
479 		    D(vm_map, vm_map)->max_offset);
480 		printf("\tsz=%ld, ref=%d, version=%d, flags=0x%x\n",
481 		    D(vm_map, vm_map)->size,
482 		    D(vm_map, vm_map)->ref_count,
483 		    D(vm_map, vm_map)->timestamp,
484 		    D(vm_map, vm_map)->flags);
485 		printf("\tpmap=%p(resident=<unknown>)\n",
486 		    D(vm_map, vm_map)->pmap);
487 	}
488 
489 	/* headers */
490 #ifdef DISABLED_HEADERS
491 	if (print_map)
492 		printf("%-*s   %-*s rwxS RWX CPY NCP I W A\n",
493 		    (int)sizeof(long) * 2 + 2, "Start",
494 		    (int)sizeof(long) * 2 + 2, "End");
495 	if (print_maps)
496 		printf("%-*s   %-*s   rwxSp %-*s Dev   Inode      File\n",
497 		    (int)sizeof(long) * 2 + 0, "Start",
498 		    (int)sizeof(long) * 2 + 0, "End",
499 		    (int)sizeof(long) * 2 + 0, "Offset");
500 	if (print_solaris)
501 		printf("%-*s %*s Protection        File\n",
502 		    (int)sizeof(long) * 2 + 0, "Start",
503 		    (int)sizeof(int) * 2 - 1,  "Size ");
504 #endif
505 	if (print_all)
506 		printf("%-*s %-*s %*s %-*s rwxSIpc  RWX  I/W/A Dev  %*s - File\n",
507 		    (int)sizeof(long) * 2, "Start",
508 		    (int)sizeof(long) * 2, "End",
509 		    (int)sizeof(int)  * 2, "Size ",
510 		    (int)sizeof(long) * 2, "Offset",
511 		    (int)sizeof(int)  * 2, "Inode");
512 
513 	/* these are the "sub entries" */
514 	vm_map_entry = load_vm_map_entries(kd,
515 	    RBT_ROOT(uvm_map_addr, &D(vm_map, vm_map)->addr), NULL);
516 	if (vm_map_entry != NULL) {
517 		/* RBTs point at rb_entries inside nodes */
518 		D(vm_map, vm_map)->addr.rbh_root.rbt_root =
519 		    &vm_map_entry->daddrs.addr_entry;
520 	} else
521 		RBT_INIT(uvm_map_addr, &D(vm_map, vm_map)->addr);
522 
523 	RBT_FOREACH(vm_map_entry, uvm_map_addr, &D(vm_map, vm_map)->addr)
524 		total += dump_vm_map_entry(kd, vmspace, vm_map_entry, sum);
525 	unload_vm_map_entries(RBT_ROOT(uvm_map_addr, &D(vm_map, vm_map)->addr));
526 
527 	if (print_solaris)
528 		printf("%-*s %8luK\n",
529 		    (int)sizeof(void *) * 2 - 2, " total",
530 		    (unsigned long)total);
531 	if (print_all)
532 		printf("%-*s %9luk\n",
533 		    (int)sizeof(void *) * 4 - 1, " total",
534 		    (unsigned long)total);
535 }
536 
537 void
load_symbols(kvm_t * kd)538 load_symbols(kvm_t *kd)
539 {
540 	int rc, i;
541 
542 	rc = kvm_nlist(kd, &nl[0]);
543 	if (rc == -1)
544 		errx(1, "%s == %d", kvm_geterr(kd), rc);
545 	for (i = 0; i < sizeof(nl)/sizeof(nl[0]); i++)
546 		if (nl[i].n_value == 0 && nl[i].n_name)
547 			printf("%s not found\n", nl[i].n_name);
548 
549 	uvm_vnodeops =	(void*)nl[NL_UVM_VNODEOPS].n_value;
550 	uvm_deviceops =	(void*)nl[NL_UVM_DEVICEOPS].n_value;
551 	aobj_pager =	(void*)nl[NL_AOBJ_PAGER].n_value;
552 
553 	nclruhead_addr = nl[NL_NCLRUHEAD].n_value;
554 
555 	_KDEREF(kd, nl[NL_MAXSSIZ].n_value, &maxssiz,
556 	    sizeof(maxssiz));
557 	_KDEREF(kd, nl[NL_KERNEL_MAP].n_value, &kernel_map_addr,
558 	    sizeof(kernel_map_addr));
559 }
560 
561 /*
562  * Recreate the addr tree of vm_map in local memory.
563  */
564 struct vm_map_entry *
load_vm_map_entries(kvm_t * kd,struct vm_map_entry * kptr,struct vm_map_entry * parent)565 load_vm_map_entries(kvm_t *kd, struct vm_map_entry *kptr,
566     struct vm_map_entry *parent)
567 {
568 	static struct kbit map_ent;
569 	struct vm_map_entry *result, *ld;
570 
571 	if (kptr == NULL)
572 		return NULL;
573 
574 	A(&map_ent) = (u_long)kptr;
575 	S(&map_ent) = sizeof(struct vm_map_entry);
576 	KDEREF(kd, &map_ent);
577 
578 	result = malloc(sizeof(*result));
579 	if (result == NULL)
580 		err(1, "malloc");
581 	memcpy(result, D(&map_ent, vm_map_entry), sizeof(struct vm_map_entry));
582 
583 	/*
584 	 * Recurse to download rest of the tree.
585 	 */
586 
587 	/* RBTs point at rb_entries inside nodes */
588 	ld = load_vm_map_entries(kd, RBT_LEFT(uvm_map_addr, result), result);
589 	result->daddrs.addr_entry.rbt_left = &ld->daddrs.addr_entry;
590 	ld = load_vm_map_entries(kd, RBT_RIGHT(uvm_map_addr, result), result);
591 	result->daddrs.addr_entry.rbt_right = &ld->daddrs.addr_entry;
592 	result->daddrs.addr_entry.rbt_parent = &parent->daddrs.addr_entry;
593 
594 	return result;
595 }
596 
597 /*
598  * Release the addr tree of vm_map.
599  */
600 void
unload_vm_map_entries(struct vm_map_entry * ent)601 unload_vm_map_entries(struct vm_map_entry *ent)
602 {
603 	if (ent == NULL)
604 		return;
605 
606 	unload_vm_map_entries(RBT_LEFT(uvm_map_addr, ent));
607 	unload_vm_map_entries(RBT_RIGHT(uvm_map_addr, ent));
608 	free(ent);
609 }
610 
611 size_t
dump_vm_map_entry(kvm_t * kd,struct kbit * vmspace,struct vm_map_entry * vme,struct sum * sum)612 dump_vm_map_entry(kvm_t *kd, struct kbit *vmspace,
613     struct vm_map_entry *vme, struct sum *sum)
614 {
615 	struct kbit kbit[5], *uvm_obj, *vp, *vfs, *amap, *uvn;
616 	ino_t inode = 0;
617 	dev_t dev = 0;
618 	size_t sz = 0;
619 	char *name;
620 	static u_long prevend;
621 
622 	uvm_obj = &kbit[0];
623 	vp = &kbit[1];
624 	vfs = &kbit[2];
625 	amap = &kbit[3];
626 	uvn = &kbit[4];
627 
628 	A(uvm_obj) = 0;
629 	A(vp) = 0;
630 	A(vfs) = 0;
631 	A(uvn) = 0;
632 
633 	if (debug & PRINT_VM_MAP_ENTRY) {
634 		printf("%s = {", "vm_map_entry");
635 		printf(" start = %lx,", vme->start);
636 		printf(" end = %lx,", vme->end);
637 		printf(" fspace = %lx,\n", vme->fspace);
638 		printf("    object.uvm_obj/sub_map = %p,\n",
639 		    vme->object.uvm_obj);
640 		printf("    offset = %lx,", (unsigned long)vme->offset);
641 		printf(" etype = %x <%s%s%s%s%s >,", vme->etype,
642 		    vme->etype & UVM_ET_OBJ ? " OBJ" : "",
643 		    vme->etype & UVM_ET_SUBMAP ? " SUBMAP" : "",
644 		    vme->etype & UVM_ET_COPYONWRITE ? " COW" : "",
645 		    vme->etype & UVM_ET_NEEDSCOPY ? " NEEDSCOPY" : "",
646 		    vme->etype & UVM_ET_HOLE ? " HOLE" : "");
647 		printf(" protection = %x,\n", vme->protection);
648 		printf("    max_protection = %x,", vme->max_protection);
649 		printf(" inheritance = %d,", vme->inheritance);
650 		printf(" wired_count = %d,\n", vme->wired_count);
651 		printf("    aref = <struct vm_aref>,");
652 		printf(" advice = %d,", vme->advice);
653 		printf(" flags = %x <%s%s > }\n", vme->flags,
654 		    vme->flags & UVM_MAP_STATIC ? " STATIC" : "",
655 		    vme->flags & UVM_MAP_KMEM ? " KMEM" : "");
656 	}
657 
658 	A(vp) = 0;
659 	A(uvm_obj) = 0;
660 
661 	if (vme->object.uvm_obj != NULL) {
662 		P(uvm_obj) = vme->object.uvm_obj;
663 		S(uvm_obj) = sizeof(struct uvm_object);
664 		KDEREF(kd, uvm_obj);
665 		if (UVM_ET_ISOBJ(vme) &&
666 		    UVM_OBJ_IS_VNODE(D(uvm_obj, uvm_object))) {
667 			P(uvn) = P(uvm_obj);
668 			S(uvn) = sizeof(struct uvm_vnode);
669 			KDEREF(kd, uvn);
670 
671 			P(vp) = D(uvn, uvm_vnode)->u_vnode;
672 			S(vp) = sizeof(struct vnode);
673 			KDEREF(kd, vp);
674 		}
675 	}
676 
677 	if (vme->aref.ar_amap != NULL) {
678 		P(amap) = vme->aref.ar_amap;
679 		S(amap) = sizeof(struct vm_amap);
680 		KDEREF(kd, amap);
681 	}
682 
683 	A(vfs) = 0;
684 
685 	if (P(vp) != NULL && D(vp, vnode)->v_mount != NULL) {
686 		P(vfs) = D(vp, vnode)->v_mount;
687 		S(vfs) = sizeof(struct mount);
688 		KDEREF(kd, vfs);
689 		D(vp, vnode)->v_mount = D(vfs, mount);
690 	}
691 
692 	/*
693 	 * dig out the device number and inode number from certain
694 	 * file system types.
695 	 */
696 #define V_DATA_IS(vp, type, d, i) do { \
697 	struct kbit data; \
698 	P(&data) = D(vp, vnode)->v_data; \
699 	S(&data) = sizeof(*D(&data, type)); \
700 	KDEREF(kd, &data); \
701 	dev = D(&data, type)->d; \
702 	inode = D(&data, type)->i; \
703 } while (0/*CONSTCOND*/)
704 
705 	if (A(vp) &&
706 	    D(vp, vnode)->v_type == VREG &&
707 	    D(vp, vnode)->v_data != NULL) {
708 		switch (D(vp, vnode)->v_tag) {
709 		case VT_UFS:
710 		case VT_EXT2FS:
711 			V_DATA_IS(vp, inode, i_dev, i_number);
712 			break;
713 		case VT_ISOFS:
714 			V_DATA_IS(vp, iso_node, i_dev, i_number);
715 			break;
716 		case VT_NON:
717 		case VT_NFS:
718 		case VT_MFS:
719 		case VT_MSDOSFS:
720 		default:
721 			break;
722 		}
723 	}
724 
725 	name = findname(kd, vmspace, vme, vp, vfs, uvm_obj);
726 
727 	if (print_map) {
728 		printf("0x%-*lx 0x%-*lx %c%c%c%c%c %c%c%c %s %s %d %d %d",
729 		    (int)sizeof(long) * 2 + 0, vme->start,
730 		    (int)sizeof(long) * 2 + 0, vme->end,
731 		    (vme->protection & PROT_READ) ? 'r' : '-',
732 		    (vme->protection & PROT_WRITE) ? 'w' : '-',
733 		    (vme->protection & PROT_EXEC) ? 'x' : '-',
734 		    (vme->etype & UVM_ET_STACK) ? 'S' : '-',
735 		    (vme->etype & UVM_ET_IMMUTABLE) ? 'I' : '-',
736 		    (vme->max_protection & PROT_READ) ? 'r' : '-',
737 		    (vme->max_protection & PROT_WRITE) ? 'w' : '-',
738 		    (vme->max_protection & PROT_EXEC) ? 'x' : '-',
739 		    (vme->etype & UVM_ET_COPYONWRITE) ? "COW" : "NCOW",
740 		    (vme->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC",
741 		    vme->inheritance, vme->wired_count,
742 		    vme->advice);
743 		if (verbose) {
744 			if (inode)
745 				printf(" %u,%u %llu",
746 				    major(dev), minor(dev),
747 				    (unsigned long long)inode);
748 			if (name[0])
749 				printf(" %s", name);
750 		}
751 		printf("\n");
752 	}
753 
754 	if (print_maps)
755 		printf("0x%-*lx 0x%-*lx %c%c%c%c%c%c %0*lx %02x:%02x %llu     %s\n",
756 		    (int)sizeof(void *) * 2, vme->start,
757 		    (int)sizeof(void *) * 2, vme->end,
758 		    (vme->protection & PROT_READ) ? 'r' : '-',
759 		    (vme->protection & PROT_WRITE) ? 'w' : '-',
760 		    (vme->protection & PROT_EXEC) ? 'x' : '-',
761 		    (vme->etype & UVM_ET_STACK) ? 'S' : '-',
762 		    (vme->etype & UVM_ET_IMMUTABLE) ? 'I' : '-',
763 		    (vme->etype & UVM_ET_COPYONWRITE) ? 'p' : 's',
764 		    (int)sizeof(void *) * 2,
765 		    (unsigned long)vme->offset,
766 		    major(dev), minor(dev), (unsigned long long)inode,
767 		    inode ? name : "");
768 
769 	if (print_ddb) {
770 		printf(" - <lost address>: 0x%lx->0x%lx: "
771 		    "obj=%p/0x%lx, amap=%p/%d\n",
772 		    vme->start, vme->end,
773 		    vme->object.uvm_obj, (unsigned long)vme->offset,
774 		    vme->aref.ar_amap, vme->aref.ar_pageoff);
775 		printf("\tsubmap=%c, cow=%c, nc=%c, stack=%c, "
776 		    "immutable=%c, prot(max)=%d/%d, inh=%d, "
777 		    "wc=%d, adv=%d\n",
778 		    (vme->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
779 		    (vme->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
780 		    (vme->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
781 		    (vme->etype & UVM_ET_STACK) ? 'T' : 'F',
782 		    (vme->etype & UVM_ET_IMMUTABLE) ? 'T' : 'F',
783 		    vme->protection, vme->max_protection,
784 		    vme->inheritance, vme->wired_count, vme->advice);
785 		if (inode && verbose)
786 			printf("\t(dev=%u,%u ino=%llu [%s] [%p])\n",
787 			    major(dev), minor(dev), (unsigned long long)inode,
788 			    inode ? name : "", P(vp));
789 		else if (name[0] == ' ' && verbose)
790 			printf("\t(%s)\n", &name[2]);
791 	}
792 
793 	if (print_solaris) {
794 		char prot[30];
795 
796 		prot[0] = '\0';
797 		prot[1] = '\0';
798 		if (vme->protection & PROT_READ)
799 			strlcat(prot, "/read", sizeof(prot));
800 		if (vme->protection & PROT_WRITE)
801 			strlcat(prot, "/write", sizeof(prot));
802 		if (vme->protection & PROT_EXEC)
803 			strlcat(prot, "/exec", sizeof(prot));
804 
805 		sz = (size_t)((vme->end - vme->start) / 1024);
806 		printf("%0*lX %6luK %-15s   %s\n",
807 		    (int)sizeof(void *) * 2, (unsigned long)vme->start,
808 		    (unsigned long)sz, &prot[1], name);
809 	}
810 
811 	if (print_all) {
812 		if (verbose) {
813 			if  (prevend < vme->start)
814 				printf("%0*lx-%0*lx %7luk *\n",
815 				    (int)sizeof(void *) * 2, prevend,
816 				    (int)sizeof(void *) * 2, vme->start - 1,
817 				    (vme->start - prevend) / 1024);
818 			prevend = vme->end;
819 		}
820 
821 		sz = (size_t)((vme->end - vme->start) / 1024);
822 		printf("%0*lx-%0*lx %7luk %0*lx %c%c%c%c%c%c%c (%c%c%c) %d/%d/%d %02u:%02u %7llu - %s",
823 		    (int)sizeof(void *) * 2, vme->start, (int)sizeof(void *) * 2,
824 		    vme->end - (vme->start != vme->end ? 1 : 0), (unsigned long)sz,
825 		    (int)sizeof(void *) * 2, (unsigned long)vme->offset,
826 		    (vme->protection & PROT_READ) ? 'r' : '-',
827 		    (vme->protection & PROT_WRITE) ? 'w' : '-',
828 		    (vme->protection & PROT_EXEC) ? 'x' : '-',
829 		    (vme->etype & UVM_ET_STACK) ? 'S' : '-',
830 		    (vme->etype & UVM_ET_IMMUTABLE) ? 'I' : '-',
831 		    (vme->etype & UVM_ET_COPYONWRITE) ? 'p' : 's',
832 		    (vme->etype & UVM_ET_NEEDSCOPY) ? '+' : '-',
833 		    (vme->max_protection & PROT_READ) ? 'r' : '-',
834 		    (vme->max_protection & PROT_WRITE) ? 'w' : '-',
835 		    (vme->max_protection & PROT_EXEC) ? 'x' : '-',
836 		    vme->inheritance, vme->wired_count, vme->advice,
837 		    major(dev), minor(dev), (unsigned long long)inode, name);
838 		if (A(vp))
839 			printf(" [%p]", P(vp));
840 		printf("\n");
841 	}
842 
843 	if (print_amap && vme->aref.ar_amap) {
844 		printf(" amap - ref: %d fl: 0x%x nsl: %d nuse: %d\n",
845 		    D(amap, vm_amap)->am_ref,
846 		    D(amap, vm_amap)->am_flags,
847 		    D(amap, vm_amap)->am_nslot,
848 		    D(amap, vm_amap)->am_nused);
849 		if (sum) {
850 			sum->s_am_nslots += D(amap, vm_amap)->am_nslot;
851 			sum->s_am_nusedslots += D(amap, vm_amap)->am_nused;
852 		}
853 	}
854 
855 	/* no access allowed, don't count space */
856 	if ((vme->protection & rwx) == 0)
857 		sz = 0;
858 
859 	return (sz);
860 }
861 
862 char *
findname(kvm_t * kd,struct kbit * vmspace,struct vm_map_entry * vme,struct kbit * vp,struct kbit * vfs,struct kbit * uvm_obj)863 findname(kvm_t *kd, struct kbit *vmspace,
864     struct vm_map_entry *vme, struct kbit *vp,
865     struct kbit *vfs, struct kbit *uvm_obj)
866 {
867 	static char buf[1024], *name;
868 	size_t l;
869 
870 	if (UVM_ET_ISOBJ(vme)) {
871 		if (A(vfs)) {
872 			l = strlen(D(vfs, mount)->mnt_stat.f_mntonname);
873 			switch (search_cache(kd, vp, &name, buf, sizeof(buf))) {
874 			case 0: /* found something */
875 				if (name - (1 + 11 + l) < buf)
876 					break;
877 				name--;
878 				*name = '/';
879 				/*FALLTHROUGH*/
880 			case 2: /* found nothing */
881 				name -= 11;
882 				memcpy(name, " -unknown- ", (size_t)11);
883 				name -= l;
884 				memcpy(name,
885 				    D(vfs, mount)->mnt_stat.f_mntonname, l);
886 				break;
887 			case 1: /* all is well */
888 				if (name - (1 + l) < buf)
889 					break;
890 				name--;
891 				*name = '/';
892 				if (l != 1) {
893 					name -= l;
894 					memcpy(name,
895 					    D(vfs, mount)->mnt_stat.f_mntonname, l);
896 				}
897 				break;
898 			}
899 		} else if (UVM_OBJ_IS_DEVICE(D(uvm_obj, uvm_object))) {
900 			struct kbit kdev;
901 			dev_t dev;
902 
903 			P(&kdev) = P(uvm_obj);
904 			S(&kdev) = sizeof(struct uvm_device);
905 			KDEREF(kd, &kdev);
906 			dev = D(&kdev, uvm_device)->u_device;
907 			name = devname(dev, S_IFCHR);
908 			if (name != NULL)
909 				snprintf(buf, sizeof(buf), "/dev/%s", name);
910 			else
911 				snprintf(buf, sizeof(buf), "  [ device %u,%u ]",
912 				    major(dev), minor(dev));
913 			name = buf;
914 		} else if (UVM_OBJ_IS_AOBJ(D(uvm_obj, uvm_object)))
915 			name = "  [ uvm_aobj ]";
916 		else if (UVM_OBJ_IS_VNODE(D(uvm_obj, uvm_object)))
917 			name = "  [ ?VNODE? ]";
918 		else {
919 			snprintf(buf, sizeof(buf), "  [ unknown (%p) ]",
920 			    D(uvm_obj, uvm_object)->pgops);
921 			name = buf;
922 		}
923 	} else if (D(vmspace, vmspace)->vm_maxsaddr <= (caddr_t)vme->start &&
924 	    (D(vmspace, vmspace)->vm_maxsaddr + (size_t)maxssiz) >=
925 	    (caddr_t)vme->end) {
926 		name = "  [ stack ]";
927 	} else if (UVM_ET_ISHOLE(vme))
928 		name = "  [ hole ]";
929 	else
930 		name = "  [ anon ]";
931 
932 	return (name);
933 }
934 
935 int
search_cache(kvm_t * kd,struct kbit * vp,char ** name,char * buf,size_t blen)936 search_cache(kvm_t *kd, struct kbit *vp, char **name, char *buf, size_t blen)
937 {
938 	struct cache_entry *ce;
939 	struct kbit svp;
940 	char *o, *e;
941 	u_long cid;
942 
943 	if (!namecache_loaded)
944 		load_name_cache(kd);
945 
946 	P(&svp) = P(vp);
947 	S(&svp) = sizeof(struct vnode);
948 	cid = D(vp, vnode)->v_id;
949 
950 	e = &buf[blen - 1];
951 	o = e;
952 	do {
953 		LIST_FOREACH(ce, &lcache, ce_next)
954 			if (ce->ce_vp == P(&svp) && ce->ce_cid == cid)
955 				break;
956 		if (ce && ce->ce_vp == P(&svp) && ce->ce_cid == cid) {
957 			if (o != e) {
958 				if (o <= buf)
959 					break;
960 				*(--o) = '/';
961 			}
962 			if (o - ce->ce_nlen <= buf)
963 				break;
964 			o -= ce->ce_nlen;
965 			memcpy(o, ce->ce_name, ce->ce_nlen);
966 			P(&svp) = ce->ce_pvp;
967 			cid = ce->ce_pcid;
968 		} else
969 			break;
970 	} while (1/*CONSTCOND*/);
971 	*e = '\0';
972 	*name = o;
973 
974 	if (e == o)
975 		return (2);
976 
977 	KDEREF(kd, &svp);
978 	return (D(&svp, vnode)->v_flag & VROOT);
979 }
980 
981 void
load_name_cache(kvm_t * kd)982 load_name_cache(kvm_t *kd)
983 {
984 	struct namecache n, *tmp;
985 	struct namecache_head nchead;
986 
987 	LIST_INIT(&lcache);
988 	_KDEREF(kd, nclruhead_addr, &nchead, sizeof(nchead));
989 	tmp = TAILQ_FIRST(&nchead);
990 	while (tmp != NULL) {
991 		_KDEREF(kd, (u_long)tmp, &n, sizeof(n));
992 
993 		if (n.nc_nlen > 0) {
994 			if (n.nc_nlen > 2 ||
995 			    n.nc_name[0] != '.' ||
996 			    (n.nc_nlen != 1 && n.nc_name[1] != '.'))
997 				cache_enter(&n);
998 		}
999 		tmp = TAILQ_NEXT(&n, nc_lru);
1000 	}
1001 
1002 	namecache_loaded = 1;
1003 }
1004 
1005 void
cache_enter(struct namecache * ncp)1006 cache_enter(struct namecache *ncp)
1007 {
1008 	struct cache_entry *ce;
1009 
1010 	if (debug & DUMP_NAMEI_CACHE)
1011 		printf("ncp->nc_vp %10p, ncp->nc_dvp %10p, ncp->nc_nlen "
1012 		    "%3d [%.*s] (nc_dvpid=%lu, nc_vpid=%lu)\n",
1013 		    ncp->nc_vp, ncp->nc_dvp,
1014 		    ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name,
1015 		    ncp->nc_dvpid, ncp->nc_vpid);
1016 
1017 	ce = malloc(sizeof(struct cache_entry));
1018 	if (ce == NULL)
1019 		err(1, "cache_enter");
1020 
1021 	ce->ce_vp = ncp->nc_vp;
1022 	ce->ce_pvp = ncp->nc_dvp;
1023 	ce->ce_cid = ncp->nc_vpid;
1024 	ce->ce_pcid = ncp->nc_dvpid;
1025 	ce->ce_nlen = (unsigned)ncp->nc_nlen;
1026 	strlcpy(ce->ce_name, ncp->nc_name, sizeof(ce->ce_name));
1027 
1028 	LIST_INSERT_HEAD(&lcache, ce, ce_next);
1029 }
1030 
1031 static void __dead
usage(void)1032 usage(void)
1033 {
1034 	extern char *__progname;
1035 	fprintf(stderr, "usage: %s [-AadlmPsv] [-D number] "
1036 	    "[-M core] [-N system] [-p pid] [pid ...]\n",
1037 	    __progname);
1038 	exit(1);
1039 }
1040 
1041 static pid_t
strtopid(const char * str)1042 strtopid(const char *str)
1043 {
1044 	pid_t pid;
1045 
1046 	errno = 0;
1047 	pid = (pid_t)strtonum(str, 0, INT_MAX, NULL);
1048 	if (errno != 0)
1049 		usage();
1050 	return (pid);
1051 }
1052