Home | History | Annotate | Line # | Download | only in ktrace
ktrace.c revision 1.19
      1 /*	$NetBSD: ktrace.c,v 1.19 2000/07/07 15:13:24 itojun 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.19 2000/07/07 15:13:24 itojun 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((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 	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 	extern char *__progname;
    256 #ifdef KTRUSS
    257 # define LONG_OPTION "[-e emulation] [-m maxdata] [-o outfile] "
    258 # define SHRT_OPTION "RT"
    259 #else
    260 # define LONG_OPTION ""
    261 # define SHRT_OPTION ""
    262 #endif
    263 	(void)fprintf(stderr,
    264 "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",
    265 	__progname, SHRT_OPTION, LONG_OPTION,
    266 	__progname, SHRT_OPTION, LONG_OPTION);
    267 	exit(1);
    268 }
    269 
    270 void
    271 no_ktrace(sig)
    272         int sig;
    273 {
    274         (void)fprintf(stderr,
    275 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
    276         exit(1);
    277 }
    278 
    279 int
    280 do_ktrace(tracefile, ops, trpoints, pid)
    281 	char *tracefile;
    282 	int ops;
    283 	int trpoints;
    284 	int pid;
    285 {
    286 	int ret;
    287 
    288 	if (!tracefile || strcmp(tracefile, "-") == 0) {
    289 		int pi[2], dofork, fpid;
    290 		int large = 32768;
    291 
    292 		if (pipe(pi) < 0)
    293 			err(1, "pipe(2)");
    294 		fcntl(pi[0], F_SETFD, FD_CLOEXEC|fcntl(pi[0], F_GETFD, 0));
    295 		fcntl(pi[1], F_SETFD, FD_CLOEXEC|fcntl(pi[1], F_GETFD, 0));
    296 
    297 		if (setsockopt(pi[1], SOL_SOCKET, SO_SNDBUF, &large, sizeof(large)) < 0)
    298 			err(1, "setsockopt(SO_SNDBUF)");
    299 		if (setsockopt(pi[0], SOL_SOCKET, SO_RCVBUF, &large, sizeof(large)) < 0)
    300 			err(1, "setsockopt(SO_RCVBUF)");
    301 
    302 		dofork = (pid == getpid());
    303 
    304 #ifdef KTRUSS
    305 		if (dofork)
    306 			fpid = fork();
    307 		else
    308 			fpid = pid;	/* XXX: Gcc */
    309 #else
    310 		if (dofork)
    311 			fpid = fork();
    312 		else
    313 			fpid = 0;	/* XXX: Gcc */
    314 #endif
    315 #ifdef KTRUSS
    316 		if (fpid)
    317 #else
    318 		if (!dofork || !fpid)
    319 #endif
    320 		{
    321 			if (!dofork)
    322 #ifdef KTRUSS
    323 				ret = fktrace(pi[1], ops, trpoints, fpid);
    324 #else
    325 				ret = fktrace(pi[1], ops, trpoints, pid);
    326 #endif
    327 			else
    328 				close(pi[1]);
    329 #ifdef KTRUSS
    330 			dumpfile(NULL, pi[0], trpoints);
    331 			waitpid(fpid, NULL, 0);
    332 #else
    333 			{
    334 				char	buf[512];
    335 				int	n, cnt = 0;
    336 
    337 				while ((n = read(pi[0], buf, sizeof(buf)))>0) {
    338 					write(1, buf, n);
    339 					cnt += n;
    340 				}
    341 			}
    342 			if (dofork)
    343 				_exit(0);
    344 #endif
    345 			return 0;
    346 		}
    347 		close(pi[0]);
    348 #ifdef KTRUSS
    349 		if (dofork && !fpid) {
    350 			ret = fktrace(pi[1], ops, trpoints, getpid());
    351 			return 1;
    352 		}
    353 #else
    354 		ret = fktrace(pi[1], ops, trpoints, pid);
    355 #endif
    356 	} else
    357 		ret = ktrace(tracefile, ops, trpoints, pid);
    358 	if (ret < 0)
    359 		err(1, "%s", tracefile);
    360 	return 1;
    361 }
    362