Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / truss / i386-fbsd.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
32 #ifndef lint
33 static const char rcsid[] =
34   "$FreeBSD: src/usr.bin/truss/i386-fbsd.c,v 1.7.2.2 2001/10/29 20:12:56 des Exp $";
35 #endif /* not lint */
36
37 /*
38  * FreeBSD/386-specific system call handling.  This is probably the most
39  * complex part of the entire truss program, although I've got lots of
40  * it handled relatively cleanly now.  The system call names are generated
41  * automatically, thanks to /usr/src/sys/kern/syscalls.master.  The
42  * names used for the various structures are confusing, I sadly admit.
43  */
44
45 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 #include <sys/pioctl.h>
48 #include <sys/syscall.h>
49
50 #include <machine/reg.h>
51 #include <machine/psl.h>
52
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <signal.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include "syscall.h"
62
63 static int fd = -1;
64 static int cpid = -1;
65 extern int Procfd;
66
67 extern FILE *outfile;
68 #include "syscalls.h"
69
70 static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]);
71
72 /*
73  * This is what this particular file uses to keep track of a system call.
74  * It is probably not quite sufficient -- I can probably use the same
75  * structure for the various syscall personalities, and I also probably
76  * need to nest system calls (for signal handlers).
77  *
78  * 'struct syscall' describes the system call; it may be NULL, however,
79  * if we don't know about this particular system call yet.
80  */
81 static struct freebsd_syscall {
82         struct syscall *sc;
83         char *name;
84         int number;
85         unsigned long *args;
86         int nargs;      /* number of arguments -- *not* number of words! */
87         char **s_args;  /* the printable arguments */
88 } fsc;
89
90 /* Clear up and free parts of the fsc structure. */
91 static inline void
92 clear_fsc() {
93   if (fsc.args) {
94     free(fsc.args);
95   }
96   if (fsc.s_args) {
97     int i;
98     for (i = 0; i < fsc.nargs; i++)
99       if (fsc.s_args[i])
100         free(fsc.s_args[i]);
101     free(fsc.s_args);
102   }
103   memset(&fsc, 0, sizeof(fsc));
104 }
105
106 /*
107  * Called when a process has entered a system call.  nargs is the
108  * number of words, not number of arguments (a necessary distinction
109  * in some cases).  Note that if the STOPEVENT() code in i386/i386/trap.c
110  * is ever changed these functions need to keep up.
111  */
112
113 void
114 i386_syscall_entry(int pid, int nargs) {
115   char buf[32];
116   struct reg regs = { 0 };
117   int syscall;
118   int i;
119   unsigned int parm_offset;
120   struct syscall *sc;
121
122   if (fd == -1 || pid != cpid) {
123     sprintf(buf, "/proc/%d/regs", pid);
124     fd = open(buf, O_RDWR);
125     if (fd == -1) {
126       fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
127       return;
128     }
129     cpid = pid;
130   }
131
132   clear_fsc();
133   lseek(fd, 0L, 0);
134   i = read(fd, &regs, sizeof(regs));
135   parm_offset = regs.r_esp + sizeof(int);
136
137   /*
138    * FreeBSD has two special kinds of system call redirctions --
139    * SYS_syscall, and SYS___syscall.  The former is the old syscall()
140    * routine, basicly; the latter is for quad-aligned arguments.
141    */
142   syscall = regs.r_eax;
143   switch (syscall) {
144   case SYS_syscall:
145     lseek(Procfd, parm_offset, SEEK_SET);
146     read(Procfd, &syscall, sizeof(int));
147     parm_offset += sizeof(int);
148     break;
149   case SYS___syscall:
150     lseek(Procfd, parm_offset, SEEK_SET);
151     read(Procfd, &syscall, sizeof(int));
152     parm_offset += sizeof(quad_t);
153     break;
154   }
155
156   fsc.number = syscall;
157   fsc.name =
158     (syscall < 0 || syscall > nsyscalls) ? NULL : syscallnames[syscall];
159   if (!fsc.name) {
160     fprintf(outfile, "-- UNKNOWN SYSCALL %d --\n", syscall);
161   }
162
163   if (nargs == 0)
164     return;
165
166   fsc.args = malloc((1+nargs) * sizeof(unsigned long));
167   lseek(Procfd, parm_offset, SEEK_SET);
168   if (read(Procfd, fsc.args, nargs * sizeof(unsigned long)) == -1)
169     return;
170
171   sc = get_syscall(fsc.name);
172   if (sc) {
173     fsc.nargs = sc->nargs;
174   } else {
175 #if DEBUG
176     fprintf(outfile, "unknown syscall %s -- setting args to %d\n",
177            fsc.name, nargs);
178 #endif
179     fsc.nargs = nargs;
180   }
181
182   fsc.s_args = malloc((1+fsc.nargs) * sizeof(char*));
183   memset(fsc.s_args, 0, fsc.nargs * sizeof(char*));
184   fsc.sc = sc;
185
186   /*
187    * At this point, we set up the system call arguments.
188    * We ignore any OUT ones, however -- those are arguments that
189    * are set by the system call, and so are probably meaningless
190    * now.  This doesn't currently support arguments that are
191    * passed in *and* out, however.
192    */
193
194   if (fsc.name) {
195
196 #if DEBUG
197     fprintf(stderr, "syscall %s(", fsc.name);
198 #endif
199     for (i = 0; i < fsc.nargs; i++) {
200 #if DEBUG
201       fprintf(stderr, "0x%x%s",
202              sc
203              ? fsc.args[sc->args[i].offset]
204              : fsc.args[i],
205              i < (fsc.nargs -1) ? "," : "");
206 #endif
207       if (sc && !(sc->args[i].type & OUT)) {
208         fsc.s_args[i] = print_arg(Procfd, &sc->args[i], fsc.args);
209       }
210     }
211 #if DEBUG
212     fprintf(stderr, ")\n");
213 #endif
214   }
215
216 #if DEBUG
217   fprintf(outfile, "\n");
218 #endif
219
220   /*
221    * Some system calls should be printed out before they are done --
222    * execve() and exit(), for example, never return.  Possibly change
223    * this to work for any system call that doesn't have an OUT
224    * parameter?
225    */
226
227   if (!strcmp(fsc.name, "execve") || !strcmp(fsc.name, "exit")) {
228     print_syscall(outfile, fsc.name, fsc.nargs, fsc.s_args);
229   }
230
231   return;
232 }
233
234 /*
235  * And when the system call is done, we handle it here.
236  * Currently, no attempt is made to ensure that the system calls
237  * match -- this needs to be fixed (and is, in fact, why S_SCX includes
238  * the sytem call number instead of, say, an error status).
239  */
240
241 void
242 i386_syscall_exit(int pid, int syscall) {
243   char buf[32];
244   struct reg regs;
245   int retval;
246   int i;
247   int errorp;
248   struct syscall *sc;
249
250   if (fd == -1 || pid != cpid) {
251     sprintf(buf, "/proc/%d/regs", pid);
252     fd = open(buf, O_RDONLY);
253     if (fd == -1) {
254       fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
255       return;
256     }
257     cpid = pid;
258   }
259
260   lseek(fd, 0L, 0);
261   if (read(fd, &regs, sizeof(regs)) != sizeof(regs))
262     return;
263   retval = regs.r_eax;
264   errorp = !!(regs.r_eflags & PSL_C);
265
266   /*
267    * This code, while simpler than the initial versions I used, could
268    * stand some significant cleaning.
269    */
270
271   sc = fsc.sc;
272   if (!sc) {
273     for (i = 0; i < fsc.nargs; i++) {
274       fsc.s_args[i] = malloc(12);
275       sprintf(fsc.s_args[i], "0x%lx", fsc.args[i]);
276     }
277   } else {
278     /*
279      * Here, we only look for arguments that have OUT masked in --
280      * otherwise, they were handled in the syscall_entry function.
281      */
282     for (i = 0; i < sc->nargs; i++) {
283       char *temp;
284       if (sc->args[i].type & OUT) {
285         /*
286          * If an error occurred, than don't bothe getting the data;
287          * it may not be valid.
288          */
289         if (errorp) {
290           temp = malloc(12);
291           sprintf(temp, "0x%lx", fsc.args[sc->args[i].offset]);
292         } else {
293           temp = print_arg(Procfd, &sc->args[i], fsc.args);
294         }
295         fsc.s_args[i] = temp;
296       }
297     }
298   }
299
300   /*
301    * It would probably be a good idea to merge the error handling,
302    * but that complicates things considerably.
303    */
304
305   print_syscall_ret(outfile, fsc.name, fsc.nargs, fsc.s_args, errorp, retval);
306   clear_fsc();
307
308   return;
309 }