1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright 1997 Sean Eric Fagan 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Sean Eric Fagan 17 * 4. Neither the name of the author may be used to endorse or promote 18 * products derived from this software without specific prior written 19 * permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 /* 38 * The main module for truss. Surprisingly 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 <sys/ptrace.h> 44 45 #include <err.h> 46 #include <signal.h> 47 #include <stdbool.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <sysdecode.h> 51 #include <time.h> 52 #include <unistd.h> 53 54 #include "truss.h" 55 #include "extern.h" 56 #include "syscall.h" 57 58 static void 59 usage(void) 60 { 61 fprintf(stderr, "%s\n%s\n", 62 "usage: truss [-cfaedDHS] [-o file] [-s strsize] -p pid", 63 " truss [-cfaedDHS] [-o file] [-s strsize] command [args]"); 64 exit(1); 65 } 66 67 int 68 main(int ac, char **av) 69 { 70 struct sigaction sa; 71 struct trussinfo *trussinfo; 72 char *fname; 73 char **command; 74 pid_t pid; 75 int c; 76 77 fname = NULL; 78 79 /* Initialize the trussinfo struct */ 80 trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo)); 81 if (trussinfo == NULL) 82 errx(1, "calloc() failed"); 83 84 pid = 0; 85 trussinfo->outfile = stderr; 86 trussinfo->strsize = 32; 87 trussinfo->curthread = NULL; 88 LIST_INIT(&trussinfo->proclist); 89 init_syscalls(); 90 while ((c = getopt(ac, av, "p:o:facedDs:SH")) != -1) { 91 switch (c) { 92 case 'p': /* specified pid */ 93 pid = atoi(optarg); 94 /* make sure i don't trace me */ 95 if (pid == getpid()) { 96 errx(2, "attempt to grab self."); 97 } 98 break; 99 case 'f': /* Follow fork()'s */ 100 trussinfo->flags |= FOLLOWFORKS; 101 break; 102 case 'a': /* Print execve() argument strings. */ 103 trussinfo->flags |= EXECVEARGS; 104 break; 105 case 'c': /* Count number of system calls and time. */ 106 trussinfo->flags |= (COUNTONLY | NOSIGS); 107 break; 108 case 'e': /* Print execve() environment strings. */ 109 trussinfo->flags |= EXECVEENVS; 110 break; 111 case 'd': /* Absolute timestamps */ 112 trussinfo->flags |= ABSOLUTETIMESTAMPS; 113 break; 114 case 'D': /* Relative timestamps */ 115 trussinfo->flags |= RELATIVETIMESTAMPS; 116 break; 117 case 'o': /* Specified output file */ 118 fname = optarg; 119 break; 120 case 's': /* Specified string size */ 121 trussinfo->strsize = atoi(optarg); 122 break; 123 case 'S': /* Don't trace signals */ 124 trussinfo->flags |= NOSIGS; 125 break; 126 case 'H': 127 trussinfo->flags |= DISPLAYTIDS; 128 break; 129 default: 130 usage(); 131 } 132 } 133 134 ac -= optind; av += optind; 135 if ((pid == 0 && ac == 0) || 136 (pid != 0 && ac != 0)) 137 usage(); 138 139 if (fname != NULL) { /* Use output file */ 140 /* 141 * Set close-on-exec ('e'), so that the output file is not 142 * shared with the traced process. 143 */ 144 if ((trussinfo->outfile = fopen(fname, "we")) == NULL) 145 err(1, "cannot open %s", fname); 146 } 147 148 /* 149 * If truss starts the process itself, it will ignore some signals -- 150 * they should be passed off to the process, which may or may not 151 * exit. If, however, we are examining an already-running process, 152 * then we restore the event mask on these same signals. 153 */ 154 if (pid == 0) { 155 /* Start a command ourselves */ 156 command = av; 157 setup_and_wait(trussinfo, command); 158 signal(SIGINT, SIG_IGN); 159 signal(SIGTERM, SIG_IGN); 160 signal(SIGQUIT, SIG_IGN); 161 } else { 162 sa.sa_handler = restore_proc; 163 sa.sa_flags = 0; 164 sigemptyset(&sa.sa_mask); 165 sigaction(SIGINT, &sa, NULL); 166 sigaction(SIGQUIT, &sa, NULL); 167 sigaction(SIGTERM, &sa, NULL); 168 start_tracing(trussinfo, pid); 169 } 170 171 /* 172 * At this point, if we started the process, it is stopped waiting to 173 * be woken up, either in exit() or in execve(). 174 */ 175 if (LIST_FIRST(&trussinfo->proclist)->abi == NULL) { 176 /* 177 * If we are not able to handle this ABI, detach from the 178 * process and exit. If we just created a new process to 179 * run a command, kill the new process rather than letting 180 * it run untraced. 181 */ 182 if (pid == 0) 183 kill(LIST_FIRST(&trussinfo->proclist)->pid, SIGKILL); 184 ptrace(PT_DETACH, LIST_FIRST(&trussinfo->proclist)->pid, NULL, 185 0); 186 return (1); 187 } 188 ptrace(PT_SYSCALL, LIST_FIRST(&trussinfo->proclist)->pid, (caddr_t)1, 189 0); 190 191 /* 192 * At this point, it's a simple loop, waiting for the process to 193 * stop, finding out why, printing out why, and then continuing it. 194 * All of the grunt work is done in the support routines. 195 */ 196 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 197 198 eventloop(trussinfo); 199 200 if (trussinfo->flags & COUNTONLY) 201 print_summary(trussinfo); 202 203 fflush(trussinfo->outfile); 204 205 return (0); 206 } 207