xref: /freebsd/usr.bin/truss/main.c (revision a220d00e74dd245b4fca59c5eca0c53963686325)
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   "$FreeBSD$";
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 <sys/param.h>
44 #include <sys/ioctl.h>
45 #include <sys/pioctl.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 extern int setup_and_wait(char **);
57 extern int start_tracing(int, int);
58 #ifdef __alpha__
59 extern void alpha_syscall_entry(int, int);
60 extern void alpha_syscall_exit(int, int);
61 #endif
62 #ifdef __i386__
63 extern void i386_syscall_entry(int, int);
64 extern void i386_syscall_exit(int, int);
65 extern void i386_linux_syscall_entry(int, int);
66 extern void i386_linux_syscall_exit(int, int);
67 #endif
68 
69 /*
70  * These should really be parameterized -- I don't like having globals,
71  * but this is the easiest way, right now, to deal with them.
72  */
73 
74 int pid = 0;
75 int nosigs = 0;
76 FILE *outfile;
77 int Procfd;
78 char progtype[50];	/* OS and type of executable */
79 
80 static inline void
81 usage(void)
82 {
83   fprintf(stderr, "%s\n%s\n",
84 	"usage: truss [-S] [-o file] -p pid",
85 	"       truss [-S] [-o file] command [args]");
86   exit(1);
87 }
88 
89 /*
90  * WARNING! "FreeBSD a.out" must be first, or set_etype will not
91  * work correctly.
92  */
93 struct ex_types {
94   char *type;
95   void (*enter_syscall)(int, int);
96   void (*exit_syscall)(int, int);
97 } ex_types[] = {
98 #ifdef __alpha__
99   { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
100 #endif
101 #ifdef __i386__
102   { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
103   { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
104   { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
105 #endif
106   { 0, 0, 0 },
107 };
108 
109 /*
110  * Set the execution type.  This is called after every exec, and when
111  * a process is first monitored.  The procfs pseudo-file "etype" has
112  * the execution module type -- see /proc/curproc/etype for an example.
113  */
114 
115 static struct ex_types *
116 set_etype() {
117   struct ex_types *funcs;
118   char etype[24];
119   char progtype[32];
120   int fd;
121 
122   sprintf(etype, "/proc/%d/etype", pid);
123   if ((fd = open(etype, O_RDONLY)) == -1) {
124     strcpy(progtype, "FreeBSD a.out");
125   } else {
126     int len = read(fd, progtype, sizeof(progtype));
127     progtype[len-1] = '\0';
128     close(fd);
129   }
130 
131   for (funcs = ex_types; funcs->type; funcs++)
132     if (!strcmp(funcs->type, progtype))
133       break;
134 
135   if (funcs == NULL) {
136     warn("Execution type %s is not supported -- using FreeBSD a.out\n",
137       progtype);
138     funcs = &ex_types[0];
139   }
140   return funcs;
141 }
142 
143 int
144 main(int ac, char **av) {
145   int c;
146   int i;
147   char **command;
148   struct procfs_status pfs;
149   struct ex_types *funcs;
150   int in_exec = 0;
151   char *fname = NULL;
152   int sigexit = 0;
153 
154   outfile = stdout;
155   while ((c = getopt(ac, av, "p:o:S")) != -1) {
156     switch (c) {
157     case 'p':	/* specified pid */
158       pid = atoi(optarg);
159       break;
160     case 'o':	/* Specified output file */
161       fname = optarg;
162       break;
163     case 'S':	/* Don't trace signals */
164       nosigs = 1;
165       break;
166     default:
167       usage();
168     }
169   }
170 
171   ac -= optind; av += optind;
172   if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0))
173     usage();
174 
175   if (fname != NULL) { /* Use output file */
176     if ((outfile = fopen(fname, "w")) == NULL)
177       errx(1, "cannot open %s", fname);
178   }
179 
180   /*
181    * If truss starts the process itself, it will ignore some signals --
182    * they should be passed off to the process, which may or may not
183    * exit.  If, however, we are examining an already-running process,
184    * then we restore the event mask on these same signals.
185    */
186 
187   if (pid == 0) {	/* Start a command ourselves */
188     command = av;
189     pid = setup_and_wait(command);
190     signal(SIGINT, SIG_IGN);
191     signal(SIGTERM, SIG_IGN);
192     signal(SIGQUIT, SIG_IGN);
193   } else {
194     extern void restore_proc(int);
195     signal(SIGINT, restore_proc);
196     signal(SIGTERM, restore_proc);
197     signal(SIGQUIT, restore_proc);
198   }
199 
200 
201   /*
202    * At this point, if we started the process, it is stopped waiting to
203    * be woken up, either in exit() or in execve().
204    */
205 
206   Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
207 		     (nosigs ? 0 : S_SIG));
208   if (Procfd == -1)
209     return 0;
210 
211   pfs.why = 0;
212 
213   funcs = set_etype();
214   /*
215    * At this point, it's a simple loop, waiting for the process to
216    * stop, finding out why, printing out why, and then continuing it.
217    * All of the grunt work is done in the support routines.
218    */
219 
220   do {
221     int val = 0;
222 
223     if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
224       warn("PIOCWAIT top of loop");
225     else {
226       switch(i = pfs.why) {
227       case S_SCE:
228 	funcs->enter_syscall(pid, pfs.val);
229 	break;
230       case S_SCX:
231 	/*
232 	 * This is so we don't get two messages for an exec -- one
233 	 * for the S_EXEC, and one for the syscall exit.  It also,
234 	 * conveniently, ensures that the first message printed out
235 	 * isn't the return-from-syscall used to create the process.
236 	 */
237 
238 	if (in_exec) {
239 	  in_exec = 0;
240 	  break;
241 	}
242 	funcs->exit_syscall(pid, pfs.val);
243 	break;
244       case S_SIG:
245 	fprintf(outfile, "SIGNAL %lu\n", pfs.val);
246 	sigexit = pfs.val;
247 	break;
248       case S_EXIT:
249 	fprintf (outfile, "process exit, rval = %lu\n", pfs.val);
250 	break;
251       case S_EXEC:
252 	funcs = set_etype();
253 	in_exec = 1;
254 	break;
255       default:
256 	fprintf (outfile, "Process stopped because of:  %d\n", i);
257 	break;
258       }
259     }
260     if (ioctl(Procfd, PIOCCONT, val) == -1) {
261       if (kill(pid, 0) == -1 && errno == ESRCH)
262 	break;
263       else
264 	warn("PIOCCONT");
265     }
266   } while (pfs.why != S_EXIT);
267   fflush(outfile);
268   if (sigexit) {
269     if (sigexit == SIGQUIT)
270       exit(sigexit);
271     (void) signal(sigexit, SIG_DFL);
272     (void) kill(getpid(), sigexit);
273   }
274   return 0;
275 }
276