powerpc64-freebsd32.c revision 154047
1154047Sgrehan/*
2154047Sgrehan * Copyright 2006 Peter Grehan <grehan@freebsd.org>
3154047Sgrehan * Copryight 2005 Orlando Bassotto <orlando@break.net>
4154047Sgrehan * Copryight 1998 Sean Eric Fagan
5154047Sgrehan *
6154047Sgrehan * Redistribution and use in source and binary forms, with or without
7154047Sgrehan * modification, are permitted provided that the following conditions
8154047Sgrehan * are met:
9154047Sgrehan * 1. Redistributions of source code must retain the above copyright
10154047Sgrehan *    notice, this list of conditions and the following disclaimer.
11154047Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
12154047Sgrehan *    notice, this list of conditions and the following disclaimer in the
13154047Sgrehan *    documentation and/or other materials provided with the distribution.
14154047Sgrehan *
15154047Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16154047Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17154047Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18154047Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19154047Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20154047Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21154047Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22154047Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23154047Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24154047Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25154047Sgrehan * SUCH DAMAGE.
26154047Sgrehan */
27154047Sgrehan
28154047Sgrehan#ifndef lint
29154047Sgrehanstatic const char rcsid[] =
30154047Sgrehan  "$FreeBSD: head/usr.bin/truss/powerpc-fbsd.c 154047 2006-01-05 05:57:47Z grehan $";
31154047Sgrehan#endif /* not lint */
32154047Sgrehan
33154047Sgrehan/*
34154047Sgrehan * FreeBSD/powerpc-specific system call handling.  This is probably the most
35154047Sgrehan * complex part of the entire truss program, although I've got lots of
36154047Sgrehan * it handled relatively cleanly now.  The system call names are generated
37154047Sgrehan * automatically, thanks to /usr/src/sys/kern/syscalls.master.  The
38154047Sgrehan * names used for the various structures are confusing, I sadly admit.
39154047Sgrehan *
40154047Sgrehan * This file is almost nothing more than a slightly-edited i386-fbsd.c.
41154047Sgrehan */
42154047Sgrehan
43154047Sgrehan#include <sys/types.h>
44154047Sgrehan#include <sys/ioctl.h>
45154047Sgrehan#include <sys/pioctl.h>
46154047Sgrehan#include <sys/syscall.h>
47154047Sgrehan
48154047Sgrehan#include <machine/reg.h>
49154047Sgrehan#include <machine/frame.h>
50154047Sgrehan
51154047Sgrehan#include <err.h>
52154047Sgrehan#include <errno.h>
53154047Sgrehan#include <fcntl.h>
54154047Sgrehan#include <signal.h>
55154047Sgrehan#include <stdio.h>
56154047Sgrehan#include <stdlib.h>
57154047Sgrehan#include <string.h>
58154047Sgrehan#include <time.h>
59154047Sgrehan#include <unistd.h>
60154047Sgrehan
61154047Sgrehan#include "truss.h"
62154047Sgrehan#include "syscall.h"
63154047Sgrehan#include "extern.h"
64154047Sgrehan
65154047Sgrehanstatic int fd = -1;
66154047Sgrehanstatic int cpid = -1;
67154047Sgrehan
68154047Sgrehan#include "syscalls.h"
69154047Sgrehan
70154047Sgrehanstatic int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]);
71154047Sgrehan
72154047Sgrehan/*
73154047Sgrehan * This is what this particular file uses to keep track of a system call.
74154047Sgrehan * It is probably not quite sufficient -- I can probably use the same
75154047Sgrehan * structure for the various syscall personalities, and I also probably
76154047Sgrehan * need to nest system calls (for signal handlers).
77154047Sgrehan *
78154047Sgrehan * 'struct syscall' describes the system call; it may be NULL, however,
79154047Sgrehan * if we don't know about this particular system call yet.
80154047Sgrehan */
81154047Sgrehanstatic struct freebsd_syscall {
82154047Sgrehan	struct syscall *sc;
83154047Sgrehan	const char *name;
84154047Sgrehan	int number;
85154047Sgrehan	unsigned long *args;
86154047Sgrehan	int nargs;	/* number of arguments -- *not* number of words! */
87154047Sgrehan	char **s_args;	/* the printable arguments */
88154047Sgrehan} fsc;
89154047Sgrehan
90154047Sgrehan/* Clear up and free parts of the fsc structure. */
91154047Sgrehanstatic __inline void
92154047Sgrehanclear_fsc(void) {
93154047Sgrehan  if (fsc.args) {
94154047Sgrehan    free(fsc.args);
95154047Sgrehan  }
96154047Sgrehan  if (fsc.s_args) {
97154047Sgrehan    int i;
98154047Sgrehan    for (i = 0; i < fsc.nargs; i++)
99154047Sgrehan      if (fsc.s_args[i])
100154047Sgrehan	free(fsc.s_args[i]);
101154047Sgrehan    free(fsc.s_args);
102154047Sgrehan  }
103154047Sgrehan  memset(&fsc, 0, sizeof(fsc));
104154047Sgrehan}
105154047Sgrehan
106154047Sgrehan/*
107154047Sgrehan * Called when a process has entered a system call.  nargs is the
108154047Sgrehan * number of words, not number of arguments (a necessary distinction
109154047Sgrehan * in some cases).  Note that if the STOPEVENT() code in powerpc/powerpc/trap.c
110154047Sgrehan * is ever changed these functions need to keep up.
111154047Sgrehan */
112154047Sgrehan
113154047Sgrehanvoid
114154047Sgrehanpowerpc_syscall_entry(struct trussinfo *trussinfo, int nargs) {
115154047Sgrehan  char buf[32];
116154047Sgrehan  struct reg regs;
117154047Sgrehan  void *args;
118154047Sgrehan  int syscall_num;
119154047Sgrehan  int i;
120154047Sgrehan  unsigned int regargs;
121154047Sgrehan  struct syscall *sc;
122154047Sgrehan
123154047Sgrehan  if (fd == -1 || trussinfo->pid != cpid) {
124154047Sgrehan    sprintf(buf, "/proc/%d/regs", trussinfo->pid);
125154047Sgrehan    fd = open(buf, O_RDWR);
126154047Sgrehan    if (fd == -1) {
127154047Sgrehan      fprintf(trussinfo->outfile, "-- CANNOT OPEN REGISTERS --\n");
128154047Sgrehan      return;
129154047Sgrehan    }
130154047Sgrehan    cpid = trussinfo->pid;
131154047Sgrehan  }
132154047Sgrehan
133154047Sgrehan  clear_fsc();
134154047Sgrehan  lseek(fd, 0L, 0);
135154047Sgrehan  if (read(fd, &regs, sizeof(regs)) != sizeof(regs)) {
136154047Sgrehan    fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
137154047Sgrehan    return;
138154047Sgrehan  }
139154047Sgrehan
140154047Sgrehan  /*
141154047Sgrehan   * FreeBSD has two special kinds of system call redirctions --
142154047Sgrehan   * SYS_syscall, and SYS___syscall.  The former is the old syscall()
143154047Sgrehan   * routine, basicly; the latter is for quad-aligned arguments.
144154047Sgrehan   */
145154047Sgrehan  regargs = NARGREG;
146154047Sgrehan  syscall_num = regs.fixreg[0];
147154047Sgrehan  args = &regs.fixreg[3];
148154047Sgrehan  if (syscall_num == SYS_syscall) {
149154047Sgrehan    args = &regs.fixreg[4];
150154047Sgrehan    regargs -= 1;
151154047Sgrehan    syscall_num = regs.fixreg[3];
152154047Sgrehan  } else if (syscall_num == SYS___syscall) {
153154047Sgrehan    args = &regs.fixreg[5];
154154047Sgrehan    regargs -= 2;
155154047Sgrehan    syscall_num = regs.fixreg[4];
156154047Sgrehan  }
157154047Sgrehan
158154047Sgrehan  fsc.number = syscall_num;
159154047Sgrehan  fsc.name =
160154047Sgrehan    (syscall_num < 0 || syscall_num > nsyscalls) ? NULL : syscallnames[syscall_num];
161154047Sgrehan  if (!fsc.name) {
162154047Sgrehan    fprintf(trussinfo->outfile, "-- UNKNOWN SYSCALL %d --\n", syscall_num);
163154047Sgrehan  }
164154047Sgrehan
165154047Sgrehan  if (fsc.name && (trussinfo->flags & FOLLOWFORKS)
166154047Sgrehan   && ((!strcmp(fsc.name, "fork")
167154047Sgrehan    || !strcmp(fsc.name, "rfork")
168154047Sgrehan    || !strcmp(fsc.name, "vfork"))))
169154047Sgrehan  {
170154047Sgrehan    trussinfo->in_fork = 1;
171154047Sgrehan  }
172154047Sgrehan
173154047Sgrehan  if (nargs == 0)
174154047Sgrehan    return;
175154047Sgrehan
176154047Sgrehan  fsc.args = malloc((1+nargs) * sizeof(unsigned long));
177154047Sgrehan
178154047Sgrehan  if (nargs > regargs) {
179154047Sgrehan    memmove(&fsc.args[0], args, regargs * sizeof(fsc.args[0]));
180154047Sgrehan    lseek(Procfd, regs.fixreg[1] + 8, SEEK_SET);
181154047Sgrehan    read(Procfd, &fsc.args[regargs], (nargs - regargs) * sizeof(fsc.args[0]));
182154047Sgrehan  } else {
183154047Sgrehan    memmove(&fsc.args[0], args, nargs * sizeof(fsc.args[0]));
184154047Sgrehan  }
185154047Sgrehan
186154047Sgrehan  sc = get_syscall(fsc.name);
187154047Sgrehan  if (sc) {
188154047Sgrehan    fsc.nargs = sc->nargs;
189154047Sgrehan  } else {
190154047Sgrehan#if DEBUG
191154047Sgrehan    fprintf(trussinfo->outfile, "unknown syscall %s -- setting args to %d\n",
192154047Sgrehan	   fsc.name, nargs);
193154047Sgrehan#endif
194154047Sgrehan    fsc.nargs = nargs;
195154047Sgrehan  }
196154047Sgrehan
197154047Sgrehan  fsc.s_args = malloc((1+fsc.nargs) * sizeof(char*));
198154047Sgrehan  memset(fsc.s_args, 0, fsc.nargs * sizeof(char*));
199154047Sgrehan  fsc.sc = sc;
200154047Sgrehan
201154047Sgrehan  /*
202154047Sgrehan   * At this point, we set up the system call arguments.
203154047Sgrehan   * We ignore any OUT ones, however -- those are arguments that
204154047Sgrehan   * are set by the system call, and so are probably meaningless
205154047Sgrehan   * now.  This doesn't currently support arguments that are
206154047Sgrehan   * passed in *and* out, however.
207154047Sgrehan   */
208154047Sgrehan
209154047Sgrehan  if (fsc.name) {
210154047Sgrehan
211154047Sgrehan#if DEBUG
212154047Sgrehan    fprintf(stderr, "syscall %s(", fsc.name);
213154047Sgrehan#endif
214154047Sgrehan    for (i = 0; i < fsc.nargs; i++) {
215154047Sgrehan#if DEBUG
216154047Sgrehan      fprintf(stderr, "0x%x%s",
217154047Sgrehan	      sc
218154047Sgrehan	      ? fsc.args[sc->args[i].offset]
219154047Sgrehan	      : fsc.args[i],
220154047Sgrehan	      i < (fsc.nargs - 1) ? "," : "");
221154047Sgrehan#endif
222154047Sgrehan      if (sc && !(sc->args[i].type & OUT)) {
223154047Sgrehan	fsc.s_args[i] = print_arg(Procfd, &sc->args[i], fsc.args, 0, trussinfo);
224154047Sgrehan      }
225154047Sgrehan    }
226154047Sgrehan#if DEBUG
227154047Sgrehan    fprintf(stderr, ")\n");
228154047Sgrehan#endif
229154047Sgrehan  }
230154047Sgrehan
231154047Sgrehan#if DEBUG
232154047Sgrehan  fprintf(trussinfo->outfile, "\n");
233154047Sgrehan#endif
234154047Sgrehan
235154047Sgrehan  /*
236154047Sgrehan   * Some system calls should be printed out before they are done --
237154047Sgrehan   * execve() and exit(), for example, never return.  Possibly change
238154047Sgrehan   * this to work for any system call that doesn't have an OUT
239154047Sgrehan   * parameter?
240154047Sgrehan   */
241154047Sgrehan
242154047Sgrehan  if (fsc.name && (!strcmp(fsc.name, "execve") || !strcmp(fsc.name, "exit"))) {
243154047Sgrehan
244154047Sgrehan    /* XXX
245154047Sgrehan     * This could be done in a more general
246154047Sgrehan     * manner but it still wouldn't be very pretty.
247154047Sgrehan     */
248154047Sgrehan    if (!strcmp(fsc.name, "execve")) {
249154047Sgrehan        if ((trussinfo->flags & EXECVEARGS) == 0)
250154047Sgrehan          if (fsc.s_args[1]) {
251154047Sgrehan            free(fsc.s_args[1]);
252154047Sgrehan            fsc.s_args[1] = NULL;
253154047Sgrehan          }
254154047Sgrehan        if ((trussinfo->flags & EXECVEENVS) == 0)
255154047Sgrehan          if (fsc.s_args[2]) {
256154047Sgrehan            free(fsc.s_args[2]);
257154047Sgrehan            fsc.s_args[2] = NULL;
258154047Sgrehan          }
259154047Sgrehan    }
260154047Sgrehan
261154047Sgrehan    print_syscall(trussinfo, fsc.name, fsc.nargs, fsc.s_args);
262154047Sgrehan    fprintf(trussinfo->outfile, "\n");
263154047Sgrehan  }
264154047Sgrehan
265154047Sgrehan  return;
266154047Sgrehan}
267154047Sgrehan
268154047Sgrehan/*
269154047Sgrehan * And when the system call is done, we handle it here.
270154047Sgrehan * Currently, no attempt is made to ensure that the system calls
271154047Sgrehan * match -- this needs to be fixed (and is, in fact, why S_SCX includes
272154047Sgrehan * the sytem call number instead of, say, an error status).
273154047Sgrehan */
274154047Sgrehan
275154047Sgrehanlong
276154047Sgrehanpowerpc_syscall_exit(struct trussinfo *trussinfo, int syscall_num __unused)
277154047Sgrehan{
278154047Sgrehan  char buf[32];
279154047Sgrehan  struct reg regs;
280154047Sgrehan  long retval;
281154047Sgrehan  int i;
282154047Sgrehan  int errorp;
283154047Sgrehan  struct syscall *sc;
284154047Sgrehan
285154047Sgrehan  if (fd == -1 || trussinfo->pid != cpid) {
286154047Sgrehan    sprintf(buf, "/proc/%d/regs", trussinfo->pid);
287154047Sgrehan    fd = open(buf, O_RDONLY);
288154047Sgrehan    if (fd == -1) {
289154047Sgrehan      fprintf(trussinfo->outfile, "-- CANNOT OPEN REGISTERS --\n");
290154047Sgrehan      return (-1);
291154047Sgrehan    }
292154047Sgrehan    cpid = trussinfo->pid;
293154047Sgrehan  }
294154047Sgrehan
295154047Sgrehan  lseek(fd, 0L, 0);
296154047Sgrehan  if (read(fd, &regs, sizeof(regs)) != sizeof(regs)) {
297154047Sgrehan    fprintf(trussinfo->outfile, "\n");
298154047Sgrehan    return (-1);
299154047Sgrehan  }
300154047Sgrehan  retval = regs.fixreg[3];
301154047Sgrehan  errorp = !!(regs.cr & 0x10000000);
302154047Sgrehan
303154047Sgrehan  /*
304154047Sgrehan   * This code, while simpler than the initial versions I used, could
305154047Sgrehan   * stand some significant cleaning.
306154047Sgrehan   */
307154047Sgrehan
308154047Sgrehan  sc = fsc.sc;
309154047Sgrehan  if (!sc) {
310154047Sgrehan    for (i = 0; i < fsc.nargs; i++)
311154047Sgrehan      asprintf(&fsc.s_args[i], "0x%lx", fsc.args[i]);
312154047Sgrehan  } else {
313154047Sgrehan    /*
314154047Sgrehan     * On 32-bit big-endian, the low word of a 64-bit return is
315154047Sgrehan     * in the greater address. Switch to this. XXX note that
316154047Sgrehan     * print_syscall_ret can't handle 64-bit return values (llseek)
317154047Sgrehan     */
318154047Sgrehan    if (sc->ret_type == 2)
319154047Sgrehan      retval = regs.fixreg[4];
320154047Sgrehan
321154047Sgrehan    /*
322154047Sgrehan     * Here, we only look for arguments that have OUT masked in --
323154047Sgrehan     * otherwise, they were handled in the syscall_entry function.
324154047Sgrehan     */
325154047Sgrehan    for (i = 0; i < sc->nargs; i++) {
326154047Sgrehan      char *temp;
327154047Sgrehan      if (sc->args[i].type & OUT) {
328154047Sgrehan	/*
329154047Sgrehan	 * If an error occurred, than don't bothe getting the data;
330154047Sgrehan	 * it may not be valid.
331154047Sgrehan	 */
332154047Sgrehan	if (errorp)
333154047Sgrehan	  asprintf(&temp, "0x%lx", fsc.args[sc->args[i].offset]);
334154047Sgrehan	else
335154047Sgrehan	  temp = print_arg(Procfd, &sc->args[i], fsc.args, retval, trussinfo);
336154047Sgrehan	fsc.s_args[i] = temp;
337154047Sgrehan      }
338154047Sgrehan    }
339154047Sgrehan  }
340154047Sgrehan
341154047Sgrehan  /*
342154047Sgrehan   * It would probably be a good idea to merge the error handling,
343154047Sgrehan   * but that complicates things considerably.
344154047Sgrehan   */
345154047Sgrehan
346154047Sgrehan  print_syscall_ret(trussinfo, fsc.name, fsc.nargs, fsc.s_args, errorp, retval);
347154047Sgrehan  clear_fsc();
348154047Sgrehan
349154047Sgrehan  return (retval);
350154047Sgrehan}
351