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
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
33 static const char rcsid[] =
34 "$Id: main.c,v 1.12 1998/10/03 00:43:05 sef Exp $";
38 * The main module for truss. Suprisingly simple, but, then, the other
39 * files handle the bulk of the work. And, of course, the kernel has to
40 * do a lot of the work :).
51 #include <sys/ioctl.h>
52 #include <sys/pioctl.h>
54 extern int setup_and_wait(char **);
55 extern int start_tracing(int, int);
57 extern void alpha_syscall_entry(int, int);
58 extern void alpha_syscall_exit(int, int);
61 extern void i386_syscall_entry(int, int);
62 extern void i386_syscall_exit(int, int);
63 extern void i386_linux_syscall_entry(int, int);
64 extern void i386_linux_syscall_exit(int, int);
68 * These should really be parameterized -- I don't like having globals,
69 * but this is the easiest way, right now, to deal with them.
74 FILE *outfile = stderr;
76 char progtype[50]; /* OS and type of executable */
81 fprintf(stderr, "%s\n%s\n",
82 "usage: truss [-S] [-o file] -p pid",
83 " truss [-S] [-o file] command [args]");
88 * WARNING! "FreeBSD a.out" must be first, or set_etype will not
93 void (*enter_syscall)(int, int);
94 void (*exit_syscall)(int, int);
97 { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
100 { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
101 { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
102 { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
108 * Set the execution type. This is called after every exec, and when
109 * a process is first monitored. The procfs pseudo-file "etype" has
110 * the execution module type -- see /proc/curproc/etype for an example.
113 static struct ex_types *
115 struct ex_types *funcs;
120 sprintf(etype, "/proc/%d/etype", pid);
121 if ((fd = open(etype, O_RDONLY)) == -1) {
122 strcpy(progtype, "FreeBSD a.out");
124 int len = read(fd, progtype, sizeof(progtype));
125 progtype[len-1] = '\0';
129 for (funcs = ex_types; funcs->type; funcs++)
130 if (!strcmp(funcs->type, progtype))
134 warn("Execution type %s is not supported -- using FreeBSD a.out\n",
136 funcs = &ex_types[0];
142 main(int ac, char **av) {
146 struct procfs_status pfs;
147 struct ex_types *funcs;
152 while ((c = getopt(ac, av, "p:o:S")) != -1) {
154 case 'p': /* specified pid */
157 case 'o': /* Specified output file */
160 case 'S': /* Don't trace signals */
168 ac -= optind; av += optind;
169 if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0))
172 if (fname != NULL) { /* Use output file */
173 if ((outfile = fopen(fname, "w")) == NULL)
174 errx(1, "cannot open %s", fname);
178 * If truss starts the process itself, it will ignore some signals --
179 * they should be passed off to the process, which may or may not
180 * exit. If, however, we are examining an already-running process,
181 * then we restore the event mask on these same signals.
184 if (pid == 0) { /* Start a command ourselves */
186 pid = setup_and_wait(command);
187 signal(SIGINT, SIG_IGN);
188 signal(SIGTERM, SIG_IGN);
189 signal(SIGQUIT, SIG_IGN);
191 extern void restore_proc(int);
192 signal(SIGINT, restore_proc);
193 signal(SIGTERM, restore_proc);
194 signal(SIGQUIT, restore_proc);
199 * At this point, if we started the process, it is stopped waiting to
200 * be woken up, either in exit() or in execve().
203 Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
204 (nosigs ? 0 : S_SIG));
209 * At this point, it's a simple loop, waiting for the process to
210 * stop, finding out why, printing out why, and then continuing it.
211 * All of the grunt work is done in the support routines.
217 if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
218 warn("PIOCWAIT top of loop");
220 switch(i = pfs.why) {
222 funcs->enter_syscall(pid, pfs.val);
226 * This is so we don't get two messages for an exec -- one
227 * for the S_EXEC, and one for the syscall exit. It also,
228 * conveniently, ensures that the first message printed out
229 * isn't the return-from-syscall used to create the process.
236 funcs->exit_syscall(pid, pfs.val);
239 fprintf(outfile, "SIGNAL %lu\n", pfs.val);
243 fprintf (outfile, "process exit, rval = %lu\n", pfs.val);
250 fprintf (outfile, "Process stopped because of: %d\n", i);
254 if (ioctl(Procfd, PIOCCONT, val) == -1)
256 } while (pfs.why != S_EXIT);
259 if (sigexit == SIGQUIT)
261 (void) signal(sigexit, SIG_DFL);
262 (void) kill(getpid(), sigexit);