main.c revision 154047
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 154047 2006-01-05 05:57:47Z grehan $"); 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> 4285301Sdes#include <sys/ioctl.h> 4385301Sdes#include <sys/pioctl.h> 44123916Scracauer#include <sys/types.h> 45104581Smike#include <sys/time.h> 46123916Scracauer#include <sys/resource.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 6231567Ssef/* 63101282Smdodd * It's difficult to parameterize this because it must be 64101282Smdodd * accessible in a signal handler. 6531567Ssef */ 6631567Ssef 6731567Ssefint Procfd; 6831567Ssef 69144177Salfredstatic void 7032275Scharnierusage(void) 7132275Scharnier{ 72144177Salfred fprintf(stderr, "%s\n%s\n", 73153963Sbrian "usage: truss [-faedDS] [-o file] [-s strsize] -p pid", 74153963Sbrian " truss [-faedDS] [-o file] [-s strsize] command [args]"); 75144177Salfred exit(1); 7631567Ssef} 7731567Ssef 7838897Ssef/* 7938897Ssef * WARNING! "FreeBSD a.out" must be first, or set_etype will not 8038897Ssef * work correctly. 8138897Ssef */ 8231567Ssefstruct ex_types { 83144177Salfred const char *type; 84144177Salfred void (*enter_syscall)(struct trussinfo *, int); 85144177Salfred long (*exit_syscall)(struct trussinfo *, int); 8631567Ssef} ex_types[] = { 8739908Ssef#ifdef __alpha__ 88144177Salfred { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit }, 8939908Ssef#endif 90130394Sdwmalone#ifdef __amd64__ 91144177Salfred { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 92130394Sdwmalone#endif 9339908Ssef#ifdef __i386__ 94144177Salfred { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 95144177Salfred { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 96144177Salfred { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 97144177Salfred { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 9839908Ssef#endif 99106716Smarcel#ifdef __ia64__ 100144177Salfred { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit }, 101106716Smarcel#endif 102154047Sgrehan#ifdef __powerpc__ 103154047Sgrehan { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit }, 104154047Sgrehan { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit }, 105154047Sgrehan#endif 106101320Sjake#ifdef __sparc64__ 107144177Salfred { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 108101320Sjake#endif 109144177Salfred { 0, 0, 0 }, 11031567Ssef}; 11131567Ssef 11231567Ssef/* 11331567Ssef * Set the execution type. This is called after every exec, and when 11431567Ssef * a process is first monitored. The procfs pseudo-file "etype" has 11531567Ssef * the execution module type -- see /proc/curproc/etype for an example. 11631567Ssef */ 11731567Ssef 11831567Ssefstatic struct ex_types * 119144177Salfredset_etype(struct trussinfo *trussinfo) 120144177Salfred{ 121144177Salfred struct ex_types *funcs; 122144177Salfred char etype[24]; 123144177Salfred char progt[32]; 124144177Salfred int fd; 12531567Ssef 126144177Salfred sprintf(etype, "/proc/%d/etype", trussinfo->pid); 127144177Salfred if ((fd = open(etype, O_RDONLY)) == -1) { 128144177Salfred strcpy(progt, "FreeBSD a.out"); 129144177Salfred } else { 130144177Salfred int len = read(fd, progt, sizeof(progt)); 131144177Salfred progt[len-1] = '\0'; 132144177Salfred close(fd); 133144177Salfred } 13431567Ssef 135144177Salfred for (funcs = ex_types; funcs->type; funcs++) 136144177Salfred if (!strcmp(funcs->type, progt)) 137144177Salfred break; 13831567Ssef 139144177Salfred if (funcs->type == NULL) { 140144177Salfred funcs = &ex_types[0]; 141144177Salfred warn("execution type %s is not supported -- using %s", 142144177Salfred progt, funcs->type); 143144177Salfred } 144144177Salfred return (funcs); 14531567Ssef} 14631567Ssef 147132306Salfredchar * 148132306Salfredstrsig(int sig) 149132306Salfred{ 150132306Salfred char *ret; 151132306Salfred 152132306Salfred ret = NULL; 153132306Salfred if (sig > 0 && sig < NSIG) { 154132306Salfred int i; 155132306Salfred asprintf(&ret, "sig%s", sys_signame[sig]); 156132306Salfred if (ret == NULL) 157132306Salfred return (NULL); 158132306Salfred for (i = 0; ret[i] != '\0'; ++i) 159132306Salfred ret[i] = toupper(ret[i]); 160132306Salfred } 161132306Salfred return (ret); 162132306Salfred} 163132306Salfred 16432275Scharnierint 165144177Salfredmain(int ac, char **av) 166144177Salfred{ 167144177Salfred int c; 168144177Salfred int i; 169144177Salfred char **command; 170144177Salfred struct procfs_status pfs; 171144177Salfred struct ex_types *funcs; 172144178Salfred int in_exec, sigexit, initial_open; 173144178Salfred char *fname; 174144177Salfred struct trussinfo *trussinfo; 175144177Salfred char *signame; 17631567Ssef 177144178Salfred in_exec = 0; 178144178Salfred sigexit = 0; 179144178Salfred fname = NULL; 180144178Salfred initial_open = 1; 181144178Salfred 182144177Salfred /* Initialize the trussinfo struct */ 183144177Salfred trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo)); 184144177Salfred if (trussinfo == NULL) 185144177Salfred errx(1, "malloc() failed"); 186144177Salfred bzero(trussinfo, sizeof(struct trussinfo)); 187144177Salfred trussinfo->outfile = stderr; 188153963Sbrian trussinfo->strsize = 32; 189101282Smdodd 190153963Sbrian while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) { 191144177Salfred switch (c) { 192144177Salfred case 'p': /* specified pid */ 193144177Salfred trussinfo->pid = atoi(optarg); 194144177Salfred break; 195144177Salfred case 'f': /* Follow fork()'s */ 196144177Salfred trussinfo->flags |= FOLLOWFORKS; 197144177Salfred break; 198144177Salfred case 'a': /* Print execve() argument strings. */ 199144177Salfred trussinfo->flags |= EXECVEARGS; 200144177Salfred break; 201144177Salfred case 'e': /* Print execve() environment strings. */ 202144177Salfred trussinfo->flags |= EXECVEENVS; 203144177Salfred break; 204144177Salfred case 'd': /* Absolute timestamps */ 205144177Salfred trussinfo->flags |= ABSOLUTETIMESTAMPS; 206144177Salfred break; 207144177Salfred case 'D': /* Relative timestamps */ 208144177Salfred trussinfo->flags |= RELATIVETIMESTAMPS; 209144177Salfred break; 210144177Salfred case 'o': /* Specified output file */ 211144177Salfred fname = optarg; 212144177Salfred break; 213153963Sbrian case 's': /* Specified string size */ 214153963Sbrian trussinfo->strsize = atoi(optarg); 215153963Sbrian break; 216144177Salfred case 'S': /* Don't trace signals */ 217144177Salfred trussinfo->flags |= NOSIGS; 218144177Salfred break; 219144177Salfred default: 220144177Salfred usage(); 221144177Salfred } 222144177Salfred } 22331567Ssef 224144177Salfred ac -= optind; av += optind; 225144177Salfred if ((trussinfo->pid == 0 && ac == 0) || 226144177Salfred (trussinfo->pid != 0 && ac != 0)) 227144177Salfred usage(); 22831567Ssef 229144177Salfred if (fname != NULL) { /* Use output file */ 230144177Salfred if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 231144177Salfred errx(1, "cannot open %s", fname); 232144177Salfred } 23332275Scharnier 234144177Salfred /* 235144177Salfred * If truss starts the process itself, it will ignore some signals -- 236144177Salfred * they should be passed off to the process, which may or may not 237144177Salfred * exit. If, however, we are examining an already-running process, 238144177Salfred * then we restore the event mask on these same signals. 239144177Salfred */ 24031567Ssef 241144177Salfred if (trussinfo->pid == 0) { /* Start a command ourselves */ 242144177Salfred command = av; 243144177Salfred trussinfo->pid = setup_and_wait(command); 244144177Salfred signal(SIGINT, SIG_IGN); 245144177Salfred signal(SIGTERM, SIG_IGN); 246144177Salfred signal(SIGQUIT, SIG_IGN); 247144177Salfred } else { 248144177Salfred signal(SIGINT, restore_proc); 249144177Salfred signal(SIGTERM, restore_proc); 250144177Salfred signal(SIGQUIT, restore_proc); 251144177Salfred } 25231567Ssef 25331567Ssef 254144177Salfred /* 255144177Salfred * At this point, if we started the process, it is stopped waiting to 256144177Salfred * be woken up, either in exit() or in execve(). 257144177Salfred */ 25831567Ssef 259101283SmdoddSTART_TRACE: 260144177Salfred Procfd = start_tracing( 261144178Salfred trussinfo->pid, initial_open, 262144178Salfred S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT | 263144177Salfred ((trussinfo->flags & NOSIGS) ? 0 : S_SIG), 264144177Salfred ((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0)); 265144178Salfred initial_open = 0; 266144177Salfred if (Procfd == -1) 267144177Salfred return (0); 26855707Ssef 269144177Salfred pfs.why = 0; 27031567Ssef 271144177Salfred funcs = set_etype(trussinfo); 272144177Salfred /* 273144177Salfred * At this point, it's a simple loop, waiting for the process to 274144177Salfred * stop, finding out why, printing out why, and then continuing it. 275144177Salfred * All of the grunt work is done in the support routines. 276144177Salfred */ 27731567Ssef 278144177Salfred clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 279101285Smdodd 280144177Salfred do { 281144177Salfred int val = 0; 28231567Ssef 283144177Salfred if (ioctl(Procfd, PIOCWAIT, &pfs) == -1) 284144177Salfred warn("PIOCWAIT top of loop"); 285144177Salfred else { 286144177Salfred switch(i = pfs.why) { 287144177Salfred case S_SCE: 288144177Salfred funcs->enter_syscall(trussinfo, pfs.val); 289144177Salfred clock_gettime(CLOCK_REALTIME, 290144177Salfred &trussinfo->before); 291144177Salfred break; 292144177Salfred case S_SCX: 293144177Salfred clock_gettime(CLOCK_REALTIME, 294144177Salfred &trussinfo->after); 295144177Salfred /* 296144177Salfred * This is so we don't get two messages for 297144177Salfred * an exec -- one for the S_EXEC, and one for 298144177Salfred * the syscall exit. It also, conveniently, 299144177Salfred * ensures that the first message printed out 300144177Salfred * isn't the return-from-syscall used to 301144177Salfred * create the process. 302144177Salfred */ 303144177Salfred if (in_exec) { 304144177Salfred in_exec = 0; 305144177Salfred break; 306144177Salfred } 30731567Ssef 308144177Salfred if (trussinfo->in_fork && 309144177Salfred (trussinfo->flags & FOLLOWFORKS)) { 310144177Salfred int childpid; 311101283Smdodd 312144177Salfred trussinfo->in_fork = 0; 313144177Salfred childpid = 314144177Salfred funcs->exit_syscall(trussinfo, 315144177Salfred pfs.val); 316101283Smdodd 317144177Salfred /* 318144177Salfred * Fork a new copy of ourself to trace 319144177Salfred * the child of the original traced 320144177Salfred * process. 321144177Salfred */ 322144177Salfred if (fork() == 0) { 323144177Salfred trussinfo->pid = childpid; 324144177Salfred goto START_TRACE; 325144177Salfred } 326144177Salfred break; 327144177Salfred } 328144177Salfred funcs->exit_syscall(trussinfo, pfs.val); 329144177Salfred break; 330144177Salfred case S_SIG: 331144177Salfred signame = strsig(pfs.val); 332144177Salfred fprintf(trussinfo->outfile, 333144177Salfred "SIGNAL %lu (%s)\n", pfs.val, 334144177Salfred signame == NULL ? "?" : signame); 335144177Salfred free(signame); 336144177Salfred sigexit = pfs.val; 337144177Salfred break; 338144177Salfred case S_EXIT: 339144177Salfred fprintf(trussinfo->outfile, 340144177Salfred "process exit, rval = %lu\n", pfs.val); 341144177Salfred break; 342144177Salfred case S_EXEC: 343144177Salfred funcs = set_etype(trussinfo); 344144177Salfred in_exec = 1; 345144177Salfred break; 346144177Salfred default: 347144177Salfred fprintf(trussinfo->outfile, 348144177Salfred "Process stopped because of: %d\n", i); 349144177Salfred break; 350144177Salfred } 351144177Salfred } 352144177Salfred if (ioctl(Procfd, PIOCCONT, val) == -1) { 353144177Salfred if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH) 354144177Salfred break; 355144177Salfred else 356144177Salfred warn("PIOCCONT"); 357144177Salfred } 358144177Salfred } while (pfs.why != S_EXIT); 359144177Salfred fflush(trussinfo->outfile); 360144177Salfred if (sigexit) { 361144177Salfred struct rlimit rlp; 362101283Smdodd 363144177Salfred rlp.rlim_cur = 0; 364144177Salfred rlp.rlim_max = 0; 365144177Salfred setrlimit(RLIMIT_CORE, &rlp); 366144177Salfred (void) signal(sigexit, SIG_DFL); 367144177Salfred (void) kill(getpid(), sigexit); 368101283Smdodd } 369144177Salfred return (0); 37031567Ssef} 371