xref: /csrg-svn/usr.bin/ktrace/ktrace.c (revision 53174)
144584Smarc /*-
236426Smarc  * Copyright (c) 1988 The Regents of the University of California.
336426Smarc  * All rights reserved.
436426Smarc  *
544584Smarc  * %sccs.include.redist.c%
636426Smarc  */
736426Smarc 
836426Smarc #ifndef lint
936426Smarc char copyright[] =
1036426Smarc "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
1136426Smarc  All rights reserved.\n";
1236426Smarc #endif /* not lint */
1336426Smarc 
1436426Smarc #ifndef lint
15*53174Smarc static char sccsid[] = "@(#)ktrace.c	5.4 (Berkeley) 04/15/92";
1636426Smarc #endif /* not lint */
1736426Smarc 
1845987Sbostic #include <sys/param.h>
1945987Sbostic #include <sys/stat.h>
2045987Sbostic #include <sys/file.h>
2145987Sbostic #include <sys/time.h>
2245987Sbostic #include <sys/errno.h>
2345987Sbostic #include <sys/uio.h>
2445987Sbostic #include <sys/ktrace.h>
2545987Sbostic #include <stdio.h>
2636462Smarc #include "ktrace.h"
2736426Smarc 
2836426Smarc main(argc, argv)
2945987Sbostic 	int argc;
3045987Sbostic 	char **argv;
3136426Smarc {
3236426Smarc 	extern int optind;
3336426Smarc 	extern char *optarg;
3445987Sbostic 	enum { NOTSET, CLEAR, CLEARALL } clear;
3545987Sbostic 	int append, ch, fd, inherit, ops, pid, pidset, trpoints;
3645987Sbostic 	char *tracefile;
3736426Smarc 
3845987Sbostic 	clear = NOTSET;
39*53174Smarc 	append = ops = pidset = inherit = 0;
4053171Smarc 	trpoints = DEF_POINTS;
4145987Sbostic 	tracefile = DEF_TRACEFILE;
4245987Sbostic 	while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF)
4336426Smarc 		switch((char)ch) {
4445987Sbostic 		case 'a':
4545987Sbostic 			append = 1;
4645987Sbostic 			break;
4740361Smarc 		case 'C':
4845987Sbostic 			clear = CLEARALL;
4947026Smarc 			pidset = 1;
5040361Smarc 			break;
5140361Smarc 		case 'c':
5245987Sbostic 			clear = CLEAR;
5340361Smarc 			break;
5440361Smarc 		case 'd':
5540361Smarc 			ops |= KTRFLAG_DESCEND;
5640361Smarc 			break;
5745987Sbostic 		case 'f':
5845987Sbostic 			tracefile = optarg;
5940361Smarc 			break;
6040361Smarc 		case 'g':
6145987Sbostic 			pid = -rpid(optarg);
6245987Sbostic 			pidset = 1;
6340361Smarc 			break;
6440361Smarc 		case 'i':
6545987Sbostic 			inherit = 1;
6640361Smarc 			break;
6745987Sbostic 		case 'p':
6845987Sbostic 			pid = rpid(optarg);
6945987Sbostic 			pidset = 1;
7040361Smarc 			break;
7145987Sbostic 		case 't':
7245987Sbostic 			trpoints = getpoints(optarg);
7345987Sbostic 			if (trpoints < 0) {
7445987Sbostic 				(void)fprintf(stderr,
7545987Sbostic 				    "ktrace: unknown facility in %s\n", optarg);
7645987Sbostic 				usage();
7745987Sbostic 			}
7840361Smarc 			break;
7940361Smarc 		default:
8045987Sbostic 			usage();
8136426Smarc 		}
8245987Sbostic 	argv += optind;
8345987Sbostic 	argc -= optind;
8436426Smarc 
8545987Sbostic 	if (pidset && *argv || !pidset && !*argv)
8645987Sbostic 		usage();
8745987Sbostic 
8840361Smarc 	if (inherit)
8944584Smarc 		trpoints |= KTRFAC_INHERIT;
9045987Sbostic 
9145987Sbostic 	if (clear != NOTSET) {
9245987Sbostic 		if (clear == CLEARALL) {
9340361Smarc 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
9453171Smarc 			trpoints = ALL_POINTS;
9540361Smarc 			pid = 1;
9645987Sbostic 		} else
9740361Smarc 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
9845987Sbostic 
9945987Sbostic 		if (ktrace(tracefile, ops, trpoints, pid) < 0)
10045987Sbostic 			error(tracefile);
10136426Smarc 		exit(0);
10236426Smarc 	}
10336462Smarc 
10445987Sbostic 	if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC),
10545987Sbostic 	    DEFFILEMODE)) < 0)
10645987Sbostic 		error(tracefile);
10745987Sbostic 	(void)close(fd);
10845987Sbostic 
10945987Sbostic 	if (*argv) {
11045987Sbostic 		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
11145987Sbostic 			error();
11245987Sbostic 		execvp(argv[0], &argv[0]);
11345987Sbostic 		error(argv[0]);
11436462Smarc 		exit(1);
11536462Smarc 	}
11645987Sbostic 	else if (ktrace(tracefile, ops, trpoints, pid) < 0)
11745987Sbostic 		error(tracefile);
11836426Smarc 	exit(0);
11936426Smarc }
12045987Sbostic 
12145987Sbostic rpid(p)
12245987Sbostic 	char *p;
12345987Sbostic {
12445987Sbostic 	static int first;
12545987Sbostic 
12645987Sbostic 	if (first++) {
12745987Sbostic 		(void)fprintf(stderr,
12845987Sbostic 		    "ktrace: only one -g or -p flag is permitted.\n");
12945987Sbostic 		usage();
13045987Sbostic 	}
13145987Sbostic 	if (!*p) {
13245987Sbostic 		(void)fprintf(stderr, "ktrace: illegal process id.\n");
13345987Sbostic 		usage();
13445987Sbostic 	}
13545987Sbostic 	return(atoi(p));
13645987Sbostic }
13745987Sbostic 
13845987Sbostic error(name)
13945987Sbostic 	char *name;
14045987Sbostic {
14145987Sbostic 	(void)fprintf(stderr, "ktrace: %s: %s.\n", name, strerror(errno));
14245987Sbostic 	exit(1);
14345987Sbostic }
14445987Sbostic 
14545987Sbostic usage()
14645987Sbostic {
14745987Sbostic 	(void)fprintf(stderr,
14845987Sbostic "usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n");
14945987Sbostic 	exit(1);
15045987Sbostic }
151