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