ktrace.c revision 1.30
1/*	$OpenBSD: ktrace.c,v 1.30 2014/05/24 17:04:16 deraadt Exp $	*/
2/*	$NetBSD: ktrace.c,v 1.4 1995/08/31 23:01:44 jtc Exp $	*/
3
4/*-
5 * Copyright (c) 1988, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/stat.h>
35#include <sys/time.h>
36#include <sys/uio.h>
37#include <sys/ktrace.h>
38
39#include <err.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <stdlib.h>
43#include <stdio.h>
44#include <string.h>
45#include <unistd.h>
46
47#include "ktrace.h"
48#include "extern.h"
49
50extern char *__progname;
51
52static int rpid(const char *);
53static void no_ktrace(int);
54static void usage(void);
55
56int	is_ltrace;
57
58int
59main(int argc, char *argv[])
60{
61	enum { NOTSET, CLEAR, CLEARALL } clear;
62	int append, ch, fd, inherit, ops, pidset, trpoints;
63	pid_t pid;
64	char *tracefile, *tracespec;
65	mode_t omask;
66	struct stat sb;
67
68	is_ltrace = strcmp(__progname, "ltrace") == 0;
69
70	clear = NOTSET;
71	append = ops = pidset = inherit = pid = 0;
72	trpoints = is_ltrace ? KTRFAC_USER : DEF_POINTS;
73	tracefile = DEF_TRACEFILE;
74	tracespec = NULL;
75
76	if (is_ltrace) {
77		while ((ch = getopt(argc, argv, "af:it:u:")) != -1)
78			switch ((char)ch) {
79			case 'a':
80				append = 1;
81				break;
82			case 'f':
83				tracefile = optarg;
84				break;
85			case 'i':
86				inherit = 1;
87				break;
88			case 't':
89				trpoints = getpoints(optarg);
90				if (trpoints < 0) {
91					warnx("unknown facility in %s", optarg);
92					usage();
93				}
94				break;
95			case 'u':
96				tracespec = optarg;
97				break;
98			default:
99				usage();
100			}
101	} else {
102		while ((ch = getopt(argc, argv, "aBCcdf:g:ip:t:")) != -1)
103			switch ((char)ch) {
104			case 'a':
105				append = 1;
106				break;
107			case 'B':
108				putenv("LD_BIND_NOW=");
109				break;
110			case 'C':
111				clear = CLEARALL;
112				pidset = 1;
113				break;
114			case 'c':
115				clear = CLEAR;
116				break;
117			case 'd':
118				ops |= KTRFLAG_DESCEND;
119				break;
120			case 'f':
121				tracefile = optarg;
122				break;
123			case 'g':
124				pid = -rpid(optarg);
125				pidset = 1;
126				break;
127			case 'i':
128				inherit = 1;
129				break;
130			case 'p':
131				pid = rpid(optarg);
132				pidset = 1;
133				break;
134			case 't':
135				trpoints = getpoints(optarg);
136				if (trpoints < 0) {
137					warnx("unknown facility in %s", optarg);
138					usage();
139				}
140				break;
141			default:
142				usage();
143			}
144	}
145
146	argv += optind;
147	argc -= optind;
148
149	if ((pidset && *argv) || (!pidset && !*argv && clear != CLEAR))
150		usage();
151
152	if (inherit)
153		trpoints |= KTRFAC_INHERIT;
154
155	(void)signal(SIGSYS, no_ktrace);
156	if (clear != NOTSET) {
157		if (clear == CLEARALL) {
158			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
159			trpoints = ALL_POINTS;
160			pid = 1;
161		} else
162			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
163
164		if (ktrace(tracefile, ops, trpoints, pid) < 0) {
165			if (errno == ESRCH)
166				err(1, "%d", pid);
167			err(1, "%s", tracefile);
168		}
169		exit(0);
170	}
171
172	omask = umask(S_IRWXG|S_IRWXO);
173	if (append) {
174		if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) < 0)
175			err(1, "%s", tracefile);
176		if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
177			errx(1, "Refuse to append to %s: not owned by you.",
178			    tracefile);
179	} else {
180		if (unlink(tracefile) == -1 && errno != ENOENT)
181			err(1, "unlink %s", tracefile);
182		if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
183		    DEFFILEMODE)) < 0)
184			err(1, "%s", tracefile);
185	}
186	(void)umask(omask);
187	(void)close(fd);
188
189	if (*argv) {
190		if (is_ltrace) {
191			if (setenv("LD_TRACE_PLT", inherit ? "i" : "", 1) < 0)
192				err(1, "setenv(LD_TRACE_PLT)");
193			if (tracespec &&
194			    setenv("LD_TRACE_PLTSPEC", tracespec, 1) < 0)
195				err(1, "setenv(LD_TRACE_PLTSPEC)");
196		}
197		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
198			err(1, "%s", tracefile);
199		execvp(argv[0], &argv[0]);
200		err(1, "exec of '%s' failed", argv[0]);
201	}
202	else if (ktrace(tracefile, ops, trpoints, pid) < 0) {
203		if (errno == ESRCH)
204			err(1, "%d", pid);
205		err(1, "%s", tracefile);
206	}
207	exit(0);
208}
209
210static int
211rpid(const char *p)
212{
213	static int first;
214
215	if (first++) {
216		warnx("only one -g or -p flag is permitted.");
217		usage();
218	}
219	if (!*p) {
220		warnx("illegal process id.");
221		usage();
222	}
223	return(atoi(p));
224}
225
226static void
227usage(void)
228{
229	if (is_ltrace)
230		fprintf(stderr, "usage: %s [-ai] [-f trfile] [-t trstr]"
231		    " [-u trspec] command\n",
232		    __progname);
233	else
234		fprintf(stderr, "usage: %s [-aBCcdi] [-f trfile] [-g pgid]"
235		    " [-p pid] [-t trstr]\n"
236		    "       %s [-adi] [-f trfile] [-t trstr] command\n",
237		    __progname, __progname);
238	exit(1);
239}
240
241/* ARGSUSED */
242static void
243no_ktrace(int signo)
244{
245	char buf[8192];
246
247	snprintf(buf, sizeof(buf),
248"error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'option KTRACE'\n");
249	write(STDERR_FILENO, buf, strlen(buf));
250	_exit(1);
251}
252