xref: /netbsd/sys/sys/ktrace.h (revision 4f46ee44)
1 /*	$NetBSD: ktrace.h,v 1.68 2022/06/29 22:10:43 riastradh 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 #include <sys/signal.h>
40 #include <sys/time.h>
41 #include <sys/uio.h>
42 
43 /*
44  * operations to ktrace system call  (KTROP(op))
45  */
46 #define KTROP_SET		0	/* set trace points */
47 #define KTROP_CLEAR		1	/* clear trace points */
48 #define KTROP_CLEARFILE		2	/* stop all tracing to file */
49 #define	KTROP_MASK		0x3
50 #define	KTROP(o)		((o)&KTROP_MASK) /* macro to extract operation */
51 /*
52  * flags (ORed in with operation)
53  */
54 #define KTRFLAG_DESCEND		4	/* perform op on all children too */
55 
56 /*
57  * ktrace record header
58  */
59 struct ktr_header {
60 	int	ktr_len;		/* length of record minus length of old header */
61 #if BYTE_ORDER == LITTLE_ENDIAN
62 	short	ktr_type;		/* trace record type */
63 	short	ktr_version;		/* trace record version */
64 #else
65 	short	ktr_version;		/* trace record version */
66 	short	ktr_type;		/* trace record type */
67 #endif
68 	pid_t	ktr_pid;		/* process id */
69 	char	ktr_comm[MAXCOMLEN+1];	/* command name */
70 	union {
71 		struct { /* v0 */
72 			struct {
73 				int32_t tv_sec;
74 				long tv_usec;
75 			} _tv;
76 			const void *_buf;
77 		} _v0;
78 		struct { /* v1 */
79 			struct {
80 				int32_t tv_sec;
81 				long tv_nsec;
82 			} _ts;
83 			lwpid_t _lid;
84 		} _v1;
85 		struct { /* v2 */
86 			struct timespec _ts;
87 			lwpid_t _lid;
88 		} _v2;
89 	} _v;
90 };
91 
92 #define ktr_lid		_v._v2._lid
93 #define ktr_olid	_v._v1._lid
94 #define ktr_time	_v._v2._ts
95 #define ktr_otv		_v._v0._tv
96 #define ktr_ots		_v._v1._ts
97 #define ktr_ts		_v._v2._ts
98 #define ktr_unused	_v._v0._buf
99 
100 #define	KTR_SHIMLEN	offsetof(struct ktr_header, ktr_pid)
101 
102 /*
103  * Test for kernel trace point
104  */
105 #define KTRPOINT(p, type)	\
106 	(((p)->p_traceflag & (1<<(type))) != 0)
107 
108 /*
109  * ktrace record types
110  */
111 
112 /*
113  * KTR_SYSCALL - system call record
114  */
115 #define KTR_SYSCALL	1
116 struct ktr_syscall {
117 	int	ktr_code;		/* syscall number */
118 	int	ktr_argsize;		/* size of arguments */
119 	/*
120 	 * followed by ktr_argsize/sizeof(register_t) "register_t"s
121 	 */
122 };
123 
124 /*
125  * KTR_SYSRET - return from system call record
126  */
127 #define KTR_SYSRET	2
128 struct ktr_sysret {
129 	short	ktr_code;
130 	short	ktr_eosys;		/* XXX unused */
131 	int	ktr_error;
132 	__register_t ktr_retval;
133 	__register_t ktr_retval_1;
134 };
135 
136 /*
137  * KTR_NAMEI - namei record
138  */
139 #define KTR_NAMEI	3
140 	/* record contains pathname */
141 
142 /*
143  * KTR_GENIO - trace generic process i/o
144  */
145 #define KTR_GENIO	4
146 struct ktr_genio {
147 	int	ktr_fd;
148 	enum	uio_rw ktr_rw;
149 	/*
150 	 * followed by data successfully read/written
151 	 */
152 };
153 
154 /*
155  * KTR_PSIG - trace processed signal
156  */
157 #define	KTR_PSIG	5
158 struct ktr_psig {
159 	int	signo;
160 	sig_t	action;
161 	sigset_t mask;
162 	int	code;
163 	/*
164 	 * followed by optional siginfo_t
165 	 */
166 };
167 
168 /*
169  * KTR_CSW - trace context switches
170  */
171 #define KTR_CSW		6
172 struct ktr_csw {
173 	int	out;	/* 1 if switch out, 0 if switch in */
174 	int	user;	/* 1 if usermode (ivcsw), 0 if kernel (vcsw) */
175 };
176 
177 /*
178  * KTR_EMUL - emulation change
179  */
180 #define KTR_EMUL	7
181 	/* record contains emulation name */
182 
183 /*
184  * KTR_USER - user record
185  */
186 #define	KTR_USER	8
187 #define KTR_USER_MAXIDLEN	20
188 #define KTR_USER_MAXLEN		2048	/* maximum length of passed data */
189 struct ktr_user {
190 	char 	ktr_id[KTR_USER_MAXIDLEN];	/* string id of caller */
191 	/*
192 	 * Followed by ktr_len - sizeof(struct ktr_user) of user data.
193 	 */
194 };
195 
196 /*
197  * KTR_EXEC_ARG, KTR_EXEC_ENV - Arguments and environment from exec
198  */
199 #define KTR_EXEC_ARG		10
200 #define KTR_EXEC_ENV		11
201 	/* record contains arg/env string */
202 
203 /*
204  * KTR_SAUPCALL - scheduler activated upcall.
205  *
206  * The structure is no longer used, but retained for compatibility.
207  */
208 #define	KTR_SAUPCALL	13
209 struct ktr_saupcall {
210 	int ktr_type;
211 	int ktr_nevent;
212 	int ktr_nint;
213 	void *ktr_sas;
214 	void *ktr_ap;
215 	/*
216 	 * followed by nevent sa_t's from sas[]
217 	 */
218 };
219 
220 /*
221  * KTR_MIB - MIB name and data
222  */
223 #define KTR_MIB		14
224 	/* Record contains MIB name */
225 
226 /*
227  * KTR_EXEC_FD - Opened file descriptor from exec
228  */
229 #define KTR_EXEC_FD		15
230 struct ktr_execfd {
231 	int   ktr_fd;
232 	u_int ktr_dtype; /* one of DTYPE_* constants */
233 };
234 
235 /*
236  * kernel trace points (in p_traceflag)
237  */
238 #define KTRFAC_MASK	0x00ffffff
239 #define KTRFAC_SYSCALL	(1<<KTR_SYSCALL)
240 #define KTRFAC_SYSRET	(1<<KTR_SYSRET)
241 #define KTRFAC_NAMEI	(1<<KTR_NAMEI)
242 #define KTRFAC_GENIO	(1<<KTR_GENIO)
243 #define	KTRFAC_PSIG	(1<<KTR_PSIG)
244 #define KTRFAC_CSW	(1<<KTR_CSW)
245 #define KTRFAC_EMUL	(1<<KTR_EMUL)
246 #define	KTRFAC_USER	(1<<KTR_USER)
247 #define KTRFAC_EXEC_ARG	(1<<KTR_EXEC_ARG)
248 #define KTRFAC_EXEC_ENV	(1<<KTR_EXEC_ENV)
249 #define	KTRFAC_MIB	(1<<KTR_MIB)
250 #define	KTRFAC_EXEC_FD	(1<<KTR_EXEC_FD)
251 
252 #define __KTRACE_FLAG_BITS \
253     "\177\020" \
254     "b\1SYSCALL\0" \
255     "b\2SYSRET\0" \
256     "b\3NAMEI\0" \
257     "b\4GENIO\0" \
258     "b\5PSIG\0" \
259     "b\6CSW\0" \
260     "b\7EMUL\0" \
261     "b\10USER\0" \
262     "b\12EXEC_ARG\0" \
263     "b\13EXEC_ENV\0" \
264     "b\15SAUPCALL\0" \
265     "b\16MIB\0" \
266     "b\17EXEC_FD\0" \
267     "f\30\4VERSION\0" \
268     "b\36TRC_EMUL\0" \
269     "b\37INHERIT\0" \
270     "b\40PERSISTENT\0"
271 
272 /*
273  * trace flags (also in p_traceflags)
274  */
275 #define KTRFAC_PERSISTENT	0x80000000	/* persistent trace across sugid
276 						   exec (exclusive) */
277 #define KTRFAC_INHERIT	0x40000000	/* pass trace flags to children */
278 #define KTRFAC_TRC_EMUL	0x10000000	/* ktrace KTR_EMUL before next trace */
279 #define	KTRFAC_VER_MASK	0x0f000000	/* record version mask */
280 #define	KTRFAC_VER_SHIFT	24	/* record version shift */
281 
282 #define	KTRFAC_VERSION(tf)	(((tf) & KTRFAC_VER_MASK) >> KTRFAC_VER_SHIFT)
283 
284 #define	KTRFACv0	(0 << KTRFAC_VER_SHIFT)
285 #define	KTRFACv1	(1 << KTRFAC_VER_SHIFT)
286 #define	KTRFACv2	(2 << KTRFAC_VER_SHIFT)
287 
288 #ifndef	_KERNEL
289 
290 #include <sys/cdefs.h>
291 
292 __BEGIN_DECLS
293 int	ktrace(const char *, int, int, pid_t);
294 int	fktrace(int, int, int, pid_t);
295 int	utrace(const char *, void *, size_t);
296 __END_DECLS
297 
298 #else
299 
300 struct syncobj;
301 
302 void ktrinit(void);
303 void ktrderef(struct proc *);
304 void ktradref(struct proc *);
305 
306 extern kmutex_t ktrace_lock;
307 extern int ktrace_on;
308 
309 int ktruser(const char *, void *, size_t, int);
310 bool ktr_point(int);
311 
312 void ktr_csw(int, int, const struct syncobj *);
313 void ktr_emul(void);
314 void ktr_geniov(int, enum uio_rw, struct iovec *, size_t, int);
315 void ktr_genio(int, enum uio_rw, const void *, size_t, int);
316 void ktr_mibio(int, enum uio_rw, const void *, size_t, int);
317 void ktr_namei(const char *, size_t);
318 void ktr_namei2(const char *, size_t, const char *, size_t);
319 void ktr_psig(int, sig_t, const sigset_t *, const ksiginfo_t *);
320 void ktr_syscall(register_t, const register_t [], int);
321 void ktr_sysret(register_t, int, register_t *);
322 void ktr_kuser(const char *, const void *, size_t);
323 void ktr_mib(const int *a , u_int b);
324 void ktr_execarg(const void *, size_t);
325 void ktr_execenv(const void *, size_t);
326 void ktr_execfd(int, u_int);
327 
328 int  ktrace_common(lwp_t *, int, int, int, file_t **);
329 
330 static __inline int
331 ktrenter(lwp_t *l)
332 {
333 
334 	if ((l->l_pflag & LP_KTRACTIVE) != 0)
335 		return 1;
336 	l->l_pflag |= LP_KTRACTIVE;
337 	return 0;
338 }
339 
340 static __inline void
341 ktrexit(lwp_t *l)
342 {
343 
344 	l->l_pflag &= ~LP_KTRACTIVE;
345 }
346 
347 static __inline bool
348 ktrpoint(int fac)
349 {
350     return __predict_false(ktrace_on) && __predict_false(ktr_point(1 << fac));
351 }
352 
353 static __inline void
354 ktrcsw(int a, int b, const struct syncobj *c)
355 {
356 	if (__predict_false(ktrace_on))
357 		ktr_csw(a, b, c);
358 }
359 
360 static __inline void
361 ktremul(void)
362 {
363 	if (__predict_false(ktrace_on))
364 		ktr_emul();
365 }
366 
367 static __inline void
368 ktrgenio(int a, enum uio_rw b, const void *c, size_t d, int e)
369 {
370 	if (__predict_false(ktrace_on))
371 		ktr_genio(a, b, c, d, e);
372 }
373 
374 static __inline void
375 ktrgeniov(int a, enum uio_rw b, struct iovec *c, int d, int e)
376 {
377 	if (__predict_false(ktrace_on))
378 		ktr_geniov(a, b, c, d, e);
379 }
380 
381 static __inline void
382 ktrmibio(int a, enum uio_rw b, const void *c, size_t d, int e)
383 {
384 	if (__predict_false(ktrace_on))
385 		ktr_mibio(a, b, c, d, e);
386 }
387 
388 static __inline void
389 ktrnamei(const char *a, size_t b)
390 {
391 	if (__predict_false(ktrace_on))
392 		ktr_namei(a, b);
393 }
394 
395 static __inline void
396 ktrnamei2(const char *a, size_t b, const char *c, size_t d)
397 {
398 	if (__predict_false(ktrace_on))
399 		ktr_namei2(a, b, c, d);
400 }
401 
402 static __inline void
403 ktrpsig(int a, sig_t b, const sigset_t *c, const ksiginfo_t * d)
404 {
405 	if (__predict_false(ktrace_on))
406 		ktr_psig(a, b, c, d);
407 }
408 
409 static __inline void
410 ktrsyscall(register_t code, const register_t args[], int narg)
411 {
412 	if (__predict_false(ktrace_on))
413 		ktr_syscall(code, args, narg);
414 }
415 
416 static __inline void
417 ktrsysret(register_t a, int b, register_t *c)
418 {
419 	if (__predict_false(ktrace_on))
420 		ktr_sysret(a, b, c);
421 }
422 
423 static __inline void
424 ktrkuser(const char *a, const void *b, size_t c)
425 {
426 	if (__predict_false(ktrace_on))
427 		ktr_kuser(a, b, c);
428 }
429 
430 static __inline void
431 ktrmib(const int *a , u_int b)
432 {
433 	if (__predict_false(ktrace_on))
434 		ktr_mib(a, b);
435 }
436 
437 static __inline void
438 ktrexecarg(const void *a, size_t b)
439 {
440 	if (__predict_false(ktrace_on))
441 		ktr_execarg(a, b);
442 }
443 
444 static __inline void
445 ktrexecenv(const void *a, size_t b)
446 {
447 	if (__predict_false(ktrace_on))
448 		ktr_execenv(a, b);
449 }
450 
451 static __inline void
452 ktrexecfd(int fd, u_int dtype)
453 {
454 	if (__predict_false(ktrace_on))
455 		ktr_execfd(fd, dtype);
456 }
457 
458 struct ktrace_entry;
459 int	ktealloc(struct ktrace_entry **, void **, lwp_t *, int, size_t);
460 void	ktesethdrlen(struct ktrace_entry *, size_t);
461 void	ktraddentry(lwp_t *, struct ktrace_entry *, int);
462 /* Flags for ktraddentry (3rd arg) */
463 #define	KTA_NOWAIT		0x0000
464 #define	KTA_WAITOK		0x0001
465 #define	KTA_LARGE		0x0002
466 
467 #endif	/* !_KERNEL */
468 
469 #endif /* _SYS_KTRACE_H_ */
470