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