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>
49 #include <sys/ioctl.h>
51 #include <sys/sysctl.h>
52 #include <sys/linker.h>
57 #include <vm/vm_param.h>
59 #include <machine/vmparam.h>
73 #include "kvm_private.h"
75 /* from src/lib/libc/gen/nlist.c */
76 int __fdnlist(int, struct nlist *);
88 * Report an error using printf style arguments. "program" is kd->program
89 * on hard errors, and 0 on soft errors, so that under sun error emulation,
90 * only hard errors are printed out (otherwise, programs like gdb will
91 * generate tons of error messages when trying to access bogus pointers).
94 _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...)
99 if (program != NULL) {
100 (void)fprintf(stderr, "%s: ", program);
101 (void)vfprintf(stderr, fmt, ap);
102 (void)fputc('\n', stderr);
104 (void)vsnprintf(kd->errbuf,
105 sizeof(kd->errbuf), (char *)fmt, ap);
111 _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...)
117 if (program != NULL) {
118 (void)fprintf(stderr, "%s: ", program);
119 (void)vfprintf(stderr, fmt, ap);
120 (void)fprintf(stderr, ": %s\n", strerror(errno));
122 char *cp = kd->errbuf;
124 (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap);
126 (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s",
139 if ((p = calloc(n, sizeof(char))) == NULL)
140 _kvm_err(kd, kd->program, "can't allocate %u bytes: %s",
146 _kvm_open(kd, uf, mf, flag, errout)
165 else if (strlen(uf) >= MAXPATHLEN) {
166 _kvm_err(kd, kd->program, "exec file name too long");
169 if (flag & ~O_RDWR) {
170 _kvm_err(kd, kd->program, "bad flags arg");
176 if ((kd->pmfd = open(mf, flag, 0)) < 0) {
177 _kvm_syserr(kd, kd->program, "%s", mf);
180 if (fstat(kd->pmfd, &st) < 0) {
181 _kvm_syserr(kd, kd->program, "%s", mf);
184 if (S_ISREG(st.st_mode) && st.st_size <= 0) {
186 _kvm_syserr(kd, kd->program, "empty file");
189 if (fcntl(kd->pmfd, F_SETFD, FD_CLOEXEC) < 0) {
190 _kvm_syserr(kd, kd->program, "%s", mf);
193 if (S_ISCHR(st.st_mode)) {
195 * If this is a character special device, then check that
196 * it's /dev/mem. If so, open kmem too. (Maybe we should
197 * make it work for either /dev/mem or /dev/kmem -- in either
198 * case you're working with a live kernel.)
200 if (strcmp(mf, _PATH_DEVNULL) == 0) {
201 kd->vmfd = open(_PATH_DEVNULL, O_RDONLY);
203 } else if (strcmp(mf, _PATH_MEM) == 0) {
204 if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) {
205 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
208 if (fcntl(kd->vmfd, F_SETFD, FD_CLOEXEC) < 0) {
209 _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM);
216 * This is a crash dump.
217 * Initialize the virtual address translation machinery,
218 * but first setup the namelist fd.
220 if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) {
221 _kvm_syserr(kd, kd->program, "%s", uf);
224 if (fcntl(kd->nlfd, F_SETFD, FD_CLOEXEC) < 0) {
225 _kvm_syserr(kd, kd->program, "%s", uf);
228 if (strncmp(mf, _PATH_FWMEM, strlen(_PATH_FWMEM)) == 0)
230 if (_kvm_initvtop(kd) < 0)
235 * Copy out the error if doing sane error semantics.
238 strlcpy(errout, kd->errbuf, _POSIX2_LINE_MAX);
244 kvm_openfiles(uf, mf, sf, flag, errout)
247 const char *sf __unused;
253 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
254 (void)strlcpy(errout, strerror(errno), _POSIX2_LINE_MAX);
258 return (_kvm_open(kd, uf, mf, flag, errout));
262 kvm_open(uf, mf, sf, flag, errstr)
265 const char *sf __unused;
271 if ((kd = calloc(1, sizeof(*kd))) == NULL) {
273 (void)fprintf(stderr, "%s: %s\n",
274 errstr, strerror(errno));
277 kd->program = errstr;
278 return (_kvm_open(kd, uf, mf, flag, NULL));
288 error |= close(kd->pmfd);
290 error |= close(kd->vmfd);
292 error |= close(kd->nlfd);
295 if (kd->procbase != 0)
296 free((void *)kd->procbase);
298 free((void *) kd->argbuf);
300 free((void *) kd->argspc);
302 free((void *)kd->argv);
309 * Walk the list of unresolved symbols, generate a new list and prefix the
310 * symbol names, try again, and merge back what we could resolve.
313 kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
314 uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
316 struct nlist *n, *np, *p;
322 * Calculate the space we need to malloc for nlist and names.
323 * We are going to store the name twice for later lookups: once
324 * with the prefix and once the unmodified name delmited by \0.
328 for (p = nl; p->n_name && p->n_name[0]; ++p) {
329 if (p->n_type != N_UNDF)
331 len += sizeof(struct nlist) + strlen(prefix) +
332 2 * (strlen(p->n_name) + 1);
337 /* Add space for the terminating nlist entry. */
338 len += sizeof(struct nlist);
341 /* Alloc one chunk for (nlist, [names]) and setup pointers. */
342 n = np = malloc(len);
346 cp = ce = (char *)np;
347 cp += unresolved * sizeof(struct nlist);
350 /* Generate shortened nlist with special prefix. */
352 for (p = nl; p->n_name && p->n_name[0]; ++p) {
353 if (p->n_type != N_UNDF)
355 bcopy(p, np, sizeof(struct nlist));
356 /* Save the new\0orig. name so we can later match it again. */
357 len = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
358 (prefix[0] != '\0' && p->n_name[0] == '_') ?
359 (p->n_name + 1) : p->n_name, '\0', p->n_name);
368 /* Do lookup on the reduced list. */
370 unresolved = __fdnlist(kd->nlfd, np);
372 /* Check if we could resolve further symbols and update the list. */
373 if (unresolved >= 0 && unresolved < missing) {
374 /* Find the first freshly resolved entry. */
375 for (; np->n_name && np->n_name[0]; np++)
376 if (np->n_type != N_UNDF)
379 * The lists are both in the same order,
380 * so we can walk them in parallel.
382 for (p = nl; np->n_name && np->n_name[0] &&
383 p->n_name && p->n_name[0]; ++p) {
384 if (p->n_type != N_UNDF)
386 /* Skip expanded name and compare to orig. one. */
387 cp = np->n_name + strlen(np->n_name) + 1;
388 if (strcmp(cp, p->n_name))
390 /* Update nlist with new, translated results. */
391 p->n_type = np->n_type;
392 p->n_other = np->n_other;
393 p->n_desc = np->n_desc;
395 p->n_value = (*validate_fn)(kd, np->n_value);
397 p->n_value = np->n_value;
399 /* Find next freshly resolved entry. */
400 for (np++; np->n_name && np->n_name[0]; np++)
401 if (np->n_type != N_UNDF)
405 /* We could assert missing = unresolved here. */
412 _kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
416 struct kld_sym_lookup lookup;
418 char *prefix = "", symname[1024]; /* XXX-BZ symbol name length limit? */
420 * If we can't use the kld symbol lookup, revert to the
424 error = __fdnlist(kd->nlfd, nl);
425 if (error <= 0) /* Hard error or success. */
428 if (_kvm_vnet_initialized(kd, initialize))
429 error = kvm_fdnlist_prefix(kd, nl, error,
430 VNET_SYMPREFIX, _kvm_vnet_validaddr);
436 * We can use the kld lookup syscall. Go through each nlist entry
437 * and look it up with a kldsym(2) syscall.
441 for (p = nl; p->n_name && p->n_name[0]; ++p) {
442 if (p->n_type != N_UNDF)
445 lookup.version = sizeof(lookup);
449 error = snprintf(symname, sizeof(symname), "%s%s", prefix,
450 (prefix[0] != '\0' && p->n_name[0] == '_') ?
451 (p->n_name + 1) : p->n_name);
452 if (error >= sizeof(symname))
455 lookup.symname = symname;
456 if (lookup.symname[0] == '_')
459 if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) {
463 if (_kvm_vnet_initialized(kd, initialize) &&
464 !strcmp(prefix, VNET_SYMPREFIX))
466 _kvm_vnet_validaddr(kd, lookup.symvalue);
468 p->n_value = lookup.symvalue;
475 * Check the number of entries that weren't found. If they exist,
476 * try again with a prefix for virtualized symbol names.
478 error = ((p - nl) - nvalid);
479 if (error && _kvm_vnet_initialized(kd, initialize) &&
480 strcmp(prefix, VNET_SYMPREFIX)) {
481 prefix = VNET_SYMPREFIX;
486 * Return the number of entries that weren't found. If they exist,
487 * also fill internal error buffer.
489 error = ((p - nl) - nvalid);
491 _kvm_syserr(kd, kd->program, "kvm_nlist");
502 * If called via the public interface, permit intialization of
503 * further virtualized modules on demand.
505 return (_kvm_nlist(kd, nl, 1));
509 kvm_read(kd, kva, buf, len)
520 * We're using /dev/kmem. Just read straight from the
521 * device and let the active kernel do the address translation.
524 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
525 _kvm_err(kd, 0, "invalid address (%x)", kva);
528 cc = read(kd->vmfd, buf, len);
530 _kvm_syserr(kd, 0, "kvm_read");
533 _kvm_err(kd, kd->program, "short read");
540 cc = _kvm_kvatop(kd, kva, &pa);
546 if (lseek(kd->pmfd, pa, 0) == -1 && errno != 0) {
547 _kvm_syserr(kd, 0, _PATH_MEM);
550 cc = read(kd->pmfd, cp, cc);
552 _kvm_syserr(kd, kd->program, "kvm_read");
556 * If kvm_kvatop returns a bogus value or our core
557 * file is truncated, we might wind up seeking beyond
558 * the end of the core file in which case the read will
567 return (cp - (char *)buf);
573 kvm_write(kd, kva, buf, len)
583 * Just like kvm_read, only we write.
586 if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) {
587 _kvm_err(kd, 0, "invalid address (%x)", kva);
590 cc = write(kd->vmfd, buf, len);
592 _kvm_syserr(kd, 0, "kvm_write");
595 _kvm_err(kd, kd->program, "short write");
598 _kvm_err(kd, kd->program,
599 "kvm_write not implemented for dead kernels");