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/ioctl.h> 43 #include <sys/pioctl.h> 44 #include <sys/types.h> 45 #include <sys/time.h> 46 #include <sys/resource.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 62 /* 63 * It's difficult to parameterize this because it must be 64 * accessible in a signal handler. 65 */ 66 67 int Procfd; 68 69 static __inline void 70 usage(void) 71 { 72 fprintf(stderr, "%s\n%s\n", 73 "usage: truss [-faedDS] [-o file] -p pid", 74 " truss [-faedDS] [-o file] command [args]"); 75 exit(1); 76 } 77 78 /* 79 * WARNING! "FreeBSD a.out" must be first, or set_etype will not 80 * work correctly. 81 */ 82 struct ex_types { 83 const char *type; 84 void (*enter_syscall)(struct trussinfo *, int); 85 long (*exit_syscall)(struct trussinfo *, int); 86 } ex_types[] = { 87 #ifdef __alpha__ 88 { "FreeBSD ELF", alpha_syscall_entry, alpha_syscall_exit }, 89 #endif 90 #ifdef __amd64__ 91 { "FreeBSD ELF64", amd64_syscall_entry, amd64_syscall_exit }, 92 #endif 93 #ifdef __i386__ 94 { "FreeBSD a.out", i386_syscall_entry, i386_syscall_exit }, 95 { "FreeBSD ELF", i386_syscall_entry, i386_syscall_exit }, 96 { "FreeBSD ELF32", i386_syscall_entry, i386_syscall_exit }, 97 { "Linux ELF", i386_linux_syscall_entry, i386_linux_syscall_exit }, 98 #endif 99 #ifdef __ia64__ 100 { "FreeBSD ELF64", ia64_syscall_entry, ia64_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. The procfs pseudo-file "etype" has 111 * the execution module type -- see /proc/curproc/etype for an example. 112 */ 113 114 static struct ex_types * 115 set_etype(struct trussinfo *trussinfo) { 116 struct ex_types *funcs; 117 char etype[24]; 118 char progt[32]; 119 int fd; 120 121 sprintf(etype, "/proc/%d/etype", trussinfo->pid); 122 if ((fd = open(etype, O_RDONLY)) == -1) { 123 strcpy(progt, "FreeBSD a.out"); 124 } else { 125 int len = read(fd, progt, sizeof(progt)); 126 progt[len-1] = '\0'; 127 close(fd); 128 } 129 130 for (funcs = ex_types; funcs->type; funcs++) 131 if (!strcmp(funcs->type, progt)) 132 break; 133 134 if (funcs->type == NULL) { 135 funcs = &ex_types[0]; 136 warn("execution type %s is not supported -- using %s", 137 progt, funcs->type); 138 } 139 return funcs; 140 } 141 142 char * 143 strsig(int sig) 144 { 145 char *ret; 146 147 ret = NULL; 148 if (sig > 0 && sig < NSIG) { 149 int i; 150 asprintf(&ret, "sig%s", sys_signame[sig]); 151 if (ret == NULL) 152 return (NULL); 153 for (i = 0; ret[i] != '\0'; ++i) 154 ret[i] = toupper(ret[i]); 155 } 156 return (ret); 157 } 158 159 int 160 main(int ac, char **av) { 161 int c; 162 int i; 163 char **command; 164 struct procfs_status pfs; 165 struct ex_types *funcs; 166 int in_exec = 0; 167 char *fname = NULL; 168 int sigexit = 0; 169 struct trussinfo *trussinfo; 170 char *signame; 171 172 /* Initialize the trussinfo struct */ 173 trussinfo = (struct trussinfo *)malloc(sizeof(struct trussinfo)); 174 if (trussinfo == NULL) 175 errx(1, "malloc() failed"); 176 bzero(trussinfo, sizeof(struct trussinfo)); 177 trussinfo->outfile = stderr; 178 179 while ((c = getopt(ac, av, "p:o:faedDS")) != -1) { 180 switch (c) { 181 case 'p': /* specified pid */ 182 trussinfo->pid = atoi(optarg); 183 break; 184 case 'f': /* Follow fork()'s */ 185 trussinfo->flags |= FOLLOWFORKS; 186 break; 187 case 'a': /* Print execve() argument strings. */ 188 trussinfo->flags |= EXECVEARGS; 189 break; 190 case 'e': /* Print execve() environment strings. */ 191 trussinfo->flags |= EXECVEENVS; 192 break; 193 case 'd': /* Absolute timestamps */ 194 trussinfo->flags |= ABSOLUTETIMESTAMPS; 195 break; 196 case 'D': /* Relative timestamps */ 197 trussinfo->flags |= RELATIVETIMESTAMPS; 198 break; 199 case 'o': /* Specified output file */ 200 fname = optarg; 201 break; 202 case 'S': /* Don't trace signals */ 203 trussinfo->flags |= NOSIGS; 204 break; 205 default: 206 usage(); 207 } 208 } 209 210 ac -= optind; av += optind; 211 if ((trussinfo->pid == 0 && ac == 0) || (trussinfo->pid != 0 && ac != 0)) 212 usage(); 213 214 if (fname != NULL) { /* Use output file */ 215 if ((trussinfo->outfile = fopen(fname, "w")) == NULL) 216 errx(1, "cannot open %s", fname); 217 } 218 219 /* 220 * If truss starts the process itself, it will ignore some signals -- 221 * they should be passed off to the process, which may or may not 222 * exit. If, however, we are examining an already-running process, 223 * then we restore the event mask on these same signals. 224 */ 225 226 if (trussinfo->pid == 0) { /* Start a command ourselves */ 227 command = av; 228 trussinfo->pid = setup_and_wait(command); 229 signal(SIGINT, SIG_IGN); 230 signal(SIGTERM, SIG_IGN); 231 signal(SIGQUIT, SIG_IGN); 232 } else { 233 signal(SIGINT, restore_proc); 234 signal(SIGTERM, restore_proc); 235 signal(SIGQUIT, restore_proc); 236 } 237 238 239 /* 240 * At this point, if we started the process, it is stopped waiting to 241 * be woken up, either in exit() or in execve(). 242 */ 243 244 START_TRACE: 245 Procfd = start_tracing( 246 trussinfo->pid, S_EXEC | S_SCE | S_SCX | S_CORE | S_EXIT | 247 ((trussinfo->flags & NOSIGS) ? 0 : S_SIG), 248 ((trussinfo->flags & FOLLOWFORKS) ? PF_FORK : 0)); 249 if (Procfd == -1) 250 return 0; 251 252 pfs.why = 0; 253 254 funcs = set_etype(trussinfo); 255 /* 256 * At this point, it's a simple loop, waiting for the process to 257 * stop, finding out why, printing out why, and then continuing it. 258 * All of the grunt work is done in the support routines. 259 */ 260 261 clock_gettime(CLOCK_REALTIME, &trussinfo->start_time); 262 263 do { 264 int val = 0; 265 266 if (ioctl(Procfd, PIOCWAIT, &pfs) == -1) 267 warn("PIOCWAIT top of loop"); 268 else { 269 switch(i = pfs.why) { 270 case S_SCE: 271 funcs->enter_syscall(trussinfo, pfs.val); 272 clock_gettime(CLOCK_REALTIME, &trussinfo->before); 273 break; 274 case S_SCX: 275 clock_gettime(CLOCK_REALTIME, &trussinfo->after); 276 /* 277 * This is so we don't get two messages for an exec -- one 278 * for the S_EXEC, and one for the syscall exit. It also, 279 * conveniently, ensures that the first message printed out 280 * isn't the return-from-syscall used to create the process. 281 */ 282 283 if (in_exec) { 284 in_exec = 0; 285 break; 286 } 287 288 if (trussinfo->in_fork && (trussinfo->flags & FOLLOWFORKS)) { 289 int childpid; 290 291 trussinfo->in_fork = 0; 292 childpid = funcs->exit_syscall(trussinfo, pfs.val); 293 294 /* 295 * Fork a new copy of ourself to trace the child of the 296 * original traced process. 297 */ 298 if (fork() == 0) { 299 trussinfo->pid = childpid; 300 goto START_TRACE; 301 } 302 break; 303 } 304 funcs->exit_syscall(trussinfo, pfs.val); 305 break; 306 case S_SIG: 307 signame = strsig(pfs.val); 308 fprintf(trussinfo->outfile, "SIGNAL %lu (%s)\n", pfs.val, 309 signame == NULL ? "?" : signame); 310 free(signame); 311 sigexit = pfs.val; 312 break; 313 case S_EXIT: 314 fprintf (trussinfo->outfile, "process exit, rval = %lu\n", pfs.val); 315 break; 316 case S_EXEC: 317 funcs = set_etype(trussinfo); 318 in_exec = 1; 319 break; 320 default: 321 fprintf (trussinfo->outfile, "Process stopped because of: %d\n", i); 322 break; 323 } 324 } 325 if (ioctl(Procfd, PIOCCONT, val) == -1) { 326 if (kill(trussinfo->pid, 0) == -1 && errno == ESRCH) 327 break; 328 else 329 warn("PIOCCONT"); 330 } 331 } while (pfs.why != S_EXIT); 332 fflush(trussinfo->outfile); 333 if (sigexit) { 334 struct rlimit rlp; 335 336 rlp.rlim_cur = 0; 337 rlp.rlim_max = 0; 338 setrlimit(RLIMIT_CORE, &rlp); 339 (void) signal(sigexit, SIG_DFL); 340 (void) kill(getpid(), sigexit); 341 } 342 return 0; 343 } 344