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