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