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