2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef BSD_SYS_KDEBUG_KERNEL_H
30 #define BSD_SYS_KDEBUG_KERNEL_H
32 #include <mach/boolean.h>
33 #include <mach/clock_types.h>
36 #include <sys/cdefs.h>
43 * To use kdebug in the kernel:
45 * #include <sys/kdebug_kernel.h>
47 * #define DBG_NETIPINIT NETDBG_CODE(DBG_NETIP, 1)
52 * KDBG(DBG_NETIPINIT | DBG_FUNC_START, 1, 2, 3, 4);
54 * KDBG(DBG_NETIPINIT);
56 * KDBG(DBG_NETIPINIT | DBG_FUNC_END);
60 #pragma mark - kernel tracepoints
63 * The KDBG{,_DEBUG,_RELEASE,_FILTERED} macros are the preferred method of
66 * Kernel pointers must be unslid or permuted using VM_KERNEL_UNSLIDE_OR_PERM.
67 * Do not trace any sensitive data.
71 * Traced on debug and development (and release macOS) kernels.
73 #define KDBG(x, ...) KDBG_(, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
76 * Traced on debug and development (and release macOS) kernels if explicitly
77 * requested. Omitted from tracing without a typefilter.
79 #define KDBG_FILTERED(x, ...) KDBG_(_FILTERED, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
84 * Traced on debug and development (and release macOS) kernels, even if the
85 * process filter would reject it.
87 #define KDBG_RELEASE_NOPROCFILT(x, ...) \
88 KDBG_(_RELEASE_NOPROCFILT, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
90 #endif /* KERNEL_PRIVATE */
93 * Traced on debug, development, and release kernels.
95 * Only use this tracepoint if the events are required for a shipping trace
98 #define KDBG_RELEASE(x, ...) KDBG_(_RELEASE, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
101 * Traced only on debug kernels.
103 #define KDBG_DEBUG(x, ...) KDBG_(_DEBUG, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
105 #pragma mark - kernel API
107 #ifdef KERNEL_PRIVATE
110 * kernel_debug_string provides the same functionality as the
111 * kdebug_trace_string syscall as a KPI. str_id is an in/out
112 * parameter that, if it's pointing to a string ID of 0, will
113 * receive a generated ID. If it provides a value in str_id,
114 * then that will be used, instead.
116 * Returns an errno indicating the type of failure.
118 int kernel_debug_string(uint32_t debugid
, uint64_t *str_id
, const char *str
);
121 * kernel_debug_disable disables event logging, but leaves any buffers
124 void kernel_debug_disable(void);
126 #endif /* KERNEL_PRIVATE */
129 * Returns true if kdebug is using continuous time for its events, and false
132 bool kdebug_using_continuous_time(void);
135 * Returns true if kdebug will log an event with the provided debugid, and
138 bool kdebug_debugid_enabled(uint32_t debugid
);
141 * Returns true only if the debugid is explicitly enabled by filters. Returns
142 * false otherwise, including when no filters are active.
144 bool kdebug_debugid_explicitly_enabled(uint32_t debugid
);
146 uint32_t kdebug_commpage_state(void);
148 #pragma mark - IOP tracing
151 * Definitions to support IOP tracing.
155 /* Trace is now enabled; no arguments. */
156 KD_CALLBACK_KDEBUG_ENABLED
,
157 /* Trace is now disabled; no arguments. */
158 KD_CALLBACK_KDEBUG_DISABLED
,
160 * Request the latest entries from the IOP and block until complete; no
163 KD_CALLBACK_SYNC_FLUSH
,
165 * The typefilter is enabled; a read-only pointer to the typefilter is
166 * provided, valid only while in the callback.
168 KD_CALLBACK_TYPEFILTER_CHANGED
,
171 typedef void (*kd_callback_fn
) (void *context
, kd_callback_type reason
,
177 /* name of IOP, NUL-terminated */
181 typedef struct kd_callback kd_callback_t
;
184 * Registers an IOP for participation in tracing.
186 * The registered callback function will be called with the
187 * supplied context as the first argument, followed by a
188 * kd_callback_type and an associated void* argument.
190 * The return value is a nonzero coreid that shall be used in
191 * kernel_debug_enter() to refer to your IOP. If the allocation
192 * failed, then 0 will be returned.
195 * Note that not all callback calls will indicate a change in
196 * state (e.g. disabling trace twice would send two disable
199 int kernel_debug_register_callback(kd_callback_t callback
);
201 void kernel_debug_enter(uint32_t coreid
, uint32_t debugid
, uint64_t timestamp
,
202 uintptr_t arg1
, uintptr_t arg2
, uintptr_t arg3
, uintptr_t arg4
,
205 #pragma mark - internals
207 #define KDBG_(f, x, a, b, c, d, n, ...) KDBG##n(f, x, a, b, c, d)
208 #define KDBG0(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, 0, 0, 0, 0, 0)
209 #define KDBG1(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, 0, 0, 0, 0)
210 #define KDBG2(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, 0, 0, 0)
211 #define KDBG3(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, c, 0, 0)
212 #define KDBG4(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, c, d, 0)
214 #ifdef XNU_KERNEL_PRIVATE
215 #define KDBG_IMPROBABLE __improbable
217 #define KDBG_IMPROBABLE
220 extern unsigned int kdebug_enable
;
223 * The kernel debug configuration level. These values control which events are
224 * compiled in under different build configurations.
226 * Infer the supported kernel debug event level from config option. Use
227 * (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) as a guard to protect unaudited debug
230 #define KDEBUG_LEVEL_NONE 0
231 #define KDEBUG_LEVEL_IST 1
232 #define KDEBUG_LEVEL_STANDARD 2
233 #define KDEBUG_LEVEL_FULL 3
236 #define KDEBUG_LEVEL KDEBUG_LEVEL_NONE
238 #define KDEBUG_LEVEL KDEBUG_LEVEL_IST
240 #define KDEBUG_LEVEL KDEBUG_LEVEL_FULL
242 #define KDEBUG_LEVEL KDEBUG_LEVEL_STANDARD
244 * Currently, all other kernel configurations (development, etc) build with
245 * KDEBUG_LEVEL_STANDARD.
250 * KERNEL_DEBUG_CONSTANT_FILTERED events are omitted from tracing unless they
251 * are explicitly requested in the typefilter. They are not emitted when
252 * tracing without a typefilter.
254 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
255 #define KERNEL_DEBUG_CONSTANT_FILTERED(x, a, b, c, d, ...) \
257 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
258 kernel_debug_filtered((x), (uintptr_t)(a), (uintptr_t)(b), \
259 (uintptr_t)(c), (uintptr_t)(d)); \
262 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
263 #define KERNEL_DEBUG_CONSTANT_FILTERED(type, x, a, b, c, d, ...) do {} while (0)
264 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
266 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
267 #define KERNEL_DEBUG_CONSTANT_RELEASE_NOPROCFILT(x, a, b, c, d, ...) \
269 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
270 kernel_debug_flags((x), (uintptr_t)(a), (uintptr_t)(b), \
271 (uintptr_t)(c), (uintptr_t)(d), KDBG_FLAG_NOPROCFILT); \
274 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
275 #define KERNEL_DEBUG_CONSTANT_RELEASE_NOPROCFILT(x, a, b, c, d, ...) \
277 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
280 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
281 #define KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e) \
283 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
284 kernel_debug((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
285 (uintptr_t)(d),(uintptr_t)(e)); \
290 * DO NOT USE THIS MACRO -- it breaks fundamental assumptions about ktrace and
291 * is only meant to be used by the pthread kext and other points in the kernel
292 * where the thread ID must be provided explicitly.
294 #define KERNEL_DEBUG_CONSTANT1(x, a, b, c, d, e) \
296 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
297 kernel_debug1((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
298 (uintptr_t)(d), (uintptr_t)(e)); \
302 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
303 #define KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e) do {} while (0)
304 #define KERNEL_DEBUG_CONSTANT1(x, a, b, c, d, e) do {} while (0)
305 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
308 * KERNEL_DEBUG_CONSTANT_IST (in-system trace) events provide an audited subset
309 * of tracepoints for userland system tracing tools. This tracing level was
310 * created by 8857227 to protect fairplayd and other PT_DENY_ATTACH processes.
311 * It has two effects: only KERNEL_DEBUG_CONSTANT_IST() traces are emitted and
312 * any PT_DENY_ATTACH processes will only emit basic traces as defined by the
313 * kernel_debug_filter() routine.
315 #define KERNEL_DEBUG_CONSTANT_RELEASE(x, a, b, c, d, e) \
316 KERNEL_DEBUG_CONSTANT_IST(~KDEBUG_ENABLE_PPT, x, a, b, c, d, 0)
318 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
319 #define KERNEL_DEBUG_CONSTANT_IST(type, x, a, b, c, d, e) \
321 if (KDBG_IMPROBABLE(kdebug_enable & (type))) { \
322 kernel_debug((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
323 (uintptr_t)(d), 0); \
327 #define KERNEL_DEBUG_CONSTANT_IST1(x, a, b, c, d, e) \
329 if (KDBG_IMPROBABLE(kdebug_enable)) { \
330 kernel_debug1((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
331 (uintptr_t)(d), (uintptr_t)(e)); \
335 #define KERNEL_DEBUG_EARLY(x, a, b, c, d) \
337 kernel_debug_early((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
338 (uintptr_t)(c), (uintptr_t)(d)); \
341 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
342 #define KERNEL_DEBUG_CONSTANT_IST(type, x, a, b, c, d, e) do {} while (0)
343 #define KERNEL_DEBUG_CONSTANT_IST1(x, a, b, c, d, e) do {} while (0)
344 #define KERNEL_DEBUG_EARLY(x, a, b, c, d) do {} while (0)
345 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
348 #define __kdebug_constant_only __unused
352 * KERNEL_DEBUG events are only traced for DEBUG kernels.
354 #define KERNEL_DEBUG_CONSTANT_DEBUG(x, a, b, c, d, e) \
355 KERNEL_DEBUG(x, a, b, c, d, e)
357 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL)
358 #define __kdebug_only
361 #define KERNEL_DEBUG(x, a, b, c, d, e) \
363 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
364 kernel_debug((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
365 (uintptr_t)(c), (uintptr_t)(d), (uintptr_t)(e)); \
370 * DO NOT USE THIS MACRO -- see warning above for KERNEL_DEBUG_CONSTANT1.
372 #define KERNEL_DEBUG1(x, a, b, c, d, e) \
374 if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
375 kernel_debug1((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
376 (uintptr_t)(c), (uintptr_t)(d), (uintptr_t)(e)); \
380 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL) */
381 #define __kdebug_only __unused
384 #define KERNEL_DEBUG(x, a, b, c, d, e) do {} while (0)
385 #define KERNEL_DEBUG1(x, a, b, c, d, e) do {} while (0)
386 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL) */
388 void kernel_debug(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
389 uintptr_t arg3
, uintptr_t arg4
, uintptr_t arg5
);
391 void kernel_debug1(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
392 uintptr_t arg3
, uintptr_t arg4
, uintptr_t arg5
);
394 #define KDBG_FLAG_FILTERED 0x01
395 #define KDBG_FLAG_NOPROCFILT 0x02
397 void kernel_debug_flags(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
398 uintptr_t arg3
, uintptr_t arg4
, uint64_t flags
);
400 void kernel_debug_filtered(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
401 uintptr_t arg3
, uintptr_t arg4
);
403 #pragma mark - xnu API
405 #ifdef XNU_KERNEL_PRIVATE
407 /* Used in early boot to log events. */
408 void kernel_debug_early(uint32_t debugid
, uintptr_t arg1
, uintptr_t arg2
,
409 uintptr_t arg3
, uintptr_t arg4
);
410 /* Used in early boot to log strings spanning only a single tracepoint. */
411 void kernel_debug_string_early(const char *message
);
412 /* Used to trace strings within kdebug tracepoints on arbitrary eventids. */
413 void kernel_debug_string_simple(uint32_t eventid
, const char *str
);
414 /* Only used by ktrace to reset kdebug. ktrace_lock must be held. */
415 extern void kdebug_reset(void);
417 void kdbg_dump_trace_to_file(const char *);
420 KDOPT_WRAPPING
= 0x1,
424 void kdebug_init(unsigned int n_events
, char *filterdesc
,
425 enum kdebug_opts opts
);
426 void kdebug_trace_start(unsigned int n_events
, const char *filterdesc
,
427 enum kdebug_opts opts
);
428 uint64_t kdebug_wake(void);
429 void kdebug_free_early_buf(void);
430 void release_storage_unit(int cpu
, uint32_t storage_unit
);
431 bool allocate_storage_unit(int cpu
);
434 void kdbg_trace_data(struct proc
*proc
, long *arg_pid
, long *arg_uniqueid
);
435 void kdbg_trace_string(struct proc
*proc
, long *arg1
, long *arg2
, long *arg3
,
438 #define KDBG_VFS_LOOKUP_FLAG_LOOKUP 0x01
439 #define KDBG_VFS_LOOKUP_FLAG_NOPROCFILT 0x02
440 void kdebug_vfs_lookup(unsigned long *path_words
, int path_len
, void *vnp
,
443 #endif /* XNU_KERNEL_PRIVATE */
445 #ifdef KERNEL_PRIVATE
448 void kdebug_lookup_gen_events(long *path_words
, int path_len
, void *vnp
,
451 #pragma mark - EnergyTracing
453 #define KERNEL_DBG_IST_SANE KDBG_RELEASE
454 #define ENTR_KDTRACEFUNC KDBG_RELEASE
456 // value is int64_t, quality is uint32_t
457 #define KERNEL_ENERGYTRACE(opcode, lifespan, id, quality, value) \
458 ENTR_KDTRACE(kEnTrCompKernel, opcode, lifespan, id, \
460 #define KERNEL_ENTR_ASSOCIATE(par_opcode, par_act_id, sub_opcode, sub_act_id) \
461 ENTR_KDASSOCIATE(kEnTrCompKernel, par_opcode, par_act_id, \
462 kEnTrCompKernel, sub_opcode, sub_act_id)
464 #endif /* KERNEL_PRIVATE */
470 #endif /* !defined(BSD_SYS_KDEBUG_KERNEL_H) */