2 * Copyright 1997 Sean Eric Fagan
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Sean Eric Fagan
15 * 4. Neither the name of the author may be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char rcsid[] =
38 * Linux/i386-specific system call handling. Given how much of this code
39 * is taken from the freebsd equivalent, I can probably put even more of
40 * it in support routines that can be used by any personality support.
43 #include <sys/types.h>
44 #include <sys/ptrace.h>
46 #include <machine/reg.h>
47 #include <machine/psl.h>
64 #include "linux32_syscalls.h"
66 static int nsyscalls =
67 sizeof(linux32_syscallnames) / sizeof(linux32_syscallnames[0]);
70 * This is what this particular file uses to keep track of a system call.
71 * It is probably not quite sufficient -- I can probably use the same
72 * structure for the various syscall personalities, and I also probably
73 * need to nest system calls (for signal handlers).
75 * 'struct syscall' describes the system call; it may be NULL, however,
76 * if we don't know about this particular system call yet.
78 static struct linux_syscall {
82 unsigned long args[5];
83 int nargs; /* number of arguments -- *not* number of words! */
84 char **s_args; /* the printable arguments */
87 /* Clear up and free parts of the fsc structure. */
92 for (i = 0; i < fsc.nargs; i++)
97 memset(&fsc, 0, sizeof(fsc));
101 * Called when a process has entered a system call. nargs is the
102 * number of words, not number of arguments (a necessary distinction
103 * in some cases). Note that if the STOPEVENT() code in i386/i386/trap.c
104 * is ever changed these functions need to keep up.
108 amd64_linux32_syscall_entry(struct trussinfo *trussinfo, int nargs) {
114 cpid = trussinfo->curthread->tid;
118 if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0)
120 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
123 syscall_num = regs.r_rax;
125 fsc.number = syscall_num;
127 (syscall_num < 0 || syscall_num >= nsyscalls) ? NULL : linux32_syscallnames[syscall_num];
129 fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num);
132 if (fsc.name && (trussinfo->flags & FOLLOWFORKS)
133 && ((!strcmp(fsc.name, "linux_fork")
134 || !strcmp(fsc.name, "linux_vfork"))))
136 trussinfo->curthread->in_fork = 1;
143 * Linux passes syscall arguments in registers, not
144 * on the stack. Fortunately, we've got access to the
145 * register set. Note that we don't bother checking the
146 * number of arguments. And what does linux do for syscalls
147 * that have more than five arguments?
150 fsc.args[0] = regs.r_rbx;
151 fsc.args[1] = regs.r_rcx;
152 fsc.args[2] = regs.r_rdx;
153 fsc.args[3] = regs.r_rsi;
154 fsc.args[4] = regs.r_rdi;
156 sc = get_syscall(fsc.name);
158 fsc.nargs = sc->nargs;
161 fprintf(trussinfo->outfile, "unknown syscall %s -- setting args to %d\n",
167 fsc.s_args = calloc(1, (1+fsc.nargs) * sizeof(char*));
171 * At this point, we set up the system call arguments.
172 * We ignore any OUT ones, however -- those are arguments that
173 * are set by the system call, and so are probably meaningless
174 * now. This doesn't currently support arguments that are
175 * passed in *and* out, however.
181 fprintf(stderr, "syscall %s(", fsc.name);
183 for (i = 0; i < fsc.nargs; i++) {
185 fprintf(stderr, "0x%x%s",
187 ? fsc.args[sc->args[i].offset]
189 i < (fsc.nargs - 1) ? "," : "");
191 if (sc && !(sc->args[i].type & OUT)) {
192 fsc.s_args[i] = print_arg(&sc->args[i], fsc.args, 0, trussinfo);
196 fprintf(stderr, ")\n");
201 fprintf(trussinfo->outfile, "\n");
204 if (fsc.name != NULL &&
205 (!strcmp(fsc.name, "linux_execve") || !strcmp(fsc.name, "exit"))) {
208 * This could be done in a more general
209 * manner but it still wouldn't be very pretty.
211 if (!strcmp(fsc.name, "linux_execve")) {
212 if ((trussinfo->flags & EXECVEARGS) == 0)
215 fsc.s_args[1] = NULL;
217 if ((trussinfo->flags & EXECVEENVS) == 0)
220 fsc.s_args[2] = NULL;
229 * Linux syscalls return negative errno's, we do positive and map them
231 const int bsd_to_linux_errno[] = {
232 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
233 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
234 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
235 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
236 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
237 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
238 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
239 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
244 amd64_linux32_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused)
252 if (fsc.name == NULL)
255 cpid = trussinfo->curthread->tid;
256 if (ptrace(PT_GETREGS, cpid, (caddr_t)®s, 0) < 0)
258 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
263 errorp = !!(regs.r_rflags & PSL_C);
266 * This code, while simpler than the initial versions I used, could
267 * stand some significant cleaning.
272 for (i = 0; i < fsc.nargs; i++)
273 asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]);
276 * Here, we only look for arguments that have OUT masked in --
277 * otherwise, they were handled in the syscall_entry function.
279 for (i = 0; i < sc->nargs; i++) {
281 if (sc->args[i].type & OUT) {
283 * If an error occurred, than don't bothe getting the data;
284 * it may not be valid.
287 asprintf(&temp, "0x%lx", fsc.args[sc->args[i].offset]);
289 temp = print_arg(&sc->args[i], fsc.args, retval, trussinfo);
290 fsc.s_args[i] = temp;
296 * It would probably be a good idea to merge the error handling,
297 * but that complicates things considerably.
300 for (i = 0; (size_t)i < sizeof(bsd_to_linux_errno) / sizeof(int); i++)
301 if (retval == bsd_to_linux_errno[i])
305 if (fsc.name != NULL &&
306 (!strcmp(fsc.name, "linux_execve") || !strcmp(fsc.name, "exit"))) {
307 trussinfo->curthread->in_syscall = 1;
310 print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp,
311 errorp ? i : retval, fsc.sc);