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