main.c revision 38520
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: main.c,v 1.9 1998/07/06 21:01:47 bde Exp $"; 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 int sigexit = 0; 131 132 while ((c = getopt(ac, av, "p:o:S")) != -1) { 133 switch (c) { 134 case 'p': /* specified pid */ 135 pid = atoi(optarg); 136 break; 137 case 'o': /* Specified output file */ 138 fname = optarg; 139 break; 140 case 'S': /* Don't trace signals */ 141 nosigs = 1; 142 break; 143 default: 144 usage(); 145 } 146 } 147 148 ac -= optind; av += optind; 149 if ((pid == 0 && ac == 0) || (pid != 0 && ac != 0)) 150 usage(); 151 152 if (fname != NULL) { /* Use output file */ 153 if ((outfile = fopen(fname, "w")) == NULL) 154 errx(1, "cannot open %s", fname); 155 } 156 157 /* 158 * If truss starts the process itself, it will ignore some signals -- 159 * they should be passed off to the process, which may or may not 160 * exit. If, however, we are examining an already-running process, 161 * then we restore the event mask on these same signals. 162 */ 163 164 if (pid == 0) { /* Start a command ourselves */ 165 command = av; 166 pid = setup_and_wait(command); 167 signal(SIGINT, SIG_IGN); 168 signal(SIGTERM, SIG_IGN); 169 signal(SIGQUIT, SIG_IGN); 170 } else { 171 extern void restore_proc(int); 172 signal(SIGINT, restore_proc); 173 signal(SIGTERM, restore_proc); 174 signal(SIGQUIT, restore_proc); 175 } 176 177 178 /* 179 * At this point, if we started the process, it is stopped waiting to 180 * be woken up, either in exit() or in execve(). 181 */ 182 183 Procfd = start_tracing(pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT | 184 (nosigs ? 0 : S_SIG)); 185 pfs.why = 0; 186 187 funcs = set_etype(); 188 /* 189 * At this point, it's a simple loop, waiting for the process to 190 * stop, finding out why, printing out why, and then continuing it. 191 * All of the grunt work is done in the support routines. 192 */ 193 194 do { 195 int val = 0; 196 197 if (ioctl(Procfd, PIOCWAIT, &pfs) == -1) 198 warn("PIOCWAIT top of loop"); 199 else { 200 switch(i = pfs.why) { 201 case S_SCE: 202 funcs->enter_syscall(pid, pfs.val); 203 break; 204 case S_SCX: 205 /* 206 * This is so we don't get two messages for an exec -- one 207 * for the S_EXEC, and one for the syscall exit. It also, 208 * conveniently, ensures that the first message printed out 209 * isn't the return-from-syscall used to create the process. 210 */ 211 212 if (in_exec) { 213 in_exec = 0; 214 break; 215 } 216 funcs->exit_syscall(pid, pfs.val); 217 break; 218 case S_SIG: 219 fprintf(outfile, "SIGNAL %lu\n", pfs.val); 220 sigexit = pfs.val; 221 break; 222 case S_EXIT: 223 fprintf (outfile, "process exit, rval = %lu\n", pfs.val); 224 break; 225 case S_EXEC: 226 funcs = set_etype(); 227 in_exec = 1; 228 break; 229 default: 230 fprintf (outfile, "Process stopped because of: %d\n", i); 231 break; 232 } 233 } 234 if (ioctl(Procfd, PIOCCONT, val) == -1) 235 warn("PIOCCONT"); 236 } while (pfs.why != S_EXIT); 237 if (sigexit) { 238 if (sigexit == SIGQUIT) 239 exit(sigexit); 240 (void) signal(sigexit, SIG_DFL); 241 (void) kill(getpid(), sigexit); 242 } 243 return 0; 244} 245