xref: /freebsd/usr.bin/truss/main.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*
2  * Copryight 1997 Sean Eric Fagan
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *	This product includes software developed by Sean Eric Fagan
15  * 4. Neither the name of the author may be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char rcsid[] =
34 	"$Id: main.c,v 1.10 1998/08/24 10:17:20 cracauer Exp $";
35 #endif /* not lint */
36 
37 /*
38  * The main module for truss.  Suprisingly simple, but, then, the other
39  * files handle the bulk of the work.  And, of course, the kernel has to
40  * do a lot of the work :).
41  */
42 
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <sys/ioctl.h>
52 #include <sys/pioctl.h>
53 
54 extern int setup_and_wait(char **);
55 extern int start_tracing(int, int);
56 extern void i386_syscall_entry(int, int);
57 extern void i386_syscall_exit(int, int);
58 extern void i386_linux_syscall_entry(int, int);
59 extern void i386_linux_syscall_exit(int, int);
60 
61 /*
62  * These should really be parameterized -- I don't like having globals,
63  * but this is the easiest way, right now, to deal with them.
64  */
65 
66 int pid = 0;
67 int nosigs = 0;
68 FILE *outfile = stderr;
69 int Procfd;
70 char progtype[50];	/* OS and type of executable */
71 
72 static inline void
73 usage(void)
74 {
75   fprintf(stderr, "%s\n%s\n",
76 	"usage: truss [-S] [-o file] -p pid",
77 	"       truss [-S] [-o file] command [args]");
78   exit(1);
79 }
80 
81 /*
82  * WARNING! "FreeBSD a.out" must be first, or set_etype will not
83  * work correctly.
84  */
85 struct ex_types {
86   char *type;
87   void (*enter_syscall)(int, int);
88   void (*exit_syscall)(int, int);
89 } ex_types[] = {
90   { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
91   { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
92   { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
93   { 0, 0, 0 },
94 };
95 
96 /*
97  * Set the execution type.  This is called after every exec, and when
98  * a process is first monitored.  The procfs pseudo-file "etype" has
99  * the execution module type -- see /proc/curproc/etype for an example.
100  */
101 
102 static struct ex_types *
103 set_etype() {
104   struct ex_types *funcs;
105   char etype[24];
106   char progtype[32];
107   int fd;
108 
109   sprintf(etype, "/proc/%d/etype", pid);
110   if ((fd = open(etype, O_RDONLY)) == -1) {
111     strcpy(progtype, "FreeBSD a.out");
112   } else {
113     int len = read(fd, progtype, sizeof(progtype));
114     progtype[len-1] = '\0';
115     close(fd);
116   }
117 
118   for (funcs = ex_types; funcs->type; funcs++)
119     if (!strcmp(funcs->type, progtype))
120       break;
121 
122   if (funcs == NULL) {
123     warn("Execution type %s is not supported -- using FreeBSD a.out\n",
124       progtype);
125     funcs = &ex_types[0];
126   }
127   return funcs;
128 }
129 
130 int
131 main(int ac, char **av) {
132   int c;
133   int i;
134   char **command;
135   struct procfs_status pfs;
136   struct ex_types *funcs;
137   int in_exec = 0;
138   char *fname = NULL;
139   int sigexit = 0;
140 
141   while ((c = getopt(ac, av, "p:o:S")) != -1) {
142     switch (c) {
143     case 'p':	/* specified pid */
144       pid = atoi(optarg);
145       break;
146     case 'o':	/* Specified output file */
147       fname = optarg;
148       break;
149     case 'S':	/* Don't trace signals */
150       nosigs = 1;
151       break;
152     default:
153       usage();
154     }
155   }
156 
157   ac -= optind; av += optind;
158   if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0))
159     usage();
160 
161   if (fname != NULL) { /* Use output file */
162     if ((outfile = fopen(fname, "w")) == NULL)
163       errx(1, "cannot open %s", fname);
164   }
165 
166   /*
167    * If truss starts the process itself, it will ignore some signals --
168    * they should be passed off to the process, which may or may not
169    * exit.  If, however, we are examining an already-running process,
170    * then we restore the event mask on these same signals.
171    */
172 
173   if (pid == 0) {	/* Start a command ourselves */
174     command = av;
175     pid = setup_and_wait(command);
176     signal(SIGINT, SIG_IGN);
177     signal(SIGTERM, SIG_IGN);
178     signal(SIGQUIT, SIG_IGN);
179   } else {
180     extern void restore_proc(int);
181     signal(SIGINT, restore_proc);
182     signal(SIGTERM, restore_proc);
183     signal(SIGQUIT, restore_proc);
184   }
185 
186 
187   /*
188    * At this point, if we started the process, it is stopped waiting to
189    * be woken up, either in exit() or in execve().
190    */
191 
192   Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
193 		     (nosigs ? 0 : S_SIG));
194   pfs.why = 0;
195 
196   funcs = set_etype();
197   /*
198    * At this point, it's a simple loop, waiting for the process to
199    * stop, finding out why, printing out why, and then continuing it.
200    * All of the grunt work is done in the support routines.
201    */
202 
203   do {
204     int val = 0;
205 
206     if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
207       warn("PIOCWAIT top of loop");
208     else {
209       switch(i = pfs.why) {
210       case S_SCE:
211 	funcs->enter_syscall(pid, pfs.val);
212 	break;
213       case S_SCX:
214 	/*
215 	 * This is so we don't get two messages for an exec -- one
216 	 * for the S_EXEC, and one for the syscall exit.  It also,
217 	 * conveniently, ensures that the first message printed out
218 	 * isn't the return-from-syscall used to create the process.
219 	 */
220 
221 	if (in_exec) {
222 	  in_exec = 0;
223 	  break;
224 	}
225 	funcs->exit_syscall(pid, pfs.val);
226 	break;
227       case S_SIG:
228 	fprintf(outfile, "SIGNAL %lu\n", pfs.val);
229 	sigexit = pfs.val;
230 	break;
231       case S_EXIT:
232 	fprintf (outfile, "process exit, rval = %lu\n", pfs.val);
233 	break;
234       case S_EXEC:
235 	funcs = set_etype();
236 	in_exec = 1;
237 	break;
238       default:
239 	fprintf (outfile, "Process stopped because of:  %d\n", i);
240 	break;
241       }
242     }
243     if (ioctl(Procfd, PIOCCONT, val) == -1)
244       warn("PIOCCONT");
245   } while (pfs.why != S_EXIT);
246   if (sigexit) {
247     if (sigexit == SIGQUIT)
248       exit(sigexit);
249     (void) signal(sigexit, SIG_DFL);
250     (void) kill(getpid(), sigexit);
251   }
252   return 0;
253 }
254