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