main.c revision 188628
131567Ssef/* 231899Ssef * Copryight 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: head/usr.bin/truss/main.c 188628 2009-02-15 01:26:49Z imp $"); 3432275Scharnier 3531899Ssef/* 3631567Ssef * The main module for truss. Suprisingly 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> 4685301Sdes 47132306Salfred#include <ctype.h> 4832275Scharnier#include <err.h> 4932275Scharnier#include <errno.h> 5032275Scharnier#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" 6031567Ssef 61171645Smarcel#define MAXARGS 6 6231567Ssef 63144177Salfredstatic void 6432275Scharnierusage(void) 6532275Scharnier{ 66144177Salfred fprintf(stderr, "%s\n%s\n", 67153963Sbrian "usage: truss [-faedDS] [-o file] [-s strsize] -p pid", 68153963Sbrian " truss [-faedDS] [-o file] [-s strsize] command [args]"); 69144177Salfred exit(1); 7031567Ssef} 7131567Ssef 7238897Ssef/* 7338897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not 7438897Ssef * work correctly. 7538897Ssef */ 7631567Ssefstruct ex_types { 77144177Salfred const char *type; 78144177Salfred void (*enter_syscall)(struct trussinfo *, int); 79144177Salfred long (*exit_syscall)(struct trussinfo *, int); 8031567Ssef} ex_types[] = { 81130394Sdwmalone#ifdef __amd64__ 82144177Salfred { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 83179051Sjhb { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit }, 84179051Sjhb { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit }, 85130394Sdwmalone#endif 8639908Ssef#ifdef __i386__ 87144177Salfred { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 88144177Salfred { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 89144177Salfred { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 90144177Salfred { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 9139908Ssef#endif 92106716Smarcel#ifdef __ia64__ 93144177Salfred { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit }, 94106716Smarcel#endif 95154047Sgrehan#ifdef __powerpc__ 96154047Sgrehan { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit }, 97154047Sgrehan { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit }, 98154047Sgrehan#endif 99101320Sjake#ifdef __sparc64__ 100144177Salfred { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 101101320Sjake#endif 102188628Simp#ifdef __mips__ 103188628Simp { "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit }, 104188628Simp { "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit }, 105188628Simp { "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX 106188628Simp#endif 107144177Salfred { 0, 0, 0 }, 10831567Ssef}; 10931567Ssef 11031567Ssef/* 11131567Ssef * Set the execution type. This is called after every exec, and when 112168569Sdelphij * a process is first monitored. 11331567Ssef */ 11431567Ssef 11531567Ssefstatic struct ex_types * 116144177Salfredset_etype(struct trussinfo *trussinfo) 117144177Salfred{ 118144177Salfred struct ex_types *funcs; 119144177Salfred char progt[32]; 120168569Sdelphij 121168569Sdelphij size_t len = sizeof(progt); 122168569Sdelphij int mib[4]; 123168569Sdelphij int error; 12431567Ssef 125168569Sdelphij mib[0] = CTL_KERN; 126168569Sdelphij mib[1] = KERN_PROC; 127168569Sdelphij mib[2] = KERN_PROC_SV_NAME; 128168569Sdelphij mib[3] = trussinfo->pid; 129168569Sdelphij error = sysctl(mib, 4, progt, &len, NULL, 0); 130168569Sdelphij if (error != 0) 131168569Sdelphij err(2, "can not get etype"); 13231567Ssef 133144177Salfred for (funcs = ex_types; funcs->type; funcs++) 134144177Salfred if (!strcmp(funcs->type, progt)) 135144177Salfred break; 13631567Ssef 137144177Salfred if (funcs->type == NULL) { 138144177Salfred funcs = &ex_types[0]; 139144177Salfred warn("execution type %s is not supported -- using %s", 140144177Salfred progt, funcs->type); 141144177Salfred } 142144177Salfred return (funcs); 14331567Ssef} 14431567Ssef 145132306Salfredchar * 146132306Salfredstrsig(int sig) 147132306Salfred{ 148132306Salfred char *ret; 149132306Salfred 150132306Salfred ret = NULL; 151132306Salfred if (sig > 0 && sig < NSIG) { 152132306Salfred int i; 153132306Salfred asprintf(&ret, "sig%s", sys_signame[sig]); 154132306Salfred if (ret == NULL) 155132306Salfred return (NULL); 156132306Salfred for (i = 0; ret[i] != '\0'; ++i) 157132306Salfred ret[i] = toupper(ret[i]); 158132306Salfred } 159132306Salfred return (ret); 160132306Salfred} 161132306Salfred 16232275Scharnierint 163144177Salfredmain(int ac, char **av) 164144177Salfred{ 165144177Salfred int c; 166144177Salfred int i; 167144177Salfred char **command; 168144177Salfred struct ex_types *funcs; 169171055Sdelphij int initial_open; 170144178Salfred char *fname; 171144177Salfred struct trussinfo *trussinfo; 172144177Salfred char *signame; 17331567Ssef 174144178Salfred fname = NULL; 175144178Salfred initial_open = 1; 176144178Salfred 177144177Salfred /* Initialize the trussinfo struct */ 178144177Salfred trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo)); 179144177Salfred if (trussinfo == NULL) 180144177Salfred errx(1, "malloc() failed"); 181144177Salfred bzero(trussinfo, sizeof(struct trussinfo)); 182168569Sdelphij 183144177Salfred trussinfo->outfile = stderr; 184153963Sbrian trussinfo->strsize = 32; 185168569Sdelphij trussinfo->pr_why = S_NONE; 186168569Sdelphij trussinfo->curthread = NULL; 187168569Sdelphij SLIST_INIT(&trussinfo->threadlist); 188153963Sbrian while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) { 189144177Salfred switch (c) { 190144177Salfred case 'p': /* specified pid */ 191144177Salfred trussinfo->pid = atoi(optarg); 192171055Sdelphij /* make sure i don't trace me */ 193171055Sdelphij if(trussinfo->pid == getpid()) { 194171055Sdelphij fprintf(stderr, "attempt to grab self.\n"); 195171055Sdelphij exit(2); 196171055Sdelphij } 197144177Salfred break; 198144177Salfred case 'f': /* Follow fork()'s */ 199144177Salfred trussinfo->flags |= FOLLOWFORKS; 200144177Salfred break; 201144177Salfred case 'a': /* Print execve() argument strings. */ 202144177Salfred trussinfo->flags |= EXECVEARGS; 203144177Salfred break; 204144177Salfred case 'e': /* Print execve() environment strings. */ 205144177Salfred trussinfo->flags |= EXECVEENVS; 206144177Salfred break; 207144177Salfred case 'd': /* Absolute timestamps */ 208144177Salfred trussinfo->flags |= ABSOLUTETIMESTAMPS; 209144177Salfred break; 210144177Salfred case 'D': /* Relative timestamps */ 211144177Salfred trussinfo->flags |= RELATIVETIMESTAMPS; 212144177Salfred break; 213144177Salfred case 'o': /* Specified output file */ 214144177Salfred fname = optarg; 215144177Salfred break; 216153963Sbrian case 's': /* Specified string size */ 217153963Sbrian trussinfo->strsize = atoi(optarg); 218153963Sbrian break; 219144177Salfred case 'S': /* Don't trace signals */ 220144177Salfred trussinfo->flags |= NOSIGS; 221144177Salfred break; 222144177Salfred default: 223144177Salfred usage(); 224144177Salfred } 225144177Salfred } 22631567Ssef 227144177Salfred ac -= optind; av += optind; 228144177Salfred if ((trussinfo->pid == 0 && ac == 0) || 229144177Salfred (trussinfo->pid != 0 && ac != 0)) 230144177Salfred usage(); 23131567Ssef 232144177Salfred if (fname != NULL) { /* Use output file */ 233144177Salfred if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 234144177Salfred errx(1, "cannot open %s", fname); 235144177Salfred } 23632275Scharnier 237144177Salfred /* 238144177Salfred * If truss starts the process itself, it will ignore some signals -- 239144177Salfred * they should be passed off to the process, which may or may not 240144177Salfred * exit. If, however, we are examining an already-running process, 241144177Salfred * then we restore the event mask on these same signals. 242144177Salfred */ 24331567Ssef 244144177Salfred if (trussinfo->pid == 0) { /* Start a command ourselves */ 245144177Salfred command = av; 246144177Salfred trussinfo->pid = setup_and_wait(command); 247144177Salfred signal(SIGINT, SIG_IGN); 248144177Salfred signal(SIGTERM, SIG_IGN); 249144177Salfred signal(SIGQUIT, SIG_IGN); 250144177Salfred } else { 251168569Sdelphij start_tracing(trussinfo->pid); 252144177Salfred signal(SIGINT, restore_proc); 253144177Salfred signal(SIGTERM, restore_proc); 254144177Salfred signal(SIGQUIT, restore_proc); 255144177Salfred } 25631567Ssef 25731567Ssef 258144177Salfred /* 259144177Salfred * At this point, if we started the process, it is stopped waiting to 260144177Salfred * be woken up, either in exit() or in execve(). 261144177Salfred */ 26231567Ssef 263101283SmdoddSTART_TRACE: 264168569Sdelphij funcs = set_etype(trussinfo); 265168569Sdelphij 266144178Salfred initial_open = 0; 267144177Salfred /* 268144177Salfred * At this point, it's a simple loop, waiting for the process to 269144177Salfred * stop, finding out why, printing out why, and then continuing it. 270144177Salfred * All of the grunt work is done in the support routines. 271144177Salfred */ 27231567Ssef 273144177Salfred clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 274101285Smdodd 275144177Salfred do { 276158630Spav struct timespec timediff; 277168569Sdelphij waitevent(trussinfo); 27831567Ssef 279168569Sdelphij switch(i = trussinfo->pr_why) { 280168569Sdelphij case S_SCE: 281168569Sdelphij funcs->enter_syscall(trussinfo, MAXARGS); 282168569Sdelphij clock_gettime(CLOCK_REALTIME, 283168569Sdelphij &trussinfo->before); 284168569Sdelphij break; 285168569Sdelphij case S_SCX: 286168569Sdelphij clock_gettime(CLOCK_REALTIME, 287168569Sdelphij &trussinfo->after); 28831567Ssef 289168569Sdelphij if (trussinfo->curthread->in_fork && 290168569Sdelphij (trussinfo->flags & FOLLOWFORKS)) { 291168569Sdelphij int childpid; 292101283Smdodd 293168569Sdelphij trussinfo->curthread->in_fork = 0; 294168569Sdelphij childpid = 295168569Sdelphij funcs->exit_syscall(trussinfo, 296168569Sdelphij trussinfo->pr_data); 297101283Smdodd 298168569Sdelphij /* 299168569Sdelphij * Fork a new copy of ourself to trace 300168569Sdelphij * the child of the original traced 301168569Sdelphij * process. 302168569Sdelphij */ 303168569Sdelphij if (fork() == 0) { 304168569Sdelphij trussinfo->pid = childpid; 305168569Sdelphij start_tracing(trussinfo->pid); 306168569Sdelphij goto START_TRACE; 307144177Salfred } 308144177Salfred break; 309168569Sdelphij } 310168569Sdelphij funcs->exit_syscall(trussinfo, MAXARGS); 311168569Sdelphij break; 312168569Sdelphij case S_SIG: 313168569Sdelphij if (trussinfo->flags & NOSIGS) 314144177Salfred break; 315168569Sdelphij if (trussinfo->flags & FOLLOWFORKS) 316168569Sdelphij fprintf(trussinfo->outfile, "%5d: ", 317168569Sdelphij trussinfo->pid); 318168569Sdelphij if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 319168569Sdelphij timespecsubt(&trussinfo->after, 320168569Sdelphij &trussinfo->start_time, &timediff); 321168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 322168569Sdelphij (long)timediff.tv_sec, 323168569Sdelphij timediff.tv_nsec); 324144177Salfred } 325168569Sdelphij if (trussinfo->flags & RELATIVETIMESTAMPS) { 326168569Sdelphij timespecsubt(&trussinfo->after, 327168569Sdelphij &trussinfo->before, &timediff); 328168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 329168569Sdelphij (long)timediff.tv_sec, 330168569Sdelphij timediff.tv_nsec); 331168569Sdelphij } 332168569Sdelphij signame = strsig(trussinfo->pr_data); 333168569Sdelphij fprintf(trussinfo->outfile, 334168569Sdelphij "SIGNAL %u (%s)\n", trussinfo->pr_data, 335168569Sdelphij signame == NULL ? "?" : signame); 336168569Sdelphij free(signame); 337168569Sdelphij break; 338168569Sdelphij case S_EXIT: 339168569Sdelphij if (trussinfo->flags & FOLLOWFORKS) 340168569Sdelphij fprintf(trussinfo->outfile, "%5d: ", 341168569Sdelphij trussinfo->pid); 342168569Sdelphij if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 343168569Sdelphij timespecsubt(&trussinfo->after, 344168569Sdelphij &trussinfo->start_time, &timediff); 345168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 346168569Sdelphij (long)timediff.tv_sec, 347168569Sdelphij timediff.tv_nsec); 348168569Sdelphij } 349168569Sdelphij if (trussinfo->flags & RELATIVETIMESTAMPS) { 350168569Sdelphij timespecsubt(&trussinfo->after, 351168569Sdelphij &trussinfo->before, &timediff); 352168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 353168569Sdelphij (long)timediff.tv_sec, timediff.tv_nsec); 354168569Sdelphij } 355168569Sdelphij fprintf(trussinfo->outfile, 356168569Sdelphij "process exit, rval = %u\n", trussinfo->pr_data); 357168569Sdelphij break; 358168569Sdelphij default: 359168569Sdelphij break; 360144177Salfred } 361168569Sdelphij } while (trussinfo->pr_why != S_EXIT); 362144177Salfred fflush(trussinfo->outfile); 363168569Sdelphij 364144177Salfred return (0); 36531567Ssef} 366