ktrace.c revision 1.34 1 /* $NetBSD: ktrace.c,v 1.34 2004/02/28 02:42:45 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.34 2004/02/28 02:42:45 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(EXIT_FAILURE, "%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(EXIT_FAILURE, "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(EXIT_FAILURE, "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 #define TRPOINTS "[Aaceilmnsuvw+-]"
269 #ifdef KTRUSS
270 (void)fprintf(stderr, "usage:\t%s "
271 "[-aCcdilRT] [-e emulation] [-f infile] [-g pgid] "
272 "[-m maxdata]\n\t "
273 "[-o outfile] [-p pid] [-t " TRPOINTS "]\n", getprogname());
274 (void)fprintf(stderr, "\t%s "
275 "[-adiRT] [-e emulation] [-m maxdata] [-o outfile]\n\t "
276 "[-t " TRPOINTS "] command\n",
277 getprogname());
278 #else
279 (void)fprintf(stderr, "usage:\t%s "
280 "[-aCcdis] [-f trfile] [-g pgid] [-p pid] [-t " TRPOINTS "]\n",
281 getprogname());
282 (void)fprintf(stderr, "\t%s "
283 "[-adis] [-f trfile] [-t " TRPOINTS "] command\n",
284 getprogname());
285 #endif
286 exit(1);
287 }
288
289 static const char *ktracefile = NULL;
290 void
291 no_ktrace(sig)
292 int sig;
293 {
294
295 if (ktracefile)
296 (void)unlink(ktracefile);
297 (void)errx(EXIT_FAILURE,
298 "ktrace(2) system call not supported in the running"
299 " kernel; re-compile kernel with `options KTRACE'");
300 }
301
302 int
303 do_ktrace(tracefile, ops, trpoints, pid)
304 const char *tracefile;
305 int ops;
306 int trpoints;
307 int pid;
308 {
309 int ret;
310
311 if (KTROP(ops) == KTROP_SET &&
312 (!tracefile || strcmp(tracefile, "-") == 0)) {
313 int pi[2], dofork, fpid;
314
315 if (pipe(pi) < 0)
316 err(EXIT_FAILURE, "pipe(2)");
317 fcntl(pi[0], F_SETFD, FD_CLOEXEC | fcntl(pi[0], F_GETFD, 0));
318 fcntl(pi[1], F_SETFD, FD_CLOEXEC | fcntl(pi[1], F_GETFD, 0));
319
320 dofork = (pid == getpid());
321
322 #ifdef KTRUSS
323 if (dofork)
324 fpid = fork();
325 else
326 fpid = pid; /* XXX: Gcc */
327 #else
328 if (dofork)
329 fpid = fork();
330 else
331 fpid = 0; /* XXX: Gcc */
332 #endif
333 #ifdef KTRUSS
334 if (fpid)
335 #else
336 if (!dofork || !fpid)
337 #endif
338 {
339 if (!dofork)
340 #ifdef KTRUSS
341 ret = fktrace(pi[1], ops, trpoints, fpid);
342 #else
343 ret = fktrace(pi[1], ops, trpoints, pid);
344 #endif
345 else
346 close(pi[1]);
347 #ifdef KTRUSS
348 dumpfile(NULL, pi[0], trpoints);
349 waitpid(fpid, NULL, 0);
350 #else
351 {
352 char buf[512];
353 int n, cnt = 0;
354
355 while ((n =
356 read(pi[0], buf, sizeof(buf))) > 0) {
357 write(1, buf, n);
358 cnt += n;
359 }
360 }
361 if (dofork)
362 _exit(0);
363 #endif
364 return 0;
365 }
366 close(pi[0]);
367 #ifdef KTRUSS
368 if (dofork && !fpid) {
369 ret = fktrace(pi[1], ops, trpoints, getpid());
370 return 1;
371 }
372 #else
373 ret = fktrace(pi[1], ops, trpoints, pid);
374 #endif
375 if (ret == -1)
376 err(EXIT_FAILURE, "fd %d, pid %d", pi[1], pid);
377 } else {
378 ret = ktrace(ktracefile = tracefile, ops, trpoints, pid);
379 if (ret == -1)
380 err(EXIT_FAILURE, "file %s, pid %d",
381 tracefile != NULL ? tracefile : "NULL", pid);
382 }
383 return 1;
384 }
385