xref: /minix/sys/sys/ktrace.h (revision 0a6a1f1d)
1 /*	$NetBSD: ktrace.h,v 1.61 2013/12/09 17:43:58 pooka 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ktrace.h	8.2 (Berkeley) 2/19/95
32  */
33 
34 #ifndef _SYS_KTRACE_H_
35 #define _SYS_KTRACE_H_
36 
37 #include <sys/mutex.h>
38 #include <sys/lwp.h>
39 
40 /*
41  * operations to ktrace system call  (KTROP(op))
42  */
43 #define KTROP_SET		0	/* set trace points */
44 #define KTROP_CLEAR		1	/* clear trace points */
45 #define KTROP_CLEARFILE		2	/* stop all tracing to file */
46 #define	KTROP_MASK		0x3
47 #define	KTROP(o)		((o)&KTROP_MASK) /* macro to extract operation */
48 /*
49  * flags (ORed in with operation)
50  */
51 #define KTRFLAG_DESCEND		4	/* perform op on all children too */
52 
53 /*
54  * ktrace record header
55  */
56 struct ktr_header {
57 	int	ktr_len;		/* length of record minus length of old header */
58 #if BYTE_ORDER == LITTLE_ENDIAN
59 	short	ktr_type;		/* trace record type */
60 	short	ktr_version;		/* trace record version */
61 #else
62 	short	ktr_version;		/* trace record version */
63 	short	ktr_type;		/* trace record type */
64 #endif
65 	pid_t	ktr_pid;		/* process id */
66 	char	ktr_comm[MAXCOMLEN+1];	/* command name */
67 	union {
68 		struct { /* v0 */
69 			struct {
70 				int32_t tv_sec;
71 				long tv_usec;
72 			} _tv;
73 			const void *_buf;
74 		} _v0;
75 		struct { /* v1 */
76 			struct {
77 				int32_t tv_sec;
78 				long tv_nsec;
79 			} _ts;
80 			lwpid_t _lid;
81 		} _v1;
82 		struct { /* v2 */
83 			struct timespec _ts;
84 			lwpid_t _lid;
85 		} _v2;
86 	} _v;
87 };
88 
89 #define ktr_lid		_v._v2._lid
90 #define ktr_olid	_v._v1._lid
91 #define ktr_time	_v._v2._ts
92 #define ktr_otv		_v._v0._tv
93 #define ktr_ots		_v._v1._ts
94 #define ktr_ts		_v._v2._ts
95 #define ktr_unused	_v._v0._buf
96 
97 #define	KTR_SHIMLEN	offsetof(struct ktr_header, ktr_pid)
98 
99 /*
100  * Test for kernel trace point
101  */
102 #define KTRPOINT(p, type)	\
103 	(((p)->p_traceflag & (1<<(type))) != 0)
104 
105 /*
106  * ktrace record types
107  */
108 
109 /*
110  * KTR_SYSCALL - system call record
111  */
112 #define KTR_SYSCALL	1
113 struct ktr_syscall {
114 	int	ktr_code;		/* syscall number */
115 	int	ktr_argsize;		/* size of arguments */
116 	/*
117 	 * followed by ktr_argsize/sizeof(register_t) "register_t"s
118 	 */
119 };
120 
121 /*
122  * KTR_SYSRET - return from system call record
123  */
124 #define KTR_SYSRET	2
125 struct ktr_sysret {
126 	short	ktr_code;
127 	short	ktr_eosys;		/* XXX unused */
128 	int	ktr_error;
129 	register_t ktr_retval;
130 	register_t ktr_retval_1;
131 };
132 
133 /*
134  * KTR_NAMEI - namei record
135  */
136 #define KTR_NAMEI	3
137 	/* record contains pathname */
138 
139 /*
140  * KTR_GENIO - trace generic process i/o
141  */
142 #define KTR_GENIO	4
143 struct ktr_genio {
144 	int	ktr_fd;
145 	enum	uio_rw ktr_rw;
146 	/*
147 	 * followed by data successfully read/written
148 	 */
149 };
150 
151 /*
152  * KTR_PSIG - trace processed signal
153  */
154 #define	KTR_PSIG	5
155 struct ktr_psig {
156 	int	signo;
157 	sig_t	action;
158 	sigset_t mask;
159 	int	code;
160 	/*
161 	 * followed by optional siginfo_t
162 	 */
163 };
164 
165 /*
166  * KTR_CSW - trace context switches
167  */
168 #define KTR_CSW		6
169 struct ktr_csw {
170 	int	out;	/* 1 if switch out, 0 if switch in */
171 	int	user;	/* 1 if usermode (ivcsw), 0 if kernel (vcsw) */
172 };
173 
174 /*
175  * KTR_EMUL - emulation change
176  */
177 #define KTR_EMUL	7
178 	/* record contains emulation name */
179 
180 /*
181  * KTR_USER - user record
182  */
183 #define	KTR_USER	8
184 #define KTR_USER_MAXIDLEN	20
185 #define KTR_USER_MAXLEN		2048	/* maximum length of passed data */
186 struct ktr_user {
187 	char 	ktr_id[KTR_USER_MAXIDLEN];	/* string id of caller */
188 	/*
189 	 * Followed by ktr_len - sizeof(struct ktr_user) of user data.
190 	 */
191 };
192 
193 /*
194  * KTR_EXEC_ARG, KTR_EXEC_ENV - Arguments and environment from exec
195  */
196 #define KTR_EXEC_ARG		10
197 #define KTR_EXEC_ENV		11
198 	/* record contains arg/env string */
199 
200 /*
201  * KTR_SAUPCALL - scheduler activated upcall.
202  *
203  * The structure is no longer used, but retained for compatibility.
204  */
205 #define	KTR_SAUPCALL	13
206 struct ktr_saupcall {
207 	int ktr_type;
208 	int ktr_nevent;
209 	int ktr_nint;
210 	void *ktr_sas;
211 	void *ktr_ap;
212 	/*
213 	 * followed by nevent sa_t's from sas[]
214 	 */
215 };
216 
217 /*
218  * KTR_MIB - MIB name and data
219  */
220 #define KTR_MIB		14
221 	/* Record contains MIB name */
222 
223 /*
224  * KTR_EXEC_FD - Opened file descriptor from exec
225  */
226 #define KTR_EXEC_FD		15
227 struct ktr_execfd {
228 	int   ktr_fd;
229 	u_int ktr_dtype; /* one of DTYPE_* constants */
230 };
231 
232 /*
233  * kernel trace points (in p_traceflag)
234  */
235 #define KTRFAC_MASK	0x00ffffff
236 #define KTRFAC_SYSCALL	(1<<KTR_SYSCALL)
237 #define KTRFAC_SYSRET	(1<<KTR_SYSRET)
238 #define KTRFAC_NAMEI	(1<<KTR_NAMEI)
239 #define KTRFAC_GENIO	(1<<KTR_GENIO)
240 #define	KTRFAC_PSIG	(1<<KTR_PSIG)
241 #define KTRFAC_CSW	(1<<KTR_CSW)
242 #define KTRFAC_EMUL	(1<<KTR_EMUL)
243 #define	KTRFAC_USER	(1<<KTR_USER)
244 #define KTRFAC_EXEC_ARG	(1<<KTR_EXEC_ARG)
245 #define KTRFAC_EXEC_ENV	(1<<KTR_EXEC_ENV)
246 #define	KTRFAC_MIB	(1<<KTR_MIB)
247 #define	KTRFAC_EXEC_FD	(1<<KTR_EXEC_FD)
248 /*
249  * trace flags (also in p_traceflags)
250  */
251 #define KTRFAC_PERSISTENT	0x80000000	/* persistent trace across sugid
252 						   exec (exclusive) */
253 #define KTRFAC_INHERIT	0x40000000	/* pass trace flags to children */
254 #define KTRFAC_TRC_EMUL	0x10000000	/* ktrace KTR_EMUL before next trace */
255 #define	KTRFAC_VER_MASK	0x0f000000	/* record version mask */
256 #define	KTRFAC_VER_SHIFT	24	/* record version shift */
257 
258 #define	KTRFAC_VERSION(tf)	(((tf) & KTRFAC_VER_MASK) >> KTRFAC_VER_SHIFT)
259 
260 #define	KTRFACv0	(0 << KTRFAC_VER_SHIFT)
261 #define	KTRFACv1	(1 << KTRFAC_VER_SHIFT)
262 #define	KTRFACv2	(2 << KTRFAC_VER_SHIFT)
263 
264 #ifndef	_KERNEL
265 
266 #include <sys/cdefs.h>
267 
268 __BEGIN_DECLS
269 int	ktrace(const char *, int, int, pid_t);
270 int	fktrace(int, int, int, pid_t);
271 int	utrace(const char *, void *, size_t);
272 __END_DECLS
273 
274 #else
275 
276 void ktrinit(void);
277 void ktrderef(struct proc *);
278 void ktradref(struct proc *);
279 
280 extern kmutex_t ktrace_lock;
281 extern int ktrace_on;
282 
283 int ktruser(const char *, void *, size_t, int);
284 bool ktr_point(int);
285 
286 void ktr_csw(int, int);
287 void ktr_emul(void);
288 void ktr_geniov(int, enum uio_rw, struct iovec *, size_t, int);
289 void ktr_genio(int, enum uio_rw, const void *, size_t, int);
290 void ktr_mibio(int, enum uio_rw, const void *, size_t, int);
291 void ktr_namei(const char *, size_t);
292 void ktr_namei2(const char *, size_t, const char *, size_t);
293 void ktr_psig(int, sig_t, const sigset_t *, const ksiginfo_t *);
294 void ktr_syscall(register_t, const register_t [], int);
295 void ktr_sysret(register_t, int, register_t *);
296 void ktr_kuser(const char *, void *, size_t);
297 void ktr_mib(const int *a , u_int b);
298 void ktr_execarg(const void *, size_t);
299 void ktr_execenv(const void *, size_t);
300 void ktr_execfd(int, u_int);
301 
302 int  ktrace_common(lwp_t *, int, int, int, file_t **);
303 
304 static inline int
305 ktrenter(lwp_t *l)
306 {
307 
308 	if ((l->l_pflag & LP_KTRACTIVE) != 0)
309 		return 1;
310 	l->l_pflag |= LP_KTRACTIVE;
311 	return 0;
312 }
313 
314 static inline void
315 ktrexit(lwp_t *l)
316 {
317 
318 	l->l_pflag &= ~LP_KTRACTIVE;
319 }
320 
321 static inline bool
322 ktrpoint(int fac)
323 {
324     return __predict_false(ktrace_on) && __predict_false(ktr_point(1 << fac));
325 }
326 
327 static inline void
328 ktrcsw(int a, int b)
329 {
330 	if (__predict_false(ktrace_on))
331 		ktr_csw(a, b);
332 }
333 
334 static inline void
335 ktremul(void)
336 {
337 	if (__predict_false(ktrace_on))
338 		ktr_emul();
339 }
340 
341 static inline void
342 ktrgenio(int a, enum uio_rw b, const void *c, size_t d, int e)
343 {
344 	if (__predict_false(ktrace_on))
345 		ktr_genio(a, b, c, d, e);
346 }
347 
348 static inline void
349 ktrgeniov(int a, enum uio_rw b, struct iovec *c, int d, int e)
350 {
351 	if (__predict_false(ktrace_on))
352 		ktr_geniov(a, b, c, d, e);
353 }
354 
355 static inline void
356 ktrmibio(int a, enum uio_rw b, const void *c, size_t d, int e)
357 {
358 	if (__predict_false(ktrace_on))
359 		ktr_mibio(a, b, c, d, e);
360 }
361 
362 static inline void
363 ktrnamei(const char *a, size_t b)
364 {
365 	if (__predict_false(ktrace_on))
366 		ktr_namei(a, b);
367 }
368 
369 static inline void
370 ktrnamei2(const char *a, size_t b, const char *c, size_t d)
371 {
372 	if (__predict_false(ktrace_on))
373 		ktr_namei2(a, b, c, d);
374 }
375 
376 static inline void
377 ktrpsig(int a, sig_t b, const sigset_t *c, const ksiginfo_t * d)
378 {
379 	if (__predict_false(ktrace_on))
380 		ktr_psig(a, b, c, d);
381 }
382 
383 static inline void
384 ktrsyscall(register_t code, const register_t args[], int narg)
385 {
386 	if (__predict_false(ktrace_on))
387 		ktr_syscall(code, args, narg);
388 }
389 
390 static inline void
391 ktrsysret(register_t a, int b, register_t *c)
392 {
393 	if (__predict_false(ktrace_on))
394 		ktr_sysret(a, b, c);
395 }
396 
397 static inline void
398 ktrkuser(const char *a, void *b, size_t c)
399 {
400 	if (__predict_false(ktrace_on))
401 		ktr_kuser(a, b, c);
402 }
403 
404 static inline void
405 ktrmib(const int *a , u_int b)
406 {
407 	if (__predict_false(ktrace_on))
408 		ktr_mib(a, b);
409 }
410 
411 static inline void
412 ktrexecarg(const void *a, size_t b)
413 {
414 	if (__predict_false(ktrace_on))
415 		ktr_execarg(a, b);
416 }
417 
418 static inline void
419 ktrexecenv(const void *a, size_t b)
420 {
421 	if (__predict_false(ktrace_on))
422 		ktr_execenv(a, b);
423 }
424 
425 static inline void
426 ktrexecfd(int fd, u_int dtype)
427 {
428 	if (__predict_false(ktrace_on))
429 		ktr_execfd(fd, dtype);
430 }
431 
432 #endif	/* !_KERNEL */
433 
434 #endif /* _SYS_KTRACE_H_ */
435