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>
68 fprintf(stderr, "%s\n%s\n",
69 "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid",
70 " truss [-cfaedDS] [-o file] [-s strsize] command [args]");
75 * WARNING! "FreeBSD a.out" must be first, or set_etype will not
80 void (*enter_syscall)(struct trussinfo *, int);
81 long (*exit_syscall)(struct trussinfo *, int);
84 { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit },
85 { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit },
86 { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit },
89 { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
90 { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
91 { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
92 { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
95 { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
98 { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit },
99 { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit },
102 { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
105 { "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit },
106 { "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit },
107 { "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX
113 * Set the execution type. This is called after every exec, and when
114 * a process is first monitored.
117 static struct ex_types *
118 set_etype(struct trussinfo *trussinfo)
120 struct ex_types *funcs;
123 size_t len = sizeof(progt);
129 mib[2] = KERN_PROC_SV_NAME;
130 mib[3] = trussinfo->pid;
131 error = sysctl(mib, 4, progt, &len, NULL, 0);
133 err(2, "can not get etype");
135 for (funcs = ex_types; funcs->type; funcs++)
136 if (!strcmp(funcs->type, progt))
139 if (funcs->type == NULL) {
140 funcs = &ex_types[0];
141 warn("execution type %s is not supported -- using %s",
153 if (sig > 0 && sig < NSIG) {
155 asprintf(&ret, "sig%s", sys_signame[sig]);
158 for (i = 0; ret[i] != '\0'; ++i)
159 ret[i] = toupper(ret[i]);
165 main(int ac, char **av)
172 struct ex_types *funcs;
175 struct trussinfo *trussinfo;
181 /* Initialize the trussinfo struct */
182 trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo));
183 if (trussinfo == NULL)
184 errx(1, "calloc() failed");
186 trussinfo->outfile = stderr;
187 trussinfo->strsize = 32;
188 trussinfo->pr_why = S_NONE;
189 trussinfo->curthread = NULL;
190 SLIST_INIT(&trussinfo->threadlist);
191 while ((c = getopt(ac, av, "p:o:facedDs:S")) != -1) {
193 case 'p': /* specified pid */
194 trussinfo->pid = atoi(optarg);
195 /* make sure i don't trace me */
196 if(trussinfo->pid == getpid()) {
197 fprintf(stderr, "attempt to grab self.\n");
201 case 'f': /* Follow fork()'s */
202 trussinfo->flags |= FOLLOWFORKS;
204 case 'a': /* Print execve() argument strings. */
205 trussinfo->flags |= EXECVEARGS;
207 case 'c': /* Count number of system calls and time. */
208 trussinfo->flags |= COUNTONLY;
210 case 'e': /* Print execve() environment strings. */
211 trussinfo->flags |= EXECVEENVS;
213 case 'd': /* Absolute timestamps */
214 trussinfo->flags |= ABSOLUTETIMESTAMPS;
216 case 'D': /* Relative timestamps */
217 trussinfo->flags |= RELATIVETIMESTAMPS;
219 case 'o': /* Specified output file */
222 case 's': /* Specified string size */
223 trussinfo->strsize = atoi(optarg);
225 case 'S': /* Don't trace signals */
226 trussinfo->flags |= NOSIGS;
233 ac -= optind; av += optind;
234 if ((trussinfo->pid == 0 && ac == 0) ||
235 (trussinfo->pid != 0 && ac != 0))
238 if (fname != NULL) { /* Use output file */
239 if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
240 errx(1, "cannot open %s", fname);
242 * Set FD_CLOEXEC, so that the output file is not shared with
243 * the traced process.
245 if (fcntl(fileno(trussinfo->outfile), F_SETFD, FD_CLOEXEC) ==
251 * If truss starts the process itself, it will ignore some signals --
252 * they should be passed off to the process, which may or may not
253 * exit. If, however, we are examining an already-running process,
254 * then we restore the event mask on these same signals.
257 if (trussinfo->pid == 0) { /* Start a command ourselves */
259 trussinfo->pid = setup_and_wait(command);
260 signal(SIGINT, SIG_IGN);
261 signal(SIGTERM, SIG_IGN);
262 signal(SIGQUIT, SIG_IGN);
264 start_tracing(trussinfo->pid);
265 signal(SIGINT, restore_proc);
266 signal(SIGTERM, restore_proc);
267 signal(SIGQUIT, restore_proc);
272 * At this point, if we started the process, it is stopped waiting to
273 * be woken up, either in exit() or in execve().
277 funcs = set_etype(trussinfo);
281 * At this point, it's a simple loop, waiting for the process to
282 * stop, finding out why, printing out why, and then continuing it.
283 * All of the grunt work is done in the support routines.
286 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
289 struct timespec timediff;
290 waitevent(trussinfo);
292 switch(i = trussinfo->pr_why) {
294 funcs->enter_syscall(trussinfo, MAXARGS);
295 clock_gettime(CLOCK_REALTIME,
299 clock_gettime(CLOCK_REALTIME,
302 if (trussinfo->curthread->in_fork &&
303 (trussinfo->flags & FOLLOWFORKS)) {
304 trussinfo->curthread->in_fork = 0;
306 funcs->exit_syscall(trussinfo,
310 * Fork a new copy of ourself to trace
311 * the child of the original traced
315 trussinfo->pid = childpid;
316 start_tracing(trussinfo->pid);
321 funcs->exit_syscall(trussinfo, MAXARGS);
324 if (trussinfo->flags & NOSIGS)
326 if (trussinfo->flags & FOLLOWFORKS)
327 fprintf(trussinfo->outfile, "%5d: ",
329 if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
330 timespecsubt(&trussinfo->after,
331 &trussinfo->start_time, &timediff);
332 fprintf(trussinfo->outfile, "%ld.%09ld ",
333 (long)timediff.tv_sec,
336 if (trussinfo->flags & RELATIVETIMESTAMPS) {
337 timespecsubt(&trussinfo->after,
338 &trussinfo->before, &timediff);
339 fprintf(trussinfo->outfile, "%ld.%09ld ",
340 (long)timediff.tv_sec,
343 signame = strsig(trussinfo->pr_data);
344 fprintf(trussinfo->outfile,
345 "SIGNAL %u (%s)\n", trussinfo->pr_data,
346 signame == NULL ? "?" : signame);
350 if (trussinfo->flags & COUNTONLY)
352 if (trussinfo->flags & FOLLOWFORKS)
353 fprintf(trussinfo->outfile, "%5d: ",
355 if (trussinfo->flags & ABSOLUTETIMESTAMPS) {
356 timespecsubt(&trussinfo->after,
357 &trussinfo->start_time, &timediff);
358 fprintf(trussinfo->outfile, "%ld.%09ld ",
359 (long)timediff.tv_sec,
362 if (trussinfo->flags & RELATIVETIMESTAMPS) {
363 timespecsubt(&trussinfo->after,
364 &trussinfo->before, &timediff);
365 fprintf(trussinfo->outfile, "%ld.%09ld ",
366 (long)timediff.tv_sec, timediff.tv_nsec);
368 fprintf(trussinfo->outfile,
369 "process exit, rval = %u\n", trussinfo->pr_data);
374 } while (trussinfo->pr_why != S_EXIT);
376 if (trussinfo->flags & FOLLOWFORKS)
378 childpid = wait(&status);
379 } while (childpid != -1);
381 if (trussinfo->flags & COUNTONLY)
382 print_summary(trussinfo);
384 fflush(trussinfo->outfile);