Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * $FreeBSD: src/usr.bin/truss/main.c,v 1.15.2.3 2002/05/16 23:41:23 peter Exp $
32  * $DragonFly: src/usr.bin/truss/main.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
33  */
34
35 /*
36  * The main module for truss.  Suprisingly simple, but, then, the other
37  * files handle the bulk of the work.  And, of course, the kernel has to
38  * do a lot of the work :).
39  */
40
41 #include <sys/param.h>
42 #include <sys/ioctl.h>
43 #include <sys/pioctl.h>
44
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include "extern.h"
55
56 /*
57  * These should really be parameterized -- I don't like having globals,
58  * but this is the easiest way, right now, to deal with them.
59  */
60
61 int pid = 0;
62 int nosigs = 0;
63 FILE *outfile;
64 int Procfd;
65
66 static inline void
67 usage(void)
68 {
69   fprintf(stderr, "%s\n%s\n",
70         "usage: truss [-S] [-o file] -p pid",
71         "       truss [-S] [-o file] command [args]");
72   exit(1);
73 }
74
75 /*
76  * WARNING! "FreeBSD a.out" must be first, or set_etype will not
77  * work correctly.
78  */
79 struct ex_types {
80   const char *type;
81   void (*enter_syscall)(int, int);
82   void (*exit_syscall)(int, int);
83 } ex_types[] = {
84 #ifdef __alpha__
85   { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
86 #endif
87 #ifdef __i386__
88   { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
89   { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
90   { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
91 #endif
92   { 0, 0, 0 },
93 };
94
95 /*
96  * Set the execution type.  This is called after every exec, and when
97  * a process is first monitored.  The procfs pseudo-file "etype" has
98  * the execution module type -- see /proc/curproc/etype for an example.
99  */
100
101 static struct ex_types *
102 set_etype(void) {
103   struct ex_types *funcs;
104   char etype[24];
105   char progt[32];
106   int fd;
107
108   sprintf(etype, "/proc/%d/etype", pid);
109   if ((fd = open(etype, O_RDONLY)) == -1) {
110     strcpy(progt, "FreeBSD a.out");
111   } else {
112     int len = read(fd, progt, sizeof(progt));
113     progt[len-1] = '\0';
114     close(fd);
115   }
116
117   for (funcs = ex_types; funcs->type; funcs++)
118     if (!strcmp(funcs->type, progt))
119       break;
120
121   if (funcs->type == NULL) {
122     funcs = &ex_types[0];
123     warn("Execution type %s is not supported -- using %s\n",
124       progt, funcs->type);
125   }
126   return funcs;
127 }
128
129 int
130 main(int ac, char **av) {
131   int c;
132   int i;
133   char **command;
134   struct procfs_status pfs;
135   struct ex_types *funcs;
136   int in_exec = 0;
137   char *fname = NULL;
138   int sigexit = 0;
139
140   outfile = stderr;
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     signal(SIGINT, restore_proc);
181     signal(SIGTERM, restore_proc);
182     signal(SIGQUIT, restore_proc);
183   }
184
185
186   /*
187    * At this point, if we started the process, it is stopped waiting to
188    * be woken up, either in exit() or in execve().
189    */
190
191   Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
192                      (nosigs ? 0 : S_SIG));
193   if (Procfd == -1)
194     return 0;
195
196   pfs.why = 0;
197
198   funcs = set_etype();
199   /*
200    * At this point, it's a simple loop, waiting for the process to
201    * stop, finding out why, printing out why, and then continuing it.
202    * All of the grunt work is done in the support routines.
203    */
204
205   do {
206     int val = 0;
207
208     if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
209       warn("PIOCWAIT top of loop");
210     else {
211       switch(i = pfs.why) {
212       case S_SCE:
213         funcs->enter_syscall(pid, pfs.val);
214         break;
215       case S_SCX:
216         /*
217          * This is so we don't get two messages for an exec -- one
218          * for the S_EXEC, and one for the syscall exit.  It also,
219          * conveniently, ensures that the first message printed out
220          * isn't the return-from-syscall used to create the process.
221          */
222
223         if (in_exec) {
224           in_exec = 0;
225           break;
226         }
227         funcs->exit_syscall(pid, pfs.val);
228         break;
229       case S_SIG:
230         fprintf(outfile, "SIGNAL %lu\n", pfs.val);
231         sigexit = pfs.val;
232         break;
233       case S_EXIT:
234         fprintf (outfile, "process exit, rval = %lu\n", pfs.val);
235         break;
236       case S_EXEC:
237         funcs = set_etype();
238         in_exec = 1;
239         break;
240       default:
241         fprintf (outfile, "Process stopped because of:  %d\n", i);
242         break;
243       }
244     }
245     if (ioctl(Procfd, PIOCCONT, val) == -1) {
246       if (kill(pid, 0) == -1 && errno == ESRCH)
247         break;
248       else
249         warn("PIOCCONT");
250     }
251   } while (pfs.why != S_EXIT);
252   fflush(outfile);
253   if (sigexit) {
254     if (sigexit == SIGQUIT)
255       exit(sigexit);
256     (void) signal(sigexit, SIG_DFL);
257     (void) kill(getpid(), sigexit);
258   }
259   return 0;
260 }