xref: /netbsd-src/usr.bin/pmap/main.c (revision 4b71a66d0f279143147d63ebfcfd8a59499a3684)
1 /*	$NetBSD: main.c,v 1.18 2008/04/28 20:24:14 martin Exp $ */
2 
3 /*
4  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Brown.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: main.c,v 1.18 2008/04/28 20:24:14 martin Exp $");
35 #endif
36 
37 #include <sys/param.h>
38 
39 #ifndef __NetBSD_Version__
40 #error go away, you fool
41 #elif (__NetBSD_Version__ < 105000000)
42 #error only works with uvm
43 #endif
44 
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <limits.h>
49 #include <string.h>
50 
51 #include "pmap.h"
52 #include "main.h"
53 
54 struct cache_head lcache;
55 struct nchashhead *nchashtbl;
56 void *uvm_vnodeops, *uvm_deviceops, *aobj_pager, *ubc_pager;
57 void *kernel_floor;
58 struct vm_map *kmem_map, *mb_map, *phys_map, *exec_map, *pager_map;
59 struct vm_map *st_map, *pt_map, *lkm_map, *buf_map;
60 u_long nchash_addr, nchashtbl_addr, kernel_map_addr;
61 int debug, verbose, recurse, page_size;
62 int print_all, print_map, print_maps, print_solaris, print_ddb;
63 rlim_t maxssiz;
64 
65 struct nlist ksyms[] = {
66 	{ "_maxsmap" },
67 #define NL_MAXSSIZ		0
68 	{ "_uvm_vnodeops" },
69 #define NL_UVM_VNODEOPS		1
70 	{ "_uvm_deviceops" },
71 #define NL_UVM_DEVICEOPS	2
72 	{ "_aobj_pager" },
73 #define NL_AOBJ_PAGER		3
74 	{ "_ubc_pager" },
75 #define NL_UBC_PAGER		4
76 	{ "_kernel_map" },
77 #define NL_KERNEL_MAP		5
78 	{ "_nchashtbl" },
79 #define NL_NCHASHTBL		6
80 	{ "_nchash" },
81 #define NL_NCHASH		7
82 	{ "_kernel_text" },
83 #define NL_KENTER		8
84 	{ NULL }
85 };
86 
87 struct nlist kmaps[] = {
88 	{ "_kmem_map" },
89 #define NL_kmem_map		0
90 	{ "_mb_map" },
91 #define NL_mb_map		1
92 	{ "_phys_map" },
93 #define NL_phys_map		2
94 	{ "_exec_map" },
95 #define NL_exec_map		3
96 	{ "_pager_map" },
97 #define NL_pager_map		4
98 	{ "_st_map" },
99 #define NL_st_map		5
100 	{ "_pt_map" },
101 #define NL_pt_map		6
102 	{ "_lkm_map" },
103 #define NL_lkm_map		7
104 	{ "_buf_map" },
105 #define NL_buf_map		8
106 	{ NULL }
107 };
108 
109 #define VMSPACE_ADDRESS		1
110 #define VM_MAP_ADDRESS		2
111 #define VM_MAP_ENTRY_ADDRESS	3
112 #define AMAP_ADDRESS		4
113 
114 void check_fd(int);
115 void load_symbols(kvm_t *);
116 void cache_enter(int, struct namecache *);
117 
118 int
119 main(int argc, char *argv[])
120 {
121 	kvm_t *kd;
122 	pid_t pid;
123 	int which, many, ch, rc;
124 	char errbuf[_POSIX2_LINE_MAX + 1];
125 	struct kinfo_proc2 *kproc;
126 	char *kmem, *kernel, *t;
127 	gid_t egid;
128 	struct kbit kbit, *vmspace;
129 	u_long address;
130 
131 	egid = getegid();
132 	if (setegid(getgid()) == -1)
133 		err(1, "failed to reset privileges");
134 
135 	check_fd(STDIN_FILENO);
136 	check_fd(STDOUT_FILENO);
137 	check_fd(STDERR_FILENO);
138 
139 	pid = -1;
140 	which = verbose = debug = 0;
141 	print_all = print_map = print_maps = print_solaris = print_ddb = 0;
142 	recurse = 0;
143 	kmem = kernel = NULL;
144 	address = 0;
145 	vmspace = &kbit;
146 
147 	while ((ch = getopt(argc, argv, "A:aD:dE:lM:mN:Pp:RrS:sV:vx")) != -1) {
148 		switch (ch) {
149 		case 'A':
150 		case 'E':
151 		case 'S':
152 		case 'V':
153 			if (which != 0)
154 				errx(1, "use only one of -A, -E, -S, or -V");
155 			errno = 0;
156 			address = strtoul(optarg, &t, 0);
157 			if (*t != '\0')
158 				errx(1, "%s is not a valid address", optarg);
159 			if (errno != 0)
160 				err(1, "%s is not a valid address", optarg);
161 			switch (ch) {
162 			case 'A':	which = AMAP_ADDRESS;		break;
163 			case 'E':	which = VM_MAP_ENTRY_ADDRESS;	break;
164 			case 'S':	which = VMSPACE_ADDRESS;	break;
165 			case 'V':	which = VM_MAP_ADDRESS;		break;
166 			}
167 			break;
168 		case 'a':
169 			print_all = 1;
170 			break;
171 		case 'd':
172 			print_ddb = 1;
173 			break;
174 		case 'D':
175 			errno = 0;
176 			debug = strtoul(optarg, &t, 0);
177 			if (*t != '\0')
178 				errx(1, "%s is not a valid number", optarg);
179 			if (errno != 0)
180 				err(1, "%s is not a valid number", optarg);
181 			break;
182 		case 'l':
183 			print_maps = 1;
184 			break;
185 		case 'm':
186 			print_map = 1;
187 			break;
188 		case 'M':
189 			kmem = optarg;
190 			break;
191 		case 'N':
192 			kernel = optarg;
193 			break;
194 		case 'p':
195 			errno = 0;
196 			pid = strtol(optarg, &t, 0);
197 			if (pid < 0)
198 				errno = EINVAL;
199 			if (*t != '\0')
200 				errx(1, "%s is not a valid pid", optarg);
201 			if (errno != 0)
202 				err(1, "%s is not a valid pid", optarg);
203 			break;
204 		case 'P':
205 			pid = getpid();
206 			break;
207 		case 'R':
208 			recurse = 1;
209 			break;
210 		case 's':
211 			print_solaris = 1;
212 			break;
213 		case 'v':
214 			verbose++;
215 			break;
216 		case 'r':
217 		case 'x':
218 			errx(1, "-%c option not implemented, sorry", optopt);
219 			/*NOTREACHED*/
220 		case '?':
221 		default:
222 			fprintf(stderr, "usage: %s [-adlmPRsv] [-A address] "
223 				"[-D number] [-E address] [-M core]\n"
224 				"\t[-N system] [-p pid] [-S address] "
225 				"[-V address] [pid ...]\n",
226 				getprogname());
227 			exit(1);
228 		}
229 	}
230 	argc -= optind;
231 	argv += optind;
232 
233 	/* more than one "process" to dump? */
234 	many = (argc > 1 - (pid == -1 ? 0 : 1)) ? 1 : 0;
235 
236 	/* apply default */
237 	if (print_all + print_map + print_maps + print_solaris +
238 	    print_ddb == 0)
239 		print_solaris = 1;
240 
241 	/* get privs back if it appears to be safe, otherwise toss them */
242 	if (kernel == NULL && kmem == NULL && address == 0)
243 		rc = setegid(egid);
244 	else
245 		rc = setgid(getgid());
246 	if (rc == -1)
247 		err(1, "failed to reset privileges");
248 
249 	/* start by opening libkvm */
250 	kd = kvm_openfiles(kernel, kmem, NULL, O_RDONLY, errbuf);
251 
252 	/* we're completely done with privileges now */
253 	rc = setgid(getgid());
254 	if (rc == -1)
255 		err(1, "failed to reset privileges");
256 
257 	/* print the kvm_open error, if any */
258 	errbuf[_POSIX2_LINE_MAX] = '\0';
259 	if (kd == NULL)
260 		errx(1, "%s", errbuf);
261 
262 	/* get "bootstrap" addresses from kernel */
263 	load_symbols(kd);
264 
265 	if (address) {
266 		struct kbit kbit2, *at = &kbit2;
267 
268 		memset(vmspace, 0, sizeof(*vmspace));
269 		A(at) = address;
270 		S(at) = (size_t)-1;
271 
272 		switch (which) {
273 		    case VMSPACE_ADDRESS:
274 			/* (kd, kproc, vmspace, thing) */
275 			(*process_map)(kd, NULL, at, "vm_map");
276 			break;
277 		    case VM_MAP_ADDRESS:
278 			/* (kd, proc, vmspace, vm_map, thing) */
279 			(*dump_vm_map)(kd, NULL, vmspace, at, "vm_map");
280 			break;
281 		    case VM_MAP_ENTRY_ADDRESS:
282 			/* (kd, proc, vmspace, vm_map_entry, 0) */
283 			(*dump_vm_map_entry)(kd, NULL, vmspace, at, 0);
284 			break;
285 		    case AMAP_ADDRESS:
286 			/* (kd, amap) */
287 			(*dump_amap)(kd, at);
288 			break;
289 		}
290 		exit(0);
291 	}
292 
293 	do {
294 		if (pid == -1) {
295 			if (argc == 0)
296 				pid = getppid();
297 			else {
298 				errno = 0;
299 				pid = strtol(argv[0], &t, 0);
300 				if (pid < 0)
301 					errno = EINVAL;
302 				if (*t != '\0')
303 					errx(1, "%s is not a valid pid",
304 					    argv[0]);
305 				if (errno != 0)
306 					err(1, "%s is not a valid pid",
307 					    argv[0]);
308 				argv++;
309 				argc--;
310 			}
311 		}
312 
313 		/* find the process id */
314 		if (pid == 0)
315 			kproc = NULL;
316 		else {
317 			kproc = kvm_getproc2(kd, KERN_PROC_PID, pid,
318 					     sizeof(struct kinfo_proc2), &rc);
319 			if (kproc == NULL || rc == 0) {
320 				errno = ESRCH;
321 				warn("%d", pid);
322 				pid = -1;
323 				continue;
324 			}
325 		}
326 
327 		/* dump it */
328 		if (many) {
329 			if (kproc)
330 				printf("process %d:\n", kproc->p_pid);
331 			else
332 				printf("kernel:\n");
333 		}
334 
335 		(*process_map)(kd, kproc, vmspace, NULL);
336 		pid = -1;
337 	} while (argc > 0);
338 
339 	/* done.  go away. */
340 	rc = kvm_close(kd);
341 	if (rc == -1)
342 		err(1, "kvm_close");
343 
344 	return (0);
345 }
346 
347 void
348 check_fd(int fd)
349 {
350 	struct stat st;
351 	int n;
352 
353 	if (fstat(fd, &st) == -1) {
354 		(void)close(fd);
355 		n = open("/dev/null", O_RDWR);
356 		if (n == fd || n == -1)
357 			/* we're either done or we can do no more */
358 			return;
359 		/* if either of these fail, there's not much we can do */
360 		(void)dup2(n, fd);
361 		(void)close(n);
362 		/* XXX should we exit if it fails? */
363 	}
364 }
365 
366 void
367 load_symbols(kvm_t *kd)
368 {
369 	int rc, i, mib[2];
370 	size_t sz;
371 
372 	rc = kvm_nlist(kd, &ksyms[0]);
373 	if (rc != 0) {
374 		for (i = 0; ksyms[i].n_name != NULL; i++)
375 			if (ksyms[i].n_value == 0)
376 				warnx("symbol %s: not found", ksyms[i].n_name);
377 		exit(1);
378 	}
379 
380 	uvm_vnodeops =	(void*)ksyms[NL_UVM_VNODEOPS].n_value;
381 	uvm_deviceops =	(void*)ksyms[NL_UVM_DEVICEOPS].n_value;
382 	aobj_pager =	(void*)ksyms[NL_AOBJ_PAGER].n_value;
383 	ubc_pager =	(void*)ksyms[NL_UBC_PAGER].n_value;
384 
385 	kernel_floor =	(void*)ksyms[NL_KENTER].n_value;
386 	nchash_addr =	ksyms[NL_NCHASH].n_value;
387 
388 	_KDEREF(kd, ksyms[NL_MAXSSIZ].n_value, &maxssiz,
389 		sizeof(maxssiz));
390 	_KDEREF(kd, ksyms[NL_NCHASHTBL].n_value, &nchashtbl_addr,
391 	       sizeof(nchashtbl_addr));
392 	_KDEREF(kd, ksyms[NL_KERNEL_MAP].n_value, &kernel_map_addr,
393 		sizeof(kernel_map_addr));
394 
395 	/*
396 	 * Some of these may be missing from some platforms, for
397 	 * example sparc, sh3, and most powerpc platforms don't
398 	 * have a "phys_map", etc.
399 	 */
400 	(void)kvm_nlist(kd, &kmaps[0]);
401 
402 #define get_map_address(m) do {\
403 	if (kmaps[__CONCAT(NL_,m)].n_value != 0) \
404 		_KDEREF(kd, kmaps[__CONCAT(NL_,m)].n_value, &m, sizeof(m)); \
405 	} while (0/*CONSTCOND*/)
406 
407 	get_map_address(kmem_map);
408 	get_map_address(mb_map);
409 	get_map_address(phys_map);
410 	get_map_address(exec_map);
411 	get_map_address(pager_map);
412 	get_map_address(st_map);
413 	get_map_address(pt_map);
414 	get_map_address(lkm_map);
415 	get_map_address(buf_map);
416 
417 	mib[0] = CTL_HW;
418 	mib[1] = HW_PAGESIZE;
419 	sz = sizeof(page_size);
420 	if (sysctl(&mib[0], 2, &page_size, &sz, NULL, 0) == -1)
421 		err(1, "sysctl: hw.pagesize");
422 }
423 
424 const char *
425 mapname(void *addr)
426 {
427 
428 	if (addr == (void*)kernel_map_addr)
429 		return ("kernel_map");
430 	else if (addr == kmem_map)
431 		return ("kmem_map");
432 	else if (addr == mb_map)
433 		return ("mb_map");
434 	else if (addr == phys_map)
435 		return ("phys_map");
436 	else if (addr == exec_map)
437 		return ("exec_map");
438 	else if (addr == pager_map)
439 		return ("pager_map");
440 	else if (addr == st_map)
441 		return ("st_map");
442 	else if (addr == pt_map)
443 		return ("pt_map");
444 	else if (addr == lkm_map)
445 		return ("lkm_map");
446 	else if (addr == buf_map)
447 		return ("buf_map");
448 	else
449 		return (NULL);
450 }
451 
452 void
453 load_name_cache(kvm_t *kd)
454 {
455 	struct namecache _ncp, *ncp, *oncp;
456 	struct nchashhead _ncpp, *ncpp;
457 	u_long nchash;
458 	int i;
459 
460 	LIST_INIT(&lcache);
461 
462 	_KDEREF(kd, nchash_addr, &nchash, sizeof(nchash));
463 	nchashtbl = malloc(sizeof(nchashtbl) * (int)(nchash + 1));
464 	_KDEREF(kd, nchashtbl_addr, nchashtbl,
465 		sizeof(nchashtbl) * (int)(nchash + 1));
466 
467 	ncpp = &_ncpp;
468 
469 	for (i = 0; i <= nchash; i++) {
470 		ncpp = &nchashtbl[i];
471 		oncp = NULL;
472 		LIST_FOREACH(ncp, ncpp, nc_hash) {
473 			if (ncp == oncp ||
474 			    (void*)ncp < kernel_floor ||
475 			    ncp == (void*)0xdeadbeef)
476 				break;
477 			oncp = ncp;
478 			_KDEREF(kd, (u_long)ncp, &_ncp, sizeof(*ncp));
479 			ncp = &_ncp;
480 			if ((void*)ncp->nc_vp > kernel_floor &&
481 			    ncp->nc_nlen > 0) {
482 				if (ncp->nc_nlen > 2 ||
483 				    ncp->nc_name[0] != '.' ||
484 				    (ncp->nc_name[1] != '.' &&
485 				     ncp->nc_nlen != 1))
486 					cache_enter(i, ncp);
487 			}
488 		}
489 	}
490 }
491 
492 void
493 cache_enter(int i, struct namecache *ncp)
494 {
495 	struct cache_entry *ce;
496 
497 	if (debug & DUMP_NAMEI_CACHE)
498 		printf("[%d] ncp->nc_vp %10p, ncp->nc_dvp %10p, "
499 		       "ncp->nc_nlen %3d [%.*s]\n",
500 		       i, ncp->nc_vp, ncp->nc_dvp,
501 		       ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
502 
503 	ce = malloc(sizeof(struct cache_entry));
504 
505 	ce->ce_vp = ncp->nc_vp;
506 	ce->ce_pvp = ncp->nc_dvp;
507 	ce->ce_nlen = ncp->nc_nlen;
508 	strncpy(ce->ce_name, ncp->nc_name, sizeof(ce->ce_name));
509 	ce->ce_name[MIN(ce->ce_nlen, sizeof(ce->ce_name) - 1)] = '\0';
510 
511 	LIST_INSERT_HEAD(&lcache, ce, ce_next);
512 }
513