xref: /openbsd-src/usr.bin/ktrace/ktrace.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: ktrace.c,v 1.27 2013/11/06 17:26:55 sobrado 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 
50 extern char *__progname;
51 
52 static int rpid(const char *);
53 static void no_ktrace(int);
54 static void usage(void);
55 
56 int	is_ltrace;
57 
58 int
59 main(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:iu:")) != -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 'u':
89 				tracespec = optarg;
90 				break;
91 			default:
92 				usage();
93 			}
94 	} else {
95 		while ((ch = getopt(argc, argv, "aBCcdf:g:ip:t:")) != -1)
96 			switch ((char)ch) {
97 			case 'a':
98 				append = 1;
99 				break;
100 			case 'B':
101 				putenv("LD_BIND_NOW=");
102 				break;
103 			case 'C':
104 				clear = CLEARALL;
105 				pidset = 1;
106 				break;
107 			case 'c':
108 				clear = CLEAR;
109 				break;
110 			case 'd':
111 				ops |= KTRFLAG_DESCEND;
112 				break;
113 			case 'f':
114 				tracefile = optarg;
115 				break;
116 			case 'g':
117 				pid = -rpid(optarg);
118 				pidset = 1;
119 				break;
120 			case 'i':
121 				inherit = 1;
122 				break;
123 			case 'p':
124 				pid = rpid(optarg);
125 				pidset = 1;
126 				break;
127 			case 't':
128 				trpoints = getpoints(optarg);
129 				if (trpoints < 0) {
130 					warnx("unknown facility in %s", optarg);
131 					usage();
132 				}
133 				break;
134 			default:
135 				usage();
136 			}
137 	}
138 
139 	argv += optind;
140 	argc -= optind;
141 
142 	if ((pidset && *argv) || (!pidset && !*argv && clear != CLEAR))
143 		usage();
144 
145 	if (inherit)
146 		trpoints |= KTRFAC_INHERIT;
147 
148 	(void)signal(SIGSYS, no_ktrace);
149 	if (clear != NOTSET) {
150 		if (clear == CLEARALL) {
151 			ops = KTROP_CLEAR | KTRFLAG_DESCEND;
152 			trpoints = ALL_POINTS;
153 			pid = 1;
154 		} else
155 			ops |= pid ? KTROP_CLEAR : KTROP_CLEARFILE;
156 
157 		if (ktrace(tracefile, ops, trpoints, pid) < 0)
158 			err(1, "%s", tracefile);
159 		exit(0);
160 	}
161 
162 	omask = umask(S_IRWXG|S_IRWXO);
163 	if (append) {
164 		if ((fd = open(tracefile, O_CREAT | O_WRONLY, DEFFILEMODE)) < 0)
165 			err(1, "%s", tracefile);
166 		if (fstat(fd, &sb) != 0 || sb.st_uid != getuid())
167 			errx(1, "Refuse to append to %s: not owned by you.",
168 			    tracefile);
169 	} else {
170 		if (unlink(tracefile) == -1 && errno != ENOENT)
171 			err(1, "unlink %s", tracefile);
172 		if ((fd = open(tracefile, O_CREAT | O_EXCL | O_WRONLY,
173 		    DEFFILEMODE)) < 0)
174 			err(1, "%s", tracefile);
175 	}
176 	(void)umask(omask);
177 	(void)close(fd);
178 
179 	if (*argv) {
180 		if (is_ltrace) {
181 			if (setenv("LD_TRACE_PLT", inherit ? "i" : "", 1) < 0)
182 				err(1, "setenv(LD_TRACE_PLT)");
183 			if (tracespec &&
184 			    setenv("LD_TRACE_PLTSPEC", tracespec, 1) < 0)
185 				err(1, "setenv(LD_TRACE_PLTSPEC)");
186 		}
187 		if (ktrace(tracefile, ops, trpoints, getpid()) < 0)
188 			err(1, "%s", tracefile);
189 		execvp(argv[0], &argv[0]);
190 		err(1, "exec of '%s' failed", argv[0]);
191 	}
192 	else if (ktrace(tracefile, ops, trpoints, pid) < 0)
193 		err(1, "%s", tracefile);
194 	exit(0);
195 }
196 
197 static int
198 rpid(const char *p)
199 {
200 	static int first;
201 
202 	if (first++) {
203 		warnx("only one -g or -p flag is permitted.");
204 		usage();
205 	}
206 	if (!*p) {
207 		warnx("illegal process id.");
208 		usage();
209 	}
210 	return(atoi(p));
211 }
212 
213 static void
214 usage(void)
215 {
216 	if (is_ltrace)
217 		fprintf(stderr, "usage: %s [-ai] [-f trfile] [-u trspec]"
218 		    " command\n",
219 		    __progname);
220 	else
221 		fprintf(stderr, "usage: %s [-aBCcdi] [-f trfile] [-g pgid]"
222 		    " [-p pid] [-t trstr]\n"
223 		    "       %s [-adi] [-f trfile] [-t trstr] command\n",
224 		    __progname, __progname);
225 	exit(1);
226 }
227 
228 /* ARGSUSED */
229 static void
230 no_ktrace(int signo)
231 {
232 	char buf[8192];
233 
234 	snprintf(buf, sizeof(buf),
235 "error:\tktrace() system call not supported in the running kernel\n\tre-compile kernel with 'option KTRACE'\n");
236 	write(STDERR_FILENO, buf, strlen(buf));
237 	_exit(1);
238 }
239