ktrace.c revision 1.32 1 /* $NetBSD: ktrace.c,v 1.32 2004/02/28 01:43:07 enami 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. 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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)ktrace.c 8.2 (Berkeley) 4/28/95";
41 #else
42 __RCSID("$NetBSD: ktrace.c,v 1.32 2004/02/28 01:43:07 enami Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/wait.h>
49 #include <sys/file.h>
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 #include <sys/ktrace.h>
53 #include <sys/socket.h>
54
55 #include <err.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include "ktrace.h"
62
63 #ifdef KTRUSS
64 #include <string.h>
65 #include "setemul.h"
66 #endif
67
68 int main __P((int, char **));
69 int rpid __P((char *));
70 void usage __P((void));
71 int do_ktrace __P((const char *, int, int,int));
72 void no_ktrace __P((int));
73
74 #ifdef KTRUSS
75 extern int timestamp, decimal, fancy, tail, maxdata;
76 #endif
77
78 int
79 main(argc, argv)
80 int argc;
81 char **argv;
82 {
83 enum { NOTSET, CLEAR, CLEARALL } clear;
84 int append, ch, fd, trset, ops, pid, pidset, synclog, trpoints;
85 const char *outfile;
86 #ifdef KTRUSS
87 const char *infile;
88 const char *emul_name = "netbsd";
89 #endif
90
91 clear = NOTSET;
92 append = ops = pidset = trset = synclog = 0;
93 trpoints = 0;
94 pid = 0; /* Appease GCC */
95
96 #ifdef KTRUSS
97 # define OPTIONS "aCce:df:g:ilm:o:p:RTt:"
98 outfile = infile = NULL;
99 #else
100 # define OPTIONS "aCcdf:g:ip:st:"
101 outfile = DEF_TRACEFILE;
102 #endif
103
104 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
105 switch (ch) {
106 case 'a':
107 append = 1;
108 break;
109 case 'C':
110 clear = CLEARALL;
111 pidset = 1;
112 break;
113 case 'c':
114 clear = CLEAR;
115 pidset = 1;
116 break;
117 case 'd':
118 ops |= KTRFLAG_DESCEND;
119 break;
120 #ifdef KTRUSS
121 case 'e':
122 emul_name = strdup(optarg); /* it's safer to copy it */
123 break;
124 case 'f':
125 infile = optarg;
126 break;
127 #else
128 case 'f':
129 outfile = optarg;
130 break;
131 #endif
132 case 'g':
133 pid = -rpid(optarg);
134 pidset = 1;
135 break;
136 case 'i':
137 trpoints |= KTRFAC_INHERIT;
138 break;
139 #ifdef KTRUSS
140 case 'l':
141 tail = 1;
142 break;
143 case 'm':
144 maxdata = atoi(optarg);
145 break;
146 case 'o':
147 outfile = optarg;
148 break;
149 #endif
150 case 'p':
151 pid = rpid(optarg);
152 pidset = 1;
153 break;
154 #ifdef KTRUSS
155 case 'R':
156 timestamp = 2; /* relative timestamp */
157 break;
158 #else
159 case 's':
160 synclog = 1;
161 break;
162 #endif
163 #ifdef KTRUSS
164 case 'T':
165 timestamp = 1;
166 break;
167 #endif
168 case 't':
169 trset = 1;
170 trpoints = getpoints(trpoints, optarg);
171 if (trpoints < 0) {
172 warnx("unknown facility in %s", optarg);
173 usage();
174 }
175 break;
176 default:
177 usage();
178 }
179 argv += optind;
180 argc -= optind;
181
182 if (!trset)
183 trpoints |= clear == NOTSET ? DEF_POINTS : ALL_POINTS;
184
185 if ((pidset && *argv) || (!pidset && !*argv)) {
186 #ifdef KTRUSS
187 if (!infile)
188 #endif
189 usage();
190 }
191
192 #ifdef KTRUSS
193 if (clear == CLEAR && outfile == NULL && pid == 0)
194 usage();
195
196 if (infile) {
197 dumpfile(infile, 0, trpoints);
198 exit(0);
199 }
200
201 setemul(emul_name, 0, 0);
202 #endif
203
204 /*
205 * For cleaner traces, initialize malloc now rather
206 * than in a traced subprocess.
207 */
208 free(malloc(1));
209
210 (void)signal(SIGSYS, no_ktrace);
211 if (clear != NOTSET) {
212 if (clear == CLEARALL) {
213 ops = KTROP_CLEAR | KTRFLAG_DESCEND;
214 trpoints = ALL_POINTS;
215 pid = 1;
216 } else
217 ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
218
219 (void)do_ktrace(outfile, ops, trpoints, pid);
220 exit(0);
221 }
222
223 if (outfile && strcmp(outfile, "-")) {
224 if ((fd = open(outfile, O_CREAT | O_WRONLY |
225 (append ? 0 : O_TRUNC) | (synclog ? 0 : O_SYNC),
226 DEFFILEMODE)) < 0)
227 err(1, "%s", outfile);
228 (void)close(fd);
229 }
230
231 if (*argv) {
232 #ifdef KTRUSS
233 if (do_ktrace(outfile, ops, trpoints, getpid()) == 1) {
234 execvp(argv[0], &argv[0]);
235 err(1, "exec of '%s' failed", argv[0]);
236 }
237 #else
238 (void)do_ktrace(outfile, ops, trpoints, getpid());
239 execvp(argv[0], &argv[0]);
240 err(1, "exec of '%s' failed", argv[0]);
241 #endif
242 } else
243 (void)do_ktrace(outfile, ops, trpoints, pid);
244 exit(0);
245 }
246
247 int
248 rpid(p)
249 char *p;
250 {
251 static int first;
252
253 if (first++) {
254 warnx("only one -g or -p flag is permitted.");
255 usage();
256 }
257 if (!*p) {
258 warnx("illegal process id.");
259 usage();
260 }
261 return (atoi(p));
262 }
263
264 void
265 usage()
266 {
267
268 #ifdef KTRUSS
269 # define LONG_OPTION "[-e emulation] [-m maxdata] [-o outfile] "
270 # define SHRT_OPTION "RT"
271 #else
272 # define LONG_OPTION ""
273 # define SHRT_OPTION ""
274 #endif
275 (void)fprintf(stderr,
276 "usage:\t%s [-aCcid%s] %s[-f trfile] [-g pgid] [-p pid] "
277 "[-t [Aaceilmnsuvw+]]\n\t%s [-aCcid%s] %s[-f trfile] [-t "
278 "[Aaceilmnsuvw+]] command\n",
279 getprogname(), SHRT_OPTION, LONG_OPTION,
280 getprogname(), SHRT_OPTION, LONG_OPTION);
281 exit(1);
282 }
283
284 static const char *ktracefile = NULL;
285 void
286 no_ktrace(sig)
287 int sig;
288 {
289
290 if (ktracefile)
291 (void)unlink(ktracefile);
292 (void)errx(1, "ktrace(2) system call not supported in the running"
293 " kernel; re-compile kernel with `options KTRACE'");
294 }
295
296 int
297 do_ktrace(tracefile, ops, trpoints, pid)
298 const char *tracefile;
299 int ops;
300 int trpoints;
301 int pid;
302 {
303 int ret;
304
305 if (KTROP(ops) == KTROP_SET &&
306 (!tracefile || strcmp(tracefile, "-") == 0)) {
307 int pi[2], dofork, fpid;
308
309 if (pipe(pi) < 0)
310 err(1, "pipe(2)");
311 fcntl(pi[0], F_SETFD, FD_CLOEXEC | fcntl(pi[0], F_GETFD, 0));
312 fcntl(pi[1], F_SETFD, FD_CLOEXEC | fcntl(pi[1], F_GETFD, 0));
313
314 dofork = (pid == getpid());
315
316 #ifdef KTRUSS
317 if (dofork)
318 fpid = fork();
319 else
320 fpid = pid; /* XXX: Gcc */
321 #else
322 if (dofork)
323 fpid = fork();
324 else
325 fpid = 0; /* XXX: Gcc */
326 #endif
327 #ifdef KTRUSS
328 if (fpid)
329 #else
330 if (!dofork || !fpid)
331 #endif
332 {
333 if (!dofork)
334 #ifdef KTRUSS
335 ret = fktrace(pi[1], ops, trpoints, fpid);
336 #else
337 ret = fktrace(pi[1], ops, trpoints, pid);
338 #endif
339 else
340 close(pi[1]);
341 #ifdef KTRUSS
342 dumpfile(NULL, pi[0], trpoints);
343 waitpid(fpid, NULL, 0);
344 #else
345 {
346 char buf[512];
347 int n, cnt = 0;
348
349 while ((n =
350 read(pi[0], buf, sizeof(buf))) > 0) {
351 write(1, buf, n);
352 cnt += n;
353 }
354 }
355 if (dofork)
356 _exit(0);
357 #endif
358 return 0;
359 }
360 close(pi[0]);
361 #ifdef KTRUSS
362 if (dofork && !fpid) {
363 ret = fktrace(pi[1], ops, trpoints, getpid());
364 return 1;
365 }
366 #else
367 ret = fktrace(pi[1], ops, trpoints, pid);
368 #endif
369 if (ret == -1)
370 err(EXIT_FAILURE, "fd %d, pid %d", pi[1], pid);
371 } else {
372 ret = ktrace(ktracefile = tracefile, ops, trpoints, pid);
373 if (ret == -1)
374 err(EXIT_FAILURE, "file %s, pid %d",
375 tracefile != NULL ? tracefile : "NULL", pid);
376 }
377 return 1;
378 }
379