Home | History | Annotate | Line # | Download | only in ktrace
ktrace.c revision 1.17
      1 /*	$NetBSD: ktrace.c,v 1.17 2000/05/27 00:45:37 sommerfeld 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.17 2000/05/27 00:45:37 sommerfeld Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/stat.h>
     52 #include <sys/wait.h>
     53 #include <sys/file.h>
     54 #include <sys/time.h>
     55 #include <sys/uio.h>
     56 #include <sys/ktrace.h>
     57 #include <sys/socket.h>
     58 
     59 #include <err.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include <unistd.h>
     63 
     64 #include "ktrace.h"
     65 
     66 #ifdef KTRUSS
     67 #include <string.h>
     68 #include "setemul.h"
     69 #endif
     70 
     71 int	main __P((int, char **));
     72 int	rpid __P((char *));
     73 void	usage __P((void));
     74 int	do_ktrace __P((char *, int, int,int));
     75 void	no_ktrace __P((int));
     76 
     77 #ifdef KTRUSS
     78 extern int timestamp, decimal, fancy, tail, maxdata;
     79 #endif
     80 
     81 int
     82 main(argc, argv)
     83 	int argc;
     84 	char **argv;
     85 {
     86 	enum { NOTSET, CLEAR, CLEARALL } clear;
     87 	int append, ch, fd, inherit, ops, pid, pidset, trpoints;
     88 	char *infile, *outfile;
     89 #ifdef KTRUSS
     90 	const char *emul_name = "netbsd";
     91 #endif
     92 
     93 	clear = NOTSET;
     94 	append = ops = pidset = inherit = 0;
     95 	trpoints = DEF_POINTS;
     96 	pid = 0;	/* Appease GCC */
     97 
     98 #ifdef KTRUSS
     99 # define OPTIONS "aCce:df:g:ilm:o:p:RTt:"
    100 	outfile = infile = NULL;
    101 #else
    102 # define OPTIONS "aCcdf:g:ip:t:"
    103 	outfile = infile = DEF_TRACEFILE;
    104 #endif
    105 
    106 	while ((ch = getopt(argc,argv, OPTIONS)) != -1)
    107 		switch((char)ch) {
    108 		case 'a':
    109 			append = 1;
    110 			break;
    111 		case 'C':
    112 			clear = CLEARALL;
    113 			pidset = 1;
    114 			break;
    115 		case 'c':
    116 			clear = CLEAR;
    117 			pidset = 1;
    118 			break;
    119 		case 'd':
    120 			ops |= KTRFLAG_DESCEND;
    121 			break;
    122 #ifdef KTRUSS
    123 		case 'e':
    124 			emul_name = strdup(optarg); /* it's safer to copy it */
    125 			break;
    126 		case 'f':
    127 			infile = optarg;
    128 			break;
    129 #else
    130 		case 'f':
    131 			infile = outfile = optarg;
    132 			break;
    133 #endif
    134 		case 'g':
    135 			pid = -rpid(optarg);
    136 			pidset = 1;
    137 			break;
    138 		case 'i':
    139 			inherit = 1;
    140 			break;
    141 #ifdef KTRUSS
    142 		case 'l':
    143 			tail = 1;
    144 			break;
    145 		case 'm':
    146 			maxdata = atoi(optarg);
    147 			break;
    148 		case 'o':
    149 			outfile = optarg;
    150 			break;
    151 #endif
    152 		case 'p':
    153 			pid = rpid(optarg);
    154 			pidset = 1;
    155 			break;
    156 #ifdef KTRUSS
    157 		case 'R':
    158 			timestamp = 2;	/* relative timestamp */
    159 			break;
    160 		case 'T':
    161 			timestamp = 1;
    162 			break;
    163 #endif
    164 		case 't':
    165 			trpoints = getpoints(optarg);
    166 			if (trpoints < 0) {
    167 				warnx("unknown facility in %s", optarg);
    168 				usage();
    169 			}
    170 			break;
    171 		default:
    172 			usage();
    173 		}
    174 	argv += optind;
    175 	argc -= optind;
    176 
    177 	if (!infile && ((pidset && *argv) || (!pidset && !*argv)))
    178 		usage();
    179 
    180 #ifdef KTRUSS
    181 	if (infile) {
    182 		dumpfile(infile, 0, trpoints);
    183 		exit(0);
    184 	}
    185 
    186 	setemul(emul_name, 0, 0);
    187 #endif
    188 
    189 	/*
    190 	 * For cleaner traces, initialize malloc now rather
    191 	 * than in a traced subprocess.
    192 	 */
    193 	free(malloc(1));
    194 
    195 	if (inherit)
    196 		trpoints |= KTRFAC_INHERIT;
    197 
    198 	(void)signal(SIGSYS, no_ktrace);
    199 	if (clear != NOTSET) {
    200 		if (clear == CLEARALL) {
    201 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
    202 			trpoints = ALL_POINTS;
    203 			pid = 1;
    204 		} else
    205 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
    206 
    207 		(void)do_ktrace(outfile, ops, trpoints, pid);
    208 		exit(0);
    209 	}
    210 
    211 	if (outfile && strcmp(outfile, "-")) {
    212 		if ((fd = open(outfile, O_CREAT | O_WRONLY |
    213 		    (append ? 0 : O_TRUNC), DEFFILEMODE)) < 0)
    214 			err(1, outfile);
    215 		(void)close(fd);
    216 	}
    217 
    218 	if (*argv) {
    219 #ifdef KTRUSS
    220 		if (do_ktrace(outfile, ops, trpoints, getpid()) == 1) {
    221 			execvp(argv[0], &argv[0]);
    222 			err(1, "exec of '%s' failed", argv[0]);
    223 		}
    224 #else
    225 		(void) do_ktrace(outfile, ops, trpoints, getpid());
    226 		execvp(argv[0], &argv[0]);
    227 		err(1, "exec of '%s' failed", argv[0]);
    228 #endif
    229 	} else
    230 		(void)do_ktrace(outfile, ops, trpoints, pid);
    231 	exit(0);
    232 }
    233 
    234 int
    235 rpid(p)
    236 	char *p;
    237 {
    238 	static int first;
    239 
    240 	if (first++) {
    241 		warnx("only one -g or -p flag is permitted.");
    242 		usage();
    243 	}
    244 	if (!*p) {
    245 		warnx("illegal process id.");
    246 		usage();
    247 	}
    248 	return(atoi(p));
    249 }
    250 
    251 void
    252 usage()
    253 {
    254 	extern char *__progname;
    255 #ifdef KTRUSS
    256 # define LONG_OPTION "[-e emulation] [-m maxdata] [-o outfile] "
    257 # define SHRT_OPTION "RT"
    258 #else
    259 # define LONG_OPTION ""
    260 # define SHRT_OPTION ""
    261 #endif
    262 	(void)fprintf(stderr,
    263 "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",
    264 	__progname, SHRT_OPTION, LONG_OPTION,
    265 	__progname, SHRT_OPTION, LONG_OPTION);
    266 	exit(1);
    267 }
    268 
    269 void
    270 no_ktrace(sig)
    271         int sig;
    272 {
    273         (void)fprintf(stderr,
    274 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
    275         exit(1);
    276 }
    277 
    278 int
    279 do_ktrace(tracefile, ops, trpoints, pid)
    280 	char *tracefile;
    281 	int ops;
    282 	int trpoints;
    283 	int pid;
    284 {
    285 	int ret;
    286 
    287 	if (!tracefile || strcmp(tracefile, "-") == 0) {
    288 		int pi[2], dofork, fpid;
    289 		int large = 32768;
    290 
    291 		if (pipe(pi) < 0)
    292 			err(1, "pipe(2)");
    293 		fcntl(pi[0], F_SETFD, FD_CLOEXEC|fcntl(pi[0], F_GETFD, 0));
    294 		fcntl(pi[1], F_SETFD, FD_CLOEXEC|fcntl(pi[1], F_GETFD, 0));
    295 
    296 		if (setsockopt(pi[1], SOL_SOCKET, SO_SNDBUF, &large, sizeof(large)) < 0)
    297 			err(1, "setsockopt(SO_SNDBUF)");
    298 		if (setsockopt(pi[0], SOL_SOCKET, SO_RCVBUF, &large, sizeof(large)) < 0)
    299 			err(1, "setsockopt(SO_RCVBUF)");
    300 
    301 		dofork = (pid == getpid());
    302 
    303 #ifdef KTRUSS
    304 		if (dofork)
    305 			fpid = fork();
    306 		else
    307 			fpid = pid;	/* XXX: Gcc */
    308 #else
    309 		if (dofork)
    310 			fpid = fork();
    311 		else
    312 			fpid = 0;	/* XXX: Gcc */
    313 #endif
    314 #ifdef KTRUSS
    315 		if (fpid)
    316 #else
    317 		if (!dofork || !fpid)
    318 #endif
    319 		{
    320 			if (!dofork)
    321 #ifdef KTRUSS
    322 				ret = fktrace(pi[1], ops, trpoints, fpid);
    323 #else
    324 				ret = fktrace(pi[1], ops, trpoints, pid);
    325 #endif
    326 			else
    327 				close(pi[1]);
    328 #ifdef KTRUSS
    329 			dumpfile(NULL, pi[0], trpoints);
    330 			waitpid(fpid, NULL, 0);
    331 #else
    332 			{
    333 				char	buf[512];
    334 				int	n, cnt = 0;
    335 
    336 				while ((n = read(pi[0], buf, sizeof(buf)))>0) {
    337 					write(1, buf, n);
    338 					cnt += n;
    339 				}
    340 			}
    341 			if (dofork)
    342 				_exit(0);
    343 #endif
    344 			return 0;
    345 		}
    346 		close(pi[0]);
    347 #ifdef KTRUSS
    348 		if (dofork && !fpid) {
    349 			ret = fktrace(pi[1], ops, trpoints, getpid());
    350 			return 1;
    351 		}
    352 #else
    353 		ret = fktrace(pi[1], ops, trpoints, pid);
    354 #endif
    355 	} else
    356 		ret = ktrace(tracefile, ops, trpoints, pid);
    357 	if (ret < 0)
    358 		err(1, tracefile);
    359 	return 1;
    360 }
    361