1/* $NetBSD: ktrace.h,v 1.65 2016/09/13 07:39:45 martin 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 */
59struct 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
116struct 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
128struct 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
146struct 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
158struct 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
172struct 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 */
189struct 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
209struct 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
230struct 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 * trace flags (also in p_traceflags)
253 */
254#define KTRFAC_PERSISTENT 0x80000000 /* persistent trace across sugid
255 exec (exclusive) */
256#define KTRFAC_INHERIT 0x40000000 /* pass trace flags to children */
257#define KTRFAC_TRC_EMUL 0x10000000 /* ktrace KTR_EMUL before next trace */
258#define KTRFAC_VER_MASK 0x0f000000 /* record version mask */
259#define KTRFAC_VER_SHIFT 24 /* record version shift */
260
261#define KTRFAC_VERSION(tf) (((tf) & KTRFAC_VER_MASK) >> KTRFAC_VER_SHIFT)
262
263#define KTRFACv0 (0 << KTRFAC_VER_SHIFT)
264#define KTRFACv1 (1 << KTRFAC_VER_SHIFT)
265#define KTRFACv2 (2 << KTRFAC_VER_SHIFT)
266
267#ifndef _KERNEL
268
269#include <sys/cdefs.h>
270
271__BEGIN_DECLS
272int ktrace(const char *, int, int, pid_t);
273int fktrace(int, int, int, pid_t);
274int utrace(const char *, void *, size_t);
275__END_DECLS
276
277#else
278
279void ktrinit(void);
280void ktrderef(struct proc *);
281void ktradref(struct proc *);
282
283extern kmutex_t ktrace_lock;
284extern int ktrace_on;
285
286int ktruser(const char *, void *, size_t, int);
287bool ktr_point(int);
288
289void ktr_csw(int, int);
290void ktr_emul(void);
291void ktr_geniov(int, enum uio_rw, struct iovec *, size_t, int);
292void ktr_genio(int, enum uio_rw, const void *, size_t, int);
293void ktr_mibio(int, enum uio_rw, const void *, size_t, int);
294void ktr_namei(const char *, size_t);
295void ktr_namei2(const char *, size_t, const char *, size_t);
296void ktr_psig(int, sig_t, const sigset_t *, const ksiginfo_t *);
297void ktr_syscall(register_t, const register_t [], int);
298void ktr_sysret(register_t, int, register_t *);
299void ktr_kuser(const char *, const void *, size_t);
300void ktr_mib(const int *a , u_int b);
301void ktr_execarg(const void *, size_t);
302void ktr_execenv(const void *, size_t);
303void ktr_execfd(int, u_int);
304
305int ktrace_common(lwp_t *, int, int, int, file_t **);
306
307static inline int
308ktrenter(lwp_t *l)
309{
310
311 if ((l->l_pflag & LP_KTRACTIVE) != 0)
312 return 1;
313 l->l_pflag |= LP_KTRACTIVE;
314 return 0;
315}
316
317static inline void
318ktrexit(lwp_t *l)
319{
320
321 l->l_pflag &= ~LP_KTRACTIVE;
322}
323
324static inline bool
325ktrpoint(int fac)
326{
327 return __predict_false(ktrace_on) && __predict_false(ktr_point(1 << fac));
328}
329
330static inline void
331ktrcsw(int a, int b)
332{
333 if (__predict_false(ktrace_on))
334 ktr_csw(a, b);
335}
336
337static inline void
338ktremul(void)
339{
340 if (__predict_false(ktrace_on))
341 ktr_emul();
342}
343
344static inline void
345ktrgenio(int a, enum uio_rw b, const void *c, size_t d, int e)
346{
347 if (__predict_false(ktrace_on))
348 ktr_genio(a, b, c, d, e);
349}
350
351static inline void
352ktrgeniov(int a, enum uio_rw b, struct iovec *c, int d, int e)
353{
354 if (__predict_false(ktrace_on))
355 ktr_geniov(a, b, c, d, e);
356}
357
358static inline void
359ktrmibio(int a, enum uio_rw b, const void *c, size_t d, int e)
360{
361 if (__predict_false(ktrace_on))
362 ktr_mibio(a, b, c, d, e);
363}
364
365static inline void
366ktrnamei(const char *a, size_t b)
367{
368 if (__predict_false(ktrace_on))
369 ktr_namei(a, b);
370}
371
372static inline void
373ktrnamei2(const char *a, size_t b, const char *c, size_t d)
374{
375 if (__predict_false(ktrace_on))
376 ktr_namei2(a, b, c, d);
377}
378
379static inline void
380ktrpsig(int a, sig_t b, const sigset_t *c, const ksiginfo_t * d)
381{
382 if (__predict_false(ktrace_on))
383 ktr_psig(a, b, c, d);
384}
385
386static inline void
387ktrsyscall(register_t code, const register_t args[], int narg)
388{
389 if (__predict_false(ktrace_on))
390 ktr_syscall(code, args, narg);
391}
392
393static inline void
394ktrsysret(register_t a, int b, register_t *c)
395{
396 if (__predict_false(ktrace_on))
397 ktr_sysret(a, b, c);
398}
399
400static inline void
401ktrkuser(const char *a, const void *b, size_t c)
402{
403 if (__predict_false(ktrace_on))
404 ktr_kuser(a, b, c);
405}
406
407static inline void
408ktrmib(const int *a , u_int b)
409{
410 if (__predict_false(ktrace_on))
411 ktr_mib(a, b);
412}
413
414static inline void
415ktrexecarg(const void *a, size_t b)
416{
417 if (__predict_false(ktrace_on))
418 ktr_execarg(a, b);
419}
420
421static inline void
422ktrexecenv(const void *a, size_t b)
423{
424 if (__predict_false(ktrace_on))
425 ktr_execenv(a, b);
426}
427
428static inline void
429ktrexecfd(int fd, u_int dtype)
430{
431 if (__predict_false(ktrace_on))
432 ktr_execfd(fd, dtype);
433}
434
435struct ktrace_entry;
436int ktealloc(struct ktrace_entry **, void **, lwp_t *, int, size_t);
437void ktesethdrlen(struct ktrace_entry *, size_t);
438void ktraddentry(lwp_t *, struct ktrace_entry *, int);
439/* Flags for ktraddentry (3rd arg) */
440#define KTA_NOWAIT 0x0000
441#define KTA_WAITOK 0x0001
442#define KTA_LARGE 0x0002
443
444#endif /* !_KERNEL */
445
446#endif /* _SYS_KTRACE_H_ */
447