Home | History | Annotate | Line # | Download | only in libkvm
kvm.c revision 1.41
      1 /*-
      2  * Copyright (c) 1989, 1992, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software developed by the Computer Systems
      6  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
      7  * BG 91-66 and contributed to Berkeley.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #if defined(LIBC_SCCS) && !defined(lint)
     39 static char sccsid[] = "@(#)kvm.c	8.2 (Berkeley) 2/13/94";
     40 #endif /* LIBC_SCCS and not lint */
     41 
     42 #include <sys/param.h>
     43 #include <sys/user.h>
     44 #include <sys/proc.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/stat.h>
     47 #include <sys/sysctl.h>
     48 
     49 #include <sys/core.h>
     50 #include <sys/exec_aout.h>
     51 #include <sys/kcore.h>
     52 
     53 #include <vm/vm.h>
     54 #include <vm/vm_param.h>
     55 #include <vm/swap_pager.h>
     56 
     57 #include <machine/vmparam.h>
     58 #include <machine/kcore.h>
     59 
     60 #include <ctype.h>
     61 #include <db.h>
     62 #include <fcntl.h>
     63 #include <limits.h>
     64 #include <nlist.h>
     65 #include <paths.h>
     66 #include <stdio.h>
     67 #include <stdlib.h>
     68 #include <string.h>
     69 #include <unistd.h>
     70 #include <kvm.h>
     71 
     72 #include "kvm_private.h"
     73 
     74 static int	kvm_dbopen __P((kvm_t *, const char *));
     75 static int	_kvm_get_header __P((kvm_t *));
     76 static kvm_t	*_kvm_open __P((kvm_t *, const char *, const char *,
     77 		    const char *, int, char *));
     78 static int	clear_gap __P((kvm_t *, FILE *, int));
     79 static off_t	Lseek __P((kvm_t *, int, off_t, int));
     80 static ssize_t	Read __P(( kvm_t *, int, void *, size_t));
     81 
     82 char *
     83 kvm_geterr(kd)
     84 	kvm_t *kd;
     85 {
     86 	return (kd->errbuf);
     87 }
     88 
     89 #if __STDC__
     90 #include <stdarg.h>
     91 #else
     92 #include <varargs.h>
     93 #endif
     94 
     95 /*
     96  * Report an error using printf style arguments.  "program" is kd->program
     97  * on hard errors, and 0 on soft errors, so that under sun error emulation,
     98  * only hard errors are printed out (otherwise, programs like gdb will
     99  * generate tons of error messages when trying to access bogus pointers).
    100  */
    101 void
    102 #if __STDC__
    103 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
    104 #else
    105 _kvm_err(kd, program, fmt, va_alist)
    106 	kvm_t *kd;
    107 	char *program, *fmt;
    108 	va_dcl
    109 #endif
    110 {
    111 	va_list ap;
    112 
    113 #ifdef __STDC__
    114 	va_start(ap, fmt);
    115 #else
    116 	va_start(ap);
    117 #endif
    118 	if (program != NULL) {
    119 		(void)fprintf(stderr, "%s: ", program);
    120 		(void)vfprintf(stderr, fmt, ap);
    121 		(void)fputc('\n', stderr);
    122 	} else
    123 		(void)vsnprintf(kd->errbuf,
    124 		    sizeof(kd->errbuf), (char *)fmt, ap);
    125 
    126 	va_end(ap);
    127 }
    128 
    129 void
    130 #if __STDC__
    131 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
    132 #else
    133 _kvm_syserr(kd, program, fmt, va_alist)
    134 	kvm_t *kd;
    135 	char *program, *fmt;
    136 	va_dcl
    137 #endif
    138 {
    139 	va_list ap;
    140 	register int n;
    141 
    142 #if __STDC__
    143 	va_start(ap, fmt);
    144 #else
    145 	va_start(ap);
    146 #endif
    147 	if (program != NULL) {
    148 		(void)fprintf(stderr, "%s: ", program);
    149 		(void)vfprintf(stderr, fmt, ap);
    150 		(void)fprintf(stderr, ": %s\n", strerror(errno));
    151 	} else {
    152 		register char *cp = kd->errbuf;
    153 
    154 		(void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
    155 		n = strlen(cp);
    156 		(void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
    157 		    strerror(errno));
    158 	}
    159 	va_end(ap);
    160 }
    161 
    162 void *
    163 _kvm_malloc(kd, n)
    164 	register kvm_t *kd;
    165 	register size_t n;
    166 {
    167 	void *p;
    168 
    169 	if ((p = malloc(n)) == NULL)
    170 		_kvm_err(kd, kd->program, strerror(errno));
    171 	return (p);
    172 }
    173 
    174 /*
    175  * Wrappers for Lseek/Read system calls.  They check for errors and
    176  * call _kvm_syserr() if appropriate.
    177  */
    178 static off_t
    179 Lseek(kd, fd, offset, whence)
    180 	kvm_t	*kd;
    181 	int	fd, whence;
    182 	off_t	offset;
    183 {
    184 	off_t	off;
    185 
    186 	errno = 0;
    187 	if ((off = lseek(fd, offset, whence)) == -1 && errno != 0) {
    188 		_kvm_syserr(kd, kd->program, "Lseek");
    189 		return (-1);
    190 	}
    191 	return (off);
    192 }
    193 
    194 static ssize_t
    195 Read(kd, fd, buf, nbytes)
    196 	kvm_t	*kd;
    197 	int	fd;
    198 	void	*buf;
    199 	size_t	nbytes;
    200 {
    201 	ssize_t	rv;
    202 
    203 	errno = 0;
    204 
    205 	if ((rv = read(fd, buf, nbytes)) != nbytes && errno != 0)
    206 		_kvm_syserr(kd, kd->program, "Read");
    207 	return (rv);
    208 }
    209 
    210 static kvm_t *
    211 _kvm_open(kd, uf, mf, sf, flag, errout)
    212 	register kvm_t *kd;
    213 	const char *uf;
    214 	const char *mf;
    215 	const char *sf;
    216 	int flag;
    217 	char *errout;
    218 {
    219 	struct stat st;
    220 
    221 	kd->db = 0;
    222 	kd->pmfd = -1;
    223 	kd->vmfd = -1;
    224 	kd->swfd = -1;
    225 	kd->nlfd = -1;
    226 	kd->procbase = 0;
    227 	kd->nbpg = getpagesize();
    228 	kd->swapspc = 0;
    229 	kd->argspc = 0;
    230 	kd->argbuf = 0;
    231 	kd->argv = 0;
    232 	kd->vmst = 0;
    233 	kd->vm_page_buckets = 0;
    234 	kd->kcore_hdr = 0;
    235 	kd->cpu_hdr = 0;
    236 	kd->dump_off = 0;
    237 
    238 	if (uf == 0)
    239 		uf = _PATH_UNIX;
    240 	else if (strlen(uf) >= MAXPATHLEN) {
    241 		_kvm_err(kd, kd->program, "exec file name too long");
    242 		goto failed;
    243 	}
    244 	if (flag & ~O_RDWR) {
    245 		_kvm_err(kd, kd->program, "bad flags arg");
    246 		goto failed;
    247 	}
    248 	if (mf == 0)
    249 		mf = _PATH_MEM;
    250 	if (sf == 0)
    251 		sf = _PATH_DRUM;
    252 
    253 	if ((kd->pmfd = open(mf, flag, 0)) < 0) {
    254 		_kvm_syserr(kd, kd->program, "%s", mf);
    255 		goto failed;
    256 	}
    257 	if (fstat(kd->pmfd, &st) < 0) {
    258 		_kvm_syserr(kd, kd->program, "%s", mf);
    259 		goto failed;
    260 	}
    261 	if (S_ISCHR(st.st_mode)) {
    262 		/*
    263 		 * If this is a character special device, then check that
    264 		 * it's /dev/mem.  If so, open kmem too.  (Maybe we should
    265 		 * make it work for either /dev/mem or /dev/kmem -- in either
    266 		 * case you're working with a live kernel.)
    267 		 */
    268 		if (strcmp(mf, _PATH_MEM) != 0) {	/* XXX */
    269 			_kvm_err(kd, kd->program,
    270 				 "%s: not physical memory device", mf);
    271 			goto failed;
    272 		}
    273 		if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
    274 			_kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
    275 			goto failed;
    276 		}
    277 		if ((kd->swfd = open(sf, flag, 0)) < 0) {
    278 			_kvm_syserr(kd, kd->program, "%s", sf);
    279 			goto failed;
    280 		}
    281 		/*
    282 		 * Open kvm nlist database.  We go ahead and do this
    283 		 * here so that we don't have to hold on to the vmunix
    284 		 * path name.  Since a kvm application will surely do
    285 		 * a kvm_nlist(), this probably won't be a wasted effort.
    286 		 * If the database cannot be opened, open the namelist
    287 		 * argument so we revert to slow nlist() calls.
    288 		 */
    289 		if (kvm_dbopen(kd, uf) < 0 &&
    290 		    (kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
    291 			_kvm_syserr(kd, kd->program, "%s", uf);
    292 			goto failed;
    293 		}
    294 	} else {
    295 		/*
    296 		 * This is a crash dump.
    297 		 * Initalize the virtual address translation machinery,
    298 		 * but first setup the namelist fd.
    299 		 */
    300 		if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
    301 			_kvm_syserr(kd, kd->program, "%s", uf);
    302 			goto failed;
    303 		}
    304 
    305 		/*
    306 		 * If there is no valid core header, fail silently here.
    307 		 * The address translations however will fail without
    308 		 * header. Things can be made to run by calling
    309 		 * kvm_dump_mkheader() before doing any translation.
    310 		 */
    311 		if (_kvm_get_header(kd) == 0) {
    312 			if (_kvm_initvtop(kd) < 0)
    313 				goto failed;
    314 		}
    315 	}
    316 	return (kd);
    317 failed:
    318 	/*
    319 	 * Copy out the error if doing sane error semantics.
    320 	 */
    321 	if (errout != 0)
    322 		strcpy(errout, kd->errbuf);
    323 	(void)kvm_close(kd);
    324 	return (0);
    325 }
    326 
    327 static int
    328 _kvm_get_header(kd)
    329 kvm_t	*kd;
    330 {
    331 	cpu_kcore_hdr_t	ckhdr;
    332 	kcore_hdr_t	khdr;
    333 	kcore_seg_t	seghdr;
    334 	off_t		offset;
    335 
    336 	if (Lseek(kd, kd->pmfd, (off_t)0, SEEK_SET) == -1)
    337 		return (-1);
    338 
    339 	if (Read(kd, kd->pmfd, &khdr, sizeof(khdr)) != sizeof(khdr))
    340 		return (-1);
    341 	offset = khdr.c_hdrsize;
    342 
    343 	/*
    344 	 * Currently, we only support dump-files made by the current
    345 	 * architecture...
    346 	 */
    347 	if ((CORE_GETMAGIC(khdr) != KCORE_MAGIC)
    348 	     || ((CORE_GETMID(khdr) != MID_MACHINE)))
    349 			return (-1);
    350 
    351 	/*
    352 	 * Currently, we only support exactly 2 segments: cpu-segment
    353 	 * and data-segment in exactly that order.
    354 	 */
    355 	if (khdr.c_nseg != 2)
    356 		return (-1);
    357 
    358 	/*
    359 	 * Read the next segment header: cpu segment
    360 	 */
    361 	if (Lseek(kd, kd->pmfd, (off_t)offset, SEEK_SET) == -1)
    362 		return (-1);
    363 	if (Read(kd, kd->pmfd, &seghdr, sizeof(seghdr)) != sizeof(seghdr))
    364 		return (-1);
    365 	if (CORE_GETMAGIC(seghdr) != KCORESEG_MAGIC
    366 		|| CORE_GETFLAG(seghdr) != CORE_CPU)
    367 		return (-1);
    368 	offset += khdr.c_seghdrsize;
    369 	if (Lseek(kd, kd->pmfd, (off_t)offset, SEEK_SET) == -1)
    370 		return (-1);
    371 	if (Read(kd, kd->pmfd, &ckhdr, sizeof(ckhdr)) != sizeof(ckhdr))
    372 		return (-1);
    373 	offset += seghdr.c_size;
    374 
    375 	/*
    376 	 * Read the next segment header: data segment
    377 	 */
    378 	if (Lseek(kd, kd->pmfd, (off_t)offset, SEEK_SET) == -1)
    379 		return (-1);
    380 	if (Read(kd, kd->pmfd, &seghdr, sizeof(seghdr)) != sizeof(seghdr))
    381 		return (-1);
    382 	offset += khdr.c_seghdrsize;
    383 
    384 	if (CORE_GETMAGIC(seghdr) != KCORESEG_MAGIC
    385 		|| CORE_GETFLAG(seghdr) != CORE_DATA)
    386 		return (-1);
    387 
    388 	kd->kcore_hdr = (kcore_hdr_t *)_kvm_malloc(kd, sizeof(*kd->kcore_hdr));
    389 	if (kd->kcore_hdr == NULL)
    390 		return (-1);
    391 	kd->cpu_hdr = (cpu_kcore_hdr_t *)_kvm_malloc(kd, sizeof(*kd->cpu_hdr));
    392 	if (kd->cpu_hdr == NULL) {
    393 		free((void *)kd->kcore_hdr);
    394 		kd->kcore_hdr = NULL;
    395 		return (-1);
    396 	}
    397 
    398 	*kd->kcore_hdr = khdr;
    399 	*kd->cpu_hdr   = ckhdr;
    400 	kd->dump_off   = offset;
    401 	return (0);
    402 }
    403 
    404 /*
    405  * Translate a physical address to a file-offset in the crash-dump.
    406  */
    407 off_t
    408 _kvm_pa2off(kd, pa)
    409 	kvm_t	*kd;
    410 	u_long	pa;
    411 {
    412 	off_t		off;
    413 	phys_ram_seg_t	*rsp;
    414 
    415 	off = 0;
    416 	for (rsp = kd->cpu_hdr->ram_segs; rsp->size; rsp++) {
    417 		if (pa >= rsp->start && pa < rsp->start + rsp->size) {
    418 			pa -= rsp->start;
    419 			break;
    420 		}
    421 		off += rsp->size;
    422 	}
    423 	return(pa + off + kd->dump_off);
    424 }
    425 
    426 int
    427 kvm_dump_mkheader(kd, dump_off)
    428 kvm_t	*kd;
    429 off_t	dump_off;
    430 {
    431 	kcore_hdr_t	kch;
    432 	kcore_seg_t	kseg;
    433 	cpu_kcore_hdr_t	ckhdr;
    434 	int		hdr_size;
    435 
    436 	hdr_size = 0;
    437 	if (kd->kcore_hdr != NULL) {
    438 	    _kvm_err(kd, kd->program, "already has a dump header");
    439 	    return (-1);
    440 	}
    441 	if (ISALIVE(kd)) {
    442 		_kvm_err(kd, kd->program, "don't use on live kernel");
    443 		return (-1);
    444 	}
    445 
    446 	/*
    447 	 * Check for new format crash dump
    448 	 */
    449 	if (Lseek(kd, kd->pmfd, dump_off, SEEK_SET) == -1)
    450 		return (-1);
    451 	if (Read(kd, kd->pmfd, &kseg, sizeof(kseg)) != sizeof(kseg))
    452 		return (-1);
    453 	if ((CORE_GETMAGIC(kseg) == KCORE_MAGIC)
    454 	     && ((CORE_GETMID(kseg) == MID_MACHINE))) {
    455 		hdr_size += ALIGN(sizeof(kcore_seg_t));
    456 		if (Lseek(kd, kd->pmfd, dump_off+hdr_size, SEEK_SET) == -1)
    457 			return (-1);
    458 		if (Read(kd, kd->pmfd, &ckhdr, sizeof(ckhdr)) != sizeof(ckhdr))
    459 			return (-1);
    460 		hdr_size += kseg.c_size;
    461 		if (Lseek(kd, kd->pmfd, dump_off+hdr_size, SEEK_SET) == -1)
    462 			return (-1);
    463 		kd->cpu_hdr = (cpu_kcore_hdr_t *)
    464 				_kvm_malloc(kd, sizeof(cpu_kcore_hdr_t));
    465 		*kd->cpu_hdr = ckhdr;
    466 	}
    467 
    468 	/*
    469 	 * Create a kcore_hdr.
    470 	 */
    471 	kd->kcore_hdr = (kcore_hdr_t *) _kvm_malloc(kd, sizeof(kcore_hdr_t));
    472 	if (kd->kcore_hdr == NULL) {
    473 		if (kd->cpu_hdr != NULL) {
    474 			free((void *)kd->cpu_hdr);
    475 			kd->cpu_hdr = NULL;
    476 		}
    477 		return (-1);
    478 	}
    479 
    480 	kd->kcore_hdr->c_hdrsize    = ALIGN(sizeof(kcore_hdr_t));
    481 	kd->kcore_hdr->c_seghdrsize = ALIGN(sizeof(kcore_seg_t));
    482 	kd->kcore_hdr->c_nseg       = 2;
    483 	CORE_SETMAGIC(*(kd->kcore_hdr), KCORE_MAGIC, MID_MACHINE,0);
    484 
    485 	/*
    486 	 * If there is no cpu_hdr at this point, we probably have an
    487 	 * old format crash dump.....bail out
    488 	 */
    489 	if (kd->cpu_hdr == NULL) {
    490 		free((void *)kd->kcore_hdr);
    491 		kd->kcore_hdr = NULL;
    492 		_kvm_err(kd, kd->program, "invalid dump");
    493 	}
    494 
    495 	kd->dump_off  = dump_off + hdr_size;
    496 
    497 	/*
    498 	 * Now that we have a valid header, enable translations.
    499 	 */
    500 	_kvm_initvtop(kd);
    501 
    502 	return(hdr_size);
    503 }
    504 
    505 static int
    506 clear_gap(kd, fp, size)
    507 kvm_t	*kd;
    508 FILE	*fp;
    509 int	size;
    510 {
    511 	if (size <= 0) /* XXX - < 0 should never happen */
    512 		return (0);
    513 	while (size-- > 0) {
    514 		if (fputc(0, fp) == EOF) {
    515 			_kvm_syserr(kd, kd->program, "clear_gap");
    516 			return (-1);
    517 		}
    518 	}
    519 	return (0);
    520 }
    521 
    522 /*
    523  * Write the dump header info to 'fp'. Note that we can't use fseek(3) here
    524  * because 'fp' might be a file pointer obtained by zopen().
    525  */
    526 int
    527 kvm_dump_wrtheader(kd, fp, dumpsize)
    528 kvm_t	*kd;
    529 FILE	*fp;
    530 int	dumpsize;
    531 {
    532 	kcore_seg_t	seghdr;
    533 	long		offset;
    534 	int		gap;
    535 
    536 	if (kd->kcore_hdr == NULL || kd->cpu_hdr == NULL) {
    537 		_kvm_err(kd, kd->program, "no valid dump header(s)");
    538 		return (-1);
    539 	}
    540 
    541 	/*
    542 	 * Write the generic header
    543 	 */
    544 	offset = 0;
    545 	if (fwrite((void*)kd->kcore_hdr, sizeof(kcore_hdr_t), 1, fp) <= 0) {
    546 		_kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
    547 		return (-1);
    548 	}
    549 	offset += kd->kcore_hdr->c_hdrsize;
    550 	gap     = kd->kcore_hdr->c_hdrsize - sizeof(kcore_hdr_t);
    551 	if (clear_gap(kd, fp, gap) == -1)
    552 		return (-1);
    553 
    554 	/*
    555 	 * Write the cpu header
    556 	 */
    557 	CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_CPU);
    558 	seghdr.c_size = ALIGN(sizeof(cpu_kcore_hdr_t));
    559 	if (fwrite((void*)&seghdr, sizeof(seghdr), 1, fp) <= 0) {
    560 		_kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
    561 		return (-1);
    562 	}
    563 	offset += kd->kcore_hdr->c_seghdrsize;
    564 	gap     = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
    565 	if (clear_gap(kd, fp, gap) == -1)
    566 		return (-1);
    567 
    568 	if (fwrite((void*)kd->cpu_hdr, sizeof(cpu_kcore_hdr_t), 1, fp) <= 0) {
    569 		_kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
    570 		return (-1);
    571 	}
    572 	offset += seghdr.c_size;
    573 	gap     = seghdr.c_size - sizeof(cpu_kcore_hdr_t);
    574 	if (clear_gap(kd, fp, gap) == -1)
    575 		return (-1);
    576 
    577 	/*
    578 	 * Write the actual dump data segment header
    579 	 */
    580 	CORE_SETMAGIC(seghdr, KCORESEG_MAGIC, 0, CORE_DATA);
    581 	seghdr.c_size = dumpsize;
    582 	if (fwrite((void*)&seghdr, sizeof(seghdr), 1, fp) <= 0) {
    583 		_kvm_syserr(kd, kd->program, "kvm_dump_wrtheader");
    584 		return (-1);
    585 	}
    586 	offset += kd->kcore_hdr->c_seghdrsize;
    587 	gap     = kd->kcore_hdr->c_seghdrsize - sizeof(seghdr);
    588 	if (clear_gap(kd, fp, gap) == -1)
    589 		return (-1);
    590 
    591 	return (offset);
    592 }
    593 
    594 kvm_t *
    595 kvm_openfiles(uf, mf, sf, flag, errout)
    596 	const char *uf;
    597 	const char *mf;
    598 	const char *sf;
    599 	int flag;
    600 	char *errout;
    601 {
    602 	register kvm_t *kd;
    603 
    604 	if ((kd = malloc(sizeof(*kd))) == NULL) {
    605 		(void)strcpy(errout, strerror(errno));
    606 		return (0);
    607 	}
    608 	kd->program = 0;
    609 	return (_kvm_open(kd, uf, mf, sf, flag, errout));
    610 }
    611 
    612 kvm_t *
    613 kvm_open(uf, mf, sf, flag, program)
    614 	const char *uf;
    615 	const char *mf;
    616 	const char *sf;
    617 	int flag;
    618 	const char *program;
    619 {
    620 	register kvm_t *kd;
    621 
    622 	if ((kd = malloc(sizeof(*kd))) == NULL && program != NULL) {
    623 		(void)fprintf(stderr, "%s: %s\n", strerror(errno));
    624 		return (0);
    625 	}
    626 	kd->program = program;
    627 	return (_kvm_open(kd, uf, mf, sf, flag, NULL));
    628 }
    629 
    630 int
    631 kvm_close(kd)
    632 	kvm_t *kd;
    633 {
    634 	register int error = 0;
    635 
    636 	if (kd->pmfd >= 0)
    637 		error |= close(kd->pmfd);
    638 	if (kd->vmfd >= 0)
    639 		error |= close(kd->vmfd);
    640 	if (kd->nlfd >= 0)
    641 		error |= close(kd->nlfd);
    642 	if (kd->swfd >= 0)
    643 		error |= close(kd->swfd);
    644 	if (kd->db != 0)
    645 		error |= (kd->db->close)(kd->db);
    646 	if (kd->vmst)
    647 		_kvm_freevtop(kd);
    648 	if (kd->cpu_hdr != NULL)
    649 		free((void *)kd->cpu_hdr);
    650 	if (kd->kcore_hdr != NULL)
    651 		free((void *)kd->kcore_hdr);
    652 	if (kd->procbase != 0)
    653 		free((void *)kd->procbase);
    654 	if (kd->swapspc != 0)
    655 		free((void *)kd->swapspc);
    656 	if (kd->argspc != 0)
    657 		free((void *)kd->argspc);
    658 	if (kd->argbuf != 0)
    659 		free((void *)kd->argbuf);
    660 	if (kd->argv != 0)
    661 		free((void *)kd->argv);
    662 	free((void *)kd);
    663 
    664 	return (0);
    665 }
    666 
    667 /*
    668  * Set up state necessary to do queries on the kernel namelist
    669  * data base.  If the data base is out-of-data/incompatible with
    670  * given executable, set up things so we revert to standard nlist call.
    671  * Only called for live kernels.  Return 0 on success, -1 on failure.
    672  */
    673 static int
    674 kvm_dbopen(kd, uf)
    675 	kvm_t *kd;
    676 	const char *uf;
    677 {
    678 	char *cp;
    679 	DBT rec;
    680 	int dbversionlen;
    681 	struct nlist nitem;
    682 	char dbversion[_POSIX2_LINE_MAX];
    683 	char kversion[_POSIX2_LINE_MAX];
    684 	char dbname[MAXPATHLEN];
    685 
    686 	if ((cp = rindex(uf, '/')) != 0)
    687 		uf = cp + 1;
    688 
    689 	(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", _PATH_VARDB, uf);
    690 	kd->db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL);
    691 	if (kd->db == 0)
    692 		return (-1);
    693 	/*
    694 	 * read version out of database
    695 	 */
    696 	rec.data = VRS_KEY;
    697 	rec.size = sizeof(VRS_KEY) - 1;
    698 	if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
    699 		goto close;
    700 	if (rec.data == 0 || rec.size > sizeof(dbversion))
    701 		goto close;
    702 
    703 	bcopy(rec.data, dbversion, rec.size);
    704 	dbversionlen = rec.size;
    705 	/*
    706 	 * Read version string from kernel memory.
    707 	 * Since we are dealing with a live kernel, we can call kvm_read()
    708 	 * at this point.
    709 	 */
    710 	rec.data = VRS_SYM;
    711 	rec.size = sizeof(VRS_SYM) - 1;
    712 	if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
    713 		goto close;
    714 	if (rec.data == 0 || rec.size != sizeof(struct nlist))
    715 		goto close;
    716 	bcopy((char *)rec.data, (char *)&nitem, sizeof(nitem));
    717 	if (kvm_read(kd, (u_long)nitem.n_value, kversion, dbversionlen) !=
    718 	    dbversionlen)
    719 		goto close;
    720 	/*
    721 	 * If they match, we win - otherwise clear out kd->db so
    722 	 * we revert to slow nlist().
    723 	 */
    724 	if (bcmp(dbversion, kversion, dbversionlen) == 0)
    725 		return (0);
    726 close:
    727 	(void)(kd->db->close)(kd->db);
    728 	kd->db = 0;
    729 
    730 	return (-1);
    731 }
    732 
    733 int
    734 kvm_nlist(kd, nl)
    735 	kvm_t *kd;
    736 	struct nlist *nl;
    737 {
    738 	register struct nlist *p;
    739 	register int nvalid;
    740 
    741 	/*
    742 	 * If we can't use the data base, revert to the
    743 	 * slow library call.
    744 	 */
    745 	if (kd->db == 0)
    746 		return (__fdnlist(kd->nlfd, nl));
    747 
    748 	/*
    749 	 * We can use the kvm data base.  Go through each nlist entry
    750 	 * and look it up with a db query.
    751 	 */
    752 	nvalid = 0;
    753 	for (p = nl; p->n_name && p->n_name[0]; ++p) {
    754 		register int len;
    755 		DBT rec;
    756 
    757 		if ((len = strlen(p->n_name)) > 4096) {
    758 			/* sanity */
    759 			_kvm_err(kd, kd->program, "symbol too large");
    760 			return (-1);
    761 		}
    762 		rec.data = p->n_name;
    763 		rec.size = len;
    764 
    765 		/*
    766 		 * Make sure that n_value = 0 when the symbol isn't found
    767 		 */
    768 		p->n_value = 0;
    769 
    770 		if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0))
    771 			continue;
    772 		if (rec.data == 0 || rec.size != sizeof(struct nlist))
    773 			continue;
    774 		++nvalid;
    775 		/*
    776 		 * Avoid alignment issues.
    777 		 */
    778 		bcopy((char *)&((struct nlist *)rec.data)->n_type,
    779 		      (char *)&p->n_type,
    780 		      sizeof(p->n_type));
    781 		bcopy((char *)&((struct nlist *)rec.data)->n_value,
    782 		      (char *)&p->n_value,
    783 		      sizeof(p->n_value));
    784 	}
    785 	/*
    786 	 * Return the number of entries that weren't found.
    787 	 */
    788 	return ((p - nl) - nvalid);
    789 }
    790 
    791 int kvm_dump_inval(kd)
    792 kvm_t	*kd;
    793 {
    794 	struct nlist	nlist[2];
    795 	u_long		pa;
    796 
    797 	if (ISALIVE(kd)) {
    798 		_kvm_err(kd, kd->program, "clearing dump on live kernel");
    799 		return (-1);
    800 	}
    801 	nlist[0].n_name = "_dumpmag";
    802 	nlist[1].n_name = NULL;
    803 
    804 	if (kvm_nlist(kd, nlist) == -1) {
    805 		_kvm_err(kd, 0, "bad namelist");
    806 		return (-1);
    807 	}
    808 	if (_kvm_kvatop(kd, (u_long)nlist[0].n_value, &pa) == 0)
    809 		return (-1);
    810 
    811 	errno = 0;
    812 	if (lseek(kd->pmfd, _kvm_pa2off(kd, pa), SEEK_SET) == -1
    813 		&& errno != 0) {
    814 		_kvm_err(kd, 0, "cannot invalidate dump - lseek");
    815 		return (-1);
    816 	}
    817 	pa = 0;
    818 	if (write(kd->pmfd, &pa, sizeof(pa)) != sizeof(pa)) {
    819 		_kvm_err(kd, 0, "cannot invalidate dump - write");
    820 		return (-1);
    821 	}
    822 	return (0);
    823 }
    824 
    825 ssize_t
    826 kvm_read(kd, kva, buf, len)
    827 	kvm_t *kd;
    828 	register u_long kva;
    829 	register void *buf;
    830 	register size_t len;
    831 {
    832 	register int cc;
    833 	register void *cp;
    834 
    835 	if (ISALIVE(kd)) {
    836 		/*
    837 		 * We're using /dev/kmem.  Just read straight from the
    838 		 * device and let the active kernel do the address translation.
    839 		 */
    840 		errno = 0;
    841 		if (lseek(kd->vmfd, (off_t)kva, SEEK_SET) == -1
    842 			&& errno != 0) {
    843 			_kvm_err(kd, 0, "invalid address (%x)", kva);
    844 			return (0);
    845 		}
    846 		cc = read(kd->vmfd, buf, len);
    847 		if (cc < 0) {
    848 			_kvm_syserr(kd, 0, "kvm_read");
    849 			return (0);
    850 		} else if (cc < len)
    851 			_kvm_err(kd, kd->program, "short read");
    852 		return (cc);
    853 	} else {
    854 		if ((kd->kcore_hdr == NULL) || (kd->cpu_hdr == NULL)) {
    855 			_kvm_err(kd, kd->program, "no valid dump header");
    856 			return (0);
    857 		}
    858 		cp = buf;
    859 		while (len > 0) {
    860 			u_long	pa;
    861 			off_t	foff;
    862 
    863 			cc = _kvm_kvatop(kd, kva, &pa);
    864 			if (cc == 0)
    865 				return (0);
    866 			if (cc > len)
    867 				cc = len;
    868 			foff = _kvm_pa2off(kd, pa);
    869 			errno = 0;
    870 			if (lseek(kd->pmfd, foff, SEEK_SET) == -1
    871 				&& errno != 0) {
    872 				_kvm_syserr(kd, 0, _PATH_MEM);
    873 				break;
    874 			}
    875 			cc = read(kd->pmfd, cp, cc);
    876 			if (cc < 0) {
    877 				_kvm_syserr(kd, kd->program, "kvm_read");
    878 				break;
    879 			}
    880 			/*
    881 			 * If kvm_kvatop returns a bogus value or our core
    882 			 * file is truncated, we might wind up seeking beyond
    883 			 * the end of the core file in which case the read will
    884 			 * return 0 (EOF).
    885 			 */
    886 			if (cc == 0)
    887 				break;
    888 			cp = (char *)cp + cc;
    889 			kva += cc;
    890 			len -= cc;
    891 		}
    892 		return ((char *)cp - (char *)buf);
    893 	}
    894 	/* NOTREACHED */
    895 }
    896 
    897 ssize_t
    898 kvm_write(kd, kva, buf, len)
    899 	kvm_t *kd;
    900 	register u_long kva;
    901 	register const void *buf;
    902 	register size_t len;
    903 {
    904 	register int cc;
    905 
    906 	if (ISALIVE(kd)) {
    907 		/*
    908 		 * Just like kvm_read, only we write.
    909 		 */
    910 		errno = 0;
    911 		if (lseek(kd->vmfd, (off_t)kva, SEEK_SET) == -1
    912 			&& errno != 0) {
    913 			_kvm_err(kd, 0, "invalid address (%x)", kva);
    914 			return (0);
    915 		}
    916 		cc = write(kd->vmfd, buf, len);
    917 		if (cc < 0) {
    918 			_kvm_syserr(kd, 0, "kvm_write");
    919 			return (0);
    920 		} else if (cc < len)
    921 			_kvm_err(kd, kd->program, "short write");
    922 		return (cc);
    923 	} else {
    924 		_kvm_err(kd, kd->program,
    925 		    "kvm_write not implemented for dead kernels");
    926 		return (0);
    927 	}
    928 	/* NOTREACHED */
    929 }
    930