2011-10-26 02:13:22 +04:00
|
|
|
/* $NetBSD: main.c,v 1.23 2011/10/25 22:13:22 jym Exp $ */
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Andrew Brown.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2011-10-26 02:13:22 +04:00
|
|
|
__RCSID("$NetBSD: main.c,v 1.23 2011/10/25 22:13:22 jym Exp $");
|
2003-01-08 23:25:12 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#ifndef __NetBSD_Version__
|
|
|
|
#error go away, you fool
|
|
|
|
#elif (__NetBSD_Version__ < 105000000)
|
|
|
|
#error only works with uvm
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
2011-06-24 02:50:53 +04:00
|
|
|
#include <signal.h>
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
#include "pmap.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
struct cache_head lcache;
|
|
|
|
struct nchashhead *nchashtbl;
|
|
|
|
void *uvm_vnodeops, *uvm_deviceops, *aobj_pager, *ubc_pager;
|
|
|
|
struct vm_map *kmem_map, *mb_map, *phys_map, *exec_map, *pager_map;
|
2004-01-06 17:01:19 +03:00
|
|
|
struct vm_map *st_map, *pt_map, *lkm_map, *buf_map;
|
2003-01-08 23:25:12 +03:00
|
|
|
u_long nchash_addr, nchashtbl_addr, kernel_map_addr;
|
2003-03-29 02:10:32 +03:00
|
|
|
int debug, verbose, recurse, page_size;
|
2003-01-08 23:25:12 +03:00
|
|
|
int print_all, print_map, print_maps, print_solaris, print_ddb;
|
|
|
|
rlim_t maxssiz;
|
|
|
|
|
|
|
|
struct nlist ksyms[] = {
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_maxsmap", 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
#define NL_MAXSSIZ 0
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_uvm_vnodeops", 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
#define NL_UVM_VNODEOPS 1
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_uvm_deviceops", 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
#define NL_UVM_DEVICEOPS 2
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_aobj_pager", 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
#define NL_AOBJ_PAGER 3
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_ubc_pager", 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
#define NL_UBC_PAGER 4
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_kernel_map", 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
#define NL_KERNEL_MAP 5
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_nchashtbl", 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
#define NL_NCHASHTBL 6
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_nchash", 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
#define NL_NCHASH 7
|
2009-04-13 04:27:38 +04:00
|
|
|
{ NULL, 0, 0, 0, 0 }
|
2003-01-08 23:25:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nlist kmaps[] = {
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_kmem_map", 0, 0, 0, 0 },
|
2003-02-27 07:10:36 +03:00
|
|
|
#define NL_kmem_map 0
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_mb_map", 0, 0, 0, 0 },
|
2003-02-27 07:10:36 +03:00
|
|
|
#define NL_mb_map 1
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_phys_map", 0, 0, 0, 0 },
|
2003-02-27 07:10:36 +03:00
|
|
|
#define NL_phys_map 2
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_exec_map", 0, 0, 0, 0 },
|
2003-02-27 07:10:36 +03:00
|
|
|
#define NL_exec_map 3
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_pager_map", 0, 0, 0, 0 },
|
2003-02-27 07:10:36 +03:00
|
|
|
#define NL_pager_map 4
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_st_map", 0, 0, 0, 0 },
|
2003-02-27 07:10:36 +03:00
|
|
|
#define NL_st_map 5
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_pt_map", 0, 0, 0, 0 },
|
2003-02-27 07:10:36 +03:00
|
|
|
#define NL_pt_map 6
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_lkm_map", 0, 0, 0, 0 },
|
2003-02-27 07:10:36 +03:00
|
|
|
#define NL_lkm_map 7
|
2009-04-13 04:27:38 +04:00
|
|
|
{ "_buf_map", 0, 0, 0, 0 },
|
2004-01-06 17:01:19 +03:00
|
|
|
#define NL_buf_map 8
|
2009-04-13 04:27:38 +04:00
|
|
|
{ NULL, 0, 0, 0, 0 },
|
2003-01-08 23:25:12 +03:00
|
|
|
};
|
|
|
|
|
2003-04-04 07:49:20 +04:00
|
|
|
#define VMSPACE_ADDRESS 1
|
|
|
|
#define VM_MAP_ADDRESS 2
|
|
|
|
#define VM_MAP_ENTRY_ADDRESS 3
|
|
|
|
#define AMAP_ADDRESS 4
|
|
|
|
|
2003-01-08 23:25:12 +03:00
|
|
|
void check_fd(int);
|
|
|
|
void load_symbols(kvm_t *);
|
2009-04-13 04:27:38 +04:00
|
|
|
void cache_enter(u_long, struct namecache *);
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
kvm_t *kd;
|
|
|
|
pid_t pid;
|
2011-10-26 02:13:22 +04:00
|
|
|
uid_t uid;
|
2003-04-04 07:49:20 +04:00
|
|
|
int which, many, ch, rc;
|
2003-01-08 23:25:12 +03:00
|
|
|
char errbuf[_POSIX2_LINE_MAX + 1];
|
|
|
|
struct kinfo_proc2 *kproc;
|
2003-04-04 07:49:20 +04:00
|
|
|
char *kmem, *kernel, *t;
|
2003-01-09 16:05:12 +03:00
|
|
|
gid_t egid;
|
2003-04-04 07:49:20 +04:00
|
|
|
struct kbit kbit, *vmspace;
|
|
|
|
u_long address;
|
2003-01-09 16:05:12 +03:00
|
|
|
|
|
|
|
egid = getegid();
|
|
|
|
if (setegid(getgid()) == -1)
|
|
|
|
err(1, "failed to reset privileges");
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
check_fd(STDIN_FILENO);
|
|
|
|
check_fd(STDOUT_FILENO);
|
|
|
|
check_fd(STDERR_FILENO);
|
|
|
|
|
|
|
|
pid = -1;
|
2003-04-04 07:49:20 +04:00
|
|
|
which = verbose = debug = 0;
|
2003-01-08 23:25:12 +03:00
|
|
|
print_all = print_map = print_maps = print_solaris = print_ddb = 0;
|
|
|
|
recurse = 0;
|
|
|
|
kmem = kernel = NULL;
|
2003-04-04 07:49:20 +04:00
|
|
|
address = 0;
|
|
|
|
vmspace = &kbit;
|
2003-01-08 23:25:12 +03:00
|
|
|
|
2003-04-04 07:49:20 +04:00
|
|
|
while ((ch = getopt(argc, argv, "A:aD:dE:lM:mN:Pp:RrS:sV:vx")) != -1) {
|
2003-01-08 23:25:12 +03:00
|
|
|
switch (ch) {
|
2003-04-04 07:49:20 +04:00
|
|
|
case 'A':
|
|
|
|
case 'E':
|
|
|
|
case 'S':
|
|
|
|
case 'V':
|
|
|
|
if (which != 0)
|
|
|
|
errx(1, "use only one of -A, -E, -S, or -V");
|
|
|
|
errno = 0;
|
|
|
|
address = strtoul(optarg, &t, 0);
|
|
|
|
if (*t != '\0')
|
|
|
|
errx(1, "%s is not a valid address", optarg);
|
|
|
|
if (errno != 0)
|
|
|
|
err(1, "%s is not a valid address", optarg);
|
|
|
|
switch (ch) {
|
|
|
|
case 'A': which = AMAP_ADDRESS; break;
|
|
|
|
case 'E': which = VM_MAP_ENTRY_ADDRESS; break;
|
|
|
|
case 'S': which = VMSPACE_ADDRESS; break;
|
|
|
|
case 'V': which = VM_MAP_ADDRESS; break;
|
|
|
|
}
|
|
|
|
break;
|
2003-01-08 23:25:12 +03:00
|
|
|
case 'a':
|
|
|
|
print_all = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
print_ddb = 1;
|
|
|
|
break;
|
|
|
|
case 'D':
|
2003-04-04 07:49:20 +04:00
|
|
|
errno = 0;
|
|
|
|
debug = strtoul(optarg, &t, 0);
|
|
|
|
if (*t != '\0')
|
|
|
|
errx(1, "%s is not a valid number", optarg);
|
|
|
|
if (errno != 0)
|
|
|
|
err(1, "%s is not a valid number", optarg);
|
2003-01-08 23:25:12 +03:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
print_maps = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
print_map = 1;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
kmem = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
kernel = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
2003-04-04 07:49:20 +04:00
|
|
|
errno = 0;
|
|
|
|
pid = strtol(optarg, &t, 0);
|
|
|
|
if (pid < 0)
|
|
|
|
errno = EINVAL;
|
|
|
|
if (*t != '\0')
|
|
|
|
errx(1, "%s is not a valid pid", optarg);
|
|
|
|
if (errno != 0)
|
|
|
|
err(1, "%s is not a valid pid", optarg);
|
2003-01-08 23:25:12 +03:00
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
pid = getpid();
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
recurse = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
print_solaris = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
case 'x':
|
|
|
|
errx(1, "-%c option not implemented, sorry", optopt);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
case '?':
|
|
|
|
default:
|
2003-04-04 07:49:20 +04:00
|
|
|
fprintf(stderr, "usage: %s [-adlmPRsv] [-A address] "
|
|
|
|
"[-D number] [-E address] [-M core]\n"
|
2003-04-16 16:15:07 +04:00
|
|
|
"\t[-N system] [-p pid] [-S address] "
|
|
|
|
"[-V address] [pid ...]\n",
|
2003-01-08 23:25:12 +03:00
|
|
|
getprogname());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* more than one "process" to dump? */
|
|
|
|
many = (argc > 1 - (pid == -1 ? 0 : 1)) ? 1 : 0;
|
|
|
|
|
|
|
|
/* apply default */
|
|
|
|
if (print_all + print_map + print_maps + print_solaris +
|
|
|
|
print_ddb == 0)
|
|
|
|
print_solaris = 1;
|
|
|
|
|
2003-01-09 16:05:12 +03:00
|
|
|
/* get privs back if it appears to be safe, otherwise toss them */
|
2003-04-04 07:49:20 +04:00
|
|
|
if (kernel == NULL && kmem == NULL && address == 0)
|
2003-01-09 16:05:12 +03:00
|
|
|
rc = setegid(egid);
|
|
|
|
else
|
|
|
|
rc = setgid(getgid());
|
|
|
|
if (rc == -1)
|
|
|
|
err(1, "failed to reset privileges");
|
|
|
|
|
2003-01-08 23:25:12 +03:00
|
|
|
/* start by opening libkvm */
|
|
|
|
kd = kvm_openfiles(kernel, kmem, NULL, O_RDONLY, errbuf);
|
2003-05-04 19:09:45 +04:00
|
|
|
|
|
|
|
/* we're completely done with privileges now */
|
|
|
|
rc = setgid(getgid());
|
|
|
|
if (rc == -1)
|
|
|
|
err(1, "failed to reset privileges");
|
|
|
|
|
|
|
|
/* print the kvm_open error, if any */
|
2003-01-08 23:25:12 +03:00
|
|
|
errbuf[_POSIX2_LINE_MAX] = '\0';
|
|
|
|
if (kd == NULL)
|
|
|
|
errx(1, "%s", errbuf);
|
|
|
|
|
|
|
|
/* get "bootstrap" addresses from kernel */
|
|
|
|
load_symbols(kd);
|
|
|
|
|
2003-04-04 07:49:20 +04:00
|
|
|
if (address) {
|
|
|
|
struct kbit kbit2, *at = &kbit2;
|
|
|
|
|
|
|
|
memset(vmspace, 0, sizeof(*vmspace));
|
|
|
|
A(at) = address;
|
2004-01-31 23:53:55 +03:00
|
|
|
S(at) = (size_t)-1;
|
2003-04-04 07:49:20 +04:00
|
|
|
|
|
|
|
switch (which) {
|
|
|
|
case VMSPACE_ADDRESS:
|
|
|
|
/* (kd, kproc, vmspace, thing) */
|
|
|
|
(*process_map)(kd, NULL, at, "vm_map");
|
|
|
|
break;
|
|
|
|
case VM_MAP_ADDRESS:
|
|
|
|
/* (kd, proc, vmspace, vm_map, thing) */
|
|
|
|
(*dump_vm_map)(kd, NULL, vmspace, at, "vm_map");
|
|
|
|
break;
|
|
|
|
case VM_MAP_ENTRY_ADDRESS:
|
|
|
|
/* (kd, proc, vmspace, vm_map_entry, 0) */
|
|
|
|
(*dump_vm_map_entry)(kd, NULL, vmspace, at, 0);
|
|
|
|
break;
|
|
|
|
case AMAP_ADDRESS:
|
|
|
|
/* (kd, amap) */
|
|
|
|
(*dump_amap)(kd, at);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (pid == -1) {
|
|
|
|
if (argc == 0)
|
|
|
|
pid = getppid();
|
|
|
|
else {
|
2003-04-04 07:49:20 +04:00
|
|
|
errno = 0;
|
|
|
|
pid = strtol(argv[0], &t, 0);
|
|
|
|
if (pid < 0)
|
|
|
|
errno = EINVAL;
|
|
|
|
if (*t != '\0')
|
|
|
|
errx(1, "%s is not a valid pid",
|
|
|
|
argv[0]);
|
|
|
|
if (errno != 0)
|
|
|
|
err(1, "%s is not a valid pid",
|
|
|
|
argv[0]);
|
2003-01-08 23:25:12 +03:00
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 02:13:22 +04:00
|
|
|
uid = getuid();
|
2011-10-19 00:54:56 +04:00
|
|
|
|
2011-10-26 02:13:22 +04:00
|
|
|
errno = 0;
|
2003-01-08 23:25:12 +03:00
|
|
|
/* find the process id */
|
2011-10-26 02:13:22 +04:00
|
|
|
if (pid == 0) {
|
2003-01-08 23:25:12 +03:00
|
|
|
kproc = NULL;
|
2011-10-26 02:13:22 +04:00
|
|
|
if (uid != 0) {
|
|
|
|
/* only root can print kernel mappings */
|
|
|
|
errno = EPERM;
|
|
|
|
}
|
|
|
|
} else {
|
2003-01-08 23:25:12 +03:00
|
|
|
kproc = kvm_getproc2(kd, KERN_PROC_PID, pid,
|
2011-10-26 02:13:22 +04:00
|
|
|
sizeof(struct kinfo_proc2), &rc);
|
2003-01-08 23:25:12 +03:00
|
|
|
if (kproc == NULL || rc == 0) {
|
|
|
|
errno = ESRCH;
|
2011-10-26 02:13:22 +04:00
|
|
|
} else if (uid != 0 && uid != kproc->p_uid) {
|
|
|
|
/*
|
|
|
|
* only the real owner of the process and
|
|
|
|
* root can print process mappings
|
|
|
|
*/
|
|
|
|
errno = EPERM;
|
2003-01-08 23:25:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 02:13:22 +04:00
|
|
|
if (errno != 0) {
|
|
|
|
warn("%d", pid);
|
|
|
|
pid = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-01-08 23:25:12 +03:00
|
|
|
/* dump it */
|
|
|
|
if (many) {
|
|
|
|
if (kproc)
|
|
|
|
printf("process %d:\n", kproc->p_pid);
|
|
|
|
else
|
|
|
|
printf("kernel:\n");
|
|
|
|
}
|
|
|
|
|
2003-04-04 07:49:20 +04:00
|
|
|
(*process_map)(kd, kproc, vmspace, NULL);
|
2003-01-08 23:25:12 +03:00
|
|
|
pid = -1;
|
|
|
|
} while (argc > 0);
|
|
|
|
|
|
|
|
/* done. go away. */
|
|
|
|
rc = kvm_close(kd);
|
|
|
|
if (rc == -1)
|
|
|
|
err(1, "kvm_close");
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
check_fd(int fd)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
(void)close(fd);
|
|
|
|
n = open("/dev/null", O_RDWR);
|
|
|
|
if (n == fd || n == -1)
|
|
|
|
/* we're either done or we can do no more */
|
|
|
|
return;
|
|
|
|
/* if either of these fail, there's not much we can do */
|
|
|
|
(void)dup2(n, fd);
|
|
|
|
(void)close(n);
|
|
|
|
/* XXX should we exit if it fails? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
load_symbols(kvm_t *kd)
|
|
|
|
{
|
2003-03-29 02:10:32 +03:00
|
|
|
int rc, i, mib[2];
|
2003-03-29 21:01:21 +03:00
|
|
|
size_t sz;
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
rc = kvm_nlist(kd, &ksyms[0]);
|
|
|
|
if (rc != 0) {
|
|
|
|
for (i = 0; ksyms[i].n_name != NULL; i++)
|
|
|
|
if (ksyms[i].n_value == 0)
|
2003-01-16 18:34:18 +03:00
|
|
|
warnx("symbol %s: not found", ksyms[i].n_name);
|
2003-01-08 23:25:12 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uvm_vnodeops = (void*)ksyms[NL_UVM_VNODEOPS].n_value;
|
|
|
|
uvm_deviceops = (void*)ksyms[NL_UVM_DEVICEOPS].n_value;
|
|
|
|
aobj_pager = (void*)ksyms[NL_AOBJ_PAGER].n_value;
|
|
|
|
ubc_pager = (void*)ksyms[NL_UBC_PAGER].n_value;
|
|
|
|
|
|
|
|
nchash_addr = ksyms[NL_NCHASH].n_value;
|
|
|
|
|
|
|
|
_KDEREF(kd, ksyms[NL_MAXSSIZ].n_value, &maxssiz,
|
|
|
|
sizeof(maxssiz));
|
|
|
|
_KDEREF(kd, ksyms[NL_NCHASHTBL].n_value, &nchashtbl_addr,
|
|
|
|
sizeof(nchashtbl_addr));
|
|
|
|
_KDEREF(kd, ksyms[NL_KERNEL_MAP].n_value, &kernel_map_addr,
|
|
|
|
sizeof(kernel_map_addr));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some of these may be missing from some platforms, for
|
|
|
|
* example sparc, sh3, and most powerpc platforms don't
|
2003-02-27 07:10:36 +03:00
|
|
|
* have a "phys_map", etc.
|
2003-01-08 23:25:12 +03:00
|
|
|
*/
|
|
|
|
(void)kvm_nlist(kd, &kmaps[0]);
|
2003-02-27 07:10:36 +03:00
|
|
|
|
2004-01-06 17:01:19 +03:00
|
|
|
#define get_map_address(m) do {\
|
2008-01-02 20:23:31 +03:00
|
|
|
if (kmaps[__CONCAT(NL_,m)].n_value != 0) \
|
|
|
|
_KDEREF(kd, kmaps[__CONCAT(NL_,m)].n_value, &m, sizeof(m)); \
|
2004-01-31 23:53:55 +03:00
|
|
|
} while (0/*CONSTCOND*/)
|
2003-02-27 07:10:36 +03:00
|
|
|
|
|
|
|
get_map_address(kmem_map);
|
|
|
|
get_map_address(mb_map);
|
|
|
|
get_map_address(phys_map);
|
|
|
|
get_map_address(exec_map);
|
|
|
|
get_map_address(pager_map);
|
|
|
|
get_map_address(st_map);
|
|
|
|
get_map_address(pt_map);
|
|
|
|
get_map_address(lkm_map);
|
2004-01-06 17:01:19 +03:00
|
|
|
get_map_address(buf_map);
|
2003-03-29 02:10:32 +03:00
|
|
|
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_PAGESIZE;
|
2003-03-29 21:01:21 +03:00
|
|
|
sz = sizeof(page_size);
|
|
|
|
if (sysctl(&mib[0], 2, &page_size, &sz, NULL, 0) == -1)
|
2003-03-29 02:10:32 +03:00
|
|
|
err(1, "sysctl: hw.pagesize");
|
2003-02-27 07:10:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
mapname(void *addr)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (addr == (void*)kernel_map_addr)
|
|
|
|
return ("kernel_map");
|
|
|
|
else if (addr == kmem_map)
|
|
|
|
return ("kmem_map");
|
|
|
|
else if (addr == mb_map)
|
|
|
|
return ("mb_map");
|
|
|
|
else if (addr == phys_map)
|
|
|
|
return ("phys_map");
|
|
|
|
else if (addr == exec_map)
|
|
|
|
return ("exec_map");
|
|
|
|
else if (addr == pager_map)
|
|
|
|
return ("pager_map");
|
|
|
|
else if (addr == st_map)
|
|
|
|
return ("st_map");
|
|
|
|
else if (addr == pt_map)
|
|
|
|
return ("pt_map");
|
|
|
|
else if (addr == lkm_map)
|
|
|
|
return ("lkm_map");
|
2004-01-06 17:01:19 +03:00
|
|
|
else if (addr == buf_map)
|
|
|
|
return ("buf_map");
|
2003-02-27 07:10:36 +03:00
|
|
|
else
|
|
|
|
return (NULL);
|
2003-01-08 23:25:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
load_name_cache(kvm_t *kd)
|
|
|
|
{
|
|
|
|
struct namecache _ncp, *ncp, *oncp;
|
|
|
|
struct nchashhead _ncpp, *ncpp;
|
2009-04-13 04:27:38 +04:00
|
|
|
u_long nchash, i;
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
LIST_INIT(&lcache);
|
|
|
|
|
|
|
|
_KDEREF(kd, nchash_addr, &nchash, sizeof(nchash));
|
2003-07-24 04:43:50 +04:00
|
|
|
nchashtbl = malloc(sizeof(nchashtbl) * (int)(nchash + 1));
|
2003-01-08 23:25:12 +03:00
|
|
|
_KDEREF(kd, nchashtbl_addr, nchashtbl,
|
2003-07-24 04:43:50 +04:00
|
|
|
sizeof(nchashtbl) * (int)(nchash + 1));
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
ncpp = &_ncpp;
|
|
|
|
|
|
|
|
for (i = 0; i <= nchash; i++) {
|
|
|
|
ncpp = &nchashtbl[i];
|
|
|
|
oncp = NULL;
|
|
|
|
LIST_FOREACH(ncp, ncpp, nc_hash) {
|
|
|
|
if (ncp == oncp ||
|
|
|
|
ncp == (void*)0xdeadbeef)
|
|
|
|
break;
|
|
|
|
oncp = ncp;
|
|
|
|
_KDEREF(kd, (u_long)ncp, &_ncp, sizeof(*ncp));
|
|
|
|
ncp = &_ncp;
|
2008-06-17 19:54:45 +04:00
|
|
|
if (ncp->nc_nlen > 0) {
|
2003-01-08 23:25:12 +03:00
|
|
|
if (ncp->nc_nlen > 2 ||
|
|
|
|
ncp->nc_name[0] != '.' ||
|
|
|
|
(ncp->nc_name[1] != '.' &&
|
|
|
|
ncp->nc_nlen != 1))
|
|
|
|
cache_enter(i, ncp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-13 04:27:38 +04:00
|
|
|
cache_enter(u_long i, struct namecache *ncp)
|
2003-01-08 23:25:12 +03:00
|
|
|
{
|
|
|
|
struct cache_entry *ce;
|
|
|
|
|
|
|
|
if (debug & DUMP_NAMEI_CACHE)
|
2009-04-13 04:27:38 +04:00
|
|
|
printf("[%lu] ncp->nc_vp %10p, ncp->nc_dvp %10p, "
|
2003-07-31 19:43:06 +04:00
|
|
|
"ncp->nc_nlen %3d [%.*s]\n",
|
2003-01-08 23:25:12 +03:00
|
|
|
i, ncp->nc_vp, ncp->nc_dvp,
|
2003-07-31 19:43:06 +04:00
|
|
|
ncp->nc_nlen, ncp->nc_nlen, ncp->nc_name);
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
ce = malloc(sizeof(struct cache_entry));
|
|
|
|
|
|
|
|
ce->ce_vp = ncp->nc_vp;
|
|
|
|
ce->ce_pvp = ncp->nc_dvp;
|
|
|
|
ce->ce_nlen = ncp->nc_nlen;
|
|
|
|
strncpy(ce->ce_name, ncp->nc_name, sizeof(ce->ce_name));
|
2009-04-13 04:27:38 +04:00
|
|
|
ce->ce_name[MIN(ce->ce_nlen, (int)(sizeof(ce->ce_name) - 1))] = '\0';
|
2003-01-08 23:25:12 +03:00
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&lcache, ce, ce_next);
|
|
|
|
}
|