Home | History | Annotate | Line # | Download | only in ktrace
ktrace.c revision 1.10
      1 /*	$NetBSD: ktrace.c,v 1.10 1999/01/11 22:40:01 kleink 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.10 1999/01/11 22:40:01 kleink 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/uio.h>
     55 #include <sys/ktrace.h>
     56 
     57 #include <err.h>
     58 #include <errno.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 		case 'f':
    118 			infile = optarg;
    119 			break;
    120 #else
    121 		case 'f':
    122 			infile = outfile = optarg;
    123 			break;
    124 #endif
    125 		case 'g':
    126 			pid = -rpid(optarg);
    127 			pidset = 1;
    128 			break;
    129 		case 'i':
    130 			inherit = 1;
    131 			break;
    132 #ifdef KTRUSS
    133 		case 'l':
    134 			tail = 1;
    135 			break;
    136 		case 'm':
    137 			maxdata = atoi(optarg);
    138 			break;
    139 		case 'o':
    140 			outfile = optarg;
    141 			break;
    142 #endif
    143 		case 'p':
    144 			pid = rpid(optarg);
    145 			pidset = 1;
    146 			break;
    147 #ifdef KTRUSS
    148 		case 'R':
    149 			timestamp = 2;	/* relative timestamp */
    150 			break;
    151 		case 'T':
    152 			timestamp = 1;
    153 			break;
    154 #endif
    155 		case 't':
    156 			trpoints = getpoints(optarg);
    157 			if (trpoints < 0) {
    158 				warnx("unknown facility in %s", optarg);
    159 				usage();
    160 			}
    161 			break;
    162 		default:
    163 			usage();
    164 		}
    165 	argv += optind;
    166 	argc -= optind;
    167 
    168 	if (!infile && ((pidset && *argv) || (!pidset && !*argv)))
    169 		usage();
    170 
    171 #ifdef KTRUSS
    172 	if (infile) {
    173 		dumpfile(infile, 0, trpoints);
    174 		exit(0);
    175 	}
    176 #endif
    177 	if (inherit)
    178 		trpoints |= KTRFAC_INHERIT;
    179 
    180 	(void)signal(SIGSYS, no_ktrace);
    181 	if (clear != NOTSET) {
    182 		if (clear == CLEARALL) {
    183 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
    184 			trpoints = ALL_POINTS;
    185 			pid = 1;
    186 		} else
    187 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
    188 
    189 		(void)do_ktrace(outfile, ops, trpoints, pid);
    190 		exit(0);
    191 	}
    192 
    193 	if (outfile && strcmp(outfile, "-")) {
    194 		if ((fd = open(outfile, O_CREAT | O_WRONLY |
    195 		    (append ? 0 : O_TRUNC), DEFFILEMODE)) < 0)
    196 			err(1, outfile);
    197 		(void)close(fd);
    198 	}
    199 
    200 	if (*argv) {
    201 		(void)do_ktrace(outfile, ops, trpoints, getpid());
    202 		execvp(argv[0], &argv[0]);
    203 		err(1, "exec of '%s' failed", argv[0]);
    204 	} else
    205 		(void)do_ktrace(outfile, ops, trpoints, pid);
    206 	exit(0);
    207 }
    208 
    209 int
    210 rpid(p)
    211 	char *p;
    212 {
    213 	static int first;
    214 
    215 	if (first++) {
    216 		warnx("only one -g or -p flag is permitted.");
    217 		usage();
    218 	}
    219 	if (!*p) {
    220 		warnx("illegal process id.");
    221 		usage();
    222 	}
    223 	return(atoi(p));
    224 }
    225 
    226 void
    227 usage()
    228 {
    229 	extern char *__progname;
    230 #ifdef KTRUSS
    231 # define LONG_OPTION "[-e emulation] [-m maxdata] [-o outfile] "
    232 # define SHRT_OPTION "RT"
    233 #else
    234 # define LONG_OPTION ""
    235 # define SHRT_OPTION ""
    236 #endif
    237 	(void)fprintf(stderr,
    238 "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",
    239 	__progname, SHRT_OPTION, LONG_OPTION,
    240 	__progname, SHRT_OPTION, LONG_OPTION);
    241 	exit(1);
    242 }
    243 
    244 void
    245 no_ktrace(sig)
    246         int sig;
    247 {
    248         (void)fprintf(stderr,
    249 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
    250         exit(1);
    251 }
    252 
    253 int
    254 do_ktrace(tracefile, ops, trpoints, pid)
    255 	char *tracefile;
    256 	int ops;
    257 	int trpoints;
    258 	int pid;
    259 {
    260 	int ret;
    261 
    262 	if (!tracefile || strcmp(tracefile, "-") == 0) {
    263 		int pi[2], nofork, fpid;
    264 
    265 		if (pipe(pi) < 0)
    266 			err(1, "pipe(2)");
    267 		fcntl(pi[0], F_SETFD, FD_CLOEXEC|fcntl(pi[0], F_GETFD, 0));
    268 		fcntl(pi[1], F_SETFD, FD_CLOEXEC|fcntl(pi[1], F_GETFD, 0));
    269 
    270 		nofork = (pid != getpid());
    271 
    272 		if (!nofork)
    273 			fpid = fork();
    274 		else
    275 			fpid = 0;	/* XXX: Gcc */
    276 		if (nofork || !fpid) {
    277 			if (nofork)
    278 				ret = fktrace(pi[1], ops, trpoints, pid);
    279 			else
    280 				close(pi[1]);
    281 #ifdef KTRUSS
    282 			dumpfile(NULL, pi[0], trpoints);
    283 #else
    284 			{
    285 				char	buf[512];
    286 				int	n, cnt = 0;
    287 
    288 				while ((n = read(pi[0], buf, sizeof(buf)))>0) {
    289 					write(1, buf, n);
    290 					cnt += n;
    291 				}
    292 			}
    293 #endif
    294 			return 0;
    295 		}
    296 		close(pi[0]);
    297 		ret = fktrace(pi[1], ops, trpoints, pid);
    298 	} else
    299 		ret = ktrace(tracefile, ops, trpoints, pid);
    300 	if (ret < 0)
    301 		err(1, tracefile);
    302 	return ret;
    303 }
    304