main.c revision 179051
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 179051 2008-05-16 15:34:06Z jhb $"); 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 47#include <ctype.h> 48#include <err.h> 49#include <errno.h> 50#include <fcntl.h> 51#include <signal.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <time.h> 56#include <unistd.h> 57 58#include "truss.h" 59#include "extern.h" 60 61#define MAXARGS 6 62 63static void 64usage(void) 65{ 66 fprintf(stderr, "%s\n%s\n", 67 "usage: truss [-faedDS] [-o file] [-s strsize] -p pid", 68 " truss [-faedDS] [-o file] [-s strsize] command [args]"); 69 exit(1); 70} 71 72/* 73 * WARNING! "FreeBSD a.out" must be first, or set_etype will not 74 * work correctly. 75 */ 76struct ex_types { 77 const char *type; 78 void (*enter_syscall)(struct trussinfo *, int); 79 long (*exit_syscall)(struct trussinfo *, int); 80} ex_types[] = { 81#ifdef __alpha__ 82 { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit }, 83#endif 84#ifdef __amd64__ 85 { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 86 { "FreeBSD ELF32", amd64_fbsd32_syscall_entry, amd64_fbsd32_syscall_exit }, 87 { "Linux ELF32", amd64_linux32_syscall_entry, amd64_linux32_syscall_exit }, 88#endif 89#ifdef __i386__ 90 { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 91 { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 92 { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 93 { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 94#endif 95#ifdef __ia64__ 96 { "FreeBSD ELF64", ia64_syscall_entry, ia64_syscall_exit }, 97#endif 98#ifdef __powerpc__ 99 { "FreeBSD ELF", powerpc_syscall_entry, powerpc_syscall_exit }, 100 { "FreeBSD ELF32", powerpc_syscall_entry, powerpc_syscall_exit }, 101#endif 102#ifdef __sparc64__ 103 { "FreeBSD ELF64", sparc64_syscall_entry, sparc64_syscall_exit }, 104#endif 105 { 0, 0, 0 }, 106}; 107 108/* 109 * Set the execution type. This is called after every exec, and when 110 * a process is first monitored. 111 */ 112 113static struct ex_types * 114set_etype(struct trussinfo *trussinfo) 115{ 116 struct ex_types *funcs; 117 char progt[32]; 118 119 size_t len = sizeof(progt); 120 int mib[4]; 121 int error; 122 123 mib[0] = CTL_KERN; 124 mib[1] = KERN_PROC; 125 mib[2] = KERN_PROC_SV_NAME; 126 mib[3] = trussinfo->pid; 127 error = sysctl(mib, 4, progt, &len, NULL, 0); 128 if (error != 0) 129 err(2, "can not get etype"); 130 131 for (funcs = ex_types; funcs->type; funcs++) 132 if (!strcmp(funcs->type, progt)) 133 break; 134 135 if (funcs->type == NULL) { 136 funcs = &ex_types[0]; 137 warn("execution type %s is not supported -- using %s", 138 progt, funcs->type); 139 } 140 return (funcs); 141} 142 143char * 144strsig(int sig) 145{ 146 char *ret; 147 148 ret = NULL; 149 if (sig > 0 && sig < NSIG) { 150 int i; 151 asprintf(&ret, "sig%s", sys_signame[sig]); 152 if (ret == NULL) 153 return (NULL); 154 for (i = 0; ret[i] != '\0'; ++i) 155 ret[i] = toupper(ret[i]); 156 } 157 return (ret); 158} 159 160int 161main(int ac, char **av) 162{ 163 int c; 164 int i; 165 char **command; 166 struct ex_types *funcs; 167 int initial_open; 168 char *fname; 169 struct trussinfo *trussinfo; 170 char *signame; 171 172 fname = NULL; 173 initial_open = 1; 174 175 /* Initialize the trussinfo struct */ 176 trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo)); 177 if (trussinfo == NULL) 178 errx(1, "malloc() failed"); 179 bzero(trussinfo, sizeof(struct trussinfo)); 180 181 trussinfo->outfile = stderr; 182 trussinfo->strsize = 32; 183 trussinfo->pr_why = S_NONE; 184 trussinfo->curthread = NULL; 185 SLIST_INIT(&trussinfo->threadlist); 186 while ((c = getopt(ac, av, "p:o:faedDs:S")) != -1) { 187 switch (c) { 188 case 'p': /* specified pid */ 189 trussinfo->pid = atoi(optarg); 190 /* make sure i don't trace me */ 191 if(trussinfo->pid == getpid()) { 192 fprintf(stderr, "attempt to grab self.\n"); 193 exit(2); 194 } 195 break; 196 case 'f': /* Follow fork()'s */ 197 trussinfo->flags |= FOLLOWFORKS; 198 break; 199 case 'a': /* Print execve() argument strings. */ 200 trussinfo->flags |= EXECVEARGS; 201 break; 202 case 'e': /* Print execve() environment strings. */ 203 trussinfo->flags |= EXECVEENVS; 204 break; 205 case 'd': /* Absolute timestamps */ 206 trussinfo->flags |= ABSOLUTETIMESTAMPS; 207 break; 208 case 'D': /* Relative timestamps */ 209 trussinfo->flags |= RELATIVETIMESTAMPS; 210 break; 211 case 'o': /* Specified output file */ 212 fname = optarg; 213 break; 214 case 's': /* Specified string size */ 215 trussinfo->strsize = atoi(optarg); 216 break; 217 case 'S': /* Don't trace signals */ 218 trussinfo->flags |= NOSIGS; 219 break; 220 default: 221 usage(); 222 } 223 } 224 225 ac -= optind; av += optind; 226 if ((trussinfo->pid == 0 && ac == 0) || 227 (trussinfo->pid != 0 && ac != 0)) 228 usage(); 229 230 if (fname != NULL) { /* Use output file */ 231 if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 232 errx(1, "cannot open %s", fname); 233 } 234 235 /* 236 * If truss starts the process itself, it will ignore some signals -- 237 * they should be passed off to the process, which may or may not 238 * exit. If, however, we are examining an already-running process, 239 * then we restore the event mask on these same signals. 240 */ 241 242 if (trussinfo->pid == 0) { /* Start a command ourselves */ 243 command = av; 244 trussinfo->pid = setup_and_wait(command); 245 signal(SIGINT, SIG_IGN); 246 signal(SIGTERM, SIG_IGN); 247 signal(SIGQUIT, SIG_IGN); 248 } else { 249 start_tracing(trussinfo->pid); 250 signal(SIGINT, restore_proc); 251 signal(SIGTERM, restore_proc); 252 signal(SIGQUIT, restore_proc); 253 } 254 255 256 /* 257 * At this point, if we started the process, it is stopped waiting to 258 * be woken up, either in exit() or in execve(). 259 */ 260 261START_TRACE: 262 funcs = set_etype(trussinfo); 263 264 initial_open = 0; 265 /* 266 * At this point, it's a simple loop, waiting for the process to 267 * stop, finding out why, printing out why, and then continuing it. 268 * All of the grunt work is done in the support routines. 269 */ 270 271 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 272 273 do { 274 struct timespec timediff; 275 waitevent(trussinfo); 276 277 switch(i = trussinfo->pr_why) { 278 case S_SCE: 279 funcs->enter_syscall(trussinfo, MAXARGS); 280 clock_gettime(CLOCK_REALTIME, 281 &trussinfo->before); 282 break; 283 case S_SCX: 284 clock_gettime(CLOCK_REALTIME, 285 &trussinfo->after); 286 287 if (trussinfo->curthread->in_fork && 288 (trussinfo->flags & FOLLOWFORKS)) { 289 int childpid; 290 291 trussinfo->curthread->in_fork = 0; 292 childpid = 293 funcs->exit_syscall(trussinfo, 294 trussinfo->pr_data); 295 296 /* 297 * Fork a new copy of ourself to trace 298 * the child of the original traced 299 * process. 300 */ 301 if (fork() == 0) { 302 trussinfo->pid = childpid; 303 start_tracing(trussinfo->pid); 304 goto START_TRACE; 305 } 306 break; 307 } 308 funcs->exit_syscall(trussinfo, MAXARGS); 309 break; 310 case S_SIG: 311 if (trussinfo->flags & NOSIGS) 312 break; 313 if (trussinfo->flags & FOLLOWFORKS) 314 fprintf(trussinfo->outfile, "%5d: ", 315 trussinfo->pid); 316 if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 317 timespecsubt(&trussinfo->after, 318 &trussinfo->start_time, &timediff); 319 fprintf(trussinfo->outfile, "%ld.%09ld ", 320 (long)timediff.tv_sec, 321 timediff.tv_nsec); 322 } 323 if (trussinfo->flags & RELATIVETIMESTAMPS) { 324 timespecsubt(&trussinfo->after, 325 &trussinfo->before, &timediff); 326 fprintf(trussinfo->outfile, "%ld.%09ld ", 327 (long)timediff.tv_sec, 328 timediff.tv_nsec); 329 } 330 signame = strsig(trussinfo->pr_data); 331 fprintf(trussinfo->outfile, 332 "SIGNAL %u (%s)\n", trussinfo->pr_data, 333 signame == NULL ? "?" : signame); 334 free(signame); 335 break; 336 case S_EXIT: 337 if (trussinfo->flags & FOLLOWFORKS) 338 fprintf(trussinfo->outfile, "%5d: ", 339 trussinfo->pid); 340 if (trussinfo->flags & ABSOLUTETIMESTAMPS) { 341 timespecsubt(&trussinfo->after, 342 &trussinfo->start_time, &timediff); 343 fprintf(trussinfo->outfile, "%ld.%09ld ", 344 (long)timediff.tv_sec, 345 timediff.tv_nsec); 346 } 347 if (trussinfo->flags & RELATIVETIMESTAMPS) { 348 timespecsubt(&trussinfo->after, 349 &trussinfo->before, &timediff); 350 fprintf(trussinfo->outfile, "%ld.%09ld ", 351 (long)timediff.tv_sec, timediff.tv_nsec); 352 } 353 fprintf(trussinfo->outfile, 354 "process exit, rval = %u\n", trussinfo->pr_data); 355 break; 356 default: 357 break; 358 } 359 } while (trussinfo->pr_why != S_EXIT); 360 fflush(trussinfo->outfile); 361 362 return (0); 363} 364