main.c revision 87703
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 87703 2001-12-11 23:34:02Z markm $"; 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 5687703Smarkm#include "extern.h" 5731567Ssef 5831567Ssef/* 5931567Ssef * These should really be parameterized -- I don't like having globals, 6031567Ssef * but this is the easiest way, right now, to deal with them. 6131567Ssef */ 6231567Ssef 6331567Ssefint pid = 0; 6431567Ssefint nosigs = 0; 6581608SpeterFILE *outfile; 6631567Ssefint Procfd; 6731567Ssef 6831567Ssefstatic inline void 6932275Scharnierusage(void) 7032275Scharnier{ 7132275Scharnier fprintf(stderr, "%s\n%s\n", 7232275Scharnier "usage: truss [-S] [-o file] -p pid", 7332275Scharnier " truss [-S] [-o file] command [args]"); 7431567Ssef exit(1); 7531567Ssef} 7631567Ssef 7738897Ssef/* 7838897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not 7938897Ssef * work correctly. 8038897Ssef */ 8131567Ssefstruct ex_types { 8287703Smarkm const char *type; 8331567Ssef void (*enter_syscall)(int, int); 8431567Ssef void (*exit_syscall)(int, int); 8531567Ssef} ex_types[] = { 8639908Ssef#ifdef __alpha__ 8739908Ssef { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit }, 8839908Ssef#endif 8939908Ssef#ifdef __i386__ 9031567Ssef { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 9131580Speter { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 9231567Ssef { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 9339908Ssef#endif 9431567Ssef { 0, 0, 0 }, 9531567Ssef}; 9631567Ssef 9731567Ssef/* 9831567Ssef * Set the execution type. This is called after every exec, and when 9931567Ssef * a process is first monitored. The procfs pseudo-file "etype" has 10031567Ssef * the execution module type -- see /proc/curproc/etype for an example. 10131567Ssef */ 10231567Ssef 10331567Ssefstatic struct ex_types * 10487703Smarkmset_etype(void) { 10531567Ssef struct ex_types *funcs; 10631567Ssef char etype[24]; 10787703Smarkm char progt[32]; 10831567Ssef int fd; 10931567Ssef 11031567Ssef sprintf(etype, "/proc/%d/etype", pid); 11131567Ssef if ((fd = open(etype, O_RDONLY)) == -1) { 11287703Smarkm strcpy(progt, "FreeBSD a.out"); 11331567Ssef } else { 11487703Smarkm int len = read(fd, progt, sizeof(progt)); 11587703Smarkm progt[len-1] = '\0'; 11631580Speter close(fd); 11731567Ssef } 11831567Ssef 11931567Ssef for (funcs = ex_types; funcs->type; funcs++) 12087703Smarkm if (!strcmp(funcs->type, progt)) 12131567Ssef break; 12231567Ssef 12338897Ssef if (funcs == NULL) { 12438897Ssef warn("Execution type %s is not supported -- using FreeBSD a.out\n", 12587703Smarkm progt); 12638897Ssef funcs = &ex_types[0]; 12738897Ssef } 12831567Ssef return funcs; 12931567Ssef} 13031567Ssef 13132275Scharnierint 13231567Ssefmain(int ac, char **av) { 13331567Ssef int c; 13431567Ssef int i; 13531567Ssef char **command; 13631567Ssef struct procfs_status pfs; 13731567Ssef struct ex_types *funcs; 13831567Ssef int in_exec = 0; 13932275Scharnier char *fname = NULL; 14038520Scracauer int sigexit = 0; 14131567Ssef 14281608Speter outfile = stdout; 14332306Sjmg while ((c = getopt(ac, av, "p:o:S")) != -1) { 14431567Ssef switch (c) { 14531567Ssef case 'p': /* specified pid */ 14631567Ssef pid = atoi(optarg); 14731567Ssef break; 14831567Ssef case 'o': /* Specified output file */ 14932275Scharnier fname = optarg; 15031567Ssef break; 15131567Ssef case 'S': /* Don't trace signals */ 15231567Ssef nosigs = 1; 15331567Ssef break; 15431567Ssef default: 15531567Ssef usage(); 15631567Ssef } 15731567Ssef } 15831567Ssef 15931567Ssef ac -= optind; av += optind; 16031582Ssef if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0)) 16131567Ssef usage(); 16231567Ssef 16332275Scharnier if (fname != NULL) { /* Use output file */ 16432275Scharnier if ((outfile = fopen(fname, "w")) == NULL) 16532275Scharnier errx(1, "cannot open %s", fname); 16632275Scharnier } 16732275Scharnier 16831567Ssef /* 16931567Ssef * If truss starts the process itself, it will ignore some signals -- 17031567Ssef * they should be passed off to the process, which may or may not 17131567Ssef * exit. If, however, we are examining an already-running process, 17231567Ssef * then we restore the event mask on these same signals. 17331567Ssef */ 17431567Ssef 17531567Ssef if (pid == 0) { /* Start a command ourselves */ 17631567Ssef command = av; 17731567Ssef pid = setup_and_wait(command); 17831567Ssef signal(SIGINT, SIG_IGN); 17931567Ssef signal(SIGTERM, SIG_IGN); 18031567Ssef signal(SIGQUIT, SIG_IGN); 18131567Ssef } else { 18231567Ssef signal(SIGINT, restore_proc); 18331567Ssef signal(SIGTERM, restore_proc); 18431567Ssef signal(SIGQUIT, restore_proc); 18531567Ssef } 18631567Ssef 18731567Ssef 18831567Ssef /* 18931567Ssef * At this point, if we started the process, it is stopped waiting to 19031567Ssef * be woken up, either in exit() or in execve(). 19131567Ssef */ 19231567Ssef 19331567Ssef Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT | 19431567Ssef (nosigs ? 0 : S_SIG)); 19555707Ssef if (Procfd == -1) 19655707Ssef return 0; 19755707Ssef 19831567Ssef pfs.why = 0; 19931567Ssef 20031567Ssef funcs = set_etype(); 20131567Ssef /* 20231567Ssef * At this point, it's a simple loop, waiting for the process to 20331567Ssef * stop, finding out why, printing out why, and then continuing it. 20431567Ssef * All of the grunt work is done in the support routines. 20531567Ssef */ 20631567Ssef 20731567Ssef do { 20831567Ssef int val = 0; 20931567Ssef 21031567Ssef if (ioctl(Procfd, PIOCWAIT, &pfs) == -1) 21132275Scharnier warn("PIOCWAIT top of loop"); 21231567Ssef else { 21331567Ssef switch(i = pfs.why) { 21431567Ssef case S_SCE: 21531567Ssef funcs->enter_syscall(pid, pfs.val); 21631567Ssef break; 21731567Ssef case S_SCX: 21831567Ssef /* 21931567Ssef * This is so we don't get two messages for an exec -- one 22031567Ssef * for the S_EXEC, and one for the syscall exit. It also, 22131567Ssef * conveniently, ensures that the first message printed out 22231567Ssef * isn't the return-from-syscall used to create the process. 22331567Ssef */ 22431567Ssef 22531567Ssef if (in_exec) { 22631567Ssef in_exec = 0; 22731567Ssef break; 22831567Ssef } 22931567Ssef funcs->exit_syscall(pid, pfs.val); 23031567Ssef break; 23131567Ssef case S_SIG: 23237453Sbde fprintf(outfile, "SIGNAL %lu\n", pfs.val); 23338520Scracauer sigexit = pfs.val; 23431567Ssef break; 23531567Ssef case S_EXIT: 23637453Sbde fprintf (outfile, "process exit, rval = %lu\n", pfs.val); 23731567Ssef break; 23831567Ssef case S_EXEC: 23931567Ssef funcs = set_etype(); 24031567Ssef in_exec = 1; 24131567Ssef break; 24231567Ssef default: 24331567Ssef fprintf (outfile, "Process stopped because of: %d\n", i); 24431567Ssef break; 24531567Ssef } 24631567Ssef } 24755707Ssef if (ioctl(Procfd, PIOCCONT, val) == -1) { 24855707Ssef if (kill(pid, 0) == -1 && errno == ESRCH) 24955707Ssef break; 25055707Ssef else 25155707Ssef warn("PIOCCONT"); 25255707Ssef } 25331567Ssef } while (pfs.why != S_EXIT); 25441970Smsmith fflush(outfile); 25538520Scracauer if (sigexit) { 25638520Scracauer if (sigexit == SIGQUIT) 25738520Scracauer exit(sigexit); 25838520Scracauer (void) signal(sigexit, SIG_DFL); 25938520Scracauer (void) kill(getpid(), sigexit); 26038520Scracauer } 26131567Ssef return 0; 26231567Ssef} 263