ktrace.c revision 1.15 1 /* $NetBSD: ktrace.c,v 1.15 2000/04/10 09:32:03 jdolecek Exp $ */
2
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)ktrace.c 8.2 (Berkeley) 4/28/95";
45 #else
46 __RCSID("$NetBSD: ktrace.c,v 1.15 2000/04/10 09:32:03 jdolecek Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/wait.h>
53 #include <sys/file.h>
54 #include <sys/time.h>
55 #include <sys/uio.h>
56 #include <sys/ktrace.h>
57
58 #include <err.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62
63 #include "ktrace.h"
64
65 #ifdef KTRUSS
66 #include <string.h>
67 #include "setemul.h"
68 #endif
69
70 int main __P((int, char **));
71 int rpid __P((char *));
72 void usage __P((void));
73 int do_ktrace __P((char *, int, int,int));
74 void no_ktrace __P((int));
75
76 #ifdef KTRUSS
77 extern int timestamp, decimal, fancy, tail, maxdata;
78 #endif
79
80 int
81 main(argc, argv)
82 int argc;
83 char **argv;
84 {
85 enum { NOTSET, CLEAR, CLEARALL } clear;
86 int append, ch, fd, inherit, ops, pid, pidset, trpoints;
87 char *infile, *outfile;
88 #ifdef KTRUSS
89 const char *emul_name = "netbsd";
90 #endif
91
92 clear = NOTSET;
93 append = ops = pidset = inherit = 0;
94 trpoints = DEF_POINTS;
95 pid = 0; /* Appease GCC */
96
97 #ifdef KTRUSS
98 # define OPTIONS "aCce:df:g:ilm:o:p:RTt:"
99 outfile = infile = NULL;
100 #else
101 # define OPTIONS "aCcdf:g:ip:t:"
102 outfile = infile = DEF_TRACEFILE;
103 #endif
104
105 while ((ch = getopt(argc,argv, OPTIONS)) != -1)
106 switch((char)ch) {
107 case 'a':
108 append = 1;
109 break;
110 case 'C':
111 clear = CLEARALL;
112 pidset = 1;
113 break;
114 case 'c':
115 clear = CLEAR;
116 pidset = 1;
117 break;
118 case 'd':
119 ops |= KTRFLAG_DESCEND;
120 break;
121 #ifdef KTRUSS
122 case 'e':
123 emul_name = strdup(optarg); /* it's safer to copy it */
124 break;
125 case 'f':
126 infile = optarg;
127 break;
128 #else
129 case 'f':
130 infile = outfile = optarg;
131 break;
132 #endif
133 case 'g':
134 pid = -rpid(optarg);
135 pidset = 1;
136 break;
137 case 'i':
138 inherit = 1;
139 break;
140 #ifdef KTRUSS
141 case 'l':
142 tail = 1;
143 break;
144 case 'm':
145 maxdata = atoi(optarg);
146 break;
147 case 'o':
148 outfile = optarg;
149 break;
150 #endif
151 case 'p':
152 pid = rpid(optarg);
153 pidset = 1;
154 break;
155 #ifdef KTRUSS
156 case 'R':
157 timestamp = 2; /* relative timestamp */
158 break;
159 case 'T':
160 timestamp = 1;
161 break;
162 #endif
163 case 't':
164 trpoints = getpoints(optarg);
165 if (trpoints < 0) {
166 warnx("unknown facility in %s", optarg);
167 usage();
168 }
169 break;
170 default:
171 usage();
172 }
173 argv += optind;
174 argc -= optind;
175
176 if (!infile && ((pidset && *argv) || (!pidset && !*argv)))
177 usage();
178
179 #ifdef KTRUSS
180 if (infile) {
181 dumpfile(infile, 0, trpoints);
182 exit(0);
183 }
184
185 setemul(emul_name, 0, 0);
186 #endif
187
188 if (inherit)
189 trpoints |= KTRFAC_INHERIT;
190
191 (void)signal(SIGSYS, no_ktrace);
192 if (clear != NOTSET) {
193 if (clear == CLEARALL) {
194 ops = KTROP_CLEAR | KTRFLAG_DESCEND;
195 trpoints = ALL_POINTS;
196 pid = 1;
197 } else
198 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
199
200 (void)do_ktrace(outfile, ops, trpoints, pid);
201 exit(0);
202 }
203
204 if (outfile && strcmp(outfile, "-")) {
205 if ((fd = open(outfile, O_CREAT | O_WRONLY |
206 (append ? 0 : O_TRUNC), DEFFILEMODE)) < 0)
207 err(1, outfile);
208 (void)close(fd);
209 }
210
211 if (*argv) {
212 #ifdef KTRUSS
213 if (do_ktrace(outfile, ops, trpoints, getpid()) == 1) {
214 execvp(argv[0], &argv[0]);
215 err(1, "exec of '%s' failed", argv[0]);
216 }
217 #else
218 (void) do_ktrace(outfile, ops, trpoints, getpid());
219 execvp(argv[0], &argv[0]);
220 err(1, "exec of '%s' failed", argv[0]);
221 #endif
222 } else
223 (void)do_ktrace(outfile, ops, trpoints, pid);
224 exit(0);
225 }
226
227 int
228 rpid(p)
229 char *p;
230 {
231 static int first;
232
233 if (first++) {
234 warnx("only one -g or -p flag is permitted.");
235 usage();
236 }
237 if (!*p) {
238 warnx("illegal process id.");
239 usage();
240 }
241 return(atoi(p));
242 }
243
244 void
245 usage()
246 {
247 extern char *__progname;
248 #ifdef KTRUSS
249 # define LONG_OPTION "[-e emulation] [-m maxdata] [-o outfile] "
250 # define SHRT_OPTION "RT"
251 #else
252 # define LONG_OPTION ""
253 # define SHRT_OPTION ""
254 #endif
255 (void)fprintf(stderr,
256 "Usage:\t%s [-aCcid%s] %s[-f trfile] [-g pgid] [-p pid] [-t [cenisw+]]\n\t%s [-aCcid%s] %s[-f trfile] [-t [cenisw+]] command\n",
257 __progname, SHRT_OPTION, LONG_OPTION,
258 __progname, SHRT_OPTION, LONG_OPTION);
259 exit(1);
260 }
261
262 void
263 no_ktrace(sig)
264 int sig;
265 {
266 (void)fprintf(stderr,
267 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
268 exit(1);
269 }
270
271 int
272 do_ktrace(tracefile, ops, trpoints, pid)
273 char *tracefile;
274 int ops;
275 int trpoints;
276 int pid;
277 {
278 int ret;
279
280 if (!tracefile || strcmp(tracefile, "-") == 0) {
281 int pi[2], dofork, fpid;
282
283 if (pipe(pi) < 0)
284 err(1, "pipe(2)");
285 fcntl(pi[0], F_SETFD, FD_CLOEXEC|fcntl(pi[0], F_GETFD, 0));
286 fcntl(pi[1], F_SETFD, FD_CLOEXEC|fcntl(pi[1], F_GETFD, 0));
287
288 dofork = (pid == getpid());
289
290 #ifdef KTRUSS
291 if (dofork)
292 fpid = vfork();
293 else
294 fpid = pid; /* XXX: Gcc */
295 #else
296 if (dofork)
297 fpid = fork();
298 else
299 fpid = 0; /* XXX: Gcc */
300 #endif
301 #ifdef KTRUSS
302 if (fpid)
303 #else
304 if (!dofork || !fpid)
305 #endif
306 {
307 if (!dofork)
308 #ifdef KTRUSS
309 ret = fktrace(pi[1], ops, trpoints, fpid);
310 #else
311 ret = fktrace(pi[1], ops, trpoints, pid);
312 #endif
313 else
314 close(pi[1]);
315 #ifdef KTRUSS
316 dumpfile(NULL, pi[0], trpoints);
317 waitpid(fpid, NULL, 0);
318 #else
319 {
320 char buf[512];
321 int n, cnt = 0;
322
323 while ((n = read(pi[0], buf, sizeof(buf)))>0) {
324 write(1, buf, n);
325 cnt += n;
326 }
327 }
328 if (dofork)
329 _exit(0);
330 #endif
331 return 0;
332 }
333 close(pi[0]);
334 #ifdef KTRUSS
335 if (dofork && !fpid) {
336 ret = fktrace(pi[1], ops, trpoints, getpid());
337 return 1;
338 }
339 #else
340 ret = fktrace(pi[1], ops, trpoints, pid);
341 #endif
342 } else
343 ret = ktrace(tracefile, ops, trpoints, pid);
344 if (ret < 0)
345 err(1, tracefile);
346 return 1;
347 }
348