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