xref: /freebsd/usr.bin/truss/main.c (revision b956c13cfe56eae3077dbb877f4ba0a850473cc4)
1bbeaf6c0SSean Eric Fagan /*
209d64da3SSean Eric Fagan  * Copryight 1997 Sean Eric Fagan
309d64da3SSean Eric Fagan  *
409d64da3SSean Eric Fagan  * Redistribution and use in source and binary forms, with or without
509d64da3SSean Eric Fagan  * modification, are permitted provided that the following conditions
609d64da3SSean Eric Fagan  * are met:
709d64da3SSean Eric Fagan  * 1. Redistributions of source code must retain the above copyright
809d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer.
909d64da3SSean Eric Fagan  * 2. Redistributions in binary form must reproduce the above copyright
1009d64da3SSean Eric Fagan  *    notice, this list of conditions and the following disclaimer in the
1109d64da3SSean Eric Fagan  *    documentation and/or other materials provided with the distribution.
1209d64da3SSean Eric Fagan  * 3. All advertising materials mentioning features or use of this software
1309d64da3SSean Eric Fagan  *    must display the following acknowledgement:
1409d64da3SSean Eric Fagan  *	This product includes software developed by Sean Eric Fagan
1509d64da3SSean Eric Fagan  * 4. Neither the name of the author may be used to endorse or promote
1609d64da3SSean Eric Fagan  *    products derived from this software without specific prior written
1709d64da3SSean Eric Fagan  *    permission.
1809d64da3SSean Eric Fagan  *
1909d64da3SSean Eric Fagan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2009d64da3SSean Eric Fagan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2109d64da3SSean Eric Fagan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2209d64da3SSean Eric Fagan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2309d64da3SSean Eric Fagan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2409d64da3SSean Eric Fagan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2509d64da3SSean Eric Fagan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2609d64da3SSean Eric Fagan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2709d64da3SSean Eric Fagan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2809d64da3SSean Eric Fagan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2909d64da3SSean Eric Fagan  * SUCH DAMAGE.
3009d64da3SSean Eric Fagan  */
3109d64da3SSean Eric Fagan 
32b956c13cSPhilippe Charnier #include <sys/cdefs.h>
33b956c13cSPhilippe Charnier __FBSDID("$FreeBSD$");
343cf51049SPhilippe Charnier 
3509d64da3SSean Eric Fagan /*
36bbeaf6c0SSean Eric Fagan  * The main module for truss.  Suprisingly simple, but, then, the other
37bbeaf6c0SSean Eric Fagan  * files handle the bulk of the work.  And, of course, the kernel has to
38bbeaf6c0SSean Eric Fagan  * do a lot of the work :).
39bbeaf6c0SSean Eric Fagan  */
40bbeaf6c0SSean Eric Fagan 
41580e0a2bSDag-Erling Smørgrav #include <sys/param.h>
42580e0a2bSDag-Erling Smørgrav #include <sys/ioctl.h>
43580e0a2bSDag-Erling Smørgrav #include <sys/pioctl.h>
441d631f7eSMike Barcroft #include <sys/time.h>
45580e0a2bSDag-Erling Smørgrav 
463cf51049SPhilippe Charnier #include <err.h>
473cf51049SPhilippe Charnier #include <errno.h>
483cf51049SPhilippe Charnier #include <fcntl.h>
493cf51049SPhilippe Charnier #include <signal.h>
50bbeaf6c0SSean Eric Fagan #include <stdio.h>
51bbeaf6c0SSean Eric Fagan #include <stdlib.h>
52bbeaf6c0SSean Eric Fagan #include <string.h>
5337169f94SMatthew N. Dodd #include <time.h>
5495c4ef65SPeter Wemm #include <unistd.h>
55bbeaf6c0SSean Eric Fagan 
56ec0bed25SMatthew N. Dodd #include "truss.h"
571be5d704SMark Murray #include "extern.h"
58bbeaf6c0SSean Eric Fagan 
59bbeaf6c0SSean Eric Fagan /*
60ec0bed25SMatthew N. Dodd  * It's difficult to parameterize this because it must be
61ec0bed25SMatthew N. Dodd  * accessible in a signal handler.
62bbeaf6c0SSean Eric Fagan  */
63bbeaf6c0SSean Eric Fagan 
64bbeaf6c0SSean Eric Fagan int Procfd;
65bbeaf6c0SSean Eric Fagan 
66bfc3d86aSMark Murray static __inline void
673cf51049SPhilippe Charnier usage(void)
683cf51049SPhilippe Charnier {
693cf51049SPhilippe Charnier   fprintf(stderr, "%s\n%s\n",
709897b203SMatthew N. Dodd 	"usage: truss [-faedDS] [-o file] -p pid",
719897b203SMatthew N. Dodd 	"       truss [-faedDS] [-o file] command [args]");
72bbeaf6c0SSean Eric Fagan   exit(1);
73bbeaf6c0SSean Eric Fagan }
74bbeaf6c0SSean Eric Fagan 
753625b514SSean Eric Fagan /*
763625b514SSean Eric Fagan  * WARNING! "FreeBSD a.out" must be first, or set_etype will not
773625b514SSean Eric Fagan  * work correctly.
783625b514SSean Eric Fagan  */
79bbeaf6c0SSean Eric Fagan struct ex_types {
801be5d704SMark Murray   const char *type;
81ec0bed25SMatthew N. Dodd   void (*enter_syscall)(struct trussinfo *, int);
82ec0bed25SMatthew N. Dodd   int (*exit_syscall)(struct trussinfo *, int);
83bbeaf6c0SSean Eric Fagan } ex_types[] = {
8450cc4492SSean Eric Fagan #ifdef __alpha__
8550cc4492SSean Eric Fagan   { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
8650cc4492SSean Eric Fagan #endif
8750cc4492SSean Eric Fagan #ifdef __i386__
88bbeaf6c0SSean Eric Fagan   { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
8987893934SPeter Wemm   { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
900629483cSMatthew N. Dodd   { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
91bbeaf6c0SSean Eric Fagan   { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
9250cc4492SSean Eric Fagan #endif
93a3e32192SMarcel Moolenaar #ifdef __ia64__
94a3e32192SMarcel Moolenaar   { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit },
95a3e32192SMarcel Moolenaar #endif
96f84c971aSJake Burkholder #ifdef __sparc64__
97f84c971aSJake Burkholder   { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
98f84c971aSJake Burkholder #endif
99bbeaf6c0SSean Eric Fagan   { 0, 0, 0 },
100bbeaf6c0SSean Eric Fagan };
101bbeaf6c0SSean Eric Fagan 
102bbeaf6c0SSean Eric Fagan /*
103bbeaf6c0SSean Eric Fagan  * Set the execution type.  This is called after every exec, and when
104bbeaf6c0SSean Eric Fagan  * a process is first monitored.  The procfs pseudo-file "etype" has
105bbeaf6c0SSean Eric Fagan  * the execution module type -- see /proc/curproc/etype for an example.
106bbeaf6c0SSean Eric Fagan  */
107bbeaf6c0SSean Eric Fagan 
108bbeaf6c0SSean Eric Fagan static struct ex_types *
109ec0bed25SMatthew N. Dodd set_etype(struct trussinfo *trussinfo) {
110bbeaf6c0SSean Eric Fagan   struct ex_types *funcs;
111bbeaf6c0SSean Eric Fagan   char etype[24];
1121be5d704SMark Murray   char progt[32];
113bbeaf6c0SSean Eric Fagan   int fd;
114bbeaf6c0SSean Eric Fagan 
115ec0bed25SMatthew N. Dodd   sprintf(etype, "/proc/%d/etype", trussinfo->pid);
116bbeaf6c0SSean Eric Fagan   if ((fd = open(etype, O_RDONLY)) == -1) {
1171be5d704SMark Murray     strcpy(progt, "FreeBSD a.out");
118bbeaf6c0SSean Eric Fagan   } else {
1191be5d704SMark Murray     int len = read(fd, progt, sizeof(progt));
1201be5d704SMark Murray     progt[len-1] = '\0';
12187893934SPeter Wemm     close(fd);
122bbeaf6c0SSean Eric Fagan   }
123bbeaf6c0SSean Eric Fagan 
124bbeaf6c0SSean Eric Fagan   for (funcs = ex_types; funcs->type; funcs++)
1251be5d704SMark Murray     if (!strcmp(funcs->type, progt))
126bbeaf6c0SSean Eric Fagan       break;
127bbeaf6c0SSean Eric Fagan 
1284525f3a8SDag-Erling Smørgrav   if (funcs->type == NULL) {
1293625b514SSean Eric Fagan     funcs = &ex_types[0];
130b956c13cSPhilippe Charnier     warn("execution type %s is not supported -- using %s",
1314525f3a8SDag-Erling Smørgrav       progt, funcs->type);
1323625b514SSean Eric Fagan   }
133bbeaf6c0SSean Eric Fagan   return funcs;
134bbeaf6c0SSean Eric Fagan }
135bbeaf6c0SSean Eric Fagan 
1363cf51049SPhilippe Charnier int
137bbeaf6c0SSean Eric Fagan main(int ac, char **av) {
138bbeaf6c0SSean Eric Fagan   int c;
139bbeaf6c0SSean Eric Fagan   int i;
140bbeaf6c0SSean Eric Fagan   char **command;
141bbeaf6c0SSean Eric Fagan   struct procfs_status pfs;
142bbeaf6c0SSean Eric Fagan   struct ex_types *funcs;
143bbeaf6c0SSean Eric Fagan   int in_exec = 0;
1443cf51049SPhilippe Charnier   char *fname = NULL;
1459a4902a9SMartin Cracauer   int sigexit = 0;
146ec0bed25SMatthew N. Dodd   struct trussinfo *trussinfo;
147bbeaf6c0SSean Eric Fagan 
148ec0bed25SMatthew N. Dodd   /* Initialize the trussinfo struct */
149ec0bed25SMatthew N. Dodd   trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
150ec0bed25SMatthew N. Dodd   if (trussinfo == NULL)
151ec0bed25SMatthew N. Dodd     errx(1, "malloc() failed");
152ec0bed25SMatthew N. Dodd   bzero(trussinfo, sizeof(struct trussinfo));
153ec0bed25SMatthew N. Dodd   trussinfo->outfile = stderr;
154ec0bed25SMatthew N. Dodd 
1559897b203SMatthew N. Dodd   while ((c = getopt(ac, av, "p:o:faedDS")) != -1) {
156bbeaf6c0SSean Eric Fagan     switch (c) {
157bbeaf6c0SSean Eric Fagan     case 'p':	/* specified pid */
158ec0bed25SMatthew N. Dodd       trussinfo->pid = atoi(optarg);
159bbeaf6c0SSean Eric Fagan       break;
160c03bfcc8SMatthew N. Dodd     case 'f': /* Follow fork()'s */
161c03bfcc8SMatthew N. Dodd       trussinfo->flags |= FOLLOWFORKS;
162c03bfcc8SMatthew N. Dodd       break;
1639897b203SMatthew N. Dodd     case 'a': /* Print execve() argument strings. */
1649897b203SMatthew N. Dodd       trussinfo->flags |= EXECVEARGS;
1659897b203SMatthew N. Dodd       break;
1669897b203SMatthew N. Dodd     case 'e': /* Print execve() environment strings. */
1679897b203SMatthew N. Dodd       trussinfo->flags |= EXECVEENVS;
1689897b203SMatthew N. Dodd       break;
1690d0bd00eSMatthew N. Dodd     case 'd': /* Absolute timestamps */
1700d0bd00eSMatthew N. Dodd       trussinfo->flags |= ABSOLUTETIMESTAMPS;
1710d0bd00eSMatthew N. Dodd       break;
1720d0bd00eSMatthew N. Dodd     case 'D': /* Relative timestamps */
1730d0bd00eSMatthew N. Dodd       trussinfo->flags |= RELATIVETIMESTAMPS;
1740d0bd00eSMatthew N. Dodd       break;
175bbeaf6c0SSean Eric Fagan     case 'o':	/* Specified output file */
1763cf51049SPhilippe Charnier       fname = optarg;
177bbeaf6c0SSean Eric Fagan       break;
178bbeaf6c0SSean Eric Fagan     case 'S':	/* Don't trace signals */
179ec0bed25SMatthew N. Dodd       trussinfo->flags |= NOSIGS;
180bbeaf6c0SSean Eric Fagan       break;
181bbeaf6c0SSean Eric Fagan     default:
182bbeaf6c0SSean Eric Fagan       usage();
183bbeaf6c0SSean Eric Fagan     }
184bbeaf6c0SSean Eric Fagan   }
185bbeaf6c0SSean Eric Fagan 
186bbeaf6c0SSean Eric Fagan   ac -= optind; av += optind;
187ec0bed25SMatthew N. Dodd   if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0))
188bbeaf6c0SSean Eric Fagan     usage();
189bbeaf6c0SSean Eric Fagan 
1903cf51049SPhilippe Charnier   if (fname != NULL) { /* Use output file */
191ec0bed25SMatthew N. Dodd     if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
1923cf51049SPhilippe Charnier       errx(1, "cannot open %s", fname);
1933cf51049SPhilippe Charnier   }
1943cf51049SPhilippe Charnier 
195bbeaf6c0SSean Eric Fagan   /*
196bbeaf6c0SSean Eric Fagan    * If truss starts the process itself, it will ignore some signals --
197bbeaf6c0SSean Eric Fagan    * they should be passed off to the process, which may or may not
198bbeaf6c0SSean Eric Fagan    * exit.  If, however, we are examining an already-running process,
199bbeaf6c0SSean Eric Fagan    * then we restore the event mask on these same signals.
200bbeaf6c0SSean Eric Fagan    */
201bbeaf6c0SSean Eric Fagan 
202ec0bed25SMatthew N. Dodd   if (trussinfo->pid == 0) {	/* Start a command ourselves */
203bbeaf6c0SSean Eric Fagan     command = av;
204ec0bed25SMatthew N. Dodd     trussinfo->pid = setup_and_wait(command);
205bbeaf6c0SSean Eric Fagan     signal(SIGINT, SIG_IGN);
206bbeaf6c0SSean Eric Fagan     signal(SIGTERM, SIG_IGN);
207bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, SIG_IGN);
208bbeaf6c0SSean Eric Fagan   } else {
209bbeaf6c0SSean Eric Fagan     signal(SIGINT, restore_proc);
210bbeaf6c0SSean Eric Fagan     signal(SIGTERM, restore_proc);
211bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, restore_proc);
212bbeaf6c0SSean Eric Fagan   }
213bbeaf6c0SSean Eric Fagan 
214bbeaf6c0SSean Eric Fagan 
215bbeaf6c0SSean Eric Fagan   /*
216bbeaf6c0SSean Eric Fagan    * At this point, if we started the process, it is stopped waiting to
217bbeaf6c0SSean Eric Fagan    * be woken up, either in exit() or in execve().
218bbeaf6c0SSean Eric Fagan    */
219bbeaf6c0SSean Eric Fagan 
220c03bfcc8SMatthew N. Dodd START_TRACE:
221ec0bed25SMatthew N. Dodd   Procfd = start_tracing(
222ec0bed25SMatthew N. Dodd 		trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
223c03bfcc8SMatthew N. Dodd 		((trussinfo->flags & NOSIGS) ? 0 : S_SIG),
224c03bfcc8SMatthew N. Dodd 		((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0));
22589361835SSean Eric Fagan   if (Procfd == -1)
22689361835SSean Eric Fagan     return 0;
22789361835SSean Eric Fagan 
228bbeaf6c0SSean Eric Fagan   pfs.why = 0;
229bbeaf6c0SSean Eric Fagan 
230ec0bed25SMatthew N. Dodd   funcs = set_etype(trussinfo);
231bbeaf6c0SSean Eric Fagan   /*
232bbeaf6c0SSean Eric Fagan    * At this point, it's a simple loop, waiting for the process to
233bbeaf6c0SSean Eric Fagan    * stop, finding out why, printing out why, and then continuing it.
234bbeaf6c0SSean Eric Fagan    * All of the grunt work is done in the support routines.
235bbeaf6c0SSean Eric Fagan    */
236bbeaf6c0SSean Eric Fagan 
237203098d8SMatthew N. Dodd   clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
2380d0bd00eSMatthew N. Dodd 
239bbeaf6c0SSean Eric Fagan   do {
240bbeaf6c0SSean Eric Fagan     int val = 0;
241bbeaf6c0SSean Eric Fagan 
242bbeaf6c0SSean Eric Fagan     if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
2433cf51049SPhilippe Charnier       warn("PIOCWAIT top of loop");
244bbeaf6c0SSean Eric Fagan     else {
245bbeaf6c0SSean Eric Fagan       switch(i = pfs.why) {
246bbeaf6c0SSean Eric Fagan       case S_SCE:
247ec0bed25SMatthew N. Dodd 	funcs->enter_syscall(trussinfo, pfs.val);
248203098d8SMatthew N. Dodd 	clock_gettime(CLOCK_REALTIME, &trussinfo->before);
249bbeaf6c0SSean Eric Fagan 	break;
250bbeaf6c0SSean Eric Fagan       case S_SCX:
251203098d8SMatthew N. Dodd 	clock_gettime(CLOCK_REALTIME, &trussinfo->after);
252bbeaf6c0SSean Eric Fagan 	/*
253bbeaf6c0SSean Eric Fagan 	 * This is so we don't get two messages for an exec -- one
254bbeaf6c0SSean Eric Fagan 	 * for the S_EXEC, and one for the syscall exit.  It also,
255bbeaf6c0SSean Eric Fagan 	 * conveniently, ensures that the first message printed out
256bbeaf6c0SSean Eric Fagan 	 * isn't the return-from-syscall used to create the process.
257bbeaf6c0SSean Eric Fagan 	 */
258bbeaf6c0SSean Eric Fagan 
259bbeaf6c0SSean Eric Fagan 	if (in_exec) {
260bbeaf6c0SSean Eric Fagan 	  in_exec = 0;
261bbeaf6c0SSean Eric Fagan 	  break;
262bbeaf6c0SSean Eric Fagan 	}
263c03bfcc8SMatthew N. Dodd 
264c03bfcc8SMatthew N. Dodd 	if (trussinfo->in_fork && (trussinfo->flags & FOLLOWFORKS)) {
265c03bfcc8SMatthew N. Dodd 	  int childpid;
266c03bfcc8SMatthew N. Dodd 
267c03bfcc8SMatthew N. Dodd 	  trussinfo->in_fork = 0;
268c03bfcc8SMatthew N. Dodd 	  childpid = funcs->exit_syscall(trussinfo, pfs.val);
269c03bfcc8SMatthew N. Dodd 
270c03bfcc8SMatthew N. Dodd 	  /*
271c03bfcc8SMatthew N. Dodd 	   * Fork a new copy of ourself to trace the child of the
272c03bfcc8SMatthew N. Dodd 	   * original traced process.
273c03bfcc8SMatthew N. Dodd 	   */
274c03bfcc8SMatthew N. Dodd 	  if (fork() == 0) {
275c03bfcc8SMatthew N. Dodd 	    trussinfo->pid = childpid;
276c03bfcc8SMatthew N. Dodd 	    goto START_TRACE;
277c03bfcc8SMatthew N. Dodd 	  }
278c03bfcc8SMatthew N. Dodd 	  break;
279c03bfcc8SMatthew N. Dodd 	}
280ec0bed25SMatthew N. Dodd 	funcs->exit_syscall(trussinfo, pfs.val);
281bbeaf6c0SSean Eric Fagan 	break;
282bbeaf6c0SSean Eric Fagan       case S_SIG:
283ec0bed25SMatthew N. Dodd 	fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val);
2849a4902a9SMartin Cracauer 	sigexit = pfs.val;
285bbeaf6c0SSean Eric Fagan 	break;
286bbeaf6c0SSean Eric Fagan       case S_EXIT:
287ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val);
288bbeaf6c0SSean Eric Fagan 	break;
289bbeaf6c0SSean Eric Fagan       case S_EXEC:
290ec0bed25SMatthew N. Dodd 	funcs = set_etype(trussinfo);
291bbeaf6c0SSean Eric Fagan 	in_exec = 1;
292bbeaf6c0SSean Eric Fagan 	break;
293bbeaf6c0SSean Eric Fagan       default:
294ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "Process stopped because of:  %d\n", i);
295bbeaf6c0SSean Eric Fagan 	break;
296bbeaf6c0SSean Eric Fagan       }
297bbeaf6c0SSean Eric Fagan     }
29889361835SSean Eric Fagan     if (ioctl(Procfd, PIOCCONT, val) == -1) {
299ec0bed25SMatthew N. Dodd       if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
30089361835SSean Eric Fagan 	break;
30189361835SSean Eric Fagan       else
3023cf51049SPhilippe Charnier 	warn("PIOCCONT");
30389361835SSean Eric Fagan     }
304bbeaf6c0SSean Eric Fagan   } while (pfs.why != S_EXIT);
305ec0bed25SMatthew N. Dodd   fflush(trussinfo->outfile);
3069a4902a9SMartin Cracauer   if (sigexit) {
3079a4902a9SMartin Cracauer     if (sigexit == SIGQUIT)
3089a4902a9SMartin Cracauer       exit(sigexit);
3099a4902a9SMartin Cracauer     (void) signal(sigexit, SIG_DFL);
3109a4902a9SMartin Cracauer     (void) kill(getpid(), sigexit);
3119a4902a9SMartin Cracauer   }
312bbeaf6c0SSean Eric Fagan   return 0;
313bbeaf6c0SSean Eric Fagan }
314