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