Deleted Added
full compact
1/*
2 * Copryight 1997 Sean Eric Fagan
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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
17 * permission.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32#ifndef lint
33static const char rcsid[] =
34 "$FreeBSD: head/usr.bin/truss/amd64-linux32.c 158626 2006-05-15 21:03:02Z pav $";
34 "$FreeBSD: head/usr.bin/truss/amd64-linux32.c 168569 2007-04-10 04:03:34Z delphij $";
35#endif /* not lint */
36
37/*
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.
41 */
42
43#include <sys/types.h>
44#include <sys/ioctl.h>
45#include <sys/pioctl.h>
44#include <sys/ptrace.h>
45
46#include <machine/reg.h>
47#include <machine/psl.h>
48
49#include <errno.h>
50#include <fcntl.h>
51#include <signal.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <time.h>
56#include <unistd.h>
57
58#include "truss.h"
59#include "syscall.h"
60#include "extern.h"
61
63static int fd = -1;
62static int cpid = -1;
63
64#include "linux_syscalls.h"
65
66static int nsyscalls =
67 sizeof(linux_syscallnames) / sizeof(linux_syscallnames[0]);
68
69/*
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).
74 *
75 * 'struct syscall' describes the system call; it may be NULL, however,
76 * if we don't know about this particular system call yet.
77 */
78static struct linux_syscall {
79 struct syscall *sc;
80 const char *name;
81 int number;
82 unsigned long args[5];
83 int nargs; /* number of arguments -- *not* number of words! */
84 char **s_args; /* the printable arguments */
85} fsc;
86
87/* Clear up and free parts of the fsc structure. */
88static __inline void
89clear_fsc(void) {
90 if (fsc.s_args) {
91 int i;
92 for (i = 0; i < fsc.nargs; i++)
93 if (fsc.s_args[i])
94 free(fsc.s_args[i]);
95 free(fsc.s_args);
96 }
97 memset(&fsc, 0, sizeof(fsc));
98}
99
100/*
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.
105 */
106
107void
108i386_linux_syscall_entry(struct trussinfo *trussinfo, int nargs) {
111 char buf[32];
109 struct reg regs;
110 int syscall_num;
111 int i;
112 struct syscall *sc;
113
117 if (fd == -1 || trussinfo->pid != cpid) {
118 sprintf(buf, "/proc/%d/regs", trussinfo->pid);
119 fd = open(buf, O_RDWR);
120 if (fd == -1) {
121 fprintf(trussinfo->outfile, "-- CANNOT OPEN REGISTERS --\n");
122 return;
123 }
124 cpid = trussinfo->pid;
125 }
114 cpid = trussinfo->curthread->tid;
115
116 clear_fsc();
128 lseek(fd, 0L, 0);
129 if (read(fd, &regs, sizeof(regs)) != sizeof(regs)) {
117
118 if (ptrace(PT_GETREGS, cpid, (caddr_t)&regs, 0) < 0)
119 {
120 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
121 return;
132 }
122 }
123 syscall_num = regs.r_eax;
124
125 fsc.number = syscall_num;
126 fsc.name =
127 (syscall_num < 0 || syscall_num > nsyscalls) ? NULL : linux_syscallnames[syscall_num];
128 if (!fsc.name) {
129 fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num);
130 }
131
132 if (fsc.name && (trussinfo->flags & FOLLOWFORKS)
133 && ((!strcmp(fsc.name, "linux_fork")
134 || !strcmp(fsc.name, "linux_vfork"))))
135 {
146 trussinfo->in_fork = 1;
136 trussinfo->curthread->in_fork = 1;
137 }
138
139 if (nargs == 0)
140 return;
141
142 /*
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?
148 */
149
150 fsc.args[0] = regs.r_ebx;
151 fsc.args[1] = regs.r_ecx;
152 fsc.args[2] = regs.r_edx;
153 fsc.args[3] = regs.r_esi;
154 fsc.args[4] = regs.r_edi;
155
156 sc = get_syscall(fsc.name);
157 if (sc) {
158 fsc.nargs = sc->nargs;
159 } else {
160#if DEBUG
161 fprintf(trussinfo->outfile, "unknown syscall %s -- setting args to %d\n",
162 fsc.name, nargs);
163#endif
164 fsc.nargs = nargs;
165 }
166
167 fsc.s_args = malloc((1+fsc.nargs) * sizeof(char*));
168 memset(fsc.s_args, 0, fsc.nargs * sizeof(char*));
169 fsc.sc = sc;
170
171 /*
172 * At this point, we set up the system call arguments.
173 * We ignore any OUT ones, however -- those are arguments that
174 * are set by the system call, and so are probably meaningless
175 * now. This doesn't currently support arguments that are
176 * passed in *and* out, however.
177 */
178
179 if (fsc.name) {
180
181#if DEBUG
182 fprintf(stderr, "syscall %s(", fsc.name);
183#endif
184 for (i = 0; i < fsc.nargs; i++) {
185#if DEBUG
186 fprintf(stderr, "0x%x%s",
187 sc
188 ? fsc.args[sc->args[i].offset]
189 : fsc.args[i],
190 i < (fsc.nargs - 1) ? "," : "");
191#endif
192 if (sc && !(sc->args[i].type & OUT)) {
203 fsc.s_args[i] = print_arg(Procfd, &sc->args[i], fsc.args, 0, trussinfo);
193 fsc.s_args[i] = print_arg(&sc->args[i], fsc.args, 0, trussinfo);
194 }
195 }
196#if DEBUG
197 fprintf(stderr, ")\n");
198#endif
199 }
200
201#if DEBUG
202 fprintf(trussinfo->outfile, "\n");
203#endif
204
205 /*
206 * Some system calls should be printed out before they are done --
207 * execve() and exit(), for example, never return. Possibly change
208 * this to work for any system call that doesn't have an OUT
209 * parameter?
210 */
211
212 if (fsc.name != NULL &&
213 (!strcmp(fsc.name, "execve") || !strcmp(fsc.name, "exit"))) {
214
215 /* XXX
216 * This could be done in a more general
217 * manner but it still wouldn't be very pretty.
218 */
219 if (!strcmp(fsc.name, "linux_execve")) {
220 if ((trussinfo->flags & EXECVEARGS) == 0)
221 if (fsc.s_args[1]) {
222 free(fsc.s_args[1]);
223 fsc.s_args[1] = NULL;
224 }
225 if ((trussinfo->flags & EXECVEENVS) == 0)
226 if (fsc.s_args[2]) {
227 free(fsc.s_args[2]);
228 fsc.s_args[2] = NULL;
229 }
230 }
231
232 print_syscall(trussinfo, fsc.name, fsc.nargs, fsc.s_args);
233 fprintf(trussinfo->outfile, "\n");
234 }
235
236 return;
237}
238
239/*
240 * Linux syscalls return negative errno's, we do positive and map them
241 */
242const int bsd_to_linux_errno[] = {
243 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
244 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
245 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
246 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
247 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
248 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
249 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
250 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
251 -6,
252};
253
254long
255i386_linux_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused)
256{
267 char buf[32];
257 struct reg regs;
258 long retval;
259 int i;
260 int errorp;
261 struct syscall *sc;
262
274 if (fd == -1 || trussinfo->pid != cpid) {
275 sprintf(buf, "/proc/%d/regs", trussinfo->pid);
276 fd = open(buf, O_RDONLY);
277 if (fd == -1) {
278 fprintf(trussinfo->outfile, "-- CANNOT OPEN REGISTERS --\n");
279 return (-1);
280 }
281 cpid = trussinfo->pid;
282 }
283
284 lseek(fd, 0L, 0);
285 if (read(fd, &regs, sizeof(regs)) != sizeof(regs)) {
286 fprintf(trussinfo->outfile, "\n");
263 cpid = trussinfo->curthread->tid;
264 if (ptrace(PT_GETREGS, cpid, (caddr_t)&regs, 0) < 0)
265 {
266 fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
267 return (-1);
268 }
269
270 retval = regs.r_eax;
271 errorp = !!(regs.r_eflags & PSL_C);
272
273 /*
274 * This code, while simpler than the initial versions I used, could
275 * stand some significant cleaning.
276 */
277
278 sc = fsc.sc;
279 if (!sc) {
280 for (i = 0; i < fsc.nargs; i++)
281 asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]);
282 } else {
283 /*
284 * Here, we only look for arguments that have OUT masked in --
285 * otherwise, they were handled in the syscall_entry function.
286 */
287 for (i = 0; i < sc->nargs; i++) {
288 char *temp;
289 if (sc->args[i].type & OUT) {
290 /*
291 * If an error occurred, than don't bothe getting the data;
292 * it may not be valid.
293 */
294 if (errorp)
295 asprintf(&temp, "0x%lx", fsc.args[sc->args[i].offset]);
296 else
316 temp = print_arg(Procfd, &sc->args[i], fsc.args, retval, trussinfo);
297 temp = print_arg(&sc->args[i], fsc.args, retval, trussinfo);
298 fsc.s_args[i] = temp;
299 }
300 }
301 }
302
303 /*
304 * It would probably be a good idea to merge the error handling,
305 * but that complicates things considerably.
306 */
307 if (errorp) {
308 for (i = 0; (size_t)i < sizeof(bsd_to_linux_errno) / sizeof(int); i++)
309 if (retval == bsd_to_linux_errno[i])
310 break;
311 }
312 print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp,
313 errorp ? i : retval);
314 clear_fsc();
315
316 return (retval);
317}