xref: /freebsd/usr.bin/ktrace/ktrace.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
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 #endif
40 
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/uio.h>
47 #include <sys/ktrace.h>
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <inttypes.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 
56 #include "ktrace.h"
57 
58 static char def_tracefile[] = DEF_TRACEFILE;
59 
60 static enum clear { NOTSET, CLEAR, CLEARALL } clear = NOTSET;
61 static int pid;
62 
63 static void no_ktrace(int);
64 static void set_pid_clear(const char *, enum clear);
65 static void usage(void) __dead2;
66 
67 int
68 main(int argc, char *argv[])
69 {
70 	int append, ch, fd, inherit, ops, trpoints;
71 	const char *tracefile;
72 	mode_t omask;
73 	struct stat sb;
74 
75 	append = ops = inherit = 0;
76 	trpoints = DEF_POINTS;
77 	tracefile = def_tracefile;
78 	while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
79 		switch((char)ch) {
80 		case 'a':
81 			append = 1;
82 			break;
83 		case 'C':
84 			set_pid_clear("1", CLEARALL);
85 			break;
86 		case 'c':
87 			set_pid_clear(NULL, CLEAR);
88 			break;
89 		case 'd':
90 			ops |= KTRFLAG_DESCEND;
91 			break;
92 		case 'f':
93 			tracefile = optarg;
94 			break;
95 		case 'g':
96 			set_pid_clear(optarg, NOTSET);
97 			pid = -pid;
98 			break;
99 		case 'i':
100 			inherit = 1;
101 			break;
102 		case 'p':
103 			set_pid_clear(optarg, NOTSET);
104 			break;
105 		case 't':
106 			trpoints = getpoints(optarg);
107 			if (trpoints < 0) {
108 				warnx("unknown facility in %s", optarg);
109 				usage();
110 			}
111 			break;
112 		default:
113 			usage();
114 		}
115 
116 	argv += optind;
117 	argc -= optind;
118 
119 	/* must have either -[Cc], a pid or a command */
120 	if (clear == NOTSET && pid == 0 && argc == 0)
121 		usage();
122 	/* can't have both a pid and a command */
123 	/* (note that -C sets pid to 1) */
124 	if (pid != 0 && argc > 0) {
125 		usage();
126 	}
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 		} else {
137 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
138 		}
139 		if (ktrace(tracefile, ops, trpoints, pid) < 0)
140 			err(1, "%s", tracefile);
141 		exit(0);
142 	}
143 
144 	omask = umask(S_IRWXG|S_IRWXO);
145 	if (append) {
146 		if ((fd = open(tracefile, O_CREAT | O_WRONLY | O_NONBLOCK,
147 		    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 		if (!(S_ISREG(sb.st_mode)))
153 			errx(1, "%s not regular file", tracefile);
154 	} else {
155 		if (unlink(tracefile) == -1 && errno != ENOENT)
156 			err(1, "unlink %s", tracefile);
157 		if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
158 		    DEFFILEMODE)) < 0)
159 			err(1, "%s", tracefile);
160 	}
161 	(void)umask(omask);
162 	(void)close(fd);
163 
164 	trpoints |= PROC_ABI_POINTS;
165 
166 	if (argc > 0) {
167 		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
168 			err(1, "%s", tracefile);
169 		execvp(*argv, argv);
170 		err(1, "exec of '%s' failed", *argv);
171 	}
172 	if (ktrace(tracefile, ops, trpoints, pid) < 0)
173 		err(1, "%s", tracefile);
174 	exit(0);
175 }
176 
177 static void
178 set_pid_clear(const char *p, enum clear cl)
179 {
180 	intmax_t n;
181 	char *e;
182 
183 	if (clear != NOTSET && cl != NOTSET) {
184 		/* either -c and -C or either of them twice */
185 		warnx("only one -c or -C flag is permitted");
186 		usage();
187 	}
188 	if ((clear == CLEARALL && p != NULL) || (cl == CLEARALL && pid != 0)) {
189 		/* both -C and a pid or pgid */
190 		warnx("the -C flag may not be combined with -g or -p");
191 		usage();
192 	}
193 	if (p != NULL && pid != 0) {
194 		/* either -p and -g or either of them twice */
195 		warnx("only one -g or -p flag is permitted");
196 		usage();
197 	}
198 	if (p != NULL) {
199 		errno = 0;
200 		n = strtoimax(p, &e, 10);
201 		/*
202 		 * 1) not a number, or outside the range of an intmax_t
203 		 * 2) inside the range of intmax_t but outside the range
204 		 *    of an int, keeping in mind that the pid may be
205 		 *    negated if it's actually a pgid.
206 		 */
207 		if (*e != '\0' || n < 1 || errno == ERANGE ||
208 		    n > (intmax_t)INT_MAX || n > -(intmax_t)INT_MIN) {
209 			warnx("invalid process or group id");
210 			usage();
211 		}
212 		pid = n;
213 	}
214 	if (cl != NOTSET)
215 		if ((clear = cl) == CLEARALL)
216 			pid = 1;
217 }
218 
219 static void
220 usage(void)
221 {
222 
223 	fprintf(stderr, "%s\n%s\n",
224 	    "usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t trstr]",
225 	    "       ktrace [-adi] [-f trfile] [-t trstr] command");
226 	exit(1);
227 }
228 
229 static void
230 no_ktrace(int sig __unused)
231 {
232 
233 	fprintf(stderr, "error:\t%s\n\t%s\n",
234 	    "ktrace() system call not supported in the running kernel",
235 	    "re-compile kernel with 'options KTRACE'");
236         exit(1);
237 }
238