2 * Copryight 1998 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
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 $
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.
42 * This file is almost nothing more than a slightly-edited i386-fbsd.c.
45 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 #include <sys/pioctl.h>
48 #include <sys/syscall.h>
50 #include <machine/reg.h>
51 #include <machine/psl.h>
72 static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]);
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).
80 * 'struct syscall' describes the system call; it may be NULL, however,
81 * if we don't know about this particular system call yet.
83 static struct freebsd_syscall {
88 int nargs; /* number of arguments -- *not* number of words! */
89 char **s_args; /* the printable arguments */
92 /* Clear up and free parts of the fsc structure. */
100 for (i = 0; i < fsc.nargs; i++)
105 memset(&fsc, 0, sizeof(fsc));
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.
116 alpha_syscall_entry(int pid, int nargs) {
118 struct reg regs = { { 0 } };
120 unsigned int parm_offset;
122 int indir = 0; /* indirect system call */
124 if (fd == -1 || pid != cpid) {
125 asprintf(&buf, "%s/%d/regs", procfs_path, pid);
127 err(1, "Out of memory");
128 fd = open(buf, O_RDWR);
131 fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
139 i = read(fd, ®s, sizeof(regs));
140 parm_offset = regs.r_regs[R_SP] + sizeof(int);
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.
147 syscall = regs.r_regs[R_V0];
148 if (syscall == SYS_syscall || syscall == SYS___syscall) {
150 syscall = regs.r_regs[R_A0];
153 fsc.number = syscall;
155 (syscall < 0 || syscall > nsyscalls) ? NULL : syscallnames[syscall];
157 fprintf(outfile, "-- UNKNOWN SYSCALL %d --\n", syscall);
163 fsc.args = malloc((1+nargs) * sizeof(unsigned long));
167 * The OS doesn't seem to allow more than 10 words of
168 * parameters (yay!). So we shouldn't be here.
170 warn("More than 10 words (%d) of arguments!\n", nargs);
172 case 10: case 9: case 8: case 7:
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!!!
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];
191 memmove(&fsc.args[0], &fsc.args[1], (nargs-1) * sizeof(fsc.args[0]));
194 sc = get_syscall(fsc.name);
196 fsc.nargs = sc->nargs;
199 fprintf(outfile, "unknown syscall %s -- setting args to %d\n",
205 fsc.s_args = malloc((1+fsc.nargs) * sizeof(char*));
206 memset(fsc.s_args, 0, fsc.nargs * sizeof(char*));
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.
220 fprintf(stderr, "syscall %s(", fsc.name);
222 for (i = 0; i < fsc.nargs; i++) {
224 fprintf(stderr, "0x%x%s",
226 ? fsc.args[sc->args[i].offset]
228 i < (fsc.nargs -1) ? "," : "");
230 if (sc && !(sc->args[i].type & OUT)) {
231 fsc.s_args[i] = print_arg(Procfd, &sc->args[i], fsc.args);
235 fprintf(stderr, ")\n");
240 fprintf(outfile, "\n");
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
250 if (!strcmp(fsc.name, "execve") || !strcmp(fsc.name, "exit")) {
251 print_syscall(outfile, fsc.name, fsc.nargs, fsc.s_args);
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).
265 alpha_syscall_exit(int pid, int syscall) {
273 if (fd == -1 || pid != cpid) {
274 asprintf(&buf, "%s/%d/regs", procfs_path, pid);
276 err(1, "Out of memory");
277 fd = open(buf, O_RDONLY);
280 fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
287 if (read(fd, ®s, sizeof(regs)) != sizeof(regs))
289 retval = regs.r_regs[R_V0];
290 errorp = !!(regs.r_regs[R_A3]);
293 * This code, while simpler than the initial versions I used, could
294 * stand some significant cleaning.
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]);
305 * Here, we only look for arguments that have OUT masked in --
306 * otherwise, they were handled in the syscall_entry function.
308 for (i = 0; i < sc->nargs; i++) {
310 if (sc->args[i].type & OUT) {
312 * If an error occurred, than don't bothe getting the data;
313 * it may not be valid.
317 sprintf(temp, "0x%lx", fsc.args[sc->args[i].offset]);
319 temp = print_arg(Procfd, &sc->args[i], fsc.args);
321 fsc.s_args[i] = temp;
327 * It would probably be a good idea to merge the error handling,
328 * but that complicates things considerably.
331 print_syscall_ret(outfile, fsc.name, fsc.nargs, fsc.s_args, errorp, retval);