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