Home | History | Annotate | Line # | Download | only in kdump
setemul.c revision 1.13
      1 /*	$NetBSD: setemul.c,v 1.13 2002/10/29 07:17:43 manu Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the NetBSD
     18  *	Foundation, Inc. and its contributors.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Copyright (c) 1988, 1993
     38  *	The Regents of the University of California.  All rights reserved.
     39  * (c) UNIX System Laboratories, Inc.
     40  * All or some portions of this file are derived from material licensed
     41  * to the University of California by American Telephone and Telegraph
     42  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     43  * the permission of UNIX System Laboratories, Inc.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *	This product includes software developed by the University of
     56  *	California, Berkeley and its contributors.
     57  * 4. Neither the name of the University nor the names of its contributors
     58  *    may be used to endorse or promote products derived from this software
     59  *    without specific prior written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71  * SUCH DAMAGE.
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 #ifndef lint
     76 __RCSID("$NetBSD: setemul.c,v 1.13 2002/10/29 07:17:43 manu Exp $");
     77 #endif /* not lint */
     78 
     79 #include <sys/param.h>
     80 #include <sys/errno.h>
     81 #include <sys/time.h>
     82 
     83 #include <err.h>
     84 #include <stdio.h>
     85 #include <stdlib.h>
     86 #include <string.h>
     87 #include <unistd.h>
     88 #include <vis.h>
     89 
     90 #include "setemul.h"
     91 
     92 #include <sys/syscall.h>
     93 
     94 #include "../../sys/compat/netbsd32/netbsd32_syscall.h"
     95 #include "../../sys/compat/freebsd/freebsd_syscall.h"
     96 #include "../../sys/compat/hpux/hpux_syscall.h"
     97 #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
     98 #include "../../sys/compat/irix/irix_syscall.h"
     99 #include "../../sys/compat/linux/linux_syscall.h"
    100 #include "../../sys/compat/mach/mach_syscall.h"
    101 #include "../../sys/compat/osf1/osf1_syscall.h"
    102 #include "../../sys/compat/sunos32/sunos32_syscall.h"
    103 #include "../../sys/compat/sunos/sunos_syscall.h"
    104 #include "../../sys/compat/svr4/svr4_syscall.h"
    105 #include "../../sys/compat/svr4_32/svr4_32_syscall.h"
    106 #include "../../sys/compat/ultrix/ultrix_syscall.h"
    107 
    108 #define KTRACE
    109 #include "../../sys/kern/syscalls.c"
    110 
    111 #include "../../sys/compat/netbsd32/netbsd32_syscalls.c"
    112 #include "../../sys/compat/freebsd/freebsd_syscalls.c"
    113 #include "../../sys/compat/hpux/hpux_syscalls.c"
    114 #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
    115 #include "../../sys/compat/irix/irix_syscalls.c"
    116 #include "../../sys/compat/linux/linux_syscalls.c"
    117 #include "../../sys/compat/mach/mach_syscalls.c"
    118 #include "../../sys/compat/osf1/osf1_syscalls.c"
    119 #include "../../sys/compat/sunos/sunos_syscalls.c"
    120 #include "../../sys/compat/sunos32/sunos32_syscalls.c"
    121 #include "../../sys/compat/svr4/svr4_syscalls.c"
    122 #include "../../sys/compat/svr4_32/svr4_32_syscalls.c"
    123 #include "../../sys/compat/ultrix/ultrix_syscalls.c"
    124 
    125 #include "../../sys/compat/hpux/hpux_errno.c"
    126 #include "../../sys/compat/svr4/svr4_errno.c"
    127 #include "../../sys/compat/ibcs2/ibcs2_errno.c"
    128 #include "../../sys/compat/irix/irix_errno.c"
    129 #include "../../sys/compat/osf1/osf1_errno.c"
    130 #include "../../sys/compat/linux/common/linux_errno.c"
    131 #undef KTRACE
    132 
    133 #define SIGRTMIN	33	/* XXX */
    134 #include "../../sys/compat/hpux/hpux_signo.c"
    135 #include "../../sys/compat/svr4/svr4_signo.c"
    136 #include "../../sys/compat/ibcs2/ibcs2_signo.c"
    137 /* irix uses svr4 */
    138 #include "../../sys/compat/osf1/osf1_signo.c"
    139 #include "../../sys/compat/linux/common/linux_signo.c"
    140 
    141 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
    142 
    143 static const struct emulation emulations[] = {
    144 	{ "netbsd",	syscallnames,		SYS_MAXSYSCALL,
    145 	  NULL,				0,
    146 	  NULL,				0 },
    147 
    148 	{ "netbsd32",	netbsd32_syscallnames,	SYS_MAXSYSCALL,
    149 	  NULL,				0,
    150 	  NULL,				0 },
    151 
    152 	{ "freebsd",	freebsd_syscallnames,	FREEBSD_SYS_MAXSYSCALL,
    153 	  NULL,				0,
    154 	  NULL,				0 },
    155 
    156 	{ "hpux",	hpux_syscallnames,	HPUX_SYS_MAXSYSCALL,
    157 	  native_to_hpux_errno,		NELEM(native_to_hpux_errno),
    158 	  hpux_to_native_signo,		NSIG },
    159 
    160 	{ "ibcs2",	ibcs2_syscallnames,	IBCS2_SYS_MAXSYSCALL,
    161 	  native_to_ibcs2_errno,	NELEM(native_to_ibcs2_errno),
    162 	  ibcs2_to_native_signo,	NSIG },
    163 
    164 	{ "irix o32",	irix_syscallnames,	IRIX_SYS_MAXSYSCALL,
    165 	  native_to_irix_errno,		NELEM(native_to_irix_errno),
    166 	  svr4_to_native_signo,		NSIG },
    167 
    168 	{ "irix n32",	irix_syscallnames,	IRIX_SYS_MAXSYSCALL,
    169 	  native_to_irix_errno,		NELEM(native_to_irix_errno),
    170 	  svr4_to_native_signo,		NSIG },
    171 
    172 	{ "linux",	linux_syscallnames,	LINUX_SYS_MAXSYSCALL,
    173 	  native_to_linux_errno,	NELEM(native_to_linux_errno),
    174 	  linux_to_native_signo,	NSIG },
    175 
    176 	{ "mach",	mach_syscallnames,	MACH_SYS_MAXSYSCALL,
    177 	  NULL,				0,
    178 	  NULL,				0 },
    179 
    180 	{ "osf1",	osf1_syscallnames,	OSF1_SYS_MAXSYSCALL,
    181 	  native_to_osf1_errno,		NELEM(native_to_osf1_errno),
    182 	  osf1_to_native_signo,		NSIG },
    183 
    184 	{ "sunos32",	sunos32_syscallnames,	SUNOS32_SYS_MAXSYSCALL,
    185 	  NULL,				0,
    186 	  NULL,				0 },
    187 
    188 	{ "sunos",	sunos_syscallnames,	SUNOS_SYS_MAXSYSCALL,
    189 	  NULL,				0,
    190 	  NULL,				0 },
    191 
    192 	{ "svr4",	svr4_syscallnames,	SVR4_SYS_MAXSYSCALL,
    193 	  native_to_svr4_errno,		NELEM(native_to_svr4_errno),
    194 	  svr4_to_native_signo,		NSIG },
    195 
    196 	{ "svr4_32",	svr4_syscallnames,	SVR4_SYS_MAXSYSCALL,
    197 	  native_to_svr4_errno,		NELEM(native_to_svr4_errno),
    198 	  svr4_to_native_signo,		NSIG },
    199 
    200 	{ "ultrix",	ultrix_syscallnames,	ULTRIX_SYS_MAXSYSCALL,
    201 	  NULL,				0,
    202 	  NULL,				0 },
    203 
    204 	{ "pecoff",	syscallnames,		SYS_MAXSYSCALL,
    205 	  NULL,				0,
    206 	  NULL,				0 },
    207 
    208 	{ NULL,		NULL,			0,
    209 	  NULL,				0,
    210 	  NULL,				0 }
    211 };
    212 
    213 struct emulation_ctx {
    214 	pid_t	pid;
    215 	const struct emulation *emulation;
    216 	struct emulation_ctx *next;
    217 };
    218 
    219 const struct emulation *current;
    220 const struct emulation *previous;
    221 
    222 static const struct emulation *default_emul=NULL;
    223 
    224 struct emulation_ctx *current_ctx;
    225 struct emulation_ctx *emul_ctx = NULL;
    226 
    227 static struct emulation_ctx *ectx_find __P((pid_t));
    228 static void	ectx_update __P((pid_t, const struct emulation *));
    229 
    230 void
    231 setemul(name, pid, update_ectx)
    232 	const char *name;
    233 	pid_t pid;
    234 	int update_ectx;
    235 {
    236 	int i;
    237 	const struct emulation *match = NULL;
    238 
    239 	for (i = 0; emulations[i].name != NULL; i++) {
    240 		if (strcmp(emulations[i].name, name) == 0) {
    241 			match = &emulations[i];
    242 			break;
    243 		}
    244 	}
    245 
    246 	if (!match) {
    247 		warnx("Emulation `%s' unknown", name);
    248 		return;
    249 	}
    250 
    251 	if (update_ectx)
    252 		ectx_update(pid, match);
    253 
    254 	if (!default_emul)
    255 		default_emul = match;
    256 
    257 	if (current != NULL)
    258 		previous = current;
    259 	else
    260 		previous = match;
    261 
    262 	current = match;
    263 }
    264 
    265 /*
    266  * Emulation context list is very simple chained list, not even hashed.
    267  * We expect the number of separate traced contexts/processes to be
    268  * fairly low, so it's not worth it to optimize this.
    269  */
    270 
    271 /*
    272  * Find an emulation context appropriate for the given pid.
    273  */
    274 static struct emulation_ctx *
    275 ectx_find(pid)
    276 	pid_t pid;
    277 {
    278 	struct emulation_ctx *ctx;
    279 
    280 	for(ctx = emul_ctx; ctx != NULL; ctx = ctx->next) {
    281 		if (ctx->pid == pid)
    282 			return ctx;
    283 	}
    284 
    285 	return NULL;
    286 }
    287 
    288 /*
    289  * Update emulation context for given pid, or create new if no context
    290  * for this pid exists.
    291  */
    292 static void
    293 ectx_update(pid, emul)
    294 	pid_t pid;
    295 	const struct emulation *emul;
    296 {
    297 	struct emulation_ctx *ctx;
    298 
    299 
    300 	if ((ctx = ectx_find(pid)) != NULL) {
    301 		/* found and entry, ensure the emulation is right (exec!) */
    302 		ctx->emulation = emul;
    303 		return;
    304 	}
    305 
    306 	ctx = (struct emulation_ctx *)malloc(sizeof(struct emulation_ctx));
    307 	ctx->pid = pid;
    308 	ctx->emulation = emul;
    309 
    310 	/* put the entry on start of emul_ctx chain */
    311 	ctx->next = emul_ctx;
    312 	emul_ctx = ctx;
    313 }
    314 
    315 /*
    316  * Ensure current emulation context is correct for given pid.
    317  */
    318 void
    319 ectx_sanify(pid)
    320 	pid_t pid;
    321 {
    322 	struct emulation_ctx *ctx;
    323 
    324 	if ((ctx = ectx_find(pid)) != NULL)
    325 		current = ctx->emulation;
    326 	else if (default_emul)
    327 		current = default_emul;
    328 	else
    329 		current = &emulations[0]; /* NetBSD */
    330 }
    331