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