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[] = "from: @(#)ktrace.c 8.1 (Berkeley) 6/6/93";*/ 42 static char *rcsid = "$Id: ktrace.c,v 1.2 1994/10/06 15:45:41 mycroft Exp $"; 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 #include <sys/file.h> 48 #include <sys/time.h> 49 #include <sys/errno.h> 50 #include <sys/uio.h> 51 #include <sys/ktrace.h> 52 #include <stdio.h> 53 #include "ktrace.h" 54 55 main(argc, argv) 56 int argc; 57 char **argv; 58 { 59 extern int optind; 60 extern char *optarg; 61 enum { NOTSET, CLEAR, CLEARALL } clear; 62 int append, ch, fd, inherit, ops, pid, pidset, trpoints; 63 char *tracefile; 64 65 clear = NOTSET; 66 append = ops = pidset = inherit = 0; 67 trpoints = DEF_POINTS; 68 tracefile = DEF_TRACEFILE; 69 while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF) 70 switch((char)ch) { 71 case 'a': 72 append = 1; 73 break; 74 case 'C': 75 clear = CLEARALL; 76 pidset = 1; 77 break; 78 case 'c': 79 clear = CLEAR; 80 break; 81 case 'd': 82 ops |= KTRFLAG_DESCEND; 83 break; 84 case 'f': 85 tracefile = optarg; 86 break; 87 case 'g': 88 pid = -rpid(optarg); 89 pidset = 1; 90 break; 91 case 'i': 92 inherit = 1; 93 break; 94 case 'p': 95 pid = rpid(optarg); 96 pidset = 1; 97 break; 98 case 't': 99 trpoints = getpoints(optarg); 100 if (trpoints < 0) { 101 (void)fprintf(stderr, 102 "ktrace: unknown facility in %s\n", optarg); 103 usage(); 104 } 105 break; 106 default: 107 usage(); 108 } 109 argv += optind; 110 argc -= optind; 111 112 if (pidset && *argv || !pidset && !*argv) 113 usage(); 114 115 if (inherit) 116 trpoints |= KTRFAC_INHERIT; 117 118 if (clear != NOTSET) { 119 if (clear == CLEARALL) { 120 ops = KTROP_CLEAR | KTRFLAG_DESCEND; 121 trpoints = ALL_POINTS; 122 pid = 1; 123 } else 124 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; 125 126 if (ktrace(tracefile, ops, trpoints, pid) < 0) 127 error(tracefile); 128 exit(0); 129 } 130 131 if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC), 132 DEFFILEMODE)) < 0) 133 error(tracefile); 134 (void)close(fd); 135 136 if (*argv) { 137 if (ktrace(tracefile, ops, trpoints, getpid()) < 0) 138 error(); 139 execvp(argv[0], &argv[0]); 140 error(argv[0]); 141 exit(1); 142 } 143 else if (ktrace(tracefile, ops, trpoints, pid) < 0) 144 error(tracefile); 145 exit(0); 146 } 147 148 rpid(p) 149 char *p; 150 { 151 static int first; 152 153 if (first++) { 154 (void)fprintf(stderr, 155 "ktrace: only one -g or -p flag is permitted.\n"); 156 usage(); 157 } 158 if (!*p) { 159 (void)fprintf(stderr, "ktrace: illegal process id.\n"); 160 usage(); 161 } 162 return(atoi(p)); 163 } 164 165 error(name) 166 char *name; 167 { 168 (void)fprintf(stderr, "ktrace: %s: %s.\n", name, strerror(errno)); 169 exit(1); 170 } 171 172 usage() 173 { 174 (void)fprintf(stderr, 175 "usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n"); 176 exit(1); 177 } 178