]> git.saurik.com Git - apple/system_cmds.git/blame - ktrace.tproj/ktrace.c
system_cmds-336.6.tar.gz
[apple/system_cmds.git] / ktrace.tproj / ktrace.c
CommitLineData
1815bff5 1/*
b51d5b5f 2 * Copyright (c) 1999, 2000-2001 Apple Computer, Inc. All rights reserved.
1815bff5
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
2fc1e207
A
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
1815bff5
A
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2fc1e207
A
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
1815bff5
A
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*-
25 * Copyright (c) 1988, 1993
b51d5b5f 26 * The Regents of the University of California. All rights reserved.
1815bff5
A
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
b51d5b5f
A
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
1815bff5
A
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
2fc1e207
A
57#include <sys/cdefs.h>
58
b51d5b5f 59#ifndef lint
2fc1e207 60__unused static char copyright[] =
b51d5b5f
A
61"@(#) Copyright (c) 1988, 1993\n\
62 The Regents of the University of California. All rights reserved.\n";
63#endif /* not lint */
64
65#ifndef lint
66#if 0
2fc1e207 67__unused static char sccsid[] = "@(#)ktrace.c 8.1 (Berkeley) 6/6/93";
b51d5b5f 68#endif
2fc1e207 69__unused static const char rcsid[] =
b51d5b5f
A
70 "$FreeBSD: src/usr.bin/ktrace/ktrace.c,v 1.12.2.3 2001/07/11 00:29:27 mikeh Exp $";
71#endif /* not lint */
72
1815bff5
A
73#include <sys/param.h>
74#include <sys/stat.h>
75#include <sys/file.h>
76#include <sys/time.h>
77#include <sys/errno.h>
78#include <sys/uio.h>
79#include <sys/ktrace.h>
80
81#include <err.h>
82#include <stdio.h>
2fc1e207 83#include <stdlib.h>
1815bff5
A
84#include <unistd.h>
85
86#include "ktrace.h"
87
88void no_ktrace __P((int));
89void usage __P((void));
2fc1e207 90int rpid(char *p);
1815bff5 91
2fc1e207
A
92int
93main(int argc, char *argv[])
1815bff5
A
94{
95 enum { NOTSET, CLEAR, CLEARALL } clear;
2fc1e207
A
96 int append;
97 int ch;
98 int fd;
99 int inherit;
100 int ops;
101 int pid = 1; /* protected by pidset */
102 int pidset;
103 int trpoints;
1815bff5 104 char *tracefile;
b51d5b5f
A
105 mode_t omask;
106 struct stat sb;
1815bff5
A
107
108 clear = NOTSET;
109 append = ops = pidset = inherit = 0;
110 trpoints = DEF_POINTS;
111 tracefile = DEF_TRACEFILE;
b51d5b5f 112 while ((ch = getopt(argc,argv,"aCcdf:g:ip:t:")) != -1)
1815bff5
A
113 switch((char)ch) {
114 case 'a':
115 append = 1;
116 break;
117 case 'C':
118 clear = CLEARALL;
119 pidset = 1;
120 break;
121 case 'c':
122 clear = CLEAR;
123 break;
124 case 'd':
125 ops |= KTRFLAG_DESCEND;
126 break;
127 case 'f':
128 tracefile = optarg;
129 break;
130 case 'g':
131 pid = -rpid(optarg);
132 pidset = 1;
133 break;
134 case 'i':
135 inherit = 1;
136 break;
137 case 'p':
138 pid = rpid(optarg);
139 pidset = 1;
140 break;
141 case 't':
142 trpoints = getpoints(optarg);
143 if (trpoints < 0) {
144 warnx("unknown facility in %s", optarg);
145 usage();
146 }
147 break;
148 default:
149 usage();
150 }
151 argv += optind;
152 argc -= optind;
153
154 if (pidset && *argv || !pidset && !*argv)
155 usage();
156
157 if (inherit)
158 trpoints |= KTRFAC_INHERIT;
159
160 (void)signal(SIGSYS, no_ktrace);
161 if (clear != NOTSET) {
162 if (clear == CLEARALL) {
163 ops = KTROP_CLEAR | KTRFLAG_DESCEND;
164 trpoints = ALL_POINTS;
165 pid = 1;
166 } else
b51d5b5f 167 ops |= pidset ? KTROP_CLEAR : KTROP_CLEARFILE;
1815bff5
A
168
169 if (ktrace(tracefile, ops, trpoints, pid) < 0)
b51d5b5f 170 err(1, "%s", tracefile);
1815bff5
A
171 exit(0);
172 }
173
b51d5b5f
A
174 omask = umask(S_IRWXG|S_IRWXO);
175 if (append) {
176 if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) < 0)
177 err(1, "%s", tracefile);
178 if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
179 errx(1, "Refuse to append to %s not owned by you.",
180 tracefile);
181 } else {
182 if (unlink(tracefile) == -1 && errno != ENOENT)
183 err(1, "unlink %s", tracefile);
184 if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
185 DEFFILEMODE)) < 0)
186 err(1, "%s", tracefile);
187 }
188 (void)umask(omask);
1815bff5
A
189 (void)close(fd);
190
191 if (*argv) {
192 if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
b51d5b5f 193 err(1, "%s", tracefile);
1815bff5
A
194 execvp(argv[0], &argv[0]);
195 err(1, "exec of '%s' failed", argv[0]);
196 }
197 else if (ktrace(tracefile, ops, trpoints, pid) < 0)
b51d5b5f 198 err(1, "%s", tracefile);
1815bff5
A
199 exit(0);
200}
201
2fc1e207
A
202int
203rpid(char *p)
1815bff5
A
204{
205 static int first;
206
207 if (first++) {
208 warnx("only one -g or -p flag is permitted.");
209 usage();
210 }
211 if (!*p) {
212 warnx("illegal process id.");
213 usage();
214 }
215 return(atoi(p));
216}
217
218void
2fc1e207 219usage(void)
1815bff5 220{
b51d5b5f
A
221 (void)fprintf(stderr, "%s\n%s\n",
222"usage: ktrace [-aCcdi] [-f trfile] [-g pgrp | -p pid] [-t cnisuw]",
223" ktrace [-adi] [-f trfile] [-t cnisuw] command");
1815bff5
A
224 exit(1);
225}
226
227void
2fc1e207 228no_ktrace(int sig)
1815bff5
A
229{
230 (void)fprintf(stderr,
231"error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'options KTRACE'\n");
232 exit(1);
233}