]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/ktrace/ktrace.c
MFC r235211:
[FreeBSD/stable/8.git] / usr.bin / ktrace / ktrace.c
1 /*-
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)ktrace.c    8.1 (Berkeley) 6/6/93";
43 #endif /* not lint */
44 #endif
45
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 #include <sys/param.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/uio.h>
54 #include <sys/ktrace.h>
55
56 #include <err.h>
57 #include <errno.h>
58 #include <inttypes.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62
63 #include "ktrace.h"
64
65 static char def_tracefile[] = DEF_TRACEFILE;
66
67 static enum clear { NOTSET, CLEAR, CLEARALL } clear = NOTSET;
68 static int pid;
69
70 static void no_ktrace(int);
71 static void set_pid_clear(const char *, enum clear);
72 static void usage(void);
73
74 int
75 main(int argc, char *argv[])
76 {
77         int append, ch, fd, inherit, ops, trpoints;
78         const char *tracefile;
79         mode_t omask;
80         struct stat sb;
81
82         append = ops = inherit = 0;
83         trpoints = DEF_POINTS;
84         tracefile = def_tracefile;
85         while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
86                 switch((char)ch) {
87                 case 'a':
88                         append = 1;
89                         break;
90                 case 'C':
91                         set_pid_clear("1", CLEARALL);
92                         break;
93                 case 'c':
94                         set_pid_clear(NULL, CLEAR);
95                         break;
96                 case 'd':
97                         ops |= KTRFLAG_DESCEND;
98                         break;
99                 case 'f':
100                         tracefile = optarg;
101                         break;
102                 case 'g':
103                         set_pid_clear(optarg, NOTSET);
104                         pid = -pid;
105                         break;
106                 case 'i':
107                         inherit = 1;
108                         break;
109                 case 'p':
110                         set_pid_clear(optarg, NOTSET);
111                         break;
112                 case 't':
113                         trpoints = getpoints(optarg);
114                         if (trpoints < 0) {
115                                 warnx("unknown facility in %s", optarg);
116                                 usage();
117                         }
118                         break;
119                 default:
120                         usage();
121                 }
122
123         argv += optind;
124         argc -= optind;
125
126         /* must have either -[Cc], a pid or a command */
127         if (clear == NOTSET && pid == 0 && argc == 0)
128                 usage();
129         /* can't have both a pid and a command */
130         /* (note that -C sets pid to 1) */
131         if (pid != 0 && argc > 0) {
132                 usage();
133         }
134
135         if (inherit)
136                 trpoints |= KTRFAC_INHERIT;
137
138         (void)signal(SIGSYS, no_ktrace);
139         if (clear != NOTSET) {
140                 if (clear == CLEARALL) {
141                         ops = KTROP_CLEAR | KTRFLAG_DESCEND;
142                         trpoints = ALL_POINTS;
143                 } else {
144                         ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
145                 }
146                 if (ktrace(tracefile, ops, trpoints, pid) < 0)
147                         err(1, "%s", tracefile);
148                 exit(0);
149         }
150
151         omask = umask(S_IRWXG|S_IRWXO);
152         if (append) {
153                 if ((fd = open(tracefile, O_CREAT | O_WRONLY | O_NONBLOCK,
154                     DEFFILEMODE)) < 0)
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",
158                             tracefile);
159                 if (!(S_ISREG(sb.st_mode)))
160                         errx(1, "%s not regular file", tracefile);
161         } else {
162                 if (unlink(tracefile) == -1 && errno != ENOENT)
163                         err(1, "unlink %s", tracefile);
164                 if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
165                     DEFFILEMODE)) < 0)
166                         err(1, "%s", tracefile);
167         }
168         (void)umask(omask);
169         (void)close(fd);
170
171         trpoints |= PROC_ABI_POINTS;
172
173         if (argc > 0) { 
174                 if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
175                         err(1, "%s", tracefile);
176                 execvp(*argv, argv);
177                 err(1, "exec of '%s' failed", *argv);
178         }
179         if (ktrace(tracefile, ops, trpoints, pid) < 0)
180                 err(1, "%s", tracefile);
181         exit(0);
182 }
183
184 static void
185 set_pid_clear(const char *p, enum clear cl)
186 {
187         intmax_t n;
188         char *e;
189
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");
193                 usage();
194         }
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");
198                 usage();
199         }
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");
203                 usage();
204         }
205         if (p != NULL) {
206                 errno = 0;
207                 n = strtoimax(p, &e, 10);
208                 /*
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.
213                  */
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");
217                         usage();
218                 }
219                 pid = n;
220         }
221         if (cl != NOTSET)
222                 if ((clear = cl) == CLEARALL)
223                         pid = 1;
224 }
225
226 static void
227 usage(void)
228 {
229
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");
233         exit(1);
234 }
235
236 static void
237 no_ktrace(int sig __unused)
238 {
239
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'");
243         exit(1);
244 }