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