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
37 * @(#)kvm.c 8.2 (Berkeley) 2/13/94
38 * $FreeBSD: src/lib/libkvm/kvm.c,v 1.12.2.3 2002/09/13 14:53:43 nectar Exp $
39 * $DragonFly: src/lib/libkvm/kvm.c,v 1.5 2004/10/25 19:38:45 drhodus Exp $
42 #include <sys/param.h>
45 #include <sys/ioctl.h>
47 #include <sys/sysctl.h>
48 #include <sys/linker.h>
51 #include <vm/vm_param.h>
52 #include <vm/swap_pager.h>
54 #include <machine/vmparam.h>
68 #include "kvm_private.h"
70 /* from src/lib/libc/gen/nlist.c */
71 int __fdnlist (int, struct nlist *);
80 * Report an error using printf style arguments. "program" is kd->program
81 * on hard errors, and 0 on soft errors, so that under sun error emulation,
82 * only hard errors are printed out (otherwise, programs like gdb will
83 * generate tons of error messages when trying to access bogus pointers).
86 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
91 if (program != NULL) {
92 (void)fprintf(stderr, "%s: ", program);
93 (void)vfprintf(stderr, fmt, ap);
94 (void)fputc('\n', stderr);
96 (void)vsnprintf(kd->errbuf,
97 sizeof(kd->errbuf), (char *)fmt, ap);
103 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
109 if (program != NULL) {
110 (void)fprintf(stderr, "%s: ", program);
111 (void)vfprintf(stderr, fmt, ap);
112 (void)fprintf(stderr, ": %s\n", strerror(errno));
114 char *cp = kd->errbuf;
116 (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
118 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
125 _kvm_malloc(kvm_t *kd, size_t n)
129 if ((p = calloc(n, sizeof(char))) == NULL)
130 _kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
136 _kvm_open(kvm_t *kd, const char *uf, const char *mf, int flag, char *errout)
150 else if (strlen(uf) >= MAXPATHLEN) {
151 _kvm_err(kd, kd->program, "exec file name too long");
154 if (flag & ~O_RDWR) {
155 _kvm_err(kd, kd->program, "bad flags arg");
161 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
162 _kvm_syserr(kd, kd->program, "%s", mf);
165 if (fstat(kd->pmfd, &st) < 0) {
166 _kvm_syserr(kd, kd->program, "%s", mf);
169 if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
170 _kvm_syserr(kd, kd->program, "%s", mf);
173 if (S_ISCHR(st.st_mode)) {
175 * If this is a character special device, then check that
176 * it's /dev/mem. If so, open kmem too. (Maybe we should
177 * make it work for either /dev/mem or /dev/kmem -- in either
178 * case you're working with a live kernel.)
180 if (strcmp(mf, _PATH_DEVNULL) == 0) {
181 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
182 } else if (strcmp(mf, _PATH_MEM) != 0) {
183 _kvm_err(kd, kd->program,
184 "%s: not physical memory device", mf);
187 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
188 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
191 if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
192 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
198 * This is a crash dump.
199 * Initialize the virtual address translation machinery,
200 * but first setup the namelist fd.
202 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
203 _kvm_syserr(kd, kd->program, "%s", uf);
206 if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
207 _kvm_syserr(kd, kd->program, "%s", uf);
210 if (_kvm_initvtop(kd) < 0)
216 * Copy out the error if doing sane error semantics.
219 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
225 kvm_openfiles(const char *uf, const char *mf, const char *sf, int flag,
230 if ((kd = malloc(sizeof(*kd))) == NULL) {
231 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
234 memset(kd, 0, sizeof(*kd));
236 return (_kvm_open(kd, uf, mf, flag, errout));
240 kvm_open(const char *uf, const char *mf, const char *sf, int flag,
245 if ((kd = malloc(sizeof(*kd))) == NULL) {
247 (void)fprintf(stderr, "%s: %s\n",
248 errstr, strerror(errno));
251 memset(kd, 0, sizeof(*kd));
252 kd->program = errstr;
253 return (_kvm_open(kd, uf, mf, flag, NULL));
262 error |= close(kd->pmfd);
264 error |= close(kd->vmfd);
266 error |= close(kd->nlfd);
269 if (kd->procbase != 0)
270 free((void *)kd->procbase);
272 free((void *)kd->argv);
279 kvm_nlist(kvm_t *kd, struct nlist *nl)
283 struct kld_sym_lookup lookup;
286 * If we can't use the kld symbol lookup, revert to the
290 return (__fdnlist(kd->nlfd, nl));
293 * We can use the kld lookup syscall. Go through each nlist entry
294 * and look it up with a kldsym(2) syscall.
297 for (p = nl; p->n_name && p->n_name[0]; ++p) {
298 lookup.version = sizeof(lookup);
299 lookup.symname = p->n_name;
303 if (lookup.symname[0] == '_')
306 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
310 p->n_value = lookup.symvalue;
316 * Return the number of entries that weren't found.
318 return ((p - nl) - nvalid);
322 kvm_read(kvm_t *kd, u_long kva, void *buf, size_t len)
329 * We're using /dev/kmem. Just read straight from the
330 * device and let the active kernel do the address translation.
333 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
334 _kvm_err(kd, 0, "invalid address (%x)", kva);
337 cc = read(kd->vmfd, buf, len);
339 _kvm_syserr(kd, 0, "kvm_read");
342 _kvm_err(kd, kd->program, "short read");
349 cc = _kvm_kvatop(kd, kva, &pa);
355 if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) {
356 _kvm_syserr(kd, 0, _PATH_MEM);
359 cc = read(kd->pmfd, cp, cc);
361 _kvm_syserr(kd, kd->program, "kvm_read");
365 * If kvm_kvatop returns a bogus value or our core
366 * file is truncated, we might wind up seeking beyond
367 * the end of the core file in which case the read will
376 return ((char *)cp - (char *)buf);
382 kvm_write(kvm_t *kd, u_long kva, const void *buf, size_t len)
388 * Just like kvm_read, only we write.
391 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
392 _kvm_err(kd, 0, "invalid address (%x)", kva);
395 cc = write(kd->vmfd, buf, len);
397 _kvm_syserr(kd, 0, "kvm_write");
400 _kvm_err(kd, kd->program, "short write");
403 _kvm_err(kd, kd->program,
404 "kvm_write not implemented for dead kernels");