2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software developed by the Computer Systems
6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7 * BG 91-66 and contributed to Berkeley.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
42 static const char rcsid[] =
45 #endif /* LIBC_SCCS and not lint */
47 #include <sys/param.h>
50 #include <sys/ioctl.h>
52 #include <sys/sysctl.h>
53 #include <sys/linker.h>
56 #include <vm/vm_param.h>
57 #include <vm/swap_pager.h>
59 #include <machine/vmparam.h>
72 #include "kvm_private.h"
74 /* from src/lib/libc/gen/nlist.c */
75 int __fdnlist __P((int, struct nlist *));
91 * Report an error using printf style arguments. "program" is kd->program
92 * on hard errors, and 0 on soft errors, so that under sun error emulation,
93 * only hard errors are printed out (otherwise, programs like gdb will
94 * generate tons of error messages when trying to access bogus pointers).
98 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
100 _kvm_err(kd, program, fmt, va_alist)
113 if (program != NULL) {
114 (void)fprintf(stderr, "%s: ", program);
115 (void)vfprintf(stderr, fmt, ap);
116 (void)fputc('\n', stderr);
118 (void)vsnprintf(kd->errbuf,
119 sizeof(kd->errbuf), (char *)fmt, ap);
126 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
128 _kvm_syserr(kd, program, fmt, va_alist)
142 if (program != NULL) {
143 (void)fprintf(stderr, "%s: ", program);
144 (void)vfprintf(stderr, fmt, ap);
145 (void)fprintf(stderr, ": %s\n", strerror(errno));
147 register char *cp = kd->errbuf;
149 (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
151 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
164 if ((p = calloc(n, sizeof(char))) == NULL)
165 _kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
171 _kvm_open(kd, uf, mf, flag, errout)
190 else if (strlen(uf) >= MAXPATHLEN) {
191 _kvm_err(kd, kd->program, "exec file name too long");
194 if (flag & ~O_RDWR) {
195 _kvm_err(kd, kd->program, "bad flags arg");
201 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
202 _kvm_syserr(kd, kd->program, "%s", mf);
205 if (fstat(kd->pmfd, &st) < 0) {
206 _kvm_syserr(kd, kd->program, "%s", mf);
209 if (S_ISCHR(st.st_mode)) {
211 * If this is a character special device, then check that
212 * it's /dev/mem. If so, open kmem too. (Maybe we should
213 * make it work for either /dev/mem or /dev/kmem -- in either
214 * case you're working with a live kernel.)
216 if (strcmp(mf, _PATH_DEVNULL) == 0) {
217 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
218 } else if (strcmp(mf, _PATH_MEM) != 0) {
219 _kvm_err(kd, kd->program,
220 "%s: not physical memory device", mf);
223 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
224 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
230 * This is a crash dump.
231 * Initialize the virtual address translation machinery,
232 * but first setup the namelist fd.
234 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
235 _kvm_syserr(kd, kd->program, "%s", uf);
238 if (_kvm_initvtop(kd) < 0)
244 * Copy out the error if doing sane error semantics.
247 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
253 kvm_openfiles(uf, mf, sf, flag, errout)
262 if ((kd = malloc(sizeof(*kd))) == NULL) {
263 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
266 memset(kd, 0, sizeof(*kd));
268 return (_kvm_open(kd, uf, mf, flag, errout));
272 kvm_open(uf, mf, sf, flag, errstr)
281 if ((kd = malloc(sizeof(*kd))) == NULL) {
283 (void)fprintf(stderr, "%s: %s\n",
284 errstr, strerror(errno));
287 memset(kd, 0, sizeof(*kd));
288 kd->program = errstr;
289 return (_kvm_open(kd, uf, mf, flag, NULL));
296 register int error = 0;
299 error |= close(kd->pmfd);
301 error |= close(kd->vmfd);
303 error |= close(kd->nlfd);
306 if (kd->procbase != 0)
307 free((void *)kd->procbase);
309 free((void *)kd->argv);
320 register struct nlist *p;
322 struct kld_sym_lookup lookup;
325 * If we can't use the kld symbol lookup, revert to the
329 return (__fdnlist(kd->nlfd, nl));
332 * We can use the kld lookup syscall. Go through each nlist entry
333 * and look it up with a kldsym(2) syscall.
336 for (p = nl; p->n_name && p->n_name[0]; ++p) {
337 lookup.version = sizeof(lookup);
338 lookup.symname = p->n_name;
342 if (lookup.symname[0] == '_')
345 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
349 p->n_value = lookup.symvalue;
355 * Return the number of entries that weren't found.
357 return ((p - nl) - nvalid);
361 kvm_read(kd, kva, buf, len)
372 * We're using /dev/kmem. Just read straight from the
373 * device and let the active kernel do the address translation.
376 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
377 _kvm_err(kd, 0, "invalid address (%x)", kva);
380 cc = read(kd->vmfd, buf, len);
382 _kvm_syserr(kd, 0, "kvm_read");
385 _kvm_err(kd, kd->program, "short read");
392 cc = _kvm_kvatop(kd, kva, &pa);
398 if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
399 _kvm_syserr(kd, 0, _PATH_MEM);
402 cc = read(kd->pmfd, cp, cc);
404 _kvm_syserr(kd, kd->program, "kvm_read");
408 * If kvm_kvatop returns a bogus value or our core
409 * file is truncated, we might wind up seeking beyond
410 * the end of the core file in which case the read will
419 return ((char *)cp - (char *)buf);
425 kvm_write(kd, kva, buf, len)
428 register const void *buf;
435 * Just like kvm_read, only we write.
438 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
439 _kvm_err(kd, 0, "invalid address (%x)", kva);
442 cc = write(kd->vmfd, buf, len);
444 _kvm_syserr(kd, 0, "kvm_write");
447 _kvm_err(kd, kd->program, "short write");
450 _kvm_err(kd, kd->program,
451 "kvm_write not implemented for dead kernels");