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