Home | History | Annotate | Line # | Download | only in ktrace
ktrace.c revision 1.3
      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.3 1995/01/03 07:04:18 glass 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 
     53 #include <err.h>
     54 #include <stdio.h>
     55 #include <unistd.h>
     56 
     57 #include "ktrace.h"
     58 
     59 void no_ktrace __P((int));
     60 void usage __P((void));
     61 
     62 main(argc, argv)
     63 	int argc;
     64 	char **argv;
     65 {
     66 	enum { NOTSET, CLEAR, CLEARALL } clear;
     67 	int append, ch, fd, inherit, ops, pid, pidset, trpoints;
     68 	char *tracefile;
     69 
     70 	clear = NOTSET;
     71 	append = ops = pidset = inherit = 0;
     72 	trpoints = DEF_POINTS;
     73 	tracefile = DEF_TRACEFILE;
     74 	while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF)
     75 		switch((char)ch) {
     76 		case 'a':
     77 			append = 1;
     78 			break;
     79 		case 'C':
     80 			clear = CLEARALL;
     81 			pidset = 1;
     82 			break;
     83 		case 'c':
     84 			clear = CLEAR;
     85 			break;
     86 		case 'd':
     87 			ops |= KTRFLAG_DESCEND;
     88 			break;
     89 		case 'f':
     90 			tracefile = optarg;
     91 			break;
     92 		case 'g':
     93 			pid = -rpid(optarg);
     94 			pidset = 1;
     95 			break;
     96 		case 'i':
     97 			inherit = 1;
     98 			break;
     99 		case 'p':
    100 			pid = rpid(optarg);
    101 			pidset = 1;
    102 			break;
    103 		case 't':
    104 			trpoints = getpoints(optarg);
    105 			if (trpoints < 0) {
    106 				warnx("unknown facility in %s", optarg);
    107 				usage();
    108 			}
    109 			break;
    110 		default:
    111 			usage();
    112 		}
    113 	argv += optind;
    114 	argc -= optind;
    115 
    116 	if (pidset && *argv || !pidset && !*argv)
    117 		usage();
    118 
    119 	if (inherit)
    120 		trpoints |= KTRFAC_INHERIT;
    121 
    122 	(void)signal(SIGSYS, no_ktrace);
    123 	if (clear != NOTSET) {
    124 		if (clear == CLEARALL) {
    125 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
    126 			trpoints = ALL_POINTS;
    127 			pid = 1;
    128 		} else
    129 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
    130 
    131 		if (ktrace(tracefile, ops, trpoints, pid) < 0)
    132 			err(1, tracefile);
    133 		exit(0);
    134 	}
    135 
    136 	if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC),
    137 	    DEFFILEMODE)) < 0)
    138 		err(1, tracefile);
    139 	(void)close(fd);
    140 
    141 	if (*argv) {
    142 		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
    143 			err(1, tracefile);
    144 		execvp(argv[0], &argv[0]);
    145 		err(1, "exec of '%s' failed", argv[0]);
    146 	}
    147 	else if (ktrace(tracefile, ops, trpoints, pid) < 0)
    148 		err(1, tracefile);
    149 	exit(0);
    150 }
    151 
    152 rpid(p)
    153 	char *p;
    154 {
    155 	static int first;
    156 
    157 	if (first++) {
    158 		warnx("only one -g or -p flag is permitted.");
    159 		usage();
    160 	}
    161 	if (!*p) {
    162 		warnx("illegal process id.");
    163 		usage();
    164 	}
    165 	return(atoi(p));
    166 }
    167 
    168 void
    169 usage()
    170 {
    171 	(void)fprintf(stderr,
    172 "usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n");
    173 	exit(1);
    174 }
    175 
    176 void
    177 no_ktrace(sig)
    178         int sig;
    179 {
    180         (void)fprintf(stderr,
    181 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
    182         exit(1);
    183 }
    184