Home | History | Annotate | Line # | Download | only in ktrace
ktrace.c revision 1.21
      1 /*	$NetBSD: ktrace.c,v 1.21 2001/02/19 23:03:48 cgd 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.21 2001/02/19 23:03:48 cgd 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 <string.h>
     63 #include <unistd.h>
     64 
     65 #include "ktrace.h"
     66 
     67 #ifdef KTRUSS
     68 #include <string.h>
     69 #include "setemul.h"
     70 #endif
     71 
     72 int	main __P((int, char **));
     73 int	rpid __P((char *));
     74 void	usage __P((void));
     75 int	do_ktrace __P((const char *, int, int,int));
     76 void	no_ktrace __P((int));
     77 
     78 #ifdef KTRUSS
     79 extern int timestamp, decimal, fancy, tail, maxdata;
     80 #endif
     81 
     82 int
     83 main(argc, argv)
     84 	int argc;
     85 	char **argv;
     86 {
     87 	enum { NOTSET, CLEAR, CLEARALL } clear;
     88 	int append, ch, fd, inherit, ops, pid, pidset, trpoints;
     89 	const char *infile, *outfile;
     90 #ifdef KTRUSS
     91 	const char *emul_name = "netbsd";
     92 #endif
     93 
     94 	clear = NOTSET;
     95 	append = ops = pidset = inherit = 0;
     96 	trpoints = DEF_POINTS;
     97 	pid = 0;	/* Appease GCC */
     98 
     99 #ifdef KTRUSS
    100 # define OPTIONS "aCce:df:g:ilm:o:p:RTt:"
    101 	outfile = infile = NULL;
    102 #else
    103 # define OPTIONS "aCcdf:g:ip:t:"
    104 	outfile = infile = DEF_TRACEFILE;
    105 #endif
    106 
    107 	while ((ch = getopt(argc,argv, OPTIONS)) != -1)
    108 		switch((char)ch) {
    109 		case 'a':
    110 			append = 1;
    111 			break;
    112 		case 'C':
    113 			clear = CLEARALL;
    114 			pidset = 1;
    115 			break;
    116 		case 'c':
    117 			clear = CLEAR;
    118 			pidset = 1;
    119 			break;
    120 		case 'd':
    121 			ops |= KTRFLAG_DESCEND;
    122 			break;
    123 #ifdef KTRUSS
    124 		case 'e':
    125 			emul_name = strdup(optarg); /* it's safer to copy it */
    126 			break;
    127 		case 'f':
    128 			infile = optarg;
    129 			break;
    130 #else
    131 		case 'f':
    132 			infile = outfile = optarg;
    133 			break;
    134 #endif
    135 		case 'g':
    136 			pid = -rpid(optarg);
    137 			pidset = 1;
    138 			break;
    139 		case 'i':
    140 			inherit = 1;
    141 			break;
    142 #ifdef KTRUSS
    143 		case 'l':
    144 			tail = 1;
    145 			break;
    146 		case 'm':
    147 			maxdata = atoi(optarg);
    148 			break;
    149 		case 'o':
    150 			outfile = optarg;
    151 			break;
    152 #endif
    153 		case 'p':
    154 			pid = rpid(optarg);
    155 			pidset = 1;
    156 			break;
    157 #ifdef KTRUSS
    158 		case 'R':
    159 			timestamp = 2;	/* relative timestamp */
    160 			break;
    161 		case 'T':
    162 			timestamp = 1;
    163 			break;
    164 #endif
    165 		case 't':
    166 			trpoints = getpoints(optarg);
    167 			if (trpoints < 0) {
    168 				warnx("unknown facility in %s", optarg);
    169 				usage();
    170 			}
    171 			break;
    172 		default:
    173 			usage();
    174 		}
    175 	argv += optind;
    176 	argc -= optind;
    177 
    178 	if (!infile && ((pidset && *argv) || (!pidset && !*argv)))
    179 		usage();
    180 
    181 #ifdef KTRUSS
    182 	if (infile) {
    183 		dumpfile(infile, 0, trpoints);
    184 		exit(0);
    185 	}
    186 
    187 	setemul(emul_name, 0, 0);
    188 #endif
    189 
    190 	/*
    191 	 * For cleaner traces, initialize malloc now rather
    192 	 * than in a traced subprocess.
    193 	 */
    194 	free(malloc(1));
    195 
    196 	if (inherit)
    197 		trpoints |= KTRFAC_INHERIT;
    198 
    199 	(void)signal(SIGSYS, no_ktrace);
    200 	if (clear != NOTSET) {
    201 		if (clear == CLEARALL) {
    202 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
    203 			trpoints = ALL_POINTS;
    204 			pid = 1;
    205 		} else
    206 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
    207 
    208 		(void)do_ktrace(outfile, ops, trpoints, pid);
    209 		exit(0);
    210 	}
    211 
    212 	if (outfile && strcmp(outfile, "-")) {
    213 		if ((fd = open(outfile, O_CREAT | O_WRONLY |
    214 		    (append ? 0 : O_TRUNC), DEFFILEMODE)) < 0)
    215 			err(1, "%s", outfile);
    216 		(void)close(fd);
    217 	}
    218 
    219 	if (*argv) {
    220 #ifdef KTRUSS
    221 		if (do_ktrace(outfile, ops, trpoints, getpid()) == 1) {
    222 			execvp(argv[0], &argv[0]);
    223 			err(1, "exec of '%s' failed", argv[0]);
    224 		}
    225 #else
    226 		(void) do_ktrace(outfile, ops, trpoints, getpid());
    227 		execvp(argv[0], &argv[0]);
    228 		err(1, "exec of '%s' failed", argv[0]);
    229 #endif
    230 	} else
    231 		(void)do_ktrace(outfile, ops, trpoints, pid);
    232 	exit(0);
    233 }
    234 
    235 int
    236 rpid(p)
    237 	char *p;
    238 {
    239 	static int first;
    240 
    241 	if (first++) {
    242 		warnx("only one -g or -p flag is permitted.");
    243 		usage();
    244 	}
    245 	if (!*p) {
    246 		warnx("illegal process id.");
    247 		usage();
    248 	}
    249 	return(atoi(p));
    250 }
    251 
    252 void
    253 usage()
    254 {
    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 	getprogname(), SHRT_OPTION, LONG_OPTION,
    265 	getprogname(), 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 	const 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, "%s", tracefile);
    359 	return 1;
    360 }
    361