summaryrefslogtreecommitdiffstats
path: root/lib/libkvm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libkvm')
-rw-r--r--lib/libkvm/Makefile20
-rw-r--r--lib/libkvm/kvm.3104
-rw-r--r--lib/libkvm/kvm.c543
-rw-r--r--lib/libkvm/kvm.h96
-rw-r--r--lib/libkvm/kvm_alpha.c212
-rw-r--r--lib/libkvm/kvm_amd64.c194
-rw-r--r--lib/libkvm/kvm_file.c188
-rw-r--r--lib/libkvm/kvm_geterr.381
-rw-r--r--lib/libkvm/kvm_getfiles.386
-rw-r--r--lib/libkvm/kvm_getloadavg.364
-rw-r--r--lib/libkvm/kvm_getloadavg.c106
-rw-r--r--lib/libkvm/kvm_getprocs.3164
-rw-r--r--lib/libkvm/kvm_getswapinfo.362
-rw-r--r--lib/libkvm/kvm_getswapinfo.c420
-rw-r--r--lib/libkvm/kvm_i386.c194
-rw-r--r--lib/libkvm/kvm_nlist.389
-rw-r--r--lib/libkvm/kvm_open.3186
-rw-r--r--lib/libkvm/kvm_private.h82
-rw-r--r--lib/libkvm/kvm_proc.c784
-rw-r--r--lib/libkvm/kvm_read.393
-rw-r--r--lib/libkvm/kvm_sparc.c236
21 files changed, 4004 insertions, 0 deletions
diff --git a/lib/libkvm/Makefile b/lib/libkvm/Makefile
new file mode 100644
index 0000000..75dcc7f
--- /dev/null
+++ b/lib/libkvm/Makefile
@@ -0,0 +1,20 @@
+# @(#)Makefile 8.1 (Berkeley) 6/4/93
+# $FreeBSD$
+
+LIB= kvm
+CFLAGS+=-DLIBC_SCCS -I${.CURDIR}/../../sys
+SRCS= kvm.c kvm_${MACHINE_ARCH}.c kvm_file.c kvm_getloadavg.c \
+ kvm_getswapinfo.c kvm_proc.c
+
+MAN3= kvm.3 kvm_geterr.3 kvm_getfiles.3 kvm_getloadavg.3 kvm_getprocs.3 \
+ kvm_getswapinfo.3 kvm_nlist.3 kvm_open.3 kvm_read.3
+
+MLINKS+=kvm_getprocs.3 kvm_getargv.3 kvm_getprocs.3 kvm_getenvv.3
+MLINKS+=kvm_open.3 kvm_close.3 kvm_open.3 kvm_openfiles.3
+MLINKS+=kvm_read.3 kvm_write.3
+
+beforeinstall:
+ ${INSTALL} -C -o ${BINOWN} -g ${BINGRP} -m 444 ${.CURDIR}/kvm.h \
+ ${DESTDIR}/usr/include
+
+.include <bsd.lib.mk>
diff --git a/lib/libkvm/kvm.3 b/lib/libkvm/kvm.3
new file mode 100644
index 0000000..7647ca6
--- /dev/null
+++ b/lib/libkvm/kvm.3
@@ -0,0 +1,104 @@
+.\" Copyright (c) 1992, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software developed by the Computer Systems
+.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+.\" BG 91-66 and contributed to Berkeley.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+.\"
+.\" @(#)kvm.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
+.\"
+.Dd June 4, 1993
+.Dt KVM 3
+.Os
+.Sh NAME
+.Nm kvm
+.Nd kernel memory interface
+.Sh DESCRIPTION
+The
+.Xr kvm 3
+library provides a uniform interface for accessing kernel virtual memory
+images, including live systems and crash dumps.
+Access to live systems is via
+/dev/mem
+while crash dumps can be examined via the core file generated by
+.Xr savecore 8 .
+The interface behaves identically in both cases.
+Memory can be read and written, kernel symbol addresses can be
+looked up efficiently, and information about user processes can
+be gathered.
+.Pp
+.Fn kvm_open
+is first called to obtain a descriptor for all subsequent calls.
+.Sh COMPATIBILITY
+The kvm interface was first introduced in SunOS. A considerable
+number of programs have been developed that use this interface,
+making backward compatibility highly desirable.
+In most respects, the Sun kvm interface is consistent and clean.
+Accordingly, the generic portion of the interface (i.e.,
+.Fn kvm_open ,
+.Fn kvm_close ,
+.Fn kvm_read ,
+.Fn kvm_write ,
+and
+.Fn kvm_nlist )
+has been incorporated into the BSD interface. Indeed, many kvm
+applications (i.e., debuggers and statistical monitors) use only
+this subset of the interface.
+.Pp
+The process interface was not kept. This is not a portability
+issue since any code that manipulates processes is inherently
+machine dependent.
+.Pp
+Finally, the Sun kvm error reporting semantics are poorly defined.
+The library can be configured either to print errors to stderr automatically,
+or to print no error messages at all.
+In the latter case, the nature of the error cannot be determined.
+To overcome this, the BSD interface includes a
+routine,
+.Xr kvm_geterr 3 ,
+to return (not print out) the error message
+corresponding to the most recent error condition on the
+given descriptor.
+.Sh SEE ALSO
+.Xr kvm_close 3 ,
+.Xr kvm_getargv 3 ,
+.Xr kvm_getenvv 3 ,
+.Xr kvm_geterr 3 ,
+.Xr kvm_getfiles 3 ,
+.Xr kvm_getloadavg 3 ,
+.Xr kvm_getprocs 3 ,
+.Xr kvm_getswapinfo 3 ,
+.Xr kvm_nlist 3 ,
+.Xr kvm_open 3 ,
+.Xr kvm_openfiles 3 ,
+.Xr kvm_read 3 ,
+.Xr kvm_write 3
diff --git a/lib/libkvm/kvm.c b/lib/libkvm/kvm.c
new file mode 100644
index 0000000..b35eceb
--- /dev/null
+++ b/lib/libkvm/kvm.c
@@ -0,0 +1,543 @@
+/*-
+ * Copyright (c) 1989, 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software developed by the Computer Systems
+ * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+ * BG 91-66 and contributed to Berkeley.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+#if 0
+static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
+#else
+static const char rcsid[] =
+ "$FreeBSD$";
+#endif
+#endif /* LIBC_SCCS and not lint */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/proc.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+#include <vm/swap_pager.h>
+
+#include <machine/vmparam.h>
+
+#include <ctype.h>
+#include <db.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <limits.h>
+#include <nlist.h>
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "kvm_private.h"
+
+static int kvm_dbopen __P((kvm_t *, const char *));
+
+char *
+kvm_geterr(kd)
+ kvm_t *kd;
+{
+ return (kd->errbuf);
+}
+
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+/*
+ * Report an error using printf style arguments. "program" is kd->program
+ * on hard errors, and 0 on soft errors, so that under sun error emulation,
+ * only hard errors are printed out (otherwise, programs like gdb will
+ * generate tons of error messages when trying to access bogus pointers).
+ */
+void
+#if __STDC__
+_kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
+#else
+_kvm_err(kd, program, fmt, va_alist)
+ kvm_t *kd;
+ char *program, *fmt;
+ va_dcl
+#endif
+{
+ va_list ap;
+
+#ifdef __STDC__
+ va_start(ap, fmt);
+#else
+ va_start(ap);
+#endif
+ if (program != NULL) {
+ (void)fprintf(stderr, "%s: ", program);
+ (void)vfprintf(stderr, fmt, ap);
+ (void)fputc('\n', stderr);
+ } else
+ (void)vsnprintf(kd->errbuf,
+ sizeof(kd->errbuf), (char *)fmt, ap);
+
+ va_end(ap);
+}
+
+void
+#if __STDC__
+_kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
+#else
+_kvm_syserr(kd, program, fmt, va_alist)
+ kvm_t *kd;
+ char *program, *fmt;
+ va_dcl
+#endif
+{
+ va_list ap;
+ register int n;
+
+#if __STDC__
+ va_start(ap, fmt);
+#else
+ va_start(ap);
+#endif
+ if (program != NULL) {
+ (void)fprintf(stderr, "%s: ", program);
+ (void)vfprintf(stderr, fmt, ap);
+ (void)fprintf(stderr, ": %s\n", strerror(errno));
+ } else {
+ register char *cp = kd->errbuf;
+
+ (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
+ n = strlen(cp);
+ (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
+ strerror(errno));
+ }
+ va_end(ap);
+}
+
+void *
+_kvm_malloc(kd, n)
+ register kvm_t *kd;
+ register size_t n;
+{
+ void *p;
+
+ if ((p = calloc(n, sizeof(char))) == NULL)
+ _kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
+ n, strerror(errno));
+ return (p);
+}
+
+static kvm_t *
+_kvm_open(kd, uf, mf, flag, errout)
+ register kvm_t *kd;
+ const char *uf;
+ const char *mf;
+ int flag;
+ char *errout;
+{
+ struct stat st;
+
+ kd->vmfd = -1;
+ kd->pmfd = -1;
+ kd->nlfd = -1;
+ kd->vmst = 0;
+ kd->db = 0;
+ kd->procbase = 0;
+ kd->argspc = 0;
+ kd->argv = 0;
+
+ if (uf == 0)
+ uf = getbootfile();
+ else if (strlen(uf) >= MAXPATHLEN) {
+ _kvm_err(kd, kd->program, "exec file name too long");
+ goto failed;
+ }
+ if (flag & ~O_RDWR) {
+ _kvm_err(kd, kd->program, "bad flags arg");
+ goto failed;
+ }
+ if (mf == 0)
+ mf = _PATH_MEM;
+
+ if ((kd->pmfd = open(mf, flag, 0)) < 0) {
+ _kvm_syserr(kd, kd->program, "%s", mf);
+ goto failed;
+ }
+ if (fstat(kd->pmfd, &st) < 0) {
+ _kvm_syserr(kd, kd->program, "%s", mf);
+ goto failed;
+ }
+ if (S_ISCHR(st.st_mode)) {
+ /*
+ * If this is a character special device, then check that
+ * it's /dev/mem. If so, open kmem too. (Maybe we should
+ * make it work for either /dev/mem or /dev/kmem -- in either
+ * case you're working with a live kernel.)
+ */
+ if (strcmp(mf, _PATH_DEVNULL) == 0) {
+ kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
+ } else if (strcmp(mf, _PATH_MEM) != 0) {
+ _kvm_err(kd, kd->program,
+ "%s: not physical memory device", mf);
+ goto failed;
+ } else {
+ if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
+ _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
+ goto failed;
+ }
+ }
+ /*
+ * Open kvm nlist database. We go ahead and do this
+ * here so that we don't have to hold on to the kernel
+ * path name. Since a kvm application will surely do
+ * a kvm_nlist(), this probably won't be a wasted effort.
+ * If the database cannot be opened, open the namelist
+ * argument so we revert to slow nlist() calls.
+ */
+ if (kvm_dbopen(kd, uf) < 0 &&
+ (kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
+ _kvm_syserr(kd, kd->program, "%s", uf);
+ goto failed;
+ }
+ } else {
+ /*
+ * This is a crash dump.
+ * Initialize the virtual address translation machinery,
+ * but first setup the namelist fd.
+ */
+ if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
+ _kvm_syserr(kd, kd->program, "%s", uf);
+ goto failed;
+ }
+ if (_kvm_initvtop(kd) < 0)
+ goto failed;
+ }
+ return (kd);
+failed:
+ /*
+ * Copy out the error if doing sane error semantics.
+ */
+ if (errout != 0)
+ strcpy(errout, kd->errbuf);
+ (void)kvm_close(kd);
+ return (0);
+}
+
+kvm_t *
+kvm_openfiles(uf, mf, sf, flag, errout)
+ const char *uf;
+ const char *mf;
+ const char *sf;
+ int flag;
+ char *errout;
+{
+ register kvm_t *kd;
+
+ if ((kd = malloc(sizeof(*kd))) == NULL) {
+ (void)strcpy(errout, strerror(errno));
+ return (0);
+ }
+ memset(kd, 0, sizeof(*kd));
+ kd->program = 0;
+ return (_kvm_open(kd, uf, mf, flag, errout));
+}
+
+kvm_t *
+kvm_open(uf, mf, sf, flag, errstr)
+ const char *uf;
+ const char *mf;
+ const char *sf;
+ int flag;
+ const char *errstr;
+{
+ register kvm_t *kd;
+
+ if ((kd = malloc(sizeof(*kd))) == NULL) {
+ if (errstr != NULL)
+ (void)fprintf(stderr, "%s: %s\n",
+ errstr, strerror(errno));
+ return (0);
+ }
+ memset(kd, 0, sizeof(*kd));
+ kd->program = errstr;
+ return (_kvm_open(kd, uf, mf, flag, NULL));
+}
+
+int
+kvm_close(kd)
+ kvm_t *kd;
+{
+ register int error = 0;
+
+ if (kd->pmfd >= 0)
+ error |= close(kd->pmfd);
+ if (kd->vmfd >= 0)
+ error |= close(kd->vmfd);
+ if (kd->nlfd >= 0)
+ error |= close(kd->nlfd);
+ if (kd->db != 0)
+ error |= (kd->db->close)(kd->db);
+ if (kd->vmst)
+ _kvm_freevtop(kd);
+ if (kd->procbase != 0)
+ free((void *)kd->procbase);
+ if (kd->argv != 0)
+ free((void *)kd->argv);
+ free((void *)kd);
+
+ return (0);
+}
+
+/*
+ * Set up state necessary to do queries on the kernel namelist
+ * data base. If the data base is out-of-data/incompatible with
+ * given executable, set up things so we revert to standard nlist call.
+ * Only called for live kernels. Return 0 on success, -1 on failure.
+ */
+static int
+kvm_dbopen(kd, uf)
+ kvm_t *kd;
+ const char *uf;
+{
+ char *cp;
+ DBT rec;
+ int dbversionlen;
+ struct nlist nitem;
+ char dbversion[_POSIX2_LINE_MAX];
+ char kversion[_POSIX2_LINE_MAX];
+ char dbname[MAXPATHLEN];
+
+ if ((cp = rindex(uf, '/')) != 0)
+ uf = cp + 1;
+
+ (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", _PATH_VARDB, uf);
+ kd->db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL);
+ if (kd->db == 0)
+ return (-1);
+ /*
+ * read version out of database
+ */
+ rec.data = VRS_KEY;
+ rec.size = sizeof(VRS_KEY) - 1;
+ if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
+ goto close;
+ if (rec.data == 0 || rec.size > sizeof(dbversion))
+ goto close;
+
+ bcopy(rec.data, dbversion, rec.size);
+ dbversionlen = rec.size;
+ /*
+ * Read version string from kernel memory.
+ * Since we are dealing with a live kernel, we can call kvm_read()
+ * at this point.
+ */
+ rec.data = VRS_SYM;
+ rec.size = sizeof(VRS_SYM) - 1;
+ if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
+ goto close;
+ if (rec.data == 0 || rec.size != sizeof(struct nlist))
+ goto close;
+ bcopy((char *)rec.data, (char *)&nitem, sizeof(nitem));
+ if (kvm_read(kd, (u_long)nitem.n_value, kversion, dbversionlen) !=
+ dbversionlen)
+ goto close;
+ /*
+ * If they match, we win - otherwise clear out kd->db so
+ * we revert to slow nlist().
+ */
+ if (bcmp(dbversion, kversion, dbversionlen) == 0)
+ return (0);
+close:
+ (void)(kd->db->close)(kd->db);
+ kd->db = 0;
+
+ return (-1);
+}
+
+int
+kvm_nlist(kd, nl)
+ kvm_t *kd;
+ struct nlist *nl;
+{
+ register struct nlist *p;
+ register int nvalid;
+
+ /*
+ * If we can't use the data base, revert to the
+ * slow library call.
+ */
+ if (kd->db == 0)
+ return (__fdnlist(kd->nlfd, nl));
+
+ /*
+ * We can use the kvm data base. Go through each nlist entry
+ * and look it up with a db query.
+ */
+ nvalid = 0;
+ for (p = nl; p->n_name && p->n_name[0]; ++p) {
+ register int len;
+ DBT rec;
+
+ if ((len = strlen(p->n_name)) > 4096) {
+ /* sanity */
+ _kvm_err(kd, kd->program, "symbol too large");
+ return (-1);
+ }
+ rec.data = p->n_name;
+ rec.size = len;
+ if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
+ continue;
+ if (rec.data == 0 || rec.size != sizeof(struct nlist))
+ continue;
+ ++nvalid;
+ /*
+ * Avoid alignment issues.
+ */
+ bcopy((char *)&((struct nlist *)rec.data)->n_type,
+ (char *)&p->n_type,
+ sizeof(p->n_type));
+ bcopy((char *)&((struct nlist *)rec.data)->n_value,
+ (char *)&p->n_value,
+ sizeof(p->n_value));
+ }
+ /*
+ * Return the number of entries that weren't found.
+ */
+ return ((p - nl) - nvalid);
+}
+
+ssize_t
+kvm_read(kd, kva, buf, len)
+ kvm_t *kd;
+ register u_long kva;
+ register void *buf;
+ register size_t len;
+{
+ register int cc;
+ register void *cp;
+
+ if (ISALIVE(kd)) {
+ /*
+ * We're using /dev/kmem. Just read straight from the
+ * device and let the active kernel do the address translation.
+ */
+ errno = 0;
+ if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
+ _kvm_err(kd, 0, "invalid address (%x)", kva);
+ return (0);
+ }
+ cc = read(kd->vmfd, buf, len);
+ if (cc < 0) {
+ _kvm_syserr(kd, 0, "kvm_read");
+ return (0);
+ } else if (cc < len)
+ _kvm_err(kd, kd->program, "short read");
+ return (cc);
+ } else {
+ cp = buf;
+ while (len > 0) {
+ u_long pa;
+
+ cc = _kvm_kvatop(kd, kva, &pa);
+ if (cc == 0)
+ return (0);
+ if (cc > len)
+ cc = len;
+ errno = 0;
+ if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
+ _kvm_syserr(kd, 0, _PATH_MEM);
+ break;
+ }
+ cc = read(kd->pmfd, cp, cc);
+ if (cc < 0) {
+ _kvm_syserr(kd, kd->program, "kvm_read");
+ break;
+ }
+ /*
+ * If kvm_kvatop returns a bogus value or our core
+ * file is truncated, we might wind up seeking beyond
+ * the end of the core file in which case the read will
+ * return 0 (EOF).
+ */
+ if (cc == 0)
+ break;
+ (char *)cp += cc;
+ kva += cc;
+ len -= cc;
+ }
+ return ((char *)cp - (char *)buf);
+ }
+ /* NOTREACHED */
+}
+
+ssize_t
+kvm_write(kd, kva, buf, len)
+ kvm_t *kd;
+ register u_long kva;
+ register const void *buf;
+ register size_t len;
+{
+ register int cc;
+
+ if (ISALIVE(kd)) {
+ /*
+ * Just like kvm_read, only we write.
+ */
+ errno = 0;
+ if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
+ _kvm_err(kd, 0, "invalid address (%x)", kva);
+ return (0);
+ }
+ cc = write(kd->vmfd, buf, len);
+ if (cc < 0) {
+ _kvm_syserr(kd, 0, "kvm_write");
+ return (0);
+ } else if (cc < len)
+ _kvm_err(kd, kd->program, "short write");
+ return (cc);
+ } else {
+ _kvm_err(kd, kd->program,
+ "kvm_write not implemented for dead kernels");
+ return (0);
+ }
+ /* NOTREACHED */
+}
diff --git a/lib/libkvm/kvm.h b/lib/libkvm/kvm.h
new file mode 100644
index 0000000..b4978be
--- /dev/null
+++ b/lib/libkvm/kvm.h
@@ -0,0 +1,96 @@
+/*-
+ * Copyright (c) 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ *
+ * @(#)kvm.h 8.1 (Berkeley) 6/2/93
+ * $FreeBSD$
+ */
+
+#ifndef _KVM_H_
+#define _KVM_H_
+
+#include <sys/cdefs.h>
+#include <machine/ansi.h>
+#include <nlist.h>
+
+/* Default version symbol. */
+#define VRS_SYM "_version"
+#define VRS_KEY "VERSION"
+
+#ifdef _BSD_SIZE_T_
+typedef _BSD_SIZE_T_ size_t;
+#undef _BSD_SIZE_T_
+#endif
+
+#ifdef _BSD_SSIZE_T_
+typedef _BSD_SSIZE_T_ ssize_t;
+#undef _BSD_SSIZE_T_
+#endif
+
+typedef struct __kvm kvm_t;
+
+struct kinfo_proc;
+struct proc;
+
+struct kvm_swap {
+ char ksw_devname[32];
+ int ksw_used;
+ int ksw_total;
+ int ksw_flags;
+ int ksw_reserved1;
+ int ksw_reserved2;
+};
+
+#define SWIF_DUMP_TREE 0x0001
+#define SWIF_DEV_PREFIX 0x0002
+
+__BEGIN_DECLS
+int kvm_close __P((kvm_t *));
+char **kvm_getargv __P((kvm_t *, const struct kinfo_proc *, int));
+char **kvm_getenvv __P((kvm_t *, const struct kinfo_proc *, int));
+char *kvm_geterr __P((kvm_t *));
+char *kvm_getfiles __P((kvm_t *, int, int, int *));
+int kvm_getloadavg __P((kvm_t *, double [], int));
+struct kinfo_proc *
+ kvm_getprocs __P((kvm_t *, int, int, int *));
+int kvm_getswapinfo __P((kvm_t *, struct kvm_swap *, int, int));
+int kvm_nlist __P((kvm_t *, struct nlist *));
+kvm_t *kvm_open
+ __P((const char *, const char *, const char *, int, const char *));
+kvm_t *kvm_openfiles
+ __P((const char *, const char *, const char *, int, char *));
+ssize_t kvm_read __P((kvm_t *, unsigned long, void *, size_t));
+ssize_t kvm_uread
+ __P((kvm_t *, const struct proc *, unsigned long, char *, size_t));
+ssize_t kvm_write __P((kvm_t *, unsigned long, const void *, size_t));
+__END_DECLS
+
+#endif /* !_KVM_H_ */
diff --git a/lib/libkvm/kvm_alpha.c b/lib/libkvm/kvm_alpha.c
new file mode 100644
index 0000000..391a955
--- /dev/null
+++ b/lib/libkvm/kvm_alpha.c
@@ -0,0 +1,212 @@
+/* $FreeBSD$ */
+/* $NetBSD: kvm_alpha.c,v 1.7.2.1 1997/11/02 20:34:26 mellon Exp $ */
+
+/*
+ * Copyright (c) 1994, 1995 Carnegie-Mellon University.
+ * All rights reserved.
+ *
+ * Author: Chris G. Demetriou
+ *
+ * Permission to use, copy, modify and distribute this software and
+ * its documentation is hereby granted, provided that both the copyright
+ * notice and this permission notice appear in all copies of the
+ * software, derivative works or modified versions, and any portions
+ * thereof, and that both notices appear in supporting documentation.
+ *
+ * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
+ * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
+ * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
+ *
+ * Carnegie Mellon requests users of this software to return to
+ *
+ * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
+ * School of Computer Science
+ * Carnegie Mellon University
+ * Pittsburgh PA 15213-3890
+ *
+ * any improvements or extensions that they make and grant Carnegie the
+ * rights to redistribute these changes.
+ */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/proc.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <nlist.h>
+#include <kvm.h>
+
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+
+#include <limits.h>
+#include <db.h>
+#include <stdlib.h>
+#include <machine/pmap.h>
+#include "kvm_private.h"
+
+static off_t _kvm_pa2off(kvm_t *kd, u_long pa);
+
+struct vmstate {
+ u_int64_t lev1map_pa; /* PA of Lev1map */
+ u_int64_t page_size; /* Page size */
+ u_int64_t nmemsegs; /* Number of RAM segm */
+};
+
+void
+_kvm_freevtop(kd)
+ kvm_t *kd;
+{
+
+ /* Not actually used for anything right now, but safe. */
+ if (kd->vmst != 0)
+ free(kd->vmst);
+}
+
+int
+_kvm_initvtop(kd)
+ kvm_t *kd;
+{
+ struct vmstate *vm;
+ struct nlist nlist[2];
+ u_long pa;
+
+ vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
+ if (vm == 0) {
+ _kvm_err(kd, kd->program, "cannot allocate vm");
+ return (-1);
+ }
+ kd->vmst = vm;
+ vm->page_size = ALPHA_PGBYTES;
+
+ nlist[0].n_name = "_Lev1map";
+ nlist[1].n_name = 0;
+
+ if (kvm_nlist(kd, nlist) != 0) {
+ _kvm_err(kd, kd->program, "bad namelist");
+ return (-1);
+ }
+
+ if(!ISALIVE(kd)) {
+ if (kvm_read(kd, (nlist[0].n_value), &pa, sizeof(pa)) != sizeof(pa)) {
+ _kvm_err(kd, kd->program, "cannot read Lev1map");
+ return (-1);
+ }
+ } else
+ if (kvm_read(kd, (nlist[0].n_value), &pa, sizeof(pa)) != sizeof(pa)) {
+ _kvm_err(kd, kd->program, "cannot read Lev1map");
+ return (-1);
+ }
+ vm->lev1map_pa = pa;
+ return (0);
+
+}
+
+int
+_kvm_kvatop(kd, va, pa)
+ kvm_t *kd;
+ u_long va;
+ u_long *pa;
+{
+ u_int64_t lev1map_pa; /* PA of Lev1map */
+ u_int64_t page_size;
+ int rv, page_off;
+ alpha_pt_entry_t pte;
+ off_t pteoff;
+ struct vmstate *vm;
+ vm = kd->vmst ;
+
+
+ if (ISALIVE(kd)) {
+ _kvm_err(kd, 0, "vatop called in live kernel!");
+ return(0);
+ }
+ lev1map_pa = vm->lev1map_pa;
+ page_size = vm->page_size;
+
+ page_off = va & (page_size - 1);
+ if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) {
+ /*
+ * Direct-mapped address: just convert it.
+ */
+
+ *pa = ALPHA_K0SEG_TO_PHYS(va);
+ rv = page_size - page_off;
+ } else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) {
+ /*
+ * Real kernel virtual address: do the translation.
+ */
+#define PTMASK ((1 << ALPHA_PTSHIFT) - 1)
+#define pmap_lev1_index(va) (((va) >> ALPHA_L1SHIFT) & PTMASK)
+#define pmap_lev2_index(va) (((va) >> ALPHA_L2SHIFT) & PTMASK)
+#define pmap_lev3_index(va) (((va) >> ALPHA_L3SHIFT) & PTMASK)
+
+ /* Find and read the L1 PTE. */
+ pteoff = lev1map_pa +
+ pmap_lev1_index(va) * sizeof(alpha_pt_entry_t);
+ if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 ||
+ read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) {
+ _kvm_syserr(kd, 0, "could not read L1 PTE");
+ goto lose;
+ }
+
+ /* Find and read the L2 PTE. */
+ if ((pte & ALPHA_PTE_VALID) == 0) {
+ _kvm_err(kd, 0, "invalid translation (invalid L1 PTE)");
+ goto lose;
+ }
+ pteoff = ALPHA_PTE_TO_PFN(pte) * page_size +
+ pmap_lev2_index(va) * sizeof(alpha_pt_entry_t);
+ if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 ||
+ read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) {
+ _kvm_syserr(kd, 0, "could not read L2 PTE");
+ goto lose;
+ }
+
+ /* Find and read the L3 PTE. */
+ if ((pte & ALPHA_PTE_VALID) == 0) {
+ _kvm_err(kd, 0, "invalid translation (invalid L2 PTE)");
+ goto lose;
+ }
+ pteoff = ALPHA_PTE_TO_PFN(pte) * page_size +
+ pmap_lev3_index(va) * sizeof(alpha_pt_entry_t);
+ if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 ||
+ read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) {
+ _kvm_syserr(kd, 0, "could not read L3 PTE");
+ goto lose;
+ }
+
+ /* Fill in the PA. */
+ if ((pte & ALPHA_PTE_VALID) == 0) {
+ _kvm_err(kd, 0, "invalid translation (invalid L3 PTE)");
+ goto lose;
+ }
+ *pa = ALPHA_PTE_TO_PFN(pte) * page_size + page_off;
+ rv = page_size - page_off;
+ } else {
+ /*
+ * Bogus address (not in KV space): punt.
+ */
+
+ _kvm_err(kd, 0, "invalid kernel virtual address");
+lose:
+ *pa = -1;
+ rv = 0;
+ }
+
+ return (rv);
+}
+
+/*
+ * Translate a physical address to a file-offset in the crash-dump.
+ */
+off_t
+_kvm_pa2off(kd, pa)
+ kvm_t *kd;
+ u_long pa;
+{
+ return ALPHA_K0SEG_TO_PHYS(pa);
+}
+
diff --git a/lib/libkvm/kvm_amd64.c b/lib/libkvm/kvm_amd64.c
new file mode 100644
index 0000000..d36b922
--- /dev/null
+++ b/lib/libkvm/kvm_amd64.c
@@ -0,0 +1,194 @@
+/*-
+ * Copyright (c) 1989, 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software developed by the Computer Systems
+ * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+ * BG 91-66 and contributed to Berkeley.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+
+/*
+ * i386 machine dependent routines for kvm. Hopefully, the forthcoming
+ * vm code will one day obsolete this module.
+ */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/proc.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <nlist.h>
+#include <kvm.h>
+
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+
+#include <limits.h>
+#include <db.h>
+
+#include "kvm_private.h"
+
+#ifndef btop
+#define btop(x) (i386_btop(x))
+#define ptob(x) (i386_ptob(x))
+#endif
+
+struct vmstate {
+ pd_entry_t *PTD;
+};
+
+void
+_kvm_freevtop(kvm_t *kd)
+{
+ if (kd->vmst != 0) {
+ if (kd->vmst->PTD) {
+ free(kd->vmst->PTD);
+ }
+ free(kd->vmst);
+ }
+}
+
+int
+_kvm_initvtop(kvm_t *kd)
+{
+ struct vmstate *vm;
+ struct nlist nlist[2];
+ u_long pa;
+ pd_entry_t *PTD;
+
+ vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
+ if (vm == 0) {
+ _kvm_err(kd, kd->program, "cannot allocate vm");
+ return (-1);
+ }
+ kd->vmst = vm;
+ vm->PTD = 0;
+
+ nlist[0].n_name = "_IdlePTD";
+ nlist[1].n_name = 0;
+
+ if (kvm_nlist(kd, nlist) != 0) {
+ _kvm_err(kd, kd->program, "bad namelist");
+ return (-1);
+ }
+ if (kvm_read(kd, (nlist[0].n_value - KERNBASE), &pa, sizeof(pa)) != sizeof(pa)) {
+ _kvm_err(kd, kd->program, "cannot read IdlePTD");
+ return (-1);
+ }
+ PTD = _kvm_malloc(kd, PAGE_SIZE);
+ if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) {
+ _kvm_err(kd, kd->program, "cannot read PTD");
+ return (-1);
+ }
+ vm->PTD = PTD;
+ return (0);
+}
+
+static int
+_kvm_vatop(kvm_t *kd, u_long va, u_long *pa)
+{
+ struct vmstate *vm;
+ u_long offset;
+ u_long pte_pa;
+ pd_entry_t pde;
+ pt_entry_t pte;
+ u_long pdeindex;
+ u_long pteindex;
+ int i;
+
+ if (ISALIVE(kd)) {
+ _kvm_err(kd, 0, "vatop called in live kernel!");
+ return((off_t)0);
+ }
+
+ vm = kd->vmst;
+ offset = va & (PAGE_SIZE - 1);
+
+ /*
+ * If we are initializing (kernel page table descriptor pointer
+ * not yet set) then return pa == va to avoid infinite recursion.
+ */
+ if (vm->PTD == 0) {
+ *pa = va;
+ return (PAGE_SIZE - offset);
+ }
+
+ pdeindex = va >> PDRSHIFT;
+ pde = vm->PTD[pdeindex];
+ if (((u_long)pde & PG_V) == 0)
+ goto invalid;
+
+ if ((u_long)pde & PG_PS) {
+ /*
+ * No second-level page table; ptd describes one 4MB page.
+ * (We assume that the kernel wouldn't set PG_PS without enabling
+ * it cr0, and that the kernel doesn't support 36-bit physical
+ * addresses).
+ */
+#define PAGE4M_MASK (NBPDR - 1)
+#define PG_FRAME4M (~PAGE4M_MASK)
+ *pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
+ return (NBPDR - (va & PAGE4M_MASK));
+ }
+
+ pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
+ pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t));
+
+ /* XXX This has to be a physical address read, kvm_read is virtual */
+ if (lseek(kd->pmfd, pte_pa, 0) == -1) {
+ _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
+ goto invalid;
+ }
+ if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
+ _kvm_syserr(kd, kd->program, "_kvm_vatop: read");
+ goto invalid;
+ }
+ if (((u_long)pte & PG_V) == 0)
+ goto invalid;
+
+ *pa = ((u_long)pte & PG_FRAME) + offset;
+ return (PAGE_SIZE - offset);
+
+invalid:
+ _kvm_err(kd, 0, "invalid address (%x)", va);
+ return (0);
+}
+
+int
+_kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
+{
+ return (_kvm_vatop(kd, va, pa));
+}
diff --git a/lib/libkvm/kvm_file.c b/lib/libkvm/kvm_file.c
new file mode 100644
index 0000000..4af6b71
--- /dev/null
+++ b/lib/libkvm/kvm_file.c
@@ -0,0 +1,188 @@
+/*-
+ * Copyright (c) 1989, 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)kvm_file.c 8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+
+/*
+ * File list interface for kvm. pstat, fstat and netstat are
+ * users of this code, so we've factored it out into a separate module.
+ * Thus, we keep this grunge out of the other kvm applications (i.e.,
+ * most other applications are interested only in open/close/read/nlist).
+ */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/proc.h>
+#define KERNEL
+#include <sys/file.h>
+#undef KERNEL
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <nlist.h>
+#include <kvm.h>
+
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+#include <vm/swap_pager.h>
+
+#include <sys/sysctl.h>
+
+#include <limits.h>
+#include <ndbm.h>
+#include <paths.h>
+
+#include "kvm_private.h"
+
+#define KREAD(kd, addr, obj) \
+ (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
+
+/*
+ * Get file structures.
+ */
+static int
+kvm_deadfiles(kd, op, arg, filehead_o, nfiles)
+ kvm_t *kd;
+ int op, arg, nfiles;
+ long filehead_o;
+{
+ int buflen = kd->arglen, n = 0;
+ struct file *fp;
+ register char *where = kd->argspc;
+ struct filelist filehead;
+
+ /*
+ * first copyout filehead
+ */
+ if (buflen > sizeof (filehead)) {
+ if (KREAD(kd, filehead_o, &filehead)) {
+ _kvm_err(kd, kd->program, "can't read filehead");
+ return (0);
+ }
+ buflen -= sizeof (filehead);
+ where += sizeof (filehead);
+ *(struct filelist *)kd->argspc = filehead;
+ }
+ /*
+ * followed by an array of file structures
+ */
+ for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
+ if (buflen > sizeof (struct file)) {
+ if (KREAD(kd, (long)fp, ((struct file *)where))) {
+ _kvm_err(kd, kd->program, "can't read kfp");
+ return (0);
+ }
+ buflen -= sizeof (struct file);
+ fp = (struct file *)where;
+ where += sizeof (struct file);
+ n++;
+ }
+ }
+ if (n != nfiles) {
+ _kvm_err(kd, kd->program, "inconsistant nfiles");
+ return (0);
+ }
+ return (nfiles);
+}
+
+char *
+kvm_getfiles(kd, op, arg, cnt)
+ kvm_t *kd;
+ int op, arg;
+ int *cnt;
+{
+ int mib[2], st, nfiles;
+ size_t size;
+ struct file *fp, *fplim;
+ struct filelist filehead;
+
+ if (ISALIVE(kd)) {
+ size = 0;
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_FILE;
+ st = sysctl(mib, 2, NULL, &size, NULL, 0);
+ if (st == -1) {
+ _kvm_syserr(kd, kd->program, "kvm_getprocs");
+ return (0);
+ }
+ if (kd->argspc == 0)
+ kd->argspc = (char *)_kvm_malloc(kd, size);
+ else if (kd->arglen < size)
+ kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
+ if (kd->argspc == 0)
+ return (0);
+ kd->arglen = size;
+ st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
+ if (st == -1 || size < sizeof(filehead)) {
+ _kvm_syserr(kd, kd->program, "kvm_getfiles");
+ return (0);
+ }
+ filehead = *(struct filelist *)kd->argspc;
+ fp = (struct file *)(kd->argspc + sizeof (filehead));
+ fplim = (struct file *)(kd->argspc + size);
+ for (nfiles = 0; filehead.lh_first && (fp < fplim); nfiles++, fp++)
+ filehead.lh_first = fp->f_list.le_next;
+ } else {
+ struct nlist nl[3], *p;
+
+ nl[0].n_name = "_filehead";
+ nl[1].n_name = "_nfiles";
+ nl[2].n_name = 0;
+
+ if (kvm_nlist(kd, nl) != 0) {
+ for (p = nl; p->n_type != 0; ++p)
+ ;
+ _kvm_err(kd, kd->program,
+ "%s: no such symbol", p->n_name);
+ return (0);
+ }
+ if (KREAD(kd, nl[0].n_value, &nfiles)) {
+ _kvm_err(kd, kd->program, "can't read nfiles");
+ return (0);
+ }
+ size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
+ if (kd->argspc == 0)
+ kd->argspc = (char *)_kvm_malloc(kd, size);
+ else if (kd->arglen < size)
+ kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
+ if (kd->argspc == 0)
+ return (0);
+ kd->arglen = size;
+ nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles);
+ if (nfiles == 0)
+ return (0);
+ }
+ *cnt = nfiles;
+ return (kd->argspc);
+}
diff --git a/lib/libkvm/kvm_geterr.3 b/lib/libkvm/kvm_geterr.3
new file mode 100644
index 0000000..bab730e
--- /dev/null
+++ b/lib/libkvm/kvm_geterr.3
@@ -0,0 +1,81 @@
+.\" Copyright (c) 1992, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software developed by the Computer Systems
+.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+.\" BG 91-66 and contributed to Berkeley.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+.\"
+.\" @(#)kvm_geterr.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
+.\"
+.Dd June 4, 1993
+.Dt KVM_GETERR 3
+.Os
+.Sh NAME
+.Nm kvm_geterr
+.Nd get error message on kvm descriptor
+.Sh SYNOPSIS
+.Fd #include <kvm.h>
+.br
+.Ft char *
+.Fn kvm_geterr "kvm_t *kd"
+.Sh DESCRIPTION
+This function returns a string describing the most recent error condition
+on the descriptor
+.Fa kd .
+The results are undefined if the most recent
+.Xr kvm 3
+library call did not produce an error.
+The string returned is stored in memory owned by
+.Xr kvm 3
+so the message should be copied out and saved elsewhere if necessary.
+.Sh BUGS
+This routine cannot be used to access error conditions due to a failed
+.Fn kvm_openfiles
+call, since failure is indicated by returning a
+.Dv NULL
+descriptor.
+Therefore, errors on open are output to the special error buffer
+passed to
+.Fn kvm_openfiles .
+This option is not available to
+.Fn kvm_open .
+.Sh SEE ALSO
+.Xr kvm 3 ,
+.Xr kvm_close 3 ,
+.Xr kvm_getargv 3 ,
+.Xr kvm_getenvv 3 ,
+.Xr kvm_getprocs 3 ,
+.Xr kvm_nlist 3 ,
+.Xr kvm_open 3 ,
+.Xr kvm_openfiles 3 ,
+.Xr kvm_read 3 ,
+.Xr kvm_write 3
diff --git a/lib/libkvm/kvm_getfiles.3 b/lib/libkvm/kvm_getfiles.3
new file mode 100644
index 0000000..0f8050d
--- /dev/null
+++ b/lib/libkvm/kvm_getfiles.3
@@ -0,0 +1,86 @@
+.\" Copyright (c) 1992, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software developed by the Computer Systems
+.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+.\" BG 91-66 and contributed to Berkeley.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+.\"
+.\" @(#)kvm_getfiles.3 8.2 (Berkeley) 4/19/94
+.\" $FreeBSD$
+.\"
+.Dd April 19, 1994
+.Dt KVM_GETFILES 3
+.Os
+.Sh NAME
+.Nm kvm_getfiles
+.Nd survey open files
+.Sh SYNOPSIS
+.Fd #include <kvm.h>
+.Fd #define KERNEL
+.Fd #include <sys/file.h>
+.Fd #undef KERNEL
+.\" .Fa kvm_t *kd
+.br
+.Ft char *
+.Fn kvm_getfiles "kvm_t *kd" "int op" "int arg" "int *cnt"
+.Sh DESCRIPTION
+.Fn kvm_getfiles
+returns a (sub-)set of the open files in the kernel indicated by
+.Fa kd.
+The
+.Fa op
+and
+.Fa arg
+arguments constitute a predicate which limits the set of files
+returned. No predicates are currently defined.
+.Pp
+The number of processes found is returned in the reference parameter
+.Fa cnt .
+The files are returned as a contiguous array of file structures,
+preceded by the address of the first file entry in the kernel.
+This memory is owned by kvm and is not guaranteed to be persistent across
+subsequent kvm library calls. Data should be copied out if it needs to be
+saved.
+.Sh RETURN VALUES
+.Fn kvm_getfiles
+will return NULL on failure.
+.Pp
+.Sh BUGS
+This routine does not belong in the kvm interface.
+.Sh SEE ALSO
+.Xr kvm 3 ,
+.Xr kvm_close 3 ,
+.Xr kvm_geterr 3 ,
+.Xr kvm_nlist 3 ,
+.Xr kvm_open 3 ,
+.Xr kvm_openfiles 3 ,
+.Xr kvm_read 3 ,
+.Xr kvm_write 3
diff --git a/lib/libkvm/kvm_getloadavg.3 b/lib/libkvm/kvm_getloadavg.3
new file mode 100644
index 0000000..7df1847
--- /dev/null
+++ b/lib/libkvm/kvm_getloadavg.3
@@ -0,0 +1,64 @@
+.\" Copyright (c) 1992, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+.\"
+.\" @(#)kvm_getloadavg.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
+.\"
+.Dd June 4, 1993
+.Dt KVM_GETLOADAVG 3
+.Os
+.Sh NAME
+.Nm kvm_getloadavg
+.Nd get load average of the system
+.Sh SYNOPSIS
+.Fd #include <kvm.h>
+.Ft int
+.Fn kvm_getloadavg "kvm_t *kd" "double loadavg[]" "int nelem"
+.Sh DESCRIPTION
+The
+.Fn kvm_getloadavg
+function returns the number of processes in the system run queue
+of the kernel indicated by
+.Fa kd ,
+averaged over various periods of time.
+Up to
+.Fa nelem
+samples are retrieved and assigned to successive elements of
+.Fa loadavg Ns Bq .
+The system imposes a maximum of 3 samples, representing averages
+over the last 1, 5, and 15 minutes, respectively.
+.Sh DIAGNOSTICS
+If the load average was unobtainable, \-1 is returned; otherwise,
+the number of samples actually retrieved is returned.
+.Sh SEE ALSO
+.Xr uptime 1 ,
+.Xr getloadavg 3 ,
+.Xr kvm 3
diff --git a/lib/libkvm/kvm_getloadavg.c b/lib/libkvm/kvm_getloadavg.c
new file mode 100644
index 0000000..3368e63
--- /dev/null
+++ b/lib/libkvm/kvm_getloadavg.c
@@ -0,0 +1,106 @@
+/*-
+ * Copyright (c) 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)kvm_getloadavg.c 8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/proc.h>
+#include <sys/sysctl.h>
+#include <vm/vm_param.h>
+
+#include <stdlib.h>
+#include <db.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <nlist.h>
+#include <kvm.h>
+
+#include "kvm_private.h"
+
+static struct nlist nl[] = {
+ { "_averunnable" },
+#define X_AVERUNNABLE 0
+ { "_fscale" },
+#define X_FSCALE 1
+ { "" },
+};
+
+/*
+ * kvm_getloadavg() -- Get system load averages, from live or dead kernels.
+ *
+ * Put `nelem' samples into `loadavg' array.
+ * Return number of samples retrieved, or -1 on error.
+ */
+int
+kvm_getloadavg(kd, loadavg, nelem)
+ kvm_t *kd;
+ double loadavg[];
+ int nelem;
+{
+ struct loadavg loadinfo;
+ struct nlist *p;
+ int fscale, i;
+
+ if (ISALIVE(kd))
+ return (getloadavg(loadavg, nelem));
+
+ if (kvm_nlist(kd, nl) != 0) {
+ for (p = nl; p->n_type != 0; ++p);
+ _kvm_err(kd, kd->program,
+ "%s: no such symbol", p->n_name);
+ return (-1);
+ }
+
+#define KREAD(kd, addr, obj) \
+ (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
+ if (KREAD(kd, nl[X_AVERUNNABLE].n_value, &loadinfo)) {
+ _kvm_err(kd, kd->program, "can't read averunnable");
+ return (-1);
+ }
+
+ /*
+ * Old kernels have fscale separately; if not found assume
+ * running new format.
+ */
+ if (!KREAD(kd, nl[X_FSCALE].n_value, &fscale))
+ loadinfo.fscale = fscale;
+
+ nelem = MIN(nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t));
+ for (i = 0; i < nelem; i++)
+ loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale;
+ return (nelem);
+}
diff --git a/lib/libkvm/kvm_getprocs.3 b/lib/libkvm/kvm_getprocs.3
new file mode 100644
index 0000000..bd6bb74
--- /dev/null
+++ b/lib/libkvm/kvm_getprocs.3
@@ -0,0 +1,164 @@
+.\" Copyright (c) 1992, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software developed by the Computer Systems
+.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+.\" BG 91-66 and contributed to Berkeley.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+.\"
+.\" @(#)kvm_getprocs.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
+.\"
+.Dd June 4, 1993
+.Dt KVM_GETPROCS 3
+.Os
+.Sh NAME
+.Nm kvm_getprocs ,
+.Nm kvm_getargv ,
+.Nm kvm_getenvv
+.Nd access user process state
+.Sh SYNOPSIS
+.Fd #include <kvm.h>
+.Fd #include <sys/param.h>
+.Fd #include <sys/sysctl.h>
+.\" .Fa kvm_t *kd
+.br
+.Ft struct kinfo_proc *
+.Fn kvm_getprocs "kvm_t *kd" "int op" "int arg" "int *cnt"
+.Ft char **
+.Fn kvm_getargv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr"
+.Ft char **
+.Fn kvm_getenvv "kvm_t *kd" "const struct kinfo_proc *p" "int nchr"
+.Sh DESCRIPTION
+.Fn kvm_getprocs
+returns a (sub-)set of active processes in the kernel indicated by
+.Fa kd.
+The
+.Fa op
+and
+.Fa arg
+arguments constitute a predicate which limits the set of processes
+returned. The value of
+.Fa op
+describes the filtering predicate as follows:
+.Pp
+.Bl -tag -width 20n -offset indent -compact
+.It Sy KERN_PROC_ALL
+all processes
+.It Sy KERN_PROC_PID
+processes with process id
+.Fa arg
+.It Sy KERN_PROC_PGRP
+processes with process group
+.Fa arg
+.It Sy KERN_PROC_SESSION
+processes with session
+.Fa arg
+.It Sy KERN_PROC_TTY
+processes with tty
+.Fa arg
+.It Sy KERN_PROC_UID
+processes with effective user id
+.Fa arg
+.It Sy KERN_PROC_RUID
+processes with real user id
+.Fa arg
+.El
+.Pp
+The number of processes found is returned in the reference parameter
+.Fa cnt .
+The processes are returned as a contiguous array of kinfo_proc structures.
+This memory is locally allocated, and subsequent calls to
+.Fn kvm_getprocs
+and
+.Fn kvm_close
+will overwrite this storage.
+.Pp
+.Fn kvm_getargv
+returns a null-terminated argument vector that corresponds to the
+command line arguments passed to process indicated by
+.Fa p .
+Most likely, these arguments correspond to the values passed to
+.Xr exec 3
+on process creation. This information is, however,
+deliberately under control of the process itself.
+Note that the original command name can be found, unaltered,
+in the p_comm field of the process structure returned by
+.Fn kvm_getprocs .
+.Pp
+The
+.Fa nchr
+argument indicates the maximum number of characters, including null bytes,
+to use in building the strings. If this amount is exceeded, the string
+causing the overflow is truncated and the partial result is returned.
+This is handy for programs like
+.Xr ps 1
+and
+.Xr w 1
+that print only a one line summary of a command and should not copy
+out large amounts of text only to ignore it.
+If
+.Fa nchr
+is zero, no limit is imposed and all argument strings are returned in
+their entirety.
+.Pp
+The memory allocated to the argv pointers and string storage
+is owned by the kvm library. Subsequent
+.Fn kvm_getprocs
+and
+.Xr kvm_close 3
+calls will clobber this storage.
+.Pp
+The
+.Fn kvm_getenvv
+function is similar to
+.Fn kvm_getargv
+but returns the vector of environment strings. This data is
+also alterable by the process.
+.Sh RETURN VALUES
+.Fn kvm_getprocs ,
+.Fn kvm_getargv ,
+and
+.Fn kvm_getenvv ,
+all return
+.Dv NULL
+on failure.
+.Pp
+.Sh BUGS
+These routines do not belong in the kvm interface.
+.Sh SEE ALSO
+.Xr kvm 3 ,
+.Xr kvm_close 3 ,
+.Xr kvm_geterr 3 ,
+.Xr kvm_nlist 3 ,
+.Xr kvm_open 3 ,
+.Xr kvm_openfiles 3 ,
+.Xr kvm_read 3 ,
+.Xr kvm_write 3
diff --git a/lib/libkvm/kvm_getswapinfo.3 b/lib/libkvm/kvm_getswapinfo.3
new file mode 100644
index 0000000..dfcac1f
--- /dev/null
+++ b/lib/libkvm/kvm_getswapinfo.3
@@ -0,0 +1,62 @@
+.\" Copyright (c) 1999, Matthew Dillon. All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided under the terms of the BSD
+.\" Copyright as found in /usr/src/COPYRIGHT in the FreeBSD source tree.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd January 22, 1999
+.Dt KVM_SWAPINFO 3
+.Os
+.Sh NAME
+.Nm kvm_getswapinfo
+.Nd return swap summary statistics for the system
+.Sh SYNOPSIS
+.Fd #include <kvm.h>
+.Ft int
+.Fn kvm_getswapinfo "kvm_t *kd" "struct kvm_swap *" "int maxswap" "int flags"
+.Sh DESCRIPTION
+The
+.Fn kvm_getswapinfo
+function fills an array of kvm_swap structures with swap summary
+information for each swap device, for up to maxswap - 1 devices.
+The number of devices, up to maxswap - 1, is returned. A grand
+total of all swap devices ( including any devices that go beyond
+maxswap - 1 ) is returned in one additional array entry. This
+entry is not counted in the return value. Thus, if you specify
+a maxswap value of 1, the function will typically return the
+value 0 and the single kvm_swap structure will be filled with
+the grand total over all swap devices. The grand total is calculated
+from all available swap devices whether or not you made room
+for them all in the array.
+the grant total is returned.
+.Pp
+The flags argument is currently unused and must be passed as 0.
+.Pp
+If an error occurs, -1 is returned.
+.Pp
+Each swap partition and the grand total is summarized in the kvm_swap
+structure. This structure contains the following fields:
+.Bl -inset -width indent
+.It char ksw_devname[];
+.It int ksw_total;
+.It int ksw_used;
+.It int ksw_flags;
+.El
+.Pp
+Values are in PAGE_SIZE'd chunks ( see getpagesize() ). ksw_flags contains
+a copy of the swap device flags.
+.PP
+.Sh CACHING
+This function caches the nlist values for various kernel variables which
+it reuses in successive calls. You may call the function with kd == NULL
+to clear the cache.
+.Sh DIAGNOSTICS
+If the load average was unobtainable, \-1 is returned; otherwise,
+the number of swap devices actually retrieved is returned.
+.Pp
+If the name of the swap device does not fit in the static char buffer
+in the structure, it is truncated. The buffer is always zero terminated.
+.Sh SEE ALSO
+.Xr kvm 3
diff --git a/lib/libkvm/kvm_getswapinfo.c b/lib/libkvm/kvm_getswapinfo.c
new file mode 100644
index 0000000..05aeedf
--- /dev/null
+++ b/lib/libkvm/kvm_getswapinfo.c
@@ -0,0 +1,420 @@
+/*
+ * Copyright (c) 1999, Matthew Dillon. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided under the terms of the BSD
+ * Copyright as found in /usr/src/COPYRIGHT in the FreeBSD source tree.
+ */
+
+#ifndef lint
+static const char copyright[] =
+ "@(#) Copyright (c) 1999\n"
+ "Matthew Dillon. All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static const char rcsid[] =
+ "$FreeBSD$";
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/vnode.h>
+#include <sys/ucred.h>
+#include <sys/stat.h>
+#include <sys/conf.h>
+#include <sys/blist.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <limits.h>
+#include <nlist.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static struct nlist kvm_swap_nl[] = {
+ { "_swapblist" }, /* new radix swap list */
+ { "_swdevt" }, /* list of swap devices and sizes */
+ { "_nswdev" }, /* number of swap devices */
+ { "_dmmax" }, /* maximum size of a swap block */
+ { "" }
+};
+
+#define NL_SWAPBLIST 0
+#define NL_SWDEVT 1
+#define NL_NSWDEV 2
+#define NL_DMMAX 3
+
+static int kvm_swap_nl_cached = 0;
+static int nswdev;
+static int unswdev;
+static int dmmax;
+
+static void getswapinfo_radix(kvm_t *kd, struct kvm_swap *swap_ary,
+ int swap_max, int flags);
+
+#define SVAR(var) __STRING(var) /* to force expansion */
+#define KGET(idx, var) \
+ KGET1(idx, &var, sizeof(var), SVAR(var))
+#define KGET1(idx, p, s, msg) \
+ KGET2(kvm_swap_nl[idx].n_value, p, s, msg)
+#define KGET2(addr, p, s, msg) \
+ if (kvm_read(kd, (u_long)(addr), p, s) != s) \
+ warnx("cannot read %s: %s", msg, kvm_geterr(kd))
+#define KGETN(idx, var) \
+ KGET1N(idx, &var, sizeof(var), SVAR(var))
+#define KGET1N(idx, p, s, msg) \
+ KGET2N(kvm_swap_nl[idx].n_value, p, s, msg)
+#define KGET2N(addr, p, s, msg) \
+ ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
+#define KGETRET(addr, p, s, msg) \
+ if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
+ warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
+ return (0); \
+ }
+
+int
+kvm_getswapinfo(
+ kvm_t *kd,
+ struct kvm_swap *swap_ary,
+ int swap_max,
+ int flags
+) {
+ int ti = 0;
+
+ /*
+ * clear cache
+ */
+ if (kd == NULL) {
+ kvm_swap_nl_cached = 0;
+ return(0);
+ }
+
+ /*
+ * namelist
+ */
+ if (kvm_swap_nl_cached == 0) {
+ struct swdevt *sw;
+
+ if (kvm_nlist(kd, kvm_swap_nl) < 0)
+ return(-1);
+
+ /*
+ * required entries
+ */
+
+ if (
+ kvm_swap_nl[NL_SWDEVT].n_value == 0 ||
+ kvm_swap_nl[NL_NSWDEV].n_value == 0 ||
+ kvm_swap_nl[NL_DMMAX].n_value == 0 ||
+ kvm_swap_nl[NL_SWAPBLIST].n_type == 0
+ ) {
+ return(-1);
+ }
+
+ /*
+ * get globals, type of swap
+ */
+
+ KGET(NL_NSWDEV, nswdev);
+ KGET(NL_DMMAX, dmmax);
+
+ /*
+ * figure out how many actual swap devices are enabled
+ */
+
+ KGET(NL_SWDEVT, sw);
+ for (unswdev = nswdev - 1; unswdev >= 0; --unswdev) {
+ struct swdevt swinfo;
+
+ KGET2(&sw[unswdev], &swinfo, sizeof(swinfo), "swinfo");
+ if (swinfo.sw_nblks)
+ break;
+ }
+ ++unswdev;
+
+ kvm_swap_nl_cached = 1;
+ }
+
+
+ {
+ struct swdevt *sw;
+ int i;
+
+ ti = unswdev;
+ if (ti >= swap_max)
+ ti = swap_max - 1;
+
+ if (ti >= 0)
+ bzero(swap_ary, sizeof(struct kvm_swap) * (ti + 1));
+
+ KGET(NL_SWDEVT, sw);
+ for (i = 0; i < unswdev; ++i) {
+ struct swdevt swinfo;
+ int ttl;
+
+ KGET2(&sw[i], &swinfo, sizeof(swinfo), "swinfo");
+
+ /*
+ * old style: everything in DEV_BSIZE'd chunks,
+ * convert to pages.
+ *
+ * new style: swinfo in DEV_BSIZE'd chunks but dmmax
+ * in pages.
+ *
+ * The first dmmax is never allocating to avoid
+ * trashing the disklabels
+ */
+
+ ttl = swinfo.sw_nblks - dmmax;
+
+ if (ttl == 0)
+ continue;
+
+ if (i < ti) {
+ swap_ary[i].ksw_total = ttl;
+ swap_ary[i].ksw_used = ttl;
+ swap_ary[i].ksw_flags = swinfo.sw_flags;
+ if (swinfo.sw_dev == NODEV) {
+ snprintf(
+ swap_ary[i].ksw_devname,
+ sizeof(swap_ary[i].ksw_devname),
+ "%s",
+ "[NFS swap]"
+ );
+ } else {
+ snprintf(
+ swap_ary[i].ksw_devname,
+ sizeof(swap_ary[i].ksw_devname),
+ "%s%s",
+ ((flags & SWIF_DEV_PREFIX) ? "/dev/" : ""),
+ devname(swinfo.sw_dev, S_IFCHR)
+ );
+ }
+ }
+ if (ti >= 0) {
+ swap_ary[ti].ksw_total += ttl;
+ swap_ary[ti].ksw_used += ttl;
+ }
+ }
+ }
+
+ getswapinfo_radix(kd, swap_ary, swap_max, flags);
+ return(ti);
+}
+
+/*
+ * scanradix() - support routine for radix scanner
+ */
+
+#define TABME tab, tab, ""
+
+static int
+scanradix(
+ blmeta_t *scan,
+ daddr_t blk,
+ daddr_t radix,
+ daddr_t skip,
+ daddr_t count,
+ kvm_t *kd,
+ int dmmax,
+ int nswdev,
+ struct kvm_swap *swap_ary,
+ int swap_max,
+ int tab,
+ int flags
+) {
+ blmeta_t meta;
+ int ti = (unswdev >= swap_max) ? swap_max - 1 : unswdev;
+
+ KGET2(scan, &meta, sizeof(meta), "blmeta_t");
+
+ /*
+ * Terminator
+ */
+ if (meta.bm_bighint == (daddr_t)-1) {
+ if (flags & SWIF_DUMP_TREE) {
+ printf("%*.*s(0x%06x,%d) Terminator\n",
+ TABME,
+ blk,
+ radix
+ );
+ }
+ return(-1);
+ }
+
+ if (radix == BLIST_BMAP_RADIX) {
+ /*
+ * Leaf bitmap
+ */
+ int i;
+
+ if (flags & SWIF_DUMP_TREE) {
+ printf("%*.*s(0x%06x,%d) Bitmap %08x big=%d\n",
+ TABME,
+ blk,
+ radix,
+ (int)meta.u.bmu_bitmap,
+ meta.bm_bighint
+ );
+ }
+
+ /*
+ * If not all allocated, count.
+ */
+ if (meta.u.bmu_bitmap != 0) {
+ for (i = 0; i < BLIST_BMAP_RADIX && i < count; ++i) {
+ /*
+ * A 0 bit means allocated
+ */
+ if ((meta.u.bmu_bitmap & (1 << i))) {
+ int t = 0;
+
+ if (nswdev)
+ t = (blk + i) / dmmax % nswdev;
+ if (t < ti)
+ --swap_ary[t].ksw_used;
+ if (ti >= 0)
+ --swap_ary[ti].ksw_used;
+ }
+ }
+ }
+ } else if (meta.u.bmu_avail == radix) {
+ /*
+ * Meta node if all free
+ */
+ if (flags & SWIF_DUMP_TREE) {
+ printf("%*.*s(0x%06x,%d) Submap ALL-FREE {\n",
+ TABME,
+ blk,
+ radix,
+ (int)meta.u.bmu_avail,
+ meta.bm_bighint
+ );
+ }
+ /*
+ * Note: both dmmax and radix are powers of 2. However, dmmax
+ * may be larger then radix so use a smaller increment if
+ * necessary.
+ */
+ {
+ int t;
+ int tinc = dmmax;
+
+ while (tinc > radix)
+ tinc >>= 1;
+
+ for (t = blk; t < blk + radix; t += tinc) {
+ int u = (nswdev) ? (t / dmmax % nswdev) : 0;
+
+ if (u < ti)
+ swap_ary[u].ksw_used -= tinc;
+ if (ti >= 0)
+ swap_ary[ti].ksw_used -= tinc;
+ }
+ }
+ } else if (meta.u.bmu_avail == 0) {
+ /*
+ * Meta node if all used
+ */
+ if (flags & SWIF_DUMP_TREE) {
+ printf("%*.*s(0x%06x,%d) Submap ALL-ALLOCATED\n",
+ TABME,
+ blk,
+ radix,
+ (int)meta.u.bmu_avail,
+ meta.bm_bighint
+ );
+ }
+ } else {
+ /*
+ * Meta node if not all free
+ */
+ int i;
+ int next_skip;
+
+ if (flags & SWIF_DUMP_TREE) {
+ printf("%*.*s(0x%06x,%d) Submap avail=%d big=%d {\n",
+ TABME,
+ blk,
+ radix,
+ (int)meta.u.bmu_avail,
+ meta.bm_bighint
+ );
+ }
+
+ radix >>= BLIST_META_RADIX_SHIFT;
+ next_skip = skip >> BLIST_META_RADIX_SHIFT;
+
+ for (i = 1; i <= skip; i += next_skip) {
+ int r;
+ daddr_t vcount = (count > radix) ? radix : count;
+
+ r = scanradix(
+ &scan[i],
+ blk,
+ radix,
+ next_skip - 1,
+ vcount,
+ kd,
+ dmmax,
+ nswdev,
+ swap_ary,
+ swap_max,
+ tab + 4,
+ flags
+ );
+ if (r < 0)
+ break;
+ blk += radix;
+ }
+ if (flags & SWIF_DUMP_TREE) {
+ printf("%*.*s}\n", TABME);
+ }
+ }
+ return(0);
+}
+
+static void
+getswapinfo_radix(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags)
+{
+ struct blist *swapblist = NULL;
+ struct blist blcopy = { 0 };
+
+ KGET(NL_SWAPBLIST, swapblist);
+
+ if (swapblist == NULL) {
+ if (flags & SWIF_DUMP_TREE)
+ printf("radix tree: NULL - no swap in system\n");
+ return;
+ }
+
+ KGET2(swapblist, &blcopy, sizeof(blcopy), "*swapblist");
+
+ if (flags & SWIF_DUMP_TREE) {
+ printf("radix tree: %d/%d/%d blocks, %dK wired\n",
+ blcopy.bl_free,
+ blcopy.bl_blocks,
+ blcopy.bl_radix,
+ (blcopy.bl_rootblks * sizeof(blmeta_t) + 1023)/
+ 1024
+ );
+ }
+ scanradix(
+ blcopy.bl_root,
+ 0,
+ blcopy.bl_radix,
+ blcopy.bl_skip,
+ blcopy.bl_rootblks,
+ kd,
+ dmmax,
+ nswdev,
+ swap_ary,
+ swap_max,
+ 0,
+ flags
+ );
+}
diff --git a/lib/libkvm/kvm_i386.c b/lib/libkvm/kvm_i386.c
new file mode 100644
index 0000000..d36b922
--- /dev/null
+++ b/lib/libkvm/kvm_i386.c
@@ -0,0 +1,194 @@
+/*-
+ * Copyright (c) 1989, 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software developed by the Computer Systems
+ * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+ * BG 91-66 and contributed to Berkeley.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+
+/*
+ * i386 machine dependent routines for kvm. Hopefully, the forthcoming
+ * vm code will one day obsolete this module.
+ */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/proc.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <nlist.h>
+#include <kvm.h>
+
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+
+#include <limits.h>
+#include <db.h>
+
+#include "kvm_private.h"
+
+#ifndef btop
+#define btop(x) (i386_btop(x))
+#define ptob(x) (i386_ptob(x))
+#endif
+
+struct vmstate {
+ pd_entry_t *PTD;
+};
+
+void
+_kvm_freevtop(kvm_t *kd)
+{
+ if (kd->vmst != 0) {
+ if (kd->vmst->PTD) {
+ free(kd->vmst->PTD);
+ }
+ free(kd->vmst);
+ }
+}
+
+int
+_kvm_initvtop(kvm_t *kd)
+{
+ struct vmstate *vm;
+ struct nlist nlist[2];
+ u_long pa;
+ pd_entry_t *PTD;
+
+ vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
+ if (vm == 0) {
+ _kvm_err(kd, kd->program, "cannot allocate vm");
+ return (-1);
+ }
+ kd->vmst = vm;
+ vm->PTD = 0;
+
+ nlist[0].n_name = "_IdlePTD";
+ nlist[1].n_name = 0;
+
+ if (kvm_nlist(kd, nlist) != 0) {
+ _kvm_err(kd, kd->program, "bad namelist");
+ return (-1);
+ }
+ if (kvm_read(kd, (nlist[0].n_value - KERNBASE), &pa, sizeof(pa)) != sizeof(pa)) {
+ _kvm_err(kd, kd->program, "cannot read IdlePTD");
+ return (-1);
+ }
+ PTD = _kvm_malloc(kd, PAGE_SIZE);
+ if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) {
+ _kvm_err(kd, kd->program, "cannot read PTD");
+ return (-1);
+ }
+ vm->PTD = PTD;
+ return (0);
+}
+
+static int
+_kvm_vatop(kvm_t *kd, u_long va, u_long *pa)
+{
+ struct vmstate *vm;
+ u_long offset;
+ u_long pte_pa;
+ pd_entry_t pde;
+ pt_entry_t pte;
+ u_long pdeindex;
+ u_long pteindex;
+ int i;
+
+ if (ISALIVE(kd)) {
+ _kvm_err(kd, 0, "vatop called in live kernel!");
+ return((off_t)0);
+ }
+
+ vm = kd->vmst;
+ offset = va & (PAGE_SIZE - 1);
+
+ /*
+ * If we are initializing (kernel page table descriptor pointer
+ * not yet set) then return pa == va to avoid infinite recursion.
+ */
+ if (vm->PTD == 0) {
+ *pa = va;
+ return (PAGE_SIZE - offset);
+ }
+
+ pdeindex = va >> PDRSHIFT;
+ pde = vm->PTD[pdeindex];
+ if (((u_long)pde & PG_V) == 0)
+ goto invalid;
+
+ if ((u_long)pde & PG_PS) {
+ /*
+ * No second-level page table; ptd describes one 4MB page.
+ * (We assume that the kernel wouldn't set PG_PS without enabling
+ * it cr0, and that the kernel doesn't support 36-bit physical
+ * addresses).
+ */
+#define PAGE4M_MASK (NBPDR - 1)
+#define PG_FRAME4M (~PAGE4M_MASK)
+ *pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK);
+ return (NBPDR - (va & PAGE4M_MASK));
+ }
+
+ pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
+ pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t));
+
+ /* XXX This has to be a physical address read, kvm_read is virtual */
+ if (lseek(kd->pmfd, pte_pa, 0) == -1) {
+ _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
+ goto invalid;
+ }
+ if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
+ _kvm_syserr(kd, kd->program, "_kvm_vatop: read");
+ goto invalid;
+ }
+ if (((u_long)pte & PG_V) == 0)
+ goto invalid;
+
+ *pa = ((u_long)pte & PG_FRAME) + offset;
+ return (PAGE_SIZE - offset);
+
+invalid:
+ _kvm_err(kd, 0, "invalid address (%x)", va);
+ return (0);
+}
+
+int
+_kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
+{
+ return (_kvm_vatop(kd, va, pa));
+}
diff --git a/lib/libkvm/kvm_nlist.3 b/lib/libkvm/kvm_nlist.3
new file mode 100644
index 0000000..f5611b3
--- /dev/null
+++ b/lib/libkvm/kvm_nlist.3
@@ -0,0 +1,89 @@
+.\" Copyright (c) 1992, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software developed by the Computer Systems
+.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+.\" BG 91-66 and contributed to Berkeley.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+.\"
+.\" @(#)kvm_nlist.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
+.\"
+.Dd June 4, 1993
+.Dt KVM_NLIST 3
+.Os
+.Sh NAME
+.Nm kvm_nlist
+.Nd retrieve symbol table names from a kernel image
+.Sh SYNOPSIS
+.Fd #include <kvm.h>
+.Fd #include <nlist.h>
+.Ft int
+.Fn kvm_nlist "kvm_t *kd" "struct nlist *nl"
+.Sh DESCRIPTION
+.Fn kvm_nlist
+retrieves the symbol table entries indicated by the name list argument
+.Fa \&nl .
+This argument points to an array of nlist structures, terminated by
+an entry whose n_name field is
+.Dv NULL
+(see
+.Xr nlist 3 ) .
+Each symbol is looked up using the n_name field, and if found, the
+corresponding n_type and n_value fields are filled in. These fields are set
+to 0 if the symbol is not found.
+.Pp
+The program
+.Xr kvm_mkdb 8
+builds a database from the running kernel's namelist.
+If the database matches the opened kernel,
+.Fn kvm_nlist
+uses it to speed lookups.
+.Sh RETURN VALUES
+The
+.Fn kvm_nlist
+function returns the number of invalid entries found.
+If the kernel symbol table was unreadable, -1 is returned.
+.Sh FILES
+.Bl -tag -width /var/db/kvm_kernel.db -compact
+.It Pa /var/db/kvm_kernel.db
+.El
+.Sh SEE ALSO
+.Xr kvm 3 ,
+.Xr kvm_close 3 ,
+.Xr kvm_getargv 3 ,
+.Xr kvm_getenvv 3 ,
+.Xr kvm_geterr 3 ,
+.Xr kvm_getprocs 3 ,
+.Xr kvm_open 3 ,
+.Xr kvm_openfiles 3 ,
+.Xr kvm_read 3 ,
+.Xr kvm_write 3 ,
+.Xr kvm_mkdb 8
diff --git a/lib/libkvm/kvm_open.3 b/lib/libkvm/kvm_open.3
new file mode 100644
index 0000000..b791708
--- /dev/null
+++ b/lib/libkvm/kvm_open.3
@@ -0,0 +1,186 @@
+.\" Copyright (c) 1992, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software developed by the Computer Systems
+.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+.\" BG 91-66 and contributed to Berkeley.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+.\"
+.\" @(#)kvm_open.3 8.3 (Berkeley) 4/19/94
+.\" $FreeBSD$
+.\"
+.Dd April 19, 1994
+.Dt KVM_OPEN 3
+.Os
+.Sh NAME
+.Nm kvm_open ,
+.Nm kvm_openfiles ,
+.Nm kvm_close
+.Nd initialize kernel virtual memory access
+.Sh SYNOPSIS
+.Fd #include <fcntl.h>
+.Fd #include <kvm.h>
+.br
+.Ft kvm_t *
+.Fn kvm_open "const char *execfile" "const char *corefile" "const char *swapfile" "int flags" "const char *errstr"
+.Ft kvm_t *
+.Fn kvm_openfiles "const char *execfile" "const char *corefile" "const char *swapfile" "int flags" "char *errbuf"
+.Ft int
+.Fn kvm_close "kvm_t *kd"
+.Sh DESCRIPTION
+The functions
+.Fn kvm_open
+and
+.Fn kvm_openfiles
+return a descriptor used to access kernel virtual memory
+via the
+.Xr kvm 3
+library routines. Both active kernels and crash dumps are accessible
+through this interface.
+.Pp
+.Fa execfile
+is the executable image of the kernel being examined.
+This file must contain a symbol table.
+If this argument is
+.Dv NULL ,
+the currently running system is assumed,
+as determined from
+.Xr getbootfile 3 .
+.Pp
+.Fa corefile
+is the kernel memory device file. It can be either /dev/mem
+or a crash dump core generated by
+.Xr savecore 8 .
+If
+.Fa corefile
+is
+.Dv NULL ,
+the default indicated by
+.Dv _PATH_MEM
+from <paths.h> is used.
+.Pp
+.Fa swapfile
+should indicate the swap device. If
+.Dv NULL ,
+.Dv _PATH_DRUM
+from <paths.h> is used.
+.Pp
+The
+.Fa flags
+argument indicates read/write access as in
+.Xr open 2
+and applies only to the core file.
+Only
+.Dv O_RDONLY ,
+.Dv O_WRONLY ,
+and
+.Dv O_RDWR
+are permitted.
+.Pp
+There are two open routines which differ only with respect to
+the error mechanism.
+One provides backward compatibility with the SunOS kvm library, while the
+other provides an improved error reporting framework.
+.Pp
+The
+.Fn kvm_open
+function is the Sun kvm compatible open call. Here, the
+.Fa errstr
+argument indicates how errors should be handled. If it is
+.Dv NULL ,
+no errors are reported and the application cannot know the
+specific nature of the failed kvm call.
+If it is not
+.Dv NULL ,
+errors are printed to stderr with
+.Fa errstr
+prepended to the message, as in
+.Xr perror 3 .
+Normally, the name of the program is used here.
+The string is assumed to persist at least until the corresponding
+.Fn kvm_close
+call.
+.Pp
+The
+.Fn kvm_openfiles
+function provides BSD style error reporting.
+Here, error messages are not printed out by the library.
+Instead, the application obtains the error message
+corresponding to the most recent kvm library call using
+.Fn kvm_geterr
+(see
+.Xr kvm_geterr 3 ).
+The results are undefined if the most recent kvm call did not produce
+an error.
+Since
+.Fn kvm_geterr
+requires a kvm descriptor, but the open routines return
+.Dv NULL
+on failure,
+.Fn kvm_geterr
+cannot be used to get the error message if open fails.
+Thus,
+.Fn kvm_openfiles
+will place any error message in the
+.Fa errbuf
+argument. This buffer should be _POSIX2_LINE_MAX characters large (from
+<limits.h>).
+.Sh RETURN VALUES
+The
+.Fn kvm_open
+and
+.Fn kvm_openfiles
+functions both return a descriptor to be used
+in all subsequent kvm library calls.
+The library is fully re-entrant.
+On failure,
+.Dv NULL
+is returned, in which case
+.Fn kvm_openfiles
+writes the error message into
+.Fa errbuf .
+.Pp
+The
+.Fn kvm_close
+function returns 0 on success and -1 on failure.
+.Sh BUGS
+There should not be two open calls. The ill-defined error semantics
+of the Sun library and the desire to have a backward-compatible library
+for BSD left little choice.
+.Sh SEE ALSO
+.Xr open 2 ,
+.Xr kvm 3 ,
+.Xr kvm_getargv 3 ,
+.Xr kvm_getenvv 3 ,
+.Xr kvm_geterr 3 ,
+.Xr kvm_getprocs 3 ,
+.Xr kvm_nlist 3 ,
+.Xr kvm_read 3 ,
+.Xr kvm_write 3
diff --git a/lib/libkvm/kvm_private.h b/lib/libkvm/kvm_private.h
new file mode 100644
index 0000000..2d6d07f
--- /dev/null
+++ b/lib/libkvm/kvm_private.h
@@ -0,0 +1,82 @@
+/*-
+ * Copyright (c) 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software developed by the Computer Systems
+ * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+ * BG 91-66 and contributed to Berkeley.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ *
+ * @(#)kvm_private.h 8.1 (Berkeley) 6/4/93
+ */
+
+struct __kvm {
+ /*
+ * a string to be prepended to error messages
+ * provided for compatibility with sun's interface
+ * if this value is null, errors are saved in errbuf[]
+ */
+ const char *program;
+ char *errp; /* XXX this can probably go away */
+ char errbuf[_POSIX2_LINE_MAX];
+ DB *db;
+#define ISALIVE(kd) ((kd)->vmfd >= 0)
+ int pmfd; /* physical memory file (or crashdump) */
+ int vmfd; /* virtual memory file (-1 if crashdump) */
+ int unused; /* was: swap file (e.g., /dev/drum) */
+ int nlfd; /* namelist file (e.g., /kernel) */
+ struct kinfo_proc *procbase;
+ char *argspc; /* (dynamic) storage for argv strings */
+ int arglen; /* length of the above */
+ char **argv; /* (dynamic) storage for argv pointers */
+ int argc; /* length of above (not actual # present) */
+ char *argbuf; /* (dynamic) temporary storage */
+ /*
+ * Kernel virtual address translation state. This only gets filled
+ * in for dead kernels; otherwise, the running kernel (i.e. kmem)
+ * will do the translations for us. It could be big, so we
+ * only allocate it if necessary.
+ */
+ struct vmstate *vmst;
+};
+
+/*
+ * Functions used internally by kvm, but across kvm modules.
+ */
+void _kvm_err __P((kvm_t *kd, const char *program, const char *fmt, ...));
+void _kvm_freeprocs __P((kvm_t *kd));
+void _kvm_freevtop __P((kvm_t *));
+int _kvm_initvtop __P((kvm_t *));
+int _kvm_kvatop __P((kvm_t *, u_long, u_long *));
+void *_kvm_malloc __P((kvm_t *kd, size_t));
+void *_kvm_realloc __P((kvm_t *kd, void *, size_t));
+void _kvm_syserr
+ __P((kvm_t *kd, const char *program, const char *fmt, ...));
+int _kvm_uvatop __P((kvm_t *, const struct proc *, u_long, u_long *));
diff --git a/lib/libkvm/kvm_proc.c b/lib/libkvm/kvm_proc.c
new file mode 100644
index 0000000..feba974
--- /dev/null
+++ b/lib/libkvm/kvm_proc.c
@@ -0,0 +1,784 @@
+/*-
+ * Copyright (c) 1989, 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software developed by the Computer Systems
+ * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+ * BG 91-66 and contributed to Berkeley.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ *
+ * $FreeBSD$
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
+#endif /* LIBC_SCCS and not lint */
+
+/*
+ * Proc traversal interface for kvm. ps and w are (probably) the exclusive
+ * users of this code, so we've factored it out into a separate module.
+ * Thus, we keep this grunge out of the other kvm applications (i.e.,
+ * most other applications are interested only in open/close/read/nlist).
+ */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/proc.h>
+#include <sys/exec.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/tty.h>
+#include <sys/file.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <nlist.h>
+#include <kvm.h>
+
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+#include <vm/swap_pager.h>
+
+#include <sys/sysctl.h>
+
+#include <limits.h>
+#include <memory.h>
+#include <db.h>
+#include <paths.h>
+
+#include "kvm_private.h"
+
+#if used
+static char *
+kvm_readswap(kd, p, va, cnt)
+ kvm_t *kd;
+ const struct proc *p;
+ u_long va;
+ u_long *cnt;
+{
+#ifdef __FreeBSD__
+ /* XXX Stubbed out, our vm system is differnet */
+ _kvm_err(kd, kd->program, "kvm_readswap not implemented");
+ return(0);
+#endif /* __FreeBSD__ */
+}
+#endif
+
+#define KREAD(kd, addr, obj) \
+ (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
+
+/*
+ * Read proc's from memory file into buffer bp, which has space to hold
+ * at most maxcnt procs.
+ */
+static int
+kvm_proclist(kd, what, arg, p, bp, maxcnt)
+ kvm_t *kd;
+ int what, arg;
+ struct proc *p;
+ struct kinfo_proc *bp;
+ int maxcnt;
+{
+ register int cnt = 0;
+ struct eproc eproc;
+ struct pgrp pgrp;
+ struct session sess;
+ struct tty tty;
+ struct proc proc;
+ struct proc pproc;
+
+ for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
+ if (KREAD(kd, (u_long)p, &proc)) {
+ _kvm_err(kd, kd->program, "can't read proc at %x", p);
+ return (-1);
+ }
+ if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0)
+ (void)(KREAD(kd, (u_long)eproc.e_pcred.pc_ucred,
+ &eproc.e_ucred));
+
+ switch(what) {
+
+ case KERN_PROC_PID:
+ if (proc.p_pid != (pid_t)arg)
+ continue;
+ break;
+
+ case KERN_PROC_UID:
+ if (eproc.e_ucred.cr_uid != (uid_t)arg)
+ continue;
+ break;
+
+ case KERN_PROC_RUID:
+ if (eproc.e_pcred.p_ruid != (uid_t)arg)
+ continue;
+ break;
+ }
+ /*
+ * We're going to add another proc to the set. If this
+ * will overflow the buffer, assume the reason is because
+ * nprocs (or the proc list) is corrupt and declare an error.
+ */
+ if (cnt >= maxcnt) {
+ _kvm_err(kd, kd->program, "nprocs corrupt");
+ return (-1);
+ }
+ /*
+ * gather eproc
+ */
+ eproc.e_paddr = p;
+ if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
+ _kvm_err(kd, kd->program, "can't read pgrp at %x",
+ proc.p_pgrp);
+ return (-1);
+ }
+ if (proc.p_oppid)
+ eproc.e_ppid = proc.p_oppid;
+ else if (proc.p_pptr) {
+ if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
+ _kvm_err(kd, kd->program, "can't read pproc at %x",
+ proc.p_pptr);
+ return (-1);
+ }
+ eproc.e_ppid = pproc.p_pid;
+ } else
+ eproc.e_ppid = 0;
+ eproc.e_sess = pgrp.pg_session;
+ eproc.e_pgid = pgrp.pg_id;
+ eproc.e_jobc = pgrp.pg_jobc;
+ if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
+ _kvm_err(kd, kd->program, "can't read session at %x",
+ pgrp.pg_session);
+ return (-1);
+ }
+ (void)memcpy(eproc.e_login, sess.s_login,
+ sizeof(eproc.e_login));
+ if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
+ if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
+ _kvm_err(kd, kd->program,
+ "can't read tty at %x", sess.s_ttyp);
+ return (-1);
+ }
+ eproc.e_tdev = tty.t_dev;
+ eproc.e_tsess = tty.t_session;
+ if (tty.t_pgrp != NULL) {
+ if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
+ _kvm_err(kd, kd->program,
+ "can't read tpgrp at &x",
+ tty.t_pgrp);
+ return (-1);
+ }
+ eproc.e_tpgid = pgrp.pg_id;
+ } else
+ eproc.e_tpgid = -1;
+ } else
+ eproc.e_tdev = NODEV;
+ eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
+ if (sess.s_leader == p)
+ eproc.e_flag |= EPROC_SLEADER;
+ if (proc.p_wmesg)
+ (void)kvm_read(kd, (u_long)proc.p_wmesg,
+ eproc.e_wmesg, WMESGLEN);
+
+#ifdef sparc
+ (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize,
+ (char *)&eproc.e_vm.vm_rssize,
+ sizeof(eproc.e_vm.vm_rssize));
+ (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize,
+ (char *)&eproc.e_vm.vm_tsize,
+ 3 * sizeof(eproc.e_vm.vm_rssize)); /* XXX */
+#else
+ (void)kvm_read(kd, (u_long)proc.p_vmspace,
+ (char *)&eproc.e_vm, sizeof(eproc.e_vm));
+#endif
+ eproc.e_xsize = eproc.e_xrssize = 0;
+ eproc.e_xccount = eproc.e_xswrss = 0;
+
+ switch (what) {
+
+ case KERN_PROC_PGRP:
+ if (eproc.e_pgid != (pid_t)arg)
+ continue;
+ break;
+
+ case KERN_PROC_TTY:
+ if ((proc.p_flag & P_CONTROLT) == 0 ||
+ eproc.e_tdev != (dev_t)arg)
+ continue;
+ break;
+ }
+ bcopy(&proc, &bp->kp_proc, sizeof(proc));
+ bcopy(&eproc, &bp->kp_eproc, sizeof(eproc));
+ ++bp;
+ ++cnt;
+ }
+ return (cnt);
+}
+
+/*
+ * Build proc info array by reading in proc list from a crash dump.
+ * Return number of procs read. maxcnt is the max we will read.
+ */
+static int
+kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
+ kvm_t *kd;
+ int what, arg;
+ u_long a_allproc;
+ u_long a_zombproc;
+ int maxcnt;
+{
+ register struct kinfo_proc *bp = kd->procbase;
+ register int acnt, zcnt;
+ struct proc *p;
+
+ if (KREAD(kd, a_allproc, &p)) {
+ _kvm_err(kd, kd->program, "cannot read allproc");
+ return (-1);
+ }
+ acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
+ if (acnt < 0)
+ return (acnt);
+
+ if (KREAD(kd, a_zombproc, &p)) {
+ _kvm_err(kd, kd->program, "cannot read zombproc");
+ return (-1);
+ }
+ zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
+ if (zcnt < 0)
+ zcnt = 0;
+
+ return (acnt + zcnt);
+}
+
+struct kinfo_proc *
+kvm_getprocs(kd, op, arg, cnt)
+ kvm_t *kd;
+ int op, arg;
+ int *cnt;
+{
+ int mib[4], st, nprocs;
+ size_t size;
+
+ if (kd->procbase != 0) {
+ free((void *)kd->procbase);
+ /*
+ * Clear this pointer in case this call fails. Otherwise,
+ * kvm_close() will free it again.
+ */
+ kd->procbase = 0;
+ }
+ if (ISALIVE(kd)) {
+ size = 0;
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC;
+ mib[2] = op;
+ mib[3] = arg;
+ st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0);
+ if (st == -1) {
+ _kvm_syserr(kd, kd->program, "kvm_getprocs");
+ return (0);
+ }
+ do {
+ size += size / 10;
+ kd->procbase = (struct kinfo_proc *)
+ _kvm_realloc(kd, kd->procbase, size);
+ if (kd->procbase == 0)
+ return (0);
+ st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4,
+ kd->procbase, &size, NULL, 0);
+ } while (st == -1 && errno == ENOMEM);
+ if (st == -1) {
+ _kvm_syserr(kd, kd->program, "kvm_getprocs");
+ return (0);
+ }
+ if (size % sizeof(struct kinfo_proc) != 0) {
+ _kvm_err(kd, kd->program,
+ "proc size mismatch (%d total, %d chunks)",
+ size, sizeof(struct kinfo_proc));
+ return (0);
+ }
+ nprocs = size / sizeof(struct kinfo_proc);
+ } else {
+ struct nlist nl[4], *p;
+
+ nl[0].n_name = "_nprocs";
+ nl[1].n_name = "_allproc";
+ nl[2].n_name = "_zombproc";
+ nl[3].n_name = 0;
+
+ if (kvm_nlist(kd, nl) != 0) {
+ for (p = nl; p->n_type != 0; ++p)
+ ;
+ _kvm_err(kd, kd->program,
+ "%s: no such symbol", p->n_name);
+ return (0);
+ }
+ if (KREAD(kd, nl[0].n_value, &nprocs)) {
+ _kvm_err(kd, kd->program, "can't read nprocs");
+ return (0);
+ }
+ size = nprocs * sizeof(struct kinfo_proc);
+ kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
+ if (kd->procbase == 0)
+ return (0);
+
+ nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
+ nl[2].n_value, nprocs);
+#ifdef notdef
+ size = nprocs * sizeof(struct kinfo_proc);
+ (void)realloc(kd->procbase, size);
+#endif
+ }
+ *cnt = nprocs;
+ return (kd->procbase);
+}
+
+void
+_kvm_freeprocs(kd)
+ kvm_t *kd;
+{
+ if (kd->procbase) {
+ free(kd->procbase);
+ kd->procbase = 0;
+ }
+}
+
+void *
+_kvm_realloc(kd, p, n)
+ kvm_t *kd;
+ void *p;
+ size_t n;
+{
+ void *np = (void *)realloc(p, n);
+
+ if (np == 0) {
+ free(p);
+ _kvm_err(kd, kd->program, "out of memory");
+ }
+ return (np);
+}
+
+#ifndef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
+/*
+ * Read in an argument vector from the user address space of process p.
+ * addr if the user-space base address of narg null-terminated contiguous
+ * strings. This is used to read in both the command arguments and
+ * environment strings. Read at most maxcnt characters of strings.
+ */
+static char **
+kvm_argv(kd, p, addr, narg, maxcnt)
+ kvm_t *kd;
+ const struct proc *p;
+ register u_long addr;
+ register int narg;
+ register int maxcnt;
+{
+ register char *np, *cp, *ep, *ap;
+ register u_long oaddr = -1;
+ register int len, cc;
+ register char **argv;
+
+ /*
+ * Check that there aren't an unreasonable number of agruments,
+ * and that the address is in user space.
+ */
+ if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
+ return (0);
+
+ /*
+ * kd->argv : work space for fetching the strings from the target
+ * process's space, and is converted for returning to caller
+ */
+ if (kd->argv == 0) {
+ /*
+ * Try to avoid reallocs.
+ */
+ kd->argc = MAX(narg + 1, 32);
+ kd->argv = (char **)_kvm_malloc(kd, kd->argc *
+ sizeof(*kd->argv));
+ if (kd->argv == 0)
+ return (0);
+ } else if (narg + 1 > kd->argc) {
+ kd->argc = MAX(2 * kd->argc, narg + 1);
+ kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
+ sizeof(*kd->argv));
+ if (kd->argv == 0)
+ return (0);
+ }
+ /*
+ * kd->argspc : returned to user, this is where the kd->argv
+ * arrays are left pointing to the collected strings.
+ */
+ if (kd->argspc == 0) {
+ kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
+ if (kd->argspc == 0)
+ return (0);
+ kd->arglen = PAGE_SIZE;
+ }
+ /*
+ * kd->argbuf : used to pull in pages from the target process.
+ * the strings are copied out of here.
+ */
+ if (kd->argbuf == 0) {
+ kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
+ if (kd->argbuf == 0)
+ return (0);
+ }
+
+ /* Pull in the target process'es argv vector */
+ cc = sizeof(char *) * narg;
+ if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc)
+ return (0);
+ /*
+ * ap : saved start address of string we're working on in kd->argspc
+ * np : pointer to next place to write in kd->argspc
+ * len: length of data in kd->argspc
+ * argv: pointer to the argv vector that we are hunting around the
+ * target process space for, and converting to addresses in
+ * our address space (kd->argspc).
+ */
+ ap = np = kd->argspc;
+ argv = kd->argv;
+ len = 0;
+ /*
+ * Loop over pages, filling in the argument vector.
+ * Note that the argv strings could be pointing *anywhere* in
+ * the user address space and are no longer contiguous.
+ * Note that *argv is modified when we are going to fetch a string
+ * that crosses a page boundary. We copy the next part of the string
+ * into to "np" and eventually convert the pointer.
+ */
+ while (argv < kd->argv + narg && *argv != 0) {
+
+ /* get the address that the current argv string is on */
+ addr = (u_long)*argv & ~(PAGE_SIZE - 1);
+
+ /* is it the same page as the last one? */
+ if (addr != oaddr) {
+ if (kvm_uread(kd, p, addr, kd->argbuf, PAGE_SIZE) !=
+ PAGE_SIZE)
+ return (0);
+ oaddr = addr;
+ }
+
+ /* offset within the page... kd->argbuf */
+ addr = (u_long)*argv & (PAGE_SIZE - 1);
+
+ /* cp = start of string, cc = count of chars in this chunk */
+ cp = kd->argbuf + addr;
+ cc = PAGE_SIZE - addr;
+
+ /* dont get more than asked for by user process */
+ if (maxcnt > 0 && cc > maxcnt - len)
+ cc = maxcnt - len;
+
+ /* pointer to end of string if we found it in this page */
+ ep = memchr(cp, '\0', cc);
+ if (ep != 0)
+ cc = ep - cp + 1;
+ /*
+ * at this point, cc is the count of the chars that we are
+ * going to retrieve this time. we may or may not have found
+ * the end of it. (ep points to the null if the end is known)
+ */
+
+ /* will we exceed the malloc/realloced buffer? */
+ if (len + cc > kd->arglen) {
+ register int off;
+ register char **pp;
+ register char *op = kd->argspc;
+
+ kd->arglen *= 2;
+ kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
+ kd->arglen);
+ if (kd->argspc == 0)
+ return (0);
+ /*
+ * Adjust argv pointers in case realloc moved
+ * the string space.
+ */
+ off = kd->argspc - op;
+ for (pp = kd->argv; pp < argv; pp++)
+ *pp += off;
+ ap += off;
+ np += off;
+ }
+ /* np = where to put the next part of the string in kd->argspc*/
+ /* np is kinda redundant.. could use "kd->argspc + len" */
+ memcpy(np, cp, cc);
+ np += cc; /* inc counters */
+ len += cc;
+
+ /*
+ * if end of string found, set the *argv pointer to the
+ * saved beginning of string, and advance. argv points to
+ * somewhere in kd->argv.. This is initially relative
+ * to the target process, but when we close it off, we set
+ * it to point in our address space.
+ */
+ if (ep != 0) {
+ *argv++ = ap;
+ ap = np;
+ } else {
+ /* update the address relative to the target process */
+ *argv += cc;
+ }
+
+ if (maxcnt > 0 && len >= maxcnt) {
+ /*
+ * We're stopping prematurely. Terminate the
+ * current string.
+ */
+ if (ep == 0) {
+ *np = '\0';
+ *argv++ = ap;
+ }
+ break;
+ }
+ }
+ /* Make sure argv is terminated. */
+ *argv = 0;
+ return (kd->argv);
+}
+
+static void
+ps_str_a(p, addr, n)
+ struct ps_strings *p;
+ u_long *addr;
+ int *n;
+{
+ *addr = (u_long)p->ps_argvstr;
+ *n = p->ps_nargvstr;
+}
+
+static void
+ps_str_e(p, addr, n)
+ struct ps_strings *p;
+ u_long *addr;
+ int *n;
+{
+ *addr = (u_long)p->ps_envstr;
+ *n = p->ps_nenvstr;
+}
+
+/*
+ * Determine if the proc indicated by p is still active.
+ * This test is not 100% foolproof in theory, but chances of
+ * being wrong are very low.
+ */
+static int
+proc_verify(kd, kernp, p)
+ kvm_t *kd;
+ u_long kernp;
+ const struct proc *p;
+{
+ struct kinfo_proc kp;
+ int mib[4];
+ size_t len;
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC;
+ mib[2] = KERN_PROC_PID;
+ mib[3] = p->p_pid;
+ len = sizeof(kp);
+ if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1)
+ return (0);
+ return (p->p_pid == kp.kp_proc.p_pid &&
+ (kp.kp_proc.p_stat != SZOMB || p->p_stat == SZOMB));
+}
+
+static char **
+kvm_doargv(kd, kp, nchr, info)
+ kvm_t *kd;
+ const struct kinfo_proc *kp;
+ int nchr;
+ void (*info)(struct ps_strings *, u_long *, int *);
+{
+ register const struct proc *p = &kp->kp_proc;
+ register char **ap;
+ u_long addr;
+ int cnt;
+ static struct ps_strings arginfo;
+ static u_long ps_strings;
+ size_t len;
+
+ if (ps_strings == NULL) {
+ len = sizeof(ps_strings);
+ if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
+ 0) == -1)
+ ps_strings = PS_STRINGS;
+ }
+
+ /*
+ * Pointers are stored at the top of the user stack.
+ */
+ if (p->p_stat == SZOMB ||
+ kvm_uread(kd, p, ps_strings, (char *)&arginfo,
+ sizeof(arginfo)) != sizeof(arginfo))
+ return (0);
+
+ (*info)(&arginfo, &addr, &cnt);
+ if (cnt == 0)
+ return (0);
+ ap = kvm_argv(kd, p, addr, cnt, nchr);
+ /*
+ * For live kernels, make sure this process didn't go away.
+ */
+ if (ap != 0 && ISALIVE(kd) &&
+ !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p))
+ ap = 0;
+ return (ap);
+}
+
+/*
+ * Get the command args. This code is now machine independent.
+ */
+char **
+kvm_getargv(kd, kp, nchr)
+ kvm_t *kd;
+ const struct kinfo_proc *kp;
+ int nchr;
+{
+ int oid[4];
+ int i, l;
+ static int buflen;
+ static char *buf, *p;
+ static char **bufp;
+ static int argc;
+
+ if (!buflen) {
+ l = sizeof(buflen);
+ i = sysctlbyname("kern.ps_arg_cache_limit",
+ &buflen, &l, NULL, 0);
+ if (i == -1) {
+ buflen == 0;
+ } else {
+ buf = malloc(buflen);
+ if (buf == NULL)
+ buflen = 0;
+ argc = 32;
+ bufp = malloc(sizeof(char *) * argc);
+ }
+ }
+ if (buf != NULL) {
+ oid[0] = CTL_KERN;
+ oid[1] = KERN_PROC;
+ oid[2] = KERN_PROC_ARGS;
+ oid[3] = kp->kp_proc.p_pid;
+ l = buflen;
+ i = sysctl(oid, 4, buf, &l, 0, 0);
+ if (i == 0 && l > 0) {
+ i = 0;
+ p = buf;
+ do {
+ bufp[i++] = p;
+ p += strlen(p) + 1;
+ if (i >= argc) {
+ argc += argc;
+ bufp = realloc(bufp,
+ sizeof(char *) * argc);
+ }
+ } while (p < buf + l);
+ bufp[i++] = 0;
+ return (bufp);
+ }
+ }
+ if (kp->kp_proc.p_flag & P_SYSTEM)
+ return (NULL);
+ return (kvm_doargv(kd, kp, nchr, ps_str_a));
+}
+
+char **
+kvm_getenvv(kd, kp, nchr)
+ kvm_t *kd;
+ const struct kinfo_proc *kp;
+ int nchr;
+{
+ return (kvm_doargv(kd, kp, nchr, ps_str_e));
+}
+
+/*
+ * Read from user space. The user context is given by p.
+ */
+ssize_t
+kvm_uread(kd, p, uva, buf, len)
+ kvm_t *kd;
+ register const struct proc *p;
+ register u_long uva;
+ register char *buf;
+ register size_t len;
+{
+ register char *cp;
+ char procfile[MAXPATHLEN];
+ ssize_t amount;
+ int fd;
+
+ if (!ISALIVE(kd)) {
+ _kvm_err(kd, kd->program,
+ "cannot read user space from dead kernel");
+ return (0);
+ }
+
+ sprintf(procfile, "/proc/%d/mem", p->p_pid);
+ fd = open(procfile, O_RDONLY, 0);
+ if (fd < 0) {
+ _kvm_err(kd, kd->program, "cannot open %s", procfile);
+ close(fd);
+ return (0);
+ }
+
+ cp = buf;
+ while (len > 0) {
+ errno = 0;
+ if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
+ _kvm_err(kd, kd->program, "invalid address (%x) in %s",
+ uva, procfile);
+ break;
+ }
+ amount = read(fd, cp, len);
+ if (amount < 0) {
+ _kvm_syserr(kd, kd->program, "error reading %s",
+ procfile);
+ break;
+ }
+ if (amount == 0) {
+ _kvm_err(kd, kd->program, "EOF reading %s", procfile);
+ break;
+ }
+ cp += amount;
+ uva += amount;
+ len -= amount;
+ }
+
+ close(fd);
+ return ((ssize_t)(cp - buf));
+}
diff --git a/lib/libkvm/kvm_read.3 b/lib/libkvm/kvm_read.3
new file mode 100644
index 0000000..f8d5e0a
--- /dev/null
+++ b/lib/libkvm/kvm_read.3
@@ -0,0 +1,93 @@
+.\" Copyright (c) 1992, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software developed by the Computer Systems
+.\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+.\" BG 91-66 and contributed to Berkeley.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+.\"
+.\" @(#)kvm_read.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
+.\"
+.Dd June 4, 1993
+.Dt KVM_READ 3
+.Os
+.Sh NAME
+.Nm kvm_read ,
+.Nm kvm_write
+.Nd read or write kernel virtual memory
+.Sh SYNOPSIS
+.Fd #include <kvm.h>
+.Ft ssize_t
+.Fn kvm_read "kvm_t *kd" "unsigned long addr" "void *buf" "size_t nbytes"
+.Ft ssize_t
+.Fn kvm_write "kvm_t *kd" "unsigned long addr" "const void *buf" "size_t nbytes"
+.Sh DESCRIPTION
+The
+.Fn kvm_read
+and
+.Fn kvm_write
+functions are used to read and write kernel virtual memory (or a crash
+dump file). See
+.Fn kvm_open 3
+or
+.Fn kvm_openfiles 3
+for information regarding opening kernel virtual memory and crash dumps.
+.Pp
+The
+.Fn kvm_read
+function transfers
+.Fa nbytes
+bytes of data from
+the kernel space address
+.Fa addr
+to
+.Fa buf .
+Conversely,
+.Fn kvm_write
+transfers data from
+.Fa buf
+to
+.Fa addr .
+Unlike their SunOS counterparts, these functions cannot be used to
+read or write process address spaces.
+.Sh RETURN VALUES
+Upon success, the number of bytes actually transferred is returned.
+Otherwise, -1 is returned.
+.Sh SEE ALSO
+.Xr kvm 3 ,
+.Xr kvm_close 3 ,
+.Xr kvm_getargv 3 ,
+.Xr kvm_getenvv 3 ,
+.Xr kvm_geterr 3 ,
+.Xr kvm_getprocs 3 ,
+.Xr kvm_nlist 3 ,
+.Xr kvm_open 3 ,
+.Xr kvm_openfiles 3
diff --git a/lib/libkvm/kvm_sparc.c b/lib/libkvm/kvm_sparc.c
new file mode 100644
index 0000000..a7b9594
--- /dev/null
+++ b/lib/libkvm/kvm_sparc.c
@@ -0,0 +1,236 @@
+/*-
+ * Copyright (c) 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software developed by the Computer Systems
+ * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
+ * BG 91-66 and contributed to Berkeley.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+
+/*
+ * Sparc machine dependent routines for kvm. Hopefully, the forthcoming
+ * vm code will one day obsolete this module.
+ */
+
+#include <sys/param.h>
+#include <sys/user.h>
+#include <sys/proc.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <nlist.h>
+#include <kvm.h>
+
+#include <vm/vm.h>
+#include <vm/vm_param.h>
+
+#include <limits.h>
+#include <db.h>
+
+#include "kvm_private.h"
+
+#define NPMEG 128
+
+/* XXX from sparc/pmap.c */
+#define MAXMEM (128 * 1024 * 1024) /* no more than 128 MB phys mem */
+#define NPGBANK 16 /* 2^4 pages per bank (64K / bank) */
+#define BSHIFT 4 /* log2(NPGBANK) */
+#define BOFFSET (NPGBANK - 1)
+#define BTSIZE (MAXMEM / NBPG / NPGBANK)
+#define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET))
+
+struct vmstate {
+ pmeg_t segmap[NKSEG];
+ int pmeg[NPMEG][NPTESG];
+ int pmap_stod[BTSIZE]; /* dense to sparse */
+};
+
+void
+_kvm_freevtop(kd)
+ kvm_t *kd;
+{
+ if (kd->vmst != 0)
+ free(kd->vmst);
+}
+
+int
+_kvm_initvtop(kd)
+ kvm_t *kd;
+{
+ register int i;
+ register int off;
+ register struct vmstate *vm;
+ struct stat st;
+ struct nlist nlist[2];
+
+ vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
+ if (vm == 0)
+ return (-1);
+
+ kd->vmst = vm;
+
+ if (fstat(kd->pmfd, &st) < 0)
+ return (-1);
+ /*
+ * Read segment table.
+ */
+ off = st.st_size - ctob(btoc(sizeof(vm->segmap)));
+ errno = 0;
+ if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
+ read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) {
+ _kvm_err(kd, kd->program, "cannot read segment map");
+ return (-1);
+ }
+ /*
+ * Read PMEGs.
+ */
+ off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) +
+ btoc(sizeof(vm->segmap)));
+ errno = 0;
+ if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 ||
+ read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) {
+ _kvm_err(kd, kd->program, "cannot read PMEG table");
+ return (-1);
+ }
+ /*
+ * Make pmap_stod be an identity map so we can bootstrap it in.
+ * We assume it's in the first contiguous chunk of physical memory.
+ */
+ for (i = 0; i < BTSIZE; ++i)
+ vm->pmap_stod[i] = i << 4;
+
+ /*
+ * It's okay to do this nlist separately from the one kvm_getprocs()
+ * does, since the only time we could gain anything by combining
+ * them is if we do a kvm_getprocs() on a dead kernel, which is
+ * not too common.
+ */
+ nlist[0].n_name = "_pmap_stod";
+ nlist[1].n_name = 0;
+ if (kvm_nlist(kd, nlist) != 0) {
+ _kvm_err(kd, kd->program, "pmap_stod: no such symbol");
+ return (-1);
+ }
+ if (kvm_read(kd, (u_long)nlist[0].n_value,
+ (char *)vm->pmap_stod, sizeof(vm->pmap_stod))
+ != sizeof(vm->pmap_stod)) {
+ _kvm_err(kd, kd->program, "cannot read pmap_stod");
+ return (-1);
+ }
+ return (0);
+}
+
+#define VA_OFF(va) (va & (NBPG - 1))
+
+/*
+ * Translate a user virtual address to a physical address.
+ */
+int
+_kvm_uvatop(kd, p, va, pa)
+ kvm_t *kd;
+ const struct proc *p;
+ u_long va;
+ u_long *pa;
+{
+ int kva, pte;
+ register int off, frame;
+ register struct vmspace *vms = p->p_vmspace;
+
+ if ((u_long)vms < KERNBASE) {
+ _kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc");
+ return (0);
+ }
+ if (va >= KERNBASE)
+ return (0);
+ /*
+ * Get the PTE. This takes two steps. We read the
+ * base address of the table, then we index it.
+ * Note that the index pte table is indexed by
+ * virtual segment rather than physical segment.
+ */
+ kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)];
+ if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0)
+ goto invalid;
+ kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va);
+ if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) {
+ off = VA_OFF(va);
+ /*
+ * /dev/mem adheres to the hardware model of physical memory
+ * (with holes in the address space), while crashdumps
+ * adhere to the contiguous software model.
+ */
+ if (ISALIVE(kd))
+ frame = pte & PG_PFNUM;
+ else
+ frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM);
+ *pa = (frame << PGSHIFT) | off;
+ return (NBPG - off);
+ }
+invalid:
+ _kvm_err(kd, 0, "invalid address (%x)", va);
+ return (0);
+}
+
+/*
+ * Translate a kernel virtual address to a physical address using the
+ * mapping information in kd->vm. Returns the result in pa, and returns
+ * the number of bytes that are contiguously available from this
+ * physical address. This routine is used only for crashdumps.
+ */
+int
+_kvm_kvatop(kd, va, pa)
+ kvm_t *kd;
+ u_long va;
+ u_long *pa;
+{
+ register struct vmstate *vm;
+ register int s;
+ register int pte;
+ register int off;
+
+ if (va >= KERNBASE) {
+ vm = kd->vmst;
+ s = vm->segmap[VA_VSEG(va) - NUSEG];
+ pte = vm->pmeg[s][VA_VPG(va)];
+ if ((pte & PG_V) != 0) {
+ off = VA_OFF(va);
+ *pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM)
+ << PGSHIFT) | off;
+
+ return (NBPG - off);
+ }
+ }
+ _kvm_err(kd, 0, "invalid address (%x)", va);
+ return (0);
+}
OpenPOWER on IntegriCloud