xref: /freebsd/usr.bin/ktrace/ktrace.c (revision 4ac5adf036a78e6761d6d3067e6d41ec5fde5a95)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1988, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
410bff6132SJames Raynard #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)ktrace.c	8.1 (Berkeley) 6/6/93";
430bff6132SJames Raynard #endif
440bff6132SJames Raynard static const char rcsid[] =
454ac5adf0SJoerg Wunsch 	"$Id: ktrace.c,v 1.4 1996/06/19 09:56:29 jraynard Exp $";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <sys/param.h>
499b50d902SRodney W. Grimes #include <sys/stat.h>
509b50d902SRodney W. Grimes #include <sys/file.h>
519b50d902SRodney W. Grimes #include <sys/time.h>
529b50d902SRodney W. Grimes #include <sys/errno.h>
539b50d902SRodney W. Grimes #include <sys/uio.h>
549b50d902SRodney W. Grimes #include <sys/ktrace.h>
550bff6132SJames Raynard 
560bff6132SJames Raynard #include <err.h>
579b50d902SRodney W. Grimes #include <stdio.h>
580bff6132SJames Raynard #include <unistd.h>
590bff6132SJames Raynard 
609b50d902SRodney W. Grimes #include "ktrace.h"
619b50d902SRodney W. Grimes 
620bff6132SJames Raynard void no_ktrace __P((int));
630bff6132SJames Raynard void usage __P((void));
64b0130767SAndreas Schulz 
659b50d902SRodney W. Grimes main(argc, argv)
669b50d902SRodney W. Grimes 	int argc;
679b50d902SRodney W. Grimes 	char **argv;
689b50d902SRodney W. Grimes {
699b50d902SRodney W. Grimes 	extern int optind;
709b50d902SRodney W. Grimes 	extern char *optarg;
719b50d902SRodney W. Grimes 	enum { NOTSET, CLEAR, CLEARALL } clear;
729b50d902SRodney W. Grimes 	int append, ch, fd, inherit, ops, pid, pidset, trpoints;
739b50d902SRodney W. Grimes 	char *tracefile;
744ac5adf0SJoerg Wunsch 	mode_t omask;
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes 	clear = NOTSET;
779b50d902SRodney W. Grimes 	append = ops = pidset = inherit = 0;
789b50d902SRodney W. Grimes 	trpoints = DEF_POINTS;
799b50d902SRodney W. Grimes 	tracefile = DEF_TRACEFILE;
809b50d902SRodney W. Grimes 	while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF)
819b50d902SRodney W. Grimes 		switch((char)ch) {
829b50d902SRodney W. Grimes 		case 'a':
839b50d902SRodney W. Grimes 			append = 1;
849b50d902SRodney W. Grimes 			break;
859b50d902SRodney W. Grimes 		case 'C':
869b50d902SRodney W. Grimes 			clear = CLEARALL;
879b50d902SRodney W. Grimes 			pidset = 1;
889b50d902SRodney W. Grimes 			break;
899b50d902SRodney W. Grimes 		case 'c':
909b50d902SRodney W. Grimes 			clear = CLEAR;
919b50d902SRodney W. Grimes 			break;
929b50d902SRodney W. Grimes 		case 'd':
939b50d902SRodney W. Grimes 			ops |= KTRFLAG_DESCEND;
949b50d902SRodney W. Grimes 			break;
959b50d902SRodney W. Grimes 		case 'f':
969b50d902SRodney W. Grimes 			tracefile = optarg;
979b50d902SRodney W. Grimes 			break;
989b50d902SRodney W. Grimes 		case 'g':
999b50d902SRodney W. Grimes 			pid = -rpid(optarg);
1009b50d902SRodney W. Grimes 			pidset = 1;
1019b50d902SRodney W. Grimes 			break;
1029b50d902SRodney W. Grimes 		case 'i':
1039b50d902SRodney W. Grimes 			inherit = 1;
1049b50d902SRodney W. Grimes 			break;
1059b50d902SRodney W. Grimes 		case 'p':
1069b50d902SRodney W. Grimes 			pid = rpid(optarg);
1079b50d902SRodney W. Grimes 			pidset = 1;
1089b50d902SRodney W. Grimes 			break;
1099b50d902SRodney W. Grimes 		case 't':
1109b50d902SRodney W. Grimes 			trpoints = getpoints(optarg);
1119b50d902SRodney W. Grimes 			if (trpoints < 0) {
1120bff6132SJames Raynard 				warnx("unknown facility in %s", optarg);
1139b50d902SRodney W. Grimes 				usage();
1149b50d902SRodney W. Grimes 			}
1159b50d902SRodney W. Grimes 			break;
1169b50d902SRodney W. Grimes 		default:
1179b50d902SRodney W. Grimes 			usage();
1189b50d902SRodney W. Grimes 		}
1199b50d902SRodney W. Grimes 	argv += optind;
1209b50d902SRodney W. Grimes 	argc -= optind;
1219b50d902SRodney W. Grimes 
1229b50d902SRodney W. Grimes 	if (pidset && *argv || !pidset && !*argv)
1239b50d902SRodney W. Grimes 		usage();
1249b50d902SRodney W. Grimes 
1259b50d902SRodney W. Grimes 	if (inherit)
1269b50d902SRodney W. Grimes 		trpoints |= KTRFAC_INHERIT;
1279b50d902SRodney W. Grimes 
1280bff6132SJames Raynard 	(void)signal(SIGSYS, no_ktrace);
1299b50d902SRodney W. Grimes 	if (clear != NOTSET) {
1309b50d902SRodney W. Grimes 		if (clear == CLEARALL) {
1319b50d902SRodney W. Grimes 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
1329b50d902SRodney W. Grimes 			trpoints = ALL_POINTS;
1339b50d902SRodney W. Grimes 			pid = 1;
1349b50d902SRodney W. Grimes 		} else
1359b50d902SRodney W. Grimes 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
1369b50d902SRodney W. Grimes 
1379b50d902SRodney W. Grimes 		if (ktrace(tracefile, ops, trpoints, pid) < 0)
1380bff6132SJames Raynard 			err(1, tracefile);
1399b50d902SRodney W. Grimes 		exit(0);
1409b50d902SRodney W. Grimes 	}
1419b50d902SRodney W. Grimes 
1424ac5adf0SJoerg Wunsch 	omask = umask(S_IRWXG|S_IRWXO);
1439b50d902SRodney W. Grimes 	if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC),
1449b50d902SRodney W. Grimes 	    DEFFILEMODE)) < 0)
1450bff6132SJames Raynard 		err(1, tracefile);
1464ac5adf0SJoerg Wunsch 	(void)umask(omask);
1479b50d902SRodney W. Grimes 	(void)close(fd);
1489b50d902SRodney W. Grimes 
1499b50d902SRodney W. Grimes 	if (*argv) {
1509b50d902SRodney W. Grimes 		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
1510bff6132SJames Raynard 			err(1, tracefile);
1529b50d902SRodney W. Grimes 		execvp(argv[0], &argv[0]);
1530bff6132SJames Raynard 		err(1, "exec of '%s' failed", argv[0]);
1549b50d902SRodney W. Grimes 	}
1559b50d902SRodney W. Grimes 	else if (ktrace(tracefile, ops, trpoints, pid) < 0)
1560bff6132SJames Raynard 		err(1, tracefile);
1579b50d902SRodney W. Grimes 	exit(0);
1589b50d902SRodney W. Grimes }
1599b50d902SRodney W. Grimes 
1609b50d902SRodney W. Grimes rpid(p)
1619b50d902SRodney W. Grimes 	char *p;
1629b50d902SRodney W. Grimes {
1639b50d902SRodney W. Grimes 	static int first;
1649b50d902SRodney W. Grimes 
1659b50d902SRodney W. Grimes 	if (first++) {
1660bff6132SJames Raynard 		warnx("only one -g or -p flag is permitted.");
1679b50d902SRodney W. Grimes 		usage();
1689b50d902SRodney W. Grimes 	}
1699b50d902SRodney W. Grimes 	if (!*p) {
1700bff6132SJames Raynard 		warnx("illegal process id.");
1719b50d902SRodney W. Grimes 		usage();
1729b50d902SRodney W. Grimes 	}
1739b50d902SRodney W. Grimes 	return(atoi(p));
1749b50d902SRodney W. Grimes }
1759b50d902SRodney W. Grimes 
1760bff6132SJames Raynard void
1779b50d902SRodney W. Grimes usage()
1789b50d902SRodney W. Grimes {
1799b50d902SRodney W. Grimes 	(void)fprintf(stderr,
1809b50d902SRodney W. Grimes "usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n");
1819b50d902SRodney W. Grimes 	exit(1);
1829b50d902SRodney W. Grimes }
1830bff6132SJames Raynard 
1840bff6132SJames Raynard void
1850bff6132SJames Raynard no_ktrace(sig)
1860bff6132SJames Raynard         int sig;
1870bff6132SJames Raynard {
1880bff6132SJames Raynard         (void)fprintf(stderr,
1890bff6132SJames Raynard "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
1900bff6132SJames Raynard         exit(1);
1910bff6132SJames Raynard }
192