main.c revision 192025
1/*- 2 * Copryight 1997 Sean Eric Fagan 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. All advertising materials mentioning features or use of this software 13 * must display the following acknowledgement: 14 * This product includes software developed by Sean Eric Fagan 15 * 4. Neither the name of the author may be used to endorse or promote 16 * products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/usr.bin/truss/main.c 192025 2009-05-12 20:42:12Z dds $"); 34 35/* 36 * The main module for truss. Suprisingly simple, but, then, the other 37 * files handle the bulk of the work. And, of course, the kernel has to 38 * do a lot of the work :). 39 */ 40 41#include <sys/param.h> 42#include <sys/types.h> 43#include <sys/time.h> 44#include <sys/resource.h> 45#include <sys/sysctl.h> 46#include <sys/wait.h> 47 48#include <ctype.h> 49#include <err.h> 50#include <errno.h> 51#include <fcntl.h> 52#include <signal.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <time.h> 57#include <unistd.h> 58 59#include "truss.h" 60#include "extern.h" 61#include "syscall.h" 62 63#define MAXARGS 6 64 65static void 66usage(void) 67{ 68 fprintf(stderr, "%s\n%s\n", 69 "usage: truss [-cfaedDS] [-o file] [-s strsize] -p pid", 70 " truss [-cfaedDS] [-o file] [-s strsize] command [args]"); 71 exit(1); 72} 73 74/* 75 * WARNING! "FreeBSD a.out" must be first, or set_etype will not 76 * work correctly. 77 */ 78struct ex_types { 79 const char *type; 80 void (*enter_syscall)(struct trussinfo *, int); 81 long (*exit_syscall)(struct trussinfo *, int); 82} ex_types[] = { 83#ifdef __amd64__ 84 { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 85 { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit }, 86 { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit }, 87#endif 88#ifdef __i386__ 89 { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 90 { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 91 { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 92 { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 93#endif 94#ifdef __ia64__ 95 { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit }, 96#endif 97#ifdef __powerpc__ 98 { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit }, 99 { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit }, 100#endif 101#ifdef __sparc64__ 102 { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 103#endif 104#ifdef __mips__ 105 { "FreeBSD ELF", mips_syscall_entry, mips_syscall_exit }, 106 { "FreeBSD ELF32", mips_syscall_entry, mips_syscall_exit }, 107 { "FreeBSD ELF64", mips_syscall_entry, mips_syscall_exit }, // XXX 108#endif 109 { 0, 0, 0 }, 110}; 111 112/* 113 * Set the execution type. This is called after every exec, and when 114 * a process is first monitored. 115 */ 116 117static struct ex_types * 118set_etype(struct trussinfo *trussinfo) 119{ 120 struct ex_types *funcs; 121 char progt[32]; 122 123 size_t len = sizeof(progt); 124 int mib[4]; 125 int error; 126 127 mib[0] = CTL_KERN; 128 mib[1] = KERN_PROC; 129 mib[2] = KERN_PROC_SV_NAME; 130 mib[3] = trussinfo->pid; 131 error = sysctl(mib, 4, progt, &len, NULL, 0); 132 if (error != 0) 133 err(2, "can not get etype"); 134 135 for (funcs = ex_types; funcs->type; funcs++) 136 if (!strcmp(funcs->type, progt)) 137 break; 138 139 if (funcs->type == NULL) { 140 funcs = &ex_types[0]; 141 warn("execution type %s is not supported -- using %s", 142 progt, funcs->type); 143 } 144 return (funcs); 145} 146 147char * 148strsig(int sig) 149{ 150 char *ret; 151 152 ret = NULL; 153 if (sig > 0 && sig < NSIG) { 154 int i; 155 asprintf(&ret, "sig%s", sys_signame[sig]); 156 if (ret == NULL) 157 return (NULL); 158 for (i = 0; ret[i] != '\0'; ++i) 159 ret[i] = toupper(ret[i]); 160 } 161 return (ret); 162} 163 164int 165main(int ac, char **av) 166{ 167 int c; 168 int i; 169 pid_t childpid; 170 int status; 171 char **command; 172 struct ex_types *funcs; 173 int initial_open; 174 char *fname; 175 struct trussinfo *trussinfo; 176 char *signame; 177 178 fname = NULL; 179 initial_open = 1; 180 181 /* Initialize the trussinfo struct */ 182 trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo)); 183 if (trussinfo == NULL) 184 errx(1, "malloc() failed"); 185 bzero(trussinfo, sizeof(struct trussinfo)); 186 187 trussinfo->outfile = stderr; 188 trussinfo->strsize = 32; 189 trussinfo->pr_why = S_NONE; 190 trussinfo->curthread = NULL; 191 SLIST_INIT(&trussinfo->threadlist); 192 while ((c = getopt(ac, av, "p:o:facedDs:S")) != -1) { 193 switch (c) { 194 case 'p': /* specified pid */ 195 trussinfo->pid = atoi(optarg); 196 /* make sure i don't trace me */ 197 if(trussinfo->pid == getpid()) { 198 fprintf(stderr, "attempt to grab self.\n"); 199 exit(2); 200 } 201 break; 202 case 'f': /* Follow fork()'s */ 203 trussinfo->flags |= FOLLOWFORKS; 204 break; 205 case 'a': /* Print execve() argument strings. */ 206 trussinfo->flags |= EXECVEARGS; 207 break; 208 case 'c': /* Count number of system calls and time. */ 209 trussinfo->flags |= COUNTONLY; 210 break; 211 case 'e': /* Print execve() environment strings. */ 212 trussinfo->flags |= EXECVEENVS; 213 break; 214 case 'd': /* Absolute timestamps */ 215 trussinfo->flags |= ABSOLUTETIMESTAMPS; 216 break; 217 case 'D': /* Relative timestamps */ 218 trussinfo->flags |= RELATIVETIMESTAMPS; 219 break; 220 case 'o': /* Specified output file */ 221 fname = optarg; 222 break; 223 case 's': /* Specified string size */ 224 trussinfo->strsize = atoi(optarg); 225 break; 226 case 'S': /* Don't trace signals */ 227 trussinfo->flags |= NOSIGS; 228 break; 229 default: 230 usage(); 231 } 232 } 233 234 ac -= optind; av += optind; 235 if ((trussinfo->pid == 0 && ac == 0) || 236 (trussinfo->pid != 0 && ac != 0)) 237 usage(); 238 239 if (fname != NULL) { /* Use output file */ 240 if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 241 errx(1, "cannot open %s", fname); 242 } 243 244 /* 245 * If truss starts the process itself, it will ignore some signals -- 246 * they should be passed off to the process, which may or may not 247 * exit. If, however, we are examining an already-running process, 248 * then we restore the event mask on these same signals. 249 */ 250 251 if (trussinfo->pid == 0) { /* Start a command ourselves */ 252 command = av; 253 trussinfo->pid = setup_and_wait(command); 254 signal(SIGINT, SIG_IGN); 255 signal(SIGTERM, SIG_IGN); 256 signal(SIGQUIT, SIG_IGN); 257 } else { 258 start_tracing(trussinfo->pid); 259 signal(SIGINT, restore_proc); 260 signal(SIGTERM, restore_proc); 261 signal(SIGQUIT, restore_proc); 262 } 263 264 265 /* 266 * At this point, if we started the process, it is stopped waiting to 267 * be woken up, either in exit() or in execve(). 268 */ 269 270START_TRACE: 271 funcs = set_etype(trussinfo); 272 273 initial_open = 0; 274 /* 275 * At this point, it's a simple loop, waiting for the process to 276 * stop, finding out why, printing out why, and then continuing it. 277 * All of the grunt work is done in the support routines. 278 */ 279 280 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 281 282 do { 283 struct timespec timediff; 284 waitevent(trussinfo); 285 286 switch(i = trussinfo->pr_why) { 287 case S_SCE: 288 funcs->enter_syscall(trussinfo, MAXARGS); 289 clock_gettime(CLOCK_REALTIME, 290 &trussinfo->before); 291 break; 292 case S_SCX: 293 clock_gettime(CLOCK_REALTIME, 294 &trussinfo->after); 295 296 if (trussinfo->curthread->in_fork && 297 (trussinfo->flags & FOLLOWFORKS)) { 298 trussinfo->curthread->in_fork = 0; 299 childpid = 300 funcs->exit_syscall(trussinfo, 301 trussinfo->pr_data); 302 303 /* 304 * Fork a new copy of ourself to trace 305 * the child of the original traced 306 * process. 307 */ 308 if (fork() == 0) { 309 trussinfo->pid = childpid; 310 start_tracing(trussinfo->pid); 311 goto START_TRACE; 312 } 313 break; 314 } 315 funcs->exit_syscall(trussinfo, MAXARGS); 316 break; 317 case S_SIG: 318 if (trussinfo->flags & NOSIGS) 319 break; 320 if (trussinfo->flags & FOLLOWFORKS) 321 fprintf(trussinfo->outfile, "%5d: ", 322 trussinfo->pid); 323 if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 324 timespecsubt(&trussinfo->after, 325 &trussinfo->start_time, &timediff); 326 fprintf(trussinfo->outfile, "%ld.%09ld ", 327 (long)timediff.tv_sec, 328 timediff.tv_nsec); 329 } 330 if (trussinfo->flags & RELATIVETIMESTAMPS) { 331 timespecsubt(&trussinfo->after, 332 &trussinfo->before, &timediff); 333 fprintf(trussinfo->outfile, "%ld.%09ld ", 334 (long)timediff.tv_sec, 335 timediff.tv_nsec); 336 } 337 signame = strsig(trussinfo->pr_data); 338 fprintf(trussinfo->outfile, 339 "SIGNAL %u (%s)\n", trussinfo->pr_data, 340 signame == NULL ? "?" : signame); 341 free(signame); 342 break; 343 case S_EXIT: 344 if (trussinfo->flags & COUNTONLY) 345 break; 346 if (trussinfo->flags & FOLLOWFORKS) 347 fprintf(trussinfo->outfile, "%5d: ", 348 trussinfo->pid); 349 if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 350 timespecsubt(&trussinfo->after, 351 &trussinfo->start_time, &timediff); 352 fprintf(trussinfo->outfile, "%ld.%09ld ", 353 (long)timediff.tv_sec, 354 timediff.tv_nsec); 355 } 356 if (trussinfo->flags & RELATIVETIMESTAMPS) { 357 timespecsubt(&trussinfo->after, 358 &trussinfo->before, &timediff); 359 fprintf(trussinfo->outfile, "%ld.%09ld ", 360 (long)timediff.tv_sec, timediff.tv_nsec); 361 } 362 fprintf(trussinfo->outfile, 363 "process exit, rval = %u\n", trussinfo->pr_data); 364 break; 365 default: 366 break; 367 } 368 } while (trussinfo->pr_why != S_EXIT); 369 370 if (trussinfo->flags & FOLLOWFORKS) 371 do { 372 childpid = wait(&status); 373 } while (childpid != -1); 374 375 if (trussinfo->flags & COUNTONLY) 376 print_summary(trussinfo); 377 378 fflush(trussinfo->outfile); 379 380 return (0); 381} 382