Commit | Line | Data |
---|---|---|
6c82192c | 1 | /*- |
ad787160 C |
2 | * Copyright (c) 1988, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
37f7ecbb | 4 | * |
ad787160 C |
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. | |
37f7ecbb MT |
32 | */ |
33 | ||
34 | #ifndef lint | |
ad787160 C |
35 | static char copyright[] = |
36 | "@(#) Copyright (c) 1988, 1993\n\ | |
37 | The Regents of the University of California. All rights reserved.\n"; | |
37f7ecbb MT |
38 | #endif /* not lint */ |
39 | ||
40 | #ifndef lint | |
ad787160 | 41 | static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93"; |
37f7ecbb MT |
42 | #endif /* not lint */ |
43 | ||
371824ae KB |
44 | #include <sys/param.h> |
45 | #include <sys/stat.h> | |
46 | #include <sys/file.h> | |
47 | #include <sys/time.h> | |
48 | #include <sys/errno.h> | |
49 | #include <sys/uio.h> | |
50 | #include <sys/ktrace.h> | |
51 | #include <stdio.h> | |
f4d5acdc | 52 | #include "ktrace.h" |
37f7ecbb MT |
53 | |
54 | main(argc, argv) | |
371824ae KB |
55 | int argc; |
56 | char **argv; | |
37f7ecbb MT |
57 | { |
58 | extern int optind; | |
59 | extern char *optarg; | |
371824ae KB |
60 | enum { NOTSET, CLEAR, CLEARALL } clear; |
61 | int append, ch, fd, inherit, ops, pid, pidset, trpoints; | |
62 | char *tracefile; | |
37f7ecbb | 63 | |
371824ae | 64 | clear = NOTSET; |
de0c12b9 | 65 | append = ops = pidset = inherit = 0; |
60c2c1a3 | 66 | trpoints = DEF_POINTS; |
371824ae KB |
67 | tracefile = DEF_TRACEFILE; |
68 | while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != EOF) | |
37f7ecbb | 69 | switch((char)ch) { |
371824ae KB |
70 | case 'a': |
71 | append = 1; | |
72 | break; | |
513e9867 | 73 | case 'C': |
371824ae | 74 | clear = CLEARALL; |
7f989705 | 75 | pidset = 1; |
513e9867 MT |
76 | break; |
77 | case 'c': | |
371824ae | 78 | clear = CLEAR; |
513e9867 MT |
79 | break; |
80 | case 'd': | |
81 | ops |= KTRFLAG_DESCEND; | |
82 | break; | |
371824ae KB |
83 | case 'f': |
84 | tracefile = optarg; | |
513e9867 MT |
85 | break; |
86 | case 'g': | |
371824ae KB |
87 | pid = -rpid(optarg); |
88 | pidset = 1; | |
513e9867 MT |
89 | break; |
90 | case 'i': | |
371824ae | 91 | inherit = 1; |
513e9867 | 92 | break; |
371824ae KB |
93 | case 'p': |
94 | pid = rpid(optarg); | |
95 | pidset = 1; | |
513e9867 | 96 | break; |
371824ae KB |
97 | case 't': |
98 | trpoints = getpoints(optarg); | |
99 | if (trpoints < 0) { | |
100 | (void)fprintf(stderr, | |
101 | "ktrace: unknown facility in %s\n", optarg); | |
102 | usage(); | |
103 | } | |
513e9867 MT |
104 | break; |
105 | default: | |
371824ae | 106 | usage(); |
37f7ecbb | 107 | } |
371824ae KB |
108 | argv += optind; |
109 | argc -= optind; | |
37f7ecbb | 110 | |
371824ae KB |
111 | if (pidset && *argv || !pidset && !*argv) |
112 | usage(); | |
113 | ||
513e9867 | 114 | if (inherit) |
6c82192c | 115 | trpoints |= KTRFAC_INHERIT; |
371824ae KB |
116 | |
117 | if (clear != NOTSET) { | |
118 | if (clear == CLEARALL) { | |
513e9867 | 119 | ops = KTROP_CLEAR | KTRFLAG_DESCEND; |
60c2c1a3 | 120 | trpoints = ALL_POINTS; |
513e9867 | 121 | pid = 1; |
371824ae | 122 | } else |
513e9867 | 123 | ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE; |
371824ae KB |
124 | |
125 | if (ktrace(tracefile, ops, trpoints, pid) < 0) | |
126 | error(tracefile); | |
37f7ecbb MT |
127 | exit(0); |
128 | } | |
f4d5acdc | 129 | |
371824ae KB |
130 | if ((fd = open(tracefile, O_CREAT | O_WRONLY | (append ? 0 : O_TRUNC), |
131 | DEFFILEMODE)) < 0) | |
132 | error(tracefile); | |
133 | (void)close(fd); | |
134 | ||
135 | if (*argv) { | |
136 | if (ktrace(tracefile, ops, trpoints, getpid()) < 0) | |
137 | error(); | |
37f7ecbb | 138 | execvp(argv[0], &argv[0]); |
371824ae KB |
139 | error(argv[0]); |
140 | exit(1); | |
37f7ecbb | 141 | } |
371824ae KB |
142 | else if (ktrace(tracefile, ops, trpoints, pid) < 0) |
143 | error(tracefile); | |
37f7ecbb MT |
144 | exit(0); |
145 | } | |
371824ae KB |
146 | |
147 | rpid(p) | |
148 | char *p; | |
149 | { | |
150 | static int first; | |
151 | ||
152 | if (first++) { | |
153 | (void)fprintf(stderr, | |
154 | "ktrace: only one -g or -p flag is permitted.\n"); | |
155 | usage(); | |
156 | } | |
157 | if (!*p) { | |
158 | (void)fprintf(stderr, "ktrace: illegal process id.\n"); | |
159 | usage(); | |
160 | } | |
161 | return(atoi(p)); | |
162 | } | |
163 | ||
164 | error(name) | |
165 | char *name; | |
166 | { | |
167 | (void)fprintf(stderr, "ktrace: %s: %s.\n", name, strerror(errno)); | |
168 | exit(1); | |
169 | } | |
170 | ||
171 | usage() | |
172 | { | |
173 | (void)fprintf(stderr, | |
174 | "usage:\tktrace [-aCcid] [-f trfile] [-g pgid] [-p pid] [-t [acgn]\n\tktrace [-aCcid] [-f trfile] [-t [acgn] command\n"); | |
175 | exit(1); | |
176 | } |