2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
52 #include <sys/ktrace.h>
63 static char def_tracefile[] = DEF_TRACEFILE;
65 static enum clear { NOTSET, CLEAR, CLEARALL } clear = NOTSET;
68 static void no_ktrace(int);
69 static void set_pid_clear(const char *, enum clear);
70 static void usage(void);
73 main(int argc, char *argv[])
75 int append, ch, fd, inherit, ops, trpoints;
76 const char *tracefile;
80 append = ops = inherit = 0;
81 trpoints = DEF_POINTS;
82 tracefile = def_tracefile;
83 while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
89 set_pid_clear("1", CLEARALL);
92 set_pid_clear(NULL, CLEAR);
95 ops |= KTRFLAG_DESCEND;
101 set_pid_clear(optarg, NOTSET);
108 set_pid_clear(optarg, NOTSET);
111 trpoints = getpoints(optarg);
113 warnx("unknown facility in %s", optarg);
124 /* must have either -[Cc], a pid or a command */
125 if (clear == NOTSET && pid == 0 && argc == 0)
127 /* can't have both a pid and a command */
128 /* (note that -C sets pid to 1) */
129 if (pid != 0 && argc > 0) {
134 trpoints |= KTRFAC_INHERIT;
136 (void)signal(SIGSYS, no_ktrace);
137 if (clear != NOTSET) {
138 if (clear == CLEARALL) {
139 ops = KTROP_CLEAR | KTRFLAG_DESCEND;
140 trpoints = ALL_POINTS;
142 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
144 if (ktrace(tracefile, ops, trpoints, pid) < 0)
145 err(1, "%s", tracefile);
149 omask = umask(S_IRWXG|S_IRWXO);
151 if ((fd = open(tracefile, O_CREAT | O_WRONLY | O_NONBLOCK,
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",
157 if (!(S_ISREG(sb.st_mode)))
158 errx(1, "%s not regular file", tracefile);
160 if (unlink(tracefile) == -1 && errno != ENOENT)
161 err(1, "unlink %s", tracefile);
162 if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
164 err(1, "%s", tracefile);
169 trpoints |= PROC_ABI_POINTS;
172 if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
173 err(1, "%s", tracefile);
175 err(1, "exec of '%s' failed", *argv);
177 if (ktrace(tracefile, ops, trpoints, pid) < 0)
178 err(1, "%s", tracefile);
183 set_pid_clear(const char *p, enum clear cl)
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");
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");
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");
205 n = strtoimax(p, &e, 10);
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.
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");
220 if ((clear = cl) == CLEARALL)
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");
235 no_ktrace(int sig __unused)
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'");