xref: /freebsd/usr.bin/ktrace/ktrace.c (revision c4f6a2a9e1b1879b618c436ab4f56ff75c73a0f5)
1 /*-
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)ktrace.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <sys/file.h>
51 #include <sys/time.h>
52 #include <sys/errno.h>
53 #include <sys/uio.h>
54 #include <sys/ktrace.h>
55 
56 #include <err.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <unistd.h>
61 
62 #include "ktrace.h"
63 
64 static char def_tracefile[] = DEF_TRACEFILE;
65 
66 static void no_ktrace(int);
67 static int rpid(char *);
68 static void usage(void);
69 
70 int
71 main(int argc, char *argv[])
72 {
73 	enum { NOTSET, CLEAR, CLEARALL } clear;
74 	int append, ch, fd, inherit, ops, pid, pidset, trpoints;
75 	const char *tracefile;
76 	mode_t omask;
77 	struct stat sb;
78 
79 	clear = NOTSET;
80 	append = ops = pidset = inherit = 0;
81 	trpoints = DEF_POINTS;
82 	tracefile = def_tracefile;
83 	while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
84 		switch((char)ch) {
85 		case 'a':
86 			append = 1;
87 			break;
88 		case 'C':
89 			clear = CLEARALL;
90 			pidset = 1;
91 			break;
92 		case 'c':
93 			clear = CLEAR;
94 			break;
95 		case 'd':
96 			ops |= KTRFLAG_DESCEND;
97 			break;
98 		case 'f':
99 			tracefile = optarg;
100 			break;
101 		case 'g':
102 			pid = -rpid(optarg);
103 			pidset = 1;
104 			break;
105 		case 'i':
106 			inherit = 1;
107 			break;
108 		case 'p':
109 			pid = rpid(optarg);
110 			pidset = 1;
111 			break;
112 		case 't':
113 			trpoints = getpoints(optarg);
114 			if (trpoints < 0) {
115 				warnx("unknown facility in %s", optarg);
116 				usage();
117 			}
118 			break;
119 		default:
120 			usage();
121 		}
122 	argv += optind;
123 	argc -= optind;
124 
125 	if ((pidset && *argv) || (!pidset && !*argv))
126 		usage();
127 
128 	if (inherit)
129 		trpoints |= KTRFAC_INHERIT;
130 
131 	(void)signal(SIGSYS, no_ktrace);
132 	if (clear != NOTSET) {
133 		if (clear == CLEARALL) {
134 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
135 			trpoints = ALL_POINTS;
136 			pid = 1;
137 		} else
138 			ops |= pidset ? KTROP_CLEAR : KTROP_CLEARFILE;
139 
140 		if (ktrace(tracefile, ops, trpoints, pid) < 0)
141 			err(1, "%s", tracefile);
142 		exit(0);
143 	}
144 
145 	omask = umask(S_IRWXG|S_IRWXO);
146 	if (append) {
147 		if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) < 0)
148 			err(1, "%s", tracefile);
149 		if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
150 			errx(1, "Refuse to append to %s not owned by you.",
151 			    tracefile);
152 	} else {
153 		if (unlink(tracefile) == -1 && errno != ENOENT)
154 			err(1, "unlink %s", tracefile);
155 		if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
156 		    DEFFILEMODE)) < 0)
157 			err(1, "%s", tracefile);
158 	}
159 	(void)umask(omask);
160 	(void)close(fd);
161 
162 	if (*argv) {
163 		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
164 			err(1, "%s", tracefile);
165 		execvp(argv[0], &argv[0]);
166 		err(1, "exec of '%s' failed", argv[0]);
167 	}
168 	else if (ktrace(tracefile, ops, trpoints, pid) < 0)
169 		err(1, "%s", tracefile);
170 	exit(0);
171 }
172 
173 static int
174 rpid(char *p)
175 {
176 	static int first;
177 
178 	if (first++) {
179 		warnx("only one -g or -p flag is permitted.");
180 		usage();
181 	}
182 	if (!*p) {
183 		warnx("illegal process id.");
184 		usage();
185 	}
186 	return(atoi(p));
187 }
188 
189 static void
190 usage(void)
191 {
192 	(void)fprintf(stderr, "%s\n%s\n",
193 "usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t cnisuw]",
194 "       ktrace [-adi] [-f trfile] [-t cnisuw] command");
195 	exit(1);
196 }
197 
198 static void
199 no_ktrace(int sig __unused)
200 {
201         (void)fprintf(stderr,
202 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
203         exit(1);
204 }
205