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