1106716Smarcel/*
2204977Simp * Copyright 1997 Sean Eric Fagan
3106716Smarcel *
4106716Smarcel * Redistribution and use in source and binary forms, with or without
5106716Smarcel * modification, are permitted provided that the following conditions
6106716Smarcel * are met:
7106716Smarcel * 1. Redistributions of source code must retain the above copyright
8106716Smarcel *    notice, this list of conditions and the following disclaimer.
9106716Smarcel * 2. Redistributions in binary form must reproduce the above copyright
10106716Smarcel *    notice, this list of conditions and the following disclaimer in the
11106716Smarcel *    documentation and/or other materials provided with the distribution.
12106716Smarcel * 3. All advertising materials mentioning features or use of this software
13106716Smarcel *    must display the following acknowledgement:
14106716Smarcel *	This product includes software developed by Sean Eric Fagan
15106716Smarcel * 4. Neither the name of the author may be used to endorse or promote
16106716Smarcel *    products derived from this software without specific prior written
17106716Smarcel *    permission.
18106716Smarcel *
19106716Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20106716Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21106716Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22106716Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23106716Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24106716Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25106716Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26106716Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27106716Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28106716Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29106716Smarcel * SUCH DAMAGE.
30106716Smarcel */
31106716Smarcel
32106716Smarcel#ifndef lint
33106716Smarcelstatic const char rcsid[] =
34106716Smarcel  "$FreeBSD$";
35106716Smarcel#endif /* not lint */
36106716Smarcel
37106716Smarcel/*
38111176Sru * FreeBSD/ia64-specific system call handling.  This is probably the most
39106716Smarcel * complex part of the entire truss program, although I've got lots of
40106716Smarcel * it handled relatively cleanly now.  The system call names are generated
41106716Smarcel * automatically, thanks to /usr/src/sys/kern/syscalls.master.  The
42106716Smarcel * names used for the various structures are confusing, I sadly admit.
43106716Smarcel */
44106716Smarcel
45106716Smarcel#include <sys/types.h>
46168569Sdelphij#include <sys/ptrace.h>
47106716Smarcel#include <sys/syscall.h>
48106716Smarcel
49106716Smarcel#include <machine/reg.h>
50106716Smarcel
51106716Smarcel#include <errno.h>
52106716Smarcel#include <fcntl.h>
53106716Smarcel#include <signal.h>
54106716Smarcel#include <stdio.h>
55106716Smarcel#include <stdlib.h>
56106716Smarcel#include <string.h>
57106716Smarcel#include <time.h>
58106716Smarcel#include <unistd.h>
59106716Smarcel
60106716Smarcel#include "truss.h"
61106716Smarcel#include "syscall.h"
62106716Smarcel#include "extern.h"
63106716Smarcel
64106716Smarcel#include "syscalls.h"
65106716Smarcel
66106716Smarcelstatic int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]);
67106716Smarcel
68106716Smarcel/*
69106716Smarcel * This is what this particular file uses to keep track of a system call.
70106716Smarcel * It is probably not quite sufficient -- I can probably use the same
71106716Smarcel * structure for the various syscall personalities, and I also probably
72106716Smarcel * need to nest system calls (for signal handlers).
73106716Smarcel *
74106716Smarcel * 'struct syscall' describes the system call; it may be NULL, however,
75106716Smarcel * if we don't know about this particular system call yet.
76106716Smarcel */
77241162Szontstruct freebsd_syscall {
78106716Smarcel	struct syscall *sc;
79106716Smarcel	const char *name;
80106716Smarcel	int number;
81106716Smarcel	unsigned long *args;
82106716Smarcel	int nargs;	/* number of arguments -- *not* number of words! */
83106716Smarcel	char **s_args;	/* the printable arguments */
84241162Szont};
85106716Smarcel
86241162Szontstatic struct freebsd_syscall *
87241162Szontalloc_fsc(void)
88241162Szont{
89241162Szont
90241162Szont	return (malloc(sizeof(struct freebsd_syscall)));
91241162Szont}
92241162Szont
93106716Smarcel/* Clear up and free parts of the fsc structure. */
94241162Szontstatic void
95241162Szontfree_fsc(struct freebsd_syscall *fsc)
96241162Szont{
97241162Szont	int i;
98241162Szont
99241162Szont	free(fsc->args);
100241162Szont	if (fsc->s_args) {
101241162Szont		for (i = 0; i < fsc->nargs; i++)
102241162Szont			free(fsc->s_args[i]);
103241162Szont		free(fsc->s_args);
104241162Szont	}
105241162Szont	free(fsc);
106106716Smarcel}
107106716Smarcel
108106716Smarcel/*
109106716Smarcel * Called when a process has entered a system call.  nargs is the
110106716Smarcel * number of words, not number of arguments (a necessary distinction
111106716Smarcel * in some cases).  Note that if the STOPEVENT() code in ia64/ia64/trap.c
112106716Smarcel * is ever changed these functions need to keep up.
113106716Smarcel */
114106716Smarcel
115106716Smarcelvoid
116241162Szontia64_syscall_entry(struct trussinfo *trussinfo, int nargs)
117241162Szont{
118241162Szont	struct reg regs;
119241162Szont	struct freebsd_syscall *fsc;
120241162Szont	struct syscall *sc;
121241162Szont	unsigned long *parm_offset;
122241162Szont	lwpid_t tid;
123241162Szont	int i, syscall_num;
124106716Smarcel
125241162Szont	tid = trussinfo->curthread->tid;
126106716Smarcel
127241162Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
128241162Szont		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
129241162Szont		return;
130241162Szont	}
131241162Szont	parm_offset = &regs.r_scratch.gr16;
132106716Smarcel
133241162Szont	/*
134241162Szont	 * FreeBSD has two special kinds of system call redirctions --
135241162Szont	 * SYS_syscall, and SYS___syscall.  The former is the old syscall()
136241162Szont	 * routine, basically; the latter is for quad-aligned arguments.
137241162Szont	 */
138241162Szont	syscall_num = regs.r_scratch.gr15;	/* XXX double-check. */
139241162Szont	if (syscall_num == SYS_syscall || syscall_num == SYS___syscall)
140241162Szont		syscall_num = (int)*parm_offset++;
141106716Smarcel
142241162Szont	fsc = alloc_fsc();
143241162Szont	if (fsc == NULL)
144241162Szont		return;
145241162Szont	fsc->number = syscall_num;
146241162Szont	fsc->name = (syscall_num < 0 || syscall_num >= nsyscalls) ?
147241162Szont	    NULL : syscallnames[syscall_num];
148241162Szont	if (!fsc->name) {
149241162Szont		fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n",
150241162Szont		    syscall_num);
151241162Szont	}
152106716Smarcel
153241162Szont	if (fsc->name && (trussinfo->flags & FOLLOWFORKS) &&
154241162Szont	    (strcmp(fsc->name, "fork") == 0 ||
155241162Szont	    strcmp(fsc->name, "rfork") == 0 ||
156241162Szont	    strcmp(fsc->name, "vfork") == 0))
157241162Szont		trussinfo->curthread->in_fork = 1;
158106716Smarcel
159241162Szont	if (nargs == 0)
160241162Szont		return;
161106716Smarcel
162241162Szont	fsc->args = malloc((1 + nargs) * sizeof(unsigned long));
163241162Szont	memcpy(fsc->args, parm_offset, nargs * sizeof(long));
164106716Smarcel
165241162Szont	sc = get_syscall(fsc->name);
166241162Szont	if (sc)
167241162Szont		fsc->nargs = sc->nargs;
168241162Szont	else {
169106716Smarcel#if DEBUG
170241162Szont		fprintf(trussinfo->outfile, "unknown syscall %s -- setting "
171241162Szont		    "args to %d\n", fsc->name, nargs);
172106716Smarcel#endif
173241162Szont		fsc->nargs = nargs;
174241162Szont	}
175106716Smarcel
176241162Szont	fsc->s_args = calloc(1, (1 + fsc->nargs) * sizeof(char *));
177241162Szont	fsc->sc = sc;
178106716Smarcel
179241162Szont	/*
180241162Szont	 * At this point, we set up the system call arguments.
181241162Szont	 * We ignore any OUT ones, however -- those are arguments that
182241162Szont	 * are set by the system call, and so are probably meaningless
183241162Szont	 * now.	This doesn't currently support arguments that are
184241162Szont	 * passed in *and* out, however.
185241162Szont	 */
186106716Smarcel
187241162Szont	if (fsc->name) {
188106716Smarcel#if DEBUG
189241162Szont		fprintf(stderr, "syscall %s(", fsc->name);
190106716Smarcel#endif
191241162Szont		for (i = 0; i < fsc->nargs; i++) {
192106716Smarcel#if DEBUG
193241162Szont			fprintf(stderr, "0x%x%s", sc ?
194241162Szont			    fsc->args[sc->args[i].offset] : fsc->args[i],
195241162Szont			    i < (fsc->nargs - 1) ? "," : "");
196106716Smarcel#endif
197241162Szont			if (sc && !(sc->args[i].type & OUT)) {
198241162Szont				fsc->s_args[i] = print_arg(&sc->args[i],
199241162Szont				    fsc->args, 0, trussinfo);
200241162Szont			}
201241162Szont		}
202106716Smarcel#if DEBUG
203241162Szont		fprintf(stderr, ")\n");
204106716Smarcel#endif
205241162Szont	}
206106716Smarcel
207106716Smarcel#if DEBUG
208241162Szont	fprintf(trussinfo->outfile, "\n");
209106716Smarcel#endif
210106716Smarcel
211241162Szont	if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 ||
212241162Szont	    strcmp(fsc->name, "exit") == 0)) {
213241162Szont		/*
214241162Szont		 * XXX
215241162Szont		 * This could be done in a more general
216241162Szont		 * manner but it still wouldn't be very pretty.
217241162Szont		 */
218241162Szont		if (strcmp(fsc->name, "execve") == 0) {
219241162Szont			if ((trussinfo->flags & EXECVEARGS) == 0) {
220241162Szont				if (fsc->s_args[1]) {
221241162Szont					free(fsc->s_args[1]);
222241162Szont					fsc->s_args[1] = NULL;
223241162Szont				}
224241162Szont			}
225241162Szont			if ((trussinfo->flags & EXECVEENVS) == 0) {
226241162Szont				if (fsc->s_args[2]) {
227241162Szont					free(fsc->s_args[2]);
228241162Szont					fsc->s_args[2] = NULL;
229241162Szont				}
230241162Szont			}
231241162Szont		}
232241162Szont	}
233241162Szont	trussinfo->curthread->fsc = fsc;
234106716Smarcel}
235106716Smarcel
236106716Smarcel/*
237106716Smarcel * And when the system call is done, we handle it here.
238106716Smarcel * Currently, no attempt is made to ensure that the system calls
239106716Smarcel * match -- this needs to be fixed (and is, in fact, why S_SCX includes
240213799Sbcr * the system call number instead of, say, an error status).
241106716Smarcel */
242106716Smarcel
243122348Smarcellong
244122348Smarcelia64_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused)
245122348Smarcel{
246241162Szont	struct reg regs;
247241162Szont	struct freebsd_syscall *fsc;
248241162Szont	struct syscall *sc;
249241162Szont	lwpid_t tid;
250241162Szont	long retval;
251241162Szont	int errorp, i;
252106716Smarcel
253241162Szont	if (trussinfo->curthread->fsc == NULL)
254241162Szont		return (-1);
255106716Smarcel
256241162Szont	tid = trussinfo->curthread->tid;
257106716Smarcel
258241162Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
259241162Szont		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
260241162Szont		return (-1);
261241162Szont	}
262106716Smarcel
263241162Szont	retval = regs.r_scratch.gr8;
264241162Szont	errorp = (regs.r_scratch.gr10 != 0) ? 1 : 0;
265241162Szont
266106716Smarcel	/*
267241162Szont	 * This code, while simpler than the initial versions I used, could
268241162Szont	 * stand some significant cleaning.
269106716Smarcel	 */
270106716Smarcel
271241162Szont	fsc = trussinfo->curthread->fsc;
272241162Szont	sc = fsc->sc;
273241162Szont	if (!sc) {
274241162Szont		for (i = 0; i < fsc->nargs; i++)
275241162Szont			asprintf(&fsc->s_args[i], "0x%lx", fsc->args[i]);
276241162Szont	} else {
277241162Szont		/*
278241162Szont		 * Here, we only look for arguments that have OUT masked in --
279241162Szont		 * otherwise, they were handled in the syscall_entry function.
280241162Szont		 */
281241162Szont		for (i = 0; i < sc->nargs; i++) {
282241162Szont			char *temp;
283241162Szont			if (sc->args[i].type & OUT) {
284241162Szont				/*
285241162Szont				 * If an error occurred, then don't bother
286241162Szont				 * getting the data; it may not be valid.
287241162Szont				 */
288241162Szont				if (errorp) {
289241162Szont					asprintf(&temp, "0x%lx",
290241162Szont					    fsc->args[sc->args[i].offset]);
291241162Szont				} else {
292241162Szont					temp = print_arg(&sc->args[i],
293241162Szont					    fsc->args, retval, trussinfo);
294241162Szont				}
295241162Szont				fsc->s_args[i] = temp;
296241162Szont			}
297241162Szont		}
298241162Szont	}
299106716Smarcel
300241162Szont	if (fsc->name != NULL && (strcmp(fsc->name, "execve") == 0 ||
301241162Szont	    strcmp(fsc->name, "exit") == 0))
302241162Szont		trussinfo->curthread->in_syscall = 1;
303106716Smarcel
304241162Szont	/*
305241162Szont	 * It would probably be a good idea to merge the error handling,
306241162Szont	 * but that complicates things considerably.
307241162Szont	 */
308241162Szont
309241162Szont	print_syscall_ret(trussinfo, fsc->name, fsc->nargs, fsc->s_args, errorp,
310241162Szont	    retval, fsc->sc);
311241162Szont	free_fsc(fsc);
312241162Szont
313241162Szont	return (retval);
314106716Smarcel}
315