ktrace.c revision 1.1
1/* $NetBSD: ktrace.c,v 1.4 1995/08/31 23:01:44 jtc Exp $ */ 2 3/*- 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#ifndef lint 37static char copyright[] = 38"@(#) Copyright (c) 1988, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40#endif /* not lint */ 41 42#ifndef lint 43#if 0 44static char sccsid[] = "@(#)ktrace.c 8.2 (Berkeley) 4/28/95"; 45#endif 46static char *rcsid = "$NetBSD: ktrace.c,v 1.4 1995/08/31 23:01:44 jtc Exp $"; 47#endif /* not lint */ 48 49#include <sys/param.h> 50#include <sys/stat.h> 51#include <sys/file.h> 52#include <sys/time.h> 53#include <sys/errno.h> 54#include <sys/uio.h> 55#include <sys/ktrace.h> 56 57#include <err.h> 58#include <stdio.h> 59#include <unistd.h> 60 61#include "ktrace.h" 62 63void no_ktrace __P((int)); 64void usage __P((void)); 65 66main(argc, argv) 67 int argc; 68 char **argv; 69{ 70 enum { NOTSET, CLEAR, CLEARALL } clear; 71 int append, ch, fd, inherit, ops, pid, pidset, trpoints; 72 char *tracefile; 73 74 clear = NOTSET; 75 append = ops = pidset = inherit = 0; 76 trpoints = DEF_POINTS; 77 tracefile = DEF_TRACEFILE; 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 warnx("unknown facility in %s", optarg); 111 usage(); 112 } 113 break; 114 default: 115 usage(); 116 } 117 argv += optind; 118 argc -= optind; 119 120 if (pidset && *argv || !pidset && !*argv) 121 usage(); 122 123 if (inherit) 124 trpoints |= KTRFAC_INHERIT; 125 126 (void)signal(SIGSYS, no_ktrace); 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 err(1, tracefile); 137 exit(0); 138 } 139 140 if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC), 141 DEFFILEMODE)) < 0) 142 err(1, tracefile); 143 (void)close(fd); 144 145 if (*argv) { 146 if (ktrace(tracefile, ops, trpoints, getpid()) < 0) 147 err(1, tracefile); 148 execvp(argv[0], &argv[0]); 149 err(1, "exec of '%s' failed", argv[0]); 150 } 151 else if (ktrace(tracefile, ops, trpoints, pid) < 0) 152 err(1, tracefile); 153 exit(0); 154} 155 156rpid(p) 157 char *p; 158{ 159 static int first; 160 161 if (first++) { 162 warnx("only one -g or -p flag is permitted."); 163 usage(); 164 } 165 if (!*p) { 166 warnx("illegal process id."); 167 usage(); 168 } 169 return(atoi(p)); 170} 171 172void 173usage() 174{ 175 (void)fprintf(stderr, 176"usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n"); 177 exit(1); 178} 179 180void 181no_ktrace(sig) 182 int sig; 183{ 184 (void)fprintf(stderr, 185"error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n"); 186 exit(1); 187} 188