main.c revision 101285
131567Ssef/*
231899Ssef * Copryight 1997 Sean Eric Fagan
331899Ssef *
431899Ssef * Redistribution and use in source and binary forms, with or without
531899Ssef * modification, are permitted provided that the following conditions
631899Ssef * are met:
731899Ssef * 1. Redistributions of source code must retain the above copyright
831899Ssef *    notice, this list of conditions and the following disclaimer.
931899Ssef * 2. Redistributions in binary form must reproduce the above copyright
1031899Ssef *    notice, this list of conditions and the following disclaimer in the
1131899Ssef *    documentation and/or other materials provided with the distribution.
1231899Ssef * 3. All advertising materials mentioning features or use of this software
1331899Ssef *    must display the following acknowledgement:
1431899Ssef *	This product includes software developed by Sean Eric Fagan
1531899Ssef * 4. Neither the name of the author may be used to endorse or promote
1631899Ssef *    products derived from this software without specific prior written
1731899Ssef *    permission.
1831899Ssef *
1931899Ssef * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2031899Ssef * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2131899Ssef * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2231899Ssef * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2331899Ssef * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2431899Ssef * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2531899Ssef * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2631899Ssef * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2731899Ssef * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2831899Ssef * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2931899Ssef * SUCH DAMAGE.
3031899Ssef */
3131899Ssef
3232275Scharnier#ifndef lint
3332275Scharnierstatic const char rcsid[] =
3450477Speter  "$FreeBSD: head/usr.bin/truss/main.c 101285 2002-08-04 01:27:31Z mdodd $";
3532275Scharnier#endif /* not lint */
3632275Scharnier
3731899Ssef/*
3831567Ssef * The main module for truss.  Suprisingly simple, but, then, the other
3931567Ssef * files handle the bulk of the work.  And, of course, the kernel has to
4031567Ssef * do a lot of the work :).
4131567Ssef */
4231567Ssef
4385301Sdes#include <sys/param.h>
4485301Sdes#include <sys/ioctl.h>
4585301Sdes#include <sys/pioctl.h>
4685301Sdes
4732275Scharnier#include <err.h>
4832275Scharnier#include <errno.h>
4932275Scharnier#include <fcntl.h>
5032275Scharnier#include <signal.h>
5131567Ssef#include <stdio.h>
5231567Ssef#include <stdlib.h>
5331567Ssef#include <string.h>
5431579Speter#include <unistd.h>
5531567Ssef
56101282Smdodd#include "truss.h"
5787703Smarkm#include "extern.h"
5831567Ssef
5931567Ssef/*
60101282Smdodd * It's difficult to parameterize this because it must be
61101282Smdodd * accessible in a signal handler.
6231567Ssef */
6331567Ssef
6431567Ssefint Procfd;
6531567Ssef
66100357Smarkmstatic __inline void
6732275Scharnierusage(void)
6832275Scharnier{
6932275Scharnier  fprintf(stderr, "%s\n%s\n",
70101285Smdodd	"usage: truss [-fdDS] [-o file] -p pid",
71101285Smdodd	"       truss [-fdDS] [-o file] command [args]");
7231567Ssef  exit(1);
7331567Ssef}
7431567Ssef
7538897Ssef/*
7638897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not
7738897Ssef * work correctly.
7838897Ssef */
7931567Ssefstruct ex_types {
8087703Smarkm  const char *type;
81101282Smdodd  void (*enter_syscall)(struct trussinfo *, int);
82101282Smdodd  int (*exit_syscall)(struct trussinfo *, int);
8331567Ssef} ex_types[] = {
8439908Ssef#ifdef __alpha__
8539908Ssef  { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit },
8639908Ssef#endif
8739908Ssef#ifdef __i386__
8831567Ssef  { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit },
8931580Speter  { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit },
9031567Ssef  { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit },
9139908Ssef#endif
9231567Ssef  { 0, 0, 0 },
9331567Ssef};
9431567Ssef
9531567Ssef/*
9631567Ssef * Set the execution type.  This is called after every exec, and when
9731567Ssef * a process is first monitored.  The procfs pseudo-file "etype" has
9831567Ssef * the execution module type -- see /proc/curproc/etype for an example.
9931567Ssef */
10031567Ssef
10131567Ssefstatic struct ex_types *
102101282Smdoddset_etype(struct trussinfo *trussinfo) {
10331567Ssef  struct ex_types *funcs;
10431567Ssef  char etype[24];
10587703Smarkm  char progt[32];
10631567Ssef  int fd;
10731567Ssef
108101282Smdodd  sprintf(etype, "/proc/%d/etype", trussinfo->pid);
10931567Ssef  if ((fd = open(etype, O_RDONLY)) == -1) {
11087703Smarkm    strcpy(progt, "FreeBSD a.out");
11131567Ssef  } else {
11287703Smarkm    int len = read(fd, progt, sizeof(progt));
11387703Smarkm    progt[len-1] = '\0';
11431580Speter    close(fd);
11531567Ssef  }
11631567Ssef
11731567Ssef  for (funcs = ex_types; funcs->type; funcs++)
11887703Smarkm    if (!strcmp(funcs->type, progt))
11931567Ssef      break;
12031567Ssef
12190401Sdes  if (funcs->type == NULL) {
12238897Ssef    funcs = &ex_types[0];
12390401Sdes    warn("Execution type %s is not supported -- using %s\n",
12490401Sdes      progt, funcs->type);
12538897Ssef  }
12631567Ssef  return funcs;
12731567Ssef}
12831567Ssef
12932275Scharnierint
13031567Ssefmain(int ac, char **av) {
13131567Ssef  int c;
13231567Ssef  int i;
13331567Ssef  char **command;
13431567Ssef  struct procfs_status pfs;
13531567Ssef  struct ex_types *funcs;
13631567Ssef  int in_exec = 0;
13732275Scharnier  char *fname = NULL;
13838520Scracauer  int sigexit = 0;
139101282Smdodd  struct trussinfo *trussinfo;
14031567Ssef
141101282Smdodd  /* Initialize the trussinfo struct */
142101282Smdodd  trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo));
143101282Smdodd  if (trussinfo == NULL)
144101282Smdodd    errx(1, "malloc() failed");
145101282Smdodd  bzero(trussinfo, sizeof(struct trussinfo));
146101282Smdodd  trussinfo->outfile = stderr;
147101282Smdodd
148101285Smdodd  while ((c = getopt(ac, av, "p:o:fdDS")) != -1) {
14931567Ssef    switch (c) {
15031567Ssef    case 'p':	/* specified pid */
151101282Smdodd      trussinfo->pid = atoi(optarg);
15231567Ssef      break;
153101283Smdodd    case 'f': /* Follow fork()'s */
154101283Smdodd      trussinfo->flags |= FOLLOWFORKS;
155101283Smdodd      break;
156101285Smdodd    case 'd': /* Absolute timestamps */
157101285Smdodd      trussinfo->flags |= ABSOLUTETIMESTAMPS;
158101285Smdodd      break;
159101285Smdodd    case 'D': /* Relative timestamps */
160101285Smdodd      trussinfo->flags |= RELATIVETIMESTAMPS;
161101285Smdodd      break;
16231567Ssef    case 'o':	/* Specified output file */
16332275Scharnier      fname = optarg;
16431567Ssef      break;
16531567Ssef    case 'S':	/* Don't trace signals */
166101282Smdodd      trussinfo->flags |= NOSIGS;
16731567Ssef      break;
16831567Ssef    default:
16931567Ssef      usage();
17031567Ssef    }
17131567Ssef  }
17231567Ssef
17331567Ssef  ac -= optind; av += optind;
174101282Smdodd  if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0))
17531567Ssef    usage();
17631567Ssef
17732275Scharnier  if (fname != NULL) { /* Use output file */
178101282Smdodd    if ((trussinfo->outfile = fopen(fname, "w")) == NULL)
17932275Scharnier      errx(1, "cannot open %s", fname);
18032275Scharnier  }
18132275Scharnier
18231567Ssef  /*
18331567Ssef   * If truss starts the process itself, it will ignore some signals --
18431567Ssef   * they should be passed off to the process, which may or may not
18531567Ssef   * exit.  If, however, we are examining an already-running process,
18631567Ssef   * then we restore the event mask on these same signals.
18731567Ssef   */
18831567Ssef
189101282Smdodd  if (trussinfo->pid == 0) {	/* Start a command ourselves */
19031567Ssef    command = av;
191101282Smdodd    trussinfo->pid = setup_and_wait(command);
19231567Ssef    signal(SIGINT, SIG_IGN);
19331567Ssef    signal(SIGTERM, SIG_IGN);
19431567Ssef    signal(SIGQUIT, SIG_IGN);
19531567Ssef  } else {
19631567Ssef    signal(SIGINT, restore_proc);
19731567Ssef    signal(SIGTERM, restore_proc);
19831567Ssef    signal(SIGQUIT, restore_proc);
19931567Ssef  }
20031567Ssef
20131567Ssef
20231567Ssef  /*
20331567Ssef   * At this point, if we started the process, it is stopped waiting to
20431567Ssef   * be woken up, either in exit() or in execve().
20531567Ssef   */
20631567Ssef
207101283SmdoddSTART_TRACE:
208101282Smdodd  Procfd = start_tracing(
209101282Smdodd		trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT |
210101283Smdodd		((trussinfo->flags & NOSIGS) ? 0 : S_SIG),
211101283Smdodd		((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0));
21255707Ssef  if (Procfd == -1)
21355707Ssef    return 0;
21455707Ssef
21531567Ssef  pfs.why = 0;
21631567Ssef
217101282Smdodd  funcs = set_etype(trussinfo);
21831567Ssef  /*
21931567Ssef   * At this point, it's a simple loop, waiting for the process to
22031567Ssef   * stop, finding out why, printing out why, and then continuing it.
22131567Ssef   * All of the grunt work is done in the support routines.
22231567Ssef   */
22331567Ssef
224101285Smdodd  gettimeofday(&trussinfo->start_time, (struct timezone *)NULL);
225101285Smdodd
22631567Ssef  do {
22731567Ssef    int val = 0;
22831567Ssef
22931567Ssef    if (ioctl(Procfd, PIOCWAIT, &pfs) == -1)
23032275Scharnier      warn("PIOCWAIT top of loop");
23131567Ssef    else {
23231567Ssef      switch(i = pfs.why) {
23331567Ssef      case S_SCE:
234101282Smdodd	funcs->enter_syscall(trussinfo, pfs.val);
235101285Smdodd	gettimeofday(&trussinfo->before, (struct timezone *)NULL);
23631567Ssef	break;
23731567Ssef      case S_SCX:
238101285Smdodd	gettimeofday(&trussinfo->after, (struct timezone *)NULL);
23931567Ssef	/*
24031567Ssef	 * This is so we don't get two messages for an exec -- one
24131567Ssef	 * for the S_EXEC, and one for the syscall exit.  It also,
24231567Ssef	 * conveniently, ensures that the first message printed out
24331567Ssef	 * isn't the return-from-syscall used to create the process.
24431567Ssef	 */
24531567Ssef
24631567Ssef	if (in_exec) {
24731567Ssef	  in_exec = 0;
24831567Ssef	  break;
24931567Ssef	}
250101283Smdodd
251101283Smdodd	if (trussinfo->in_fork && (trussinfo->flags & FOLLOWFORKS)) {
252101283Smdodd	  int childpid;
253101283Smdodd
254101283Smdodd	  trussinfo->in_fork = 0;
255101283Smdodd	  childpid = funcs->exit_syscall(trussinfo, pfs.val);
256101283Smdodd
257101283Smdodd	  /*
258101283Smdodd	   * Fork a new copy of ourself to trace the child of the
259101283Smdodd	   * original traced process.
260101283Smdodd	   */
261101283Smdodd	  if (fork() == 0) {
262101283Smdodd	    trussinfo->pid = childpid;
263101283Smdodd	    goto START_TRACE;
264101283Smdodd	  }
265101283Smdodd	  break;
266101283Smdodd	}
267101282Smdodd	funcs->exit_syscall(trussinfo, pfs.val);
26831567Ssef	break;
26931567Ssef      case S_SIG:
270101282Smdodd	fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val);
27138520Scracauer	sigexit = pfs.val;
27231567Ssef	break;
27331567Ssef      case S_EXIT:
274101282Smdodd	fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val);
27531567Ssef	break;
27631567Ssef      case S_EXEC:
277101282Smdodd	funcs = set_etype(trussinfo);
27831567Ssef	in_exec = 1;
27931567Ssef	break;
28031567Ssef      default:
281101282Smdodd	fprintf (trussinfo->outfile, "Process stopped because of:  %d\n", i);
28231567Ssef	break;
28331567Ssef      }
28431567Ssef    }
28555707Ssef    if (ioctl(Procfd, PIOCCONT, val) == -1) {
286101282Smdodd      if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH)
28755707Ssef	break;
28855707Ssef      else
28955707Ssef	warn("PIOCCONT");
29055707Ssef    }
29131567Ssef  } while (pfs.why != S_EXIT);
292101282Smdodd  fflush(trussinfo->outfile);
29338520Scracauer  if (sigexit) {
29438520Scracauer    if (sigexit == SIGQUIT)
29538520Scracauer      exit(sigexit);
29638520Scracauer    (void) signal(sigexit, SIG_DFL);
29738520Scracauer    (void) kill(getpid(), sigexit);
29838520Scracauer  }
29931567Ssef  return 0;
30031567Ssef}
301