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