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