Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / truss / i386-linux.c
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  * $FreeBSD: src/usr.bin/truss/i386-linux.c,v 1.7.2.4 2002/02/15 11:43:51 des Exp $
32  * $DragonFly: src/usr.bin/truss/i386-linux.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
33  */
34
35 /*
36  * Linux/i386-specific system call handling.  Given how much of this code
37  * is taken from the freebsd equivalent, I can probably put even more of
38  * it in support routines that can be used by any personality support.
39  */
40
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 #include <sys/pioctl.h>
44
45 #include <machine/reg.h>
46 #include <machine/psl.h>
47
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "extern.h"
57 #include "syscall.h"
58
59 static int fd = -1;
60 static int cpid = -1;
61 extern int Procfd;
62
63 extern FILE *outfile;
64 #include "linux_syscalls.h"
65
66 static int nsyscalls =
67         sizeof(linux_syscallnames) / sizeof(linux_syscallnames[0]);
68
69 /* See the comment in i386-fbsd.c about this structure. */
70 static struct linux_syscall {
71         struct syscall *sc;
72         char *name;
73         int number;
74         unsigned long args[5];
75         int nargs;      /* number of arguments -- *not* number of words! */
76         char **s_args;  /* the printable arguments */
77 } lsc;
78
79 static inline void
80 clear_lsc() {
81   if (lsc.s_args) {
82     int i;
83     for (i = 0; i < lsc.nargs; i++)
84       if (lsc.s_args[i])
85         free(lsc.s_args[i]);
86     free(lsc.s_args);
87   }
88   memset(&lsc, 0, sizeof(lsc));
89 }
90
91 void
92 i386_linux_syscall_entry(int pid, int nargs) {
93   char buf[32];
94   struct reg regs = { 0 };
95   int syscall;
96   int i;
97   struct syscall *sc;
98
99   if (fd == -1 || pid != cpid) {
100     sprintf(buf, "/proc/%d/regs", pid);
101     fd = open(buf, O_RDWR);
102     if (fd == -1) {
103       fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
104       return;
105     }
106     cpid = pid;
107   }
108
109   clear_lsc();
110   lseek(fd, 0L, 0);
111   i = read(fd, &regs, sizeof(regs));
112   syscall = regs.r_eax;
113
114   lsc.number = syscall;
115   lsc.name =
116     (syscall < 0 || syscall > nsyscalls) ? NULL : linux_syscallnames[syscall];
117   if (!lsc.name) {
118     fprintf (outfile, "-- UNKNOWN SYSCALL %d\n", syscall);
119   }
120
121   if (nargs == 0)
122     return;
123
124   /*
125    * Linux passes syscall arguments in registers, not
126    * on the stack.  Fortunately, we've got access to the
127    * register set.  Note that we don't bother checking the
128    * number of arguments.  And what does linux do for syscalls
129    * that have more than five arguments?
130    */
131
132   lsc.args[0] = regs.r_ebx;
133   lsc.args[1] = regs.r_ecx;
134   lsc.args[2] = regs.r_edx;
135   lsc.args[3] = regs.r_esi;
136   lsc.args[4] = regs.r_edi;
137
138   sc = get_syscall(lsc.name);
139   if (sc) {
140     lsc.nargs = sc->nargs;
141   } else {
142 #ifdef DEBUG
143     fprintf(outfile, "unknown syscall %s -- setting args to %d\n",
144             lsc.name, nargs);
145 #endif
146     lsc.nargs = nargs;
147   }
148
149   lsc.s_args = malloc((1+lsc.nargs) * sizeof(char*));
150   memset(lsc.s_args, 0, lsc.nargs * sizeof(char*));
151   lsc.sc = sc;
152
153   if (lsc.name) {
154
155 #ifdef DEBUG
156     fprintf(stderr, "syscall %s(", lsc.name);
157 #endif
158     for (i = 0; i < lsc.nargs ; i++) {
159 #ifdef DEBUG
160       fprintf(stderr, "0x%x%s",
161               sc ?
162               lsc.args[sc->args[i].offset]
163               : lsc.args[i],
164               i < (lsc.nargs - 1) ? "," : "");
165 #endif
166       if (sc && !(sc->args[i].type & OUT)) {
167         lsc.s_args[i] = print_arg(Procfd, &sc->args[i], lsc.args);
168       }
169     }
170 #ifdef DEBUG
171     fprintf(stderr, ")\n");
172 #endif
173   }
174
175   if (!strcmp(lsc.name, "linux_execve") || !strcmp(lsc.name, "exit")) {
176     print_syscall(outfile, lsc.name, lsc.nargs, lsc.s_args);
177   }
178
179   return;
180 }
181
182 /*
183  * Linux syscalls return negative errno's, we do positive and map them
184  */
185 const int bsd_to_linux_errno[] = {
186         -0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
187         -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
188         -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
189         -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
190         -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
191         -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
192         -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
193         -116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
194         -6, 
195 };
196
197 void
198 i386_linux_syscall_exit(int pid, int syscall) {
199   char buf[32];
200   struct reg regs;
201   int retval;
202   int i;
203   int errorp;
204   struct syscall *sc;
205
206   if (fd == -1 || pid != cpid) {
207     sprintf(buf, "/proc/%d/regs", pid);
208     fd = open(buf, O_RDONLY);
209     if (fd == -1) {
210       fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
211       return;
212     }
213     cpid = pid;
214   }
215
216   lseek(fd, 0L, 0);
217   if (read(fd, &regs, sizeof(regs)) != sizeof(regs))
218     return;
219
220   retval = regs.r_eax;
221   errorp = !!(regs.r_eflags & PSL_C);
222
223   sc = lsc.sc;
224   if (!sc) {
225     for (i = 0; i < lsc.nargs; i++) {
226       lsc.s_args[i] = malloc(12);
227       sprintf(lsc.s_args[i], "0x%lx", lsc.args[i]);
228     }
229   } else {
230     for (i = 0; i < sc->nargs; i++) {
231       char *temp;
232       if (sc->args[i].type & OUT) {
233         if (errorp) {
234           temp = malloc(12);
235           sprintf(temp, "0x%lx", lsc.args[sc->args[i].offset]);
236         } else {
237           temp = print_arg(Procfd, &sc->args[i], lsc.args);
238         }
239         lsc.s_args[i] = temp;
240       }
241     }
242   }
243   if (errorp) {
244     for (i = 0; i < sizeof(bsd_to_linux_errno) / sizeof(int); i++)
245       if (retval == bsd_to_linux_errno[i])
246       break;
247   }
248   print_syscall_ret(outfile, lsc.name, lsc.nargs, lsc.s_args, errorp,
249     errorp ? i : retval);
250   clear_lsc();
251   return;
252 }