xref: /freebsd/usr.bin/ktrace/ktrace.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1988, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)ktrace.c	8.1 (Berkeley) 6/6/93";
41 #endif /* not lint */
42 #endif
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/param.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 #include <sys/ktrace.h>
53 
54 #include <err.h>
55 #include <errno.h>
56 #include <inttypes.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 
61 #include "ktrace.h"
62 
63 static char def_tracefile[] = DEF_TRACEFILE;
64 
65 static enum clear { NOTSET, CLEAR, CLEARALL } clear = NOTSET;
66 static int pid;
67 
68 static void no_ktrace(int);
69 static void set_pid_clear(const char *, enum clear);
70 static void usage(void);
71 
72 int
73 main(int argc, char *argv[])
74 {
75 	int append, ch, fd, inherit, ops, trpoints;
76 	const char *tracefile;
77 	mode_t omask;
78 	struct stat sb;
79 
80 	append = ops = 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 			set_pid_clear("1", CLEARALL);
90 			break;
91 		case 'c':
92 			set_pid_clear(NULL, CLEAR);
93 			break;
94 		case 'd':
95 			ops |= KTRFLAG_DESCEND;
96 			break;
97 		case 'f':
98 			tracefile = optarg;
99 			break;
100 		case 'g':
101 			set_pid_clear(optarg, NOTSET);
102 			pid = -pid;
103 			break;
104 		case 'i':
105 			inherit = 1;
106 			break;
107 		case 'p':
108 			set_pid_clear(optarg, NOTSET);
109 			break;
110 		case 't':
111 			trpoints = getpoints(optarg);
112 			if (trpoints < 0) {
113 				warnx("unknown facility in %s", optarg);
114 				usage();
115 			}
116 			break;
117 		default:
118 			usage();
119 		}
120 
121 	argv += optind;
122 	argc -= optind;
123 
124 	/* must have either -[Cc], a pid or a command */
125 	if (clear == NOTSET && pid == 0 && argc == 0)
126 		usage();
127 	/* can't have both a pid and a command */
128 	/* (note that -C sets pid to 1) */
129 	if (pid != 0 && argc > 0) {
130 		usage();
131 	}
132 
133 	if (inherit)
134 		trpoints |= KTRFAC_INHERIT;
135 
136 	(void)signal(SIGSYS, no_ktrace);
137 	if (clear != NOTSET) {
138 		if (clear == CLEARALL) {
139 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
140 			trpoints = ALL_POINTS;
141 		} else {
142 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
143 		}
144 		if (ktrace(tracefile, ops, trpoints, pid) < 0)
145 			err(1, "%s", tracefile);
146 		exit(0);
147 	}
148 
149 	omask = umask(S_IRWXG|S_IRWXO);
150 	if (append) {
151 		if ((fd = open(tracefile, O_CREAT | O_WRONLY | O_NONBLOCK,
152 		    DEFFILEMODE)) < 0)
153 			err(1, "%s", tracefile);
154 		if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
155 			errx(1, "refuse to append to %s not owned by you",
156 			    tracefile);
157 		if (!(S_ISREG(sb.st_mode)))
158 			errx(1, "%s not regular file", tracefile);
159 	} else {
160 		if (unlink(tracefile) == -1 && errno != ENOENT)
161 			err(1, "unlink %s", tracefile);
162 		if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
163 		    DEFFILEMODE)) < 0)
164 			err(1, "%s", tracefile);
165 	}
166 	(void)umask(omask);
167 	(void)close(fd);
168 
169 	trpoints |= PROC_ABI_POINTS;
170 
171 	if (argc > 0) {
172 		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
173 			err(1, "%s", tracefile);
174 		execvp(*argv, argv);
175 		err(1, "exec of '%s' failed", *argv);
176 	}
177 	if (ktrace(tracefile, ops, trpoints, pid) < 0)
178 		err(1, "%s", tracefile);
179 	exit(0);
180 }
181 
182 static void
183 set_pid_clear(const char *p, enum clear cl)
184 {
185 	intmax_t n;
186 	char *e;
187 
188 	if (clear != NOTSET && cl != NOTSET) {
189 		/* either -c and -C or either of them twice */
190 		warnx("only one -c or -C flag is permitted");
191 		usage();
192 	}
193 	if ((clear == CLEARALL && p != NULL) || (cl == CLEARALL && pid != 0)) {
194 		/* both -C and a pid or pgid */
195 		warnx("the -C flag may not be combined with -g or -p");
196 		usage();
197 	}
198 	if (p != NULL && pid != 0) {
199 		/* either -p and -g or either of them twice */
200 		warnx("only one -g or -p flag is permitted");
201 		usage();
202 	}
203 	if (p != NULL) {
204 		errno = 0;
205 		n = strtoimax(p, &e, 10);
206 		/*
207 		 * 1) not a number, or outside the range of an intmax_t
208 		 * 2) inside the range of intmax_t but outside the range
209 		 *    of an int, keeping in mind that the pid may be
210 		 *    negated if it's actually a pgid.
211 		 */
212 		if (*e != '\0' || n < 1 || errno == ERANGE ||
213 		    n > (intmax_t)INT_MAX || n > -(intmax_t)INT_MIN) {
214 			warnx("invalid process or group id");
215 			usage();
216 		}
217 		pid = n;
218 	}
219 	if (cl != NOTSET)
220 		if ((clear = cl) == CLEARALL)
221 			pid = 1;
222 }
223 
224 static void
225 usage(void)
226 {
227 
228 	fprintf(stderr, "%s\n%s\n",
229 	    "usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t trstr]",
230 	    "       ktrace [-adi] [-f trfile] [-t trstr] command");
231 	exit(1);
232 }
233 
234 static void
235 no_ktrace(int sig __unused)
236 {
237 
238 	fprintf(stderr, "error:\t%s\n\t%s\n",
239 	    "ktrace() system call not supported in the running kernel",
240 	    "re-compile kernel with 'options KTRACE'");
241         exit(1);
242 }
243