xref: /freebsd/lib/libkvm/kvm.c (revision 8771870c7642bc292585c2ce4f6504d7fc37fd36)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1989, 1992, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software developed by the Computer Systems
658f0484fSRodney W. Grimes  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
758f0484fSRodney W. Grimes  * BG 91-66 and contributed to Berkeley.
858f0484fSRodney W. Grimes  *
958f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1058f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1158f0484fSRodney W. Grimes  * are met:
1258f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1458f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1658f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1758f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1858f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1958f0484fSRodney W. Grimes  *	This product includes software developed by the University of
2058f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2158f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2258f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2358f0484fSRodney W. Grimes  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
3658f0484fSRodney W. Grimes  */
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
398771870cSPeter Wemm #if 0
4058f0484fSRodney W. Grimes static char sccsid[] = "@(#)kvm.c	8.2 (Berkeley) 2/13/94";
418771870cSPeter Wemm #else
428771870cSPeter Wemm static const char rcsid[] =
438771870cSPeter Wemm   "$FreeBSD$";
448771870cSPeter Wemm #endif
4558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes #include <sys/param.h>
4858f0484fSRodney W. Grimes #include <sys/user.h>
4958f0484fSRodney W. Grimes #include <sys/proc.h>
5058f0484fSRodney W. Grimes #include <sys/ioctl.h>
5158f0484fSRodney W. Grimes #include <sys/stat.h>
5258f0484fSRodney W. Grimes #include <sys/sysctl.h>
5358f0484fSRodney W. Grimes 
5458f0484fSRodney W. Grimes #include <vm/vm.h>
5558f0484fSRodney W. Grimes #include <vm/vm_param.h>
5658f0484fSRodney W. Grimes #include <vm/swap_pager.h>
5758f0484fSRodney W. Grimes 
5858f0484fSRodney W. Grimes #include <machine/vmparam.h>
5958f0484fSRodney W. Grimes 
6058f0484fSRodney W. Grimes #include <ctype.h>
6158f0484fSRodney W. Grimes #include <db.h>
6258f0484fSRodney W. Grimes #include <fcntl.h>
6358f0484fSRodney W. Grimes #include <kvm.h>
6458f0484fSRodney W. Grimes #include <limits.h>
6558f0484fSRodney W. Grimes #include <nlist.h>
6658f0484fSRodney W. Grimes #include <paths.h>
6758f0484fSRodney W. Grimes #include <stdio.h>
6858f0484fSRodney W. Grimes #include <stdlib.h>
6958f0484fSRodney W. Grimes #include <string.h>
7058f0484fSRodney W. Grimes #include <unistd.h>
7158f0484fSRodney W. Grimes 
7258f0484fSRodney W. Grimes #include "kvm_private.h"
7358f0484fSRodney W. Grimes 
7458f0484fSRodney W. Grimes static int kvm_dbopen __P((kvm_t *, const char *));
7558f0484fSRodney W. Grimes 
7658f0484fSRodney W. Grimes char *
7758f0484fSRodney W. Grimes kvm_geterr(kd)
7858f0484fSRodney W. Grimes 	kvm_t *kd;
7958f0484fSRodney W. Grimes {
8058f0484fSRodney W. Grimes 	return (kd->errbuf);
8158f0484fSRodney W. Grimes }
8258f0484fSRodney W. Grimes 
8358f0484fSRodney W. Grimes #if __STDC__
8458f0484fSRodney W. Grimes #include <stdarg.h>
8558f0484fSRodney W. Grimes #else
8658f0484fSRodney W. Grimes #include <varargs.h>
8758f0484fSRodney W. Grimes #endif
8858f0484fSRodney W. Grimes 
8958f0484fSRodney W. Grimes /*
9058f0484fSRodney W. Grimes  * Report an error using printf style arguments.  "program" is kd->program
9158f0484fSRodney W. Grimes  * on hard errors, and 0 on soft errors, so that under sun error emulation,
9258f0484fSRodney W. Grimes  * only hard errors are printed out (otherwise, programs like gdb will
9358f0484fSRodney W. Grimes  * generate tons of error messages when trying to access bogus pointers).
9458f0484fSRodney W. Grimes  */
9558f0484fSRodney W. Grimes void
9658f0484fSRodney W. Grimes #if __STDC__
9758f0484fSRodney W. Grimes _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
9858f0484fSRodney W. Grimes #else
9958f0484fSRodney W. Grimes _kvm_err(kd, program, fmt, va_alist)
10058f0484fSRodney W. Grimes 	kvm_t *kd;
10158f0484fSRodney W. Grimes 	char *program, *fmt;
10258f0484fSRodney W. Grimes 	va_dcl
10358f0484fSRodney W. Grimes #endif
10458f0484fSRodney W. Grimes {
10558f0484fSRodney W. Grimes 	va_list ap;
10658f0484fSRodney W. Grimes 
10758f0484fSRodney W. Grimes #ifdef __STDC__
10858f0484fSRodney W. Grimes 	va_start(ap, fmt);
10958f0484fSRodney W. Grimes #else
11058f0484fSRodney W. Grimes 	va_start(ap);
11158f0484fSRodney W. Grimes #endif
11258f0484fSRodney W. Grimes 	if (program != NULL) {
11358f0484fSRodney W. Grimes 		(void)fprintf(stderr, "%s: ", program);
11458f0484fSRodney W. Grimes 		(void)vfprintf(stderr, fmt, ap);
11558f0484fSRodney W. Grimes 		(void)fputc('\n', stderr);
11658f0484fSRodney W. Grimes 	} else
11758f0484fSRodney W. Grimes 		(void)vsnprintf(kd->errbuf,
11858f0484fSRodney W. Grimes 		    sizeof(kd->errbuf), (char *)fmt, ap);
11958f0484fSRodney W. Grimes 
12058f0484fSRodney W. Grimes 	va_end(ap);
12158f0484fSRodney W. Grimes }
12258f0484fSRodney W. Grimes 
12358f0484fSRodney W. Grimes void
12458f0484fSRodney W. Grimes #if __STDC__
12558f0484fSRodney W. Grimes _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
12658f0484fSRodney W. Grimes #else
12758f0484fSRodney W. Grimes _kvm_syserr(kd, program, fmt, va_alist)
12858f0484fSRodney W. Grimes 	kvm_t *kd;
12958f0484fSRodney W. Grimes 	char *program, *fmt;
13058f0484fSRodney W. Grimes 	va_dcl
13158f0484fSRodney W. Grimes #endif
13258f0484fSRodney W. Grimes {
13358f0484fSRodney W. Grimes 	va_list ap;
13458f0484fSRodney W. Grimes 	register int n;
13558f0484fSRodney W. Grimes 
13658f0484fSRodney W. Grimes #if __STDC__
13758f0484fSRodney W. Grimes 	va_start(ap, fmt);
13858f0484fSRodney W. Grimes #else
13958f0484fSRodney W. Grimes 	va_start(ap);
14058f0484fSRodney W. Grimes #endif
14158f0484fSRodney W. Grimes 	if (program != NULL) {
14258f0484fSRodney W. Grimes 		(void)fprintf(stderr, "%s: ", program);
14358f0484fSRodney W. Grimes 		(void)vfprintf(stderr, fmt, ap);
14458f0484fSRodney W. Grimes 		(void)fprintf(stderr, ": %s\n", strerror(errno));
14558f0484fSRodney W. Grimes 	} else {
14658f0484fSRodney W. Grimes 		register char *cp = kd->errbuf;
14758f0484fSRodney W. Grimes 
14858f0484fSRodney W. Grimes 		(void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
14958f0484fSRodney W. Grimes 		n = strlen(cp);
15058f0484fSRodney W. Grimes 		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
15158f0484fSRodney W. Grimes 		    strerror(errno));
15258f0484fSRodney W. Grimes 	}
15358f0484fSRodney W. Grimes 	va_end(ap);
15458f0484fSRodney W. Grimes }
15558f0484fSRodney W. Grimes 
15658f0484fSRodney W. Grimes void *
15758f0484fSRodney W. Grimes _kvm_malloc(kd, n)
15858f0484fSRodney W. Grimes 	register kvm_t *kd;
15958f0484fSRodney W. Grimes 	register size_t n;
16058f0484fSRodney W. Grimes {
16158f0484fSRodney W. Grimes 	void *p;
16258f0484fSRodney W. Grimes 
163d7b100f9SAndrey A. Chernov 	if ((p = calloc(n, sizeof(char))) == NULL)
164d7b100f9SAndrey A. Chernov 		_kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
165d7b100f9SAndrey A. Chernov 			 n, strerror(errno));
16658f0484fSRodney W. Grimes 	return (p);
16758f0484fSRodney W. Grimes }
16858f0484fSRodney W. Grimes 
16958f0484fSRodney W. Grimes static kvm_t *
1708771870cSPeter Wemm _kvm_open(kd, uf, mf, flag, errout)
17158f0484fSRodney W. Grimes 	register kvm_t *kd;
17258f0484fSRodney W. Grimes 	const char *uf;
17358f0484fSRodney W. Grimes 	const char *mf;
17458f0484fSRodney W. Grimes 	int flag;
17558f0484fSRodney W. Grimes 	char *errout;
17658f0484fSRodney W. Grimes {
17758f0484fSRodney W. Grimes 	struct stat st;
17858f0484fSRodney W. Grimes 
17958f0484fSRodney W. Grimes 	kd->vmfd = -1;
18058f0484fSRodney W. Grimes 	kd->pmfd = -1;
18158f0484fSRodney W. Grimes 	kd->nlfd = -1;
18258f0484fSRodney W. Grimes 	kd->vmst = 0;
18358f0484fSRodney W. Grimes 	kd->db = 0;
18458f0484fSRodney W. Grimes 	kd->procbase = 0;
18558f0484fSRodney W. Grimes 	kd->argspc = 0;
18658f0484fSRodney W. Grimes 	kd->argv = 0;
18758f0484fSRodney W. Grimes 
18858f0484fSRodney W. Grimes 	if (uf == 0)
1894be4929cSGarrett Wollman 		uf = getbootfile();
19058f0484fSRodney W. Grimes 	else if (strlen(uf) >= MAXPATHLEN) {
19158f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "exec file name too long");
19258f0484fSRodney W. Grimes 		goto failed;
19358f0484fSRodney W. Grimes 	}
19458f0484fSRodney W. Grimes 	if (flag & ~O_RDWR) {
19558f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "bad flags arg");
19658f0484fSRodney W. Grimes 		goto failed;
19758f0484fSRodney W. Grimes 	}
19858f0484fSRodney W. Grimes 	if (mf == 0)
19958f0484fSRodney W. Grimes 		mf = _PATH_MEM;
20058f0484fSRodney W. Grimes 
20158f0484fSRodney W. Grimes 	if ((kd->pmfd = open(mf, flag, 0)) < 0) {
20258f0484fSRodney W. Grimes 		_kvm_syserr(kd, kd->program, "%s", mf);
20358f0484fSRodney W. Grimes 		goto failed;
20458f0484fSRodney W. Grimes 	}
20558f0484fSRodney W. Grimes 	if (fstat(kd->pmfd, &st) < 0) {
20658f0484fSRodney W. Grimes 		_kvm_syserr(kd, kd->program, "%s", mf);
20758f0484fSRodney W. Grimes 		goto failed;
20858f0484fSRodney W. Grimes 	}
20958f0484fSRodney W. Grimes 	if (S_ISCHR(st.st_mode)) {
21058f0484fSRodney W. Grimes 		/*
21158f0484fSRodney W. Grimes 		 * If this is a character special device, then check that
21258f0484fSRodney W. Grimes 		 * it's /dev/mem.  If so, open kmem too.  (Maybe we should
21358f0484fSRodney W. Grimes 		 * make it work for either /dev/mem or /dev/kmem -- in either
21458f0484fSRodney W. Grimes 		 * case you're working with a live kernel.)
21558f0484fSRodney W. Grimes 		 */
21635e6b695SPoul-Henning Kamp 		if (strcmp(mf, _PATH_DEVNULL) == 0) {
21735e6b695SPoul-Henning Kamp 			kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
21835e6b695SPoul-Henning Kamp 		} else if (strcmp(mf, _PATH_MEM) != 0) {
21958f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
22058f0484fSRodney W. Grimes 				 "%s: not physical memory device", mf);
22158f0484fSRodney W. Grimes 			goto failed;
22235e6b695SPoul-Henning Kamp 		} else {
22358f0484fSRodney W. Grimes 			if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
22458f0484fSRodney W. Grimes 				_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
22558f0484fSRodney W. Grimes 				goto failed;
22658f0484fSRodney W. Grimes 			}
22735e6b695SPoul-Henning Kamp 		}
22858f0484fSRodney W. Grimes 		/*
22958f0484fSRodney W. Grimes 		 * Open kvm nlist database.  We go ahead and do this
230b3bfc719SDavid Greenman 		 * here so that we don't have to hold on to the kernel
23158f0484fSRodney W. Grimes 		 * path name.  Since a kvm application will surely do
23258f0484fSRodney W. Grimes 		 * a kvm_nlist(), this probably won't be a wasted effort.
23358f0484fSRodney W. Grimes 		 * If the database cannot be opened, open the namelist
23458f0484fSRodney W. Grimes 		 * argument so we revert to slow nlist() calls.
23558f0484fSRodney W. Grimes 		 */
23658f0484fSRodney W. Grimes 		if (kvm_dbopen(kd, uf) < 0 &&
23758f0484fSRodney W. Grimes 		    (kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
23858f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "%s", uf);
23958f0484fSRodney W. Grimes 			goto failed;
24058f0484fSRodney W. Grimes 		}
24158f0484fSRodney W. Grimes 	} else {
24258f0484fSRodney W. Grimes 		/*
24358f0484fSRodney W. Grimes 		 * This is a crash dump.
24435e6b695SPoul-Henning Kamp 		 * Initialize the virtual address translation machinery,
24558f0484fSRodney W. Grimes 		 * but first setup the namelist fd.
24658f0484fSRodney W. Grimes 		 */
24758f0484fSRodney W. Grimes 		if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
24858f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "%s", uf);
24958f0484fSRodney W. Grimes 			goto failed;
25058f0484fSRodney W. Grimes 		}
25158f0484fSRodney W. Grimes 		if (_kvm_initvtop(kd) < 0)
25258f0484fSRodney W. Grimes 			goto failed;
25358f0484fSRodney W. Grimes 	}
25458f0484fSRodney W. Grimes 	return (kd);
25558f0484fSRodney W. Grimes failed:
25658f0484fSRodney W. Grimes 	/*
25758f0484fSRodney W. Grimes 	 * Copy out the error if doing sane error semantics.
25858f0484fSRodney W. Grimes 	 */
25958f0484fSRodney W. Grimes 	if (errout != 0)
26058f0484fSRodney W. Grimes 		strcpy(errout, kd->errbuf);
26158f0484fSRodney W. Grimes 	(void)kvm_close(kd);
26258f0484fSRodney W. Grimes 	return (0);
26358f0484fSRodney W. Grimes }
26458f0484fSRodney W. Grimes 
26558f0484fSRodney W. Grimes kvm_t *
26658f0484fSRodney W. Grimes kvm_openfiles(uf, mf, sf, flag, errout)
26758f0484fSRodney W. Grimes 	const char *uf;
26858f0484fSRodney W. Grimes 	const char *mf;
26958f0484fSRodney W. Grimes 	const char *sf;
27058f0484fSRodney W. Grimes 	int flag;
27158f0484fSRodney W. Grimes 	char *errout;
27258f0484fSRodney W. Grimes {
27358f0484fSRodney W. Grimes 	register kvm_t *kd;
27458f0484fSRodney W. Grimes 
27558f0484fSRodney W. Grimes 	if ((kd = malloc(sizeof(*kd))) == NULL) {
27658f0484fSRodney W. Grimes 		(void)strcpy(errout, strerror(errno));
27758f0484fSRodney W. Grimes 		return (0);
27858f0484fSRodney W. Grimes 	}
279a52219bbSPeter Wemm 	memset(kd, 0, sizeof(*kd));
28058f0484fSRodney W. Grimes 	kd->program = 0;
2818771870cSPeter Wemm 	return (_kvm_open(kd, uf, mf, flag, errout));
28258f0484fSRodney W. Grimes }
28358f0484fSRodney W. Grimes 
28458f0484fSRodney W. Grimes kvm_t *
28512eaa3d5SPoul-Henning Kamp kvm_open(uf, mf, sf, flag, errstr)
28658f0484fSRodney W. Grimes 	const char *uf;
28758f0484fSRodney W. Grimes 	const char *mf;
28858f0484fSRodney W. Grimes 	const char *sf;
28958f0484fSRodney W. Grimes 	int flag;
29012eaa3d5SPoul-Henning Kamp 	const char *errstr;
29158f0484fSRodney W. Grimes {
29258f0484fSRodney W. Grimes 	register kvm_t *kd;
29358f0484fSRodney W. Grimes 
29412eaa3d5SPoul-Henning Kamp 	if ((kd = malloc(sizeof(*kd))) == NULL) {
29564f14011SBruce Evans 		if (errstr != NULL)
29612eaa3d5SPoul-Henning Kamp 			(void)fprintf(stderr, "%s: %s\n",
29764f14011SBruce Evans 				      errstr, strerror(errno));
29858f0484fSRodney W. Grimes 		return (0);
29958f0484fSRodney W. Grimes 	}
300a52219bbSPeter Wemm 	memset(kd, 0, sizeof(*kd));
30112eaa3d5SPoul-Henning Kamp 	kd->program = errstr;
3028771870cSPeter Wemm 	return (_kvm_open(kd, uf, mf, flag, NULL));
30358f0484fSRodney W. Grimes }
30458f0484fSRodney W. Grimes 
30558f0484fSRodney W. Grimes int
30658f0484fSRodney W. Grimes kvm_close(kd)
30758f0484fSRodney W. Grimes 	kvm_t *kd;
30858f0484fSRodney W. Grimes {
30958f0484fSRodney W. Grimes 	register int error = 0;
31058f0484fSRodney W. Grimes 
31158f0484fSRodney W. Grimes 	if (kd->pmfd >= 0)
31258f0484fSRodney W. Grimes 		error |= close(kd->pmfd);
31358f0484fSRodney W. Grimes 	if (kd->vmfd >= 0)
31458f0484fSRodney W. Grimes 		error |= close(kd->vmfd);
31558f0484fSRodney W. Grimes 	if (kd->nlfd >= 0)
31658f0484fSRodney W. Grimes 		error |= close(kd->nlfd);
31758f0484fSRodney W. Grimes 	if (kd->db != 0)
31858f0484fSRodney W. Grimes 		error |= (kd->db->close)(kd->db);
31958f0484fSRodney W. Grimes 	if (kd->vmst)
32058f0484fSRodney W. Grimes 		_kvm_freevtop(kd);
32158f0484fSRodney W. Grimes 	if (kd->procbase != 0)
32258f0484fSRodney W. Grimes 		free((void *)kd->procbase);
32358f0484fSRodney W. Grimes 	if (kd->argv != 0)
32458f0484fSRodney W. Grimes 		free((void *)kd->argv);
32558f0484fSRodney W. Grimes 	free((void *)kd);
32658f0484fSRodney W. Grimes 
32758f0484fSRodney W. Grimes 	return (0);
32858f0484fSRodney W. Grimes }
32958f0484fSRodney W. Grimes 
33058f0484fSRodney W. Grimes /*
33158f0484fSRodney W. Grimes  * Set up state necessary to do queries on the kernel namelist
33258f0484fSRodney W. Grimes  * data base.  If the data base is out-of-data/incompatible with
33358f0484fSRodney W. Grimes  * given executable, set up things so we revert to standard nlist call.
33458f0484fSRodney W. Grimes  * Only called for live kernels.  Return 0 on success, -1 on failure.
33558f0484fSRodney W. Grimes  */
33658f0484fSRodney W. Grimes static int
33758f0484fSRodney W. Grimes kvm_dbopen(kd, uf)
33858f0484fSRodney W. Grimes 	kvm_t *kd;
33958f0484fSRodney W. Grimes 	const char *uf;
34058f0484fSRodney W. Grimes {
34158f0484fSRodney W. Grimes 	char *cp;
34258f0484fSRodney W. Grimes 	DBT rec;
34358f0484fSRodney W. Grimes 	int dbversionlen;
34458f0484fSRodney W. Grimes 	struct nlist nitem;
34558f0484fSRodney W. Grimes 	char dbversion[_POSIX2_LINE_MAX];
34658f0484fSRodney W. Grimes 	char kversion[_POSIX2_LINE_MAX];
34758f0484fSRodney W. Grimes 	char dbname[MAXPATHLEN];
34858f0484fSRodney W. Grimes 
34958f0484fSRodney W. Grimes 	if ((cp = rindex(uf, '/')) != 0)
35058f0484fSRodney W. Grimes 		uf = cp + 1;
35158f0484fSRodney W. Grimes 
35258f0484fSRodney W. Grimes 	(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", _PATH_VARDB, uf);
35358f0484fSRodney W. Grimes 	kd->db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL);
35458f0484fSRodney W. Grimes 	if (kd->db == 0)
35558f0484fSRodney W. Grimes 		return (-1);
35658f0484fSRodney W. Grimes 	/*
35758f0484fSRodney W. Grimes 	 * read version out of database
35858f0484fSRodney W. Grimes 	 */
35958f0484fSRodney W. Grimes 	rec.data = VRS_KEY;
36058f0484fSRodney W. Grimes 	rec.size = sizeof(VRS_KEY) - 1;
36158f0484fSRodney W. Grimes 	if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
36258f0484fSRodney W. Grimes 		goto close;
36358f0484fSRodney W. Grimes 	if (rec.data == 0 || rec.size > sizeof(dbversion))
36458f0484fSRodney W. Grimes 		goto close;
36558f0484fSRodney W. Grimes 
36658f0484fSRodney W. Grimes 	bcopy(rec.data, dbversion, rec.size);
36758f0484fSRodney W. Grimes 	dbversionlen = rec.size;
36858f0484fSRodney W. Grimes 	/*
36958f0484fSRodney W. Grimes 	 * Read version string from kernel memory.
37058f0484fSRodney W. Grimes 	 * Since we are dealing with a live kernel, we can call kvm_read()
37158f0484fSRodney W. Grimes 	 * at this point.
37258f0484fSRodney W. Grimes 	 */
37358f0484fSRodney W. Grimes 	rec.data = VRS_SYM;
37458f0484fSRodney W. Grimes 	rec.size = sizeof(VRS_SYM) - 1;
37558f0484fSRodney W. Grimes 	if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
37658f0484fSRodney W. Grimes 		goto close;
37758f0484fSRodney W. Grimes 	if (rec.data == 0 || rec.size != sizeof(struct nlist))
37858f0484fSRodney W. Grimes 		goto close;
37958f0484fSRodney W. Grimes 	bcopy((char *)rec.data, (char *)&nitem, sizeof(nitem));
38058f0484fSRodney W. Grimes 	if (kvm_read(kd, (u_long)nitem.n_value, kversion, dbversionlen) !=
38158f0484fSRodney W. Grimes 	    dbversionlen)
38258f0484fSRodney W. Grimes 		goto close;
38358f0484fSRodney W. Grimes 	/*
38458f0484fSRodney W. Grimes 	 * If they match, we win - otherwise clear out kd->db so
38558f0484fSRodney W. Grimes 	 * we revert to slow nlist().
38658f0484fSRodney W. Grimes 	 */
38758f0484fSRodney W. Grimes 	if (bcmp(dbversion, kversion, dbversionlen) == 0)
38858f0484fSRodney W. Grimes 		return (0);
38958f0484fSRodney W. Grimes close:
39058f0484fSRodney W. Grimes 	(void)(kd->db->close)(kd->db);
39158f0484fSRodney W. Grimes 	kd->db = 0;
39258f0484fSRodney W. Grimes 
39358f0484fSRodney W. Grimes 	return (-1);
39458f0484fSRodney W. Grimes }
39558f0484fSRodney W. Grimes 
39658f0484fSRodney W. Grimes int
39758f0484fSRodney W. Grimes kvm_nlist(kd, nl)
39858f0484fSRodney W. Grimes 	kvm_t *kd;
39958f0484fSRodney W. Grimes 	struct nlist *nl;
40058f0484fSRodney W. Grimes {
40158f0484fSRodney W. Grimes 	register struct nlist *p;
40258f0484fSRodney W. Grimes 	register int nvalid;
40358f0484fSRodney W. Grimes 
40458f0484fSRodney W. Grimes 	/*
40558f0484fSRodney W. Grimes 	 * If we can't use the data base, revert to the
40658f0484fSRodney W. Grimes 	 * slow library call.
40758f0484fSRodney W. Grimes 	 */
40858f0484fSRodney W. Grimes 	if (kd->db == 0)
40958f0484fSRodney W. Grimes 		return (__fdnlist(kd->nlfd, nl));
41058f0484fSRodney W. Grimes 
41158f0484fSRodney W. Grimes 	/*
41258f0484fSRodney W. Grimes 	 * We can use the kvm data base.  Go through each nlist entry
41358f0484fSRodney W. Grimes 	 * and look it up with a db query.
41458f0484fSRodney W. Grimes 	 */
41558f0484fSRodney W. Grimes 	nvalid = 0;
41658f0484fSRodney W. Grimes 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
41758f0484fSRodney W. Grimes 		register int len;
41858f0484fSRodney W. Grimes 		DBT rec;
41958f0484fSRodney W. Grimes 
42058f0484fSRodney W. Grimes 		if ((len = strlen(p->n_name)) > 4096) {
42158f0484fSRodney W. Grimes 			/* sanity */
42258f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "symbol too large");
42358f0484fSRodney W. Grimes 			return (-1);
42458f0484fSRodney W. Grimes 		}
42558f0484fSRodney W. Grimes 		rec.data = p->n_name;
42658f0484fSRodney W. Grimes 		rec.size = len;
42758f0484fSRodney W. Grimes 		if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
42858f0484fSRodney W. Grimes 			continue;
42958f0484fSRodney W. Grimes 		if (rec.data == 0 || rec.size != sizeof(struct nlist))
43058f0484fSRodney W. Grimes 			continue;
43158f0484fSRodney W. Grimes 		++nvalid;
43258f0484fSRodney W. Grimes 		/*
43358f0484fSRodney W. Grimes 		 * Avoid alignment issues.
43458f0484fSRodney W. Grimes 		 */
43558f0484fSRodney W. Grimes 		bcopy((char *)&((struct nlist *)rec.data)->n_type,
43658f0484fSRodney W. Grimes 		      (char *)&p->n_type,
43758f0484fSRodney W. Grimes 		      sizeof(p->n_type));
43858f0484fSRodney W. Grimes 		bcopy((char *)&((struct nlist *)rec.data)->n_value,
43958f0484fSRodney W. Grimes 		      (char *)&p->n_value,
44058f0484fSRodney W. Grimes 		      sizeof(p->n_value));
44158f0484fSRodney W. Grimes 	}
44258f0484fSRodney W. Grimes 	/*
44358f0484fSRodney W. Grimes 	 * Return the number of entries that weren't found.
44458f0484fSRodney W. Grimes 	 */
44558f0484fSRodney W. Grimes 	return ((p - nl) - nvalid);
44658f0484fSRodney W. Grimes }
44758f0484fSRodney W. Grimes 
44858f0484fSRodney W. Grimes ssize_t
44958f0484fSRodney W. Grimes kvm_read(kd, kva, buf, len)
45058f0484fSRodney W. Grimes 	kvm_t *kd;
45158f0484fSRodney W. Grimes 	register u_long kva;
45258f0484fSRodney W. Grimes 	register void *buf;
45358f0484fSRodney W. Grimes 	register size_t len;
45458f0484fSRodney W. Grimes {
45558f0484fSRodney W. Grimes 	register int cc;
45658f0484fSRodney W. Grimes 	register void *cp;
45758f0484fSRodney W. Grimes 
45858f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
45958f0484fSRodney W. Grimes 		/*
46058f0484fSRodney W. Grimes 		 * We're using /dev/kmem.  Just read straight from the
46158f0484fSRodney W. Grimes 		 * device and let the active kernel do the address translation.
46258f0484fSRodney W. Grimes 		 */
46358f0484fSRodney W. Grimes 		errno = 0;
46458f0484fSRodney W. Grimes 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
46558f0484fSRodney W. Grimes 			_kvm_err(kd, 0, "invalid address (%x)", kva);
46658f0484fSRodney W. Grimes 			return (0);
46758f0484fSRodney W. Grimes 		}
46858f0484fSRodney W. Grimes 		cc = read(kd->vmfd, buf, len);
46958f0484fSRodney W. Grimes 		if (cc < 0) {
47058f0484fSRodney W. Grimes 			_kvm_syserr(kd, 0, "kvm_read");
47158f0484fSRodney W. Grimes 			return (0);
47258f0484fSRodney W. Grimes 		} else if (cc < len)
47358f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "short read");
47458f0484fSRodney W. Grimes 		return (cc);
47558f0484fSRodney W. Grimes 	} else {
47658f0484fSRodney W. Grimes 		cp = buf;
47758f0484fSRodney W. Grimes 		while (len > 0) {
47858f0484fSRodney W. Grimes 			u_long pa;
47958f0484fSRodney W. Grimes 
48058f0484fSRodney W. Grimes 			cc = _kvm_kvatop(kd, kva, &pa);
48158f0484fSRodney W. Grimes 			if (cc == 0)
48258f0484fSRodney W. Grimes 				return (0);
48358f0484fSRodney W. Grimes 			if (cc > len)
48458f0484fSRodney W. Grimes 				cc = len;
48558f0484fSRodney W. Grimes 			errno = 0;
48658f0484fSRodney W. Grimes 			if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
48758f0484fSRodney W. Grimes 				_kvm_syserr(kd, 0, _PATH_MEM);
48858f0484fSRodney W. Grimes 				break;
48958f0484fSRodney W. Grimes 			}
49058f0484fSRodney W. Grimes 			cc = read(kd->pmfd, cp, cc);
49158f0484fSRodney W. Grimes 			if (cc < 0) {
49258f0484fSRodney W. Grimes 				_kvm_syserr(kd, kd->program, "kvm_read");
49358f0484fSRodney W. Grimes 				break;
49458f0484fSRodney W. Grimes 			}
49558f0484fSRodney W. Grimes 			/*
49658f0484fSRodney W. Grimes 			 * If kvm_kvatop returns a bogus value or our core
49758f0484fSRodney W. Grimes 			 * file is truncated, we might wind up seeking beyond
49858f0484fSRodney W. Grimes 			 * the end of the core file in which case the read will
49958f0484fSRodney W. Grimes 			 * return 0 (EOF).
50058f0484fSRodney W. Grimes 			 */
50158f0484fSRodney W. Grimes 			if (cc == 0)
50258f0484fSRodney W. Grimes 				break;
50358f0484fSRodney W. Grimes 			(char *)cp += cc;
50458f0484fSRodney W. Grimes 			kva += cc;
50558f0484fSRodney W. Grimes 			len -= cc;
50658f0484fSRodney W. Grimes 		}
50758f0484fSRodney W. Grimes 		return ((char *)cp - (char *)buf);
50858f0484fSRodney W. Grimes 	}
50958f0484fSRodney W. Grimes 	/* NOTREACHED */
51058f0484fSRodney W. Grimes }
51158f0484fSRodney W. Grimes 
51258f0484fSRodney W. Grimes ssize_t
51358f0484fSRodney W. Grimes kvm_write(kd, kva, buf, len)
51458f0484fSRodney W. Grimes 	kvm_t *kd;
51558f0484fSRodney W. Grimes 	register u_long kva;
51658f0484fSRodney W. Grimes 	register const void *buf;
51758f0484fSRodney W. Grimes 	register size_t len;
51858f0484fSRodney W. Grimes {
51958f0484fSRodney W. Grimes 	register int cc;
52058f0484fSRodney W. Grimes 
52158f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
52258f0484fSRodney W. Grimes 		/*
52358f0484fSRodney W. Grimes 		 * Just like kvm_read, only we write.
52458f0484fSRodney W. Grimes 		 */
52558f0484fSRodney W. Grimes 		errno = 0;
52658f0484fSRodney W. Grimes 		if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
52758f0484fSRodney W. Grimes 			_kvm_err(kd, 0, "invalid address (%x)", kva);
52858f0484fSRodney W. Grimes 			return (0);
52958f0484fSRodney W. Grimes 		}
53058f0484fSRodney W. Grimes 		cc = write(kd->vmfd, buf, len);
53158f0484fSRodney W. Grimes 		if (cc < 0) {
53258f0484fSRodney W. Grimes 			_kvm_syserr(kd, 0, "kvm_write");
53358f0484fSRodney W. Grimes 			return (0);
53458f0484fSRodney W. Grimes 		} else if (cc < len)
53558f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "short write");
53658f0484fSRodney W. Grimes 		return (cc);
53758f0484fSRodney W. Grimes 	} else {
53858f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program,
53958f0484fSRodney W. Grimes 		    "kvm_write not implemented for dead kernels");
54058f0484fSRodney W. Grimes 		return (0);
54158f0484fSRodney W. Grimes 	}
54258f0484fSRodney W. Grimes 	/* NOTREACHED */
54358f0484fSRodney W. Grimes }
544