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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)kvm.c 8.2 (Berkeley) 2/13/94
34 * $FreeBSD: src/lib/libkvm/kvm.c,v 1.12.2.3 2002/09/13 14:53:43 nectar Exp $
37 #include <sys/user.h> /* MUST BE FIRST */
38 #include <sys/param.h>
40 #include <sys/ioctl.h>
42 #include <sys/sysctl.h>
43 #include <sys/linker.h>
46 #include <vm/vm_param.h>
47 #include <vm/swap_pager.h>
49 #include <machine/vmparam.h>
63 #include "kvm_private.h"
65 /* from src/lib/libc/gen/nlist.c */
66 int __fdnlist (int, struct nlist *);
69 kvm_notrans(kvm_t *kd)
71 return kvm_ishost(kd) || kvm_isvkernel(kd);
81 * Report an error using printf style arguments. "program" is kd->program
82 * on hard errors, and 0 on soft errors, so that under sun error emulation,
83 * only hard errors are printed out (otherwise, programs like gdb will
84 * generate tons of error messages when trying to access bogus pointers).
87 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
92 if (program != NULL) {
93 (void)fprintf(stderr, "%s: ", program);
94 (void)vfprintf(stderr, fmt, ap);
95 (void)fputc('\n', stderr);
97 (void)vsnprintf(kd->errbuf,
98 sizeof(kd->errbuf), fmt, ap);
104 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
110 if (program != NULL) {
111 (void)fprintf(stderr, "%s: ", program);
112 (void)vfprintf(stderr, fmt, ap);
113 (void)fprintf(stderr, ": %s\n", strerror(errno));
115 char *cp = kd->errbuf;
117 (void)vsnprintf(cp, sizeof(kd->errbuf), fmt, ap);
119 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
126 _kvm_malloc(kvm_t *kd, size_t n)
130 if ((p = calloc(n, sizeof(char))) == NULL)
131 _kvm_err(kd, kd->program, "can't allocate %zd bytes: %s",
137 is_proc_mem(const char *p)
139 static char proc[] = "/proc/";
140 static char mem[] = "/mem";
141 if (strncmp(proc, p, sizeof(proc) - 1))
143 p += sizeof(proc) - 1;
144 for (; *p != '\0'; ++p)
147 if (!isdigit(*(p - 1)))
149 return !strncmp(p, mem, sizeof(mem) - 1);
153 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
169 else if (strlen(uf) >= MAXPATHLEN) {
170 _kvm_err(kd, kd->program, "exec file name too long");
173 if (flag & ~O_RDWR) {
174 _kvm_err(kd, kd->program, "bad flags arg");
180 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
181 _kvm_syserr(kd, kd->program, "%s", mf);
184 if (fstat(kd->pmfd, &st) < 0) {
185 _kvm_syserr(kd, kd->program, "%s", mf);
188 if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
189 _kvm_syserr(kd, kd->program, "%s", mf);
192 if (S_ISCHR(st.st_mode)) {
194 * If this is a character special device, then check that
195 * it's /dev/mem. If so, open kmem too. (Maybe we should
196 * make it work for either /dev/mem or /dev/kmem -- in either
197 * case you're working with a live kernel.)
199 if (strcmp(mf, _PATH_DEVNULL) == 0) {
200 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
202 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
203 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
206 if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
207 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
211 kd->flags |= KVMF_HOST;
214 * Crash dump or vkernel /proc/$pid/mem file:
215 * can't use kldsym, we are going to need ->nlfd
217 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
218 _kvm_syserr(kd, kd->program, "%s", uf);
221 if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
222 _kvm_syserr(kd, kd->program, "%s", uf);
225 if(is_proc_mem(mf)) {
227 * It's /proc/$pid/mem, so we rely on the host
228 * kernel to do address translation for us.
232 kd->flags |= KVMF_VKERN;
234 if (st.st_size <= 0) {
236 _kvm_syserr(kd, kd->program, "empty file");
241 * This is a crash dump.
242 * Initialize the virtual address translation machinery,
243 * but first setup the namelist fd.
245 if (_kvm_initvtop(kd) < 0)
252 * Copy out the error if doing sane error semantics.
255 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
261 kvm_openfiles(const char *uf, const char *mf, const char *sf __unused, int flag,
266 if ((kd = malloc(sizeof(*kd))) == NULL) {
267 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
270 memset(kd, 0, sizeof(*kd));
272 return (_kvm_open(kd, uf, mf, flag, errout));
276 kvm_open(const char *uf, const char *mf, const char *sf __unused, int flag,
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));
298 error |= close(kd->pmfd);
300 error |= close(kd->vmfd);
302 error |= close(kd->nlfd);
305 if (kd->procbase != NULL)
308 free((void *)kd->argv);
315 kvm_nlist(kvm_t *kd, struct nlist *nl)
319 struct kld_sym_lookup lookup;
323 * If we can't use the kld symbol lookup, revert to the
327 return (__fdnlist(kd->nlfd, nl));
330 * We can use the kld lookup syscall. Go through each nlist entry
331 * and look it up with a kldsym(2) syscall.
334 for (p = nl; p->n_name && p->n_name[0]; ++p) {
335 lookup.version = sizeof(lookup);
336 lookup.symname = p->n_name;
340 if (lookup.symname[0] == '_')
343 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
347 p->n_value = lookup.symvalue;
353 * Return the number of entries that weren't found. If they exist,
354 * also fill internal error buffer.
356 error = ((p - nl) - nvalid);
358 _kvm_syserr(kd, kd->program, "kvm_nlist");
363 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
368 if (kvm_notrans(kd)) {
370 * We're using /dev/kmem. Just read straight from the
371 * device and let the active kernel do the address translation.
374 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
375 _kvm_err(kd, 0, "invalid address (%lx)", kva);
380 * Try to pre-fault the user memory to reduce instances of
381 * races within the kernel. XXX workaround for kernel bug
382 * where kernel does a sanity check, but user faults during
383 * the copy can block and race against another kernel entity
384 * unmapping the memory in question.
387 cc = read(kd->vmfd, buf, len);
389 _kvm_syserr(kd, 0, "kvm_read");
392 _kvm_err(kd, kd->program, "short read");
399 cc = _kvm_kvatop(kd, kva, &pa);
405 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
406 _kvm_syserr(kd, 0, _PATH_MEM);
410 cc = read(kd->pmfd, cp, cc);
412 _kvm_syserr(kd, kd->program, "kvm_read");
416 * If kvm_kvatop returns a bogus value or our core
417 * file is truncated, we might wind up seeking beyond
418 * the end of the core file in which case the read will
423 cp = (char *)cp + cc;
427 return ((char *)cp - (char *)buf);
433 kvm_readstr(kvm_t *kd, u_long kva, char *buf, size_t *lenp)
443 _kvm_syserr(kd, kd->program, "kvm_readstr");
450 if (kvm_notrans(kd)) {
452 * We're using /dev/kmem. Just read straight from the
453 * device and let the active kernel do the address translation.
456 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
457 _kvm_err(kd, 0, "invalid address (%lx)", kva);
461 for (pos = 0, ch = -1; ch != 0; pos++) {
462 cc = read(kd->vmfd, &ch, 1);
463 if ((ssize_t)cc < 0) {
464 _kvm_syserr(kd, 0, "kvm_readstr");
467 _kvm_err(kd, kd->program, "short read");
469 buf = realloc(buf, asize *= 2);
471 _kvm_syserr(kd, kd->program, "kvm_readstr");
488 for (pos = 0, ch = -1; ch != 0; pos++, left--, kva++) {
492 left = _kvm_kvatop(kd, kva, &pa);
496 if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
497 _kvm_syserr(kd, 0, _PATH_MEM);
501 cc = read(kd->pmfd, &ch, 1);
502 if ((ssize_t)cc < 0) {
503 _kvm_syserr(kd, 0, "kvm_readstr");
506 _kvm_err(kd, kd->program, "short read");
508 buf = realloc(buf, asize *= 2);
510 _kvm_syserr(kd, kd->program, "kvm_readstr");
530 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
534 if (kvm_notrans(kd)) {
536 * Just like kvm_read, only we write.
539 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
540 _kvm_err(kd, 0, "invalid address (%lx)", kva);
543 cc = write(kd->vmfd, buf, len);
545 _kvm_syserr(kd, 0, "kvm_write");
548 _kvm_err(kd, kd->program, "short write");
551 _kvm_err(kd, kd->program,
552 "kvm_write not implemented for dead kernels");