/*-
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* 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. 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.
*/
/*
* Emulation context list is very simple chained list, not even hashed.
* We expect the number of separate traced contexts/processes to be
* fairly low, so it's not worth it to optimize this.
* MMMmmmm not when I use it, it is only bounded PID_MAX!
* Requeue looked up item at start of list to cache result since the
* trace file tendes to have a burst of calls for a single process.
*/
/*
* Find an emulation context appropriate for the given pid.
*/
static struct emulation_ctx *
ectx_find(pid_t pid)
{
struct emulation_ctx *ctx;
/* Find an existing entry */
LIST_FOREACH(ctx, &emul_ctx, ctx_link) {
if (ctx->pid == pid)
break;
}
if (ctx == NULL) {
/* create entry with default emulation */
ctx = malloc(sizeof *ctx);
if (ctx == NULL)
err(1, "malloc emul context");
ctx->pid = pid;
ctx->emulation = default_emul;
/* chain into the list */
LIST_INSERT_HEAD(&emul_ctx, ctx, ctx_link);
} else {
/* move entry to head to optimize lookup for syscall bursts */
LIST_REMOVE(ctx, ctx_link);
LIST_INSERT_HEAD(&emul_ctx, ctx, ctx_link);
}
return ctx;
}
/*
* Update emulation context for given pid, or create new if no context
* for this pid exists.
*/
static void
ectx_update(pid_t pid, const struct emulation *emul)
{
struct emulation_ctx *ctx;
ctx = ectx_find(pid);
ctx->emulation = emul;
}
/*
* Ensure current emulation context is correct for given pid.
*/
void
ectx_sanify(pid_t pid)
{
struct emulation_ctx *ctx;
/*
* Delete emulation context for current pid.
* (eg when tracing exit())
* Defer delete just in case we've cached a pointer...
*/
void
ectx_delete(void)
{
static struct emulation_ctx *ctx = NULL;
if (ctx != NULL)
free(ctx);
/*
* The emulation for current syscall entry is always on HEAD, due
* to code in ectx_find().
*/
ctx = LIST_FIRST(&emul_ctx);