main.c revision 153963
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 153963 2006-01-02 08:36:25Z brian $"); 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 102101320Sjake#ifdef __sparc64__ 103144177Salfred { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 104101320Sjake#endif 105144177Salfred { 0, 0, 0 }, 10631567Ssef}; 10731567Ssef 10831567Ssef/* 10931567Ssef * Set the execution type. This is called after every exec, and when 11031567Ssef * a process is first monitored. The procfs pseudo-file "etype" has 11131567Ssef * the execution module type -- see /proc/curproc/etype for an example. 11231567Ssef */ 11331567Ssef 11431567Ssefstatic struct ex_types * 115144177Salfredset_etype(struct trussinfo *trussinfo) 116144177Salfred{ 117144177Salfred struct ex_types *funcs; 118144177Salfred char etype[24]; 119144177Salfred char progt[32]; 120144177Salfred int fd; 12131567Ssef 122144177Salfred sprintf(etype, "/proc/%d/etype", trussinfo->pid); 123144177Salfred if ((fd = open(etype, O_RDONLY)) == -1) { 124144177Salfred strcpy(progt, "FreeBSD a.out"); 125144177Salfred } else { 126144177Salfred int len = read(fd, progt, sizeof(progt)); 127144177Salfred progt[len-1] = '\0'; 128144177Salfred close(fd); 129144177Salfred } 13031567Ssef 131144177Salfred for (funcs = ex_types; funcs->type; funcs++) 132144177Salfred if (!strcmp(funcs->type, progt)) 133144177Salfred break; 13431567Ssef 135144177Salfred if (funcs->type == NULL) { 136144177Salfred funcs = &ex_types[0]; 137144177Salfred warn("execution type %s is not supported -- using %s", 138144177Salfred progt, funcs->type); 139144177Salfred } 140144177Salfred return (funcs); 14131567Ssef} 14231567Ssef 143132306Salfredchar * 144132306Salfredstrsig(int sig) 145132306Salfred{ 146132306Salfred char *ret; 147132306Salfred 148132306Salfred ret = NULL; 149132306Salfred if (sig > 0 && sig < NSIG) { 150132306Salfred int i; 151132306Salfred asprintf(&ret, "sig%s", sys_signame[sig]); 152132306Salfred if (ret == NULL) 153132306Salfred return (NULL); 154132306Salfred for (i = 0; ret[i] != '\0'; ++i) 155132306Salfred ret[i] = toupper(ret[i]); 156132306Salfred } 157132306Salfred return (ret); 158132306Salfred} 159132306Salfred 16032275Scharnierint 161144177Salfredmain(int ac, char **av) 162144177Salfred{ 163144177Salfred int c; 164144177Salfred int i; 165144177Salfred char **command; 166144177Salfred struct procfs_status pfs; 167144177Salfred struct ex_types *funcs; 168144178Salfred int in_exec, sigexit, initial_open; 169144178Salfred char *fname; 170144177Salfred struct trussinfo *trussinfo; 171144177Salfred char *signame; 17231567Ssef 173144178Salfred in_exec = 0; 174144178Salfred sigexit = 0; 175144178Salfred fname = NULL; 176144178Salfred initial_open = 1; 177144178Salfred 178144177Salfred /* Initialize the trussinfo struct */ 179144177Salfred trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo)); 180144177Salfred if (trussinfo == NULL) 181144177Salfred errx(1, "malloc() failed"); 182144177Salfred bzero(trussinfo, sizeof(struct trussinfo)); 183144177Salfred trussinfo->outfile = stderr; 184153963Sbrian trussinfo->strsize = 32; 185101282Smdodd 186153963Sbrian while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) { 187144177Salfred switch (c) { 188144177Salfred case 'p': /* specified pid */ 189144177Salfred trussinfo->pid = atoi(optarg); 190144177Salfred break; 191144177Salfred case 'f': /* Follow fork()'s */ 192144177Salfred trussinfo->flags |= FOLLOWFORKS; 193144177Salfred break; 194144177Salfred case 'a': /* Print execve() argument strings. */ 195144177Salfred trussinfo->flags |= EXECVEARGS; 196144177Salfred break; 197144177Salfred case 'e': /* Print execve() environment strings. */ 198144177Salfred trussinfo->flags |= EXECVEENVS; 199144177Salfred break; 200144177Salfred case 'd': /* Absolute timestamps */ 201144177Salfred trussinfo->flags |= ABSOLUTETIMESTAMPS; 202144177Salfred break; 203144177Salfred case 'D': /* Relative timestamps */ 204144177Salfred trussinfo->flags |= RELATIVETIMESTAMPS; 205144177Salfred break; 206144177Salfred case 'o': /* Specified output file */ 207144177Salfred fname = optarg; 208144177Salfred break; 209153963Sbrian case 's': /* Specified string size */ 210153963Sbrian trussinfo->strsize = atoi(optarg); 211153963Sbrian break; 212144177Salfred case 'S': /* Don't trace signals */ 213144177Salfred trussinfo->flags |= NOSIGS; 214144177Salfred break; 215144177Salfred default: 216144177Salfred usage(); 217144177Salfred } 218144177Salfred } 21931567Ssef 220144177Salfred ac -= optind; av += optind; 221144177Salfred if ((trussinfo->pid == 0 && ac == 0) || 222144177Salfred (trussinfo->pid != 0 && ac != 0)) 223144177Salfred usage(); 22431567Ssef 225144177Salfred if (fname != NULL) { /* Use output file */ 226144177Salfred if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 227144177Salfred errx(1, "cannot open %s", fname); 228144177Salfred } 22932275Scharnier 230144177Salfred /* 231144177Salfred * If truss starts the process itself, it will ignore some signals -- 232144177Salfred * they should be passed off to the process, which may or may not 233144177Salfred * exit. If, however, we are examining an already-running process, 234144177Salfred * then we restore the event mask on these same signals. 235144177Salfred */ 23631567Ssef 237144177Salfred if (trussinfo->pid == 0) { /* Start a command ourselves */ 238144177Salfred command = av; 239144177Salfred trussinfo->pid = setup_and_wait(command); 240144177Salfred signal(SIGINT, SIG_IGN); 241144177Salfred signal(SIGTERM, SIG_IGN); 242144177Salfred signal(SIGQUIT, SIG_IGN); 243144177Salfred } else { 244144177Salfred signal(SIGINT, restore_proc); 245144177Salfred signal(SIGTERM, restore_proc); 246144177Salfred signal(SIGQUIT, restore_proc); 247144177Salfred } 24831567Ssef 24931567Ssef 250144177Salfred /* 251144177Salfred * At this point, if we started the process, it is stopped waiting to 252144177Salfred * be woken up, either in exit() or in execve(). 253144177Salfred */ 25431567Ssef 255101283SmdoddSTART_TRACE: 256144177Salfred Procfd = start_tracing( 257144178Salfred trussinfo->pid, initial_open, 258144178Salfred S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT | 259144177Salfred ((trussinfo->flags & NOSIGS) ? 0 : S_SIG), 260144177Salfred ((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0)); 261144178Salfred initial_open = 0; 262144177Salfred if (Procfd == -1) 263144177Salfred return (0); 26455707Ssef 265144177Salfred pfs.why = 0; 26631567Ssef 267144177Salfred funcs = set_etype(trussinfo); 268144177Salfred /* 269144177Salfred * At this point, it's a simple loop, waiting for the process to 270144177Salfred * stop, finding out why, printing out why, and then continuing it. 271144177Salfred * All of the grunt work is done in the support routines. 272144177Salfred */ 27331567Ssef 274144177Salfred clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 275101285Smdodd 276144177Salfred do { 277144177Salfred int val = 0; 27831567Ssef 279144177Salfred if (ioctl(Procfd, PIOCWAIT, &pfs) == -1) 280144177Salfred warn("PIOCWAIT top of loop"); 281144177Salfred else { 282144177Salfred switch(i = pfs.why) { 283144177Salfred case S_SCE: 284144177Salfred funcs->enter_syscall(trussinfo, pfs.val); 285144177Salfred clock_gettime(CLOCK_REALTIME, 286144177Salfred &trussinfo->before); 287144177Salfred break; 288144177Salfred case S_SCX: 289144177Salfred clock_gettime(CLOCK_REALTIME, 290144177Salfred &trussinfo->after); 291144177Salfred /* 292144177Salfred * This is so we don't get two messages for 293144177Salfred * an exec -- one for the S_EXEC, and one for 294144177Salfred * the syscall exit. It also, conveniently, 295144177Salfred * ensures that the first message printed out 296144177Salfred * isn't the return-from-syscall used to 297144177Salfred * create the process. 298144177Salfred */ 299144177Salfred if (in_exec) { 300144177Salfred in_exec = 0; 301144177Salfred break; 302144177Salfred } 30331567Ssef 304144177Salfred if (trussinfo->in_fork && 305144177Salfred (trussinfo->flags & FOLLOWFORKS)) { 306144177Salfred int childpid; 307101283Smdodd 308144177Salfred trussinfo->in_fork = 0; 309144177Salfred childpid = 310144177Salfred funcs->exit_syscall(trussinfo, 311144177Salfred pfs.val); 312101283Smdodd 313144177Salfred /* 314144177Salfred * Fork a new copy of ourself to trace 315144177Salfred * the child of the original traced 316144177Salfred * process. 317144177Salfred */ 318144177Salfred if (fork() == 0) { 319144177Salfred trussinfo->pid = childpid; 320144177Salfred goto START_TRACE; 321144177Salfred } 322144177Salfred break; 323144177Salfred } 324144177Salfred funcs->exit_syscall(trussinfo, pfs.val); 325144177Salfred break; 326144177Salfred case S_SIG: 327144177Salfred signame = strsig(pfs.val); 328144177Salfred fprintf(trussinfo->outfile, 329144177Salfred "SIGNAL %lu (%s)\n", pfs.val, 330144177Salfred signame == NULL ? "?" : signame); 331144177Salfred free(signame); 332144177Salfred sigexit = pfs.val; 333144177Salfred break; 334144177Salfred case S_EXIT: 335144177Salfred fprintf(trussinfo->outfile, 336144177Salfred "process exit, rval = %lu\n", pfs.val); 337144177Salfred break; 338144177Salfred case S_EXEC: 339144177Salfred funcs = set_etype(trussinfo); 340144177Salfred in_exec = 1; 341144177Salfred break; 342144177Salfred default: 343144177Salfred fprintf(trussinfo->outfile, 344144177Salfred "Process stopped because of: %d\n", i); 345144177Salfred break; 346144177Salfred } 347144177Salfred } 348144177Salfred if (ioctl(Procfd, PIOCCONT, val) == -1) { 349144177Salfred if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH) 350144177Salfred break; 351144177Salfred else 352144177Salfred warn("PIOCCONT"); 353144177Salfred } 354144177Salfred } while (pfs.why != S_EXIT); 355144177Salfred fflush(trussinfo->outfile); 356144177Salfred if (sigexit) { 357144177Salfred struct rlimit rlp; 358101283Smdodd 359144177Salfred rlp.rlim_cur = 0; 360144177Salfred rlp.rlim_max = 0; 361144177Salfred setrlimit(RLIMIT_CORE, &rlp); 362144177Salfred (void) signal(sigexit, SIG_DFL); 363144177Salfred (void) kill(getpid(), sigexit); 364101283Smdodd } 365144177Salfred return (0); 36631567Ssef} 367