1 /*- 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1988, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 #include <sys/file.h> 47 #include <sys/time.h> 48 #include <sys/errno.h> 49 #include <sys/uio.h> 50 #include <sys/ktrace.h> 51 #include <stdio.h> 52 #include <signal.h> 53 #include "ktrace.h" 54 55 void noktrace() { 56 (void)fprintf(stderr, "ktrace: ktrace not enabled in kernel,to use ktrace\n"); 57 (void)fprintf(stderr, "you need to add a line \"options KTRACE\" to your kernel\n"); 58 exit(1); 59 } 60 61 main(argc, argv) 62 int argc; 63 char **argv; 64 { 65 extern int optind; 66 extern char *optarg; 67 enum { NOTSET, CLEAR, CLEARALL } clear; 68 int append, ch, fd, inherit, ops, pid, pidset, trpoints; 69 char *tracefile; 70 71 clear = NOTSET; 72 append = ops = pidset = inherit = 0; 73 trpoints = DEF_POINTS; 74 tracefile = DEF_TRACEFILE; 75 /* set up a signal handler for SIGSYS, this indicates that ktrace 76 is not enabled in the kernel */ 77 signal(SIGSYS, noktrace); 78 while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF) 79 switch((char)ch) { 80 case 'a': 81 append = 1; 82 break; 83 case 'C': 84 clear = CLEARALL; 85 pidset = 1; 86 break; 87 case 'c': 88 clear = CLEAR; 89 break; 90 case 'd': 91 ops |= KTRFLAG_DESCEND; 92 break; 93 case 'f': 94 tracefile = optarg; 95 break; 96 case 'g': 97 pid = -rpid(optarg); 98 pidset = 1; 99 break; 100 case 'i': 101 inherit = 1; 102 break; 103 case 'p': 104 pid = rpid(optarg); 105 pidset = 1; 106 break; 107 case 't': 108 trpoints = getpoints(optarg); 109 if (trpoints < 0) { 110 (void)fprintf(stderr, 111 "ktrace: unknown facility in %s\n", optarg); 112 usage(); 113 } 114 break; 115 default: 116 usage(); 117 } 118 argv += optind; 119 argc -= optind; 120 121 if (pidset && *argv || !pidset && !*argv) 122 usage(); 123 124 if (inherit) 125 trpoints |= KTRFAC_INHERIT; 126 127 if (clear != NOTSET) { 128 if (clear == CLEARALL) { 129 ops = KTROP_CLEAR | KTRFLAG_DESCEND; 130 trpoints = ALL_POINTS; 131 pid = 1; 132 } else 133 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; 134 135 if (ktrace(tracefile, ops, trpoints, pid) < 0) 136 error(tracefile); 137 exit(0); 138 } 139 140 if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC), 141 DEFFILEMODE)) < 0) 142 error(tracefile); 143 (void)close(fd); 144 145 if (*argv) { 146 if (ktrace(tracefile, ops, trpoints, getpid()) < 0) 147 error(); 148 execvp(argv[0], &argv[0]); 149 error(argv[0]); 150 exit(1); 151 } 152 else if (ktrace(tracefile, ops, trpoints, pid) < 0) 153 error(tracefile); 154 exit(0); 155 } 156 157 rpid(p) 158 char *p; 159 { 160 static int first; 161 162 if (first++) { 163 (void)fprintf(stderr, 164 "ktrace: only one -g or -p flag is permitted.\n"); 165 usage(); 166 } 167 if (!*p) { 168 (void)fprintf(stderr, "ktrace: illegal process id.\n"); 169 usage(); 170 } 171 return(atoi(p)); 172 } 173 174 error(name) 175 char *name; 176 { 177 (void)fprintf(stderr, "ktrace: %s: %s.\n", name, strerror(errno)); 178 exit(1); 179 } 180 181 usage() 182 { 183 (void)fprintf(stderr, 184 "usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n"); 185 exit(1); 186 } 187