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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
54 #include <sys/ktrace.h>
65 static char def_tracefile[] = DEF_TRACEFILE;
67 static enum clear { NOTSET, CLEAR, CLEARALL } clear = NOTSET;
70 static void no_ktrace(int);
71 static void set_pid_clear(const char *, enum clear);
72 static void usage(void);
75 main(int argc, char *argv[])
77 int append, ch, fd, inherit, ops, trpoints;
78 const char *tracefile;
82 append = ops = inherit = 0;
83 trpoints = DEF_POINTS;
84 tracefile = def_tracefile;
85 while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
91 set_pid_clear("1", CLEARALL);
94 set_pid_clear(NULL, CLEAR);
97 ops |= KTRFLAG_DESCEND;
103 set_pid_clear(optarg, NOTSET);
110 set_pid_clear(optarg, NOTSET);
113 trpoints = getpoints(optarg);
115 warnx("unknown facility in %s", optarg);
126 /* must have either -[Cc], a pid or a command */
127 if (clear == NOTSET && pid == 0 && argc == 0)
129 /* can't have both a pid and a command */
130 /* (note that -C sets pid to 1) */
131 if (pid != 0 && argc > 0) {
136 trpoints |= KTRFAC_INHERIT;
138 (void)signal(SIGSYS, no_ktrace);
139 if (clear != NOTSET) {
140 if (clear == CLEARALL) {
141 ops = KTROP_CLEAR | KTRFLAG_DESCEND;
142 trpoints = ALL_POINTS;
144 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
146 if (ktrace(tracefile, ops, trpoints, pid) < 0)
147 err(1, "%s", tracefile);
151 omask = umask(S_IRWXG|S_IRWXO);
153 if ((fd = open(tracefile, O_CREAT | O_WRONLY | O_NONBLOCK,
155 err(1, "%s", tracefile);
156 if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
157 errx(1, "refuse to append to %s not owned by you",
159 if (!(S_ISREG(sb.st_mode)))
160 errx(1, "%s not regular file", tracefile);
162 if (unlink(tracefile) == -1 && errno != ENOENT)
163 err(1, "unlink %s", tracefile);
164 if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
166 err(1, "%s", tracefile);
171 trpoints |= PROC_ABI_POINTS;
174 if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
175 err(1, "%s", tracefile);
177 err(1, "exec of '%s' failed", *argv);
179 if (ktrace(tracefile, ops, trpoints, pid) < 0)
180 err(1, "%s", tracefile);
185 set_pid_clear(const char *p, enum clear cl)
190 if (clear != NOTSET && cl != NOTSET) {
191 /* either -c and -C or either of them twice */
192 warnx("only one -c or -C flag is permitted");
195 if ((clear == CLEARALL && p != NULL) || (cl == CLEARALL && pid != 0)) {
196 /* both -C and a pid or pgid */
197 warnx("the -C flag may not be combined with -g or -p");
200 if (p != NULL && pid != 0) {
201 /* either -p and -g or either of them twice */
202 warnx("only one -g or -p flag is permitted");
207 n = strtoimax(p, &e, 10);
209 * 1) not a number, or outside the range of an intmax_t
210 * 2) inside the range of intmax_t but outside the range
211 * of an int, keeping in mind that the pid may be
212 * negated if it's actually a pgid.
214 if (*e != '\0' || n < 1 || errno == ERANGE ||
215 n > (intmax_t)INT_MAX || n > -(intmax_t)INT_MIN) {
216 warnx("invalid process or group id");
222 if ((clear = cl) == CLEARALL)
230 fprintf(stderr, "%s\n%s\n",
231 "usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t trstr]",
232 " ktrace [-adi] [-f trfile] [-t trstr] command");
237 no_ktrace(int sig __unused)
240 fprintf(stderr, "error:\t%s\n\t%s\n",
241 "ktrace() system call not supported in the running kernel",
242 "re-compile kernel with 'options KTRACE'");