setemul.c revision 1.12 1 /* $NetBSD: setemul.c,v 1.12 2002/03/31 22:44:03 christos 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.12 2002/03/31 22:44:03 christos 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/osf1/osf1_syscall.h"
101 #include "../../sys/compat/sunos32/sunos32_syscall.h"
102 #include "../../sys/compat/sunos/sunos_syscall.h"
103 #include "../../sys/compat/svr4/svr4_syscall.h"
104 #include "../../sys/compat/svr4_32/svr4_32_syscall.h"
105 #include "../../sys/compat/ultrix/ultrix_syscall.h"
106
107 #define KTRACE
108 #include "../../sys/kern/syscalls.c"
109
110 #include "../../sys/compat/netbsd32/netbsd32_syscalls.c"
111 #include "../../sys/compat/freebsd/freebsd_syscalls.c"
112 #include "../../sys/compat/hpux/hpux_syscalls.c"
113 #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
114 #include "../../sys/compat/irix/irix_syscalls.c"
115 #include "../../sys/compat/linux/linux_syscalls.c"
116 #include "../../sys/compat/osf1/osf1_syscalls.c"
117 #include "../../sys/compat/sunos/sunos_syscalls.c"
118 #include "../../sys/compat/sunos32/sunos32_syscalls.c"
119 #include "../../sys/compat/svr4/svr4_syscalls.c"
120 #include "../../sys/compat/svr4_32/svr4_32_syscalls.c"
121 #include "../../sys/compat/ultrix/ultrix_syscalls.c"
122
123 #include "../../sys/compat/hpux/hpux_errno.c"
124 #include "../../sys/compat/svr4/svr4_errno.c"
125 #include "../../sys/compat/ibcs2/ibcs2_errno.c"
126 #include "../../sys/compat/irix/irix_errno.c"
127 #include "../../sys/compat/osf1/osf1_errno.c"
128 #include "../../sys/compat/linux/common/linux_errno.c"
129 #undef KTRACE
130
131 #define SIGRTMIN 33 /* XXX */
132 #include "../../sys/compat/hpux/hpux_signo.c"
133 #include "../../sys/compat/svr4/svr4_signo.c"
134 #include "../../sys/compat/ibcs2/ibcs2_signo.c"
135 /* irix uses svr4 */
136 #include "../../sys/compat/osf1/osf1_signo.c"
137 #include "../../sys/compat/linux/common/linux_signo.c"
138
139 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
140
141 static const struct emulation emulations[] = {
142 { "netbsd", syscallnames, SYS_MAXSYSCALL,
143 NULL, 0,
144 NULL, 0 },
145
146 { "netbsd32", netbsd32_syscallnames, SYS_MAXSYSCALL,
147 NULL, 0,
148 NULL, 0 },
149
150 { "freebsd", freebsd_syscallnames, FREEBSD_SYS_MAXSYSCALL,
151 NULL, 0,
152 NULL, 0 },
153
154 { "hpux", hpux_syscallnames, HPUX_SYS_MAXSYSCALL,
155 native_to_hpux_errno, NELEM(native_to_hpux_errno),
156 hpux_to_native_signo, NSIG },
157
158 { "ibcs2", ibcs2_syscallnames, IBCS2_SYS_MAXSYSCALL,
159 native_to_ibcs2_errno, NELEM(native_to_ibcs2_errno),
160 ibcs2_to_native_signo, NSIG },
161
162 { "irix o32", irix_syscallnames, IRIX_SYS_MAXSYSCALL,
163 native_to_irix_errno, NELEM(native_to_irix_errno),
164 svr4_to_native_signo, NSIG },
165
166 { "irix n32", irix_syscallnames, IRIX_SYS_MAXSYSCALL,
167 native_to_irix_errno, NELEM(native_to_irix_errno),
168 svr4_to_native_signo, NSIG },
169
170 { "linux", linux_syscallnames, LINUX_SYS_MAXSYSCALL,
171 native_to_linux_errno, NELEM(native_to_linux_errno),
172 linux_to_native_signo, NSIG },
173
174 { "osf1", osf1_syscallnames, OSF1_SYS_MAXSYSCALL,
175 native_to_osf1_errno, NELEM(native_to_osf1_errno),
176 osf1_to_native_signo, NSIG },
177
178 { "sunos32", sunos32_syscallnames, SUNOS32_SYS_MAXSYSCALL,
179 NULL, 0,
180 NULL, 0 },
181
182 { "sunos", sunos_syscallnames, SUNOS_SYS_MAXSYSCALL,
183 NULL, 0,
184 NULL, 0 },
185
186 { "svr4", svr4_syscallnames, SVR4_SYS_MAXSYSCALL,
187 native_to_svr4_errno, NELEM(native_to_svr4_errno),
188 svr4_to_native_signo, NSIG },
189
190 { "svr4_32", svr4_syscallnames, SVR4_SYS_MAXSYSCALL,
191 native_to_svr4_errno, NELEM(native_to_svr4_errno),
192 svr4_to_native_signo, NSIG },
193
194 { "ultrix", ultrix_syscallnames, ULTRIX_SYS_MAXSYSCALL,
195 NULL, 0,
196 NULL, 0 },
197
198 { "pecoff", syscallnames, SYS_MAXSYSCALL,
199 NULL, 0,
200 NULL, 0 },
201
202 { NULL, NULL, 0,
203 NULL, 0,
204 NULL, 0 }
205 };
206
207 struct emulation_ctx {
208 pid_t pid;
209 const struct emulation *emulation;
210 struct emulation_ctx *next;
211 };
212
213 const struct emulation *current;
214 const struct emulation *previous;
215
216 static const struct emulation *default_emul=NULL;
217
218 struct emulation_ctx *current_ctx;
219 struct emulation_ctx *emul_ctx = NULL;
220
221 static struct emulation_ctx *ectx_find __P((pid_t));
222 static void ectx_update __P((pid_t, const struct emulation *));
223
224 void
225 setemul(name, pid, update_ectx)
226 const char *name;
227 pid_t pid;
228 int update_ectx;
229 {
230 int i;
231 const struct emulation *match = NULL;
232
233 for (i = 0; emulations[i].name != NULL; i++) {
234 if (strcmp(emulations[i].name, name) == 0) {
235 match = &emulations[i];
236 break;
237 }
238 }
239
240 if (!match) {
241 warnx("Emulation `%s' unknown", name);
242 return;
243 }
244
245 if (update_ectx)
246 ectx_update(pid, match);
247
248 if (!default_emul)
249 default_emul = match;
250
251 if (current != NULL)
252 previous = current;
253 else
254 previous = match;
255
256 current = match;
257 }
258
259 /*
260 * Emulation context list is very simple chained list, not even hashed.
261 * We expect the number of separate traced contexts/processes to be
262 * fairly low, so it's not worth it to optimize this.
263 */
264
265 /*
266 * Find an emulation context appropriate for the given pid.
267 */
268 static struct emulation_ctx *
269 ectx_find(pid)
270 pid_t pid;
271 {
272 struct emulation_ctx *ctx;
273
274 for(ctx = emul_ctx; ctx != NULL; ctx = ctx->next) {
275 if (ctx->pid == pid)
276 return ctx;
277 }
278
279 return NULL;
280 }
281
282 /*
283 * Update emulation context for given pid, or create new if no context
284 * for this pid exists.
285 */
286 static void
287 ectx_update(pid, emul)
288 pid_t pid;
289 const struct emulation *emul;
290 {
291 struct emulation_ctx *ctx;
292
293
294 if ((ctx = ectx_find(pid)) != NULL) {
295 /* found and entry, ensure the emulation is right (exec!) */
296 ctx->emulation = emul;
297 return;
298 }
299
300 ctx = (struct emulation_ctx *)malloc(sizeof(struct emulation_ctx));
301 ctx->pid = pid;
302 ctx->emulation = emul;
303
304 /* put the entry on start of emul_ctx chain */
305 ctx->next = emul_ctx;
306 emul_ctx = ctx;
307 }
308
309 /*
310 * Ensure current emulation context is correct for given pid.
311 */
312 void
313 ectx_sanify(pid)
314 pid_t pid;
315 {
316 struct emulation_ctx *ctx;
317
318 if ((ctx = ectx_find(pid)) != NULL)
319 current = ctx->emulation;
320 else if (default_emul)
321 current = default_emul;
322 else
323 current = &emulations[0]; /* NetBSD */
324 }
325