summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbz <bz@FreeBSD.org>2009-07-23 21:12:21 +0000
committerbz <bz@FreeBSD.org>2009-07-23 21:12:21 +0000
commitd607f4c3e36cdfbb5b44004389ae0faeaf456f5b (patch)
treeb8a5e23e08212e3f59199c99ea9ba792ed035e7b
parentb3be1c6e3bd7d3b5cf946ddb04c1c5811644229b (diff)
downloadFreeBSD-src-d607f4c3e36cdfbb5b44004389ae0faeaf456f5b.zip
FreeBSD-src-d607f4c3e36cdfbb5b44004389ae0faeaf456f5b.tar.gz
Make libkvm work on live systems and crashdumps with and
without VIMAGE virtualization in the kernel. If we cannot resolve a symbol try to see if we can find it with prefix of the virtualized subsystem, currently only "vnet_entry" by identifying either the vnet of the current process for a live system or the vnet of proc0 (or of dumptid if compiled in a non-default way). The way this is done currently allows us to only touch libkvm but no single application. Once we are going to virtualize more subsystems we will have to review this decision for better scaling. Submitted by: rwatson (initial version of kvm_vnet.c, lots of ideas) Reviewed by: rwatson Approved by: re (kib)
-rw-r--r--lib/libkvm/Makefile2
-rw-r--r--lib/libkvm/kvm.c172
-rw-r--r--lib/libkvm/kvm_private.h10
-rw-r--r--lib/libkvm/kvm_vnet.c237
4 files changed, 412 insertions, 9 deletions
diff --git a/lib/libkvm/Makefile b/lib/libkvm/Makefile
index 52a22ca..47dc613 100644
--- a/lib/libkvm/Makefile
+++ b/lib/libkvm/Makefile
@@ -10,7 +10,7 @@ CFLAGS+=-DSUN4V
.endif
SRCS= kvm.c kvm_${MACHINE_ARCH}.c kvm_cptime.c kvm_file.c kvm_getloadavg.c \
- kvm_getswapinfo.c kvm_pcpu.c kvm_proc.c
+ kvm_getswapinfo.c kvm_pcpu.c kvm_proc.c kvm_vnet.c
.if ${MACHINE_ARCH} == "amd64" || ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "arm"
SRCS+= kvm_minidump_${MACHINE_ARCH}.c
.endif
diff --git a/lib/libkvm/kvm.c b/lib/libkvm/kvm.c
index 8ff8a2b..50f752a 100644
--- a/lib/libkvm/kvm.c
+++ b/lib/libkvm/kvm.c
@@ -41,6 +41,9 @@ static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
+
+#define _WANT_VNET
+
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/ioctl.h>
@@ -48,6 +51,8 @@ static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
#include <sys/sysctl.h>
#include <sys/linker.h>
+#include <net/vnet.h>
+
#include <vm/vm.h>
#include <vm/vm_param.h>
@@ -62,6 +67,7 @@ static char sccsid[] = "@(#)kvm.c 8.2 (Berkeley) 2/13/94";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
#include <unistd.h>
#include "kvm_private.h"
@@ -299,34 +305,154 @@ kvm_close(kd)
return (0);
}
+/*
+ * Walk the list of unresolved symbols, generate a new list and prefix the
+ * symbol names, try again, and merge back what we could resolve.
+ */
+static int
+kvm_fdnlist_prefix(kvm_t *kd, struct nlist *nl, int missing, const char *prefix,
+ uintptr_t (*validate_fn)(kvm_t *, uintptr_t))
+{
+ struct nlist *n, *np, *p;
+ char *cp, *ce;
+ size_t len;
+ int unresolved;
+
+ /*
+ * Calculate the space we need to malloc for nlist and names.
+ * We are going to store the name twice for later lookups: once
+ * with the prefix and once the unmodified name delmited by \0.
+ */
+ len = 0;
+ unresolved = 0;
+ for (p = nl; p->n_name && p->n_name[0]; ++p) {
+ if (p->n_type != N_UNDF)
+ continue;
+ len += sizeof(struct nlist) + strlen(prefix) +
+ 2 * (strlen(p->n_name) + 1);
+ unresolved++;
+ }
+ if (unresolved == 0)
+ return (unresolved);
+ /* Add space for the terminating nlist entry. */
+ len += sizeof(struct nlist);
+ unresolved++;
+
+ /* Alloc one chunk for (nlist, [names]) and setup pointers. */
+ n = np = malloc(len);
+ bzero(n, len);
+ if (n == NULL)
+ return (missing);
+ cp = ce = (char *)np;
+ cp += unresolved * sizeof(struct nlist);
+ ce += len;
+
+ /* Generate shortened nlist with special prefix. */
+ unresolved = 0;
+ for (p = nl; p->n_name && p->n_name[0]; ++p) {
+ if (p->n_type != N_UNDF)
+ continue;
+ bcopy(p, np, sizeof(struct nlist));
+ /* Save the new\0orig. name so we can later match it again. */
+ len = snprintf(cp, ce - cp, "%s%s%c%s", prefix,
+ (prefix[0] != '\0' && p->n_name[0] == '_') ?
+ (p->n_name + 1) : p->n_name, '\0', p->n_name);
+ if (len >= ce - cp)
+ continue;
+ np->n_name = cp;
+ cp += len + 1;
+ np++;
+ unresolved++;
+ }
+
+ /* Do lookup on the reduced list. */
+ np = n;
+ unresolved = __fdnlist(kd->nlfd, np);
+
+ /* Check if we could resolve further symbols and update the list. */
+ if (unresolved >= 0 && unresolved < missing) {
+ /* Find the first freshly resolved entry. */
+ for (; np->n_name && np->n_name[0]; np++)
+ if (np->n_type != N_UNDF)
+ break;
+ /*
+ * The lists are both in the same order,
+ * so we can walk them in parallel.
+ */
+ for (p = nl; np->n_name && np->n_name[0] &&
+ p->n_name && p->n_name[0]; ++p) {
+ if (p->n_type != N_UNDF)
+ continue;
+ /* Skip expanded name and compare to orig. one. */
+ cp = np->n_name + strlen(np->n_name) + 1;
+ if (strcmp(cp, p->n_name))
+ continue;
+ /* Update nlist with new, translated results. */
+ p->n_type = np->n_type;
+ p->n_other = np->n_other;
+ p->n_desc = np->n_desc;
+ if (validate_fn)
+ p->n_value = (*validate_fn)(kd, np->n_value);
+ else
+ p->n_value = np->n_value;
+ missing--;
+ /* Find next freshly resolved entry. */
+ for (np++; np->n_name && np->n_name[0]; np++)
+ if (np->n_type != N_UNDF)
+ break;
+ }
+ }
+ /* We could assert missing = unresolved here. */
+
+ free(n);
+ return (unresolved);
+}
+
int
-kvm_nlist(kd, nl)
- kvm_t *kd;
- struct nlist *nl;
+_kvm_nlist(kvm_t *kd, struct nlist *nl, int initialize)
{
struct nlist *p;
int nvalid;
struct kld_sym_lookup lookup;
int error;
-
+ char *prefix = "", symname[1024]; /* XXX-BZ symbol name length limit? */
/*
* If we can't use the kld symbol lookup, revert to the
* slow library call.
*/
- if (!ISALIVE(kd))
- return (__fdnlist(kd->nlfd, nl));
+ if (!ISALIVE(kd)) {
+ error = __fdnlist(kd->nlfd, nl);
+ if (error <= 0) /* Hard error or success. */
+ return (error);
+
+ if (_kvm_vnet_initialized(kd, initialize))
+ error = kvm_fdnlist_prefix(kd, nl, error,
+ VNET_SYMPREFIX, _kvm_vnet_validaddr);
+
+ return (error);
+ }
/*
* We can use the kld lookup syscall. Go through each nlist entry
* and look it up with a kldsym(2) syscall.
*/
nvalid = 0;
+again:
for (p = nl; p->n_name && p->n_name[0]; ++p) {
+ if (p->n_type != N_UNDF)
+ continue;
+
lookup.version = sizeof(lookup);
- lookup.symname = p->n_name;
lookup.symvalue = 0;
lookup.symsize = 0;
+ error = snprintf(symname, sizeof(symname), "%s%s", prefix,
+ (prefix[0] != '\0' && p->n_name[0] == '_') ?
+ (p->n_name + 1) : p->n_name);
+ if (error >= sizeof(symname))
+ continue;
+
+ lookup.symname = symname;
if (lookup.symname[0] == '_')
lookup.symname++;
@@ -334,11 +460,28 @@ kvm_nlist(kd, nl)
p->n_type = N_TEXT;
p->n_other = 0;
p->n_desc = 0;
- p->n_value = lookup.symvalue;
+ if (_kvm_vnet_initialized(kd, initialize) &&
+ !strcmp(prefix, VNET_SYMPREFIX))
+ p->n_value =
+ _kvm_vnet_validaddr(kd, lookup.symvalue);
+ else
+ p->n_value = lookup.symvalue;
++nvalid;
/* lookup.symsize */
}
}
+
+ /*
+ * Check the number of entries that weren't found. If they exist,
+ * try again with a prefix for virtualized symbol names.
+ */
+ error = ((p - nl) - nvalid);
+ if (error && _kvm_vnet_initialized(kd, initialize) &&
+ strcmp(prefix, VNET_SYMPREFIX)) {
+ prefix = VNET_SYMPREFIX;
+ goto again;
+ }
+
/*
* Return the number of entries that weren't found. If they exist,
* also fill internal error buffer.
@@ -349,6 +492,19 @@ kvm_nlist(kd, nl)
return (error);
}
+int
+kvm_nlist(kd, nl)
+ kvm_t *kd;
+ struct nlist *nl;
+{
+
+ /*
+ * If called via the public interface, permit intialization of
+ * further virtualized modules on demand.
+ */
+ return (_kvm_nlist(kd, nl, 1));
+}
+
ssize_t
kvm_read(kd, kva, buf, len)
kvm_t *kd;
diff --git a/lib/libkvm/kvm_private.h b/lib/libkvm/kvm_private.h
index 88ff716..cc073db 100644
--- a/lib/libkvm/kvm_private.h
+++ b/lib/libkvm/kvm_private.h
@@ -62,6 +62,12 @@ struct __kvm {
*/
struct vmstate *vmst;
int rawdump; /* raw dump format */
+
+ int vnet_initialized; /* vnet fields set up */
+ uintptr_t vnet_start; /* start of kernel's vnet region */
+ uintptr_t vnet_stop; /* stop of kernel's vnet region */
+ uintptr_t vnet_current; /* vnet we're working with */
+ uintptr_t vnet_base; /* vnet base of current vnet */
};
/*
@@ -74,10 +80,14 @@ void _kvm_freevtop(kvm_t *);
int _kvm_initvtop(kvm_t *);
int _kvm_kvatop(kvm_t *, u_long, off_t *);
void *_kvm_malloc(kvm_t *kd, size_t);
+int _kvm_nlist(kvm_t *, struct nlist *, int);
void *_kvm_realloc(kvm_t *kd, void *, size_t);
void _kvm_syserr (kvm_t *kd, const char *program, const char *fmt, ...)
__printflike(3, 4);
int _kvm_uvatop(kvm_t *, const struct proc *, u_long, u_long *);
+int _kvm_vnet_selectpid(kvm_t *, pid_t);
+int _kvm_vnet_initialized(kvm_t *, int);
+uintptr_t _kvm_vnet_validaddr(kvm_t *, uintptr_t);
#if defined(__amd64__) || defined(__i386__) || defined(__arm__)
void _kvm_minidump_freevtop(kvm_t *);
diff --git a/lib/libkvm/kvm_vnet.c b/lib/libkvm/kvm_vnet.c
new file mode 100644
index 0000000..13f7cf4
--- /dev/null
+++ b/lib/libkvm/kvm_vnet.c
@@ -0,0 +1,237 @@
+/*-
+ * Copyright (c) 2009 Robert N. M. Watson
+ * Copyright (c) 2009 Bjoern A. Zeeb <bz@FreeBSD.org>
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+
+#define _WANT_PRISON
+#define _WANT_UCRED
+#define _WANT_VNET
+
+#include <sys/_lock.h>
+#include <sys/_mutex.h>
+#include <sys/_task.h>
+#include <sys/jail.h>
+#include <sys/proc.h>
+#include <sys/types.h>
+#include <sys/vimage.h>
+
+#include <net/vnet.h>
+
+#include <nlist.h>
+#include <kvm.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "kvm_private.h"
+
+/*
+ * Set up libkvm to handle virtual network stack symbols by selecting a
+ * starting pid.
+ */
+int
+_kvm_vnet_selectpid(kvm_t *kd, pid_t pid)
+{
+ struct proc proc;
+ struct thread td;
+ struct ucred cred;
+ struct prison prison;
+ struct vnet vnet;
+ struct nlist nl[] = {
+ /*
+ * Note: kvm_nlist strips the first '_' so add an extra one
+ * here to __{start,stop}_set_vnet.
+ */
+#define NLIST_START_VNET 0
+ { .n_name = "___start_" VNET_SETNAME },
+#define NLIST_STOP_VNET 1
+ { .n_name = "___stop_" VNET_SETNAME },
+#define NLIST_VNET_HEAD 2
+ { .n_name = "vnet_head" },
+#define NLIST_ALLPROC 3
+ { .n_name = "allproc" },
+#define NLIST_DUMPTID 4
+ { .n_name = "dumptid" },
+#define NLIST_PROC0 5
+ { .n_name = "proc0" },
+ { .n_name = NULL },
+ };
+ uintptr_t procp, tdp, credp;
+ lwpid_t dumptid;
+
+ /*
+ * Locate and cache locations of important symbols
+ * using the internal version of _kvm_nlist, turning
+ * off initialization to avoid recursion in case of
+ * unresolveable symbols.
+ */
+ if (_kvm_nlist(kd, nl, 0) != 0) {
+ /*
+ * XXX-BZ: ___start_/___stop_VNET_SETNAME may fail.
+ * For now do not report an error here as we are called
+ * internally and in `void context' until we merge the
+ * functionality to optionally activate this into programs.
+ * By that time we can properly fail and let the callers
+ * handle the error.
+ */
+ /* _kvm_err(kd, kd->program, "%s: no namelist", __func__); */
+ return (-1);
+ }
+
+ /*
+ * Auto-detect if this is a crashdump by reading dumptid.
+ */
+ dumptid = 0;
+ if (nl[NLIST_DUMPTID].n_value) {
+ if (kvm_read(kd, nl[NLIST_DUMPTID].n_value, &dumptid,
+ sizeof(dumptid)) != sizeof(dumptid)) {
+ _kvm_err(kd, kd->program, "%s: dumptid", __func__);
+ return (-1);
+ }
+ }
+
+ /*
+ * First, find the process for this pid. If we are workig on a dump,
+ * either locate the thread dumptid is refering to or proc0.
+ * Based on either, take the address of the ucred.
+ */
+ credp = 0;
+
+ procp = nl[NLIST_ALLPROC].n_value;
+#define VMCORE_VNET_OF_PROC0
+#ifdef VMCORE_VNET_OF_PROC0
+ if (dumptid > 0) {
+ procp = nl[NLIST_PROC0].n_value;
+ pid = 0;
+ }
+#endif
+ while (procp != 0) {
+ if (kvm_read(kd, procp, &proc, sizeof(proc)) != sizeof(proc)) {
+ _kvm_err(kd, kd->program, "%s: proc", __func__);
+ return (-1);
+ }
+#ifndef VMCORE_VNET_OF_PROC0
+ if (dumptid > 0) {
+ tdp = (uintptr_t)TAILQ_FIRST(&proc.p_threads);
+ while (tdp != 0) {
+ if (kvm_read(kd, tdp, &td, sizeof(td)) !=
+ sizeof(td)) {
+ _kvm_err(kd, kd->program, "%s: thread",
+ __func__);
+ return (-1);
+ }
+ if (td.td_tid == dumptid) {
+ credp = (uintptr_t)td.td_ucred;
+ break;
+ }
+ tdp = (uintptr_t)TAILQ_NEXT(&td, td_plist);
+ }
+ } else
+#endif
+ if (proc.p_pid == pid)
+ credp = (uintptr_t)proc.p_ucred;
+ if (credp != 0)
+ break;
+ procp = (uintptr_t)LIST_NEXT(&proc, p_list);
+ }
+ if (credp == 0) {
+ _kvm_err(kd, kd->program, "%s: pid/tid not found", __func__);
+ return (-1);
+ }
+ if (kvm_read(kd, (uintptr_t)credp, &cred, sizeof(cred)) !=
+ sizeof(cred)) {
+ _kvm_err(kd, kd->program, "%s: cred", __func__);
+ return (-1);
+ }
+ if (cred.cr_prison == NULL) {
+ _kvm_err(kd, kd->program, "%s: no jail", __func__);
+ return (-1);
+ }
+ if (kvm_read(kd, (uintptr_t)cred.cr_prison, &prison, sizeof(prison)) !=
+ sizeof(prison)) {
+ _kvm_err(kd, kd->program, "%s: prison", __func__);
+ return (-1);
+ }
+ if (prison.pr_vnet == NULL) {
+ _kvm_err(kd, kd->program, "%s: no vnet", __func__);
+ return (-1);
+ }
+ if (kvm_read(kd, (uintptr_t)prison.pr_vnet, &vnet, sizeof(vnet)) !=
+ sizeof(vnet)) {
+ _kvm_err(kd, kd->program, "%s: vnet", __func__);
+ return (-1);
+ }
+ if (vnet.vnet_magic_n != VNET_MAGIC_N) {
+ _kvm_err(kd, kd->program, "%s: invalid vnet magic#", __func__);
+ return (-1);
+ }
+ kd->vnet_initialized = 1;
+ kd->vnet_start = nl[NLIST_START_VNET].n_value;
+ kd->vnet_stop = nl[NLIST_STOP_VNET].n_value;
+ kd->vnet_current = (uintptr_t)prison.pr_vnet;
+ kd->vnet_base = (uintptr_t)vnet.vnet_data_mem - kd->vnet_start;
+ return (0);
+}
+
+/*
+ * Check whether the vnet module has been initialized sucessfully
+ * or not, intialize it if permitted.
+ */
+int
+_kvm_vnet_initialized(kvm_t *kd, int intialize)
+{
+
+ if (kd->vnet_initialized || !intialize)
+ return (kd->vnet_initialized);
+
+ (void) _kvm_vnet_selectpid(kd, getpid());
+
+ return (kd->vnet_initialized);
+}
+
+/*
+ * Check whether the value is within the vnet symbol range and
+ * only if so adjust the offset relative to the current base.
+ */
+uintptr_t
+_kvm_vnet_validaddr(kvm_t *kd, uintptr_t value)
+{
+
+ if (value == 0)
+ return (value);
+
+ if (!kd->vnet_initialized)
+ return (value);
+
+ if (value < kd->vnet_start || value >= kd->vnet_stop)
+ return (value);
+
+ return (kd->vnet_base + value);
+}
OpenPOWER on IntegriCloud