xref: /freebsd/usr.bin/truss/main.c (revision ec0bed25ba4846eacd23f88e176d37615776fba5)
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",
703cf51049SPhilippe Charnier 	"usage: truss [-S] [-o file] -p pid",
713cf51049SPhilippe Charnier 	"       truss [-S] [-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 
148ad6af66eSJohn-Mark Gurney   while ((c = getopt(ac, av, "p:o:S")) != -1) {
149bbeaf6c0SSean Eric Fagan     switch (c) {
150bbeaf6c0SSean Eric Fagan     case 'p':	/* specified pid */
151ec0bed25SMatthew N. Dodd       trussinfo->pid = atoi(optarg);
152bbeaf6c0SSean Eric Fagan       break;
153bbeaf6c0SSean Eric Fagan     case 'o':	/* Specified output file */
1543cf51049SPhilippe Charnier       fname = optarg;
155bbeaf6c0SSean Eric Fagan       break;
156bbeaf6c0SSean Eric Fagan     case 'S':	/* Don't trace signals */
157ec0bed25SMatthew N. Dodd       trussinfo->flags |= NOSIGS;
158bbeaf6c0SSean Eric Fagan       break;
159bbeaf6c0SSean Eric Fagan     default:
160bbeaf6c0SSean Eric Fagan       usage();
161bbeaf6c0SSean Eric Fagan     }
162bbeaf6c0SSean Eric Fagan   }
163bbeaf6c0SSean Eric Fagan 
164bbeaf6c0SSean Eric Fagan   ac -= optind; av += optind;
165ec0bed25SMatthew N. Dodd   if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0))
166bbeaf6c0SSean Eric Fagan     usage();
167bbeaf6c0SSean Eric Fagan 
1683cf51049SPhilippe Charnier   if (fname != NULL) { /* Use output file */
169ec0bed25SMatthew N. Dodd     if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
1703cf51049SPhilippe Charnier       errx(1, "cannot open %s", fname);
1713cf51049SPhilippe Charnier   }
1723cf51049SPhilippe Charnier 
173bbeaf6c0SSean Eric Fagan   /*
174bbeaf6c0SSean Eric Fagan    * If truss starts the process itself, it will ignore some signals --
175bbeaf6c0SSean Eric Fagan    * they should be passed off to the process, which may or may not
176bbeaf6c0SSean Eric Fagan    * exit.  If, however, we are examining an already-running process,
177bbeaf6c0SSean Eric Fagan    * then we restore the event mask on these same signals.
178bbeaf6c0SSean Eric Fagan    */
179bbeaf6c0SSean Eric Fagan 
180ec0bed25SMatthew N. Dodd   if (trussinfo->pid == 0) {	/* Start a command ourselves */
181bbeaf6c0SSean Eric Fagan     command = av;
182ec0bed25SMatthew N. Dodd     trussinfo->pid = setup_and_wait(command);
183bbeaf6c0SSean Eric Fagan     signal(SIGINT, SIG_IGN);
184bbeaf6c0SSean Eric Fagan     signal(SIGTERM, SIG_IGN);
185bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, SIG_IGN);
186bbeaf6c0SSean Eric Fagan   } else {
187bbeaf6c0SSean Eric Fagan     signal(SIGINT, restore_proc);
188bbeaf6c0SSean Eric Fagan     signal(SIGTERM, restore_proc);
189bbeaf6c0SSean Eric Fagan     signal(SIGQUIT, restore_proc);
190bbeaf6c0SSean Eric Fagan   }
191bbeaf6c0SSean Eric Fagan 
192bbeaf6c0SSean Eric Fagan 
193bbeaf6c0SSean Eric Fagan   /*
194bbeaf6c0SSean Eric Fagan    * At this point, if we started the process, it is stopped waiting to
195bbeaf6c0SSean Eric Fagan    * be woken up, either in exit() or in execve().
196bbeaf6c0SSean Eric Fagan    */
197bbeaf6c0SSean Eric Fagan 
198ec0bed25SMatthew N. Dodd   Procfd = start_tracing(
199ec0bed25SMatthew N. Dodd 		trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
200ec0bed25SMatthew N. Dodd 		((trussinfo->flags & NOSIGS) ? 0 : S_SIG));
20189361835SSean Eric Fagan   if (Procfd == -1)
20289361835SSean Eric Fagan     return 0;
20389361835SSean Eric Fagan 
204bbeaf6c0SSean Eric Fagan   pfs.why = 0;
205bbeaf6c0SSean Eric Fagan 
206ec0bed25SMatthew N. Dodd   funcs = set_etype(trussinfo);
207bbeaf6c0SSean Eric Fagan   /*
208bbeaf6c0SSean Eric Fagan    * At this point, it's a simple loop, waiting for the process to
209bbeaf6c0SSean Eric Fagan    * stop, finding out why, printing out why, and then continuing it.
210bbeaf6c0SSean Eric Fagan    * All of the grunt work is done in the support routines.
211bbeaf6c0SSean Eric Fagan    */
212bbeaf6c0SSean Eric Fagan 
213bbeaf6c0SSean Eric Fagan   do {
214bbeaf6c0SSean Eric Fagan     int val = 0;
215bbeaf6c0SSean Eric Fagan 
216bbeaf6c0SSean Eric Fagan     if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
2173cf51049SPhilippe Charnier       warn("PIOCWAIT top of loop");
218bbeaf6c0SSean Eric Fagan     else {
219bbeaf6c0SSean Eric Fagan       switch(i = pfs.why) {
220bbeaf6c0SSean Eric Fagan       case S_SCE:
221ec0bed25SMatthew N. Dodd 	funcs->enter_syscall(trussinfo, pfs.val);
222bbeaf6c0SSean Eric Fagan 	break;
223bbeaf6c0SSean Eric Fagan       case S_SCX:
224bbeaf6c0SSean Eric Fagan 	/*
225bbeaf6c0SSean Eric Fagan 	 * This is so we don't get two messages for an exec -- one
226bbeaf6c0SSean Eric Fagan 	 * for the S_EXEC, and one for the syscall exit.  It also,
227bbeaf6c0SSean Eric Fagan 	 * conveniently, ensures that the first message printed out
228bbeaf6c0SSean Eric Fagan 	 * isn't the return-from-syscall used to create the process.
229bbeaf6c0SSean Eric Fagan 	 */
230bbeaf6c0SSean Eric Fagan 
231bbeaf6c0SSean Eric Fagan 	if (in_exec) {
232bbeaf6c0SSean Eric Fagan 	  in_exec = 0;
233bbeaf6c0SSean Eric Fagan 	  break;
234bbeaf6c0SSean Eric Fagan 	}
235ec0bed25SMatthew N. Dodd 	funcs->exit_syscall(trussinfo, pfs.val);
236bbeaf6c0SSean Eric Fagan 	break;
237bbeaf6c0SSean Eric Fagan       case S_SIG:
238ec0bed25SMatthew N. Dodd 	fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val);
2399a4902a9SMartin Cracauer 	sigexit = pfs.val;
240bbeaf6c0SSean Eric Fagan 	break;
241bbeaf6c0SSean Eric Fagan       case S_EXIT:
242ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val);
243bbeaf6c0SSean Eric Fagan 	break;
244bbeaf6c0SSean Eric Fagan       case S_EXEC:
245ec0bed25SMatthew N. Dodd 	funcs = set_etype(trussinfo);
246bbeaf6c0SSean Eric Fagan 	in_exec = 1;
247bbeaf6c0SSean Eric Fagan 	break;
248bbeaf6c0SSean Eric Fagan       default:
249ec0bed25SMatthew N. Dodd 	fprintf (trussinfo->outfile, "Process stopped because of:  %d\n", i);
250bbeaf6c0SSean Eric Fagan 	break;
251bbeaf6c0SSean Eric Fagan       }
252bbeaf6c0SSean Eric Fagan     }
25389361835SSean Eric Fagan     if (ioctl(Procfd, PIOCCONT, val) == -1) {
254ec0bed25SMatthew N. Dodd       if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
25589361835SSean Eric Fagan 	break;
25689361835SSean Eric Fagan       else
2573cf51049SPhilippe Charnier 	warn("PIOCCONT");
25889361835SSean Eric Fagan     }
259bbeaf6c0SSean Eric Fagan   } while (pfs.why != S_EXIT);
260ec0bed25SMatthew N. Dodd   fflush(trussinfo->outfile);
2619a4902a9SMartin Cracauer   if (sigexit) {
2629a4902a9SMartin Cracauer     if (sigexit == SIGQUIT)
2639a4902a9SMartin Cracauer       exit(sigexit);
2649a4902a9SMartin Cracauer     (void) signal(sigexit, SIG_DFL);
2659a4902a9SMartin Cracauer     (void) kill(getpid(), sigexit);
2669a4902a9SMartin Cracauer   }
267bbeaf6c0SSean Eric Fagan   return 0;
268bbeaf6c0SSean Eric Fagan }
269