xref: /xnu-8792.41.9/bsd/sys/kdebug_kernel.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions  *
15*5c2921b0SApple OSS Distributions  * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions  *
18*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions  * limitations under the License.
25*5c2921b0SApple OSS Distributions  *
26*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions 
29*5c2921b0SApple OSS Distributions #ifndef BSD_SYS_KDEBUG_KERNEL_H
30*5c2921b0SApple OSS Distributions #define BSD_SYS_KDEBUG_KERNEL_H
31*5c2921b0SApple OSS Distributions 
32*5c2921b0SApple OSS Distributions #include <mach/boolean.h>
33*5c2921b0SApple OSS Distributions #include <mach/clock_types.h>
34*5c2921b0SApple OSS Distributions #include <stdbool.h>
35*5c2921b0SApple OSS Distributions #include <stdint.h>
36*5c2921b0SApple OSS Distributions #include <sys/cdefs.h>
37*5c2921b0SApple OSS Distributions 
38*5c2921b0SApple OSS Distributions __BEGIN_DECLS
39*5c2921b0SApple OSS Distributions 
40*5c2921b0SApple OSS Distributions #ifdef KERNEL
41*5c2921b0SApple OSS Distributions 
42*5c2921b0SApple OSS Distributions /*
43*5c2921b0SApple OSS Distributions  * To use kdebug in the kernel:
44*5c2921b0SApple OSS Distributions  *
45*5c2921b0SApple OSS Distributions  * #include <sys/kdebug_kernel.h>
46*5c2921b0SApple OSS Distributions  *
47*5c2921b0SApple OSS Distributions  * #define DBG_NETIPINIT NETDBG_CODE(DBG_NETIP, 1)
48*5c2921b0SApple OSS Distributions  *
49*5c2921b0SApple OSS Distributions  * void
50*5c2921b0SApple OSS Distributions  * ip_init(void)
51*5c2921b0SApple OSS Distributions  * {
52*5c2921b0SApple OSS Distributions  *     KDBG(DBG_NETIPINIT | DBG_FUNC_START, 1, 2, 3, 4);
53*5c2921b0SApple OSS Distributions  *     ...
54*5c2921b0SApple OSS Distributions  *     KDBG(DBG_NETIPINIT);
55*5c2921b0SApple OSS Distributions  *     ...
56*5c2921b0SApple OSS Distributions  *     KDBG(DBG_NETIPINIT | DBG_FUNC_END);
57*5c2921b0SApple OSS Distributions  * }
58*5c2921b0SApple OSS Distributions  */
59*5c2921b0SApple OSS Distributions 
60*5c2921b0SApple OSS Distributions #pragma mark - kernel tracepoints
61*5c2921b0SApple OSS Distributions 
62*5c2921b0SApple OSS Distributions /*
63*5c2921b0SApple OSS Distributions  * The KDBG{,_DEBUG,_RELEASE,_FILTERED} macros are the preferred method of
64*5c2921b0SApple OSS Distributions  * making tracepoints.
65*5c2921b0SApple OSS Distributions  *
66*5c2921b0SApple OSS Distributions  * Kernel pointers must be unslid or permuted using VM_KERNEL_UNSLIDE_OR_PERM.
67*5c2921b0SApple OSS Distributions  * Do not trace any sensitive data.
68*5c2921b0SApple OSS Distributions  */
69*5c2921b0SApple OSS Distributions 
70*5c2921b0SApple OSS Distributions /*
71*5c2921b0SApple OSS Distributions  * Traced on debug and development (and release macOS) kernels.
72*5c2921b0SApple OSS Distributions  */
73*5c2921b0SApple OSS Distributions #define KDBG(x, ...) KDBG_(, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
74*5c2921b0SApple OSS Distributions 
75*5c2921b0SApple OSS Distributions /*
76*5c2921b0SApple OSS Distributions  * Traced on debug and development (and release macOS) kernels if explicitly
77*5c2921b0SApple OSS Distributions  * requested.  Omitted from tracing without a typefilter.
78*5c2921b0SApple OSS Distributions  */
79*5c2921b0SApple OSS Distributions #define KDBG_FILTERED(x, ...) KDBG_(_FILTERED, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
80*5c2921b0SApple OSS Distributions 
81*5c2921b0SApple OSS Distributions #ifdef KERNEL_PRIVATE
82*5c2921b0SApple OSS Distributions 
83*5c2921b0SApple OSS Distributions /*
84*5c2921b0SApple OSS Distributions  * Traced on debug and development (and release macOS) kernels, even if the
85*5c2921b0SApple OSS Distributions  * process filter would reject it.
86*5c2921b0SApple OSS Distributions  */
87*5c2921b0SApple OSS Distributions #define KDBG_RELEASE_NOPROCFILT(x, ...) \
88*5c2921b0SApple OSS Distributions 	        KDBG_(_RELEASE_NOPROCFILT, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
89*5c2921b0SApple OSS Distributions 
90*5c2921b0SApple OSS Distributions #endif /* KERNEL_PRIVATE */
91*5c2921b0SApple OSS Distributions 
92*5c2921b0SApple OSS Distributions /*
93*5c2921b0SApple OSS Distributions  * Traced on debug, development, and release kernels.
94*5c2921b0SApple OSS Distributions  *
95*5c2921b0SApple OSS Distributions  * Only use this tracepoint if the events are required for a shipping trace
96*5c2921b0SApple OSS Distributions  * tool.
97*5c2921b0SApple OSS Distributions  */
98*5c2921b0SApple OSS Distributions #define KDBG_RELEASE(x, ...) KDBG_(_RELEASE, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
99*5c2921b0SApple OSS Distributions 
100*5c2921b0SApple OSS Distributions /*
101*5c2921b0SApple OSS Distributions  * Traced only on debug kernels.
102*5c2921b0SApple OSS Distributions  */
103*5c2921b0SApple OSS Distributions #define KDBG_DEBUG(x, ...) KDBG_(_DEBUG, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
104*5c2921b0SApple OSS Distributions 
105*5c2921b0SApple OSS Distributions #pragma mark - kernel API
106*5c2921b0SApple OSS Distributions 
107*5c2921b0SApple OSS Distributions #ifdef KERNEL_PRIVATE
108*5c2921b0SApple OSS Distributions 
109*5c2921b0SApple OSS Distributions /*
110*5c2921b0SApple OSS Distributions  * kernel_debug_string provides the same functionality as the
111*5c2921b0SApple OSS Distributions  * kdebug_trace_string syscall as a KPI.  str_id is an in/out
112*5c2921b0SApple OSS Distributions  * parameter that, if it's pointing to a string ID of 0, will
113*5c2921b0SApple OSS Distributions  * receive a generated ID.  If it provides a value in str_id,
114*5c2921b0SApple OSS Distributions  * then that will be used, instead.
115*5c2921b0SApple OSS Distributions  *
116*5c2921b0SApple OSS Distributions  * Returns an errno indicating the type of failure.
117*5c2921b0SApple OSS Distributions  */
118*5c2921b0SApple OSS Distributions int kernel_debug_string(uint32_t debugid, uint64_t *str_id, const char *str);
119*5c2921b0SApple OSS Distributions 
120*5c2921b0SApple OSS Distributions /*
121*5c2921b0SApple OSS Distributions  * kernel_debug_disable disables event logging, but leaves any buffers
122*5c2921b0SApple OSS Distributions  * intact.
123*5c2921b0SApple OSS Distributions  */
124*5c2921b0SApple OSS Distributions void kernel_debug_disable(void);
125*5c2921b0SApple OSS Distributions 
126*5c2921b0SApple OSS Distributions #endif /* KERNEL_PRIVATE */
127*5c2921b0SApple OSS Distributions 
128*5c2921b0SApple OSS Distributions /*
129*5c2921b0SApple OSS Distributions  * Returns true if kdebug is using continuous time for its events, and false
130*5c2921b0SApple OSS Distributions  * otherwise.
131*5c2921b0SApple OSS Distributions  */
132*5c2921b0SApple OSS Distributions bool kdebug_using_continuous_time(void);
133*5c2921b0SApple OSS Distributions 
134*5c2921b0SApple OSS Distributions /*
135*5c2921b0SApple OSS Distributions  * Convert an absolute time to a kdebug timestamp.
136*5c2921b0SApple OSS Distributions  */
137*5c2921b0SApple OSS Distributions extern uint64_t kdebug_timestamp_from_absolute(uint64_t abstime);
138*5c2921b0SApple OSS Distributions 
139*5c2921b0SApple OSS Distributions /*
140*5c2921b0SApple OSS Distributions  * Convert a continuous time to a kdebug timestamp.
141*5c2921b0SApple OSS Distributions  */
142*5c2921b0SApple OSS Distributions extern uint64_t kdebug_timestamp_from_continuous(uint64_t conttime);
143*5c2921b0SApple OSS Distributions 
144*5c2921b0SApple OSS Distributions /*
145*5c2921b0SApple OSS Distributions  * Capture a kdebug timestamp for the current time.
146*5c2921b0SApple OSS Distributions  */
147*5c2921b0SApple OSS Distributions extern uint64_t kdebug_timestamp(void);
148*5c2921b0SApple OSS Distributions 
149*5c2921b0SApple OSS Distributions /*
150*5c2921b0SApple OSS Distributions  * Returns true if kdebug will log an event with the provided debugid, and
151*5c2921b0SApple OSS Distributions  * false otherwise.
152*5c2921b0SApple OSS Distributions  */
153*5c2921b0SApple OSS Distributions bool kdebug_debugid_enabled(uint32_t debugid);
154*5c2921b0SApple OSS Distributions 
155*5c2921b0SApple OSS Distributions /*
156*5c2921b0SApple OSS Distributions  * Returns true only if the debugid is explicitly enabled by filters.  Returns
157*5c2921b0SApple OSS Distributions  * false otherwise, including when no filters are active.
158*5c2921b0SApple OSS Distributions  */
159*5c2921b0SApple OSS Distributions bool kdebug_debugid_explicitly_enabled(uint32_t debugid);
160*5c2921b0SApple OSS Distributions 
161*5c2921b0SApple OSS Distributions uint32_t kdebug_commpage_state(void);
162*5c2921b0SApple OSS Distributions 
163*5c2921b0SApple OSS Distributions #pragma mark - Coprocessor/IOP tracing
164*5c2921b0SApple OSS Distributions 
165*5c2921b0SApple OSS Distributions typedef enum {
166*5c2921b0SApple OSS Distributions 	/* Trace is now enabled. */
167*5c2921b0SApple OSS Distributions 	KD_CALLBACK_KDEBUG_ENABLED,
168*5c2921b0SApple OSS Distributions 	/*
169*5c2921b0SApple OSS Distributions 	 * Trace is being disabled, but events are still accepted for the duration
170*5c2921b0SApple OSS Distributions 	 * of the callback.
171*5c2921b0SApple OSS Distributions 	 */
172*5c2921b0SApple OSS Distributions 	KD_CALLBACK_KDEBUG_DISABLED,
173*5c2921b0SApple OSS Distributions 	/*
174*5c2921b0SApple OSS Distributions 	 * Request the latest events from the IOP and block until complete.  Any
175*5c2921b0SApple OSS Distributions 	 * events that occur prior to this callback being called may be dropped by
176*5c2921b0SApple OSS Distributions 	 * the trace system.
177*5c2921b0SApple OSS Distributions 	 */
178*5c2921b0SApple OSS Distributions 	KD_CALLBACK_SYNC_FLUSH,
179*5c2921b0SApple OSS Distributions 	/*
180*5c2921b0SApple OSS Distributions 	 * The typefilter is being used.
181*5c2921b0SApple OSS Distributions 	 *
182*5c2921b0SApple OSS Distributions 	 * A read-only pointer to the typefilter is provided as the argument, valid
183*5c2921b0SApple OSS Distributions 	 * only in the callback.
184*5c2921b0SApple OSS Distributions 	 */
185*5c2921b0SApple OSS Distributions 	KD_CALLBACK_TYPEFILTER_CHANGED,
186*5c2921b0SApple OSS Distributions 	/*
187*5c2921b0SApple OSS Distributions 	 * The coprocessor should emit data that snapshots the current state of the
188*5c2921b0SApple OSS Distributions 	 * system.
189*5c2921b0SApple OSS Distributions 	 */
190*5c2921b0SApple OSS Distributions 	KD_CALLBACK_SNAPSHOT_STATE,
191*5c2921b0SApple OSS Distributions } kd_callback_type;
192*5c2921b0SApple OSS Distributions 
193*5c2921b0SApple OSS Distributions __options_decl(kdebug_coproc_flags_t, uint32_t, {
194*5c2921b0SApple OSS Distributions 	/*
195*5c2921b0SApple OSS Distributions 	 * Event timestamps from this coprocessor are in the continuous timebase.
196*5c2921b0SApple OSS Distributions 	 */
197*5c2921b0SApple OSS Distributions 	KDCP_CONTINUOUS_TIME = 0x001,
198*5c2921b0SApple OSS Distributions });
199*5c2921b0SApple OSS Distributions 
200*5c2921b0SApple OSS Distributions typedef void (*kd_callback_fn)(void *context, kd_callback_type reason,
201*5c2921b0SApple OSS Distributions     void *arg);
202*5c2921b0SApple OSS Distributions 
203*5c2921b0SApple OSS Distributions /*
204*5c2921b0SApple OSS Distributions  * Register a coprocessor for participation in tracing.
205*5c2921b0SApple OSS Distributions  *
206*5c2921b0SApple OSS Distributions  * The `callback` function will be called with the provided `context` when
207*5c2921b0SApple OSS Distributions  * necessary, according to the `kd_callback_type`s.
208*5c2921b0SApple OSS Distributions  *
209*5c2921b0SApple OSS Distributions  * The positive core ID is returned on success, or -1 on failure.
210*5c2921b0SApple OSS Distributions  */
211*5c2921b0SApple OSS Distributions int kdebug_register_coproc(const char *name, kdebug_coproc_flags_t flags,
212*5c2921b0SApple OSS Distributions     kd_callback_fn callback, void *context);
213*5c2921b0SApple OSS Distributions 
214*5c2921b0SApple OSS Distributions void kernel_debug_enter(uint32_t coreid, uint32_t debugid, uint64_t timestamp,
215*5c2921b0SApple OSS Distributions     uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4,
216*5c2921b0SApple OSS Distributions     uintptr_t threadid);
217*5c2921b0SApple OSS Distributions 
218*5c2921b0SApple OSS Distributions /*
219*5c2921b0SApple OSS Distributions  * Legacy definitions for the prior IOP tracing.
220*5c2921b0SApple OSS Distributions  */
221*5c2921b0SApple OSS Distributions 
222*5c2921b0SApple OSS Distributions struct kd_callback {
223*5c2921b0SApple OSS Distributions 	kd_callback_fn func;
224*5c2921b0SApple OSS Distributions 	void *context;
225*5c2921b0SApple OSS Distributions 	/* name of IOP, NUL-terminated */
226*5c2921b0SApple OSS Distributions 	char iop_name[8];
227*5c2921b0SApple OSS Distributions };
228*5c2921b0SApple OSS Distributions typedef struct kd_callback kd_callback_t;
229*5c2921b0SApple OSS Distributions 
230*5c2921b0SApple OSS Distributions __kpi_deprecated("use kdebug_register_coproc instead")
231*5c2921b0SApple OSS Distributions int kernel_debug_register_callback(kd_callback_t callback);
232*5c2921b0SApple OSS Distributions 
233*5c2921b0SApple OSS Distributions #pragma mark - internals
234*5c2921b0SApple OSS Distributions 
235*5c2921b0SApple OSS Distributions #define KDBG_(f, x, a, b, c, d, n, ...) KDBG##n(f, x, a, b, c, d)
236*5c2921b0SApple OSS Distributions #define KDBG0(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, 0, 0, 0, 0, 0)
237*5c2921b0SApple OSS Distributions #define KDBG1(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, 0, 0, 0, 0)
238*5c2921b0SApple OSS Distributions #define KDBG2(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, 0, 0, 0)
239*5c2921b0SApple OSS Distributions #define KDBG3(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, c, 0, 0)
240*5c2921b0SApple OSS Distributions #define KDBG4(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, c, d, 0)
241*5c2921b0SApple OSS Distributions 
242*5c2921b0SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
243*5c2921b0SApple OSS Distributions #define KDBG_IMPROBABLE __improbable
244*5c2921b0SApple OSS Distributions #else
245*5c2921b0SApple OSS Distributions #define KDBG_IMPROBABLE
246*5c2921b0SApple OSS Distributions #endif
247*5c2921b0SApple OSS Distributions 
248*5c2921b0SApple OSS Distributions extern unsigned int kdebug_enable;
249*5c2921b0SApple OSS Distributions 
250*5c2921b0SApple OSS Distributions /*
251*5c2921b0SApple OSS Distributions  * The kernel debug configuration level.  These values control which events are
252*5c2921b0SApple OSS Distributions  * compiled in under different build configurations.
253*5c2921b0SApple OSS Distributions  *
254*5c2921b0SApple OSS Distributions  * Infer the supported kernel debug event level from config option.  Use
255*5c2921b0SApple OSS Distributions  * (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) as a guard to protect unaudited debug
256*5c2921b0SApple OSS Distributions  * code.
257*5c2921b0SApple OSS Distributions  */
258*5c2921b0SApple OSS Distributions #define KDEBUG_LEVEL_NONE     0
259*5c2921b0SApple OSS Distributions #define KDEBUG_LEVEL_IST      1
260*5c2921b0SApple OSS Distributions #define KDEBUG_LEVEL_STANDARD 2
261*5c2921b0SApple OSS Distributions #define KDEBUG_LEVEL_FULL     3
262*5c2921b0SApple OSS Distributions 
263*5c2921b0SApple OSS Distributions #if NO_KDEBUG
264*5c2921b0SApple OSS Distributions #define KDEBUG_LEVEL KDEBUG_LEVEL_NONE
265*5c2921b0SApple OSS Distributions #elif IST_KDEBUG
266*5c2921b0SApple OSS Distributions #define KDEBUG_LEVEL KDEBUG_LEVEL_IST
267*5c2921b0SApple OSS Distributions #elif KDEBUG
268*5c2921b0SApple OSS Distributions #define KDEBUG_LEVEL KDEBUG_LEVEL_FULL
269*5c2921b0SApple OSS Distributions #else
270*5c2921b0SApple OSS Distributions #define KDEBUG_LEVEL KDEBUG_LEVEL_STANDARD
271*5c2921b0SApple OSS Distributions /*
272*5c2921b0SApple OSS Distributions  * Currently, all other kernel configurations (development, etc) build with
273*5c2921b0SApple OSS Distributions  * KDEBUG_LEVEL_STANDARD.
274*5c2921b0SApple OSS Distributions  */
275*5c2921b0SApple OSS Distributions #endif
276*5c2921b0SApple OSS Distributions 
277*5c2921b0SApple OSS Distributions /*
278*5c2921b0SApple OSS Distributions  * KERNEL_DEBUG_CONSTANT_FILTERED events are omitted from tracing unless they
279*5c2921b0SApple OSS Distributions  * are explicitly requested in the typefilter.  They are not emitted when
280*5c2921b0SApple OSS Distributions  * tracing without a typefilter.
281*5c2921b0SApple OSS Distributions  */
282*5c2921b0SApple OSS Distributions #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
283*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_FILTERED(x, a, b, c, d, ...)           \
284*5c2921b0SApple OSS Distributions 	do {                                                             \
285*5c2921b0SApple OSS Distributions 	        if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) {   \
286*5c2921b0SApple OSS Distributions 	                kernel_debug_filtered((x), (uintptr_t)(a), (uintptr_t)(b),  \
287*5c2921b0SApple OSS Distributions 	                        (uintptr_t)(c), (uintptr_t)(d)); \
288*5c2921b0SApple OSS Distributions 	        }                                                            \
289*5c2921b0SApple OSS Distributions 	} while (0)
290*5c2921b0SApple OSS Distributions #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
291*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_FILTERED(type, x, a, b, c, d, ...) do {} while (0)
292*5c2921b0SApple OSS Distributions #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
293*5c2921b0SApple OSS Distributions 
294*5c2921b0SApple OSS Distributions #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
295*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_RELEASE_NOPROCFILT(x, a, b, c, d, ...)   \
296*5c2921b0SApple OSS Distributions 	do {                                                               \
297*5c2921b0SApple OSS Distributions 	        if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) {     \
298*5c2921b0SApple OSS Distributions 	                kernel_debug_flags((x), (uintptr_t)(a), (uintptr_t)(b),    \
299*5c2921b0SApple OSS Distributions 	                        (uintptr_t)(c), (uintptr_t)(d), KDBG_FLAG_NOPROCFILT); \
300*5c2921b0SApple OSS Distributions 	        }                                                              \
301*5c2921b0SApple OSS Distributions 	} while (0)
302*5c2921b0SApple OSS Distributions #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
303*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_RELEASE_NOPROCFILT(x, a, b, c, d, ...) \
304*5c2921b0SApple OSS Distributions 	do { } while (0)
305*5c2921b0SApple OSS Distributions #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
306*5c2921b0SApple OSS Distributions 
307*5c2921b0SApple OSS Distributions 
308*5c2921b0SApple OSS Distributions #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
309*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e)                               \
310*5c2921b0SApple OSS Distributions 	do {                                                                      \
311*5c2921b0SApple OSS Distributions 	        if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) {            \
312*5c2921b0SApple OSS Distributions 	                kernel_debug((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
313*5c2921b0SApple OSS Distributions 	                        (uintptr_t)(d),(uintptr_t)(e));                               \
314*5c2921b0SApple OSS Distributions 	        }                                                                     \
315*5c2921b0SApple OSS Distributions 	} while (0)
316*5c2921b0SApple OSS Distributions 
317*5c2921b0SApple OSS Distributions /*
318*5c2921b0SApple OSS Distributions  * DO NOT USE THIS MACRO -- it breaks fundamental assumptions about ktrace and
319*5c2921b0SApple OSS Distributions  * is only meant to be used by the pthread kext and other points in the kernel
320*5c2921b0SApple OSS Distributions  * where the thread ID must be provided explicitly.
321*5c2921b0SApple OSS Distributions  */
322*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT1(x, a, b, c, d, e)                               \
323*5c2921b0SApple OSS Distributions 	do {                                                                       \
324*5c2921b0SApple OSS Distributions 	        if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) {             \
325*5c2921b0SApple OSS Distributions 	                kernel_debug1((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
326*5c2921b0SApple OSS Distributions 	                (uintptr_t)(d), (uintptr_t)(e));                                   \
327*5c2921b0SApple OSS Distributions 	        }                                                                      \
328*5c2921b0SApple OSS Distributions 	} while (0)
329*5c2921b0SApple OSS Distributions 
330*5c2921b0SApple OSS Distributions #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
331*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e) do {} while (0)
332*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT1(x, a, b, c, d, e) do {} while (0)
333*5c2921b0SApple OSS Distributions #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
334*5c2921b0SApple OSS Distributions 
335*5c2921b0SApple OSS Distributions /*
336*5c2921b0SApple OSS Distributions  * KERNEL_DEBUG_CONSTANT_IST (in-system trace) events provide an audited subset
337*5c2921b0SApple OSS Distributions  * of tracepoints for userland system tracing tools.  This tracing level was
338*5c2921b0SApple OSS Distributions  * created by 8857227 to protect fairplayd and other PT_DENY_ATTACH processes.
339*5c2921b0SApple OSS Distributions  * It has two effects: only KERNEL_DEBUG_CONSTANT_IST() traces are emitted and
340*5c2921b0SApple OSS Distributions  * any PT_DENY_ATTACH processes will only emit basic traces as defined by the
341*5c2921b0SApple OSS Distributions  * kernel_debug_filter() routine.
342*5c2921b0SApple OSS Distributions  */
343*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_RELEASE(x, a, b, c, d, e) \
344*5c2921b0SApple OSS Distributions 	KERNEL_DEBUG_CONSTANT_IST(~KDEBUG_ENABLE_PPT, x, a, b, c, d, 0)
345*5c2921b0SApple OSS Distributions 
346*5c2921b0SApple OSS Distributions #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
347*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_IST(type, x, a, b, c, d, e)                     \
348*5c2921b0SApple OSS Distributions 	do {                                                                      \
349*5c2921b0SApple OSS Distributions 	        if (KDBG_IMPROBABLE(kdebug_enable & (type))) {                        \
350*5c2921b0SApple OSS Distributions 	                kernel_debug((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
351*5c2921b0SApple OSS Distributions 	                        (uintptr_t)(d), 0);                                           \
352*5c2921b0SApple OSS Distributions 	        }                                                                     \
353*5c2921b0SApple OSS Distributions 	} while (0)
354*5c2921b0SApple OSS Distributions 
355*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_IST1(x, a, b, c, d, e)                     \
356*5c2921b0SApple OSS Distributions 	do {                                                                       \
357*5c2921b0SApple OSS Distributions 	        if (KDBG_IMPROBABLE(kdebug_enable)) {                         \
358*5c2921b0SApple OSS Distributions 	                kernel_debug1((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
359*5c2921b0SApple OSS Distributions 	                        (uintptr_t)(d), (uintptr_t)(e));                               \
360*5c2921b0SApple OSS Distributions 	        }                                                                      \
361*5c2921b0SApple OSS Distributions 	} while (0)
362*5c2921b0SApple OSS Distributions 
363*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_EARLY(x, a, b, c, d)                                 \
364*5c2921b0SApple OSS Distributions 	do {                                                                  \
365*5c2921b0SApple OSS Distributions 	        kernel_debug_early((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
366*5c2921b0SApple OSS Distributions 	                (uintptr_t)(c), (uintptr_t)(d));                              \
367*5c2921b0SApple OSS Distributions 	} while (0)
368*5c2921b0SApple OSS Distributions 
369*5c2921b0SApple OSS Distributions #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
370*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_IST(type, x, a, b, c, d, e) do {} while (0)
371*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_IST1(x, a, b, c, d, e) do {} while (0)
372*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_EARLY(x, a, b, c, d) do {} while (0)
373*5c2921b0SApple OSS Distributions #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
374*5c2921b0SApple OSS Distributions 
375*5c2921b0SApple OSS Distributions #if NO_KDEBUG
376*5c2921b0SApple OSS Distributions #define __kdebug_constant_only __unused
377*5c2921b0SApple OSS Distributions #endif
378*5c2921b0SApple OSS Distributions 
379*5c2921b0SApple OSS Distributions /*
380*5c2921b0SApple OSS Distributions  * KERNEL_DEBUG events are only traced for DEBUG kernels.
381*5c2921b0SApple OSS Distributions  */
382*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG_CONSTANT_DEBUG(x, a, b, c, d, e) \
383*5c2921b0SApple OSS Distributions 	KERNEL_DEBUG(x, a, b, c, d, e)
384*5c2921b0SApple OSS Distributions 
385*5c2921b0SApple OSS Distributions #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL)
386*5c2921b0SApple OSS Distributions #define __kdebug_only
387*5c2921b0SApple OSS Distributions 
388*5c2921b0SApple OSS Distributions #undef KERNEL_DEBUG
389*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG(x, a, b, c, d, e)                                  \
390*5c2921b0SApple OSS Distributions 	do {                                                                \
391*5c2921b0SApple OSS Distributions 	        if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) {      \
392*5c2921b0SApple OSS Distributions 	                kernel_debug((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
393*5c2921b0SApple OSS Distributions 	                        (uintptr_t)(c), (uintptr_t)(d), (uintptr_t)(e));        \
394*5c2921b0SApple OSS Distributions 	        }                                                               \
395*5c2921b0SApple OSS Distributions 	} while (0)
396*5c2921b0SApple OSS Distributions 
397*5c2921b0SApple OSS Distributions /*
398*5c2921b0SApple OSS Distributions  * DO NOT USE THIS MACRO -- see warning above for KERNEL_DEBUG_CONSTANT1.
399*5c2921b0SApple OSS Distributions  */
400*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG1(x, a, b, c, d, e)                                  \
401*5c2921b0SApple OSS Distributions 	do {                                                                 \
402*5c2921b0SApple OSS Distributions 	        if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) {       \
403*5c2921b0SApple OSS Distributions 	                kernel_debug1((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
404*5c2921b0SApple OSS Distributions 	                        (uintptr_t)(c), (uintptr_t)(d), (uintptr_t)(e));         \
405*5c2921b0SApple OSS Distributions 	        }                                                                \
406*5c2921b0SApple OSS Distributions 	} while (0)
407*5c2921b0SApple OSS Distributions 
408*5c2921b0SApple OSS Distributions #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL) */
409*5c2921b0SApple OSS Distributions #define __kdebug_only __unused
410*5c2921b0SApple OSS Distributions 
411*5c2921b0SApple OSS Distributions #undef KERNEL_DEBUG
412*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG(x, a, b, c, d, e) do {} while (0)
413*5c2921b0SApple OSS Distributions #define KERNEL_DEBUG1(x, a, b, c, d, e) do {} while (0)
414*5c2921b0SApple OSS Distributions #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL) */
415*5c2921b0SApple OSS Distributions 
416*5c2921b0SApple OSS Distributions void kernel_debug(uint32_t debugid, uintptr_t arg1, uintptr_t arg2,
417*5c2921b0SApple OSS Distributions     uintptr_t arg3, uintptr_t arg4, uintptr_t arg5);
418*5c2921b0SApple OSS Distributions 
419*5c2921b0SApple OSS Distributions void kernel_debug1(uint32_t debugid, uintptr_t arg1, uintptr_t arg2,
420*5c2921b0SApple OSS Distributions     uintptr_t arg3, uintptr_t arg4, uintptr_t arg5);
421*5c2921b0SApple OSS Distributions 
422*5c2921b0SApple OSS Distributions #define KDBG_FLAG_FILTERED 0x01
423*5c2921b0SApple OSS Distributions #define KDBG_FLAG_NOPROCFILT 0x02
424*5c2921b0SApple OSS Distributions 
425*5c2921b0SApple OSS Distributions void kernel_debug_flags(uint32_t debugid, uintptr_t arg1, uintptr_t arg2,
426*5c2921b0SApple OSS Distributions     uintptr_t arg3, uintptr_t arg4, uint64_t flags);
427*5c2921b0SApple OSS Distributions 
428*5c2921b0SApple OSS Distributions void kernel_debug_filtered(uint32_t debugid, uintptr_t arg1, uintptr_t arg2,
429*5c2921b0SApple OSS Distributions     uintptr_t arg3, uintptr_t arg4);
430*5c2921b0SApple OSS Distributions 
431*5c2921b0SApple OSS Distributions #pragma mark - xnu API
432*5c2921b0SApple OSS Distributions 
433*5c2921b0SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
434*5c2921b0SApple OSS Distributions 
435*5c2921b0SApple OSS Distributions void kdebug_startup(void);
436*5c2921b0SApple OSS Distributions 
437*5c2921b0SApple OSS Distributions /* Used in early boot to log events. */
438*5c2921b0SApple OSS Distributions void kernel_debug_early(uint32_t  debugid, uintptr_t arg1, uintptr_t arg2,
439*5c2921b0SApple OSS Distributions     uintptr_t arg3, uintptr_t arg4);
440*5c2921b0SApple OSS Distributions /* Used in early boot to log strings spanning only a single tracepoint. */
441*5c2921b0SApple OSS Distributions void kernel_debug_string_early(const char *message);
442*5c2921b0SApple OSS Distributions /* Used to trace strings within kdebug tracepoints on arbitrary eventids. */
443*5c2921b0SApple OSS Distributions void kernel_debug_string_simple(uint32_t eventid, const char *str);
444*5c2921b0SApple OSS Distributions /* Only used by ktrace to reset kdebug.  ktrace_lock must be held. */
445*5c2921b0SApple OSS Distributions extern void kdebug_reset(void);
446*5c2921b0SApple OSS Distributions 
447*5c2921b0SApple OSS Distributions void kdbg_dump_trace_to_file(const char *, bool reenable);
448*5c2921b0SApple OSS Distributions 
449*5c2921b0SApple OSS Distributions enum kdebug_opts {
450*5c2921b0SApple OSS Distributions 	KDOPT_WRAPPING = 0x1,
451*5c2921b0SApple OSS Distributions 	KDOPT_ATBOOT = 0x2,
452*5c2921b0SApple OSS Distributions };
453*5c2921b0SApple OSS Distributions 
454*5c2921b0SApple OSS Distributions enum kdebug_mode {
455*5c2921b0SApple OSS Distributions 	KDEBUG_MODE_TRACE = 0x1, /* General purpose tracing.*/
456*5c2921b0SApple OSS Distributions 	KDEBUG_MODE_TRIAGE = 0x2, /* Collect more information to triage failures / gain insight into in-kernel operations of a thread.*/
457*5c2921b0SApple OSS Distributions };
458*5c2921b0SApple OSS Distributions 
459*5c2921b0SApple OSS Distributions 
460*5c2921b0SApple OSS Distributions int kdbg_bootstrap(bool early_trace, int mode);
461*5c2921b0SApple OSS Distributions void kdebug_init(unsigned int n_events, char *filterdesc,
462*5c2921b0SApple OSS Distributions     enum kdebug_opts opts);
463*5c2921b0SApple OSS Distributions void kdebug_trace_start(unsigned int n_events, const char *filterdesc,
464*5c2921b0SApple OSS Distributions     enum kdebug_opts opts);
465*5c2921b0SApple OSS Distributions uint64_t kdebug_wake(void);
466*5c2921b0SApple OSS Distributions void kdebug_free_early_buf(void);
467*5c2921b0SApple OSS Distributions 
468*5c2921b0SApple OSS Distributions 
469*5c2921b0SApple OSS Distributions struct proc;
470*5c2921b0SApple OSS Distributions void kdbg_trace_data(struct proc *proc, long *arg_pid, long *arg_uniqueid);
471*5c2921b0SApple OSS Distributions 
472*5c2921b0SApple OSS Distributions #define KDBG_VFS_LOOKUP_FLAG_LOOKUP 0x01
473*5c2921b0SApple OSS Distributions #define KDBG_VFS_LOOKUP_FLAG_NOPROCFILT 0x02
474*5c2921b0SApple OSS Distributions void kdebug_vfs_lookup(unsigned long *path_words, int path_len, void *vnp,
475*5c2921b0SApple OSS Distributions     uint32_t flags);
476*5c2921b0SApple OSS Distributions 
477*5c2921b0SApple OSS Distributions void ktriage_extract(uint64_t thread_id, void *buf, uint32_t bufsz);
478*5c2921b0SApple OSS Distributions 
479*5c2921b0SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
480*5c2921b0SApple OSS Distributions 
481*5c2921b0SApple OSS Distributions #ifdef KERNEL_PRIVATE
482*5c2921b0SApple OSS Distributions 
483*5c2921b0SApple OSS Distributions typedef struct ktriage_strings {
484*5c2921b0SApple OSS Distributions 	int num_strings;
485*5c2921b0SApple OSS Distributions 	const char **strings;
486*5c2921b0SApple OSS Distributions } ktriage_strings_t;
487*5c2921b0SApple OSS Distributions 
488*5c2921b0SApple OSS Distributions void ktriage_record(uint64_t thread_id, uint64_t debugid, uintptr_t arg1);
489*5c2921b0SApple OSS Distributions 
490*5c2921b0SApple OSS Distributions #define NUMPARMS 23
491*5c2921b0SApple OSS Distributions void kdebug_lookup_gen_events(long *path_words, int path_len, void *vnp,
492*5c2921b0SApple OSS Distributions     bool lookup);
493*5c2921b0SApple OSS Distributions 
494*5c2921b0SApple OSS Distributions #pragma mark - EnergyTracing
495*5c2921b0SApple OSS Distributions 
496*5c2921b0SApple OSS Distributions #define KERNEL_DBG_IST_SANE KDBG_RELEASE
497*5c2921b0SApple OSS Distributions #define ENTR_KDTRACEFUNC KDBG_RELEASE
498*5c2921b0SApple OSS Distributions 
499*5c2921b0SApple OSS Distributions // value is int64_t, quality is uint32_t
500*5c2921b0SApple OSS Distributions #define KERNEL_ENERGYTRACE(opcode, lifespan, id, quality, value)        \
501*5c2921b0SApple OSS Distributions 	    ENTR_KDTRACE(kEnTrCompKernel, opcode, lifespan, id,         \
502*5c2921b0SApple OSS Distributions 	                 quality, value)
503*5c2921b0SApple OSS Distributions #define KERNEL_ENTR_ASSOCIATE(par_opcode, par_act_id, sub_opcode, sub_act_id) \
504*5c2921b0SApple OSS Distributions 	    ENTR_KDASSOCIATE(kEnTrCompKernel, par_opcode, par_act_id,   \
505*5c2921b0SApple OSS Distributions 	                     kEnTrCompKernel, sub_opcode, sub_act_id)
506*5c2921b0SApple OSS Distributions 
507*5c2921b0SApple OSS Distributions #endif /* KERNEL_PRIVATE */
508*5c2921b0SApple OSS Distributions 
509*5c2921b0SApple OSS Distributions #endif /* KERNEL */
510*5c2921b0SApple OSS Distributions 
511*5c2921b0SApple OSS Distributions __END_DECLS
512*5c2921b0SApple OSS Distributions 
513*5c2921b0SApple OSS Distributions #endif /* !defined(BSD_SYS_KDEBUG_KERNEL_H) */
514