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