2 * Copyright 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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * The main module for truss. Surprisingly simple, but, then, the other
37 * files handle the bulk of the work. And, of course, the kernel has to
38 * do a lot of the work :).
41 #include <sys/ptrace.h>
48 #include <sysdecode.h>
59 fprintf(stderr, "%s\n%s\n",
60 "usage: truss [-cfaedDHS] [-o file] [-s strsize] -p pid",
61 " truss [-cfaedDHS] [-o file] [-s strsize] command [args]");
66 main(int ac, char **av)
69 struct trussinfo *trussinfo;
77 /* Initialize the trussinfo struct */
78 trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo));
79 if (trussinfo == NULL)
80 errx(1, "calloc() failed");
83 trussinfo->outfile = stderr;
84 trussinfo->strsize = 32;
85 trussinfo->curthread = NULL;
86 LIST_INIT(&trussinfo->proclist);
88 while ((c = getopt(ac, av, "p:o:facedDs:SH")) != -1) {
90 case 'p': /* specified pid */
92 /* make sure i don't trace me */
93 if (pid == getpid()) {
94 errx(2, "attempt to grab self.");
97 case 'f': /* Follow fork()'s */
98 trussinfo->flags |= FOLLOWFORKS;
100 case 'a': /* Print execve() argument strings. */
101 trussinfo->flags |= EXECVEARGS;
103 case 'c': /* Count number of system calls and time. */
104 trussinfo->flags |= (COUNTONLY | NOSIGS);
106 case 'e': /* Print execve() environment strings. */
107 trussinfo->flags |= EXECVEENVS;
109 case 'd': /* Absolute timestamps */
110 trussinfo->flags |= ABSOLUTETIMESTAMPS;
112 case 'D': /* Relative timestamps */
113 trussinfo->flags |= RELATIVETIMESTAMPS;
115 case 'o': /* Specified output file */
118 case 's': /* Specified string size */
119 trussinfo->strsize = atoi(optarg);
121 case 'S': /* Don't trace signals */
122 trussinfo->flags |= NOSIGS;
125 trussinfo->flags |= DISPLAYTIDS;
132 ac -= optind; av += optind;
133 if ((pid == 0 && ac == 0) ||
134 (pid != 0 && ac != 0))
137 if (fname != NULL) { /* Use output file */
139 * Set close-on-exec ('e'), so that the output file is not
140 * shared with the traced process.
142 if ((trussinfo->outfile = fopen(fname, "we")) == NULL)
143 err(1, "cannot open %s", fname);
147 * If truss starts the process itself, it will ignore some signals --
148 * they should be passed off to the process, which may or may not
149 * exit. If, however, we are examining an already-running process,
150 * then we restore the event mask on these same signals.
153 /* Start a command ourselves */
155 setup_and_wait(trussinfo, command);
156 signal(SIGINT, SIG_IGN);
157 signal(SIGTERM, SIG_IGN);
158 signal(SIGQUIT, SIG_IGN);
160 sa.sa_handler = restore_proc;
162 sigemptyset(&sa.sa_mask);
163 sigaction(SIGINT, &sa, NULL);
164 sigaction(SIGQUIT, &sa, NULL);
165 sigaction(SIGTERM, &sa, NULL);
166 start_tracing(trussinfo, pid);
170 * At this point, if we started the process, it is stopped waiting to
171 * be woken up, either in exit() or in execve().
173 if (LIST_FIRST(&trussinfo->proclist)->abi == NULL) {
175 * If we are not able to handle this ABI, detach from the
176 * process and exit. If we just created a new process to
177 * run a command, kill the new process rather than letting
181 kill(LIST_FIRST(&trussinfo->proclist)->pid, SIGKILL);
182 ptrace(PT_DETACH, LIST_FIRST(&trussinfo->proclist)->pid, NULL,
186 ptrace(PT_SYSCALL, LIST_FIRST(&trussinfo->proclist)->pid, (caddr_t)1,
190 * At this point, it's a simple loop, waiting for the process to
191 * stop, finding out why, printing out why, and then continuing it.
192 * All of the grunt work is done in the support routines.
194 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
196 eventloop(trussinfo);
198 if (trussinfo->flags & COUNTONLY)
199 print_summary(trussinfo);
201 fflush(trussinfo->outfile);