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