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