2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1988, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
50 #include <sys/ktrace.h>
61 static char def_tracefile[] = DEF_TRACEFILE;
63 static enum clear { NOTSET, CLEAR, CLEARALL } clear = NOTSET;
66 static void no_ktrace(int);
67 static void set_pid_clear(const char *, enum clear);
68 static void usage(void);
71 main(int argc, char *argv[])
73 int append, ch, fd, inherit, ops, trpoints;
74 const char *tracefile;
78 append = ops = inherit = 0;
79 trpoints = DEF_POINTS;
80 tracefile = def_tracefile;
81 while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
87 set_pid_clear("1", CLEARALL);
90 set_pid_clear(NULL, CLEAR);
93 ops |= KTRFLAG_DESCEND;
99 set_pid_clear(optarg, NOTSET);
106 set_pid_clear(optarg, NOTSET);
109 trpoints = getpoints(optarg);
111 warnx("unknown facility in %s", optarg);
122 /* must have either -[Cc], a pid or a command */
123 if (clear == NOTSET && pid == 0 && argc == 0)
125 /* can't have both a pid and a command */
126 /* (note that -C sets pid to 1) */
127 if (pid != 0 && argc > 0) {
132 trpoints |= KTRFAC_INHERIT;
134 (void)signal(SIGSYS, no_ktrace);
135 if (clear != NOTSET) {
136 if (clear == CLEARALL) {
137 ops = KTROP_CLEAR | KTRFLAG_DESCEND;
138 trpoints = ALL_POINTS;
140 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
142 if (ktrace(tracefile, ops, trpoints, pid) < 0)
143 err(1, "%s", tracefile);
147 omask = umask(S_IRWXG|S_IRWXO);
149 if ((fd = open(tracefile, O_CREAT | O_WRONLY | O_NONBLOCK,
151 err(1, "%s", tracefile);
152 if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
153 errx(1, "refuse to append to %s not owned by you",
155 if (!(S_ISREG(sb.st_mode)))
156 errx(1, "%s not regular file", tracefile);
158 if (unlink(tracefile) == -1 && errno != ENOENT)
159 err(1, "unlink %s", tracefile);
160 if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
162 err(1, "%s", tracefile);
167 trpoints |= PROC_ABI_POINTS;
170 if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
171 err(1, "%s", tracefile);
173 err(1, "exec of '%s' failed", *argv);
175 if (ktrace(tracefile, ops, trpoints, pid) < 0)
176 err(1, "%s", tracefile);
181 set_pid_clear(const char *p, enum clear cl)
186 if (clear != NOTSET && cl != NOTSET) {
187 /* either -c and -C or either of them twice */
188 warnx("only one -c or -C flag is permitted");
191 if ((clear == CLEARALL && p != NULL) || (cl == CLEARALL && pid != 0)) {
192 /* both -C and a pid or pgid */
193 warnx("the -C flag may not be combined with -g or -p");
196 if (p != NULL && pid != 0) {
197 /* either -p and -g or either of them twice */
198 warnx("only one -g or -p flag is permitted");
203 n = strtoimax(p, &e, 10);
205 * 1) not a number, or outside the range of an intmax_t
206 * 2) inside the range of intmax_t but outside the range
207 * of an int, keeping in mind that the pid may be
208 * negated if it's actually a pgid.
210 if (*e != '\0' || n < 1 || errno == ERANGE ||
211 n > (intmax_t)INT_MAX || n > -(intmax_t)INT_MIN) {
212 warnx("invalid process or group id");
218 if ((clear = cl) == CLEARALL)
226 fprintf(stderr, "%s\n%s\n",
227 "usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t trstr]",
228 " ktrace [-adi] [-f trfile] [-t trstr] command");
233 no_ktrace(int sig __unused)
236 fprintf(stderr, "error:\t%s\n\t%s\n",
237 "ktrace() system call not supported in the running kernel",
238 "re-compile kernel with 'options KTRACE'");