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 * 4. 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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
41 #endif /* LIBC_SCCS and not lint */
43 #include <sys/param.h>
46 #include <sys/ioctl.h>
48 #include <sys/sysctl.h>
49 #include <sys/linker.h>
52 #include <vm/vm_param.h>
54 #include <machine/vmparam.h>
67 #include "kvm_private.h"
69 /* from src/lib/libc/gen/nlist.c */
70 int __fdnlist(int, struct nlist *);
82 * Report an error using printf style arguments. "program" is kd->program
83 * on hard errors, and 0 on soft errors, so that under sun error emulation,
84 * only hard errors are printed out (otherwise, programs like gdb will
85 * generate tons of error messages when trying to access bogus pointers).
88 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
93 if (program != NULL) {
94 (void)fprintf(stderr, "%s: ", program);
95 (void)vfprintf(stderr, fmt, ap);
96 (void)fputc('\n', stderr);
98 (void)vsnprintf(kd->errbuf,
99 sizeof(kd->errbuf), (char *)fmt, ap);
105 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
111 if (program != NULL) {
112 (void)fprintf(stderr, "%s: ", program);
113 (void)vfprintf(stderr, fmt, ap);
114 (void)fprintf(stderr, ": %s\n", strerror(errno));
116 char *cp = kd->errbuf;
118 (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
120 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
133 if ((p = calloc(n, sizeof(char))) == NULL)
134 _kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
140 _kvm_open(kd, uf, mf, flag, errout)
159 else if (strlen(uf) >= MAXPATHLEN) {
160 _kvm_err(kd, kd->program, "exec file name too long");
163 if (flag & ~O_RDWR) {
164 _kvm_err(kd, kd->program, "bad flags arg");
170 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
171 _kvm_syserr(kd, kd->program, "%s", mf);
174 if (fstat(kd->pmfd, &st) < 0) {
175 _kvm_syserr(kd, kd->program, "%s", mf);
178 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
180 _kvm_syserr(kd, kd->program, "empty file");
183 if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
184 _kvm_syserr(kd, kd->program, "%s", mf);
187 if (S_ISCHR(st.st_mode)) {
189 * If this is a character special device, then check that
190 * it's /dev/mem. If so, open kmem too. (Maybe we should
191 * make it work for either /dev/mem or /dev/kmem -- in either
192 * case you're working with a live kernel.)
194 if (strcmp(mf, _PATH_DEVNULL) == 0) {
195 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
197 } else if (strcmp(mf, _PATH_MEM) == 0) {
198 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
199 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
202 if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
203 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
210 * This is a crash dump.
211 * Initialize the virtual address translation machinery,
212 * but first setup the namelist fd.
214 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
215 _kvm_syserr(kd, kd->program, "%s", uf);
218 if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
219 _kvm_syserr(kd, kd->program, "%s", uf);
222 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0)
224 if (_kvm_initvtop(kd) < 0)
229 * Copy out the error if doing sane error semantics.
232 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
238 kvm_openfiles(uf, mf, sf, flag, errout)
241 const char *sf __unused;
247 if ((kd = malloc(sizeof(*kd))) == NULL) {
248 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
251 memset(kd, 0, sizeof(*kd));
253 return (_kvm_open(kd, uf, mf, flag, errout));
257 kvm_open(uf, mf, sf, flag, errstr)
260 const char *sf __unused;
266 if ((kd = malloc(sizeof(*kd))) == NULL) {
268 (void)fprintf(stderr, "%s: %s\n",
269 errstr, strerror(errno));
272 memset(kd, 0, sizeof(*kd));
273 kd->program = errstr;
274 return (_kvm_open(kd, uf, mf, flag, NULL));
284 error |= close(kd->pmfd);
286 error |= close(kd->vmfd);
288 error |= close(kd->nlfd);
291 if (kd->procbase != 0)
292 free((void *)kd->procbase);
294 free((void *) kd->argbuf);
296 free((void *) kd->argspc);
298 free((void *)kd->argv);
311 struct kld_sym_lookup lookup;
315 * If we can't use the kld symbol lookup, revert to the
319 return (__fdnlist(kd->nlfd, nl));
322 * We can use the kld lookup syscall. Go through each nlist entry
323 * and look it up with a kldsym(2) syscall.
326 for (p = nl; p->n_name && p->n_name[0]; ++p) {
327 lookup.version = sizeof(lookup);
328 lookup.symname = p->n_name;
332 if (lookup.symname[0] == '_')
335 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
339 p->n_value = lookup.symvalue;
345 * Return the number of entries that weren't found. If they exist,
346 * also fill internal error buffer.
348 error = ((p - nl) - nvalid);
350 _kvm_syserr(kd, kd->program, "kvm_nlist");
355 kvm_read(kd, kva, buf, len)
366 * We're using /dev/kmem. Just read straight from the
367 * device and let the active kernel do the address translation.
370 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
371 _kvm_err(kd, 0, "invalid address (%x)", kva);
374 cc = read(kd->vmfd, buf, len);
376 _kvm_syserr(kd, 0, "kvm_read");
379 _kvm_err(kd, kd->program, "short read");
386 cc = _kvm_kvatop(kd, kva, &pa);
392 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
393 _kvm_syserr(kd, 0, _PATH_MEM);
396 cc = read(kd->pmfd, cp, cc);
398 _kvm_syserr(kd, kd->program, "kvm_read");
402 * If kvm_kvatop returns a bogus value or our core
403 * file is truncated, we might wind up seeking beyond
404 * the end of the core file in which case the read will
413 return (cp - (char *)buf);
419 kvm_write(kd, kva, buf, len)
429 * Just like kvm_read, only we write.
432 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
433 _kvm_err(kd, 0, "invalid address (%x)", kva);
436 cc = write(kd->vmfd, buf, len);
438 _kvm_syserr(kd, 0, "kvm_write");
441 _kvm_err(kd, kd->program, "short write");
444 _kvm_err(kd, kd->program,
445 "kvm_write not implemented for dead kernels");