xref: /freebsd/usr.bin/ktrace/ktrace.c (revision 0c4d24a78fbd9f937393e4fdf3bbb07c9b762c03)
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[] =
45c3aac50fSPeter Wemm   "$FreeBSD$";
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;
759bedbe6cSJoerg Wunsch 	struct stat sb;
769b50d902SRodney W. Grimes 
779b50d902SRodney W. Grimes 	clear = NOTSET;
789b50d902SRodney W. Grimes 	append = ops = pidset = inherit = 0;
799b50d902SRodney W. Grimes 	trpoints = DEF_POINTS;
809b50d902SRodney W. Grimes 	tracefile = DEF_TRACEFILE;
811c8af878SWarner Losh 	while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
829b50d902SRodney W. Grimes 		switch((char)ch) {
839b50d902SRodney W. Grimes 		case 'a':
849b50d902SRodney W. Grimes 			append = 1;
859b50d902SRodney W. Grimes 			break;
869b50d902SRodney W. Grimes 		case 'C':
879b50d902SRodney W. Grimes 			clear = CLEARALL;
889b50d902SRodney W. Grimes 			pidset = 1;
899b50d902SRodney W. Grimes 			break;
909b50d902SRodney W. Grimes 		case 'c':
919b50d902SRodney W. Grimes 			clear = CLEAR;
929b50d902SRodney W. Grimes 			break;
939b50d902SRodney W. Grimes 		case 'd':
949b50d902SRodney W. Grimes 			ops |= KTRFLAG_DESCEND;
959b50d902SRodney W. Grimes 			break;
969b50d902SRodney W. Grimes 		case 'f':
979b50d902SRodney W. Grimes 			tracefile = optarg;
989b50d902SRodney W. Grimes 			break;
999b50d902SRodney W. Grimes 		case 'g':
1009b50d902SRodney W. Grimes 			pid = -rpid(optarg);
1019b50d902SRodney W. Grimes 			pidset = 1;
1029b50d902SRodney W. Grimes 			break;
1039b50d902SRodney W. Grimes 		case 'i':
1049b50d902SRodney W. Grimes 			inherit = 1;
1059b50d902SRodney W. Grimes 			break;
1069b50d902SRodney W. Grimes 		case 'p':
1079b50d902SRodney W. Grimes 			pid = rpid(optarg);
1089b50d902SRodney W. Grimes 			pidset = 1;
1099b50d902SRodney W. Grimes 			break;
1109b50d902SRodney W. Grimes 		case 't':
1119b50d902SRodney W. Grimes 			trpoints = getpoints(optarg);
1129b50d902SRodney W. Grimes 			if (trpoints < 0) {
1130bff6132SJames Raynard 				warnx("unknown facility in %s", optarg);
1149b50d902SRodney W. Grimes 				usage();
1159b50d902SRodney W. Grimes 			}
1169b50d902SRodney W. Grimes 			break;
1179b50d902SRodney W. Grimes 		default:
1189b50d902SRodney W. Grimes 			usage();
1199b50d902SRodney W. Grimes 		}
1209b50d902SRodney W. Grimes 	argv += optind;
1219b50d902SRodney W. Grimes 	argc -= optind;
1229b50d902SRodney W. Grimes 
1239b50d902SRodney W. Grimes 	if (pidset && *argv || !pidset && !*argv)
1249b50d902SRodney W. Grimes 		usage();
1259b50d902SRodney W. Grimes 
1269b50d902SRodney W. Grimes 	if (inherit)
1279b50d902SRodney W. Grimes 		trpoints |= KTRFAC_INHERIT;
1289b50d902SRodney W. Grimes 
1290bff6132SJames Raynard 	(void)signal(SIGSYS, no_ktrace);
1309b50d902SRodney W. Grimes 	if (clear != NOTSET) {
1319b50d902SRodney W. Grimes 		if (clear == CLEARALL) {
1329b50d902SRodney W. Grimes 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
1339b50d902SRodney W. Grimes 			trpoints = ALL_POINTS;
1349b50d902SRodney W. Grimes 			pid = 1;
1359b50d902SRodney W. Grimes 		} else
1369b50d902SRodney W. Grimes 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
1379b50d902SRodney W. Grimes 
1389b50d902SRodney W. Grimes 		if (ktrace(tracefile, ops, trpoints, pid) < 0)
1390c4d24a7SKris Kennaway 			err(1, "%s", tracefile);
1409b50d902SRodney W. Grimes 		exit(0);
1419b50d902SRodney W. Grimes 	}
1429b50d902SRodney W. Grimes 
1434ac5adf0SJoerg Wunsch 	omask = umask(S_IRWXG|S_IRWXO);
1449bedbe6cSJoerg Wunsch 	if (append) {
1459bedbe6cSJoerg Wunsch 		if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) < 0)
1460c4d24a7SKris Kennaway 			err(1, "%s", tracefile);
1479bedbe6cSJoerg Wunsch 		if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
1489bedbe6cSJoerg Wunsch 			errx(1, "Refuse to append to %s not owned by you.",
1499bedbe6cSJoerg Wunsch 			    tracefile);
1509bedbe6cSJoerg Wunsch 	} else {
1519bedbe6cSJoerg Wunsch 		if (unlink(tracefile) == -1 && errno != ENOENT)
1529bedbe6cSJoerg Wunsch 			err(1, "unlink %s", tracefile);
1539bedbe6cSJoerg Wunsch 		if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
1549b50d902SRodney W. Grimes 		    DEFFILEMODE)) < 0)
1550c4d24a7SKris Kennaway 			err(1, "%s", tracefile);
1569bedbe6cSJoerg Wunsch 	}
1574ac5adf0SJoerg Wunsch 	(void)umask(omask);
1589b50d902SRodney W. Grimes 	(void)close(fd);
1599b50d902SRodney W. Grimes 
1609b50d902SRodney W. Grimes 	if (*argv) {
1619b50d902SRodney W. Grimes 		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
1620c4d24a7SKris Kennaway 			err(1, "%s", tracefile);
1639b50d902SRodney W. Grimes 		execvp(argv[0], &argv[0]);
1640bff6132SJames Raynard 		err(1, "exec of '%s' failed", argv[0]);
1659b50d902SRodney W. Grimes 	}
1669b50d902SRodney W. Grimes 	else if (ktrace(tracefile, ops, trpoints, pid) < 0)
1670c4d24a7SKris Kennaway 		err(1, "%s", tracefile);
1689b50d902SRodney W. Grimes 	exit(0);
1699b50d902SRodney W. Grimes }
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes rpid(p)
1729b50d902SRodney W. Grimes 	char *p;
1739b50d902SRodney W. Grimes {
1749b50d902SRodney W. Grimes 	static int first;
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes 	if (first++) {
1770bff6132SJames Raynard 		warnx("only one -g or -p flag is permitted.");
1789b50d902SRodney W. Grimes 		usage();
1799b50d902SRodney W. Grimes 	}
1809b50d902SRodney W. Grimes 	if (!*p) {
1810bff6132SJames Raynard 		warnx("illegal process id.");
1829b50d902SRodney W. Grimes 		usage();
1839b50d902SRodney W. Grimes 	}
1849b50d902SRodney W. Grimes 	return(atoi(p));
1859b50d902SRodney W. Grimes }
1869b50d902SRodney W. Grimes 
1870bff6132SJames Raynard void
1889b50d902SRodney W. Grimes usage()
1899b50d902SRodney W. Grimes {
1904a744ef6SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n",
1914a744ef6SPhilippe Charnier "usage: ktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [cnisuv]",
1924a744ef6SPhilippe Charnier "       ktrace [-aCcid] [-f trfile] [-t [cnisuw] command");
1939b50d902SRodney W. Grimes 	exit(1);
1949b50d902SRodney W. Grimes }
1950bff6132SJames Raynard 
1960bff6132SJames Raynard void
1970bff6132SJames Raynard no_ktrace(sig)
1980bff6132SJames Raynard         int sig;
1990bff6132SJames Raynard {
2000bff6132SJames Raynard         (void)fprintf(stderr,
2010bff6132SJames Raynard "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
2020bff6132SJames Raynard         exit(1);
2030bff6132SJames Raynard }
204