Index: head/lib/libkvm/kvm.c =================================================================== --- head/lib/libkvm/kvm.c (revision 55126) +++ head/lib/libkvm/kvm.c (revision 55127) @@ -1,543 +1,452 @@ /*- * Copyright (c) 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software developed by the Computer Systems * Engineering group at Lawrence Berkeley Laboratory under DARPA contract * BG 91-66 and contributed to Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) #if 0 static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94"; #else static const char rcsid[] = - "$FreeBSD$"; + "$FreeBSD$"; #endif #endif /* LIBC_SCCS and not lint */ #include #include #include #include #include #include +#include #include #include #include #include #include -#include #include #include #include #include #include #include #include #include #include #include "kvm_private.h" -static int kvm_dbopen __P((kvm_t *, const char *)); - char * kvm_geterr(kd) kvm_t *kd; { return (kd->errbuf); } #if __STDC__ #include #else #include #endif /* * Report an error using printf style arguments. "program" is kd->program * on hard errors, and 0 on soft errors, so that under sun error emulation, * only hard errors are printed out (otherwise, programs like gdb will * generate tons of error messages when trying to access bogus pointers). */ void #if __STDC__ _kvm_err(kvm_t *kd, const char *program, const char *fmt, ...) #else _kvm_err(kd, program, fmt, va_alist) kvm_t *kd; char *program, *fmt; va_dcl #endif { va_list ap; #ifdef __STDC__ va_start(ap, fmt); #else va_start(ap); #endif if (program != NULL) { (void)fprintf(stderr, "%s: ", program); (void)vfprintf(stderr, fmt, ap); (void)fputc('\n', stderr); } else (void)vsnprintf(kd->errbuf, sizeof(kd->errbuf), (char *)fmt, ap); va_end(ap); } void #if __STDC__ _kvm_syserr(kvm_t *kd, const char *program, const char *fmt, ...) #else _kvm_syserr(kd, program, fmt, va_alist) kvm_t *kd; char *program, *fmt; va_dcl #endif { va_list ap; register int n; #if __STDC__ va_start(ap, fmt); #else va_start(ap); #endif if (program != NULL) { (void)fprintf(stderr, "%s: ", program); (void)vfprintf(stderr, fmt, ap); (void)fprintf(stderr, ": %s\n", strerror(errno)); } else { register char *cp = kd->errbuf; (void)vsnprintf(cp, sizeof(kd->errbuf), (char *)fmt, ap); n = strlen(cp); (void)snprintf(&cp[n], sizeof(kd->errbuf) - n, ": %s", strerror(errno)); } va_end(ap); } void * _kvm_malloc(kd, n) register kvm_t *kd; register size_t n; { void *p; if ((p = calloc(n, sizeof(char))) == NULL) _kvm_err(kd, kd->program, "can't allocate %u bytes: %s", n, strerror(errno)); return (p); } static kvm_t * _kvm_open(kd, uf, mf, flag, errout) register kvm_t *kd; const char *uf; const char *mf; int flag; char *errout; { struct stat st; kd->vmfd = -1; kd->pmfd = -1; kd->nlfd = -1; kd->vmst = 0; - kd->db = 0; kd->procbase = 0; kd->argspc = 0; kd->argv = 0; if (uf == 0) uf = getbootfile(); else if (strlen(uf) >= MAXPATHLEN) { _kvm_err(kd, kd->program, "exec file name too long"); goto failed; } if (flag & ~O_RDWR) { _kvm_err(kd, kd->program, "bad flags arg"); goto failed; } if (mf == 0) mf = _PATH_MEM; if ((kd->pmfd = open(mf, flag, 0)) < 0) { _kvm_syserr(kd, kd->program, "%s", mf); goto failed; } if (fstat(kd->pmfd, &st) < 0) { _kvm_syserr(kd, kd->program, "%s", mf); goto failed; } if (S_ISCHR(st.st_mode)) { /* * If this is a character special device, then check that * it's /dev/mem. If so, open kmem too. (Maybe we should * make it work for either /dev/mem or /dev/kmem -- in either * case you're working with a live kernel.) */ if (strcmp(mf, _PATH_DEVNULL) == 0) { kd->vmfd = open(_PATH_DEVNULL, O_RDONLY); } else if (strcmp(mf, _PATH_MEM) != 0) { _kvm_err(kd, kd->program, "%s: not physical memory device", mf); goto failed; } else { if ((kd->vmfd = open(_PATH_KMEM, flag)) < 0) { _kvm_syserr(kd, kd->program, "%s", _PATH_KMEM); goto failed; } } - /* - * Open kvm nlist database. We go ahead and do this - * here so that we don't have to hold on to the kernel - * path name. Since a kvm application will surely do - * a kvm_nlist(), this probably won't be a wasted effort. - * If the database cannot be opened, open the namelist - * argument so we revert to slow nlist() calls. - */ - if (kvm_dbopen(kd, uf) < 0 && - (kd->nlfd = open(uf, O_RDONLY, 0)) < 0) { - _kvm_syserr(kd, kd->program, "%s", uf); - goto failed; - } } else { /* * This is a crash dump. * Initialize the virtual address translation machinery, * but first setup the namelist fd. */ if ((kd->nlfd = open(uf, O_RDONLY, 0)) < 0) { _kvm_syserr(kd, kd->program, "%s", uf); goto failed; } if (_kvm_initvtop(kd) < 0) goto failed; } return (kd); failed: /* * Copy out the error if doing sane error semantics. */ if (errout != 0) strcpy(errout, kd->errbuf); (void)kvm_close(kd); return (0); } kvm_t * kvm_openfiles(uf, mf, sf, flag, errout) const char *uf; const char *mf; const char *sf; int flag; char *errout; { register kvm_t *kd; if ((kd = malloc(sizeof(*kd))) == NULL) { (void)strcpy(errout, strerror(errno)); return (0); } memset(kd, 0, sizeof(*kd)); kd->program = 0; return (_kvm_open(kd, uf, mf, flag, errout)); } kvm_t * kvm_open(uf, mf, sf, flag, errstr) const char *uf; const char *mf; const char *sf; int flag; const char *errstr; { register kvm_t *kd; if ((kd = malloc(sizeof(*kd))) == NULL) { if (errstr != NULL) (void)fprintf(stderr, "%s: %s\n", errstr, strerror(errno)); return (0); } memset(kd, 0, sizeof(*kd)); kd->program = errstr; return (_kvm_open(kd, uf, mf, flag, NULL)); } int kvm_close(kd) kvm_t *kd; { register int error = 0; if (kd->pmfd >= 0) error |= close(kd->pmfd); if (kd->vmfd >= 0) error |= close(kd->vmfd); if (kd->nlfd >= 0) error |= close(kd->nlfd); - if (kd->db != 0) - error |= (kd->db->close)(kd->db); if (kd->vmst) _kvm_freevtop(kd); if (kd->procbase != 0) free((void *)kd->procbase); if (kd->argv != 0) free((void *)kd->argv); free((void *)kd); return (0); } -/* - * Set up state necessary to do queries on the kernel namelist - * data base. If the data base is out-of-data/incompatible with - * given executable, set up things so we revert to standard nlist call. - * Only called for live kernels. Return 0 on success, -1 on failure. - */ -static int -kvm_dbopen(kd, uf) - kvm_t *kd; - const char *uf; -{ - char *cp; - DBT rec; - int dbversionlen; - struct nlist nitem; - char dbversion[_POSIX2_LINE_MAX]; - char kversion[_POSIX2_LINE_MAX]; - char dbname[MAXPATHLEN]; - - if ((cp = rindex(uf, '/')) != 0) - uf = cp + 1; - - (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", _PATH_VARDB, uf); - kd->db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL); - if (kd->db == 0) - return (-1); - /* - * read version out of database - */ - rec.data = VRS_KEY; - rec.size = sizeof(VRS_KEY) - 1; - if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0)) - goto close; - if (rec.data == 0 || rec.size > sizeof(dbversion)) - goto close; - - bcopy(rec.data, dbversion, rec.size); - dbversionlen = rec.size; - /* - * Read version string from kernel memory. - * Since we are dealing with a live kernel, we can call kvm_read() - * at this point. - */ - rec.data = VRS_SYM; - rec.size = sizeof(VRS_SYM) - 1; - if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0)) - goto close; - if (rec.data == 0 || rec.size != sizeof(struct nlist)) - goto close; - bcopy((char *)rec.data, (char *)&nitem, sizeof(nitem)); - if (kvm_read(kd, (u_long)nitem.n_value, kversion, dbversionlen) != - dbversionlen) - goto close; - /* - * If they match, we win - otherwise clear out kd->db so - * we revert to slow nlist(). - */ - if (bcmp(dbversion, kversion, dbversionlen) == 0) - return (0); -close: - (void)(kd->db->close)(kd->db); - kd->db = 0; - - return (-1); -} - int kvm_nlist(kd, nl) kvm_t *kd; struct nlist *nl; { register struct nlist *p; register int nvalid; + struct kld_sym_lookup lookup; /* - * If we can't use the data base, revert to the + * If we can't use the kld symbol lookup, revert to the * slow library call. */ - if (kd->db == 0) + if (!ISALIVE(kd)) return (__fdnlist(kd->nlfd, nl)); /* - * We can use the kvm data base. Go through each nlist entry - * and look it up with a db query. + * We can use the kld lookup syscall. Go through each nlist entry + * and look it up with a kldsym(2) syscall. */ nvalid = 0; for (p = nl; p->n_name && p->n_name[0]; ++p) { - register int len; - DBT rec; + lookup.version = sizeof(lookup); + lookup.symname = p->n_name; + lookup.symvalue = 0; + lookup.symsize = 0; - if ((len = strlen(p->n_name)) > 4096) { - /* sanity */ - _kvm_err(kd, kd->program, "symbol too large"); - return (-1); + if (lookup.symname[0] == '_') + lookup.symname++; + + if (kldsym(0, KLDSYM_LOOKUP, &lookup) != -1) { + p->n_type = N_TEXT; + p->n_other = 0; + p->n_desc = 0; + p->n_value = lookup.symvalue; + ++nvalid; + /* lookup.symsize */ } - rec.data = p->n_name; - rec.size = len; - if ((kd->db->get)(kd->db, (DBT *)&rec, (DBT *)&rec, 0)) - continue; - if (rec.data == 0 || rec.size != sizeof(struct nlist)) - continue; - ++nvalid; - /* - * Avoid alignment issues. - */ - bcopy((char *)&((struct nlist *)rec.data)->n_type, - (char *)&p->n_type, - sizeof(p->n_type)); - bcopy((char *)&((struct nlist *)rec.data)->n_value, - (char *)&p->n_value, - sizeof(p->n_value)); } /* * Return the number of entries that weren't found. */ return ((p - nl) - nvalid); } ssize_t kvm_read(kd, kva, buf, len) kvm_t *kd; register u_long kva; register void *buf; register size_t len; { register int cc; register void *cp; if (ISALIVE(kd)) { /* * We're using /dev/kmem. Just read straight from the * device and let the active kernel do the address translation. */ errno = 0; if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { _kvm_err(kd, 0, "invalid address (%x)", kva); return (0); } cc = read(kd->vmfd, buf, len); if (cc < 0) { _kvm_syserr(kd, 0, "kvm_read"); return (0); } else if (cc < len) _kvm_err(kd, kd->program, "short read"); return (cc); } else { cp = buf; while (len > 0) { u_long pa; cc = _kvm_kvatop(kd, kva, &pa); if (cc == 0) return (0); if (cc > len) cc = len; errno = 0; if (lseek(kd->pmfd, (off_t)pa, 0) == -1 && errno != 0) { _kvm_syserr(kd, 0, _PATH_MEM); break; } cc = read(kd->pmfd, cp, cc); if (cc < 0) { _kvm_syserr(kd, kd->program, "kvm_read"); break; } /* * If kvm_kvatop returns a bogus value or our core * file is truncated, we might wind up seeking beyond * the end of the core file in which case the read will * return 0 (EOF). */ if (cc == 0) break; (char *)cp += cc; kva += cc; len -= cc; } return ((char *)cp - (char *)buf); } /* NOTREACHED */ } ssize_t kvm_write(kd, kva, buf, len) kvm_t *kd; register u_long kva; register const void *buf; register size_t len; { register int cc; if (ISALIVE(kd)) { /* * Just like kvm_read, only we write. */ errno = 0; if (lseek(kd->vmfd, (off_t)kva, 0) == -1 && errno != 0) { _kvm_err(kd, 0, "invalid address (%x)", kva); return (0); } cc = write(kd->vmfd, buf, len); if (cc < 0) { _kvm_syserr(kd, 0, "kvm_write"); return (0); } else if (cc < len) _kvm_err(kd, kd->program, "short write"); return (cc); } else { _kvm_err(kd, kd->program, "kvm_write not implemented for dead kernels"); return (0); } /* NOTREACHED */ } Index: head/lib/libkvm/kvm_alpha.c =================================================================== --- head/lib/libkvm/kvm_alpha.c (revision 55126) +++ head/lib/libkvm/kvm_alpha.c (revision 55127) @@ -1,212 +1,211 @@ /* $FreeBSD$ */ /* $NetBSD: kvm_alpha.c,v 1.7.2.1 1997/11/02 20:34:26 mellon Exp $ */ /* * Copyright (c) 1994, 1995 Carnegie-Mellon University. * All rights reserved. * * Author: Chris G. Demetriou * * Permission to use, copy, modify and distribute this software and * its documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie the * rights to redistribute these changes. */ #include #include #include #include #include #include #include #include #include #include #include #include -#include #include #include #include "kvm_private.h" static off_t _kvm_pa2off(kvm_t *kd, u_long pa); struct vmstate { u_int64_t lev1map_pa; /* PA of Lev1map */ u_int64_t page_size; /* Page size */ u_int64_t nmemsegs; /* Number of RAM segm */ }; void _kvm_freevtop(kd) kvm_t *kd; { /* Not actually used for anything right now, but safe. */ if (kd->vmst != 0) free(kd->vmst); } int _kvm_initvtop(kd) kvm_t *kd; { struct vmstate *vm; struct nlist nlist[2]; u_long pa; vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); if (vm == 0) { _kvm_err(kd, kd->program, "cannot allocate vm"); return (-1); } kd->vmst = vm; vm->page_size = ALPHA_PGBYTES; nlist[0].n_name = "_Lev1map"; nlist[1].n_name = 0; if (kvm_nlist(kd, nlist) != 0) { _kvm_err(kd, kd->program, "bad namelist"); return (-1); } if(!ISALIVE(kd)) { if (kvm_read(kd, (nlist[0].n_value), &pa, sizeof(pa)) != sizeof(pa)) { _kvm_err(kd, kd->program, "cannot read Lev1map"); return (-1); } } else if (kvm_read(kd, (nlist[0].n_value), &pa, sizeof(pa)) != sizeof(pa)) { _kvm_err(kd, kd->program, "cannot read Lev1map"); return (-1); } vm->lev1map_pa = pa; return (0); } int _kvm_kvatop(kd, va, pa) kvm_t *kd; u_long va; u_long *pa; { u_int64_t lev1map_pa; /* PA of Lev1map */ u_int64_t page_size; int rv, page_off; alpha_pt_entry_t pte; off_t pteoff; struct vmstate *vm; vm = kd->vmst ; if (ISALIVE(kd)) { _kvm_err(kd, 0, "vatop called in live kernel!"); return(0); } lev1map_pa = vm->lev1map_pa; page_size = vm->page_size; page_off = va & (page_size - 1); if (va >= ALPHA_K0SEG_BASE && va <= ALPHA_K0SEG_END) { /* * Direct-mapped address: just convert it. */ *pa = ALPHA_K0SEG_TO_PHYS(va); rv = page_size - page_off; } else if (va >= ALPHA_K1SEG_BASE && va <= ALPHA_K1SEG_END) { /* * Real kernel virtual address: do the translation. */ #define PTMASK ((1 << ALPHA_PTSHIFT) - 1) #define pmap_lev1_index(va) (((va) >> ALPHA_L1SHIFT) & PTMASK) #define pmap_lev2_index(va) (((va) >> ALPHA_L2SHIFT) & PTMASK) #define pmap_lev3_index(va) (((va) >> ALPHA_L3SHIFT) & PTMASK) /* Find and read the L1 PTE. */ pteoff = lev1map_pa + pmap_lev1_index(va) * sizeof(alpha_pt_entry_t); if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 || read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) { _kvm_syserr(kd, 0, "could not read L1 PTE"); goto lose; } /* Find and read the L2 PTE. */ if ((pte & ALPHA_PTE_VALID) == 0) { _kvm_err(kd, 0, "invalid translation (invalid L1 PTE)"); goto lose; } pteoff = ALPHA_PTE_TO_PFN(pte) * page_size + pmap_lev2_index(va) * sizeof(alpha_pt_entry_t); if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 || read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) { _kvm_syserr(kd, 0, "could not read L2 PTE"); goto lose; } /* Find and read the L3 PTE. */ if ((pte & ALPHA_PTE_VALID) == 0) { _kvm_err(kd, 0, "invalid translation (invalid L2 PTE)"); goto lose; } pteoff = ALPHA_PTE_TO_PFN(pte) * page_size + pmap_lev3_index(va) * sizeof(alpha_pt_entry_t); if (lseek(kd->pmfd, _kvm_pa2off(kd, pteoff), 0) == -1 || read(kd->pmfd, (char *)&pte, sizeof(pte)) != sizeof(pte)) { _kvm_syserr(kd, 0, "could not read L3 PTE"); goto lose; } /* Fill in the PA. */ if ((pte & ALPHA_PTE_VALID) == 0) { _kvm_err(kd, 0, "invalid translation (invalid L3 PTE)"); goto lose; } *pa = ALPHA_PTE_TO_PFN(pte) * page_size + page_off; rv = page_size - page_off; } else { /* * Bogus address (not in KV space): punt. */ _kvm_err(kd, 0, "invalid kernel virtual address"); lose: *pa = -1; rv = 0; } return (rv); } /* * Translate a physical address to a file-offset in the crash-dump. */ off_t _kvm_pa2off(kd, pa) kvm_t *kd; u_long pa; { return ALPHA_K0SEG_TO_PHYS(pa); } Index: head/lib/libkvm/kvm_amd64.c =================================================================== --- head/lib/libkvm/kvm_amd64.c (revision 55126) +++ head/lib/libkvm/kvm_amd64.c (revision 55127) @@ -1,194 +1,198 @@ /*- * Copyright (c) 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software developed by the Computer Systems * Engineering group at Lawrence Berkeley Laboratory under DARPA contract * BG 91-66 and contributed to Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) +#if 0 static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* LIBC_SCCS and not lint */ /* * i386 machine dependent routines for kvm. Hopefully, the forthcoming * vm code will one day obsolete this module. */ #include #include #include #include #include #include #include #include #include #include #include -#include #include "kvm_private.h" #ifndef btop #define btop(x) (i386_btop(x)) #define ptob(x) (i386_ptob(x)) #endif struct vmstate { pd_entry_t *PTD; }; void _kvm_freevtop(kvm_t *kd) { if (kd->vmst != 0) { if (kd->vmst->PTD) { free(kd->vmst->PTD); } free(kd->vmst); } } int _kvm_initvtop(kvm_t *kd) { struct vmstate *vm; struct nlist nlist[2]; u_long pa; pd_entry_t *PTD; vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); if (vm == 0) { _kvm_err(kd, kd->program, "cannot allocate vm"); return (-1); } kd->vmst = vm; vm->PTD = 0; nlist[0].n_name = "_IdlePTD"; nlist[1].n_name = 0; if (kvm_nlist(kd, nlist) != 0) { _kvm_err(kd, kd->program, "bad namelist"); return (-1); } if (kvm_read(kd, (nlist[0].n_value - KERNBASE), &pa, sizeof(pa)) != sizeof(pa)) { _kvm_err(kd, kd->program, "cannot read IdlePTD"); return (-1); } PTD = _kvm_malloc(kd, PAGE_SIZE); if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) { _kvm_err(kd, kd->program, "cannot read PTD"); return (-1); } vm->PTD = PTD; return (0); } static int _kvm_vatop(kvm_t *kd, u_long va, u_long *pa) { struct vmstate *vm; u_long offset; u_long pte_pa; pd_entry_t pde; pt_entry_t pte; u_long pdeindex; u_long pteindex; int i; if (ISALIVE(kd)) { _kvm_err(kd, 0, "vatop called in live kernel!"); return((off_t)0); } vm = kd->vmst; offset = va & (PAGE_SIZE - 1); /* * If we are initializing (kernel page table descriptor pointer * not yet set) then return pa == va to avoid infinite recursion. */ if (vm->PTD == 0) { *pa = va; return (PAGE_SIZE - offset); } pdeindex = va >> PDRSHIFT; pde = vm->PTD[pdeindex]; if (((u_long)pde & PG_V) == 0) goto invalid; if ((u_long)pde & PG_PS) { /* * No second-level page table; ptd describes one 4MB page. * (We assume that the kernel wouldn't set PG_PS without enabling * it cr0, and that the kernel doesn't support 36-bit physical * addresses). */ #define PAGE4M_MASK (NBPDR - 1) #define PG_FRAME4M (~PAGE4M_MASK) *pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK); return (NBPDR - (va & PAGE4M_MASK)); } pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1); pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t)); /* XXX This has to be a physical address read, kvm_read is virtual */ if (lseek(kd->pmfd, pte_pa, 0) == -1) { _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek"); goto invalid; } if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) { _kvm_syserr(kd, kd->program, "_kvm_vatop: read"); goto invalid; } if (((u_long)pte & PG_V) == 0) goto invalid; *pa = ((u_long)pte & PG_FRAME) + offset; return (PAGE_SIZE - offset); invalid: _kvm_err(kd, 0, "invalid address (%x)", va); return (0); } int _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa) { return (_kvm_vatop(kd, va, pa)); } Index: head/lib/libkvm/kvm_file.c =================================================================== --- head/lib/libkvm/kvm_file.c (revision 55126) +++ head/lib/libkvm/kvm_file.c (revision 55127) @@ -1,188 +1,193 @@ /*- * Copyright (c) 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) +#if 0 static char sccsid[] = "@(#)kvm_file.c 8.1 (Berkeley) 6/4/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* LIBC_SCCS and not lint */ /* * File list interface for kvm. pstat, fstat and netstat are * users of this code, so we've factored it out into a separate module. * Thus, we keep this grunge out of the other kvm applications (i.e., * most other applications are interested only in open/close/read/nlist). */ #include #include #include #define KERNEL #include #undef KERNEL #include #include #include #include #include #include #include #include #include #include #include #include "kvm_private.h" #define KREAD(kd, addr, obj) \ (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj)) /* * Get file structures. */ static int kvm_deadfiles(kd, op, arg, filehead_o, nfiles) kvm_t *kd; int op, arg, nfiles; long filehead_o; { int buflen = kd->arglen, n = 0; struct file *fp; register char *where = kd->argspc; struct filelist filehead; /* * first copyout filehead */ if (buflen > sizeof (filehead)) { if (KREAD(kd, filehead_o, &filehead)) { _kvm_err(kd, kd->program, "can't read filehead"); return (0); } buflen -= sizeof (filehead); where += sizeof (filehead); *(struct filelist *)kd->argspc = filehead; } /* * followed by an array of file structures */ for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) { if (buflen > sizeof (struct file)) { if (KREAD(kd, (long)fp, ((struct file *)where))) { _kvm_err(kd, kd->program, "can't read kfp"); return (0); } buflen -= sizeof (struct file); fp = (struct file *)where; where += sizeof (struct file); n++; } } if (n != nfiles) { _kvm_err(kd, kd->program, "inconsistant nfiles"); return (0); } return (nfiles); } char * kvm_getfiles(kd, op, arg, cnt) kvm_t *kd; int op, arg; int *cnt; { int mib[2], st, nfiles; size_t size; struct file *fp, *fplim; struct filelist filehead; if (ISALIVE(kd)) { size = 0; mib[0] = CTL_KERN; mib[1] = KERN_FILE; st = sysctl(mib, 2, NULL, &size, NULL, 0); if (st == -1) { _kvm_syserr(kd, kd->program, "kvm_getprocs"); return (0); } if (kd->argspc == 0) kd->argspc = (char *)_kvm_malloc(kd, size); else if (kd->arglen < size) kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size); if (kd->argspc == 0) return (0); kd->arglen = size; st = sysctl(mib, 2, kd->argspc, &size, NULL, 0); if (st == -1 || size < sizeof(filehead)) { _kvm_syserr(kd, kd->program, "kvm_getfiles"); return (0); } filehead = *(struct filelist *)kd->argspc; fp = (struct file *)(kd->argspc + sizeof (filehead)); fplim = (struct file *)(kd->argspc + size); for (nfiles = 0; filehead.lh_first && (fp < fplim); nfiles++, fp++) filehead.lh_first = fp->f_list.le_next; } else { struct nlist nl[3], *p; nl[0].n_name = "_filehead"; nl[1].n_name = "_nfiles"; nl[2].n_name = 0; if (kvm_nlist(kd, nl) != 0) { for (p = nl; p->n_type != 0; ++p) ; _kvm_err(kd, kd->program, "%s: no such symbol", p->n_name); return (0); } if (KREAD(kd, nl[0].n_value, &nfiles)) { _kvm_err(kd, kd->program, "can't read nfiles"); return (0); } size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file); if (kd->argspc == 0) kd->argspc = (char *)_kvm_malloc(kd, size); else if (kd->arglen < size) kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size); if (kd->argspc == 0) return (0); kd->arglen = size; nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles); if (nfiles == 0) return (0); } *cnt = nfiles; return (kd->argspc); } Index: head/lib/libkvm/kvm_getloadavg.c =================================================================== --- head/lib/libkvm/kvm_getloadavg.c (revision 55126) +++ head/lib/libkvm/kvm_getloadavg.c (revision 55127) @@ -1,106 +1,110 @@ /*- * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) +#if 0 static char sccsid[] = "@(#)kvm_getloadavg.c 8.1 (Berkeley) 6/4/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* LIBC_SCCS and not lint */ #include #include #include #include #include #include #include -#include #include #include #include #include #include "kvm_private.h" static struct nlist nl[] = { { "_averunnable" }, #define X_AVERUNNABLE 0 { "_fscale" }, #define X_FSCALE 1 { "" }, }; /* * kvm_getloadavg() -- Get system load averages, from live or dead kernels. * * Put `nelem' samples into `loadavg' array. * Return number of samples retrieved, or -1 on error. */ int kvm_getloadavg(kd, loadavg, nelem) kvm_t *kd; double loadavg[]; int nelem; { struct loadavg loadinfo; struct nlist *p; int fscale, i; if (ISALIVE(kd)) return (getloadavg(loadavg, nelem)); if (kvm_nlist(kd, nl) != 0) { for (p = nl; p->n_type != 0; ++p); _kvm_err(kd, kd->program, "%s: no such symbol", p->n_name); return (-1); } #define KREAD(kd, addr, obj) \ (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) if (KREAD(kd, nl[X_AVERUNNABLE].n_value, &loadinfo)) { _kvm_err(kd, kd->program, "can't read averunnable"); return (-1); } /* * Old kernels have fscale separately; if not found assume * running new format. */ if (!KREAD(kd, nl[X_FSCALE].n_value, &fscale)) loadinfo.fscale = fscale; nelem = MIN(nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t)); for (i = 0; i < nelem; i++) loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale; return (nelem); } Index: head/lib/libkvm/kvm_getswapinfo.c =================================================================== --- head/lib/libkvm/kvm_getswapinfo.c (revision 55126) +++ head/lib/libkvm/kvm_getswapinfo.c (revision 55127) @@ -1,420 +1,419 @@ /* * Copyright (c) 1999, Matthew Dillon. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided under the terms of the BSD * Copyright as found in /usr/src/COPYRIGHT in the FreeBSD source tree. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1999\n" "Matthew Dillon. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include -#include #include #include #include #include #include static struct nlist kvm_swap_nl[] = { { "_swapblist" }, /* new radix swap list */ { "_swdevt" }, /* list of swap devices and sizes */ { "_nswdev" }, /* number of swap devices */ { "_dmmax" }, /* maximum size of a swap block */ { "" } }; #define NL_SWAPBLIST 0 #define NL_SWDEVT 1 #define NL_NSWDEV 2 #define NL_DMMAX 3 static int kvm_swap_nl_cached = 0; static int nswdev; static int unswdev; static int dmmax; static void getswapinfo_radix(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags); #define SVAR(var) __STRING(var) /* to force expansion */ #define KGET(idx, var) \ KGET1(idx, &var, sizeof(var), SVAR(var)) #define KGET1(idx, p, s, msg) \ KGET2(kvm_swap_nl[idx].n_value, p, s, msg) #define KGET2(addr, p, s, msg) \ if (kvm_read(kd, (u_long)(addr), p, s) != s) \ warnx("cannot read %s: %s", msg, kvm_geterr(kd)) #define KGETN(idx, var) \ KGET1N(idx, &var, sizeof(var), SVAR(var)) #define KGET1N(idx, p, s, msg) \ KGET2N(kvm_swap_nl[idx].n_value, p, s, msg) #define KGET2N(addr, p, s, msg) \ ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0) #define KGETRET(addr, p, s, msg) \ if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ return (0); \ } int kvm_getswapinfo( kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags ) { int ti = 0; /* * clear cache */ if (kd == NULL) { kvm_swap_nl_cached = 0; return(0); } /* * namelist */ if (kvm_swap_nl_cached == 0) { struct swdevt *sw; if (kvm_nlist(kd, kvm_swap_nl) < 0) return(-1); /* * required entries */ if ( kvm_swap_nl[NL_SWDEVT].n_value == 0 || kvm_swap_nl[NL_NSWDEV].n_value == 0 || kvm_swap_nl[NL_DMMAX].n_value == 0 || kvm_swap_nl[NL_SWAPBLIST].n_type == 0 ) { return(-1); } /* * get globals, type of swap */ KGET(NL_NSWDEV, nswdev); KGET(NL_DMMAX, dmmax); /* * figure out how many actual swap devices are enabled */ KGET(NL_SWDEVT, sw); for (unswdev = nswdev - 1; unswdev >= 0; --unswdev) { struct swdevt swinfo; KGET2(&sw[unswdev], &swinfo, sizeof(swinfo), "swinfo"); if (swinfo.sw_nblks) break; } ++unswdev; kvm_swap_nl_cached = 1; } { struct swdevt *sw; int i; ti = unswdev; if (ti >= swap_max) ti = swap_max - 1; if (ti >= 0) bzero(swap_ary, sizeof(struct kvm_swap) * (ti + 1)); KGET(NL_SWDEVT, sw); for (i = 0; i < unswdev; ++i) { struct swdevt swinfo; int ttl; KGET2(&sw[i], &swinfo, sizeof(swinfo), "swinfo"); /* * old style: everything in DEV_BSIZE'd chunks, * convert to pages. * * new style: swinfo in DEV_BSIZE'd chunks but dmmax * in pages. * * The first dmmax is never allocating to avoid * trashing the disklabels */ ttl = swinfo.sw_nblks - dmmax; if (ttl == 0) continue; if (i < ti) { swap_ary[i].ksw_total = ttl; swap_ary[i].ksw_used = ttl; swap_ary[i].ksw_flags = swinfo.sw_flags; if (swinfo.sw_dev == NODEV) { snprintf( swap_ary[i].ksw_devname, sizeof(swap_ary[i].ksw_devname), "%s", "[NFS swap]" ); } else { snprintf( swap_ary[i].ksw_devname, sizeof(swap_ary[i].ksw_devname), "%s%s", ((flags & SWIF_DEV_PREFIX) ? "/dev/" : ""), devname(swinfo.sw_dev, S_IFCHR) ); } } if (ti >= 0) { swap_ary[ti].ksw_total += ttl; swap_ary[ti].ksw_used += ttl; } } } getswapinfo_radix(kd, swap_ary, swap_max, flags); return(ti); } /* * scanradix() - support routine for radix scanner */ #define TABME tab, tab, "" static int scanradix( blmeta_t *scan, daddr_t blk, daddr_t radix, daddr_t skip, daddr_t count, kvm_t *kd, int dmmax, int nswdev, struct kvm_swap *swap_ary, int swap_max, int tab, int flags ) { blmeta_t meta; int ti = (unswdev >= swap_max) ? swap_max - 1 : unswdev; KGET2(scan, &meta, sizeof(meta), "blmeta_t"); /* * Terminator */ if (meta.bm_bighint == (daddr_t)-1) { if (flags & SWIF_DUMP_TREE) { printf("%*.*s(0x%06x,%d) Terminator\n", TABME, blk, radix ); } return(-1); } if (radix == BLIST_BMAP_RADIX) { /* * Leaf bitmap */ int i; if (flags & SWIF_DUMP_TREE) { printf("%*.*s(0x%06x,%d) Bitmap %08x big=%d\n", TABME, blk, radix, (int)meta.u.bmu_bitmap, meta.bm_bighint ); } /* * If not all allocated, count. */ if (meta.u.bmu_bitmap != 0) { for (i = 0; i < BLIST_BMAP_RADIX && i < count; ++i) { /* * A 0 bit means allocated */ if ((meta.u.bmu_bitmap & (1 << i))) { int t = 0; if (nswdev) t = (blk + i) / dmmax % nswdev; if (t < ti) --swap_ary[t].ksw_used; if (ti >= 0) --swap_ary[ti].ksw_used; } } } } else if (meta.u.bmu_avail == radix) { /* * Meta node if all free */ if (flags & SWIF_DUMP_TREE) { printf("%*.*s(0x%06x,%d) Submap ALL-FREE {\n", TABME, blk, radix, (int)meta.u.bmu_avail, meta.bm_bighint ); } /* * Note: both dmmax and radix are powers of 2. However, dmmax * may be larger then radix so use a smaller increment if * necessary. */ { int t; int tinc = dmmax; while (tinc > radix) tinc >>= 1; for (t = blk; t < blk + radix; t += tinc) { int u = (nswdev) ? (t / dmmax % nswdev) : 0; if (u < ti) swap_ary[u].ksw_used -= tinc; if (ti >= 0) swap_ary[ti].ksw_used -= tinc; } } } else if (meta.u.bmu_avail == 0) { /* * Meta node if all used */ if (flags & SWIF_DUMP_TREE) { printf("%*.*s(0x%06x,%d) Submap ALL-ALLOCATED\n", TABME, blk, radix, (int)meta.u.bmu_avail, meta.bm_bighint ); } } else { /* * Meta node if not all free */ int i; int next_skip; if (flags & SWIF_DUMP_TREE) { printf("%*.*s(0x%06x,%d) Submap avail=%d big=%d {\n", TABME, blk, radix, (int)meta.u.bmu_avail, meta.bm_bighint ); } radix >>= BLIST_META_RADIX_SHIFT; next_skip = skip >> BLIST_META_RADIX_SHIFT; for (i = 1; i <= skip; i += next_skip) { int r; daddr_t vcount = (count > radix) ? radix : count; r = scanradix( &scan[i], blk, radix, next_skip - 1, vcount, kd, dmmax, nswdev, swap_ary, swap_max, tab + 4, flags ); if (r < 0) break; blk += radix; } if (flags & SWIF_DUMP_TREE) { printf("%*.*s}\n", TABME); } } return(0); } static void getswapinfo_radix(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags) { struct blist *swapblist = NULL; struct blist blcopy = { 0 }; KGET(NL_SWAPBLIST, swapblist); if (swapblist == NULL) { if (flags & SWIF_DUMP_TREE) printf("radix tree: NULL - no swap in system\n"); return; } KGET2(swapblist, &blcopy, sizeof(blcopy), "*swapblist"); if (flags & SWIF_DUMP_TREE) { printf("radix tree: %d/%d/%d blocks, %dK wired\n", blcopy.bl_free, blcopy.bl_blocks, blcopy.bl_radix, (blcopy.bl_rootblks * sizeof(blmeta_t) + 1023)/ 1024 ); } scanradix( blcopy.bl_root, 0, blcopy.bl_radix, blcopy.bl_skip, blcopy.bl_rootblks, kd, dmmax, nswdev, swap_ary, swap_max, 0, flags ); } Index: head/lib/libkvm/kvm_i386.c =================================================================== --- head/lib/libkvm/kvm_i386.c (revision 55126) +++ head/lib/libkvm/kvm_i386.c (revision 55127) @@ -1,194 +1,198 @@ /*- * Copyright (c) 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software developed by the Computer Systems * Engineering group at Lawrence Berkeley Laboratory under DARPA contract * BG 91-66 and contributed to Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) +#if 0 static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* LIBC_SCCS and not lint */ /* * i386 machine dependent routines for kvm. Hopefully, the forthcoming * vm code will one day obsolete this module. */ #include #include #include #include #include #include #include #include #include #include #include -#include #include "kvm_private.h" #ifndef btop #define btop(x) (i386_btop(x)) #define ptob(x) (i386_ptob(x)) #endif struct vmstate { pd_entry_t *PTD; }; void _kvm_freevtop(kvm_t *kd) { if (kd->vmst != 0) { if (kd->vmst->PTD) { free(kd->vmst->PTD); } free(kd->vmst); } } int _kvm_initvtop(kvm_t *kd) { struct vmstate *vm; struct nlist nlist[2]; u_long pa; pd_entry_t *PTD; vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); if (vm == 0) { _kvm_err(kd, kd->program, "cannot allocate vm"); return (-1); } kd->vmst = vm; vm->PTD = 0; nlist[0].n_name = "_IdlePTD"; nlist[1].n_name = 0; if (kvm_nlist(kd, nlist) != 0) { _kvm_err(kd, kd->program, "bad namelist"); return (-1); } if (kvm_read(kd, (nlist[0].n_value - KERNBASE), &pa, sizeof(pa)) != sizeof(pa)) { _kvm_err(kd, kd->program, "cannot read IdlePTD"); return (-1); } PTD = _kvm_malloc(kd, PAGE_SIZE); if (kvm_read(kd, pa, PTD, PAGE_SIZE) != PAGE_SIZE) { _kvm_err(kd, kd->program, "cannot read PTD"); return (-1); } vm->PTD = PTD; return (0); } static int _kvm_vatop(kvm_t *kd, u_long va, u_long *pa) { struct vmstate *vm; u_long offset; u_long pte_pa; pd_entry_t pde; pt_entry_t pte; u_long pdeindex; u_long pteindex; int i; if (ISALIVE(kd)) { _kvm_err(kd, 0, "vatop called in live kernel!"); return((off_t)0); } vm = kd->vmst; offset = va & (PAGE_SIZE - 1); /* * If we are initializing (kernel page table descriptor pointer * not yet set) then return pa == va to avoid infinite recursion. */ if (vm->PTD == 0) { *pa = va; return (PAGE_SIZE - offset); } pdeindex = va >> PDRSHIFT; pde = vm->PTD[pdeindex]; if (((u_long)pde & PG_V) == 0) goto invalid; if ((u_long)pde & PG_PS) { /* * No second-level page table; ptd describes one 4MB page. * (We assume that the kernel wouldn't set PG_PS without enabling * it cr0, and that the kernel doesn't support 36-bit physical * addresses). */ #define PAGE4M_MASK (NBPDR - 1) #define PG_FRAME4M (~PAGE4M_MASK) *pa = ((u_long)pde & PG_FRAME4M) + (va & PAGE4M_MASK); return (NBPDR - (va & PAGE4M_MASK)); } pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1); pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t)); /* XXX This has to be a physical address read, kvm_read is virtual */ if (lseek(kd->pmfd, pte_pa, 0) == -1) { _kvm_syserr(kd, kd->program, "_kvm_vatop: lseek"); goto invalid; } if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) { _kvm_syserr(kd, kd->program, "_kvm_vatop: read"); goto invalid; } if (((u_long)pte & PG_V) == 0) goto invalid; *pa = ((u_long)pte & PG_FRAME) + offset; return (PAGE_SIZE - offset); invalid: _kvm_err(kd, 0, "invalid address (%x)", va); return (0); } int _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa) { return (_kvm_vatop(kd, va, pa)); } Index: head/lib/libkvm/kvm_nlist.3 =================================================================== --- head/lib/libkvm/kvm_nlist.3 (revision 55126) +++ head/lib/libkvm/kvm_nlist.3 (revision 55127) @@ -1,89 +1,87 @@ .\" Copyright (c) 1992, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software developed by the Computer Systems .\" Engineering group at Lawrence Berkeley Laboratory under DARPA contract .\" BG 91-66 and contributed to Berkeley. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)kvm_nlist.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" .Dd June 4, 1993 .Dt KVM_NLIST 3 .Os .Sh NAME .Nm kvm_nlist .Nd retrieve symbol table names from a kernel image .Sh SYNOPSIS .Fd #include .Fd #include .Ft int .Fn kvm_nlist "kvm_t *kd" "struct nlist *nl" .Sh DESCRIPTION .Fn kvm_nlist retrieves the symbol table entries indicated by the name list argument .Fa \&nl . This argument points to an array of nlist structures, terminated by an entry whose n_name field is .Dv NULL (see .Xr nlist 3 ) . Each symbol is looked up using the n_name field, and if found, the corresponding n_type and n_value fields are filled in. These fields are set to 0 if the symbol is not found. .Pp -The program -.Xr kvm_mkdb 8 -builds a database from the running kernel's namelist. -If the database matches the opened kernel, -.Fn kvm_nlist -uses it to speed lookups. +.Xr kldsym 2 +is used to locate the symbol. This is a less than perfect emulation +of the nlist values but has the advantage of being aware of kernel modules +and is reasonably fast. .Sh RETURN VALUES The .Fn kvm_nlist function returns the number of invalid entries found. If the kernel symbol table was unreadable, -1 is returned. .Sh FILES .Bl -tag -width /var/db/kvm_kernel.db -compact .It Pa /var/db/kvm_kernel.db .El .Sh SEE ALSO +.Xr kldsym 2 , .Xr kvm 3 , .Xr kvm_close 3 , .Xr kvm_getargv 3 , .Xr kvm_getenvv 3 , .Xr kvm_geterr 3 , .Xr kvm_getprocs 3 , .Xr kvm_open 3 , .Xr kvm_openfiles 3 , .Xr kvm_read 3 , -.Xr kvm_write 3 , -.Xr kvm_mkdb 8 +.Xr kvm_write 3 Index: head/lib/libkvm/kvm_private.h =================================================================== --- head/lib/libkvm/kvm_private.h (revision 55126) +++ head/lib/libkvm/kvm_private.h (revision 55127) @@ -1,82 +1,82 @@ /*- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software developed by the Computer Systems * Engineering group at Lawrence Berkeley Laboratory under DARPA contract * BG 91-66 and contributed to Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)kvm_private.h 8.1 (Berkeley) 6/4/93 + * $FreeBSD$ */ struct __kvm { /* * a string to be prepended to error messages * provided for compatibility with sun's interface * if this value is null, errors are saved in errbuf[] */ const char *program; char *errp; /* XXX this can probably go away */ char errbuf[_POSIX2_LINE_MAX]; - DB *db; #define ISALIVE(kd) ((kd)->vmfd >= 0) int pmfd; /* physical memory file (or crashdump) */ int vmfd; /* virtual memory file (-1 if crashdump) */ int unused; /* was: swap file (e.g., /dev/drum) */ int nlfd; /* namelist file (e.g., /kernel) */ struct kinfo_proc *procbase; char *argspc; /* (dynamic) storage for argv strings */ int arglen; /* length of the above */ char **argv; /* (dynamic) storage for argv pointers */ int argc; /* length of above (not actual # present) */ char *argbuf; /* (dynamic) temporary storage */ /* * Kernel virtual address translation state. This only gets filled * in for dead kernels; otherwise, the running kernel (i.e. kmem) * will do the translations for us. It could be big, so we * only allocate it if necessary. */ struct vmstate *vmst; }; /* * Functions used internally by kvm, but across kvm modules. */ void _kvm_err __P((kvm_t *kd, const char *program, const char *fmt, ...)); void _kvm_freeprocs __P((kvm_t *kd)); void _kvm_freevtop __P((kvm_t *)); int _kvm_initvtop __P((kvm_t *)); int _kvm_kvatop __P((kvm_t *, u_long, u_long *)); void *_kvm_malloc __P((kvm_t *kd, size_t)); void *_kvm_realloc __P((kvm_t *kd, void *, size_t)); void _kvm_syserr __P((kvm_t *kd, const char *program, const char *fmt, ...)); int _kvm_uvatop __P((kvm_t *, const struct proc *, u_long, u_long *)); Index: head/lib/libkvm/kvm_proc.c =================================================================== --- head/lib/libkvm/kvm_proc.c (revision 55126) +++ head/lib/libkvm/kvm_proc.c (revision 55127) @@ -1,784 +1,789 @@ /*- * Copyright (c) 1989, 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software developed by the Computer Systems * Engineering group at Lawrence Berkeley Laboratory under DARPA contract * BG 91-66 and contributed to Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93"; #endif /* LIBC_SCCS and not lint */ /* * Proc traversal interface for kvm. ps and w are (probably) the exclusive * users of this code, so we've factored it out into a separate module. * Thus, we keep this grunge out of the other kvm applications (i.e., * most other applications are interested only in open/close/read/nlist). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include -#include #include #include "kvm_private.h" #if used static char * kvm_readswap(kd, p, va, cnt) kvm_t *kd; const struct proc *p; u_long va; u_long *cnt; { #ifdef __FreeBSD__ /* XXX Stubbed out, our vm system is differnet */ _kvm_err(kd, kd->program, "kvm_readswap not implemented"); return(0); #endif /* __FreeBSD__ */ } #endif #define KREAD(kd, addr, obj) \ (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj)) /* * Read proc's from memory file into buffer bp, which has space to hold * at most maxcnt procs. */ static int kvm_proclist(kd, what, arg, p, bp, maxcnt) kvm_t *kd; int what, arg; struct proc *p; struct kinfo_proc *bp; int maxcnt; { register int cnt = 0; struct eproc eproc; struct pgrp pgrp; struct session sess; struct tty tty; struct proc proc; struct proc pproc; for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) { if (KREAD(kd, (u_long)p, &proc)) { _kvm_err(kd, kd->program, "can't read proc at %x", p); return (-1); } if (KREAD(kd, (u_long)proc.p_cred, &eproc.e_pcred) == 0) (void)(KREAD(kd, (u_long)eproc.e_pcred.pc_ucred, &eproc.e_ucred)); switch(what) { case KERN_PROC_PID: if (proc.p_pid != (pid_t)arg) continue; break; case KERN_PROC_UID: if (eproc.e_ucred.cr_uid != (uid_t)arg) continue; break; case KERN_PROC_RUID: if (eproc.e_pcred.p_ruid != (uid_t)arg) continue; break; } /* * We're going to add another proc to the set. If this * will overflow the buffer, assume the reason is because * nprocs (or the proc list) is corrupt and declare an error. */ if (cnt >= maxcnt) { _kvm_err(kd, kd->program, "nprocs corrupt"); return (-1); } /* * gather eproc */ eproc.e_paddr = p; if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) { _kvm_err(kd, kd->program, "can't read pgrp at %x", proc.p_pgrp); return (-1); } if (proc.p_oppid) eproc.e_ppid = proc.p_oppid; else if (proc.p_pptr) { if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) { _kvm_err(kd, kd->program, "can't read pproc at %x", proc.p_pptr); return (-1); } eproc.e_ppid = pproc.p_pid; } else eproc.e_ppid = 0; eproc.e_sess = pgrp.pg_session; eproc.e_pgid = pgrp.pg_id; eproc.e_jobc = pgrp.pg_jobc; if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) { _kvm_err(kd, kd->program, "can't read session at %x", pgrp.pg_session); return (-1); } (void)memcpy(eproc.e_login, sess.s_login, sizeof(eproc.e_login)); if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) { if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) { _kvm_err(kd, kd->program, "can't read tty at %x", sess.s_ttyp); return (-1); } eproc.e_tdev = tty.t_dev; eproc.e_tsess = tty.t_session; if (tty.t_pgrp != NULL) { if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) { _kvm_err(kd, kd->program, "can't read tpgrp at &x", tty.t_pgrp); return (-1); } eproc.e_tpgid = pgrp.pg_id; } else eproc.e_tpgid = -1; } else eproc.e_tdev = NODEV; eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0; if (sess.s_leader == p) eproc.e_flag |= EPROC_SLEADER; if (proc.p_wmesg) (void)kvm_read(kd, (u_long)proc.p_wmesg, eproc.e_wmesg, WMESGLEN); #ifdef sparc (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_rssize, (char *)&eproc.e_vm.vm_rssize, sizeof(eproc.e_vm.vm_rssize)); (void)kvm_read(kd, (u_long)&proc.p_vmspace->vm_tsize, (char *)&eproc.e_vm.vm_tsize, 3 * sizeof(eproc.e_vm.vm_rssize)); /* XXX */ #else (void)kvm_read(kd, (u_long)proc.p_vmspace, (char *)&eproc.e_vm, sizeof(eproc.e_vm)); #endif eproc.e_xsize = eproc.e_xrssize = 0; eproc.e_xccount = eproc.e_xswrss = 0; switch (what) { case KERN_PROC_PGRP: if (eproc.e_pgid != (pid_t)arg) continue; break; case KERN_PROC_TTY: if ((proc.p_flag & P_CONTROLT) == 0 || eproc.e_tdev != (dev_t)arg) continue; break; } bcopy(&proc, &bp->kp_proc, sizeof(proc)); bcopy(&eproc, &bp->kp_eproc, sizeof(eproc)); ++bp; ++cnt; } return (cnt); } /* * Build proc info array by reading in proc list from a crash dump. * Return number of procs read. maxcnt is the max we will read. */ static int kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt) kvm_t *kd; int what, arg; u_long a_allproc; u_long a_zombproc; int maxcnt; { register struct kinfo_proc *bp = kd->procbase; register int acnt, zcnt; struct proc *p; if (KREAD(kd, a_allproc, &p)) { _kvm_err(kd, kd->program, "cannot read allproc"); return (-1); } acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt); if (acnt < 0) return (acnt); if (KREAD(kd, a_zombproc, &p)) { _kvm_err(kd, kd->program, "cannot read zombproc"); return (-1); } zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt); if (zcnt < 0) zcnt = 0; return (acnt + zcnt); } struct kinfo_proc * kvm_getprocs(kd, op, arg, cnt) kvm_t *kd; int op, arg; int *cnt; { int mib[4], st, nprocs; size_t size; if (kd->procbase != 0) { free((void *)kd->procbase); /* * Clear this pointer in case this call fails. Otherwise, * kvm_close() will free it again. */ kd->procbase = 0; } if (ISALIVE(kd)) { size = 0; mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = op; mib[3] = arg; st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, NULL, &size, NULL, 0); if (st == -1) { _kvm_syserr(kd, kd->program, "kvm_getprocs"); return (0); } do { size += size / 10; kd->procbase = (struct kinfo_proc *) _kvm_realloc(kd, kd->procbase, size); if (kd->procbase == 0) return (0); st = sysctl(mib, op == KERN_PROC_ALL ? 3 : 4, kd->procbase, &size, NULL, 0); } while (st == -1 && errno == ENOMEM); if (st == -1) { _kvm_syserr(kd, kd->program, "kvm_getprocs"); return (0); } if (size % sizeof(struct kinfo_proc) != 0) { _kvm_err(kd, kd->program, "proc size mismatch (%d total, %d chunks)", size, sizeof(struct kinfo_proc)); return (0); } nprocs = size / sizeof(struct kinfo_proc); } else { struct nlist nl[4], *p; nl[0].n_name = "_nprocs"; nl[1].n_name = "_allproc"; nl[2].n_name = "_zombproc"; nl[3].n_name = 0; if (kvm_nlist(kd, nl) != 0) { for (p = nl; p->n_type != 0; ++p) ; _kvm_err(kd, kd->program, "%s: no such symbol", p->n_name); return (0); } if (KREAD(kd, nl[0].n_value, &nprocs)) { _kvm_err(kd, kd->program, "can't read nprocs"); return (0); } size = nprocs * sizeof(struct kinfo_proc); kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size); if (kd->procbase == 0) return (0); nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value, nl[2].n_value, nprocs); #ifdef notdef size = nprocs * sizeof(struct kinfo_proc); (void)realloc(kd->procbase, size); #endif } *cnt = nprocs; return (kd->procbase); } void _kvm_freeprocs(kd) kvm_t *kd; { if (kd->procbase) { free(kd->procbase); kd->procbase = 0; } } void * _kvm_realloc(kd, p, n) kvm_t *kd; void *p; size_t n; { void *np = (void *)realloc(p, n); if (np == 0) { free(p); _kvm_err(kd, kd->program, "out of memory"); } return (np); } #ifndef MAX #define MAX(a, b) ((a) > (b) ? (a) : (b)) #endif /* * Read in an argument vector from the user address space of process p. * addr if the user-space base address of narg null-terminated contiguous * strings. This is used to read in both the command arguments and * environment strings. Read at most maxcnt characters of strings. */ static char ** kvm_argv(kd, p, addr, narg, maxcnt) kvm_t *kd; const struct proc *p; register u_long addr; register int narg; register int maxcnt; { register char *np, *cp, *ep, *ap; register u_long oaddr = -1; register int len, cc; register char **argv; /* * Check that there aren't an unreasonable number of agruments, * and that the address is in user space. */ if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS) return (0); /* * kd->argv : work space for fetching the strings from the target * process's space, and is converted for returning to caller */ if (kd->argv == 0) { /* * Try to avoid reallocs. */ kd->argc = MAX(narg + 1, 32); kd->argv = (char **)_kvm_malloc(kd, kd->argc * sizeof(*kd->argv)); if (kd->argv == 0) return (0); } else if (narg + 1 > kd->argc) { kd->argc = MAX(2 * kd->argc, narg + 1); kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc * sizeof(*kd->argv)); if (kd->argv == 0) return (0); } /* * kd->argspc : returned to user, this is where the kd->argv * arrays are left pointing to the collected strings. */ if (kd->argspc == 0) { kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE); if (kd->argspc == 0) return (0); kd->arglen = PAGE_SIZE; } /* * kd->argbuf : used to pull in pages from the target process. * the strings are copied out of here. */ if (kd->argbuf == 0) { kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE); if (kd->argbuf == 0) return (0); } /* Pull in the target process'es argv vector */ cc = sizeof(char *) * narg; if (kvm_uread(kd, p, addr, (char *)kd->argv, cc) != cc) return (0); /* * ap : saved start address of string we're working on in kd->argspc * np : pointer to next place to write in kd->argspc * len: length of data in kd->argspc * argv: pointer to the argv vector that we are hunting around the * target process space for, and converting to addresses in * our address space (kd->argspc). */ ap = np = kd->argspc; argv = kd->argv; len = 0; /* * Loop over pages, filling in the argument vector. * Note that the argv strings could be pointing *anywhere* in * the user address space and are no longer contiguous. * Note that *argv is modified when we are going to fetch a string * that crosses a page boundary. We copy the next part of the string * into to "np" and eventually convert the pointer. */ while (argv < kd->argv + narg && *argv != 0) { /* get the address that the current argv string is on */ addr = (u_long)*argv & ~(PAGE_SIZE - 1); /* is it the same page as the last one? */ if (addr != oaddr) { if (kvm_uread(kd, p, addr, kd->argbuf, PAGE_SIZE) != PAGE_SIZE) return (0); oaddr = addr; } /* offset within the page... kd->argbuf */ addr = (u_long)*argv & (PAGE_SIZE - 1); /* cp = start of string, cc = count of chars in this chunk */ cp = kd->argbuf + addr; cc = PAGE_SIZE - addr; /* dont get more than asked for by user process */ if (maxcnt > 0 && cc > maxcnt - len) cc = maxcnt - len; /* pointer to end of string if we found it in this page */ ep = memchr(cp, '\0', cc); if (ep != 0) cc = ep - cp + 1; /* * at this point, cc is the count of the chars that we are * going to retrieve this time. we may or may not have found * the end of it. (ep points to the null if the end is known) */ /* will we exceed the malloc/realloced buffer? */ if (len + cc > kd->arglen) { register int off; register char **pp; register char *op = kd->argspc; kd->arglen *= 2; kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, kd->arglen); if (kd->argspc == 0) return (0); /* * Adjust argv pointers in case realloc moved * the string space. */ off = kd->argspc - op; for (pp = kd->argv; pp < argv; pp++) *pp += off; ap += off; np += off; } /* np = where to put the next part of the string in kd->argspc*/ /* np is kinda redundant.. could use "kd->argspc + len" */ memcpy(np, cp, cc); np += cc; /* inc counters */ len += cc; /* * if end of string found, set the *argv pointer to the * saved beginning of string, and advance. argv points to * somewhere in kd->argv.. This is initially relative * to the target process, but when we close it off, we set * it to point in our address space. */ if (ep != 0) { *argv++ = ap; ap = np; } else { /* update the address relative to the target process */ *argv += cc; } if (maxcnt > 0 && len >= maxcnt) { /* * We're stopping prematurely. Terminate the * current string. */ if (ep == 0) { *np = '\0'; *argv++ = ap; } break; } } /* Make sure argv is terminated. */ *argv = 0; return (kd->argv); } static void ps_str_a(p, addr, n) struct ps_strings *p; u_long *addr; int *n; { *addr = (u_long)p->ps_argvstr; *n = p->ps_nargvstr; } static void ps_str_e(p, addr, n) struct ps_strings *p; u_long *addr; int *n; { *addr = (u_long)p->ps_envstr; *n = p->ps_nenvstr; } /* * Determine if the proc indicated by p is still active. * This test is not 100% foolproof in theory, but chances of * being wrong are very low. */ static int proc_verify(kd, kernp, p) kvm_t *kd; u_long kernp; const struct proc *p; { struct kinfo_proc kp; int mib[4]; size_t len; mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PID; mib[3] = p->p_pid; len = sizeof(kp); if (sysctl(mib, 4, &kp, &len, NULL, 0) == -1) return (0); return (p->p_pid == kp.kp_proc.p_pid && (kp.kp_proc.p_stat != SZOMB || p->p_stat == SZOMB)); } static char ** kvm_doargv(kd, kp, nchr, info) kvm_t *kd; const struct kinfo_proc *kp; int nchr; void (*info)(struct ps_strings *, u_long *, int *); { register const struct proc *p = &kp->kp_proc; register char **ap; u_long addr; int cnt; static struct ps_strings arginfo; static u_long ps_strings; size_t len; if (ps_strings == NULL) { len = sizeof(ps_strings); if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL, 0) == -1) ps_strings = PS_STRINGS; } /* * Pointers are stored at the top of the user stack. */ if (p->p_stat == SZOMB || kvm_uread(kd, p, ps_strings, (char *)&arginfo, sizeof(arginfo)) != sizeof(arginfo)) return (0); (*info)(&arginfo, &addr, &cnt); if (cnt == 0) return (0); ap = kvm_argv(kd, p, addr, cnt, nchr); /* * For live kernels, make sure this process didn't go away. */ if (ap != 0 && ISALIVE(kd) && !proc_verify(kd, (u_long)kp->kp_eproc.e_paddr, p)) ap = 0; return (ap); } /* * Get the command args. This code is now machine independent. */ char ** kvm_getargv(kd, kp, nchr) kvm_t *kd; const struct kinfo_proc *kp; int nchr; { int oid[4]; int i, l; static int buflen; static char *buf, *p; static char **bufp; static int argc; + + if (!ISALIVE(kd)) { + _kvm_err(kd, kd->program, + "cannot read user space from dead kernel"); + return (0); + } if (!buflen) { l = sizeof(buflen); i = sysctlbyname("kern.ps_arg_cache_limit", &buflen, &l, NULL, 0); if (i == -1) { buflen == 0; } else { buf = malloc(buflen); if (buf == NULL) buflen = 0; argc = 32; bufp = malloc(sizeof(char *) * argc); } } if (buf != NULL) { oid[0] = CTL_KERN; oid[1] = KERN_PROC; oid[2] = KERN_PROC_ARGS; oid[3] = kp->kp_proc.p_pid; l = buflen; i = sysctl(oid, 4, buf, &l, 0, 0); if (i == 0 && l > 0) { i = 0; p = buf; do { bufp[i++] = p; p += strlen(p) + 1; if (i >= argc) { argc += argc; bufp = realloc(bufp, sizeof(char *) * argc); } } while (p < buf + l); bufp[i++] = 0; return (bufp); } } if (kp->kp_proc.p_flag & P_SYSTEM) return (NULL); return (kvm_doargv(kd, kp, nchr, ps_str_a)); } char ** kvm_getenvv(kd, kp, nchr) kvm_t *kd; const struct kinfo_proc *kp; int nchr; { return (kvm_doargv(kd, kp, nchr, ps_str_e)); } /* * Read from user space. The user context is given by p. */ ssize_t kvm_uread(kd, p, uva, buf, len) kvm_t *kd; register const struct proc *p; register u_long uva; register char *buf; register size_t len; { register char *cp; char procfile[MAXPATHLEN]; ssize_t amount; int fd; if (!ISALIVE(kd)) { _kvm_err(kd, kd->program, "cannot read user space from dead kernel"); return (0); } sprintf(procfile, "/proc/%d/mem", p->p_pid); fd = open(procfile, O_RDONLY, 0); if (fd < 0) { _kvm_err(kd, kd->program, "cannot open %s", procfile); close(fd); return (0); } cp = buf; while (len > 0) { errno = 0; if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) { _kvm_err(kd, kd->program, "invalid address (%x) in %s", uva, procfile); break; } amount = read(fd, cp, len); if (amount < 0) { _kvm_syserr(kd, kd->program, "error reading %s", procfile); break; } if (amount == 0) { _kvm_err(kd, kd->program, "EOF reading %s", procfile); break; } cp += amount; uva += amount; len -= amount; } close(fd); return ((ssize_t)(cp - buf)); } Index: head/lib/libkvm/kvm_sparc.c =================================================================== --- head/lib/libkvm/kvm_sparc.c (revision 55126) +++ head/lib/libkvm/kvm_sparc.c (revision 55127) @@ -1,236 +1,240 @@ /*- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software developed by the Computer Systems * Engineering group at Lawrence Berkeley Laboratory under DARPA contract * BG 91-66 and contributed to Berkeley. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) +#if 0 static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93"; +#else +static const char rcsid[] = + "$FreeBSD$"; +#endif #endif /* LIBC_SCCS and not lint */ /* * Sparc machine dependent routines for kvm. Hopefully, the forthcoming * vm code will one day obsolete this module. */ #include #include #include #include #include #include #include #include #include #include -#include #include "kvm_private.h" #define NPMEG 128 /* XXX from sparc/pmap.c */ #define MAXMEM (128 * 1024 * 1024) /* no more than 128 MB phys mem */ #define NPGBANK 16 /* 2^4 pages per bank (64K / bank) */ #define BSHIFT 4 /* log2(NPGBANK) */ #define BOFFSET (NPGBANK - 1) #define BTSIZE (MAXMEM / NBPG / NPGBANK) #define HWTOSW(pmap_stod, pg) (pmap_stod[(pg) >> BSHIFT] | ((pg) & BOFFSET)) struct vmstate { pmeg_t segmap[NKSEG]; int pmeg[NPMEG][NPTESG]; int pmap_stod[BTSIZE]; /* dense to sparse */ }; void _kvm_freevtop(kd) kvm_t *kd; { if (kd->vmst != 0) free(kd->vmst); } int _kvm_initvtop(kd) kvm_t *kd; { register int i; register int off; register struct vmstate *vm; struct stat st; struct nlist nlist[2]; vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); if (vm == 0) return (-1); kd->vmst = vm; if (fstat(kd->pmfd, &st) < 0) return (-1); /* * Read segment table. */ off = st.st_size - ctob(btoc(sizeof(vm->segmap))); errno = 0; if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 || read(kd->pmfd, (char *)vm->segmap, sizeof(vm->segmap)) < 0) { _kvm_err(kd, kd->program, "cannot read segment map"); return (-1); } /* * Read PMEGs. */ off = st.st_size - ctob(btoc(sizeof(vm->pmeg)) + btoc(sizeof(vm->segmap))); errno = 0; if (lseek(kd->pmfd, (off_t)off, 0) == -1 && errno != 0 || read(kd->pmfd, (char *)vm->pmeg, sizeof(vm->pmeg)) < 0) { _kvm_err(kd, kd->program, "cannot read PMEG table"); return (-1); } /* * Make pmap_stod be an identity map so we can bootstrap it in. * We assume it's in the first contiguous chunk of physical memory. */ for (i = 0; i < BTSIZE; ++i) vm->pmap_stod[i] = i << 4; /* * It's okay to do this nlist separately from the one kvm_getprocs() * does, since the only time we could gain anything by combining * them is if we do a kvm_getprocs() on a dead kernel, which is * not too common. */ nlist[0].n_name = "_pmap_stod"; nlist[1].n_name = 0; if (kvm_nlist(kd, nlist) != 0) { _kvm_err(kd, kd->program, "pmap_stod: no such symbol"); return (-1); } if (kvm_read(kd, (u_long)nlist[0].n_value, (char *)vm->pmap_stod, sizeof(vm->pmap_stod)) != sizeof(vm->pmap_stod)) { _kvm_err(kd, kd->program, "cannot read pmap_stod"); return (-1); } return (0); } #define VA_OFF(va) (va & (NBPG - 1)) /* * Translate a user virtual address to a physical address. */ int _kvm_uvatop(kd, p, va, pa) kvm_t *kd; const struct proc *p; u_long va; u_long *pa; { int kva, pte; register int off, frame; register struct vmspace *vms = p->p_vmspace; if ((u_long)vms < KERNBASE) { _kvm_err(kd, kd->program, "_kvm_uvatop: corrupt proc"); return (0); } if (va >= KERNBASE) return (0); /* * Get the PTE. This takes two steps. We read the * base address of the table, then we index it. * Note that the index pte table is indexed by * virtual segment rather than physical segment. */ kva = (u_long)&vms->vm_pmap.pm_rpte[VA_VSEG(va)]; if (kvm_read(kd, kva, (char *)&kva, 4) != 4 || kva == 0) goto invalid; kva += sizeof(vms->vm_pmap.pm_rpte[0]) * VA_VPG(va); if (kvm_read(kd, kva, (char *)&pte, 4) == 4 && (pte & PG_V)) { off = VA_OFF(va); /* * /dev/mem adheres to the hardware model of physical memory * (with holes in the address space), while crashdumps * adhere to the contiguous software model. */ if (ISALIVE(kd)) frame = pte & PG_PFNUM; else frame = HWTOSW(kd->vmst->pmap_stod, pte & PG_PFNUM); *pa = (frame << PGSHIFT) | off; return (NBPG - off); } invalid: _kvm_err(kd, 0, "invalid address (%x)", va); return (0); } /* * Translate a kernel virtual address to a physical address using the * mapping information in kd->vm. Returns the result in pa, and returns * the number of bytes that are contiguously available from this * physical address. This routine is used only for crashdumps. */ int _kvm_kvatop(kd, va, pa) kvm_t *kd; u_long va; u_long *pa; { register struct vmstate *vm; register int s; register int pte; register int off; if (va >= KERNBASE) { vm = kd->vmst; s = vm->segmap[VA_VSEG(va) - NUSEG]; pte = vm->pmeg[s][VA_VPG(va)]; if ((pte & PG_V) != 0) { off = VA_OFF(va); *pa = (HWTOSW(vm->pmap_stod, pte & PG_PFNUM) << PGSHIFT) | off; return (NBPG - off); } } _kvm_err(kd, 0, "invalid address (%x)", va); return (0); }