xref: /freebsd/usr.bin/truss/main.c (revision c03bfcc871d34aad4a41210a3f458531dc68f5c1)
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 
323cf51049SPhilippe Charnier #ifndef lint
333cf51049SPhilippe Charnier static const char rcsid[] =
34c3aac50fSPeter Wemm   "$FreeBSD$";
353cf51049SPhilippe Charnier #endif /* not lint */
363cf51049SPhilippe Charnier 
3709d64da3SSean Eric Fagan /*
38bbeaf6c0SSean Eric Fagan  * The main module for truss.  Suprisingly simple, but, then, the other
39bbeaf6c0SSean Eric Fagan  * files handle the bulk of the work.  And, of course, the kernel has to
40bbeaf6c0SSean Eric Fagan  * do a lot of the work :).
41bbeaf6c0SSean Eric Fagan  */
42bbeaf6c0SSean Eric Fagan 
43580e0a2bSDag-Erling Smørgrav #include <sys/param.h>
44580e0a2bSDag-Erling Smørgrav #include <sys/ioctl.h>
45580e0a2bSDag-Erling Smørgrav #include <sys/pioctl.h>
46580e0a2bSDag-Erling Smørgrav 
473cf51049SPhilippe Charnier #include <err.h>
483cf51049SPhilippe Charnier #include <errno.h>
493cf51049SPhilippe Charnier #include <fcntl.h>
503cf51049SPhilippe Charnier #include <signal.h>
51bbeaf6c0SSean Eric Fagan #include <stdio.h>
52bbeaf6c0SSean Eric Fagan #include <stdlib.h>
53bbeaf6c0SSean Eric Fagan #include <string.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",
70c03bfcc8SMatthew N. Dodd 	"usage: truss [-fS] [-o file] -p pid",
71c03bfcc8SMatthew N. Dodd 	"       truss [-fS] [-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 },
90bbeaf6c0SSean Eric Fagan   { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
9150cc4492SSean Eric Fagan #endif
92bbeaf6c0SSean Eric Fagan   { 0, 0, 0 },
93bbeaf6c0SSean Eric Fagan };
94bbeaf6c0SSean Eric Fagan 
95bbeaf6c0SSean Eric Fagan /*
96bbeaf6c0SSean Eric Fagan  * Set the execution type.  This is called after every exec, and when
97bbeaf6c0SSean Eric Fagan  * a process is first monitored.  The procfs pseudo-file "etype" has
98bbeaf6c0SSean Eric Fagan  * the execution module type -- see /proc/curproc/etype for an example.
99bbeaf6c0SSean Eric Fagan  */
100bbeaf6c0SSean Eric Fagan 
101bbeaf6c0SSean Eric Fagan static struct ex_types *
102ec0bed25SMatthew N. Dodd set_etype(struct trussinfo *trussinfo) {
103bbeaf6c0SSean Eric Fagan   struct ex_types *funcs;
104bbeaf6c0SSean Eric Fagan   char etype[24];
1051be5d704SMark Murray   char progt[32];
106bbeaf6c0SSean Eric Fagan   int fd;
107bbeaf6c0SSean Eric Fagan 
108ec0bed25SMatthew N. Dodd   sprintf(etype, "/proc/%d/etype", trussinfo->pid);
109bbeaf6c0SSean Eric Fagan   if ((fd = open(etype, O_RDONLY)) == -1) {
1101be5d704SMark Murray     strcpy(progt, "FreeBSD a.out");
111bbeaf6c0SSean Eric Fagan   } else {
1121be5d704SMark Murray     int len = read(fd, progt, sizeof(progt));
1131be5d704SMark Murray     progt[len-1] = '\0';
11487893934SPeter Wemm     close(fd);
115bbeaf6c0SSean Eric Fagan   }
116bbeaf6c0SSean Eric Fagan 
117bbeaf6c0SSean Eric Fagan   for (funcs = ex_types; funcs->type; funcs++)
1181be5d704SMark Murray     if (!strcmp(funcs->type, progt))
119bbeaf6c0SSean Eric Fagan       break;
120bbeaf6c0SSean Eric Fagan 
1214525f3a8SDag-Erling Smørgrav   if (funcs->type == NULL) {
1223625b514SSean Eric Fagan     funcs = &ex_types[0];
1234525f3a8SDag-Erling Smørgrav     warn("Execution type %s is not supported -- using %s\n",
1244525f3a8SDag-Erling Smørgrav       progt, funcs->type);
1253625b514SSean Eric Fagan   }
126bbeaf6c0SSean Eric Fagan   return funcs;
127bbeaf6c0SSean Eric Fagan }
128bbeaf6c0SSean Eric Fagan 
1293cf51049SPhilippe Charnier int
130bbeaf6c0SSean Eric Fagan main(int ac, char **av) {
131bbeaf6c0SSean Eric Fagan   int c;
132bbeaf6c0SSean Eric Fagan   int i;
133bbeaf6c0SSean Eric Fagan   char **command;
134bbeaf6c0SSean Eric Fagan   struct procfs_status pfs;
135bbeaf6c0SSean Eric Fagan   struct ex_types *funcs;
136bbeaf6c0SSean Eric Fagan   int in_exec = 0;
1373cf51049SPhilippe Charnier   char *fname = NULL;
1389a4902a9SMartin Cracauer   int sigexit = 0;
139ec0bed25SMatthew N. Dodd   struct trussinfo *trussinfo;
140bbeaf6c0SSean Eric Fagan 
141ec0bed25SMatthew N. Dodd   /* Initialize the trussinfo struct */
142ec0bed25SMatthew N. Dodd   trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
143ec0bed25SMatthew N. Dodd   if (trussinfo == NULL)
144ec0bed25SMatthew N. Dodd     errx(1, "malloc() failed");
145ec0bed25SMatthew N. Dodd   bzero(trussinfo, sizeof(struct trussinfo));
146ec0bed25SMatthew N. Dodd   trussinfo->outfile = stderr;
147ec0bed25SMatthew N. Dodd 
148c03bfcc8SMatthew N. Dodd   while ((c = getopt(ac, av, "p:o:fS")) != -1) {
149bbeaf6c0SSean Eric Fagan     switch (c) {
150bbeaf6c0SSean Eric Fagan     case 'p':	/* specified pid */
151ec0bed25SMatthew N. Dodd       trussinfo->pid = atoi(optarg);
152bbeaf6c0SSean Eric Fagan       break;
153c03bfcc8SMatthew N. Dodd     case 'f': /* Follow fork()'s */
154c03bfcc8SMatthew N. Dodd       trussinfo->flags |= FOLLOWFORKS;
155c03bfcc8SMatthew N. Dodd       break;
156bbeaf6c0SSean Eric Fagan     case 'o':	/* Specified output file */
1573cf51049SPhilippe Charnier       fname = optarg;
158bbeaf6c0SSean Eric Fagan       break;
159bbeaf6c0SSean Eric Fagan     case 'S':	/* Don't trace signals */
160ec0bed25SMatthew N. Dodd       trussinfo->flags |= NOSIGS;
161bbeaf6c0SSean Eric Fagan       break;
162bbeaf6c0SSean Eric Fagan     default:
163bbeaf6c0SSean Eric Fagan       usage();
164bbeaf6c0SSean Eric Fagan     }
165bbeaf6c0SSean Eric Fagan   }
166bbeaf6c0SSean Eric Fagan 
167bbeaf6c0SSean Eric Fagan   ac -= optind; av += optind;
168ec0bed25SMatthew N. Dodd   if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0))
169bbeaf6c0SSean Eric Fagan     usage();
170bbeaf6c0SSean Eric Fagan 
1713cf51049SPhilippe Charnier   if (fname != NULL) { /* Use output file */
172ec0bed25SMatthew N. Dodd     if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
1733cf51049SPhilippe Charnier       errx(1, "cannot open %s", fname);
1743cf51049SPhilippe Charnier   }
1753cf51049SPhilippe Charnier 
176bbeaf6c0SSean Eric Fagan   /*
177bbeaf6c0SSean Eric Fagan    * If truss starts the process itself, it will ignore some signals --
178bbeaf6c0SSean Eric Fagan    * they should be passed off to the process, which may or may not
179bbeaf6c0SSean Eric Fagan    * exit.  If, however, we are examining an already-running process,
180bbeaf6c0SSean Eric Fagan    * then we restore the event mask on these same signals.
181bbeaf6c0SSean Eric Fagan    */
182bbeaf6c0SSean Eric Fagan 
183ec0bed25SMatthew N. Dodd   if (trussinfo->pid == 0) {	/* Start a command ourselves */
184bbeaf6c0SSean Eric Fagan     command = av;
185ec0bed25SMatthew N. Dodd     trussinfo->pid = setup_and_wait(command);
186bbeaf6c0SSean Eric Fagan     signal(SIGINT, SIG_IGN);
187bbeaf6c0SSean Eric Fagan     signal(SIGTERM, SIG_IGN);
188bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, SIG_IGN);
189bbeaf6c0SSean Eric Fagan   } else {
190bbeaf6c0SSean Eric Fagan     signal(SIGINT, restore_proc);
191bbeaf6c0SSean Eric Fagan     signal(SIGTERM, restore_proc);
192bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, restore_proc);
193bbeaf6c0SSean Eric Fagan   }
194bbeaf6c0SSean Eric Fagan 
195bbeaf6c0SSean Eric Fagan 
196bbeaf6c0SSean Eric Fagan   /*
197bbeaf6c0SSean Eric Fagan    * At this point, if we started the process, it is stopped waiting to
198bbeaf6c0SSean Eric Fagan    * be woken up, either in exit() or in execve().
199bbeaf6c0SSean Eric Fagan    */
200bbeaf6c0SSean Eric Fagan 
201c03bfcc8SMatthew N. Dodd START_TRACE:
202ec0bed25SMatthew N. Dodd   Procfd = start_tracing(
203ec0bed25SMatthew N. Dodd 		trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
204c03bfcc8SMatthew N. Dodd 		((trussinfo->flags & NOSIGS) ? 0 : S_SIG),
205c03bfcc8SMatthew N. Dodd 		((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0));
20689361835SSean Eric Fagan   if (Procfd == -1)
20789361835SSean Eric Fagan     return 0;
20889361835SSean Eric Fagan 
209bbeaf6c0SSean Eric Fagan   pfs.why = 0;
210bbeaf6c0SSean Eric Fagan 
211ec0bed25SMatthew N. Dodd   funcs = set_etype(trussinfo);
212bbeaf6c0SSean Eric Fagan   /*
213bbeaf6c0SSean Eric Fagan    * At this point, it's a simple loop, waiting for the process to
214bbeaf6c0SSean Eric Fagan    * stop, finding out why, printing out why, and then continuing it.
215bbeaf6c0SSean Eric Fagan    * All of the grunt work is done in the support routines.
216bbeaf6c0SSean Eric Fagan    */
217bbeaf6c0SSean Eric Fagan 
218bbeaf6c0SSean Eric Fagan   do {
219bbeaf6c0SSean Eric Fagan     int val = 0;
220bbeaf6c0SSean Eric Fagan 
221bbeaf6c0SSean Eric Fagan     if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
2223cf51049SPhilippe Charnier       warn("PIOCWAIT top of loop");
223bbeaf6c0SSean Eric Fagan     else {
224bbeaf6c0SSean Eric Fagan       switch(i = pfs.why) {
225bbeaf6c0SSean Eric Fagan       case S_SCE:
226ec0bed25SMatthew N. Dodd 	funcs->enter_syscall(trussinfo, pfs.val);
227bbeaf6c0SSean Eric Fagan 	break;
228bbeaf6c0SSean Eric Fagan       case S_SCX:
229bbeaf6c0SSean Eric Fagan 	/*
230bbeaf6c0SSean Eric Fagan 	 * This is so we don't get two messages for an exec -- one
231bbeaf6c0SSean Eric Fagan 	 * for the S_EXEC, and one for the syscall exit.  It also,
232bbeaf6c0SSean Eric Fagan 	 * conveniently, ensures that the first message printed out
233bbeaf6c0SSean Eric Fagan 	 * isn't the return-from-syscall used to create the process.
234bbeaf6c0SSean Eric Fagan 	 */
235bbeaf6c0SSean Eric Fagan 
236bbeaf6c0SSean Eric Fagan 	if (in_exec) {
237bbeaf6c0SSean Eric Fagan 	  in_exec = 0;
238bbeaf6c0SSean Eric Fagan 	  break;
239bbeaf6c0SSean Eric Fagan 	}
240c03bfcc8SMatthew N. Dodd 
241c03bfcc8SMatthew N. Dodd 	if (trussinfo->in_fork && (trussinfo->flags & FOLLOWFORKS)) {
242c03bfcc8SMatthew N. Dodd 	  int childpid;
243c03bfcc8SMatthew N. Dodd 
244c03bfcc8SMatthew N. Dodd 	  trussinfo->in_fork = 0;
245c03bfcc8SMatthew N. Dodd 	  childpid = funcs->exit_syscall(trussinfo, pfs.val);
246c03bfcc8SMatthew N. Dodd 
247c03bfcc8SMatthew N. Dodd 	  /*
248c03bfcc8SMatthew N. Dodd 	   * Fork a new copy of ourself to trace the child of the
249c03bfcc8SMatthew N. Dodd 	   * original traced process.
250c03bfcc8SMatthew N. Dodd 	   */
251c03bfcc8SMatthew N. Dodd 	  if (fork() == 0) {
252c03bfcc8SMatthew N. Dodd 	    trussinfo->pid = childpid;
253c03bfcc8SMatthew N. Dodd 	    goto START_TRACE;
254c03bfcc8SMatthew N. Dodd 	  }
255c03bfcc8SMatthew N. Dodd 	  break;
256c03bfcc8SMatthew N. Dodd 	}
257ec0bed25SMatthew N. Dodd 	funcs->exit_syscall(trussinfo, pfs.val);
258bbeaf6c0SSean Eric Fagan 	break;
259bbeaf6c0SSean Eric Fagan       case S_SIG:
260ec0bed25SMatthew N. Dodd 	fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val);
2619a4902a9SMartin Cracauer 	sigexit = pfs.val;
262bbeaf6c0SSean Eric Fagan 	break;
263bbeaf6c0SSean Eric Fagan       case S_EXIT:
264ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val);
265bbeaf6c0SSean Eric Fagan 	break;
266bbeaf6c0SSean Eric Fagan       case S_EXEC:
267ec0bed25SMatthew N. Dodd 	funcs = set_etype(trussinfo);
268bbeaf6c0SSean Eric Fagan 	in_exec = 1;
269bbeaf6c0SSean Eric Fagan 	break;
270bbeaf6c0SSean Eric Fagan       default:
271ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "Process stopped because of:  %d\n", i);
272bbeaf6c0SSean Eric Fagan 	break;
273bbeaf6c0SSean Eric Fagan       }
274bbeaf6c0SSean Eric Fagan     }
27589361835SSean Eric Fagan     if (ioctl(Procfd, PIOCCONT, val) == -1) {
276ec0bed25SMatthew N. Dodd       if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
27789361835SSean Eric Fagan 	break;
27889361835SSean Eric Fagan       else
2793cf51049SPhilippe Charnier 	warn("PIOCCONT");
28089361835SSean Eric Fagan     }
281bbeaf6c0SSean Eric Fagan   } while (pfs.why != S_EXIT);
282ec0bed25SMatthew N. Dodd   fflush(trussinfo->outfile);
2839a4902a9SMartin Cracauer   if (sigexit) {
2849a4902a9SMartin Cracauer     if (sigexit == SIGQUIT)
2859a4902a9SMartin Cracauer       exit(sigexit);
2869a4902a9SMartin Cracauer     (void) signal(sigexit, SIG_DFL);
2879a4902a9SMartin Cracauer     (void) kill(getpid(), sigexit);
2889a4902a9SMartin Cracauer   }
289bbeaf6c0SSean Eric Fagan   return 0;
290bbeaf6c0SSean Eric Fagan }
291