main.c revision 211935
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: head/usr.bin/truss/main.c 211935 2010-08-28 15:04:53Z nwhitehorn $"); 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> 46191005Sdelphij#include <sys/wait.h> 4785301Sdes 48132306Salfred#include <ctype.h> 4932275Scharnier#include <err.h> 50200462Sdelphij#include <errno.h> 51200462Sdelphij#include <fcntl.h> 5232275Scharnier#include <signal.h> 5331567Ssef#include <stdio.h> 5431567Ssef#include <stdlib.h> 5531567Ssef#include <string.h> 56101423Smdodd#include <time.h> 5731579Speter#include <unistd.h> 5831567Ssef 59101282Smdodd#include "truss.h" 6087703Smarkm#include "extern.h" 61192025Sdds#include "syscall.h" 6231567Ssef 63171645Smarcel#define MAXARGS 6 6431567Ssef 65144177Salfredstatic void 6632275Scharnierusage(void) 6732275Scharnier{ 68144177Salfred fprintf(stderr, "%s\n%s\n", 69192025Sdds "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid", 70192025Sdds " truss [-cfaedDS] [-o file] [-s strsize] command [args]"); 71144177Salfred exit(1); 7231567Ssef} 7331567Ssef 7438897Ssef/* 7538897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not 7638897Ssef * work correctly. 7738897Ssef */ 7831567Ssefstruct ex_types { 79144177Salfred const char *type; 80144177Salfred void (*enter_syscall)(struct trussinfo *, int); 81144177Salfred long (*exit_syscall)(struct trussinfo *, int); 8231567Ssef} ex_types[] = { 83130394Sdwmalone#ifdef __amd64__ 84144177Salfred { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 85179051Sjhb { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit }, 86179051Sjhb { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit }, 87130394Sdwmalone#endif 8839908Ssef#ifdef __i386__ 89144177Salfred { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 90144177Salfred { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 91144177Salfred { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 92144177Salfred { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 9339908Ssef#endif 94106716Smarcel#ifdef __ia64__ 95144177Salfred { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit }, 96106716Smarcel#endif 97154047Sgrehan#ifdef __powerpc__ 98154047Sgrehan { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit }, 99154047Sgrehan { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit }, 100211935Snwhitehorn#ifdef __powerpc64__ 101211935Snwhitehorn { "FreeBSD ELF64", powerpc64_syscall_entry, powerpc64_syscall_exit }, 102154047Sgrehan#endif 103211935Snwhitehorn#endif 104101320Sjake#ifdef __sparc64__ 105144177Salfred { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 106101320Sjake#endif 107188628Simp#ifdef __mips__ 108188628Simp { "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit }, 109188628Simp { "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit }, 110188628Simp { "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX 111188628Simp#endif 112144177Salfred { 0, 0, 0 }, 11331567Ssef}; 11431567Ssef 11531567Ssef/* 11631567Ssef * Set the execution type. This is called after every exec, and when 117168569Sdelphij * a process is first monitored. 11831567Ssef */ 11931567Ssef 12031567Ssefstatic struct ex_types * 121144177Salfredset_etype(struct trussinfo *trussinfo) 122144177Salfred{ 123144177Salfred struct ex_types *funcs; 124144177Salfred char progt[32]; 125168569Sdelphij 126168569Sdelphij size_t len = sizeof(progt); 127168569Sdelphij int mib[4]; 128168569Sdelphij int error; 12931567Ssef 130168569Sdelphij mib[0] = CTL_KERN; 131168569Sdelphij mib[1] = KERN_PROC; 132168569Sdelphij mib[2] = KERN_PROC_SV_NAME; 133168569Sdelphij mib[3] = trussinfo->pid; 134168569Sdelphij error = sysctl(mib, 4, progt, &len, NULL, 0); 135168569Sdelphij if (error != 0) 136168569Sdelphij err(2, "can not get etype"); 13731567Ssef 138144177Salfred for (funcs = ex_types; funcs->type; funcs++) 139144177Salfred if (!strcmp(funcs->type, progt)) 140144177Salfred break; 14131567Ssef 142144177Salfred if (funcs->type == NULL) { 143144177Salfred funcs = &ex_types[0]; 144144177Salfred warn("execution type %s is not supported -- using %s", 145144177Salfred progt, funcs->type); 146144177Salfred } 147144177Salfred return (funcs); 14831567Ssef} 14931567Ssef 150132306Salfredchar * 151132306Salfredstrsig(int sig) 152132306Salfred{ 153132306Salfred char *ret; 154132306Salfred 155132306Salfred ret = NULL; 156132306Salfred if (sig > 0 && sig < NSIG) { 157132306Salfred int i; 158132306Salfred asprintf(&ret, "sig%s", sys_signame[sig]); 159132306Salfred if (ret == NULL) 160132306Salfred return (NULL); 161132306Salfred for (i = 0; ret[i] != '\0'; ++i) 162132306Salfred ret[i] = toupper(ret[i]); 163132306Salfred } 164132306Salfred return (ret); 165132306Salfred} 166132306Salfred 16732275Scharnierint 168144177Salfredmain(int ac, char **av) 169144177Salfred{ 170144177Salfred int c; 171144177Salfred int i; 172191005Sdelphij pid_t childpid; 173191005Sdelphij int status; 174144177Salfred char **command; 175144177Salfred struct ex_types *funcs; 176171055Sdelphij int initial_open; 177144178Salfred char *fname; 178144177Salfred struct trussinfo *trussinfo; 179144177Salfred char *signame; 18031567Ssef 181144178Salfred fname = NULL; 182144178Salfred initial_open = 1; 183144178Salfred 184144177Salfred /* Initialize the trussinfo struct */ 185192153Sdelphij trussinfo = (struct trussinfo *)calloc(1, sizeof(struct trussinfo)); 186144177Salfred if (trussinfo == NULL) 187192153Sdelphij errx(1, "calloc() failed"); 188192153Sdelphij 189144177Salfred trussinfo->outfile = stderr; 190153963Sbrian trussinfo->strsize = 32; 191168569Sdelphij trussinfo->pr_why = S_NONE; 192168569Sdelphij trussinfo->curthread = NULL; 193168569Sdelphij SLIST_INIT(&trussinfo->threadlist); 194192025Sdds while ((c = getopt(ac, av, "p:o:facedDs:S")) != -1) { 195144177Salfred switch (c) { 196144177Salfred case 'p': /* specified pid */ 197144177Salfred trussinfo->pid = atoi(optarg); 198171055Sdelphij /* make sure i don't trace me */ 199171055Sdelphij if(trussinfo->pid == getpid()) { 200171055Sdelphij fprintf(stderr, "attempt to grab self.\n"); 201171055Sdelphij exit(2); 202171055Sdelphij } 203144177Salfred break; 204144177Salfred case 'f': /* Follow fork()'s */ 205144177Salfred trussinfo->flags |= FOLLOWFORKS; 206144177Salfred break; 207144177Salfred case 'a': /* Print execve() argument strings. */ 208144177Salfred trussinfo->flags |= EXECVEARGS; 209144177Salfred break; 210192025Sdds case 'c': /* Count number of system calls and time. */ 211192025Sdds trussinfo->flags |= COUNTONLY; 212192025Sdds break; 213144177Salfred case 'e': /* Print execve() environment strings. */ 214144177Salfred trussinfo->flags |= EXECVEENVS; 215144177Salfred break; 216144177Salfred case 'd': /* Absolute timestamps */ 217144177Salfred trussinfo->flags |= ABSOLUTETIMESTAMPS; 218144177Salfred break; 219144177Salfred case 'D': /* Relative timestamps */ 220144177Salfred trussinfo->flags |= RELATIVETIMESTAMPS; 221144177Salfred break; 222144177Salfred case 'o': /* Specified output file */ 223144177Salfred fname = optarg; 224144177Salfred break; 225153963Sbrian case 's': /* Specified string size */ 226153963Sbrian trussinfo->strsize = atoi(optarg); 227153963Sbrian break; 228144177Salfred case 'S': /* Don't trace signals */ 229144177Salfred trussinfo->flags |= NOSIGS; 230144177Salfred break; 231144177Salfred default: 232144177Salfred usage(); 233144177Salfred } 234144177Salfred } 23531567Ssef 236144177Salfred ac -= optind; av += optind; 237144177Salfred if ((trussinfo->pid == 0 && ac == 0) || 238144177Salfred (trussinfo->pid != 0 && ac != 0)) 239144177Salfred usage(); 24031567Ssef 241144177Salfred if (fname != NULL) { /* Use output file */ 242144177Salfred if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 243144177Salfred errx(1, "cannot open %s", fname); 244144177Salfred } 245200752Sjh /* 246200752Sjh * Set FD_CLOEXEC, so that the output file is not shared with 247200752Sjh * the traced process. 248200752Sjh */ 249200752Sjh if (fcntl(fileno(trussinfo->outfile), F_SETFD, FD_CLOEXEC) == -1) 250200752Sjh warn("fcntl()"); 25132275Scharnier 252144177Salfred /* 253144177Salfred * If truss starts the process itself, it will ignore some signals -- 254144177Salfred * they should be passed off to the process, which may or may not 255144177Salfred * exit. If, however, we are examining an already-running process, 256144177Salfred * then we restore the event mask on these same signals. 257144177Salfred */ 25831567Ssef 259144177Salfred if (trussinfo->pid == 0) { /* Start a command ourselves */ 260144177Salfred command = av; 261144177Salfred trussinfo->pid = setup_and_wait(command); 262144177Salfred signal(SIGINT, SIG_IGN); 263144177Salfred signal(SIGTERM, SIG_IGN); 264144177Salfred signal(SIGQUIT, SIG_IGN); 265144177Salfred } else { 266168569Sdelphij start_tracing(trussinfo->pid); 267144177Salfred signal(SIGINT, restore_proc); 268144177Salfred signal(SIGTERM, restore_proc); 269144177Salfred signal(SIGQUIT, restore_proc); 270144177Salfred } 27131567Ssef 27231567Ssef 273144177Salfred /* 274144177Salfred * At this point, if we started the process, it is stopped waiting to 275144177Salfred * be woken up, either in exit() or in execve(). 276144177Salfred */ 27731567Ssef 278101283SmdoddSTART_TRACE: 279168569Sdelphij funcs = set_etype(trussinfo); 280168569Sdelphij 281144178Salfred initial_open = 0; 282144177Salfred /* 283144177Salfred * At this point, it's a simple loop, waiting for the process to 284144177Salfred * stop, finding out why, printing out why, and then continuing it. 285144177Salfred * All of the grunt work is done in the support routines. 286144177Salfred */ 28731567Ssef 288144177Salfred clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 289101285Smdodd 290144177Salfred do { 291158630Spav struct timespec timediff; 292168569Sdelphij waitevent(trussinfo); 29331567Ssef 294168569Sdelphij switch(i = trussinfo->pr_why) { 295168569Sdelphij case S_SCE: 296168569Sdelphij funcs->enter_syscall(trussinfo, MAXARGS); 297168569Sdelphij clock_gettime(CLOCK_REALTIME, 298168569Sdelphij &trussinfo->before); 299168569Sdelphij break; 300168569Sdelphij case S_SCX: 301168569Sdelphij clock_gettime(CLOCK_REALTIME, 302168569Sdelphij &trussinfo->after); 30331567Ssef 304168569Sdelphij if (trussinfo->curthread->in_fork && 305168569Sdelphij (trussinfo->flags & FOLLOWFORKS)) { 306168569Sdelphij trussinfo->curthread->in_fork = 0; 307168569Sdelphij childpid = 308168569Sdelphij funcs->exit_syscall(trussinfo, 309168569Sdelphij trussinfo->pr_data); 310101283Smdodd 311168569Sdelphij /* 312168569Sdelphij * Fork a new copy of ourself to trace 313168569Sdelphij * the child of the original traced 314168569Sdelphij * process. 315168569Sdelphij */ 316168569Sdelphij if (fork() == 0) { 317168569Sdelphij trussinfo->pid = childpid; 318168569Sdelphij start_tracing(trussinfo->pid); 319168569Sdelphij goto START_TRACE; 320144177Salfred } 321144177Salfred break; 322168569Sdelphij } 323168569Sdelphij funcs->exit_syscall(trussinfo, MAXARGS); 324168569Sdelphij break; 325168569Sdelphij case S_SIG: 326168569Sdelphij if (trussinfo->flags & NOSIGS) 327144177Salfred break; 328168569Sdelphij if (trussinfo->flags & FOLLOWFORKS) 329168569Sdelphij fprintf(trussinfo->outfile, "%5d: ", 330168569Sdelphij trussinfo->pid); 331168569Sdelphij if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 332168569Sdelphij timespecsubt(&trussinfo->after, 333168569Sdelphij &trussinfo->start_time, &timediff); 334168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 335168569Sdelphij (long)timediff.tv_sec, 336168569Sdelphij timediff.tv_nsec); 337144177Salfred } 338168569Sdelphij if (trussinfo->flags & RELATIVETIMESTAMPS) { 339168569Sdelphij timespecsubt(&trussinfo->after, 340168569Sdelphij &trussinfo->before, &timediff); 341168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 342168569Sdelphij (long)timediff.tv_sec, 343168569Sdelphij timediff.tv_nsec); 344168569Sdelphij } 345168569Sdelphij signame = strsig(trussinfo->pr_data); 346168569Sdelphij fprintf(trussinfo->outfile, 347168569Sdelphij "SIGNAL %u (%s)\n", trussinfo->pr_data, 348168569Sdelphij signame == NULL ? "?" : signame); 349168569Sdelphij free(signame); 350168569Sdelphij break; 351168569Sdelphij case S_EXIT: 352192025Sdds if (trussinfo->flags & COUNTONLY) 353192025Sdds break; 354168569Sdelphij if (trussinfo->flags & FOLLOWFORKS) 355168569Sdelphij fprintf(trussinfo->outfile, "%5d: ", 356168569Sdelphij trussinfo->pid); 357168569Sdelphij if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 358168569Sdelphij timespecsubt(&trussinfo->after, 359168569Sdelphij &trussinfo->start_time, &timediff); 360168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 361168569Sdelphij (long)timediff.tv_sec, 362168569Sdelphij timediff.tv_nsec); 363168569Sdelphij } 364168569Sdelphij if (trussinfo->flags & RELATIVETIMESTAMPS) { 365168569Sdelphij timespecsubt(&trussinfo->after, 366168569Sdelphij &trussinfo->before, &timediff); 367168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 368168569Sdelphij (long)timediff.tv_sec, timediff.tv_nsec); 369168569Sdelphij } 370168569Sdelphij fprintf(trussinfo->outfile, 371168569Sdelphij "process exit, rval = %u\n", trussinfo->pr_data); 372168569Sdelphij break; 373168569Sdelphij default: 374168569Sdelphij break; 375144177Salfred } 376168569Sdelphij } while (trussinfo->pr_why != S_EXIT); 377191005Sdelphij 378191005Sdelphij if (trussinfo->flags & FOLLOWFORKS) 379191005Sdelphij do { 380191005Sdelphij childpid = wait(&status); 381191005Sdelphij } while (childpid != -1); 382191005Sdelphij 383192025Sdds if (trussinfo->flags & COUNTONLY) 384192025Sdds print_summary(trussinfo); 385192025Sdds 386192025Sdds fflush(trussinfo->outfile); 387192025Sdds 388144177Salfred return (0); 38931567Ssef} 390