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