xref: /freebsd/usr.bin/truss/main.c (revision 37169f9446f1a507add17a8c3804bb48f56dc307)
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>
5437169f94SMatthew N. Dodd #include <time.h>
5595c4ef65SPeter Wemm #include <unistd.h>
56bbeaf6c0SSean Eric Fagan 
57ec0bed25SMatthew N. Dodd #include "truss.h"
581be5d704SMark Murray #include "extern.h"
59bbeaf6c0SSean Eric Fagan 
60bbeaf6c0SSean Eric Fagan /*
61ec0bed25SMatthew N. Dodd  * It's difficult to parameterize this because it must be
62ec0bed25SMatthew N. Dodd  * accessible in a signal handler.
63bbeaf6c0SSean Eric Fagan  */
64bbeaf6c0SSean Eric Fagan 
65bbeaf6c0SSean Eric Fagan int Procfd;
66bbeaf6c0SSean Eric Fagan 
67bfc3d86aSMark Murray static __inline void
683cf51049SPhilippe Charnier usage(void)
693cf51049SPhilippe Charnier {
703cf51049SPhilippe Charnier   fprintf(stderr, "%s\n%s\n",
719897b203SMatthew N. Dodd 	"usage: truss [-faedDS] [-o file] -p pid",
729897b203SMatthew N. Dodd 	"       truss [-faedDS] [-o file] command [args]");
73bbeaf6c0SSean Eric Fagan   exit(1);
74bbeaf6c0SSean Eric Fagan }
75bbeaf6c0SSean Eric Fagan 
763625b514SSean Eric Fagan /*
773625b514SSean Eric Fagan  * WARNING! "FreeBSD a.out" must be first, or set_etype will not
783625b514SSean Eric Fagan  * work correctly.
793625b514SSean Eric Fagan  */
80bbeaf6c0SSean Eric Fagan struct ex_types {
811be5d704SMark Murray   const char *type;
82ec0bed25SMatthew N. Dodd   void (*enter_syscall)(struct trussinfo *, int);
83ec0bed25SMatthew N. Dodd   int (*exit_syscall)(struct trussinfo *, int);
84bbeaf6c0SSean Eric Fagan } ex_types[] = {
8550cc4492SSean Eric Fagan #ifdef __alpha__
8650cc4492SSean Eric Fagan   { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
8750cc4492SSean Eric Fagan #endif
8850cc4492SSean Eric Fagan #ifdef __i386__
89bbeaf6c0SSean Eric Fagan   { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
9087893934SPeter Wemm   { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
910629483cSMatthew N. Dodd   { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit },
92bbeaf6c0SSean Eric Fagan   { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
9350cc4492SSean Eric Fagan #endif
94f84c971aSJake Burkholder #ifdef __sparc64__
95f84c971aSJake Burkholder   { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit },
96f84c971aSJake Burkholder #endif
97bbeaf6c0SSean Eric Fagan   { 0, 0, 0 },
98bbeaf6c0SSean Eric Fagan };
99bbeaf6c0SSean Eric Fagan 
100bbeaf6c0SSean Eric Fagan /*
101bbeaf6c0SSean Eric Fagan  * Set the execution type.  This is called after every exec, and when
102bbeaf6c0SSean Eric Fagan  * a process is first monitored.  The procfs pseudo-file "etype" has
103bbeaf6c0SSean Eric Fagan  * the execution module type -- see /proc/curproc/etype for an example.
104bbeaf6c0SSean Eric Fagan  */
105bbeaf6c0SSean Eric Fagan 
106bbeaf6c0SSean Eric Fagan static struct ex_types *
107ec0bed25SMatthew N. Dodd set_etype(struct trussinfo *trussinfo) {
108bbeaf6c0SSean Eric Fagan   struct ex_types *funcs;
109bbeaf6c0SSean Eric Fagan   char etype[24];
1101be5d704SMark Murray   char progt[32];
111bbeaf6c0SSean Eric Fagan   int fd;
112bbeaf6c0SSean Eric Fagan 
113ec0bed25SMatthew N. Dodd   sprintf(etype, "/proc/%d/etype", trussinfo->pid);
114bbeaf6c0SSean Eric Fagan   if ((fd = open(etype, O_RDONLY)) == -1) {
1151be5d704SMark Murray     strcpy(progt, "FreeBSD a.out");
116bbeaf6c0SSean Eric Fagan   } else {
1171be5d704SMark Murray     int len = read(fd, progt, sizeof(progt));
1181be5d704SMark Murray     progt[len-1] = '\0';
11987893934SPeter Wemm     close(fd);
120bbeaf6c0SSean Eric Fagan   }
121bbeaf6c0SSean Eric Fagan 
122bbeaf6c0SSean Eric Fagan   for (funcs = ex_types; funcs->type; funcs++)
1231be5d704SMark Murray     if (!strcmp(funcs->type, progt))
124bbeaf6c0SSean Eric Fagan       break;
125bbeaf6c0SSean Eric Fagan 
1264525f3a8SDag-Erling Smørgrav   if (funcs->type == NULL) {
1273625b514SSean Eric Fagan     funcs = &ex_types[0];
1284525f3a8SDag-Erling Smørgrav     warn("Execution type %s is not supported -- using %s\n",
1294525f3a8SDag-Erling Smørgrav       progt, funcs->type);
1303625b514SSean Eric Fagan   }
131bbeaf6c0SSean Eric Fagan   return funcs;
132bbeaf6c0SSean Eric Fagan }
133bbeaf6c0SSean Eric Fagan 
1343cf51049SPhilippe Charnier int
135bbeaf6c0SSean Eric Fagan main(int ac, char **av) {
136bbeaf6c0SSean Eric Fagan   int c;
137bbeaf6c0SSean Eric Fagan   int i;
138bbeaf6c0SSean Eric Fagan   char **command;
139bbeaf6c0SSean Eric Fagan   struct procfs_status pfs;
140bbeaf6c0SSean Eric Fagan   struct ex_types *funcs;
141bbeaf6c0SSean Eric Fagan   int in_exec = 0;
1423cf51049SPhilippe Charnier   char *fname = NULL;
1439a4902a9SMartin Cracauer   int sigexit = 0;
144ec0bed25SMatthew N. Dodd   struct trussinfo *trussinfo;
145bbeaf6c0SSean Eric Fagan 
146ec0bed25SMatthew N. Dodd   /* Initialize the trussinfo struct */
147ec0bed25SMatthew N. Dodd   trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
148ec0bed25SMatthew N. Dodd   if (trussinfo == NULL)
149ec0bed25SMatthew N. Dodd     errx(1, "malloc() failed");
150ec0bed25SMatthew N. Dodd   bzero(trussinfo, sizeof(struct trussinfo));
151ec0bed25SMatthew N. Dodd   trussinfo->outfile = stderr;
152ec0bed25SMatthew N. Dodd 
1539897b203SMatthew N. Dodd   while ((c = getopt(ac, av, "p:o:faedDS")) != -1) {
154bbeaf6c0SSean Eric Fagan     switch (c) {
155bbeaf6c0SSean Eric Fagan     case 'p':	/* specified pid */
156ec0bed25SMatthew N. Dodd       trussinfo->pid = atoi(optarg);
157bbeaf6c0SSean Eric Fagan       break;
158c03bfcc8SMatthew N. Dodd     case 'f': /* Follow fork()'s */
159c03bfcc8SMatthew N. Dodd       trussinfo->flags |= FOLLOWFORKS;
160c03bfcc8SMatthew N. Dodd       break;
1619897b203SMatthew N. Dodd     case 'a': /* Print execve() argument strings. */
1629897b203SMatthew N. Dodd       trussinfo->flags |= EXECVEARGS;
1639897b203SMatthew N. Dodd       break;
1649897b203SMatthew N. Dodd     case 'e': /* Print execve() environment strings. */
1659897b203SMatthew N. Dodd       trussinfo->flags |= EXECVEENVS;
1669897b203SMatthew N. Dodd       break;
1670d0bd00eSMatthew N. Dodd     case 'd': /* Absolute timestamps */
1680d0bd00eSMatthew N. Dodd       trussinfo->flags |= ABSOLUTETIMESTAMPS;
1690d0bd00eSMatthew N. Dodd       break;
1700d0bd00eSMatthew N. Dodd     case 'D': /* Relative timestamps */
1710d0bd00eSMatthew N. Dodd       trussinfo->flags |= RELATIVETIMESTAMPS;
1720d0bd00eSMatthew N. Dodd       break;
173bbeaf6c0SSean Eric Fagan     case 'o':	/* Specified output file */
1743cf51049SPhilippe Charnier       fname = optarg;
175bbeaf6c0SSean Eric Fagan       break;
176bbeaf6c0SSean Eric Fagan     case 'S':	/* Don't trace signals */
177ec0bed25SMatthew N. Dodd       trussinfo->flags |= NOSIGS;
178bbeaf6c0SSean Eric Fagan       break;
179bbeaf6c0SSean Eric Fagan     default:
180bbeaf6c0SSean Eric Fagan       usage();
181bbeaf6c0SSean Eric Fagan     }
182bbeaf6c0SSean Eric Fagan   }
183bbeaf6c0SSean Eric Fagan 
184bbeaf6c0SSean Eric Fagan   ac -= optind; av += optind;
185ec0bed25SMatthew N. Dodd   if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0))
186bbeaf6c0SSean Eric Fagan     usage();
187bbeaf6c0SSean Eric Fagan 
1883cf51049SPhilippe Charnier   if (fname != NULL) { /* Use output file */
189ec0bed25SMatthew N. Dodd     if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
1903cf51049SPhilippe Charnier       errx(1, "cannot open %s", fname);
1913cf51049SPhilippe Charnier   }
1923cf51049SPhilippe Charnier 
193bbeaf6c0SSean Eric Fagan   /*
194bbeaf6c0SSean Eric Fagan    * If truss starts the process itself, it will ignore some signals --
195bbeaf6c0SSean Eric Fagan    * they should be passed off to the process, which may or may not
196bbeaf6c0SSean Eric Fagan    * exit.  If, however, we are examining an already-running process,
197bbeaf6c0SSean Eric Fagan    * then we restore the event mask on these same signals.
198bbeaf6c0SSean Eric Fagan    */
199bbeaf6c0SSean Eric Fagan 
200ec0bed25SMatthew N. Dodd   if (trussinfo->pid == 0) {	/* Start a command ourselves */
201bbeaf6c0SSean Eric Fagan     command = av;
202ec0bed25SMatthew N. Dodd     trussinfo->pid = setup_and_wait(command);
203bbeaf6c0SSean Eric Fagan     signal(SIGINT, SIG_IGN);
204bbeaf6c0SSean Eric Fagan     signal(SIGTERM, SIG_IGN);
205bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, SIG_IGN);
206bbeaf6c0SSean Eric Fagan   } else {
207bbeaf6c0SSean Eric Fagan     signal(SIGINT, restore_proc);
208bbeaf6c0SSean Eric Fagan     signal(SIGTERM, restore_proc);
209bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, restore_proc);
210bbeaf6c0SSean Eric Fagan   }
211bbeaf6c0SSean Eric Fagan 
212bbeaf6c0SSean Eric Fagan 
213bbeaf6c0SSean Eric Fagan   /*
214bbeaf6c0SSean Eric Fagan    * At this point, if we started the process, it is stopped waiting to
215bbeaf6c0SSean Eric Fagan    * be woken up, either in exit() or in execve().
216bbeaf6c0SSean Eric Fagan    */
217bbeaf6c0SSean Eric Fagan 
218c03bfcc8SMatthew N. Dodd START_TRACE:
219ec0bed25SMatthew N. Dodd   Procfd = start_tracing(
220ec0bed25SMatthew N. Dodd 		trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
221c03bfcc8SMatthew N. Dodd 		((trussinfo->flags & NOSIGS) ? 0 : S_SIG),
222c03bfcc8SMatthew N. Dodd 		((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0));
22389361835SSean Eric Fagan   if (Procfd == -1)
22489361835SSean Eric Fagan     return 0;
22589361835SSean Eric Fagan 
226bbeaf6c0SSean Eric Fagan   pfs.why = 0;
227bbeaf6c0SSean Eric Fagan 
228ec0bed25SMatthew N. Dodd   funcs = set_etype(trussinfo);
229bbeaf6c0SSean Eric Fagan   /*
230bbeaf6c0SSean Eric Fagan    * At this point, it's a simple loop, waiting for the process to
231bbeaf6c0SSean Eric Fagan    * stop, finding out why, printing out why, and then continuing it.
232bbeaf6c0SSean Eric Fagan    * All of the grunt work is done in the support routines.
233bbeaf6c0SSean Eric Fagan    */
234bbeaf6c0SSean Eric Fagan 
235203098d8SMatthew N. Dodd   clock_gettime(CLOCK_REALTIME, &trussinfo->start_time);
2360d0bd00eSMatthew N. Dodd 
237bbeaf6c0SSean Eric Fagan   do {
238bbeaf6c0SSean Eric Fagan     int val = 0;
239bbeaf6c0SSean Eric Fagan 
240bbeaf6c0SSean Eric Fagan     if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
2413cf51049SPhilippe Charnier       warn("PIOCWAIT top of loop");
242bbeaf6c0SSean Eric Fagan     else {
243bbeaf6c0SSean Eric Fagan       switch(i = pfs.why) {
244bbeaf6c0SSean Eric Fagan       case S_SCE:
245ec0bed25SMatthew N. Dodd 	funcs->enter_syscall(trussinfo, pfs.val);
246203098d8SMatthew N. Dodd 	clock_gettime(CLOCK_REALTIME, &trussinfo->before);
247bbeaf6c0SSean Eric Fagan 	break;
248bbeaf6c0SSean Eric Fagan       case S_SCX:
249203098d8SMatthew N. Dodd 	clock_gettime(CLOCK_REALTIME, &trussinfo->after);
250bbeaf6c0SSean Eric Fagan 	/*
251bbeaf6c0SSean Eric Fagan 	 * This is so we don't get two messages for an exec -- one
252bbeaf6c0SSean Eric Fagan 	 * for the S_EXEC, and one for the syscall exit.  It also,
253bbeaf6c0SSean Eric Fagan 	 * conveniently, ensures that the first message printed out
254bbeaf6c0SSean Eric Fagan 	 * isn't the return-from-syscall used to create the process.
255bbeaf6c0SSean Eric Fagan 	 */
256bbeaf6c0SSean Eric Fagan 
257bbeaf6c0SSean Eric Fagan 	if (in_exec) {
258bbeaf6c0SSean Eric Fagan 	  in_exec = 0;
259bbeaf6c0SSean Eric Fagan 	  break;
260bbeaf6c0SSean Eric Fagan 	}
261c03bfcc8SMatthew N. Dodd 
262c03bfcc8SMatthew N. Dodd 	if (trussinfo->in_fork && (trussinfo->flags & FOLLOWFORKS)) {
263c03bfcc8SMatthew N. Dodd 	  int childpid;
264c03bfcc8SMatthew N. Dodd 
265c03bfcc8SMatthew N. Dodd 	  trussinfo->in_fork = 0;
266c03bfcc8SMatthew N. Dodd 	  childpid = funcs->exit_syscall(trussinfo, pfs.val);
267c03bfcc8SMatthew N. Dodd 
268c03bfcc8SMatthew N. Dodd 	  /*
269c03bfcc8SMatthew N. Dodd 	   * Fork a new copy of ourself to trace the child of the
270c03bfcc8SMatthew N. Dodd 	   * original traced process.
271c03bfcc8SMatthew N. Dodd 	   */
272c03bfcc8SMatthew N. Dodd 	  if (fork() == 0) {
273c03bfcc8SMatthew N. Dodd 	    trussinfo->pid = childpid;
274c03bfcc8SMatthew N. Dodd 	    goto START_TRACE;
275c03bfcc8SMatthew N. Dodd 	  }
276c03bfcc8SMatthew N. Dodd 	  break;
277c03bfcc8SMatthew N. Dodd 	}
278ec0bed25SMatthew N. Dodd 	funcs->exit_syscall(trussinfo, pfs.val);
279bbeaf6c0SSean Eric Fagan 	break;
280bbeaf6c0SSean Eric Fagan       case S_SIG:
281ec0bed25SMatthew N. Dodd 	fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val);
2829a4902a9SMartin Cracauer 	sigexit = pfs.val;
283bbeaf6c0SSean Eric Fagan 	break;
284bbeaf6c0SSean Eric Fagan       case S_EXIT:
285ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val);
286bbeaf6c0SSean Eric Fagan 	break;
287bbeaf6c0SSean Eric Fagan       case S_EXEC:
288ec0bed25SMatthew N. Dodd 	funcs = set_etype(trussinfo);
289bbeaf6c0SSean Eric Fagan 	in_exec = 1;
290bbeaf6c0SSean Eric Fagan 	break;
291bbeaf6c0SSean Eric Fagan       default:
292ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "Process stopped because of:  %d\n", i);
293bbeaf6c0SSean Eric Fagan 	break;
294bbeaf6c0SSean Eric Fagan       }
295bbeaf6c0SSean Eric Fagan     }
29689361835SSean Eric Fagan     if (ioctl(Procfd, PIOCCONT, val) == -1) {
297ec0bed25SMatthew N. Dodd       if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
29889361835SSean Eric Fagan 	break;
29989361835SSean Eric Fagan       else
3003cf51049SPhilippe Charnier 	warn("PIOCCONT");
30189361835SSean Eric Fagan     }
302bbeaf6c0SSean Eric Fagan   } while (pfs.why != S_EXIT);
303ec0bed25SMatthew N. Dodd   fflush(trussinfo->outfile);
3049a4902a9SMartin Cracauer   if (sigexit) {
3059a4902a9SMartin Cracauer     if (sigexit == SIGQUIT)
3069a4902a9SMartin Cracauer       exit(sigexit);
3079a4902a9SMartin Cracauer     (void) signal(sigexit, SIG_DFL);
3089a4902a9SMartin Cracauer     (void) kill(getpid(), sigexit);
3099a4902a9SMartin Cracauer   }
310bbeaf6c0SSean Eric Fagan   return 0;
311bbeaf6c0SSean Eric Fagan }
312