1191005Sdelphij/*- 2204977Simp * Copyright 1997 Sean Eric Fagan 331899Ssef * 431899Ssef * Redistribution and use in source and binary forms, with or without 531899Ssef * modification, are permitted provided that the following conditions 631899Ssef * are met: 731899Ssef * 1. Redistributions of source code must retain the above copyright 831899Ssef * notice, this list of conditions and the following disclaimer. 931899Ssef * 2. Redistributions in binary form must reproduce the above copyright 1031899Ssef * notice, this list of conditions and the following disclaimer in the 1131899Ssef * documentation and/or other materials provided with the distribution. 1231899Ssef * 3. All advertising materials mentioning features or use of this software 1331899Ssef * must display the following acknowledgement: 1431899Ssef * This product includes software developed by Sean Eric Fagan 1531899Ssef * 4. Neither the name of the author may be used to endorse or promote 1631899Ssef * products derived from this software without specific prior written 1731899Ssef * permission. 1831899Ssef * 1931899Ssef * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2031899Ssef * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2131899Ssef * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2231899Ssef * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2331899Ssef * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2431899Ssef * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2531899Ssef * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2631899Ssef * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2731899Ssef * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2831899Ssef * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2931899Ssef * SUCH DAMAGE. 3031899Ssef */ 3131899Ssef 32119852Scharnier#include <sys/cdefs.h> 33119852Scharnier__FBSDID("$FreeBSD$"); 3432275Scharnier 3531899Ssef/* 36222103Sbcr * The main module for truss. Surprisingly simple, but, then, the other 3731567Ssef * files handle the bulk of the work. And, of course, the kernel has to 3831567Ssef * do a lot of the work :). 3931567Ssef */ 4031567Ssef 4185301Sdes#include <sys/param.h> 42123916Scracauer#include <sys/types.h> 43104581Smike#include <sys/time.h> 44123916Scracauer#include <sys/resource.h> 45168569Sdelphij#include <sys/sysctl.h> 46191005Sdelphij#include <sys/wait.h> 4785301Sdes 4832275Scharnier#include <err.h> 49200462Sdelphij#include <errno.h> 50200462Sdelphij#include <fcntl.h> 5132275Scharnier#include <signal.h> 5231567Ssef#include <stdio.h> 5331567Ssef#include <stdlib.h> 5431567Ssef#include <string.h> 55101423Smdodd#include <time.h> 5631579Speter#include <unistd.h> 5731567Ssef 58101282Smdodd#include "truss.h" 5987703Smarkm#include "extern.h" 60192025Sdds#include "syscall.h" 6131567Ssef 62240005Szont#define MAXARGS 6 6331567Ssef 64144177Salfredstatic void 6532275Scharnierusage(void) 6632275Scharnier{ 67144177Salfred fprintf(stderr, "%s\n%s\n", 68192025Sdds "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid", 69192025Sdds " truss [-cfaedDS] [-o file] [-s strsize] command [args]"); 70144177Salfred exit(1); 7131567Ssef} 7231567Ssef 7338897Ssef/* 7438897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not 7538897Ssef * work correctly. 7638897Ssef */ 77228396Sedstatic struct ex_types { 78144177Salfred const char *type; 79144177Salfred void (*enter_syscall)(struct trussinfo *, int); 80144177Salfred long (*exit_syscall)(struct trussinfo *, int); 8131567Ssef} ex_types[] = { 82247975Scognet#ifdef __arm__ 83247975Scognet { "FreeBSD ELF32", arm_syscall_entry, arm_syscall_exit }, 84247975Scognet#endif 85130394Sdwmalone#ifdef __amd64__ 86144177Salfred { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 87179051Sjhb { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit }, 88179051Sjhb { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit }, 89130394Sdwmalone#endif 9039908Ssef#ifdef __i386__ 91144177Salfred { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 92144177Salfred { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 93144177Salfred { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 94144177Salfred { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 9539908Ssef#endif 96106716Smarcel#ifdef __ia64__ 97144177Salfred { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit }, 98106716Smarcel#endif 99154047Sgrehan#ifdef __powerpc__ 100154047Sgrehan { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit }, 101154047Sgrehan { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit }, 102211935Snwhitehorn#ifdef __powerpc64__ 103211935Snwhitehorn { "FreeBSD ELF64", powerpc64_syscall_entry, powerpc64_syscall_exit }, 104154047Sgrehan#endif 105211935Snwhitehorn#endif 106101320Sjake#ifdef __sparc64__ 107144177Salfred { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 108101320Sjake#endif 109188628Simp#ifdef __mips__ 110188628Simp { "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit }, 111188628Simp { "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit }, 112188628Simp { "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX 113188628Simp#endif 114144177Salfred { 0, 0, 0 }, 11531567Ssef}; 11631567Ssef 11731567Ssef/* 11831567Ssef * Set the execution type. This is called after every exec, and when 119240005Szont * a process is first monitored. 12031567Ssef */ 12131567Ssef 12231567Ssefstatic struct ex_types * 123144177Salfredset_etype(struct trussinfo *trussinfo) 124144177Salfred{ 125144177Salfred struct ex_types *funcs; 126240005Szont size_t len; 127240005Szont int error; 128240005Szont int mib[4]; 129144177Salfred char progt[32]; 13031567Ssef 131240005Szont len = sizeof(progt); 132168569Sdelphij mib[0] = CTL_KERN; 133168569Sdelphij mib[1] = KERN_PROC; 134168569Sdelphij mib[2] = KERN_PROC_SV_NAME; 135168569Sdelphij mib[3] = trussinfo->pid; 136168569Sdelphij error = sysctl(mib, 4, progt, &len, NULL, 0); 137168569Sdelphij if (error != 0) 138168569Sdelphij err(2, "can not get etype"); 13931567Ssef 140144177Salfred for (funcs = ex_types; funcs->type; funcs++) 141240005Szont if (strcmp(funcs->type, progt) == 0) 142144177Salfred break; 14331567Ssef 144144177Salfred if (funcs->type == NULL) { 145144177Salfred funcs = &ex_types[0]; 146144177Salfred warn("execution type %s is not supported -- using %s", 147144177Salfred progt, funcs->type); 148144177Salfred } 149144177Salfred return (funcs); 15031567Ssef} 15131567Ssef 152132306Salfredchar * 153132306Salfredstrsig(int sig) 154132306Salfred{ 155132306Salfred char *ret; 156132306Salfred 157132306Salfred ret = NULL; 158132306Salfred if (sig > 0 && sig < NSIG) { 159218285Sjilles asprintf(&ret, "SIG%s", sys_signame[sig]); 160132306Salfred if (ret == NULL) 161132306Salfred return (NULL); 162132306Salfred } 163132306Salfred return (ret); 164132306Salfred} 165132306Salfred 16632275Scharnierint 167144177Salfredmain(int ac, char **av) 168144177Salfred{ 169240562Szont struct timespec timediff; 170240393Szont struct sigaction sa; 171144177Salfred struct ex_types *funcs; 172240005Szont struct trussinfo *trussinfo; 173144178Salfred char *fname; 174144177Salfred char *signame; 175240005Szont char **command; 176240005Szont pid_t childpid; 177240562Szont int c, initial_open, status; 17831567Ssef 179144178Salfred fname = NULL; 180144178Salfred initial_open = 1; 181144178Salfred 182144177Salfred /* Initialize the trussinfo struct */ 183192153Sdelphij trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo)); 184144177Salfred if (trussinfo == NULL) 185192153Sdelphij errx(1, "calloc() failed"); 186192153Sdelphij 187144177Salfred trussinfo->outfile = stderr; 188153963Sbrian trussinfo->strsize = 32; 189168569Sdelphij trussinfo->pr_why = S_NONE; 190168569Sdelphij trussinfo->curthread = NULL; 191168569Sdelphij SLIST_INIT(&trussinfo->threadlist); 192192025Sdds while ((c = getopt(ac, av, "p:o:facedDs:S")) != -1) { 193144177Salfred switch (c) { 194144177Salfred case 'p': /* specified pid */ 195144177Salfred trussinfo->pid = atoi(optarg); 196171055Sdelphij /* make sure i don't trace me */ 197240005Szont if (trussinfo->pid == getpid()) { 198171055Sdelphij fprintf(stderr, "attempt to grab self.\n"); 199171055Sdelphij exit(2); 200171055Sdelphij } 201144177Salfred break; 202144177Salfred case 'f': /* Follow fork()'s */ 203144177Salfred trussinfo->flags |= FOLLOWFORKS; 204144177Salfred break; 205144177Salfred case 'a': /* Print execve() argument strings. */ 206144177Salfred trussinfo->flags |= EXECVEARGS; 207144177Salfred break; 208192025Sdds case 'c': /* Count number of system calls and time. */ 209192025Sdds trussinfo->flags |= COUNTONLY; 210192025Sdds break; 211144177Salfred case 'e': /* Print execve() environment strings. */ 212144177Salfred trussinfo->flags |= EXECVEENVS; 213144177Salfred break; 214144177Salfred case 'd': /* Absolute timestamps */ 215144177Salfred trussinfo->flags |= ABSOLUTETIMESTAMPS; 216144177Salfred break; 217144177Salfred case 'D': /* Relative timestamps */ 218144177Salfred trussinfo->flags |= RELATIVETIMESTAMPS; 219144177Salfred break; 220144177Salfred case 'o': /* Specified output file */ 221144177Salfred fname = optarg; 222144177Salfred break; 223153963Sbrian case 's': /* Specified string size */ 224153963Sbrian trussinfo->strsize = atoi(optarg); 225153963Sbrian break; 226240005Szont case 'S': /* Don't trace signals */ 227144177Salfred trussinfo->flags |= NOSIGS; 228144177Salfred break; 229144177Salfred default: 230144177Salfred usage(); 231144177Salfred } 232144177Salfred } 23331567Ssef 234144177Salfred ac -= optind; av += optind; 235144177Salfred if ((trussinfo->pid == 0 && ac == 0) || 236144177Salfred (trussinfo->pid != 0 && ac != 0)) 237144177Salfred usage(); 23831567Ssef 239144177Salfred if (fname != NULL) { /* Use output file */ 240215235Sjh /* 241245957Smjg * Set close-on-exec ('e'), so that the output file is not 242245957Smjg * shared with the traced process. 243215235Sjh */ 244245957Smjg if ((trussinfo->outfile = fopen(fname, "we")) == NULL) 245245957Smjg err(1, "cannot open %s", fname); 246144177Salfred } 24732275Scharnier 248144177Salfred /* 249144177Salfred * If truss starts the process itself, it will ignore some signals -- 250144177Salfred * they should be passed off to the process, which may or may not 251144177Salfred * exit. If, however, we are examining an already-running process, 252144177Salfred * then we restore the event mask on these same signals. 253144177Salfred */ 25431567Ssef 255144177Salfred if (trussinfo->pid == 0) { /* Start a command ourselves */ 256144177Salfred command = av; 257144177Salfred trussinfo->pid = setup_and_wait(command); 258144177Salfred signal(SIGINT, SIG_IGN); 259144177Salfred signal(SIGTERM, SIG_IGN); 260144177Salfred signal(SIGQUIT, SIG_IGN); 261144177Salfred } else { 262240393Szont sa.sa_handler = restore_proc; 263240393Szont sa.sa_flags = 0; 264240393Szont sigemptyset(&sa.sa_mask); 265240393Szont sigaction(SIGINT, &sa, NULL); 266240393Szont sigaction(SIGQUIT, &sa, NULL); 267240393Szont sigaction(SIGTERM, &sa, NULL); 268168569Sdelphij start_tracing(trussinfo->pid); 269144177Salfred } 27031567Ssef 27131567Ssef 272144177Salfred /* 273144177Salfred * At this point, if we started the process, it is stopped waiting to 274144177Salfred * be woken up, either in exit() or in execve(). 275144177Salfred */ 27631567Ssef 277101283SmdoddSTART_TRACE: 278168569Sdelphij funcs = set_etype(trussinfo); 279168569Sdelphij 280144178Salfred initial_open = 0; 281144177Salfred /* 282144177Salfred * At this point, it's a simple loop, waiting for the process to 283144177Salfred * stop, finding out why, printing out why, and then continuing it. 284144177Salfred * All of the grunt work is done in the support routines. 285144177Salfred */ 28631567Ssef 287144177Salfred clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 288101285Smdodd 289144177Salfred do { 290168569Sdelphij waitevent(trussinfo); 29131567Ssef 292240562Szont switch (trussinfo->pr_why) { 293168569Sdelphij case S_SCE: 294168569Sdelphij funcs->enter_syscall(trussinfo, MAXARGS); 295168569Sdelphij clock_gettime(CLOCK_REALTIME, 296240562Szont &trussinfo->curthread->before); 297168569Sdelphij break; 298168569Sdelphij case S_SCX: 299168569Sdelphij clock_gettime(CLOCK_REALTIME, 300240562Szont &trussinfo->curthread->after); 30131567Ssef 302168569Sdelphij if (trussinfo->curthread->in_fork && 303168569Sdelphij (trussinfo->flags & FOLLOWFORKS)) { 304168569Sdelphij trussinfo->curthread->in_fork = 0; 305240005Szont childpid = funcs->exit_syscall(trussinfo, 306240005Szont trussinfo->pr_data); 307101283Smdodd 308168569Sdelphij /* 309168569Sdelphij * Fork a new copy of ourself to trace 310168569Sdelphij * the child of the original traced 311168569Sdelphij * process. 312168569Sdelphij */ 313168569Sdelphij if (fork() == 0) { 314168569Sdelphij trussinfo->pid = childpid; 315168569Sdelphij start_tracing(trussinfo->pid); 316168569Sdelphij goto START_TRACE; 317144177Salfred } 318144177Salfred break; 319168569Sdelphij } 320168569Sdelphij funcs->exit_syscall(trussinfo, MAXARGS); 321168569Sdelphij break; 322168569Sdelphij case S_SIG: 323168569Sdelphij if (trussinfo->flags & NOSIGS) 324144177Salfred break; 325168569Sdelphij if (trussinfo->flags & FOLLOWFORKS) 326168569Sdelphij fprintf(trussinfo->outfile, "%5d: ", 327168569Sdelphij trussinfo->pid); 328168569Sdelphij if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 329247338Sdelphij timespecsubt(&trussinfo->curthread->after, 330168569Sdelphij &trussinfo->start_time, &timediff); 331168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 332168569Sdelphij (long)timediff.tv_sec, 333168569Sdelphij timediff.tv_nsec); 334144177Salfred } 335168569Sdelphij if (trussinfo->flags & RELATIVETIMESTAMPS) { 336247338Sdelphij timespecsubt(&trussinfo->curthread->after, 337240562Szont &trussinfo->curthread->before, &timediff); 338168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 339168569Sdelphij (long)timediff.tv_sec, 340168569Sdelphij timediff.tv_nsec); 341168569Sdelphij } 342168569Sdelphij signame = strsig(trussinfo->pr_data); 343168569Sdelphij fprintf(trussinfo->outfile, 344168569Sdelphij "SIGNAL %u (%s)\n", trussinfo->pr_data, 345168569Sdelphij signame == NULL ? "?" : signame); 346168569Sdelphij free(signame); 347168569Sdelphij break; 348168569Sdelphij case S_EXIT: 349192025Sdds if (trussinfo->flags & COUNTONLY) 350192025Sdds break; 351168569Sdelphij if (trussinfo->flags & FOLLOWFORKS) 352168569Sdelphij fprintf(trussinfo->outfile, "%5d: ", 353168569Sdelphij trussinfo->pid); 354168569Sdelphij if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 355247338Sdelphij timespecsubt(&trussinfo->curthread->after, 356168569Sdelphij &trussinfo->start_time, &timediff); 357168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 358168569Sdelphij (long)timediff.tv_sec, 359168569Sdelphij timediff.tv_nsec); 360168569Sdelphij } 361168569Sdelphij if (trussinfo->flags & RELATIVETIMESTAMPS) { 362247338Sdelphij timespecsubt(&trussinfo->curthread->after, 363240562Szont &trussinfo->curthread->before, &timediff); 364240005Szont fprintf(trussinfo->outfile, "%ld.%09ld ", 365240005Szont (long)timediff.tv_sec, timediff.tv_nsec); 366168569Sdelphij } 367168569Sdelphij fprintf(trussinfo->outfile, 368168569Sdelphij "process exit, rval = %u\n", trussinfo->pr_data); 369168569Sdelphij break; 370168569Sdelphij default: 371168569Sdelphij break; 372144177Salfred } 373240393Szont } while (trussinfo->pr_why != S_EXIT && 374240393Szont trussinfo->pr_why != S_DETACHED); 375191005Sdelphij 376240005Szont if (trussinfo->flags & FOLLOWFORKS) { 377191005Sdelphij do { 378191005Sdelphij childpid = wait(&status); 379191005Sdelphij } while (childpid != -1); 380240005Szont } 381191005Sdelphij 382240005Szont if (trussinfo->flags & COUNTONLY) 383240005Szont print_summary(trussinfo); 384192025Sdds 385192025Sdds fflush(trussinfo->outfile); 386192025Sdds 387144177Salfred return (0); 38831567Ssef} 389