main.c revision 101283
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 101283 2002-08-04 01:02:52Z 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", 70101283Smdodd "usage: truss [-fS] [-o file] -p pid", 71101283Smdodd " truss [-fS] [-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 148101283Smdodd while ((c = getopt(ac, av, "p:o:fS")) != -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; 15631567Ssef case 'o': /* Specified output file */ 15732275Scharnier fname = optarg; 15831567Ssef break; 15931567Ssef case 'S': /* Don't trace signals */ 160101282Smdodd trussinfo->flags |= NOSIGS; 16131567Ssef break; 16231567Ssef default: 16331567Ssef usage(); 16431567Ssef } 16531567Ssef } 16631567Ssef 16731567Ssef ac -= optind; av += optind; 168101282Smdodd if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0)) 16931567Ssef usage(); 17031567Ssef 17132275Scharnier if (fname != NULL) { /* Use output file */ 172101282Smdodd if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 17332275Scharnier errx(1, "cannot open %s", fname); 17432275Scharnier } 17532275Scharnier 17631567Ssef /* 17731567Ssef * If truss starts the process itself, it will ignore some signals -- 17831567Ssef * they should be passed off to the process, which may or may not 17931567Ssef * exit. If, however, we are examining an already-running process, 18031567Ssef * then we restore the event mask on these same signals. 18131567Ssef */ 18231567Ssef 183101282Smdodd if (trussinfo->pid == 0) { /* Start a command ourselves */ 18431567Ssef command = av; 185101282Smdodd trussinfo->pid = setup_and_wait(command); 18631567Ssef signal(SIGINT, SIG_IGN); 18731567Ssef signal(SIGTERM, SIG_IGN); 18831567Ssef signal(SIGQUIT, SIG_IGN); 18931567Ssef } else { 19031567Ssef signal(SIGINT, restore_proc); 19131567Ssef signal(SIGTERM, restore_proc); 19231567Ssef signal(SIGQUIT, restore_proc); 19331567Ssef } 19431567Ssef 19531567Ssef 19631567Ssef /* 19731567Ssef * At this point, if we started the process, it is stopped waiting to 19831567Ssef * be woken up, either in exit() or in execve(). 19931567Ssef */ 20031567Ssef 201101283SmdoddSTART_TRACE: 202101282Smdodd Procfd = start_tracing( 203101282Smdodd trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT | 204101283Smdodd ((trussinfo->flags & NOSIGS) ? 0 : S_SIG), 205101283Smdodd ((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0)); 20655707Ssef if (Procfd == -1) 20755707Ssef return 0; 20855707Ssef 20931567Ssef pfs.why = 0; 21031567Ssef 211101282Smdodd funcs = set_etype(trussinfo); 21231567Ssef /* 21331567Ssef * At this point, it's a simple loop, waiting for the process to 21431567Ssef * stop, finding out why, printing out why, and then continuing it. 21531567Ssef * All of the grunt work is done in the support routines. 21631567Ssef */ 21731567Ssef 21831567Ssef do { 21931567Ssef int val = 0; 22031567Ssef 22131567Ssef if (ioctl(Procfd, PIOCWAIT, &pfs) == -1) 22232275Scharnier warn("PIOCWAIT top of loop"); 22331567Ssef else { 22431567Ssef switch(i = pfs.why) { 22531567Ssef case S_SCE: 226101282Smdodd funcs->enter_syscall(trussinfo, pfs.val); 22731567Ssef break; 22831567Ssef case S_SCX: 22931567Ssef /* 23031567Ssef * This is so we don't get two messages for an exec -- one 23131567Ssef * for the S_EXEC, and one for the syscall exit. It also, 23231567Ssef * conveniently, ensures that the first message printed out 23331567Ssef * isn't the return-from-syscall used to create the process. 23431567Ssef */ 23531567Ssef 23631567Ssef if (in_exec) { 23731567Ssef in_exec = 0; 23831567Ssef break; 23931567Ssef } 240101283Smdodd 241101283Smdodd if (trussinfo->in_fork && (trussinfo->flags & FOLLOWFORKS)) { 242101283Smdodd int childpid; 243101283Smdodd 244101283Smdodd trussinfo->in_fork = 0; 245101283Smdodd childpid = funcs->exit_syscall(trussinfo, pfs.val); 246101283Smdodd 247101283Smdodd /* 248101283Smdodd * Fork a new copy of ourself to trace the child of the 249101283Smdodd * original traced process. 250101283Smdodd */ 251101283Smdodd if (fork() == 0) { 252101283Smdodd trussinfo->pid = childpid; 253101283Smdodd goto START_TRACE; 254101283Smdodd } 255101283Smdodd break; 256101283Smdodd } 257101282Smdodd funcs->exit_syscall(trussinfo, pfs.val); 25831567Ssef break; 25931567Ssef case S_SIG: 260101282Smdodd fprintf(trussinfo->outfile, "SIGNAL %lu\n", pfs.val); 26138520Scracauer sigexit = pfs.val; 26231567Ssef break; 26331567Ssef case S_EXIT: 264101282Smdodd fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val); 26531567Ssef break; 26631567Ssef case S_EXEC: 267101282Smdodd funcs = set_etype(trussinfo); 26831567Ssef in_exec = 1; 26931567Ssef break; 27031567Ssef default: 271101282Smdodd fprintf (trussinfo->outfile, "Process stopped because of: %d\n", i); 27231567Ssef break; 27331567Ssef } 27431567Ssef } 27555707Ssef if (ioctl(Procfd, PIOCCONT, val) == -1) { 276101282Smdodd if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH) 27755707Ssef break; 27855707Ssef else 27955707Ssef warn("PIOCCONT"); 28055707Ssef } 28131567Ssef } while (pfs.why != S_EXIT); 282101282Smdodd fflush(trussinfo->outfile); 28338520Scracauer if (sigexit) { 28438520Scracauer if (sigexit == SIGQUIT) 28538520Scracauer exit(sigexit); 28638520Scracauer (void) signal(sigexit, SIG_DFL); 28738520Scracauer (void) kill(getpid(), sigexit); 28838520Scracauer } 28931567Ssef return 0; 29031567Ssef} 291