Add two tunables to run netisr and udp_thread without mplock, so experiment
[dragonfly.git] / usr.bin / truss / i386-fbsd.c
blob89bedd0c92f93031c29d313826c4490af1e36b13
1 /*
2 * Copryight 1997 Sean Eric Fagan
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.
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.
31 * $FreeBSD: src/usr.bin/truss/i386-fbsd.c,v 1.7.2.2 2001/10/29 20:12:56 des Exp $
32 * $DragonFly: src/usr.bin/truss/i386-fbsd.c,v 1.4 2007/12/27 00:59:25 nth Exp $
36 * FreeBSD/386-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.
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
45 #include <sys/pioctl.h>
46 #include <sys/syscall.h>
48 #include <machine/reg.h>
49 #include <machine/psl.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
59 #include "extern.h"
60 #include "syscall.h"
62 static int fd = -1;
63 static int cpid = -1;
64 extern int Procfd;
66 extern FILE *outfile;
67 #include "syscalls.h"
69 static int nsyscalls = sizeof(syscallnames) / sizeof(syscallnames[0]);
72 * This is what this particular file uses to keep track of a system call.
73 * It is probably not quite sufficient -- I can probably use the same
74 * structure for the various syscall personalities, and I also probably
75 * need to nest system calls (for signal handlers).
77 * 'struct syscall' describes the system call; it may be NULL, however,
78 * if we don't know about this particular system call yet.
80 static struct freebsd_syscall {
81 struct syscall *sc;
82 char *name;
83 int number;
84 unsigned long *args;
85 int nargs; /* number of arguments -- *not* number of words! */
86 char **s_args; /* the printable arguments */
87 } fsc;
89 /* Clear up and free parts of the fsc structure. */
90 static inline void
91 clear_fsc() {
92 if (fsc.args) {
93 free(fsc.args);
95 if (fsc.s_args) {
96 int i;
97 for (i = 0; i < fsc.nargs; i++)
98 if (fsc.s_args[i])
99 free(fsc.s_args[i]);
100 free(fsc.s_args);
102 memset(&fsc, 0, sizeof(fsc));
106 * Called when a process has entered a system call. nargs is the
107 * number of words, not number of arguments (a necessary distinction
108 * in some cases). Note that if the STOPEVENT() code in i386/i386/trap.c
109 * is ever changed these functions need to keep up.
112 void
113 i386_syscall_entry(int pid, int nargs) {
114 char *buf;
115 struct reg regs = { 0 };
116 int syscall;
117 int i;
118 unsigned int parm_offset;
119 struct syscall *sc;
121 if (fd == -1 || pid != cpid) {
122 asprintf(&buf, "%s/%d/regs", procfs_path, pid);
123 if (buf == NULL)
124 err(1, "Out of memory");
125 fd = open(buf, O_RDWR);
126 free(buf);
127 if (fd == -1) {
128 fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
129 return;
131 cpid = pid;
134 clear_fsc();
135 lseek(fd, 0L, 0);
136 i = read(fd, &regs, sizeof(regs));
137 parm_offset = regs.r_esp + sizeof(int);
140 * FreeBSD has two special kinds of system call redirctions --
141 * SYS_syscall, and SYS___syscall. The former is the old syscall()
142 * routine, basicly; the latter is for quad-aligned arguments.
144 syscall = regs.r_eax;
145 switch (syscall) {
146 case SYS_syscall:
147 lseek(Procfd, parm_offset, SEEK_SET);
148 read(Procfd, &syscall, sizeof(int));
149 parm_offset += sizeof(int);
150 break;
151 case SYS___syscall:
152 lseek(Procfd, parm_offset, SEEK_SET);
153 read(Procfd, &syscall, sizeof(int));
154 parm_offset += sizeof(quad_t);
155 break;
158 fsc.number = syscall;
159 fsc.name =
160 (syscall < 0 || syscall >= nsyscalls) ? NULL : syscallnames[syscall];
161 if (!fsc.name) {
162 fprintf(outfile, "-- UNKNOWN SYSCALL %d --\n", syscall);
165 if (nargs == 0)
166 return;
168 fsc.args = malloc((1+nargs) * sizeof(unsigned long));
169 lseek(Procfd, parm_offset, SEEK_SET);
170 if (read(Procfd, fsc.args, nargs * sizeof(unsigned long)) == -1)
171 return;
173 sc = get_syscall(fsc.name);
174 if (sc) {
175 fsc.nargs = sc->nargs;
176 } else {
177 #if DEBUG
178 fprintf(outfile, "unknown syscall %s -- setting args to %d\n",
179 fsc.name, nargs);
180 #endif
181 fsc.nargs = nargs;
184 fsc.s_args = malloc((1+fsc.nargs) * sizeof(char*));
185 memset(fsc.s_args, 0, fsc.nargs * sizeof(char*));
186 fsc.sc = sc;
189 * At this point, we set up the system call arguments.
190 * We ignore any OUT ones, however -- those are arguments that
191 * are set by the system call, and so are probably meaningless
192 * now. This doesn't currently support arguments that are
193 * passed in *and* out, however.
196 if (fsc.name) {
198 #if DEBUG
199 fprintf(stderr, "syscall %s(", fsc.name);
200 #endif
201 for (i = 0; i < fsc.nargs; i++) {
202 #if DEBUG
203 fprintf(stderr, "0x%x%s",
205 ? fsc.args[sc->args[i].offset]
206 : fsc.args[i],
207 i < (fsc.nargs -1) ? "," : "");
208 #endif
209 if (sc && !(sc->args[i].type & OUT)) {
210 fsc.s_args[i] = print_arg(Procfd, &sc->args[i], fsc.args);
213 #if DEBUG
214 fprintf(stderr, ")\n");
215 #endif
218 #if DEBUG
219 fprintf(outfile, "\n");
220 #endif
223 * Some system calls should be printed out before they are done --
224 * execve() and exit(), for example, never return. Possibly change
225 * this to work for any system call that doesn't have an OUT
226 * parameter?
229 if (!strcmp(fsc.name, "execve") || !strcmp(fsc.name, "exit")) {
230 print_syscall(outfile, fsc.name, fsc.nargs, fsc.s_args);
233 return;
237 * And when the system call is done, we handle it here.
238 * Currently, no attempt is made to ensure that the system calls
239 * match -- this needs to be fixed (and is, in fact, why S_SCX includes
240 * the sytem call number instead of, say, an error status).
243 void
244 i386_syscall_exit(int pid, int syscall) {
245 char *buf;
246 struct reg regs;
247 int retval;
248 int i;
249 int errorp;
250 struct syscall *sc;
252 if (fd == -1 || pid != cpid) {
253 asprintf(&buf, "%s/%d/regs", procfs_path, pid);
254 if (buf == NULL)
255 err(1, "Out of memory");
256 fd = open(buf, O_RDONLY);
257 free(buf);
258 if (fd == -1) {
259 fprintf(outfile, "-- CANNOT READ REGISTERS --\n");
260 return;
262 cpid = pid;
265 lseek(fd, 0L, 0);
266 if (read(fd, &regs, sizeof(regs)) != sizeof(regs))
267 return;
268 retval = regs.r_eax;
269 errorp = !!(regs.r_eflags & PSL_C);
272 * This code, while simpler than the initial versions I used, could
273 * stand some significant cleaning.
276 sc = fsc.sc;
277 if (!sc) {
278 for (i = 0; i < fsc.nargs; i++) {
279 fsc.s_args[i] = malloc(12);
280 sprintf(fsc.s_args[i], "0x%lx", fsc.args[i]);
282 } else {
284 * Here, we only look for arguments that have OUT masked in --
285 * otherwise, they were handled in the syscall_entry function.
287 for (i = 0; i < sc->nargs; i++) {
288 char *temp;
289 if (sc->args[i].type & OUT) {
291 * If an error occurred, than don't bothe getting the data;
292 * it may not be valid.
294 if (errorp) {
295 temp = malloc(12);
296 sprintf(temp, "0x%lx", fsc.args[sc->args[i].offset]);
297 } else {
298 temp = print_arg(Procfd, &sc->args[i], fsc.args);
300 fsc.s_args[i] = temp;
306 * It would probably be a good idea to merge the error handling,
307 * but that complicates things considerably.
310 print_syscall_ret(outfile, fsc.name, fsc.nargs, fsc.s_args, errorp, retval);
311 clear_fsc();
313 return;