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