1 /*- 2 * Copyright 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 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 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/param.h> 42 #include <sys/types.h> 43 #include <sys/time.h> 44 #include <sys/resource.h> 45 #include <sys/sysctl.h> 46 #include <sys/wait.h> 47 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <signal.h> 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <time.h> 57 #include <unistd.h> 58 59 #include "truss.h" 60 #include "extern.h" 61 #include "syscall.h" 62 63 #define MAXARGS 6 64 65 static void 66 usage(void) 67 { 68 fprintf(stderr, "%s\n%s\n", 69 "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid", 70 " truss [-cfaedDS] [-o file] [-s strsize] command [args]"); 71 exit(1); 72 } 73 74 /* 75 * WARNING! "FreeBSD a.out" must be first, or set_etype will not 76 * work correctly. 77 */ 78 static struct ex_types { 79 const char *type; 80 void (*enter_syscall)(struct trussinfo *, int); 81 long (*exit_syscall)(struct trussinfo *, int); 82 } ex_types[] = { 83 #ifdef __arm__ 84 { "FreeBSD ELF32", arm_syscall_entry, arm_syscall_exit }, 85 #endif 86 #ifdef __amd64__ 87 { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 88 { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit }, 89 { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit }, 90 #endif 91 #ifdef __i386__ 92 { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 93 { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 94 { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 95 { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 96 #endif 97 #ifdef __powerpc__ 98 { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit }, 99 { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit }, 100 #ifdef __powerpc64__ 101 { "FreeBSD ELF64", powerpc64_syscall_entry, powerpc64_syscall_exit }, 102 #endif 103 #endif 104 #ifdef __sparc64__ 105 { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 106 #endif 107 #ifdef __mips__ 108 { "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit }, 109 { "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit }, 110 { "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX 111 #endif 112 { 0, 0, 0 }, 113 }; 114 115 /* 116 * Set the execution type. This is called after every exec, and when 117 * a process is first monitored. 118 */ 119 120 static struct ex_types * 121 set_etype(struct trussinfo *trussinfo) 122 { 123 struct ex_types *funcs; 124 size_t len; 125 int error; 126 int mib[4]; 127 char progt[32]; 128 129 len = sizeof(progt); 130 mib[0] = CTL_KERN; 131 mib[1] = KERN_PROC; 132 mib[2] = KERN_PROC_SV_NAME; 133 mib[3] = trussinfo->pid; 134 error = sysctl(mib, 4, progt, &len, NULL, 0); 135 if (error != 0) 136 err(2, "can not get etype"); 137 138 for (funcs = ex_types; funcs->type; funcs++) 139 if (strcmp(funcs->type, progt) == 0) 140 break; 141 142 if (funcs->type == NULL) { 143 funcs = &ex_types[0]; 144 warn("execution type %s is not supported -- using %s", 145 progt, funcs->type); 146 } 147 return (funcs); 148 } 149 150 char * 151 strsig(int sig) 152 { 153 static char tmp[64]; 154 155 if (sig > 0 && sig < NSIG) { 156 snprintf(tmp, sizeof(tmp), "SIG%s", sys_signame[sig]); 157 return (tmp); 158 } 159 return (NULL); 160 } 161 162 int 163 main(int ac, char **av) 164 { 165 struct timespec timediff; 166 struct sigaction sa; 167 struct ex_types *funcs; 168 struct trussinfo *trussinfo; 169 char *fname; 170 char *signame; 171 char **command; 172 pid_t childpid; 173 int c, initial_open, status; 174 175 fname = NULL; 176 initial_open = 1; 177 178 /* Initialize the trussinfo struct */ 179 trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo)); 180 if (trussinfo == NULL) 181 errx(1, "calloc() failed"); 182 183 trussinfo->outfile = stderr; 184 trussinfo->strsize = 32; 185 trussinfo->pr_why = S_NONE; 186 trussinfo->curthread = NULL; 187 SLIST_INIT(&trussinfo->threadlist); 188 while ((c = getopt(ac, av, "p:o:facedDs:S")) != -1) { 189 switch (c) { 190 case 'p': /* specified pid */ 191 trussinfo->pid = atoi(optarg); 192 /* make sure i don't trace me */ 193 if (trussinfo->pid == getpid()) { 194 fprintf(stderr, "attempt to grab self.\n"); 195 exit(2); 196 } 197 break; 198 case 'f': /* Follow fork()'s */ 199 trussinfo->flags |= FOLLOWFORKS; 200 break; 201 case 'a': /* Print execve() argument strings. */ 202 trussinfo->flags |= EXECVEARGS; 203 break; 204 case 'c': /* Count number of system calls and time. */ 205 trussinfo->flags |= COUNTONLY; 206 break; 207 case 'e': /* Print execve() environment strings. */ 208 trussinfo->flags |= EXECVEENVS; 209 break; 210 case 'd': /* Absolute timestamps */ 211 trussinfo->flags |= ABSOLUTETIMESTAMPS; 212 break; 213 case 'D': /* Relative timestamps */ 214 trussinfo->flags |= RELATIVETIMESTAMPS; 215 break; 216 case 'o': /* Specified output file */ 217 fname = optarg; 218 break; 219 case 's': /* Specified string size */ 220 trussinfo->strsize = atoi(optarg); 221 break; 222 case 'S': /* Don't trace signals */ 223 trussinfo->flags |= NOSIGS; 224 break; 225 default: 226 usage(); 227 } 228 } 229 230 ac -= optind; av += optind; 231 if ((trussinfo->pid == 0 && ac == 0) || 232 (trussinfo->pid != 0 && ac != 0)) 233 usage(); 234 235 if (fname != NULL) { /* Use output file */ 236 /* 237 * Set close-on-exec ('e'), so that the output file is not 238 * shared with the traced process. 239 */ 240 if ((trussinfo->outfile = fopen(fname, "we")) == NULL) 241 err(1, "cannot open %s", fname); 242 } 243 244 /* 245 * If truss starts the process itself, it will ignore some signals -- 246 * they should be passed off to the process, which may or may not 247 * exit. If, however, we are examining an already-running process, 248 * then we restore the event mask on these same signals. 249 */ 250 251 if (trussinfo->pid == 0) { /* Start a command ourselves */ 252 command = av; 253 trussinfo->pid = setup_and_wait(command); 254 signal(SIGINT, SIG_IGN); 255 signal(SIGTERM, SIG_IGN); 256 signal(SIGQUIT, SIG_IGN); 257 } else { 258 sa.sa_handler = restore_proc; 259 sa.sa_flags = 0; 260 sigemptyset(&sa.sa_mask); 261 sigaction(SIGINT, &sa, NULL); 262 sigaction(SIGQUIT, &sa, NULL); 263 sigaction(SIGTERM, &sa, NULL); 264 start_tracing(trussinfo->pid); 265 } 266 267 268 /* 269 * At this point, if we started the process, it is stopped waiting to 270 * be woken up, either in exit() or in execve(). 271 */ 272 273 START_TRACE: 274 funcs = set_etype(trussinfo); 275 276 initial_open = 0; 277 /* 278 * At this point, it's a simple loop, waiting for the process to 279 * stop, finding out why, printing out why, and then continuing it. 280 * All of the grunt work is done in the support routines. 281 */ 282 283 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 284 285 do { 286 waitevent(trussinfo); 287 288 switch (trussinfo->pr_why) { 289 case S_SCE: 290 funcs->enter_syscall(trussinfo, MAXARGS); 291 clock_gettime(CLOCK_REALTIME, 292 &trussinfo->curthread->before); 293 break; 294 case S_SCX: 295 clock_gettime(CLOCK_REALTIME, 296 &trussinfo->curthread->after); 297 298 if (trussinfo->curthread->in_fork && 299 (trussinfo->flags & FOLLOWFORKS)) { 300 trussinfo->curthread->in_fork = 0; 301 childpid = funcs->exit_syscall(trussinfo, 302 trussinfo->pr_data); 303 304 /* 305 * Fork a new copy of ourself to trace 306 * the child of the original traced 307 * process. 308 */ 309 if (fork() == 0) { 310 trussinfo->pid = childpid; 311 start_tracing(trussinfo->pid); 312 goto START_TRACE; 313 } 314 break; 315 } 316 funcs->exit_syscall(trussinfo, MAXARGS); 317 break; 318 case S_SIG: 319 if (trussinfo->flags & NOSIGS) 320 break; 321 if (trussinfo->flags & FOLLOWFORKS) 322 fprintf(trussinfo->outfile, "%5d: ", 323 trussinfo->pid); 324 if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 325 timespecsubt(&trussinfo->curthread->after, 326 &trussinfo->start_time, &timediff); 327 fprintf(trussinfo->outfile, "%jd.%09ld ", 328 (intmax_t)timediff.tv_sec, 329 timediff.tv_nsec); 330 } 331 if (trussinfo->flags & RELATIVETIMESTAMPS) { 332 timespecsubt(&trussinfo->curthread->after, 333 &trussinfo->curthread->before, &timediff); 334 fprintf(trussinfo->outfile, "%jd.%09ld ", 335 (intmax_t)timediff.tv_sec, 336 timediff.tv_nsec); 337 } 338 signame = strsig(trussinfo->pr_data); 339 fprintf(trussinfo->outfile, 340 "SIGNAL %u (%s)\n", trussinfo->pr_data, 341 signame == NULL ? "?" : signame); 342 break; 343 case S_EXIT: 344 if (trussinfo->flags & COUNTONLY) 345 break; 346 if (trussinfo->flags & FOLLOWFORKS) 347 fprintf(trussinfo->outfile, "%5d: ", 348 trussinfo->pid); 349 if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 350 timespecsubt(&trussinfo->curthread->after, 351 &trussinfo->start_time, &timediff); 352 fprintf(trussinfo->outfile, "%jd.%09ld ", 353 (intmax_t)timediff.tv_sec, 354 timediff.tv_nsec); 355 } 356 if (trussinfo->flags & RELATIVETIMESTAMPS) { 357 timespecsubt(&trussinfo->curthread->after, 358 &trussinfo->curthread->before, &timediff); 359 fprintf(trussinfo->outfile, "%jd.%09ld ", 360 (intmax_t)timediff.tv_sec, 361 timediff.tv_nsec); 362 } 363 fprintf(trussinfo->outfile, 364 "process exit, rval = %u\n", trussinfo->pr_data); 365 break; 366 default: 367 break; 368 } 369 } while (trussinfo->pr_why != S_EXIT && 370 trussinfo->pr_why != S_DETACHED); 371 372 if (trussinfo->flags & FOLLOWFORKS) { 373 do { 374 childpid = wait(&status); 375 } while (childpid != -1); 376 } 377 378 if (trussinfo->flags & COUNTONLY) 379 print_summary(trussinfo); 380 381 fflush(trussinfo->outfile); 382 383 return (0); 384 } 385