Home | History | Annotate | Line # | Download | only in ktrace
ktrace.c revision 1.30
      1 /*	$NetBSD: ktrace.c,v 1.30 2004/01/05 23:23:34 jmmv 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)ktrace.c	8.2 (Berkeley) 4/28/95";
     41 #else
     42 __RCSID("$NetBSD: ktrace.c,v 1.30 2004/01/05 23:23:34 jmmv Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/stat.h>
     48 #include <sys/wait.h>
     49 #include <sys/file.h>
     50 #include <sys/time.h>
     51 #include <sys/uio.h>
     52 #include <sys/ktrace.h>
     53 #include <sys/socket.h>
     54 
     55 #include <err.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 #include "ktrace.h"
     62 
     63 #ifdef KTRUSS
     64 #include <string.h>
     65 #include "setemul.h"
     66 #endif
     67 
     68 int	main __P((int, char **));
     69 int	rpid __P((char *));
     70 void	usage __P((void));
     71 int	do_ktrace __P((const char *, int, int,int));
     72 void	no_ktrace __P((int));
     73 
     74 #ifdef KTRUSS
     75 extern int timestamp, decimal, fancy, tail, maxdata;
     76 #endif
     77 
     78 int
     79 main(argc, argv)
     80 	int argc;
     81 	char **argv;
     82 {
     83 	enum { NOTSET, CLEAR, CLEARALL } clear;
     84 	int append, ch, fd, trset, ops, pid, pidset, synclog, trpoints;
     85 	const char *outfile;
     86 #ifdef KTRUSS
     87 	const char *infile;
     88 	const char *emul_name = "netbsd";
     89 #endif
     90 
     91 	clear = NOTSET;
     92 	append = ops = pidset = trset = synclog = 0;
     93 	trpoints = 0;
     94 	pid = 0;	/* Appease GCC */
     95 
     96 #ifdef KTRUSS
     97 # define OPTIONS "aCce:df:g:ilm:o:p:RTt:"
     98 	outfile = infile = NULL;
     99 #else
    100 # define OPTIONS "aCcdf:g:ip:st:"
    101 	outfile = DEF_TRACEFILE;
    102 #endif
    103 
    104 	while ((ch = getopt(argc,argv, OPTIONS)) != -1)
    105 		switch((char)ch) {
    106 		case 'a':
    107 			append = 1;
    108 			break;
    109 		case 'C':
    110 			clear = CLEARALL;
    111 			pidset = 1;
    112 			break;
    113 		case 'c':
    114 			clear = CLEAR;
    115 			pidset = 1;
    116 			break;
    117 		case 'd':
    118 			ops |= KTRFLAG_DESCEND;
    119 			break;
    120 #ifdef KTRUSS
    121 		case 'e':
    122 			emul_name = strdup(optarg); /* it's safer to copy it */
    123 			break;
    124 		case 'f':
    125 			infile = optarg;
    126 			break;
    127 #else
    128 		case 'f':
    129 			outfile = optarg;
    130 			break;
    131 #endif
    132 		case 'g':
    133 			pid = -rpid(optarg);
    134 			pidset = 1;
    135 			break;
    136 		case 'i':
    137 			trpoints |= KTRFAC_INHERIT;
    138 			break;
    139 #ifdef KTRUSS
    140 		case 'l':
    141 			tail = 1;
    142 			break;
    143 		case 'm':
    144 			maxdata = atoi(optarg);
    145 			break;
    146 		case 'o':
    147 			outfile = optarg;
    148 			break;
    149 #endif
    150 		case 'p':
    151 			pid = rpid(optarg);
    152 			pidset = 1;
    153 			break;
    154 #ifdef KTRUSS
    155 		case 'R':
    156 			timestamp = 2;	/* relative timestamp */
    157 			break;
    158 #else
    159 		case 's':
    160 			synclog = 1;
    161 			break;
    162 #endif
    163 #ifdef KTRUSS
    164 		case 'T':
    165 			timestamp = 1;
    166 			break;
    167 #endif
    168 		case 't':
    169 			trset = 1;
    170 			trpoints = getpoints(trpoints, optarg);
    171 			if (trpoints < 0) {
    172 				warnx("unknown facility in %s", optarg);
    173 				usage();
    174 			}
    175 			break;
    176 		default:
    177 			usage();
    178 		}
    179 	argv += optind;
    180 	argc -= optind;
    181 
    182 	if (!trset)
    183 		trpoints |= clear == NOTSET ? DEF_POINTS : ALL_POINTS;
    184 
    185 	if ((pidset && *argv) || (!pidset && !*argv)) {
    186 #ifdef KTRUSS
    187 	    if (!infile)
    188 #endif
    189 		usage();
    190 	}
    191 
    192 #ifdef KTRUSS
    193 	if (infile) {
    194 		dumpfile(infile, 0, trpoints);
    195 		exit(0);
    196 	}
    197 
    198 	setemul(emul_name, 0, 0);
    199 #endif
    200 
    201 	/*
    202 	 * For cleaner traces, initialize malloc now rather
    203 	 * than in a traced subprocess.
    204 	 */
    205 	free(malloc(1));
    206 
    207 	(void)signal(SIGSYS, no_ktrace);
    208 	if (clear != NOTSET) {
    209 		if (clear == CLEARALL) {
    210 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
    211 			trpoints = ALL_POINTS;
    212 			pid = 1;
    213 		} else
    214 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
    215 
    216 		(void)do_ktrace(outfile, ops, trpoints, pid);
    217 		exit(0);
    218 	}
    219 
    220 	if (outfile && strcmp(outfile, "-")) {
    221 		if ((fd = open(outfile, O_CREAT | O_WRONLY |
    222 		    (append ? 0 : O_TRUNC) | (synclog ? 0 : O_SYNC),
    223 		    DEFFILEMODE)) < 0)
    224 			err(1, "%s", outfile);
    225 		(void)close(fd);
    226 	}
    227 
    228 	if (*argv) {
    229 #ifdef KTRUSS
    230 		if (do_ktrace(outfile, ops, trpoints, getpid()) == 1) {
    231 			execvp(argv[0], &argv[0]);
    232 			err(1, "exec of '%s' failed", argv[0]);
    233 		}
    234 #else
    235 		(void)do_ktrace(outfile, ops, trpoints, getpid());
    236 		execvp(argv[0], &argv[0]);
    237 		err(1, "exec of '%s' failed", argv[0]);
    238 #endif
    239 	} else
    240 		(void)do_ktrace(outfile, ops, trpoints, pid);
    241 	exit(0);
    242 }
    243 
    244 int
    245 rpid(p)
    246 	char *p;
    247 {
    248 	static int first;
    249 
    250 	if (first++) {
    251 		warnx("only one -g or -p flag is permitted.");
    252 		usage();
    253 	}
    254 	if (!*p) {
    255 		warnx("illegal process id.");
    256 		usage();
    257 	}
    258 	return(atoi(p));
    259 }
    260 
    261 void
    262 usage()
    263 {
    264 #ifdef KTRUSS
    265 # define LONG_OPTION "[-e emulation] [-m maxdata] [-o outfile] "
    266 # define SHRT_OPTION "RT"
    267 #else
    268 # define LONG_OPTION ""
    269 # define SHRT_OPTION ""
    270 #endif
    271 	(void)fprintf(stderr,
    272 	    "usage:\t%s [-aCcid%s] %s[-f trfile] [-g pgid] [-p pid] "
    273 	    "[-t [Aaceilmnsuvw+]]\n\t%s [-aCcid%s] %s[-f trfile] [-t "
    274 	    "[Aaceilmnsuvw+]] command\n",
    275 	    getprogname(), SHRT_OPTION, LONG_OPTION,
    276 	    getprogname(), SHRT_OPTION, LONG_OPTION);
    277 	exit(1);
    278 }
    279 
    280 static const char *ktracefile = NULL;
    281 void
    282 no_ktrace(sig)
    283         int sig;
    284 {
    285 	if (ktracefile)
    286 		(void)unlink(ktracefile);
    287         (void)errx(1, "ktrace(2) system call not supported in the running"
    288 	    " kernel; re-compile kernel with `options KTRACE'");
    289 }
    290 
    291 int
    292 do_ktrace(tracefile, ops, trpoints, pid)
    293 	const char *tracefile;
    294 	int ops;
    295 	int trpoints;
    296 	int pid;
    297 {
    298 	int ret;
    299 
    300 	if (!tracefile || strcmp(tracefile, "-") == 0) {
    301 		int pi[2], dofork, fpid;
    302 
    303 		if (pipe(pi) < 0)
    304 			err(1, "pipe(2)");
    305 		fcntl(pi[0], F_SETFD, FD_CLOEXEC|fcntl(pi[0], F_GETFD, 0));
    306 		fcntl(pi[1], F_SETFD, FD_CLOEXEC|fcntl(pi[1], F_GETFD, 0));
    307 
    308 		dofork = (pid == getpid());
    309 
    310 #ifdef KTRUSS
    311 		if (dofork)
    312 			fpid = fork();
    313 		else
    314 			fpid = pid;	/* XXX: Gcc */
    315 #else
    316 		if (dofork)
    317 			fpid = fork();
    318 		else
    319 			fpid = 0;	/* XXX: Gcc */
    320 #endif
    321 #ifdef KTRUSS
    322 		if (fpid)
    323 #else
    324 		if (!dofork || !fpid)
    325 #endif
    326 		{
    327 			if (!dofork)
    328 #ifdef KTRUSS
    329 				ret = fktrace(pi[1], ops, trpoints, fpid);
    330 #else
    331 				ret = fktrace(pi[1], ops, trpoints, pid);
    332 #endif
    333 			else
    334 				close(pi[1]);
    335 #ifdef KTRUSS
    336 			dumpfile(NULL, pi[0], trpoints);
    337 			waitpid(fpid, NULL, 0);
    338 #else
    339 			{
    340 				char	buf[512];
    341 				int	n, cnt = 0;
    342 
    343 				while ((n = read(pi[0], buf, sizeof(buf)))>0) {
    344 					write(1, buf, n);
    345 					cnt += n;
    346 				}
    347 			}
    348 			if (dofork)
    349 				_exit(0);
    350 #endif
    351 			return 0;
    352 		}
    353 		close(pi[0]);
    354 #ifdef KTRUSS
    355 		if (dofork && !fpid) {
    356 			ret = fktrace(pi[1], ops, trpoints, getpid());
    357 			return 1;
    358 		}
    359 #else
    360 		ret = fktrace(pi[1], ops, trpoints, pid);
    361 #endif
    362 	} else
    363 		ret = ktrace(ktracefile = tracefile, ops, trpoints, pid);
    364 	if (ret < 0)
    365 		err(1, "%s", tracefile);
    366 	return 1;
    367 }
    368