Home | History | Annotate | Line # | Download | only in ktrace
ktrace.c revision 1.7
      1 /*	$NetBSD: ktrace.c,v 1.7 1998/06/27 21:24:22 christos 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) 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
     44 static char sccsid[] = "@(#)ktrace.c	8.2 (Berkeley) 4/28/95";
     45 #else
     46 __RCSID("$NetBSD: ktrace.c,v 1.7 1998/06/27 21:24:22 christos Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/stat.h>
     52 #include <sys/file.h>
     53 #include <sys/time.h>
     54 #include <sys/errno.h>
     55 #include <sys/uio.h>
     56 #include <sys/ktrace.h>
     57 
     58 #include <err.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <unistd.h>
     62 
     63 #include "ktrace.h"
     64 
     65 int	main __P((int, char **));
     66 int	rpid __P((char *));
     67 void	usage __P((void));
     68 int	do_ktrace __P((char *, int, int,int));
     69 void	no_ktrace __P((int));
     70 
     71 #ifdef KTRUSS
     72 extern int timestamp, decimal, fancy, tail, maxdata;
     73 #endif
     74 
     75 int
     76 main(argc, argv)
     77 	int argc;
     78 	char **argv;
     79 {
     80 	enum { NOTSET, CLEAR, CLEARALL } clear;
     81 	int append, ch, fd, inherit, ops, pid, pidset, trpoints;
     82 	char *infile, *outfile;
     83 
     84 	clear = NOTSET;
     85 	append = ops = pidset = inherit = 0;
     86 	trpoints = DEF_POINTS;
     87 	pid = 0;	/* Appease GCC */
     88 
     89 #ifdef KTRUSS
     90 # define OPTIONS "aCce:df:g:ilm:o:p:RTt:"
     91 	outfile = infile = NULL;
     92 #else
     93 # define OPTIONS "aCcdf:g:ip:t:"
     94 	outfile = infile = DEF_TRACEFILE;
     95 #endif
     96 
     97 	while ((ch = getopt(argc,argv, OPTIONS)) != -1)
     98 		switch((char)ch) {
     99 		case 'a':
    100 			append = 1;
    101 			break;
    102 		case 'C':
    103 			clear = CLEARALL;
    104 			pidset = 1;
    105 			break;
    106 		case 'c':
    107 			clear = CLEAR;
    108 			pidset = 1;
    109 			break;
    110 		case 'd':
    111 			ops |= KTRFLAG_DESCEND;
    112 			break;
    113 #ifdef KTRUSS
    114 		case 'e':
    115 			setemul(optarg);
    116 			break;
    117 #endif
    118 		case 'f':
    119 			infile = optarg;
    120 			break;
    121 		case 'g':
    122 			pid = -rpid(optarg);
    123 			pidset = 1;
    124 			break;
    125 		case 'i':
    126 			inherit = 1;
    127 			break;
    128 #ifdef KTRUSS
    129 		case 'l':
    130 			tail = 1;
    131 			break;
    132 		case 'm':
    133 			maxdata = atoi(optarg);
    134 			break;
    135 		case 'o':
    136 			outfile = optarg;
    137 			break;
    138 #endif
    139 		case 'p':
    140 			pid = rpid(optarg);
    141 			pidset = 1;
    142 			break;
    143 #ifdef KTRUSS
    144 		case 'R':
    145 			timestamp = 2;	/* relative timestamp */
    146 			break;
    147 		case 'T':
    148 			timestamp = 1;
    149 			break;
    150 #endif
    151 		case 't':
    152 			trpoints = getpoints(optarg);
    153 			if (trpoints < 0) {
    154 				warnx("unknown facility in %s", optarg);
    155 				usage();
    156 			}
    157 			break;
    158 		default:
    159 			usage();
    160 		}
    161 	argv += optind;
    162 	argc -= optind;
    163 
    164 	if (!infile && ((pidset && *argv) || (!pidset && !*argv)))
    165 		usage();
    166 
    167 #ifdef KTRUSS
    168 	if (infile) {
    169 		dumpfile(infile, 0, trpoints);
    170 		exit(0);
    171 	}
    172 #endif
    173 	if (inherit)
    174 		trpoints |= KTRFAC_INHERIT;
    175 
    176 	(void)signal(SIGSYS, no_ktrace);
    177 	if (clear != NOTSET) {
    178 		if (clear == CLEARALL) {
    179 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
    180 			trpoints = ALL_POINTS;
    181 			pid = 1;
    182 		} else
    183 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
    184 
    185 		(void)do_ktrace(outfile, ops, trpoints, pid);
    186 		exit(0);
    187 	}
    188 
    189 	if (outfile && strcmp(outfile, "-")) {
    190 		if ((fd = open(outfile, O_CREAT | O_WRONLY |
    191 		    (append ? 0 : O_TRUNC), DEFFILEMODE)) < 0)
    192 			err(1, outfile);
    193 		(void)close(fd);
    194 	}
    195 
    196 	if (*argv) {
    197 		(void)do_ktrace(outfile, ops, trpoints, getpid());
    198 		execvp(argv[0], &argv[0]);
    199 		err(1, "exec of '%s' failed", argv[0]);
    200 	} else
    201 		(void)do_ktrace(outfile, ops, trpoints, pid);
    202 	exit(0);
    203 }
    204 
    205 int
    206 rpid(p)
    207 	char *p;
    208 {
    209 	static int first;
    210 
    211 	if (first++) {
    212 		warnx("only one -g or -p flag is permitted.");
    213 		usage();
    214 	}
    215 	if (!*p) {
    216 		warnx("illegal process id.");
    217 		usage();
    218 	}
    219 	return(atoi(p));
    220 }
    221 
    222 void
    223 usage()
    224 {
    225 	extern char *__progname;
    226 #ifdef KTRUSS
    227 # define LONG_OPTION "[-e emulation] [-m maxdata] [-o outfile] "
    228 # define SHRT_OPTION "RT"
    229 #else
    230 # define LONG_OPTION ""
    231 # define SHRT_OPTION ""
    232 #endif
    233 	(void)fprintf(stderr,
    234 "Usage:\t%s [-aCcid%s] %s[-f trfile] [-g pgid] [-p pid] [-t [cenisw+]]\n\t%s [-aCcid%s] %s[-f trfile] [-t [cenisw+]] command\n",
    235 	__progname, SHRT_OPTION, LONG_OPTION,
    236 	__progname, SHRT_OPTION, LONG_OPTION);
    237 	exit(1);
    238 }
    239 
    240 void
    241 no_ktrace(sig)
    242         int sig;
    243 {
    244         (void)fprintf(stderr,
    245 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
    246         exit(1);
    247 }
    248 
    249 int
    250 do_ktrace(tracefile, ops, trpoints, pid)
    251 	char *tracefile;
    252 	int ops;
    253 	int trpoints;
    254 	int pid;
    255 {
    256 	int ret;
    257 
    258 	if (!tracefile || strcmp(tracefile, "-") == 0) {
    259 		int pi[2], nofork, fpid;
    260 
    261 		if (pipe(pi) < 0)
    262 			err(1, "pipe(2)");
    263 		fcntl(pi[0], F_SETFD, FD_CLOEXEC|fcntl(pi[0], F_GETFD, 0));
    264 		fcntl(pi[1], F_SETFD, FD_CLOEXEC|fcntl(pi[1], F_GETFD, 0));
    265 
    266 		nofork = (pid != getpid());
    267 
    268 		if (!nofork)
    269 			fpid = fork();
    270 		if (nofork || !fpid) {
    271 			if (nofork)
    272 				ret = fktrace(pi[1], ops, trpoints, pid);
    273 			else
    274 				close(pi[1]);
    275 #ifdef KTRUSS
    276 			dumpfile(NULL, pi[0], trpoints);
    277 #else
    278 			{
    279 				char	buf[512];
    280 				int	n, cnt = 0;
    281 
    282 				while ((n = read(pi[0], buf, sizeof(buf)))>0) {
    283 					write(1, buf, n);
    284 					cnt += n;
    285 				}
    286 			}
    287 #endif
    288 			return 0;
    289 		}
    290 		close(pi[0]);
    291 		ret = fktrace(pi[1], ops, trpoints, pid);
    292 	} else
    293 		ret = ktrace(tracefile, ops, trpoints, pid);
    294 	if (ret < 0)
    295 		err(1, tracefile);
    296 	return ret;
    297 }
    298