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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * The main module for truss. Suprisingly 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/param.h>
42 #include <sys/types.h>
44 #include <sys/resource.h>
45 #include <sys/sysctl.h>
66 fprintf(stderr, "%s\n%s\n",
67 "usage: truss [-faedDS] [-o file] [-s strsize] -p pid",
68 " truss [-faedDS] [-o file] [-s strsize] command [args]");
73 * WARNING! "FreeBSD a.out" must be first, or set_etype will not
78 void (*enter_syscall)(struct trussinfo *, int);
79 long (*exit_syscall)(struct trussinfo *, int);
82 { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
85 { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
88 { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
89 { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
90 { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
91 { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
94 { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
97 { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit },
98 { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit },
101 { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
107 * Set the execution type. This is called after every exec, and when
108 * a process is first monitored.
111 static struct ex_types *
112 set_etype(struct trussinfo *trussinfo)
114 struct ex_types *funcs;
117 size_t len = sizeof(progt);
123 mib[2] = KERN_PROC_SV_NAME;
124 mib[3] = trussinfo->pid;
125 error = sysctl(mib, 4, progt, &len, NULL, 0);
127 err(2, "can not get etype");
129 for (funcs = ex_types; funcs->type; funcs++)
130 if (!strcmp(funcs->type, progt))
133 if (funcs->type == NULL) {
134 funcs = &ex_types[0];
135 warn("execution type %s is not supported -- using %s",
147 if (sig > 0 && sig < NSIG) {
149 asprintf(&ret, "sig%s", sys_signame[sig]);
152 for (i = 0; ret[i] != '\0'; ++i)
153 ret[i] = toupper(ret[i]);
159 main(int ac, char **av)
164 struct ex_types *funcs;
167 struct trussinfo *trussinfo;
173 /* Initialize the trussinfo struct */
174 trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
175 if (trussinfo == NULL)
176 errx(1, "malloc() failed");
177 bzero(trussinfo, sizeof(struct trussinfo));
179 trussinfo->outfile = stderr;
180 trussinfo->strsize = 32;
181 trussinfo->pr_why = S_NONE;
182 trussinfo->curthread = NULL;
183 SLIST_INIT(&trussinfo->threadlist);
184 while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) {
186 case 'p': /* specified pid */
187 trussinfo->pid = atoi(optarg);
188 /* make sure i don't trace me */
189 if(trussinfo->pid == getpid()) {
190 fprintf(stderr, "attempt to grab self.\n");
194 case 'f': /* Follow fork()'s */
195 trussinfo->flags |= FOLLOWFORKS;
197 case 'a': /* Print execve() argument strings. */
198 trussinfo->flags |= EXECVEARGS;
200 case 'e': /* Print execve() environment strings. */
201 trussinfo->flags |= EXECVEENVS;
203 case 'd': /* Absolute timestamps */
204 trussinfo->flags |= ABSOLUTETIMESTAMPS;
206 case 'D': /* Relative timestamps */
207 trussinfo->flags |= RELATIVETIMESTAMPS;
209 case 'o': /* Specified output file */
212 case 's': /* Specified string size */
213 trussinfo->strsize = atoi(optarg);
215 case 'S': /* Don't trace signals */
216 trussinfo->flags |= NOSIGS;
223 ac -= optind; av += optind;
224 if ((trussinfo->pid == 0 && ac == 0) ||
225 (trussinfo->pid != 0 && ac != 0))
228 if (fname != NULL) { /* Use output file */
229 if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
230 errx(1, "cannot open %s", fname);
234 * If truss starts the process itself, it will ignore some signals --
235 * they should be passed off to the process, which may or may not
236 * exit. If, however, we are examining an already-running process,
237 * then we restore the event mask on these same signals.
240 if (trussinfo->pid == 0) { /* Start a command ourselves */
242 trussinfo->pid = setup_and_wait(command);
243 signal(SIGINT, SIG_IGN);
244 signal(SIGTERM, SIG_IGN);
245 signal(SIGQUIT, SIG_IGN);
247 start_tracing(trussinfo->pid);
248 signal(SIGINT, restore_proc);
249 signal(SIGTERM, restore_proc);
250 signal(SIGQUIT, restore_proc);
255 * At this point, if we started the process, it is stopped waiting to
256 * be woken up, either in exit() or in execve().
260 funcs = set_etype(trussinfo);
264 * At this point, it's a simple loop, waiting for the process to
265 * stop, finding out why, printing out why, and then continuing it.
266 * All of the grunt work is done in the support routines.
269 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
272 struct timespec timediff;
273 waitevent(trussinfo);
275 switch(i = trussinfo->pr_why) {
277 funcs->enter_syscall(trussinfo, MAXARGS);
278 clock_gettime(CLOCK_REALTIME,
282 clock_gettime(CLOCK_REALTIME,
285 if (trussinfo->curthread->in_fork &&
286 (trussinfo->flags & FOLLOWFORKS)) {
289 trussinfo->curthread->in_fork = 0;
291 funcs->exit_syscall(trussinfo,
295 * Fork a new copy of ourself to trace
296 * the child of the original traced
300 trussinfo->pid = childpid;
301 start_tracing(trussinfo->pid);
306 funcs->exit_syscall(trussinfo, MAXARGS);
309 if (trussinfo->flags & NOSIGS)
311 if (trussinfo->flags & FOLLOWFORKS)
312 fprintf(trussinfo->outfile, "%5d: ",
314 if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
315 timespecsubt(&trussinfo->after,
316 &trussinfo->start_time, &timediff);
317 fprintf(trussinfo->outfile, "%ld.%09ld ",
318 (long)timediff.tv_sec,
321 if (trussinfo->flags & RELATIVETIMESTAMPS) {
322 timespecsubt(&trussinfo->after,
323 &trussinfo->before, &timediff);
324 fprintf(trussinfo->outfile, "%ld.%09ld ",
325 (long)timediff.tv_sec,
328 signame = strsig(trussinfo->pr_data);
329 fprintf(trussinfo->outfile,
330 "SIGNAL %u (%s)\n", trussinfo->pr_data,
331 signame == NULL ? "?" : signame);
335 if (trussinfo->flags & FOLLOWFORKS)
336 fprintf(trussinfo->outfile, "%5d: ",
338 if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
339 timespecsubt(&trussinfo->after,
340 &trussinfo->start_time, &timediff);
341 fprintf(trussinfo->outfile, "%ld.%09ld ",
342 (long)timediff.tv_sec,
345 if (trussinfo->flags & RELATIVETIMESTAMPS) {
346 timespecsubt(&trussinfo->after,
347 &trussinfo->before, &timediff);
348 fprintf(trussinfo->outfile, "%ld.%09ld ",
349 (long)timediff.tv_sec, timediff.tv_nsec);
351 fprintf(trussinfo->outfile,
352 "process exit, rval = %u\n", trussinfo->pr_data);
357 } while (trussinfo->pr_why != S_EXIT);
358 fflush(trussinfo->outfile);