i386-linux.c revision 86501
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
33static const char rcsid[] =
34  "$FreeBSD: head/usr.bin/truss/i386-linux.c 86501 2001-11-17 17:18:36Z des $";
35#endif /* not lint */
36
37/*
38 * Linux/i386-specific system call handling.  Given how much of this code
39 * is taken from the freebsd equivalent, I can probably put even more of
40 * it in support routines that can be used by any personality support.
41 */
42
43#include <sys/types.h>
44#include <sys/ioctl.h>
45#include <sys/pioctl.h>
46
47#include <machine/reg.h>
48#include <machine/psl.h>
49
50#include <errno.h>
51#include <fcntl.h>
52#include <signal.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57
58#include "syscall.h"
59
60static int fd = -1;
61static int cpid = -1;
62extern int Procfd;
63
64extern FILE *outfile;
65#include "linux_syscalls.h"
66
67static int nsyscalls =
68	sizeof(linux_syscallnames) / sizeof(linux_syscallnames[0]);
69
70/* See the comment in i386-fbsd.c about this structure. */
71static struct linux_syscall {
72	struct syscall *sc;
73	char *name;
74	int number;
75	unsigned long args[5];
76	int nargs;	/* number of arguments -- *not* number of words! */
77	char **s_args;	/* the printable arguments */
78} lsc;
79
80static inline void
81clear_lsc() {
82  if (lsc.s_args) {
83    int i;
84    for (i = 0; i < lsc.nargs; i++)
85      if (lsc.s_args[i])
86	free(lsc.s_args[i]);
87    free(lsc.s_args);
88  }
89  memset(&lsc, 0, sizeof(lsc));
90}
91
92void
93i386_linux_syscall_entry(int pid, int nargs) {
94  char buf[32];
95  struct reg regs = { 0 };
96  int syscall;
97  int i;
98  struct syscall *sc;
99
100  if (fd == -1 || pid != cpid) {
101    sprintf(buf, "/proc/%d/regs", pid);
102    fd = open(buf, O_RDWR);
103    if (fd == -1) {
104      fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
105      return;
106    }
107    cpid = pid;
108  }
109
110  clear_lsc();
111  lseek(fd, 0L, 0);
112  i = read(fd, &regs, sizeof(regs));
113  syscall = regs.r_eax;
114
115  lsc.number = syscall;
116  lsc.name =
117    (syscall < 0 || syscall > nsyscalls) ? NULL : linux_syscallnames[syscall];
118  if (!lsc.name) {
119    fprintf (outfile, "-- UNKNOWN SYSCALL %d\n", syscall);
120  }
121
122  if (nargs == 0)
123    return;
124
125  /*
126   * Linux passes syscall arguments in registers, not
127   * on the stack.  Fortunately, we've got access to the
128   * register set.  Note that we don't bother checking the
129   * number of arguments.  And what does linux do for syscalls
130   * that have more than five arguments?
131   */
132
133  lsc.args[0] = regs.r_ebx;
134  lsc.args[1] = regs.r_ecx;
135  lsc.args[2] = regs.r_edx;
136  lsc.args[3] = regs.r_esi;
137  lsc.args[4] = regs.r_edi;
138
139  sc = get_syscall(lsc.name);
140  if (sc) {
141    lsc.nargs = sc->nargs;
142  } else {
143#ifdef DEBUG
144    fprintf(outfile, "unknown syscall %s -- setting args to %d\n",
145	    lsc.name, nargs);
146#endif
147    lsc.nargs = nargs;
148  }
149
150  lsc.s_args = malloc((1+lsc.nargs) * sizeof(char*));
151  memset(lsc.s_args, 0, lsc.nargs * sizeof(char*));
152  lsc.sc = sc;
153
154  if (lsc.name) {
155
156#ifdef DEBUG
157    fprintf(stderr, "syscall %s(", lsc.name);
158#endif
159    for (i = 0; i < lsc.nargs ; i++) {
160#ifdef DEBUG
161      fprintf(stderr, "0x%x%s",
162	      sc ?
163	      lsc.args[sc->args[i].offset]
164	      : lsc.args[i],
165	      i < (lsc.nargs - 1) ? "," : "");
166#endif
167      if (sc && !(sc->args[i].type & OUT)) {
168	lsc.s_args[i] = print_arg(Procfd, &sc->args[i], lsc.args);
169      }
170    }
171#ifdef DEBUG
172    fprintf(stderr, ")\n");
173#endif
174  }
175
176  if (!strcmp(lsc.name, "linux_execve") || !strcmp(lsc.name, "exit")) {
177    print_syscall(outfile, lsc.name, lsc.nargs, lsc.s_args);
178  }
179
180  return;
181}
182
183/*
184 * Linux syscalls return negative errno's, we do positive and map them
185 */
186int bsd_to_linux_errno[] = {
187  	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
188 	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
189 	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
190 	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
191 	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
192	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
193	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
194	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
195  	-6,
196};
197
198void
199i386_linux_syscall_exit(int pid, int syscall) {
200  char buf[32];
201  struct reg regs;
202  int retval;
203  int i;
204  int errorp;
205  struct syscall *sc;
206
207  if (fd == -1 || pid != cpid) {
208    sprintf(buf, "/proc/%d/regs", pid);
209    fd = open(buf, O_RDONLY);
210    if (fd == -1) {
211      fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
212      return;
213    }
214    cpid = pid;
215  }
216
217  lseek(fd, 0L, 0);
218  if (read(fd, &regs, sizeof(regs)) != sizeof(regs))
219    return;
220
221  retval = regs.r_eax;
222  errorp = !!(regs.r_eflags & PSL_C);
223
224  sc = lsc.sc;
225  if (!sc) {
226    for (i = 0; i < lsc.nargs; i++) {
227      lsc.s_args[i] = malloc(12);
228      sprintf(lsc.s_args[i], "0x%lx", lsc.args[i]);
229    }
230  } else {
231    for (i = 0; i < sc->nargs; i++) {
232      char *temp;
233      if (sc->args[i].type & OUT) {
234	if (errorp) {
235	  temp = malloc(12);
236	  sprintf(temp, "0x%lx", lsc.args[sc->args[i].offset]);
237	} else {
238	  temp = print_arg(Procfd, &sc->args[i], lsc.args);
239	}
240	lsc.s_args[i] = temp;
241      }
242    }
243  }
244  if (errorp) {
245    for (i = 0; i < sizeof(bsd_to_linux_errno) / sizeof(int); i++)
246      if (retval == bsd_to_linux_errno[i])
247      break;
248  }
249  print_syscall_ret(outfile, lsc.name, lsc.nargs, lsc.s_args, errorp,
250    errorp ? i : retval);
251  clear_lsc();
252  return;
253}
254