1 /* $OpenBSD: procmap.c,v 1.72 2024/03/29 06:54:13 deraadt Exp $ */
2 /* $NetBSD: pmap.c,v 1.1 2002/09/01 20:32:44 atatat Exp $ */
3
4 /*
5 * Copyright (c) 2002 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Andrew Brown.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #define _KERNEL
34 #include <sys/tree.h>
35 #undef _KERNEL
36
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <sys/exec.h>
40 #include <sys/signal.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/uio.h>
45 #include <sys/namei.h>
46 #include <sys/sysctl.h>
47
48 /* XXX until uvm gets cleaned up */
49 typedef int boolean_t;
50
51 #include <uvm/uvm.h>
52 #include <uvm/uvm_device.h>
53 #include <uvm/uvm_amap.h>
54 #include <uvm/uvm_vnode.h>
55
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/inode.h>
58 #undef doff_t
59 #undef IN_ACCESS
60 #undef i_size
61 #undef i_devvp
62 #include <isofs/cd9660/iso.h>
63 #include <isofs/cd9660/cd9660_node.h>
64
65 #include <kvm.h>
66 #include <fcntl.h>
67 #include <errno.h>
68 #include <err.h>
69 #include <stdlib.h>
70 #include <stddef.h>
71 #include <unistd.h>
72 #include <stdio.h>
73 #include <limits.h>
74 #include <string.h>
75
76 /*
77 * stolen (and munged) from #include <uvm/uvm_object.h>
78 */
79 #define UVM_OBJ_IS_VNODE(uobj) ((uobj)->pgops == uvm_vnodeops)
80 #define UVM_OBJ_IS_AOBJ(uobj) ((uobj)->pgops == aobj_pager)
81 #define UVM_OBJ_IS_DEVICE(uobj) ((uobj)->pgops == uvm_deviceops)
82
83 #define PRINT_VMSPACE 0x00000001
84 #define PRINT_VM_MAP 0x00000002
85 #define PRINT_VM_MAP_HEADER 0x00000004
86 #define PRINT_VM_MAP_ENTRY 0x00000008
87 #define DUMP_NAMEI_CACHE 0x00000010
88
89 struct cache_entry {
90 LIST_ENTRY(cache_entry) ce_next;
91 struct vnode *ce_vp, *ce_pvp;
92 u_long ce_cid, ce_pcid;
93 unsigned int ce_nlen;
94 char ce_name[256];
95 };
96
97 LIST_HEAD(cache_head, cache_entry) lcache;
98 TAILQ_HEAD(namecache_head, namecache) nclruhead;
99 int namecache_loaded;
100 void *uvm_vnodeops, *uvm_deviceops, *aobj_pager;
101 u_long kernel_map_addr, nclruhead_addr;
102 int debug, verbose;
103 int print_all, print_map, print_maps, print_solaris, print_ddb, print_amap;
104 int rwx = PROT_READ | PROT_WRITE | PROT_EXEC;
105 rlim_t maxssiz;
106
107 struct sum {
108 unsigned long s_am_nslots;
109 unsigned long s_am_nusedslots;
110 };
111
112 struct kbit {
113 /*
114 * size of data chunk
115 */
116 size_t k_size;
117
118 /*
119 * something for printf() and something for kvm_read()
120 */
121 union {
122 void *k_addr_p;
123 u_long k_addr_ul;
124 } k_addr;
125
126 /*
127 * where we actually put the "stuff"
128 */
129 union {
130 char data[1];
131 struct vmspace vmspace;
132 struct vm_map vm_map;
133 struct vm_map_entry vm_map_entry;
134 struct uvm_vnode uvm_vnode;
135 struct vnode vnode;
136 struct uvm_object uvm_object;
137 struct mount mount;
138 struct inode inode;
139 struct iso_node iso_node;
140 struct uvm_device uvm_device;
141 struct vm_amap vm_amap;
142 } k_data;
143 };
144
145 /* the size of the object in the kernel */
146 #define S(x) ((x)->k_size)
147 /* the address of the object in kernel, two forms */
148 #define A(x) ((x)->k_addr.k_addr_ul)
149 #define P(x) ((x)->k_addr.k_addr_p)
150 /* the data from the kernel */
151 #define D(x,d) (&((x)->k_data.d))
152
153 /* suck the data from the kernel */
154 #define _KDEREF(kd, addr, dst, sz) do { \
155 ssize_t len; \
156 len = kvm_read((kd), (addr), (dst), (sz)); \
157 if (len != (sz)) \
158 errx(1, "%s == %ld vs. %lu @ %lx", \
159 kvm_geterr(kd), (long)len, (unsigned long)(sz), (addr)); \
160 } while (0/*CONSTCOND*/)
161
162 /* suck the data using the structure */
163 #define KDEREF(kd, item) _KDEREF((kd), A(item), D(item, data), S(item))
164
165 struct nlist nl[] = {
166 { "_maxsmap" },
167 #define NL_MAXSSIZ 0
168 { "_uvm_vnodeops" },
169 #define NL_UVM_VNODEOPS 1
170 { "_uvm_deviceops" },
171 #define NL_UVM_DEVICEOPS 2
172 { "_aobj_pager" },
173 #define NL_AOBJ_PAGER 3
174 { "_kernel_map" },
175 #define NL_KERNEL_MAP 4
176 { "_nclruhead" },
177 #define NL_NCLRUHEAD 5
178 { NULL }
179 };
180
181 void load_symbols(kvm_t *);
182 void process_map(kvm_t *, pid_t, struct kinfo_proc *, struct sum *);
183 struct vm_map_entry *load_vm_map_entries(kvm_t *, struct vm_map_entry *,
184 struct vm_map_entry *);
185 void unload_vm_map_entries(struct vm_map_entry *);
186 size_t dump_vm_map_entry(kvm_t *, struct kbit *, struct vm_map_entry *,
187 struct sum *);
188 char *findname(kvm_t *, struct kbit *, struct vm_map_entry *, struct kbit *,
189 struct kbit *, struct kbit *);
190 int search_cache(kvm_t *, struct kbit *, char **, char *, size_t);
191 void load_name_cache(kvm_t *);
192 void cache_enter(struct namecache *);
193 static void __dead usage(void);
194 static pid_t strtopid(const char *);
195 void print_sum(struct sum *, struct sum *);
196
197 /*
198 * uvm_map address tree implementation.
199 */
200 static int no_impl(const void *, const void *);
201 static int
no_impl(const void * p,const void * q)202 no_impl(const void *p, const void *q)
203 {
204 errx(1, "uvm_map address comparison not implemented");
205 return 0;
206 }
207
208 RBT_PROTOTYPE(uvm_map_addr, vm_map_entry, daddrs.addr_entry, no_impl);
209 RBT_GENERATE(uvm_map_addr, vm_map_entry, daddrs.addr_entry, no_impl);
210
211 int
main(int argc,char * argv[])212 main(int argc, char *argv[])
213 {
214 const char *errstr;
215 char errbuf[_POSIX2_LINE_MAX], *kmem = NULL, *kernel = NULL;
216 struct kinfo_proc *kproc;
217 struct sum total_sum;
218 int many, ch, rc;
219 kvm_t *kd;
220 pid_t pid = -1;
221 gid_t gid;
222
223 while ((ch = getopt(argc, argv, "AaD:dlmM:N:p:Prsvx")) != -1) {
224 switch (ch) {
225 case 'A':
226 print_amap = 1;
227 break;
228 case 'a':
229 print_all = 1;
230 break;
231 case 'd':
232 print_ddb = 1;
233 break;
234 case 'D':
235 debug = strtonum(optarg, 0, 0x1f, &errstr);
236 if (errstr)
237 errx(1, "invalid debug mask");
238 break;
239 case 'l':
240 print_maps = 1;
241 break;
242 case 'm':
243 print_map = 1;
244 break;
245 case 'M':
246 kmem = optarg;
247 break;
248 case 'N':
249 kernel = optarg;
250 break;
251 case 'p':
252 pid = strtopid(optarg);
253 break;
254 case 'P':
255 pid = getpid();
256 break;
257 case 's':
258 print_solaris = 1;
259 break;
260 case 'v':
261 verbose = 1;
262 break;
263 case 'r':
264 case 'x':
265 errx(1, "-%c option not implemented, sorry", ch);
266 /*NOTREACHED*/
267 default:
268 usage();
269 }
270 }
271
272 /*
273 * Discard setgid privileges if not the running kernel so that bad
274 * guys can't print interesting stuff from kernel memory.
275 */
276 gid = getgid();
277 if (kernel != NULL || kmem != NULL)
278 if (setresgid(gid, gid, gid) == -1)
279 err(1, "setresgid");
280
281 argc -= optind;
282 argv += optind;
283
284 /* more than one "process" to dump? */
285 many = (argc > 1 - (pid == -1 ? 0 : 1)) ? 1 : 0;
286
287 /* apply default */
288 if (print_all + print_map + print_maps + print_solaris +
289 print_ddb == 0)
290 print_all = 1;
291
292 /* start by opening libkvm */
293 kd = kvm_openfiles(kernel, kmem, NULL, O_RDONLY, errbuf);
294
295 if (kernel == NULL && kmem == NULL)
296 if (setresgid(gid, gid, gid) == -1)
297 err(1, "setresgid");
298
299 if (kd == NULL)
300 errx(1, "%s", errbuf);
301
302 /* get "bootstrap" addresses from kernel */
303 load_symbols(kd);
304
305 memset(&total_sum, 0, sizeof(total_sum));
306
307 do {
308 struct sum sum;
309
310 memset(&sum, 0, sizeof(sum));
311
312 if (pid == -1) {
313 if (argc == 0)
314 pid = getppid();
315 else {
316 pid = strtopid(argv[0]);
317 argv++;
318 argc--;
319 }
320 }
321
322 /* find the process id */
323 if (pid == 0)
324 kproc = NULL;
325 else {
326 kproc = kvm_getprocs(kd, KERN_PROC_PID, pid,
327 sizeof(struct kinfo_proc), &rc);
328 if (kproc == NULL || rc == 0) {
329 warnc(ESRCH, "%d", pid);
330 pid = -1;
331 continue;
332 }
333 }
334
335 /* dump it */
336 if (many) {
337 if (kproc)
338 printf("process %d:\n", pid);
339 else
340 printf("kernel:\n");
341 }
342
343 process_map(kd, pid, kproc, &sum);
344 if (print_amap)
345 print_sum(&sum, &total_sum);
346 pid = -1;
347 } while (argc > 0);
348
349 if (print_amap)
350 print_sum(&total_sum, NULL);
351
352 /* done. go away. */
353 rc = kvm_close(kd);
354 if (rc == -1)
355 err(1, "kvm_close");
356
357 return (0);
358 }
359
360 void
print_sum(struct sum * sum,struct sum * total_sum)361 print_sum(struct sum *sum, struct sum *total_sum)
362 {
363 const char *t = total_sum == NULL ? "total " : "";
364 printf("%samap mapped slots: %lu\n", t, sum->s_am_nslots);
365 printf("%samap used slots: %lu\n", t, sum->s_am_nusedslots);
366
367 if (total_sum) {
368 total_sum->s_am_nslots += sum->s_am_nslots;
369 total_sum->s_am_nusedslots += sum->s_am_nusedslots;
370 }
371 }
372
373 void
process_map(kvm_t * kd,pid_t pid,struct kinfo_proc * proc,struct sum * sum)374 process_map(kvm_t *kd, pid_t pid, struct kinfo_proc *proc, struct sum *sum)
375 {
376 struct kbit kbit[3], *vmspace, *vm_map;
377 struct vm_map_entry *vm_map_entry;
378 size_t total = 0;
379 char *thing;
380 uid_t uid;
381 int vmmap_flags;
382
383 if ((uid = getuid())) {
384 if (pid == 0) {
385 warnx("kernel map is restricted");
386 return;
387 }
388 if (uid != proc->p_uid) {
389 warnx("other users' process maps are restricted");
390 return;
391 }
392 }
393
394 vmspace = &kbit[0];
395 vm_map = &kbit[1];
396
397 A(vmspace) = 0;
398 A(vm_map) = 0;
399
400 if (pid > 0) {
401 A(vmspace) = (u_long)proc->p_vmspace;
402 S(vmspace) = sizeof(struct vmspace);
403 KDEREF(kd, vmspace);
404 thing = "proc->p_vmspace.vm_map";
405 } else {
406 A(vmspace) = 0;
407 S(vmspace) = 0;
408 thing = "kernel_map";
409 }
410
411 if (pid > 0 && (debug & PRINT_VMSPACE)) {
412 printf("proc->p_vmspace %p = {", P(vmspace));
413 printf(" vm_refcnt = %d,", D(vmspace, vmspace)->vm_refcnt);
414 printf(" vm_shm = %p,\n", D(vmspace, vmspace)->vm_shm);
415 printf(" vm_rssize = %d,", D(vmspace, vmspace)->vm_rssize);
416 #if 0
417 printf(" vm_swrss = %d,", D(vmspace, vmspace)->vm_swrss);
418 #endif
419 printf(" vm_tsize = %d,", D(vmspace, vmspace)->vm_tsize);
420 printf(" vm_dsize = %d,\n", D(vmspace, vmspace)->vm_dsize);
421 printf(" vm_ssize = %d,", D(vmspace, vmspace)->vm_ssize);
422 printf(" vm_taddr = %p,", D(vmspace, vmspace)->vm_taddr);
423 printf(" vm_daddr = %p,\n", D(vmspace, vmspace)->vm_daddr);
424 printf(" vm_maxsaddr = %p,",
425 D(vmspace, vmspace)->vm_maxsaddr);
426 printf(" vm_minsaddr = %p }\n",
427 D(vmspace, vmspace)->vm_minsaddr);
428 }
429
430 S(vm_map) = sizeof(struct vm_map);
431 if (pid > 0) {
432 A(vm_map) = A(vmspace);
433 memcpy(D(vm_map, vm_map), &D(vmspace, vmspace)->vm_map,
434 S(vm_map));
435 } else {
436 A(vm_map) = kernel_map_addr;
437 KDEREF(kd, vm_map);
438 }
439 if (debug & PRINT_VM_MAP) {
440 printf("%s %p = {", thing, P(vm_map));
441
442 printf(" pmap = %p,\n", D(vm_map, vm_map)->pmap);
443 printf(" lock = <struct lock>\n");
444 printf(" size = %lx,", D(vm_map, vm_map)->size);
445 printf(" ref_count = %d,", D(vm_map, vm_map)->ref_count);
446 printf(" ref_lock = <struct simplelock>,\n");
447 printf(" min_offset-max_offset = 0x%lx-0x%lx\n",
448 D(vm_map, vm_map)->min_offset,
449 D(vm_map, vm_map)->max_offset);
450 printf(" b_start-b_end = 0x%lx-0x%lx\n",
451 D(vm_map, vm_map)->b_start,
452 D(vm_map, vm_map)->b_end);
453 printf(" s_start-s_end = 0x%lx-0x%lx\n",
454 D(vm_map, vm_map)->s_start,
455 D(vm_map, vm_map)->s_end);
456 vmmap_flags = D(vm_map, vm_map)->flags;
457 printf(" flags = %x <%s%s%s%s%s%s >,\n",
458 vmmap_flags,
459 vmmap_flags & VM_MAP_PAGEABLE ? " PAGEABLE" : "",
460 vmmap_flags & VM_MAP_INTRSAFE ? " INTRSAFE" : "",
461 vmmap_flags & VM_MAP_WIREFUTURE ? " WIREFUTURE" : "",
462 vmmap_flags & VM_MAP_BUSY ? " BUSY" : "",
463 vmmap_flags & VM_MAP_WANTLOCK ? " WANTLOCK" : "",
464 #if VM_MAP_TOPDOWN > 0
465 vmmap_flags & VM_MAP_TOPDOWN ? " TOPDOWN" :
466 #endif
467 "");
468 printf(" timestamp = %u }\n", D(vm_map, vm_map)->timestamp);
469 }
470 if (print_ddb) {
471 printf("MAP %p: [0x%lx->0x%lx]\n", P(vm_map),
472 D(vm_map, vm_map)->min_offset,
473 D(vm_map, vm_map)->max_offset);
474 printf("\tsz=%ld, ref=%d, version=%d, flags=0x%x\n",
475 D(vm_map, vm_map)->size,
476 D(vm_map, vm_map)->ref_count,
477 D(vm_map, vm_map)->timestamp,
478 D(vm_map, vm_map)->flags);
479 printf("\tpmap=%p(resident=<unknown>)\n",
480 D(vm_map, vm_map)->pmap);
481 }
482
483 /* headers */
484 #ifdef DISABLED_HEADERS
485 if (print_map)
486 printf("%-*s %-*s rwxSe RWX CPY NCP I W A\n",
487 (int)sizeof(long) * 2 + 2, "Start",
488 (int)sizeof(long) * 2 + 2, "End");
489 if (print_maps)
490 printf("%-*s %-*s rwxSep %-*s Dev Inode File\n",
491 (int)sizeof(long) * 2 + 0, "Start",
492 (int)sizeof(long) * 2 + 0, "End",
493 (int)sizeof(long) * 2 + 0, "Offset");
494 if (print_solaris)
495 printf("%-*s %*s Protection File\n",
496 (int)sizeof(long) * 2 + 0, "Start",
497 (int)sizeof(int) * 2 - 1, "Size ");
498 #endif
499 if (print_all)
500 printf("%-*s %-*s %*s %-*s rwxSeIpc RWX I/W/A Dev %*s - File\n",
501 (int)sizeof(long) * 2, "Start",
502 (int)sizeof(long) * 2, "End",
503 (int)sizeof(int) * 2, "Size ",
504 (int)sizeof(long) * 2, "Offset",
505 (int)sizeof(int) * 2, "Inode");
506
507 /* these are the "sub entries" */
508 vm_map_entry = load_vm_map_entries(kd,
509 RBT_ROOT(uvm_map_addr, &D(vm_map, vm_map)->addr), NULL);
510 if (vm_map_entry != NULL) {
511 /* RBTs point at rb_entries inside nodes */
512 D(vm_map, vm_map)->addr.rbh_root.rbt_root =
513 &vm_map_entry->daddrs.addr_entry;
514 } else
515 RBT_INIT(uvm_map_addr, &D(vm_map, vm_map)->addr);
516
517 RBT_FOREACH(vm_map_entry, uvm_map_addr, &D(vm_map, vm_map)->addr)
518 total += dump_vm_map_entry(kd, vmspace, vm_map_entry, sum);
519 unload_vm_map_entries(RBT_ROOT(uvm_map_addr, &D(vm_map, vm_map)->addr));
520
521 if (print_solaris)
522 printf("%-*s %8luK\n",
523 (int)sizeof(void *) * 2 - 2, " total",
524 (unsigned long)total);
525 if (print_all)
526 printf("%-*s %9luk\n",
527 (int)sizeof(void *) * 4 - 1, " total",
528 (unsigned long)total);
529 }
530
531 void
load_symbols(kvm_t * kd)532 load_symbols(kvm_t *kd)
533 {
534 int rc, i;
535
536 rc = kvm_nlist(kd, &nl[0]);
537 if (rc == -1)
538 errx(1, "%s == %d", kvm_geterr(kd), rc);
539 for (i = 0; i < sizeof(nl)/sizeof(nl[0]); i++)
540 if (nl[i].n_value == 0 && nl[i].n_name)
541 printf("%s not found\n", nl[i].n_name);
542
543 uvm_vnodeops = (void*)nl[NL_UVM_VNODEOPS].n_value;
544 uvm_deviceops = (void*)nl[NL_UVM_DEVICEOPS].n_value;
545 aobj_pager = (void*)nl[NL_AOBJ_PAGER].n_value;
546
547 nclruhead_addr = nl[NL_NCLRUHEAD].n_value;
548
549 _KDEREF(kd, nl[NL_MAXSSIZ].n_value, &maxssiz,
550 sizeof(maxssiz));
551 _KDEREF(kd, nl[NL_KERNEL_MAP].n_value, &kernel_map_addr,
552 sizeof(kernel_map_addr));
553 }
554
555 /*
556 * Recreate the addr tree of vm_map in local memory.
557 */
558 struct vm_map_entry *
load_vm_map_entries(kvm_t * kd,struct vm_map_entry * kptr,struct vm_map_entry * parent)559 load_vm_map_entries(kvm_t *kd, struct vm_map_entry *kptr,
560 struct vm_map_entry *parent)
561 {
562 static struct kbit map_ent;
563 struct vm_map_entry *result, *ld;
564
565 if (kptr == NULL)
566 return NULL;
567
568 A(&map_ent) = (u_long)kptr;
569 S(&map_ent) = sizeof(struct vm_map_entry);
570 KDEREF(kd, &map_ent);
571
572 result = malloc(sizeof(*result));
573 if (result == NULL)
574 err(1, "malloc");
575 memcpy(result, D(&map_ent, vm_map_entry), sizeof(struct vm_map_entry));
576
577 /*
578 * Recurse to download rest of the tree.
579 */
580
581 /* RBTs point at rb_entries inside nodes */
582 ld = load_vm_map_entries(kd, RBT_LEFT(uvm_map_addr, result), result);
583 result->daddrs.addr_entry.rbt_left = &ld->daddrs.addr_entry;
584 ld = load_vm_map_entries(kd, RBT_RIGHT(uvm_map_addr, result), result);
585 result->daddrs.addr_entry.rbt_right = &ld->daddrs.addr_entry;
586 result->daddrs.addr_entry.rbt_parent = &parent->daddrs.addr_entry;
587
588 return result;
589 }
590
591 /*
592 * Release the addr tree of vm_map.
593 */
594 void
unload_vm_map_entries(struct vm_map_entry * ent)595 unload_vm_map_entries(struct vm_map_entry *ent)
596 {
597 if (ent == NULL)
598 return;
599
600 unload_vm_map_entries(RBT_LEFT(uvm_map_addr, ent));
601 unload_vm_map_entries(RBT_RIGHT(uvm_map_addr, ent));
602 free(ent);
603 }
604
605 size_t
dump_vm_map_entry(kvm_t * kd,struct kbit * vmspace,struct vm_map_entry * vme,struct sum * sum)606 dump_vm_map_entry(kvm_t *kd, struct kbit *vmspace,
607 struct vm_map_entry *vme, struct sum *sum)
608 {
609 struct kbit kbit[5], *uvm_obj, *vp, *vfs, *amap, *uvn;
610 ino_t inode = 0;
611 dev_t dev = 0;
612 size_t sz = 0;
613 char *name;
614 static u_long prevend;
615
616 uvm_obj = &kbit[0];
617 vp = &kbit[1];
618 vfs = &kbit[2];
619 amap = &kbit[3];
620 uvn = &kbit[4];
621
622 A(uvm_obj) = 0;
623 A(vp) = 0;
624 A(vfs) = 0;
625 A(uvn) = 0;
626
627 if (debug & PRINT_VM_MAP_ENTRY) {
628 printf("%s = {", "vm_map_entry");
629 printf(" start = %lx,", vme->start);
630 printf(" end = %lx,", vme->end);
631 printf(" fspace = %lx,\n", vme->fspace);
632 printf(" object.uvm_obj/sub_map = %p,\n",
633 vme->object.uvm_obj);
634 printf(" offset = %lx,", (unsigned long)vme->offset);
635 printf(" etype = %x <%s%s%s%s%s >,", vme->etype,
636 vme->etype & UVM_ET_OBJ ? " OBJ" : "",
637 vme->etype & UVM_ET_SUBMAP ? " SUBMAP" : "",
638 vme->etype & UVM_ET_COPYONWRITE ? " COW" : "",
639 vme->etype & UVM_ET_NEEDSCOPY ? " NEEDSCOPY" : "",
640 vme->etype & UVM_ET_HOLE ? " HOLE" : "");
641 printf(" protection = %x,\n", vme->protection);
642 printf(" max_protection = %x,", vme->max_protection);
643 printf(" inheritance = %d,", vme->inheritance);
644 printf(" wired_count = %d,\n", vme->wired_count);
645 printf(" aref = <struct vm_aref>,");
646 printf(" advice = %d,", vme->advice);
647 printf(" flags = %x <%s%s > }\n", vme->flags,
648 vme->flags & UVM_MAP_STATIC ? " STATIC" : "",
649 vme->flags & UVM_MAP_KMEM ? " KMEM" : "");
650 }
651
652 A(vp) = 0;
653 A(uvm_obj) = 0;
654
655 if (vme->object.uvm_obj != NULL) {
656 P(uvm_obj) = vme->object.uvm_obj;
657 S(uvm_obj) = sizeof(struct uvm_object);
658 KDEREF(kd, uvm_obj);
659 if (UVM_ET_ISOBJ(vme) &&
660 UVM_OBJ_IS_VNODE(D(uvm_obj, uvm_object))) {
661 P(uvn) = P(uvm_obj);
662 S(uvn) = sizeof(struct uvm_vnode);
663 KDEREF(kd, uvn);
664
665 P(vp) = D(uvn, uvm_vnode)->u_vnode;
666 S(vp) = sizeof(struct vnode);
667 KDEREF(kd, vp);
668 }
669 }
670
671 if (vme->aref.ar_amap != NULL) {
672 P(amap) = vme->aref.ar_amap;
673 S(amap) = sizeof(struct vm_amap);
674 KDEREF(kd, amap);
675 }
676
677 A(vfs) = 0;
678
679 if (P(vp) != NULL && D(vp, vnode)->v_mount != NULL) {
680 P(vfs) = D(vp, vnode)->v_mount;
681 S(vfs) = sizeof(struct mount);
682 KDEREF(kd, vfs);
683 D(vp, vnode)->v_mount = D(vfs, mount);
684 }
685
686 /*
687 * dig out the device number and inode number from certain
688 * file system types.
689 */
690 #define V_DATA_IS(vp, type, d, i) do { \
691 struct kbit data; \
692 P(&data) = D(vp, vnode)->v_data; \
693 S(&data) = sizeof(*D(&data, type)); \
694 KDEREF(kd, &data); \
695 dev = D(&data, type)->d; \
696 inode = D(&data, type)->i; \
697 } while (0/*CONSTCOND*/)
698
699 if (A(vp) &&
700 D(vp, vnode)->v_type == VREG &&
701 D(vp, vnode)->v_data != NULL) {
702 switch (D(vp, vnode)->v_tag) {
703 case VT_UFS:
704 case VT_EXT2FS:
705 V_DATA_IS(vp, inode, i_dev, i_number);
706 break;
707 case VT_ISOFS:
708 V_DATA_IS(vp, iso_node, i_dev, i_number);
709 break;
710 case VT_NON:
711 case VT_NFS:
712 case VT_MFS:
713 case VT_MSDOSFS:
714 default:
715 break;
716 }
717 }
718
719 name = findname(kd, vmspace, vme, vp, vfs, uvm_obj);
720
721 if (print_map) {
722 printf("0x%-*lx 0x%-*lx %c%c%c%c%c %c%c%c %s %s %d %d %d",
723 (int)sizeof(long) * 2 + 0, vme->start,
724 (int)sizeof(long) * 2 + 0, vme->end,
725 (vme->protection & PROT_READ) ? 'r' : '-',
726 (vme->protection & PROT_WRITE) ? 'w' : '-',
727 (vme->protection & PROT_EXEC) ? 'x' : '-',
728 (vme->etype & UVM_ET_STACK) ? 'S' : '-',
729 (vme->etype & UVM_ET_IMMUTABLE) ? 'I' : '-',
730 (vme->max_protection & PROT_READ) ? 'r' : '-',
731 (vme->max_protection & PROT_WRITE) ? 'w' : '-',
732 (vme->max_protection & PROT_EXEC) ? 'x' : '-',
733 (vme->etype & UVM_ET_COPYONWRITE) ? "COW" : "NCOW",
734 (vme->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC",
735 vme->inheritance, vme->wired_count,
736 vme->advice);
737 if (verbose) {
738 if (inode)
739 printf(" %u,%u %llu",
740 major(dev), minor(dev),
741 (unsigned long long)inode);
742 if (name[0])
743 printf(" %s", name);
744 }
745 printf("\n");
746 }
747
748 if (print_maps)
749 printf("0x%-*lx 0x%-*lx %c%c%c%c%c%c %0*lx %02x:%02x %llu %s\n",
750 (int)sizeof(void *) * 2, vme->start,
751 (int)sizeof(void *) * 2, vme->end,
752 (vme->protection & PROT_READ) ? 'r' : '-',
753 (vme->protection & PROT_WRITE) ? 'w' : '-',
754 (vme->protection & PROT_EXEC) ? 'x' : '-',
755 (vme->etype & UVM_ET_STACK) ? 'S' : '-',
756 (vme->etype & UVM_ET_IMMUTABLE) ? 'I' : '-',
757 (vme->etype & UVM_ET_COPYONWRITE) ? 'p' : 's',
758 (int)sizeof(void *) * 2,
759 (unsigned long)vme->offset,
760 major(dev), minor(dev), (unsigned long long)inode,
761 inode ? name : "");
762
763 if (print_ddb) {
764 printf(" - <lost address>: 0x%lx->0x%lx: "
765 "obj=%p/0x%lx, amap=%p/%d\n",
766 vme->start, vme->end,
767 vme->object.uvm_obj, (unsigned long)vme->offset,
768 vme->aref.ar_amap, vme->aref.ar_pageoff);
769 printf("\tsubmap=%c, cow=%c, nc=%c, stack=%c, "
770 "immutable=%c, prot(max)=%d/%d, inh=%d, "
771 "wc=%d, adv=%d\n",
772 (vme->etype & UVM_ET_SUBMAP) ? 'T' : 'F',
773 (vme->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F',
774 (vme->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F',
775 (vme->etype & UVM_ET_STACK) ? 'T' : 'F',
776 (vme->etype & UVM_ET_IMMUTABLE) ? 'T' : 'F',
777 vme->protection, vme->max_protection,
778 vme->inheritance, vme->wired_count, vme->advice);
779 if (inode && verbose)
780 printf("\t(dev=%u,%u ino=%llu [%s] [%p])\n",
781 major(dev), minor(dev), (unsigned long long)inode,
782 inode ? name : "", P(vp));
783 else if (name[0] == ' ' && verbose)
784 printf("\t(%s)\n", &name[2]);
785 }
786
787 if (print_solaris) {
788 char prot[30];
789
790 prot[0] = '\0';
791 prot[1] = '\0';
792 if (vme->protection & PROT_READ)
793 strlcat(prot, "/read", sizeof(prot));
794 if (vme->protection & PROT_WRITE)
795 strlcat(prot, "/write", sizeof(prot));
796 if (vme->protection & PROT_EXEC)
797 strlcat(prot, "/exec", sizeof(prot));
798
799 sz = (size_t)((vme->end - vme->start) / 1024);
800 printf("%0*lX %6luK %-15s %s\n",
801 (int)sizeof(void *) * 2, (unsigned long)vme->start,
802 (unsigned long)sz, &prot[1], name);
803 }
804
805 if (print_all) {
806 if (verbose) {
807 if (prevend < vme->start)
808 printf("%0*lx-%0*lx %7luk *\n",
809 (int)sizeof(void *) * 2, prevend,
810 (int)sizeof(void *) * 2, vme->start - 1,
811 (vme->start - prevend) / 1024);
812 prevend = vme->end;
813 }
814
815 sz = (size_t)((vme->end - vme->start) / 1024);
816 printf("%0*lx-%0*lx %7luk %0*lx %c%c%c%c%c%c%c (%c%c%c) %d/%d/%d %02u:%02u %7llu - %s",
817 (int)sizeof(void *) * 2, vme->start, (int)sizeof(void *) * 2,
818 vme->end - (vme->start != vme->end ? 1 : 0), (unsigned long)sz,
819 (int)sizeof(void *) * 2, (unsigned long)vme->offset,
820 (vme->protection & PROT_READ) ? 'r' : '-',
821 (vme->protection & PROT_WRITE) ? 'w' : '-',
822 (vme->protection & PROT_EXEC) ? 'x' : '-',
823 (vme->etype & UVM_ET_STACK) ? 'S' : '-',
824 (vme->etype & UVM_ET_IMMUTABLE) ? 'I' : '-',
825 (vme->etype & UVM_ET_COPYONWRITE) ? 'p' : 's',
826 (vme->etype & UVM_ET_NEEDSCOPY) ? '+' : '-',
827 (vme->max_protection & PROT_READ) ? 'r' : '-',
828 (vme->max_protection & PROT_WRITE) ? 'w' : '-',
829 (vme->max_protection & PROT_EXEC) ? 'x' : '-',
830 vme->inheritance, vme->wired_count, vme->advice,
831 major(dev), minor(dev), (unsigned long long)inode, name);
832 if (A(vp))
833 printf(" [%p]", P(vp));
834 printf("\n");
835 }
836
837 if (print_amap && vme->aref.ar_amap) {
838 printf(" amap - ref: %d fl: 0x%x nsl: %d nuse: %d\n",
839 D(amap, vm_amap)->am_ref,
840 D(amap, vm_amap)->am_flags,
841 D(amap, vm_amap)->am_nslot,
842 D(amap, vm_amap)->am_nused);
843 if (sum) {
844 sum->s_am_nslots += D(amap, vm_amap)->am_nslot;
845 sum->s_am_nusedslots += D(amap, vm_amap)->am_nused;
846 }
847 }
848
849 /* no access allowed, don't count space */
850 if ((vme->protection & rwx) == 0)
851 sz = 0;
852
853 return (sz);
854 }
855
856 char *
findname(kvm_t * kd,struct kbit * vmspace,struct vm_map_entry * vme,struct kbit * vp,struct kbit * vfs,struct kbit * uvm_obj)857 findname(kvm_t *kd, struct kbit *vmspace,
858 struct vm_map_entry *vme, struct kbit *vp,
859 struct kbit *vfs, struct kbit *uvm_obj)
860 {
861 static char buf[1024], *name;
862 size_t l;
863
864 if (UVM_ET_ISOBJ(vme)) {
865 if (A(vfs)) {
866 l = strlen(D(vfs, mount)->mnt_stat.f_mntonname);
867 switch (search_cache(kd, vp, &name, buf, sizeof(buf))) {
868 case 0: /* found something */
869 if (name - (1 + 11 + l) < buf)
870 break;
871 name--;
872 *name = '/';
873 /*FALLTHROUGH*/
874 case 2: /* found nothing */
875 name -= 11;
876 memcpy(name, " -unknown- ", (size_t)11);
877 name -= l;
878 memcpy(name,
879 D(vfs, mount)->mnt_stat.f_mntonname, l);
880 break;
881 case 1: /* all is well */
882 if (name - (1 + l) < buf)
883 break;
884 name--;
885 *name = '/';
886 if (l != 1) {
887 name -= l;
888 memcpy(name,
889 D(vfs, mount)->mnt_stat.f_mntonname, l);
890 }
891 break;
892 }
893 } else if (UVM_OBJ_IS_DEVICE(D(uvm_obj, uvm_object))) {
894 struct kbit kdev;
895 dev_t dev;
896
897 P(&kdev) = P(uvm_obj);
898 S(&kdev) = sizeof(struct uvm_device);
899 KDEREF(kd, &kdev);
900 dev = D(&kdev, uvm_device)->u_device;
901 name = devname(dev, S_IFCHR);
902 if (name != NULL)
903 snprintf(buf, sizeof(buf), "/dev/%s", name);
904 else
905 snprintf(buf, sizeof(buf), " [ device %u,%u ]",
906 major(dev), minor(dev));
907 name = buf;
908 } else if (UVM_OBJ_IS_AOBJ(D(uvm_obj, uvm_object)))
909 name = " [ uvm_aobj ]";
910 else if (UVM_OBJ_IS_VNODE(D(uvm_obj, uvm_object)))
911 name = " [ ?VNODE? ]";
912 else {
913 snprintf(buf, sizeof(buf), " [ unknown (%p) ]",
914 D(uvm_obj, uvm_object)->pgops);
915 name = buf;
916 }
917 } else if (D(vmspace, vmspace)->vm_maxsaddr <= (caddr_t)vme->start &&
918 (D(vmspace, vmspace)->vm_maxsaddr + (size_t)maxssiz) >=
919 (caddr_t)vme->end) {
920 name = " [ stack ]";
921 } else if (UVM_ET_ISHOLE(vme))
922 name = " [ hole ]";
923 else
924 name = " [ anon ]";
925
926 return (name);
927 }
928
929 int
search_cache(kvm_t * kd,struct kbit * vp,char ** name,char * buf,size_t blen)930 search_cache(kvm_t *kd, struct kbit *vp, char **name, char *buf, size_t blen)
931 {
932 struct cache_entry *ce;
933 struct kbit svp;
934 char *o, *e;
935 u_long cid;
936
937 if (!namecache_loaded)
938 load_name_cache(kd);
939
940 P(&svp) = P(vp);
941 S(&svp) = sizeof(struct vnode);
942 cid = D(vp, vnode)->v_id;
943
944 e = &buf[blen - 1];
945 o = e;
946 do {
947 LIST_FOREACH(ce, &lcache, ce_next)
948 if (ce->ce_vp == P(&svp) && ce->ce_cid == cid)
949 break;
950 if (ce && ce->ce_vp == P(&svp) && ce->ce_cid == cid) {
951 if (o != e) {
952 if (o <= buf)
953 break;
954 *(--o) = '/';
955 }
956 if (o - ce->ce_nlen <= buf)
957 break;
958 o -= ce->ce_nlen;
959 memcpy(o, ce->ce_name, ce->ce_nlen);
960 P(&svp) = ce->ce_pvp;
961 cid = ce->ce_pcid;
962 } else
963 break;
964 } while (1/*CONSTCOND*/);
965 *e = '\0';
966 *name = o;
967
968 if (e == o)
969 return (2);
970
971 KDEREF(kd, &svp);
972 return (D(&svp, vnode)->v_flag & VROOT);
973 }
974
975 void
load_name_cache(kvm_t * kd)976 load_name_cache(kvm_t *kd)
977 {
978 struct namecache n, *tmp;
979 struct namecache_head nchead;
980
981 LIST_INIT(&lcache);
982 _KDEREF(kd, nclruhead_addr, &nchead, sizeof(nchead));
983 tmp = TAILQ_FIRST(&nchead);
984 while (tmp != NULL) {
985 _KDEREF(kd, (u_long)tmp, &n, sizeof(n));
986
987 if (n.nc_nlen > 0) {
988 if (n.nc_nlen > 2 ||
989 n.nc_name[0] != '.' ||
990 (n.nc_nlen != 1 && n.nc_name[1] != '.'))
991 cache_enter(&n);
992 }
993 tmp = TAILQ_NEXT(&n, nc_lru);
994 }
995
996 namecache_loaded = 1;
997 }
998
999 void
cache_enter(struct namecache * ncp)1000 cache_enter(struct namecache *ncp)
1001 {
1002 struct cache_entry *ce;
1003
1004 if (debug & DUMP_NAMEI_CACHE)
1005 printf("ncp->nc_vp %10p, ncp->nc_dvp %10p, ncp->nc_nlen "
1006 "%3d [%.*s] (nc_dvpid=%lu, nc_vpid=%lu)\n",
1007 ncp->nc_vp, ncp->nc_dvp,
1008 ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name,
1009 ncp->nc_dvpid, ncp->nc_vpid);
1010
1011 ce = malloc(sizeof(struct cache_entry));
1012 if (ce == NULL)
1013 err(1, "cache_enter");
1014
1015 ce->ce_vp = ncp->nc_vp;
1016 ce->ce_pvp = ncp->nc_dvp;
1017 ce->ce_cid = ncp->nc_vpid;
1018 ce->ce_pcid = ncp->nc_dvpid;
1019 ce->ce_nlen = (unsigned)ncp->nc_nlen;
1020 strlcpy(ce->ce_name, ncp->nc_name, sizeof(ce->ce_name));
1021
1022 LIST_INSERT_HEAD(&lcache, ce, ce_next);
1023 }
1024
1025 static void __dead
usage(void)1026 usage(void)
1027 {
1028 extern char *__progname;
1029 fprintf(stderr, "usage: %s [-AadlmPsv] [-D number] "
1030 "[-M core] [-N system] [-p pid] [pid ...]\n",
1031 __progname);
1032 exit(1);
1033 }
1034
1035 static pid_t
strtopid(const char * str)1036 strtopid(const char *str)
1037 {
1038 pid_t pid;
1039
1040 errno = 0;
1041 pid = (pid_t)strtonum(str, 0, INT_MAX, NULL);
1042 if (errno != 0)
1043 usage();
1044 return (pid);
1045 }
1046