main.c revision 191005
1191005Sdelphij/*- 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 191005 2009-04-13 16:23:32Z delphij $"); 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> 5032275Scharnier#include <errno.h> 5132275Scharnier#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" 6131567Ssef 62171645Smarcel#define MAXARGS 6 6331567Ssef 64144177Salfredstatic void 6532275Scharnierusage(void) 6632275Scharnier{ 67144177Salfred fprintf(stderr, "%s\n%s\n", 68153963Sbrian "usage: truss [-faedDS] [-o file] [-s strsize] -p pid", 69153963Sbrian " truss [-faedDS] [-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 */ 7731567Ssefstruct ex_types { 78144177Salfred const char *type; 79144177Salfred void (*enter_syscall)(struct trussinfo *, int); 80144177Salfred long (*exit_syscall)(struct trussinfo *, int); 8131567Ssef} ex_types[] = { 82130394Sdwmalone#ifdef __amd64__ 83144177Salfred { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 84179051Sjhb { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit }, 85179051Sjhb { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit }, 86130394Sdwmalone#endif 8739908Ssef#ifdef __i386__ 88144177Salfred { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 89144177Salfred { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 90144177Salfred { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 91144177Salfred { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 9239908Ssef#endif 93106716Smarcel#ifdef __ia64__ 94144177Salfred { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit }, 95106716Smarcel#endif 96154047Sgrehan#ifdef __powerpc__ 97154047Sgrehan { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit }, 98154047Sgrehan { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit }, 99154047Sgrehan#endif 100101320Sjake#ifdef __sparc64__ 101144177Salfred { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 102101320Sjake#endif 103188628Simp#ifdef __mips__ 104188628Simp { "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit }, 105188628Simp { "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit }, 106188628Simp { "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX 107188628Simp#endif 108144177Salfred { 0, 0, 0 }, 10931567Ssef}; 11031567Ssef 11131567Ssef/* 11231567Ssef * Set the execution type. This is called after every exec, and when 113168569Sdelphij * a process is first monitored. 11431567Ssef */ 11531567Ssef 11631567Ssefstatic struct ex_types * 117144177Salfredset_etype(struct trussinfo *trussinfo) 118144177Salfred{ 119144177Salfred struct ex_types *funcs; 120144177Salfred char progt[32]; 121168569Sdelphij 122168569Sdelphij size_t len = sizeof(progt); 123168569Sdelphij int mib[4]; 124168569Sdelphij int error; 12531567Ssef 126168569Sdelphij mib[0] = CTL_KERN; 127168569Sdelphij mib[1] = KERN_PROC; 128168569Sdelphij mib[2] = KERN_PROC_SV_NAME; 129168569Sdelphij mib[3] = trussinfo->pid; 130168569Sdelphij error = sysctl(mib, 4, progt, &len, NULL, 0); 131168569Sdelphij if (error != 0) 132168569Sdelphij err(2, "can not get etype"); 13331567Ssef 134144177Salfred for (funcs = ex_types; funcs->type; funcs++) 135144177Salfred if (!strcmp(funcs->type, progt)) 136144177Salfred break; 13731567Ssef 138144177Salfred if (funcs->type == NULL) { 139144177Salfred funcs = &ex_types[0]; 140144177Salfred warn("execution type %s is not supported -- using %s", 141144177Salfred progt, funcs->type); 142144177Salfred } 143144177Salfred return (funcs); 14431567Ssef} 14531567Ssef 146132306Salfredchar * 147132306Salfredstrsig(int sig) 148132306Salfred{ 149132306Salfred char *ret; 150132306Salfred 151132306Salfred ret = NULL; 152132306Salfred if (sig > 0 && sig < NSIG) { 153132306Salfred int i; 154132306Salfred asprintf(&ret, "sig%s", sys_signame[sig]); 155132306Salfred if (ret == NULL) 156132306Salfred return (NULL); 157132306Salfred for (i = 0; ret[i] != '\0'; ++i) 158132306Salfred ret[i] = toupper(ret[i]); 159132306Salfred } 160132306Salfred return (ret); 161132306Salfred} 162132306Salfred 16332275Scharnierint 164144177Salfredmain(int ac, char **av) 165144177Salfred{ 166144177Salfred int c; 167144177Salfred int i; 168191005Sdelphij pid_t childpid; 169191005Sdelphij int status; 170144177Salfred char **command; 171144177Salfred struct ex_types *funcs; 172171055Sdelphij int initial_open; 173144178Salfred char *fname; 174144177Salfred struct trussinfo *trussinfo; 175144177Salfred char *signame; 17631567Ssef 177144178Salfred fname = NULL; 178144178Salfred initial_open = 1; 179144178Salfred 180144177Salfred /* Initialize the trussinfo struct */ 181144177Salfred trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo)); 182144177Salfred if (trussinfo == NULL) 183144177Salfred errx(1, "malloc() failed"); 184144177Salfred bzero(trussinfo, sizeof(struct trussinfo)); 185168569Sdelphij 186144177Salfred trussinfo->outfile = stderr; 187153963Sbrian trussinfo->strsize = 32; 188168569Sdelphij trussinfo->pr_why = S_NONE; 189168569Sdelphij trussinfo->curthread = NULL; 190168569Sdelphij SLIST_INIT(&trussinfo->threadlist); 191153963Sbrian while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) { 192144177Salfred switch (c) { 193144177Salfred case 'p': /* specified pid */ 194144177Salfred trussinfo->pid = atoi(optarg); 195171055Sdelphij /* make sure i don't trace me */ 196171055Sdelphij if(trussinfo->pid == getpid()) { 197171055Sdelphij fprintf(stderr, "attempt to grab self.\n"); 198171055Sdelphij exit(2); 199171055Sdelphij } 200144177Salfred break; 201144177Salfred case 'f': /* Follow fork()'s */ 202144177Salfred trussinfo->flags |= FOLLOWFORKS; 203144177Salfred break; 204144177Salfred case 'a': /* Print execve() argument strings. */ 205144177Salfred trussinfo->flags |= EXECVEARGS; 206144177Salfred break; 207144177Salfred case 'e': /* Print execve() environment strings. */ 208144177Salfred trussinfo->flags |= EXECVEENVS; 209144177Salfred break; 210144177Salfred case 'd': /* Absolute timestamps */ 211144177Salfred trussinfo->flags |= ABSOLUTETIMESTAMPS; 212144177Salfred break; 213144177Salfred case 'D': /* Relative timestamps */ 214144177Salfred trussinfo->flags |= RELATIVETIMESTAMPS; 215144177Salfred break; 216144177Salfred case 'o': /* Specified output file */ 217144177Salfred fname = optarg; 218144177Salfred break; 219153963Sbrian case 's': /* Specified string size */ 220153963Sbrian trussinfo->strsize = atoi(optarg); 221153963Sbrian break; 222144177Salfred case 'S': /* Don't trace signals */ 223144177Salfred trussinfo->flags |= NOSIGS; 224144177Salfred break; 225144177Salfred default: 226144177Salfred usage(); 227144177Salfred } 228144177Salfred } 22931567Ssef 230144177Salfred ac -= optind; av += optind; 231144177Salfred if ((trussinfo->pid == 0 && ac == 0) || 232144177Salfred (trussinfo->pid != 0 && ac != 0)) 233144177Salfred usage(); 23431567Ssef 235144177Salfred if (fname != NULL) { /* Use output file */ 236144177Salfred if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 237144177Salfred errx(1, "cannot open %s", fname); 238144177Salfred } 23932275Scharnier 240144177Salfred /* 241144177Salfred * If truss starts the process itself, it will ignore some signals -- 242144177Salfred * they should be passed off to the process, which may or may not 243144177Salfred * exit. If, however, we are examining an already-running process, 244144177Salfred * then we restore the event mask on these same signals. 245144177Salfred */ 24631567Ssef 247144177Salfred if (trussinfo->pid == 0) { /* Start a command ourselves */ 248144177Salfred command = av; 249144177Salfred trussinfo->pid = setup_and_wait(command); 250144177Salfred signal(SIGINT, SIG_IGN); 251144177Salfred signal(SIGTERM, SIG_IGN); 252144177Salfred signal(SIGQUIT, SIG_IGN); 253144177Salfred } else { 254168569Sdelphij start_tracing(trussinfo->pid); 255144177Salfred signal(SIGINT, restore_proc); 256144177Salfred signal(SIGTERM, restore_proc); 257144177Salfred signal(SIGQUIT, restore_proc); 258144177Salfred } 25931567Ssef 26031567Ssef 261144177Salfred /* 262144177Salfred * At this point, if we started the process, it is stopped waiting to 263144177Salfred * be woken up, either in exit() or in execve(). 264144177Salfred */ 26531567Ssef 266101283SmdoddSTART_TRACE: 267168569Sdelphij funcs = set_etype(trussinfo); 268168569Sdelphij 269144178Salfred initial_open = 0; 270144177Salfred /* 271144177Salfred * At this point, it's a simple loop, waiting for the process to 272144177Salfred * stop, finding out why, printing out why, and then continuing it. 273144177Salfred * All of the grunt work is done in the support routines. 274144177Salfred */ 27531567Ssef 276144177Salfred clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 277101285Smdodd 278144177Salfred do { 279158630Spav struct timespec timediff; 280168569Sdelphij waitevent(trussinfo); 28131567Ssef 282168569Sdelphij switch(i = trussinfo->pr_why) { 283168569Sdelphij case S_SCE: 284168569Sdelphij funcs->enter_syscall(trussinfo, MAXARGS); 285168569Sdelphij clock_gettime(CLOCK_REALTIME, 286168569Sdelphij &trussinfo->before); 287168569Sdelphij break; 288168569Sdelphij case S_SCX: 289168569Sdelphij clock_gettime(CLOCK_REALTIME, 290168569Sdelphij &trussinfo->after); 29131567Ssef 292168569Sdelphij if (trussinfo->curthread->in_fork && 293168569Sdelphij (trussinfo->flags & FOLLOWFORKS)) { 294168569Sdelphij trussinfo->curthread->in_fork = 0; 295168569Sdelphij childpid = 296168569Sdelphij funcs->exit_syscall(trussinfo, 297168569Sdelphij trussinfo->pr_data); 298101283Smdodd 299168569Sdelphij /* 300168569Sdelphij * Fork a new copy of ourself to trace 301168569Sdelphij * the child of the original traced 302168569Sdelphij * process. 303168569Sdelphij */ 304168569Sdelphij if (fork() == 0) { 305168569Sdelphij trussinfo->pid = childpid; 306168569Sdelphij start_tracing(trussinfo->pid); 307168569Sdelphij goto START_TRACE; 308144177Salfred } 309144177Salfred break; 310168569Sdelphij } 311168569Sdelphij funcs->exit_syscall(trussinfo, MAXARGS); 312168569Sdelphij break; 313168569Sdelphij case S_SIG: 314168569Sdelphij if (trussinfo->flags & NOSIGS) 315144177Salfred break; 316168569Sdelphij if (trussinfo->flags & FOLLOWFORKS) 317168569Sdelphij fprintf(trussinfo->outfile, "%5d: ", 318168569Sdelphij trussinfo->pid); 319168569Sdelphij if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 320168569Sdelphij timespecsubt(&trussinfo->after, 321168569Sdelphij &trussinfo->start_time, &timediff); 322168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 323168569Sdelphij (long)timediff.tv_sec, 324168569Sdelphij timediff.tv_nsec); 325144177Salfred } 326168569Sdelphij if (trussinfo->flags & RELATIVETIMESTAMPS) { 327168569Sdelphij timespecsubt(&trussinfo->after, 328168569Sdelphij &trussinfo->before, &timediff); 329168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 330168569Sdelphij (long)timediff.tv_sec, 331168569Sdelphij timediff.tv_nsec); 332168569Sdelphij } 333168569Sdelphij signame = strsig(trussinfo->pr_data); 334168569Sdelphij fprintf(trussinfo->outfile, 335168569Sdelphij "SIGNAL %u (%s)\n", trussinfo->pr_data, 336168569Sdelphij signame == NULL ? "?" : signame); 337168569Sdelphij free(signame); 338168569Sdelphij break; 339168569Sdelphij case S_EXIT: 340168569Sdelphij if (trussinfo->flags & FOLLOWFORKS) 341168569Sdelphij fprintf(trussinfo->outfile, "%5d: ", 342168569Sdelphij trussinfo->pid); 343168569Sdelphij if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 344168569Sdelphij timespecsubt(&trussinfo->after, 345168569Sdelphij &trussinfo->start_time, &timediff); 346168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 347168569Sdelphij (long)timediff.tv_sec, 348168569Sdelphij timediff.tv_nsec); 349168569Sdelphij } 350168569Sdelphij if (trussinfo->flags & RELATIVETIMESTAMPS) { 351168569Sdelphij timespecsubt(&trussinfo->after, 352168569Sdelphij &trussinfo->before, &timediff); 353168569Sdelphij fprintf(trussinfo->outfile, "%ld.%09ld ", 354168569Sdelphij (long)timediff.tv_sec, timediff.tv_nsec); 355168569Sdelphij } 356168569Sdelphij fprintf(trussinfo->outfile, 357168569Sdelphij "process exit, rval = %u\n", trussinfo->pr_data); 358168569Sdelphij break; 359168569Sdelphij default: 360168569Sdelphij break; 361144177Salfred } 362168569Sdelphij } while (trussinfo->pr_why != S_EXIT); 363144177Salfred fflush(trussinfo->outfile); 364191005Sdelphij 365191005Sdelphij if (trussinfo->flags & FOLLOWFORKS) 366191005Sdelphij do { 367191005Sdelphij childpid = wait(&status); 368191005Sdelphij } while (childpid != -1); 369191005Sdelphij 370144177Salfred return (0); 37131567Ssef} 372