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