main.c revision 41970
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[] = 3441970Smsmith "$Id: main.c,v 1.12 1998/10/03 00:43:05 sef Exp $"; 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 4332275Scharnier#include <err.h> 4432275Scharnier#include <errno.h> 4532275Scharnier#include <fcntl.h> 4632275Scharnier#include <signal.h> 4731567Ssef#include <stdio.h> 4831567Ssef#include <stdlib.h> 4931567Ssef#include <string.h> 5031579Speter#include <unistd.h> 5131567Ssef#include <sys/ioctl.h> 5231567Ssef#include <sys/pioctl.h> 5331567Ssef 5431567Ssefextern int setup_and_wait(char **); 5531567Ssefextern int start_tracing(int, int); 5639908Ssef#ifdef __alpha__ 5739908Ssefextern void alpha_syscall_entry(int, int); 5839908Ssefextern void alpha_syscall_exit(int, int); 5939908Ssef#endif 6039908Ssef#ifdef __i386__ 6131567Ssefextern void i386_syscall_entry(int, int); 6231567Ssefextern void i386_syscall_exit(int, int); 6331567Ssefextern void i386_linux_syscall_entry(int, int); 6431567Ssefextern void i386_linux_syscall_exit(int, int); 6539908Ssef#endif 6631567Ssef 6731567Ssef/* 6831567Ssef * These should really be parameterized -- I don't like having globals, 6931567Ssef * but this is the easiest way, right now, to deal with them. 7031567Ssef */ 7131567Ssef 7231567Ssefint pid = 0; 7331567Ssefint nosigs = 0; 7431567SsefFILE *outfile = stderr; 7531567Ssefint Procfd; 7631567Ssefchar progtype[50]; /* OS and type of executable */ 7731567Ssef 7831567Ssefstatic inline void 7932275Scharnierusage(void) 8032275Scharnier{ 8132275Scharnier fprintf(stderr, "%s\n%s\n", 8232275Scharnier "usage: truss [-S] [-o file] -p pid", 8332275Scharnier " truss [-S] [-o file] command [args]"); 8431567Ssef exit(1); 8531567Ssef} 8631567Ssef 8738897Ssef/* 8838897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not 8938897Ssef * work correctly. 9038897Ssef */ 9131567Ssefstruct ex_types { 9231567Ssef char *type; 9331567Ssef void (*enter_syscall)(int, int); 9431567Ssef void (*exit_syscall)(int, int); 9531567Ssef} ex_types[] = { 9639908Ssef#ifdef __alpha__ 9739908Ssef { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit }, 9839908Ssef#endif 9939908Ssef#ifdef __i386__ 10031567Ssef { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 10131580Speter { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 10231567Ssef { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 10339908Ssef#endif 10431567Ssef { 0, 0, 0 }, 10531567Ssef}; 10631567Ssef 10731567Ssef/* 10831567Ssef * Set the execution type. This is called after every exec, and when 10931567Ssef * a process is first monitored. The procfs pseudo-file "etype" has 11031567Ssef * the execution module type -- see /proc/curproc/etype for an example. 11131567Ssef */ 11231567Ssef 11331567Ssefstatic struct ex_types * 11431567Ssefset_etype() { 11531567Ssef struct ex_types *funcs; 11631567Ssef char etype[24]; 11731567Ssef char progtype[32]; 11831567Ssef int fd; 11931567Ssef 12031567Ssef sprintf(etype, "/proc/%d/etype", pid); 12131567Ssef if ((fd = open(etype, O_RDONLY)) == -1) { 12231567Ssef strcpy(progtype, "FreeBSD a.out"); 12331567Ssef } else { 12431567Ssef int len = read(fd, progtype, sizeof(progtype)); 12531567Ssef progtype[len-1] = '\0'; 12631580Speter close(fd); 12731567Ssef } 12831567Ssef 12931567Ssef for (funcs = ex_types; funcs->type; funcs++) 13031567Ssef if (!strcmp(funcs->type, progtype)) 13131567Ssef break; 13231567Ssef 13338897Ssef if (funcs == NULL) { 13438897Ssef warn("Execution type %s is not supported -- using FreeBSD a.out\n", 13538897Ssef progtype); 13638897Ssef funcs = &ex_types[0]; 13738897Ssef } 13831567Ssef return funcs; 13931567Ssef} 14031567Ssef 14132275Scharnierint 14231567Ssefmain(int ac, char **av) { 14331567Ssef int c; 14431567Ssef int i; 14531567Ssef char **command; 14631567Ssef struct procfs_status pfs; 14731567Ssef struct ex_types *funcs; 14831567Ssef int in_exec = 0; 14932275Scharnier char *fname = NULL; 15038520Scracauer int sigexit = 0; 15131567Ssef 15232306Sjmg while ((c = getopt(ac, av, "p:o:S")) != -1) { 15331567Ssef switch (c) { 15431567Ssef case 'p': /* specified pid */ 15531567Ssef pid = atoi(optarg); 15631567Ssef break; 15731567Ssef case 'o': /* Specified output file */ 15832275Scharnier fname = optarg; 15931567Ssef break; 16031567Ssef case 'S': /* Don't trace signals */ 16131567Ssef nosigs = 1; 16231567Ssef break; 16331567Ssef default: 16431567Ssef usage(); 16531567Ssef } 16631567Ssef } 16731567Ssef 16831567Ssef ac -= optind; av += optind; 16931582Ssef if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0)) 17031567Ssef usage(); 17131567Ssef 17232275Scharnier if (fname != NULL) { /* Use output file */ 17332275Scharnier if ((outfile = fopen(fname, "w")) == NULL) 17432275Scharnier errx(1, "cannot open %s", fname); 17532275Scharnier } 17632275Scharnier 17731567Ssef /* 17831567Ssef * If truss starts the process itself, it will ignore some signals -- 17931567Ssef * they should be passed off to the process, which may or may not 18031567Ssef * exit. If, however, we are examining an already-running process, 18131567Ssef * then we restore the event mask on these same signals. 18231567Ssef */ 18331567Ssef 18431567Ssef if (pid == 0) { /* Start a command ourselves */ 18531567Ssef command = av; 18631567Ssef pid = setup_and_wait(command); 18731567Ssef signal(SIGINT, SIG_IGN); 18831567Ssef signal(SIGTERM, SIG_IGN); 18931567Ssef signal(SIGQUIT, SIG_IGN); 19031567Ssef } else { 19131567Ssef extern void restore_proc(int); 19231567Ssef signal(SIGINT, restore_proc); 19331567Ssef signal(SIGTERM, restore_proc); 19431567Ssef signal(SIGQUIT, restore_proc); 19531567Ssef } 19631567Ssef 19731567Ssef 19831567Ssef /* 19931567Ssef * At this point, if we started the process, it is stopped waiting to 20031567Ssef * be woken up, either in exit() or in execve(). 20131567Ssef */ 20231567Ssef 20331567Ssef Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT | 20431567Ssef (nosigs ? 0 : S_SIG)); 20531567Ssef pfs.why = 0; 20631567Ssef 20731567Ssef funcs = set_etype(); 20831567Ssef /* 20931567Ssef * At this point, it's a simple loop, waiting for the process to 21031567Ssef * stop, finding out why, printing out why, and then continuing it. 21131567Ssef * All of the grunt work is done in the support routines. 21231567Ssef */ 21331567Ssef 21431567Ssef do { 21531567Ssef int val = 0; 21631567Ssef 21731567Ssef if (ioctl(Procfd, PIOCWAIT, &pfs) == -1) 21832275Scharnier warn("PIOCWAIT top of loop"); 21931567Ssef else { 22031567Ssef switch(i = pfs.why) { 22131567Ssef case S_SCE: 22231567Ssef funcs->enter_syscall(pid, pfs.val); 22331567Ssef break; 22431567Ssef case S_SCX: 22531567Ssef /* 22631567Ssef * This is so we don't get two messages for an exec -- one 22731567Ssef * for the S_EXEC, and one for the syscall exit. It also, 22831567Ssef * conveniently, ensures that the first message printed out 22931567Ssef * isn't the return-from-syscall used to create the process. 23031567Ssef */ 23131567Ssef 23231567Ssef if (in_exec) { 23331567Ssef in_exec = 0; 23431567Ssef break; 23531567Ssef } 23631567Ssef funcs->exit_syscall(pid, pfs.val); 23731567Ssef break; 23831567Ssef case S_SIG: 23937453Sbde fprintf(outfile, "SIGNAL %lu\n", pfs.val); 24038520Scracauer sigexit = pfs.val; 24131567Ssef break; 24231567Ssef case S_EXIT: 24337453Sbde fprintf (outfile, "process exit, rval = %lu\n", pfs.val); 24431567Ssef break; 24531567Ssef case S_EXEC: 24631567Ssef funcs = set_etype(); 24731567Ssef in_exec = 1; 24831567Ssef break; 24931567Ssef default: 25031567Ssef fprintf (outfile, "Process stopped because of: %d\n", i); 25131567Ssef break; 25231567Ssef } 25331567Ssef } 25431691Ssef if (ioctl(Procfd, PIOCCONT, val) == -1) 25532275Scharnier warn("PIOCCONT"); 25631567Ssef } while (pfs.why != S_EXIT); 25741970Smsmith fflush(outfile); 25838520Scracauer if (sigexit) { 25938520Scracauer if (sigexit == SIGQUIT) 26038520Scracauer exit(sigexit); 26138520Scracauer (void) signal(sigexit, SIG_DFL); 26238520Scracauer (void) kill(getpid(), sigexit); 26338520Scracauer } 26431567Ssef return 0; 26531567Ssef} 266