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 /* 36 * The main module for truss. Surprisingly simple, but, then, the other 37 * files handle the bulk of the work. And, of course, the kernel has to 38 * do a lot of the work :). 39 */ 40 41 #include <sys/ptrace.h> 42 43 #include <err.h> 44 #include <signal.h> 45 #include <stdbool.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <sysdecode.h> 49 #include <time.h> 50 #include <unistd.h> 51 52 #include "truss.h" 53 #include "extern.h" 54 #include "syscall.h" 55 56 static __dead2 void 57 usage(void) 58 { 59 fprintf(stderr, "%s\n%s\n%s\n", 60 "usage: truss [-cfaedDHS] [-o file] [-s strsize] [-t expr] -p pid", 61 " truss [-cfaedDHS] [-o file] [-s strsize] [-t expr] " 62 "command [args]", 63 " truss -t"); 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 const char *errstr; 75 pid_t pid; 76 int c; 77 78 fname = NULL; 79 80 /* Initialize the trussinfo struct */ 81 trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo)); 82 if (trussinfo == NULL) 83 errx(1, "calloc() failed"); 84 85 pid = 0; 86 trussinfo->outfile = stderr; 87 trussinfo->strsize = 32; 88 trussinfo->curthread = NULL; 89 LIST_INIT(&trussinfo->proclist); 90 /* 91 * The leading ':' asks getopt() to report a missing option 92 * argument as ':' rather than '?' so that a bare -t, which lists 93 * the system call groups, can be told from a malformed option. 94 * Diagnosing the other two cases then falls to us. 95 */ 96 while ((c = getopt(ac, av, ":p:o:facedDs:t:SH")) != -1) { 97 switch (c) { 98 case 'p': /* specified pid */ 99 pid = atoi(optarg); 100 /* make sure i don't trace me */ 101 if (pid == getpid()) { 102 errx(2, "attempt to grab self."); 103 } 104 break; 105 case 'f': /* Follow fork()'s */ 106 trussinfo->flags |= FOLLOWFORKS; 107 break; 108 case 'a': /* Print execve() argument strings. */ 109 trussinfo->flags |= EXECVEARGS; 110 break; 111 case 'c': /* Count number of system calls and time. */ 112 trussinfo->flags |= (COUNTONLY | NOSIGS); 113 break; 114 case 'e': /* Print execve() environment strings. */ 115 trussinfo->flags |= EXECVEENVS; 116 break; 117 case 'd': /* Absolute timestamps */ 118 trussinfo->flags |= ABSOLUTETIMESTAMPS; 119 break; 120 case 'D': /* Relative timestamps */ 121 trussinfo->flags |= RELATIVETIMESTAMPS; 122 break; 123 case 'o': /* Specified output file */ 124 fname = optarg; 125 break; 126 case 's': /* Specified string size */ 127 trussinfo->strsize = (int)strtonum(optarg, 0, INT_MAX, 128 &errstr); 129 if (errstr) 130 errx(1, "maximum string size is %s: %s", errstr, optarg); 131 break; 132 case 't': /* Select the system calls to trace */ 133 add_syscall_filter(optarg); 134 break; 135 case 'S': /* Don't trace signals */ 136 trussinfo->flags |= NOSIGS; 137 break; 138 case 'H': 139 trussinfo->flags |= DISPLAYTIDS; 140 break; 141 case ':': 142 if (optopt == 't') { 143 /* A bare -t lists the system call groups. */ 144 list_syscall_groups(); 145 return (2); 146 } 147 warnx("option requires an argument -- %c", optopt); 148 usage(); 149 default: 150 warnx("illegal option -- %c", optopt); 151 usage(); 152 } 153 } 154 155 ac -= optind; av += optind; 156 if ((pid == 0 && ac == 0) || 157 (pid != 0 && ac != 0)) 158 usage(); 159 160 if (fname != NULL) { /* Use output file */ 161 /* 162 * Set close-on-exec ('e'), so that the output file is not 163 * shared with the traced process. 164 */ 165 if ((trussinfo->outfile = fopen(fname, "we")) == NULL) 166 err(1, "cannot open %s", fname); 167 } 168 169 /* 170 * If truss starts the process itself, it will ignore some signals -- 171 * they should be passed off to the process, which may or may not 172 * exit. If, however, we are examining an already-running process, 173 * then we restore the event mask on these same signals. 174 */ 175 if (pid == 0) { 176 /* Start a command ourselves */ 177 command = av; 178 setup_and_wait(trussinfo, command); 179 signal(SIGINT, SIG_IGN); 180 signal(SIGTERM, SIG_IGN); 181 signal(SIGQUIT, SIG_IGN); 182 } else { 183 sa.sa_handler = restore_proc; 184 sa.sa_flags = 0; 185 sigemptyset(&sa.sa_mask); 186 sigaction(SIGINT, &sa, NULL); 187 sigaction(SIGQUIT, &sa, NULL); 188 sigaction(SIGTERM, &sa, NULL); 189 start_tracing(trussinfo, pid); 190 } 191 192 /* 193 * At this point, if we started the process, it is stopped waiting to 194 * be woken up, either in exit() or in execve(). 195 */ 196 if (LIST_FIRST(&trussinfo->proclist)->abi == NULL) { 197 /* 198 * If we are not able to handle this ABI, detach from the 199 * process and exit. If we just created a new process to 200 * run a command, kill the new process rather than letting 201 * it run untraced. 202 */ 203 if (pid == 0) 204 kill(LIST_FIRST(&trussinfo->proclist)->pid, SIGKILL); 205 ptrace(PT_DETACH, LIST_FIRST(&trussinfo->proclist)->pid, NULL, 206 0); 207 return (1); 208 } 209 ptrace(PT_SYSCALL, LIST_FIRST(&trussinfo->proclist)->pid, (caddr_t)1, 210 0); 211 212 /* 213 * At this point, it's a simple loop, waiting for the process to 214 * stop, finding out why, printing out why, and then continuing it. 215 * All of the grunt work is done in the support routines. 216 */ 217 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 218 219 eventloop(trussinfo); 220 221 if (trussinfo->flags & COUNTONLY) 222 print_summary(trussinfo); 223 224 fflush(trussinfo->outfile); 225 226 return (0); 227 } 228