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 *);
75 * Report an error using printf style arguments. "program" is kd->program
76 * on hard errors, and 0 on soft errors, so that under sun error emulation,
77 * only hard errors are printed out (otherwise, programs like gdb will
78 * generate tons of error messages when trying to access bogus pointers).
81 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
86 if (program != NULL) {
87 (void)fprintf(stderr, "%s: ", program);
88 (void)vfprintf(stderr, fmt, ap);
89 (void)fputc('\n', stderr);
91 (void)vsnprintf(kd->errbuf,
92 sizeof(kd->errbuf), (char *)fmt, ap);
98 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
104 if (program != NULL) {
105 (void)fprintf(stderr, "%s: ", program);
106 (void)vfprintf(stderr, fmt, ap);
107 (void)fprintf(stderr, ": %s\n", strerror(errno));
109 char *cp = kd->errbuf;
111 (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
113 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
120 _kvm_malloc(kvm_t *kd, size_t n)
124 if ((p = calloc(n, sizeof(char))) == NULL)
125 _kvm_err(kd, kd->program, "can't allocate %zd bytes: %s",
131 is_proc_mem(const char *p)
133 static char proc[] = "/proc/";
134 static char mem[] = "/mem";
135 if (strncmp(proc, p, sizeof(proc) - 1))
137 p += sizeof(proc) - 1;
138 for (; p != '\0'; ++p)
141 if (!isdigit(*(p - 1)))
143 return !strncmp(p, mem, sizeof(mem) - 1);
147 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
163 else if (strlen(uf) >= MAXPATHLEN) {
164 _kvm_err(kd, kd->program, "exec file name too long");
167 if (flag & ~O_RDWR) {
168 _kvm_err(kd, kd->program, "bad flags arg");
174 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
175 _kvm_syserr(kd, kd->program, "%s", mf);
178 if (fstat(kd->pmfd, &st) < 0) {
179 _kvm_syserr(kd, kd->program, "%s", mf);
182 if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
183 _kvm_syserr(kd, kd->program, "%s", mf);
186 if (S_ISCHR(st.st_mode)) {
188 * If this is a character special device, then check that
189 * it's /dev/mem. If so, open kmem too. (Maybe we should
190 * make it work for either /dev/mem or /dev/kmem -- in either
191 * case you're working with a live kernel.)
193 if (strcmp(mf, _PATH_DEVNULL) == 0) {
194 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
196 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
197 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
200 if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
201 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
205 kd->flags |= KVMF_HOST;
208 * Crash dump or vkernel /proc/$pid/mem file:
209 * can't use kldsym, we are going to need ->nlfd
211 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
212 _kvm_syserr(kd, kd->program, "%s", uf);
215 if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
216 _kvm_syserr(kd, kd->program, "%s", uf);
219 if(is_proc_mem(mf)) {
221 * It's /proc/$pid/mem, so we rely on the host
222 * kernel to do address translation for us.
226 kd->flags |= KVMF_VKERN;
228 if (st.st_size <= 0) {
230 _kvm_syserr(kd, kd->program, "empty file");
235 * This is a crash dump.
236 * Initialize the virtual address translation machinery,
237 * but first setup the namelist fd.
239 if (_kvm_initvtop(kd) < 0)
246 * Copy out the error if doing sane error semantics.
249 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
255 kvm_openfiles(const char *uf, const char *mf, const char *sf, int flag,
260 if ((kd = malloc(sizeof(*kd))) == NULL) {
261 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
264 memset(kd, 0, sizeof(*kd));
266 return (_kvm_open(kd, uf, mf, flag, errout));
270 kvm_open(const char *uf, const char *mf, const char *sf, int flag,
275 if ((kd = malloc(sizeof(*kd))) == NULL) {
277 (void)fprintf(stderr, "%s: %s\n",
278 errstr, strerror(errno));
281 memset(kd, 0, sizeof(*kd));
282 kd->program = errstr;
283 return (_kvm_open(kd, uf, mf, flag, NULL));
292 error |= close(kd->pmfd);
294 error |= close(kd->vmfd);
296 error |= close(kd->nlfd);
299 if (kd->procbase != NULL)
302 free((void *)kd->argv);
309 kvm_nlist(kvm_t *kd, struct nlist *nl)
313 struct kld_sym_lookup lookup;
317 * If we can't use the kld symbol lookup, revert to the
321 return (__fdnlist(kd->nlfd, nl));
324 * We can use the kld lookup syscall. Go through each nlist entry
325 * and look it up with a kldsym(2) syscall.
328 for (p = nl; p->n_name && p->n_name[0]; ++p) {
329 lookup.version = sizeof(lookup);
330 lookup.symname = p->n_name;
334 if (lookup.symname[0] == '_')
337 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
341 p->n_value = lookup.symvalue;
347 * Return the number of entries that weren't found. If they exist,
348 * also fill internal error buffer.
350 error = ((p - nl) - nvalid);
352 _kvm_syserr(kd, kd->program, "kvm_nlist");
357 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
362 if (kvm_notrans(kd)) {
364 * We're using /dev/kmem. Just read straight from the
365 * device and let the active kernel do the address translation.
368 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
369 _kvm_err(kd, 0, "invalid address (%lx)", kva);
374 * Try to pre-fault the user memory to reduce instances of
375 * races within the kernel. XXX workaround for kernel bug
376 * where kernel does a sanity check, but user faults during
377 * the copy can block and race against another kernel entity
378 * unmapping the memory in question.
381 cc = read(kd->vmfd, buf, len);
383 _kvm_syserr(kd, 0, "kvm_read");
386 _kvm_err(kd, kd->program, "short read");
393 cc = _kvm_kvatop(kd, kva, &pa);
399 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
400 _kvm_syserr(kd, 0, _PATH_MEM);
404 cc = read(kd->pmfd, cp, cc);
406 _kvm_syserr(kd, kd->program, "kvm_read");
410 * If kvm_kvatop returns a bogus value or our core
411 * file is truncated, we might wind up seeking beyond
412 * the end of the core file in which case the read will
417 cp = (char *)cp + cc;
421 return ((char *)cp - (char *)buf);
427 kvm_readstr(kvm_t *kd, u_long kva, char *buf, size_t *lenp)
437 _kvm_syserr(kd, kd->program, "kvm_readstr");
444 if (kvm_notrans(kd)) {
446 * We're using /dev/kmem. Just read straight from the
447 * device and let the active kernel do the address translation.
450 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
451 _kvm_err(kd, 0, "invalid address (%lx)", kva);
455 for (pos = 0, ch = -1; ch != 0; pos++) {
456 cc = read(kd->vmfd, &ch, 1);
457 if ((ssize_t)cc < 0) {
458 _kvm_syserr(kd, 0, "kvm_readstr");
461 _kvm_err(kd, kd->program, "short read");
463 buf = realloc(buf, asize *= 2);
465 _kvm_syserr(kd, kd->program, "kvm_readstr");
482 for (pos = 0, ch = -1; ch != 0; pos++, left--, kva++) {
486 left = _kvm_kvatop(kd, kva, &pa);
490 if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
491 _kvm_syserr(kd, 0, _PATH_MEM);
495 cc = read(kd->pmfd, &ch, 1);
496 if ((ssize_t)cc < 0) {
497 _kvm_syserr(kd, 0, "kvm_readstr");
500 _kvm_err(kd, kd->program, "short read");
502 buf = realloc(buf, asize *= 2);
504 _kvm_syserr(kd, kd->program, "kvm_readstr");
524 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
528 if (kvm_notrans(kd)) {
530 * Just like kvm_read, only we write.
533 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
534 _kvm_err(kd, 0, "invalid address (%lx)", kva);
537 cc = write(kd->vmfd, buf, len);
539 _kvm_syserr(kd, 0, "kvm_write");
542 _kvm_err(kd, kd->program, "short write");
545 _kvm_err(kd, kd->program,
546 "kvm_write not implemented for dead kernels");