2002-01-08 02:45:10 +03:00
|
|
|
/* $NetBSD: setemul.c,v 1.11 2002/01/07 23:45:10 manu Exp $ */
|
2000-04-10 13:13:45 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation 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 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. 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2002-01-08 02:45:10 +03:00
|
|
|
__RCSID("$NetBSD: setemul.c,v 1.11 2002/01/07 23:45:10 manu Exp $");
|
2000-04-10 13:13:45 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <vis.h>
|
|
|
|
|
|
|
|
#include "setemul.h"
|
|
|
|
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
|
|
|
#include "../../sys/compat/netbsd32/netbsd32_syscall.h"
|
|
|
|
#include "../../sys/compat/freebsd/freebsd_syscall.h"
|
|
|
|
#include "../../sys/compat/hpux/hpux_syscall.h"
|
|
|
|
#include "../../sys/compat/ibcs2/ibcs2_syscall.h"
|
2001-11-28 13:36:48 +03:00
|
|
|
#include "../../sys/compat/irix/irix_syscall.h"
|
2000-04-10 13:13:45 +04:00
|
|
|
#include "../../sys/compat/linux/linux_syscall.h"
|
|
|
|
#include "../../sys/compat/osf1/osf1_syscall.h"
|
2001-02-02 10:41:54 +03:00
|
|
|
#include "../../sys/compat/sunos32/sunos32_syscall.h"
|
2000-04-10 13:13:45 +04:00
|
|
|
#include "../../sys/compat/sunos/sunos_syscall.h"
|
|
|
|
#include "../../sys/compat/svr4/svr4_syscall.h"
|
2001-03-12 00:28:59 +03:00
|
|
|
#include "../../sys/compat/svr4_32/svr4_32_syscall.h"
|
2000-04-10 13:13:45 +04:00
|
|
|
#include "../../sys/compat/ultrix/ultrix_syscall.h"
|
|
|
|
|
|
|
|
#define KTRACE
|
|
|
|
#include "../../sys/kern/syscalls.c"
|
|
|
|
|
|
|
|
#include "../../sys/compat/netbsd32/netbsd32_syscalls.c"
|
|
|
|
#include "../../sys/compat/freebsd/freebsd_syscalls.c"
|
|
|
|
#include "../../sys/compat/hpux/hpux_syscalls.c"
|
|
|
|
#include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
|
2001-11-28 13:36:48 +03:00
|
|
|
#include "../../sys/compat/irix/irix_syscalls.c"
|
2000-04-10 13:13:45 +04:00
|
|
|
#include "../../sys/compat/linux/linux_syscalls.c"
|
|
|
|
#include "../../sys/compat/osf1/osf1_syscalls.c"
|
|
|
|
#include "../../sys/compat/sunos/sunos_syscalls.c"
|
2001-02-02 10:41:54 +03:00
|
|
|
#include "../../sys/compat/sunos32/sunos32_syscalls.c"
|
2000-04-10 13:13:45 +04:00
|
|
|
#include "../../sys/compat/svr4/svr4_syscalls.c"
|
2001-03-12 00:28:59 +03:00
|
|
|
#include "../../sys/compat/svr4_32/svr4_32_syscalls.c"
|
2000-04-10 13:13:45 +04:00
|
|
|
#include "../../sys/compat/ultrix/ultrix_syscalls.c"
|
|
|
|
|
|
|
|
#include "../../sys/compat/hpux/hpux_errno.c"
|
|
|
|
#include "../../sys/compat/svr4/svr4_errno.c"
|
|
|
|
#include "../../sys/compat/ibcs2/ibcs2_errno.c"
|
2001-11-28 13:36:48 +03:00
|
|
|
#include "../../sys/compat/irix/irix_errno.c"
|
2000-04-10 13:13:45 +04:00
|
|
|
#include "../../sys/compat/linux/common/linux_errno.c"
|
|
|
|
#undef KTRACE
|
|
|
|
|
|
|
|
#define NELEM(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
|
2000-11-14 00:38:48 +03:00
|
|
|
static const struct emulation emulations[] = {
|
2000-04-10 13:13:45 +04:00
|
|
|
{ "netbsd", syscallnames, SYS_MAXSYSCALL,
|
|
|
|
NULL, 0 },
|
|
|
|
{ "netbsd32", netbsd32_syscallnames, SYS_MAXSYSCALL,
|
|
|
|
NULL, 0 },
|
|
|
|
{ "freebsd", freebsd_syscallnames, FREEBSD_SYS_MAXSYSCALL,
|
|
|
|
NULL, 0 },
|
|
|
|
{ "hpux", hpux_syscallnames, HPUX_SYS_MAXSYSCALL,
|
|
|
|
native_to_hpux_errno, NELEM(native_to_hpux_errno) },
|
|
|
|
{ "ibcs2", ibcs2_syscallnames, IBCS2_SYS_MAXSYSCALL,
|
|
|
|
native_to_ibcs2_errno, NELEM(native_to_ibcs2_errno) },
|
2002-01-08 02:45:10 +03:00
|
|
|
{ "irix o32", irix_syscallnames, IRIX_SYS_MAXSYSCALL,
|
|
|
|
native_to_irix_errno, NELEM(native_to_irix_errno) },
|
|
|
|
{ "irix n32", irix_syscallnames, IRIX_SYS_MAXSYSCALL,
|
2001-11-28 13:36:48 +03:00
|
|
|
native_to_irix_errno, NELEM(native_to_irix_errno) },
|
2000-04-10 13:13:45 +04:00
|
|
|
{ "linux", linux_syscallnames, LINUX_SYS_MAXSYSCALL,
|
|
|
|
native_to_linux_errno, NELEM(native_to_linux_errno) },
|
|
|
|
{ "osf1", osf1_syscallnames, OSF1_SYS_MAXSYSCALL,
|
|
|
|
NULL, 0 },
|
2001-02-02 10:41:54 +03:00
|
|
|
{ "sunos32",sunos32_syscallnames, SUNOS32_SYS_MAXSYSCALL,
|
|
|
|
NULL, 0 },
|
2000-04-10 13:13:45 +04:00
|
|
|
{ "sunos", sunos_syscallnames, SUNOS_SYS_MAXSYSCALL,
|
|
|
|
NULL, 0 },
|
|
|
|
{ "svr4", svr4_syscallnames, SVR4_SYS_MAXSYSCALL,
|
|
|
|
native_to_svr4_errno, NELEM(native_to_svr4_errno) },
|
2001-03-12 00:28:59 +03:00
|
|
|
{ "svr4_32", svr4_syscallnames, SVR4_SYS_MAXSYSCALL,
|
|
|
|
native_to_svr4_errno, NELEM(native_to_svr4_errno) },
|
2000-04-10 13:13:45 +04:00
|
|
|
{ "ultrix", ultrix_syscallnames, ULTRIX_SYS_MAXSYSCALL,
|
|
|
|
NULL, 0 },
|
2000-06-13 04:59:22 +04:00
|
|
|
{ "pecoff", syscallnames, SYS_MAXSYSCALL,
|
|
|
|
NULL, 0 },
|
2000-04-10 13:13:45 +04:00
|
|
|
{ NULL, NULL, 0,
|
|
|
|
NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct emulation_ctx {
|
|
|
|
pid_t pid;
|
2000-11-14 00:38:48 +03:00
|
|
|
const struct emulation *emulation;
|
2000-04-10 13:13:45 +04:00
|
|
|
struct emulation_ctx *next;
|
|
|
|
};
|
|
|
|
|
2000-11-14 00:38:48 +03:00
|
|
|
const struct emulation *current;
|
2001-02-17 02:28:44 +03:00
|
|
|
const struct emulation *previous;
|
2000-04-10 13:13:45 +04:00
|
|
|
|
2000-11-14 00:38:48 +03:00
|
|
|
static const struct emulation *default_emul=NULL;
|
2000-04-10 13:13:45 +04:00
|
|
|
|
|
|
|
struct emulation_ctx *current_ctx;
|
|
|
|
struct emulation_ctx *emul_ctx = NULL;
|
|
|
|
|
|
|
|
static struct emulation_ctx *ectx_find __P((pid_t));
|
2000-11-14 00:38:48 +03:00
|
|
|
static void ectx_update __P((pid_t, const struct emulation *));
|
2000-04-10 13:13:45 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
setemul(name, pid, update_ectx)
|
|
|
|
const char *name;
|
|
|
|
pid_t pid;
|
|
|
|
int update_ectx;
|
|
|
|
{
|
|
|
|
int i;
|
2000-11-14 00:38:48 +03:00
|
|
|
const struct emulation *match = NULL;
|
2000-04-10 13:13:45 +04:00
|
|
|
|
|
|
|
for (i = 0; emulations[i].name != NULL; i++) {
|
|
|
|
if (strcmp(emulations[i].name, name) == 0) {
|
|
|
|
match = &emulations[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
warnx("Emulation `%s' unknown", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update_ectx)
|
|
|
|
ectx_update(pid, match);
|
|
|
|
|
|
|
|
if (!default_emul)
|
|
|
|
default_emul = match;
|
|
|
|
|
2001-02-17 02:28:44 +03:00
|
|
|
if (current != NULL)
|
|
|
|
previous = current;
|
|
|
|
else
|
|
|
|
previous = match;
|
|
|
|
|
2000-04-10 13:13:45 +04:00
|
|
|
current = match;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find an emulation context appropriate for the given pid.
|
|
|
|
*/
|
|
|
|
static struct emulation_ctx *
|
|
|
|
ectx_find(pid)
|
|
|
|
pid_t pid;
|
|
|
|
{
|
|
|
|
struct emulation_ctx *ctx;
|
|
|
|
|
|
|
|
for(ctx = emul_ctx; ctx != NULL; ctx = ctx->next) {
|
|
|
|
if (ctx->pid == pid)
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update emulation context for given pid, or create new if no context
|
|
|
|
* for this pid exists.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ectx_update(pid, emul)
|
|
|
|
pid_t pid;
|
2000-11-14 00:38:48 +03:00
|
|
|
const struct emulation *emul;
|
2000-04-10 13:13:45 +04:00
|
|
|
{
|
|
|
|
struct emulation_ctx *ctx;
|
|
|
|
|
|
|
|
|
|
|
|
if ((ctx = ectx_find(pid)) != NULL) {
|
|
|
|
/* found and entry, ensure the emulation is right (exec!) */
|
|
|
|
ctx->emulation = emul;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = (struct emulation_ctx *)malloc(sizeof(struct emulation_ctx));
|
|
|
|
ctx->pid = pid;
|
|
|
|
ctx->emulation = emul;
|
|
|
|
|
|
|
|
/* put the entry on start of emul_ctx chain */
|
|
|
|
ctx->next = emul_ctx;
|
|
|
|
emul_ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure current emulation context is correct for given pid.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ectx_sanify(pid)
|
|
|
|
pid_t pid;
|
|
|
|
{
|
|
|
|
struct emulation_ctx *ctx;
|
|
|
|
|
2000-07-21 00:47:02 +04:00
|
|
|
if ((ctx = ectx_find(pid)) != NULL)
|
2000-04-10 13:13:45 +04:00
|
|
|
current = ctx->emulation;
|
2000-04-10 13:34:18 +04:00
|
|
|
else if (default_emul)
|
2000-04-10 13:13:45 +04:00
|
|
|
current = default_emul;
|
2000-04-10 13:34:18 +04:00
|
|
|
else
|
|
|
|
current = &emulations[0]; /* NetBSD */
|
2000-04-10 13:13:45 +04:00
|
|
|
}
|