xref: /freebsd/usr.bin/truss/main.c (revision 9897b203568e7b490904beecf72ff191bafeb2a0)
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",
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
93bbeaf6c0SSean Eric Fagan   { 0, 0, 0 },
94bbeaf6c0SSean Eric Fagan };
95bbeaf6c0SSean Eric Fagan 
96bbeaf6c0SSean Eric Fagan /*
97bbeaf6c0SSean Eric Fagan  * Set the execution type.  This is called after every exec, and when
98bbeaf6c0SSean Eric Fagan  * a process is first monitored.  The procfs pseudo-file "etype" has
99bbeaf6c0SSean Eric Fagan  * the execution module type -- see /proc/curproc/etype for an example.
100bbeaf6c0SSean Eric Fagan  */
101bbeaf6c0SSean Eric Fagan 
102bbeaf6c0SSean Eric Fagan static struct ex_types *
103ec0bed25SMatthew N. Dodd set_etype(struct trussinfo *trussinfo) {
104bbeaf6c0SSean Eric Fagan   struct ex_types *funcs;
105bbeaf6c0SSean Eric Fagan   char etype[24];
1061be5d704SMark Murray   char progt[32];
107bbeaf6c0SSean Eric Fagan   int fd;
108bbeaf6c0SSean Eric Fagan 
109ec0bed25SMatthew N. Dodd   sprintf(etype, "/proc/%d/etype", trussinfo->pid);
110bbeaf6c0SSean Eric Fagan   if ((fd = open(etype, O_RDONLY)) == -1) {
1111be5d704SMark Murray     strcpy(progt, "FreeBSD a.out");
112bbeaf6c0SSean Eric Fagan   } else {
1131be5d704SMark Murray     int len = read(fd, progt, sizeof(progt));
1141be5d704SMark Murray     progt[len-1] = '\0';
11587893934SPeter Wemm     close(fd);
116bbeaf6c0SSean Eric Fagan   }
117bbeaf6c0SSean Eric Fagan 
118bbeaf6c0SSean Eric Fagan   for (funcs = ex_types; funcs->type; funcs++)
1191be5d704SMark Murray     if (!strcmp(funcs->type, progt))
120bbeaf6c0SSean Eric Fagan       break;
121bbeaf6c0SSean Eric Fagan 
1224525f3a8SDag-Erling Smørgrav   if (funcs->type == NULL) {
1233625b514SSean Eric Fagan     funcs = &ex_types[0];
1244525f3a8SDag-Erling Smørgrav     warn("Execution type %s is not supported -- using %s\n",
1254525f3a8SDag-Erling Smørgrav       progt, funcs->type);
1263625b514SSean Eric Fagan   }
127bbeaf6c0SSean Eric Fagan   return funcs;
128bbeaf6c0SSean Eric Fagan }
129bbeaf6c0SSean Eric Fagan 
1303cf51049SPhilippe Charnier int
131bbeaf6c0SSean Eric Fagan main(int ac, char **av) {
132bbeaf6c0SSean Eric Fagan   int c;
133bbeaf6c0SSean Eric Fagan   int i;
134bbeaf6c0SSean Eric Fagan   char **command;
135bbeaf6c0SSean Eric Fagan   struct procfs_status pfs;
136bbeaf6c0SSean Eric Fagan   struct ex_types *funcs;
137bbeaf6c0SSean Eric Fagan   int in_exec = 0;
1383cf51049SPhilippe Charnier   char *fname = NULL;
1399a4902a9SMartin Cracauer   int sigexit = 0;
140ec0bed25SMatthew N. Dodd   struct trussinfo *trussinfo;
141bbeaf6c0SSean Eric Fagan 
142ec0bed25SMatthew N. Dodd   /* Initialize the trussinfo struct */
143ec0bed25SMatthew N. Dodd   trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
144ec0bed25SMatthew N. Dodd   if (trussinfo == NULL)
145ec0bed25SMatthew N. Dodd     errx(1, "malloc() failed");
146ec0bed25SMatthew N. Dodd   bzero(trussinfo, sizeof(struct trussinfo));
147ec0bed25SMatthew N. Dodd   trussinfo->outfile = stderr;
148ec0bed25SMatthew N. Dodd 
1499897b203SMatthew N. Dodd   while ((c = getopt(ac, av, "p:o:faedDS")) != -1) {
150bbeaf6c0SSean Eric Fagan     switch (c) {
151bbeaf6c0SSean Eric Fagan     case 'p':	/* specified pid */
152ec0bed25SMatthew N. Dodd       trussinfo->pid = atoi(optarg);
153bbeaf6c0SSean Eric Fagan       break;
154c03bfcc8SMatthew N. Dodd     case 'f': /* Follow fork()'s */
155c03bfcc8SMatthew N. Dodd       trussinfo->flags |= FOLLOWFORKS;
156c03bfcc8SMatthew N. Dodd       break;
1579897b203SMatthew N. Dodd     case 'a': /* Print execve() argument strings. */
1589897b203SMatthew N. Dodd       trussinfo->flags |= EXECVEARGS;
1599897b203SMatthew N. Dodd       break;
1609897b203SMatthew N. Dodd     case 'e': /* Print execve() environment strings. */
1619897b203SMatthew N. Dodd       trussinfo->flags |= EXECVEENVS;
1629897b203SMatthew N. Dodd       break;
1630d0bd00eSMatthew N. Dodd     case 'd': /* Absolute timestamps */
1640d0bd00eSMatthew N. Dodd       trussinfo->flags |= ABSOLUTETIMESTAMPS;
1650d0bd00eSMatthew N. Dodd       break;
1660d0bd00eSMatthew N. Dodd     case 'D': /* Relative timestamps */
1670d0bd00eSMatthew N. Dodd       trussinfo->flags |= RELATIVETIMESTAMPS;
1680d0bd00eSMatthew N. Dodd       break;
169bbeaf6c0SSean Eric Fagan     case 'o':	/* Specified output file */
1703cf51049SPhilippe Charnier       fname = optarg;
171bbeaf6c0SSean Eric Fagan       break;
172bbeaf6c0SSean Eric Fagan     case 'S':	/* Don't trace signals */
173ec0bed25SMatthew N. Dodd       trussinfo->flags |= NOSIGS;
174bbeaf6c0SSean Eric Fagan       break;
175bbeaf6c0SSean Eric Fagan     default:
176bbeaf6c0SSean Eric Fagan       usage();
177bbeaf6c0SSean Eric Fagan     }
178bbeaf6c0SSean Eric Fagan   }
179bbeaf6c0SSean Eric Fagan 
180bbeaf6c0SSean Eric Fagan   ac -= optind; av += optind;
181ec0bed25SMatthew N. Dodd   if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0))
182bbeaf6c0SSean Eric Fagan     usage();
183bbeaf6c0SSean Eric Fagan 
1843cf51049SPhilippe Charnier   if (fname != NULL) { /* Use output file */
185ec0bed25SMatthew N. Dodd     if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
1863cf51049SPhilippe Charnier       errx(1, "cannot open %s", fname);
1873cf51049SPhilippe Charnier   }
1883cf51049SPhilippe Charnier 
189bbeaf6c0SSean Eric Fagan   /*
190bbeaf6c0SSean Eric Fagan    * If truss starts the process itself, it will ignore some signals --
191bbeaf6c0SSean Eric Fagan    * they should be passed off to the process, which may or may not
192bbeaf6c0SSean Eric Fagan    * exit.  If, however, we are examining an already-running process,
193bbeaf6c0SSean Eric Fagan    * then we restore the event mask on these same signals.
194bbeaf6c0SSean Eric Fagan    */
195bbeaf6c0SSean Eric Fagan 
196ec0bed25SMatthew N. Dodd   if (trussinfo->pid == 0) {	/* Start a command ourselves */
197bbeaf6c0SSean Eric Fagan     command = av;
198ec0bed25SMatthew N. Dodd     trussinfo->pid = setup_and_wait(command);
199bbeaf6c0SSean Eric Fagan     signal(SIGINT, SIG_IGN);
200bbeaf6c0SSean Eric Fagan     signal(SIGTERM, SIG_IGN);
201bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, SIG_IGN);
202bbeaf6c0SSean Eric Fagan   } else {
203bbeaf6c0SSean Eric Fagan     signal(SIGINT, restore_proc);
204bbeaf6c0SSean Eric Fagan     signal(SIGTERM, restore_proc);
205bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, restore_proc);
206bbeaf6c0SSean Eric Fagan   }
207bbeaf6c0SSean Eric Fagan 
208bbeaf6c0SSean Eric Fagan 
209bbeaf6c0SSean Eric Fagan   /*
210bbeaf6c0SSean Eric Fagan    * At this point, if we started the process, it is stopped waiting to
211bbeaf6c0SSean Eric Fagan    * be woken up, either in exit() or in execve().
212bbeaf6c0SSean Eric Fagan    */
213bbeaf6c0SSean Eric Fagan 
214c03bfcc8SMatthew N. Dodd START_TRACE:
215ec0bed25SMatthew N. Dodd   Procfd = start_tracing(
216ec0bed25SMatthew N. Dodd 		trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
217c03bfcc8SMatthew N. Dodd 		((trussinfo->flags & NOSIGS) ? 0 : S_SIG),
218c03bfcc8SMatthew N. Dodd 		((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0));
21989361835SSean Eric Fagan   if (Procfd == -1)
22089361835SSean Eric Fagan     return 0;
22189361835SSean Eric Fagan 
222bbeaf6c0SSean Eric Fagan   pfs.why = 0;
223bbeaf6c0SSean Eric Fagan 
224ec0bed25SMatthew N. Dodd   funcs = set_etype(trussinfo);
225bbeaf6c0SSean Eric Fagan   /*
226bbeaf6c0SSean Eric Fagan    * At this point, it's a simple loop, waiting for the process to
227bbeaf6c0SSean Eric Fagan    * stop, finding out why, printing out why, and then continuing it.
228bbeaf6c0SSean Eric Fagan    * All of the grunt work is done in the support routines.
229bbeaf6c0SSean Eric Fagan    */
230bbeaf6c0SSean Eric Fagan 
2310d0bd00eSMatthew N. Dodd   gettimeofday(&trussinfo->start_time, (struct timezone *)NULL);
2320d0bd00eSMatthew N. Dodd 
233bbeaf6c0SSean Eric Fagan   do {
234bbeaf6c0SSean Eric Fagan     int val = 0;
235bbeaf6c0SSean Eric Fagan 
236bbeaf6c0SSean Eric Fagan     if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
2373cf51049SPhilippe Charnier       warn("PIOCWAIT top of loop");
238bbeaf6c0SSean Eric Fagan     else {
239bbeaf6c0SSean Eric Fagan       switch(i = pfs.why) {
240bbeaf6c0SSean Eric Fagan       case S_SCE:
241ec0bed25SMatthew N. Dodd 	funcs->enter_syscall(trussinfo, pfs.val);
2420d0bd00eSMatthew N. Dodd 	gettimeofday(&trussinfo->before, (struct timezone *)NULL);
243bbeaf6c0SSean Eric Fagan 	break;
244bbeaf6c0SSean Eric Fagan       case S_SCX:
2450d0bd00eSMatthew N. Dodd 	gettimeofday(&trussinfo->after, (struct timezone *)NULL);
246bbeaf6c0SSean Eric Fagan 	/*
247bbeaf6c0SSean Eric Fagan 	 * This is so we don't get two messages for an exec -- one
248bbeaf6c0SSean Eric Fagan 	 * for the S_EXEC, and one for the syscall exit.  It also,
249bbeaf6c0SSean Eric Fagan 	 * conveniently, ensures that the first message printed out
250bbeaf6c0SSean Eric Fagan 	 * isn't the return-from-syscall used to create the process.
251bbeaf6c0SSean Eric Fagan 	 */
252bbeaf6c0SSean Eric Fagan 
253bbeaf6c0SSean Eric Fagan 	if (in_exec) {
254bbeaf6c0SSean Eric Fagan 	  in_exec = 0;
255bbeaf6c0SSean Eric Fagan 	  break;
256bbeaf6c0SSean Eric Fagan 	}
257c03bfcc8SMatthew N. Dodd 
258c03bfcc8SMatthew N. Dodd 	if (trussinfo->in_fork && (trussinfo->flags & FOLLOWFORKS)) {
259c03bfcc8SMatthew N. Dodd 	  int childpid;
260c03bfcc8SMatthew N. Dodd 
261c03bfcc8SMatthew N. Dodd 	  trussinfo->in_fork = 0;
262c03bfcc8SMatthew N. Dodd 	  childpid = funcs->exit_syscall(trussinfo, pfs.val);
263c03bfcc8SMatthew N. Dodd 
264c03bfcc8SMatthew N. Dodd 	  /*
265c03bfcc8SMatthew N. Dodd 	   * Fork a new copy of ourself to trace the child of the
266c03bfcc8SMatthew N. Dodd 	   * original traced process.
267c03bfcc8SMatthew N. Dodd 	   */
268c03bfcc8SMatthew N. Dodd 	  if (fork() == 0) {
269c03bfcc8SMatthew N. Dodd 	    trussinfo->pid = childpid;
270c03bfcc8SMatthew N. Dodd 	    goto START_TRACE;
271c03bfcc8SMatthew N. Dodd 	  }
272c03bfcc8SMatthew N. Dodd 	  break;
273c03bfcc8SMatthew N. Dodd 	}
274ec0bed25SMatthew N. Dodd 	funcs->exit_syscall(trussinfo, pfs.val);
275bbeaf6c0SSean Eric Fagan 	break;
276bbeaf6c0SSean Eric Fagan       case S_SIG:
277ec0bed25SMatthew N. Dodd 	fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val);
2789a4902a9SMartin Cracauer 	sigexit = pfs.val;
279bbeaf6c0SSean Eric Fagan 	break;
280bbeaf6c0SSean Eric Fagan       case S_EXIT:
281ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val);
282bbeaf6c0SSean Eric Fagan 	break;
283bbeaf6c0SSean Eric Fagan       case S_EXEC:
284ec0bed25SMatthew N. Dodd 	funcs = set_etype(trussinfo);
285bbeaf6c0SSean Eric Fagan 	in_exec = 1;
286bbeaf6c0SSean Eric Fagan 	break;
287bbeaf6c0SSean Eric Fagan       default:
288ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "Process stopped because of:  %d\n", i);
289bbeaf6c0SSean Eric Fagan 	break;
290bbeaf6c0SSean Eric Fagan       }
291bbeaf6c0SSean Eric Fagan     }
29289361835SSean Eric Fagan     if (ioctl(Procfd, PIOCCONT, val) == -1) {
293ec0bed25SMatthew N. Dodd       if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
29489361835SSean Eric Fagan 	break;
29589361835SSean Eric Fagan       else
2963cf51049SPhilippe Charnier 	warn("PIOCCONT");
29789361835SSean Eric Fagan     }
298bbeaf6c0SSean Eric Fagan   } while (pfs.why != S_EXIT);
299ec0bed25SMatthew N. Dodd   fflush(trussinfo->outfile);
3009a4902a9SMartin Cracauer   if (sigexit) {
3019a4902a9SMartin Cracauer     if (sigexit == SIGQUIT)
3029a4902a9SMartin Cracauer       exit(sigexit);
3039a4902a9SMartin Cracauer     (void) signal(sigexit, SIG_DFL);
3049a4902a9SMartin Cracauer     (void) kill(getpid(), sigexit);
3059a4902a9SMartin Cracauer   }
306bbeaf6c0SSean Eric Fagan   return 0;
307bbeaf6c0SSean Eric Fagan }
308