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