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