-/*
- * To use kdebug in the kernel:
- *
- * #include <sys/kdebug.h>
- *
- * #define DBG_NETIPINIT NETDBG_CODE(DBG_NETIP, 1)
- *
- * void
- * ip_init(void)
- * {
- * KDBG(DBG_NETIPINIT | DBG_FUNC_START, 1, 2, 3, 4);
- * ...
- * KDBG(DBG_NETIPINIT);
- * ...
- * KDBG(DBG_NETIPINIT | DBG_FUNC_END);
- * }
- */
-
-#ifdef KERNEL_PRIVATE
-
-/*
- * The KDBG{,_DEBUG,_RELEASE,_FILTERED} macros are the preferred method of
- * making tracepoints.
- *
- * Kernel pointers must be unslid or permuted using VM_KERNEL_UNSLIDE_OR_PERM.
- * Do not trace any sensitive data.
- */
-
-/*
- * Traced on debug and development (and release macOS) kernels.
- */
-#define KDBG(x, ...) KDBG_(, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
-
-/*
- * Traced on debug and development (and release macOS) kernels if explicitly
- * requested. Omitted from tracing without a typefilter.
- */
-#define KDBG_FILTERED(x, ...) KDBG_(_FILTERED, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
-
-/*
- * Traced on debug and development (and release macOS) kernels, even if the
- * process filter would reject it.
- */
-#define KDBG_RELEASE_NOPROCFILT(x, ...) \
- KDBG_(_RELEASE_NOPROCFILT, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
-
-/*
- * Traced on debug, development, and release kernels.
- *
- * Only use this tracepoint if the events are required for a shipping trace
- * tool.
- */
-#define KDBG_RELEASE(x, ...) KDBG_(_RELEASE, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
-
-/*
- * Traced only on debug kernels.
- */
-#define KDBG_DEBUG(x, ...) KDBG_(_DEBUG, x, ## __VA_ARGS__, 4, 3, 2, 1, 0)
-
-#define KDBG_(f, x, a, b, c, d, n, ...) KDBG##n(f, x, a, b, c, d)
-#define KDBG0(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, 0, 0, 0, 0, 0)
-#define KDBG1(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, 0, 0, 0, 0)
-#define KDBG2(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, 0, 0, 0)
-#define KDBG3(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, c, 0, 0)
-#define KDBG4(f, x, a, b, c, d) KERNEL_DEBUG_CONSTANT##f(x, a, b, c, d, 0)
-
-#endif /* defined(KERNEL_PRIVATE) */
-
-extern unsigned int kdebug_enable;
-
-/*
- * Bits used by kdebug_enable. These control which events are traced at
- * runtime.
- */
-#define KDEBUG_ENABLE_TRACE (1U << 0)
-#define KDEBUG_ENABLE_ENTROPY (1U << 1) /* obsolete */
-#define KDEBUG_ENABLE_CHUD (1U << 2) /* obsolete */
-#define KDEBUG_ENABLE_PPT (1U << 3)
-#define KDEBUG_ENABLE_SERIAL (1U << 4)
-
-#define KDEBUG_TRACE (KDEBUG_ENABLE_TRACE)
-
-/*
- * Specify KDEBUG_PPT to indicate that the event belongs to the limited PPT set.
- * PPT is deprecated -- use a typefilter and the PPTDBG class instead.
- */
-#define KDEBUG_PPT (KDEBUG_ENABLE_PPT)
-#define KDEBUG_COMMON (KDEBUG_ENABLE_TRACE | KDEBUG_ENABLE_PPT)
-
-/*
- * The kernel debug configuration level. These values control which events are
- * compiled in under different build configurations.
- *
- * Infer the supported kernel debug event level from config option. Use
- * (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) as a guard to protect unaudited debug
- * code.
- */
-#define KDEBUG_LEVEL_NONE 0
-#define KDEBUG_LEVEL_IST 1
-#define KDEBUG_LEVEL_STANDARD 2
-#define KDEBUG_LEVEL_FULL 3
-
-#if NO_KDEBUG
-#define KDEBUG_LEVEL KDEBUG_LEVEL_NONE
-#elif IST_KDEBUG
-#define KDEBUG_LEVEL KDEBUG_LEVEL_IST
- // currently configured for the iOS release kernel
-#elif KDEBUG
-#define KDEBUG_LEVEL KDEBUG_LEVEL_FULL
-#else
-#define KDEBUG_LEVEL KDEBUG_LEVEL_STANDARD
-/*
- * Currently, all other kernel configurations (development, etc) build with
- * KDEBUG_LEVEL_STANDARD. As a result, KERNEL_DEBUG_CONSTANT*() are on by
- * default but KERNEL_DEBUG*() are not.
- */
-#endif
-
-#ifdef XNU_KERNEL_PRIVATE
-#define KDBG_IMPROBABLE __improbable
-#else
-#define KDBG_IMPROBABLE
-#endif
-
-/*
- * KERNEL_DEBUG_CONSTANT_FILTERED events are omitted from tracing unless they
- * are explicitly requested in the typefilter. They are not emitted when
- * tracing without a typefilter.
- */
-#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
-#define KERNEL_DEBUG_CONSTANT_FILTERED(x, a, b, c, d, ...) \
- do { \
- if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
- kernel_debug_filtered((x), (uintptr_t)(a), (uintptr_t)(b), \
- (uintptr_t)(c), (uintptr_t)(d)); \
- } \
- } while (0)
-#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
-#define KERNEL_DEBUG_CONSTANT_FILTERED(type, x, a, b, c, d, ...) do {} while (0)
-#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
-
-#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
-#define KERNEL_DEBUG_CONSTANT_RELEASE_NOPROCFILT(x, a, b, c, d, ...) \
- do { \
- if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
- kernel_debug_flags((x), (uintptr_t)(a), (uintptr_t)(b), \
- (uintptr_t)(c), (uintptr_t)(d), KDBG_FLAG_NOPROCFILT); \
- } \
- } while (0)
-#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
-#define KERNEL_DEBUG_CONSTANT_RELEASE_NOPROCFILT(x, a, b, c, d, ...) \
- do { } while (0)
-#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
-
-
-#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
-#define KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e) \
- do { \
- if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
- kernel_debug((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
- (uintptr_t)(d),(uintptr_t)(e)); \
- } \
- } while (0)
-
-/*
- * DO NOT USE THIS MACRO -- it breaks fundamental assumptions about ktrace and
- * is only meant to be used by the pthread kext and other points in the kernel
- * where the thread ID must be provided explicitly.
- */
-#define KERNEL_DEBUG_CONSTANT1(x, a, b, c, d, e) \
- do { \
- if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
- kernel_debug1((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
- (uintptr_t)(d), (uintptr_t)(e)); \
- } \
- } while (0)
-
-#define KERNEL_DEBUG_EARLY(x, a, b, c, d) \
- do { \
- kernel_debug_early((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
- (uintptr_t)(c), (uintptr_t)(d)); \
- } while (0)
-#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
-#define KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e) do {} while (0)
-#define KERNEL_DEBUG_CONSTANT1(x, a, b, c, d, e) do {} while (0)
-#define KERNEL_DEBUG_EARLY(x, a, b, c, d) do {} while (0)
-#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */
-
-/*
- * KERNEL_DEBUG_CONSTANT_IST (in-system trace) events provide an audited subset
- * of tracepoints for userland system tracing tools. This tracing level was
- * created by 8857227 to protect fairplayd and other PT_DENY_ATTACH processes.
- * It has two effects: only KERNEL_DEBUG_CONSTANT_IST() traces are emitted and
- * any PT_DENY_ATTACH processes will only emit basic traces as defined by the
- * kernel_debug_filter() routine.
- */
-#define KERNEL_DEBUG_CONSTANT_RELEASE(x, a, b, c, d, e) \
- KERNEL_DEBUG_CONSTANT_IST(~KDEBUG_ENABLE_PPT, x, a, b, c, d, 0)
-
-#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
-#define KERNEL_DEBUG_CONSTANT_IST(type, x, a, b, c, d, e) \
- do { \
- if (KDBG_IMPROBABLE(kdebug_enable & (type))) { \
- kernel_debug((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
- (uintptr_t)(d), 0); \
- } \
- } while (0)
-#define KERNEL_DEBUG_CONSTANT_IST1(x, a, b, c, d, e) \
- do { \
- if (KDBG_IMPROBABLE(kdebug_enable)) { \
- kernel_debug1((x), (uintptr_t)(a), (uintptr_t)(b), (uintptr_t)(c), \
- (uintptr_t)(d), (uintptr_t)(e)); \
- } \
- } while (0)
-#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
-#define KERNEL_DEBUG_CONSTANT_IST(type, x, a, b, c, d, e) do {} while (0)
-#define KERNEL_DEBUG_CONSTANT_IST1(x, a, b, c, d, e) do {} while (0)
-#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
-
-#if NO_KDEBUG
-#define __kdebug_constant_only __unused
-#endif
-
-/*
- * KERNEL_DEBUG events are only traced for DEBUG kernels.
- */
-#define KERNEL_DEBUG_CONSTANT_DEBUG(x, a, b, c, d, e) \
- KERNEL_DEBUG(x, a, b, c, d, e)
-
-#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL)
-#define __kdebug_only
-
-#define KERNEL_DEBUG(x, a, b, c, d, e) \
- do { \
- if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
- kernel_debug((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
- (uintptr_t)(c), (uintptr_t)(d), (uintptr_t)(e)); \
- } \
- } while (0)
-
-/*
- * DO NOT USE THIS MACRO -- see warning above for KERNEL_DEBUG_CONSTANT1.
- */
-#define KERNEL_DEBUG1(x, a, b, c, d, e) \
- do { \
- if (KDBG_IMPROBABLE(kdebug_enable & ~KDEBUG_ENABLE_PPT)) { \
- kernel_debug1((uint32_t)(x), (uintptr_t)(a), (uintptr_t)(b), \
- (uintptr_t)(c), (uintptr_t)(d), (uintptr_t)(e)); \
- } \
- } while (0)
-
-#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL) */
-#define __kdebug_only __unused
-
-#define KERNEL_DEBUG(x,a,b,c,d,e) do {} while (0)
-#define KERNEL_DEBUG1(x,a,b,c,d,e) do {} while (0)
-#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_FULL) */
-
-
-extern void kernel_debug(
- uint32_t debugid,
- uintptr_t arg1,
- uintptr_t arg2,
- uintptr_t arg3,
- uintptr_t arg4,
- uintptr_t arg5);
-
-extern void kernel_debug1(
- uint32_t debugid,
- uintptr_t arg1,
- uintptr_t arg2,
- uintptr_t arg3,
- uintptr_t arg4,
- uintptr_t arg5);
-
-#define KDBG_FLAG_FILTERED 0x01
-#define KDBG_FLAG_NOPROCFILT 0x02
-
-extern void kernel_debug_flags(
- uint32_t debugid,
- uintptr_t arg1,
- uintptr_t arg2,
- uintptr_t arg3,
- uintptr_t arg4,
- uint64_t flags);
-
-extern void kernel_debug_filtered(
- uint32_t debugid,
- uintptr_t arg1,
- uintptr_t arg2,
- uintptr_t arg3,
- uintptr_t arg4);
-
-extern void kernel_debug_early(
- uint32_t debugid,
- uintptr_t arg1,
- uintptr_t arg2,
- uintptr_t arg3,
- uintptr_t arg4);
-
-/*
- * EnergyTracing macros.
- */
-
-#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
-// whether to bother calculating EnergyTracing inputs
-// could change in future to see if DBG_ENERGYTRACE is active
-#define ENTR_SHOULDTRACE kdebug_enable
-// encode logical EnergyTracing into 32/64 KDebug trace
-#define ENTR_KDTRACE(component, opcode, lifespan, id, quality, value) \
-do { \
- uint32_t kdcode__; \
- uintptr_t highval__, lowval__, mask__ = 0xffffffff; \
- kdcode__ = KDBG_CODE(DBG_ENERGYTRACE,component,opcode)|(lifespan); \
- highval__ = ((value) >> 32) & mask__; \
- lowval__ = (value) & mask__; \
- ENTR_KDTRACEFUNC(kdcode__, id, quality, highval__, lowval__); \
-} while(0)
-
-/*
- Trace the association of two existing activations.
-
- An association is traced as a modification to the parent activation.
- In order to fit the sub-activation's component, activation code, and
- activation ID into a kdebug tracepoint, the arguments that would hold
- the value are left separate, and one stores the component and opcode
- of the sub-activation, while the other stores the pointer-sized
- activation ID.
-
- arg2 arg3 arg4
- +-----------------+ +~+----+----+--------+ +----------+
- |kEnTrModAssociate| | | | | | | |
- +-----------------+ +~+----+----+--------+ +----------+
- 8-bits unused sub-activation ID
- 8-bit sub-component
- 16-bit sub-opcode
-
-*/
-#define kEnTrModAssociate (1 << 28)
-#define ENTR_KDASSOCIATE(par_comp, par_opcode, par_act_id, \
- sub_comp, sub_opcode, sub_act_id) \
-do { \
- unsigned sub_compcode = ((unsigned)sub_comp << 16) | sub_opcode; \
- ENTR_KDTRACEFUNC(KDBG_CODE(DBG_ENERGYTRACE,par_comp,par_opcode), \
- par_act_id, kEnTrModAssociate, sub_compcode, \
- sub_act_id); \
-} while(0)
-
-#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
-
-#define ENTR_SHOULDTRACE FALSE
-#define ENTR_KDTRACE(component, opcode, lifespan, id, quality, value) \
- do {} while (0)
-#define ENTR_KDASSOCIATE(par_comp, par_opcode, par_act_id, \
- sub_comp, sub_opcode, sub_act_id) \
- do {} while (0)
-
-#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
-
-#ifdef KERNEL_PRIVATE
-/*
- * kernel_debug_string provides the same functionality as the
- * kdebug_trace_string syscall as a KPI. str_id is an in/out
- * parameter that, if it's pointing to a string ID of 0, will
- * receive a generated ID. If it provides a value in str_id,
- * then that will be used, instead.
- *
- * Returns an errno indicating the type of failure.
- */
-extern int
-kernel_debug_string(uint32_t debugid, uint64_t *str_id, const char *str);
-
-/*
- * kernel_debug_disable disables event logging, but leaves any buffers
- * intact.
- */
-extern void kernel_debug_disable(void);
-#endif
-
-/*
- * Bits set in the comm page for kdebug.
- */
-#define KDEBUG_COMMPAGE_ENABLE_TRACE 0x1
-#define KDEBUG_COMMPAGE_ENABLE_TYPEFILTER 0x2 /* Forced to false if ENABLE_TRACE is 0 */
-
-// for EnergyTracing user space & clients
-#define kEnTrCompKernel 2
-
-/*
- EnergyTracing opcodes
-
- Activations use DBG_FUNC_START/END.
- Events are DBG_FUNC_NONE.
- */
-
-/* Socket reads and writes are uniquely identified by the (sanitized)
- pointer to the socket struct in question. To associate this address
- with the user space file descriptor, we have a socket activation with
- the FD as its identifier and the socket struct pointer as its value.
-*/
-#define kEnTrActKernSocket 1
-#define kEnTrActKernSockRead 2
-#define kEnTrActKernSockWrite 3
-
-#define kEnTrActKernPoll 10
-#define kEnTrActKernSelect 11
-#define kEnTrActKernKQWait 12
-
-// events
-#define kEnTrEvUnblocked 256
-
-// EnergyTracing flags (the low-order 16 bits of 'quality')
-#define kEnTrFlagNonBlocking 1 << 0
-#define kEnTrFlagNoWork 1 << 1
-
-// and now the internal mechanism
-#ifdef KERNEL_PRIVATE
-
-// 20452597 requests that the trace macros not take an argument it throws away
-#define KERNEL_DBG_IST_SANE(x, a, b, c, d) \
- KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, x, a, b, c, d, \
- 0 /*__unused in kernel_debug()*/)
-#define ENTR_KDTRACEFUNC KERNEL_DBG_IST_SANE
-
-// value is int64_t, quality is uint32_t
-#define KERNEL_ENERGYTRACE(opcode, lifespan, id, quality, value) \
- ENTR_KDTRACE(kEnTrCompKernel, opcode, lifespan, id, \
- quality, value)
-#define KERNEL_ENTR_ASSOCIATE(par_opcode, par_act_id, sub_opcode, sub_act_id) \
- ENTR_KDASSOCIATE(kEnTrCompKernel, par_opcode, par_act_id, \
- kEnTrCompKernel, sub_opcode, sub_act_id)
-
-// end EnergyTracing
-
-
-#include <mach/boolean.h>
-
-#define NUMPARMS 23
-
-struct proc;
-
-/*
- * Returns false if the debugid is disabled by filters, and true if the
- * debugid is allowed to be traced. A debugid may not be traced if the
- * typefilter disables its class and subclass, it's outside a range
- * check, or if it's not an allowed debugid in a value check. Trace
- * system events bypass this check.
- */
-boolean_t kdebug_debugid_enabled(uint32_t debugid);
-
-/*
- * Returns true only if the debugid is explicitly enabled by filters. Returns
- * false otherwise, including when no filters are active.
- */
-boolean_t kdebug_debugid_explicitly_enabled(uint32_t debugid);
-
-uint32_t kdebug_commpage_state(void);
-
-#define KDBG_VFS_LOOKUP_FLAG_LOOKUP 0x01
-#define KDBG_VFS_LOOKUP_FLAG_NOPROCFILT 0x02
-void kdebug_vfs_lookup(long *dbg_parms, int dbg_namelen, void *dp,
- uint32_t flags);
-
-void kdebug_lookup_gen_events(long *dbg_parms, int dbg_namelen, void *dp,
- boolean_t lookup);
-
-void kdbg_trace_data(struct proc *proc, long *arg_pid, long *arg_uniqueid);
-
-void kdbg_trace_string(struct proc *proc, long *arg1, long *arg2, long *arg3, long *arg4);
-
-void kdbg_dump_trace_to_file(const char *);
-void kdebug_init(unsigned int n_events, char *filterdesc, boolean_t wrapping);
-void kdebug_trace_start(unsigned int n_events, const char *filterdesc,
- boolean_t wrapping, boolean_t at_wake);
-void kdebug_free_early_buf(void);
-struct task;
-void release_storage_unit(int cpu, uint32_t storage_unit);
-int allocate_storage_unit(int cpu);
-
-#define KDBG_CLASS_ENCODE(Class, SubClass) KDBG_EVENTID(Class, SubClass, 0)
-#define KDBG_CLASS_DECODE(Debugid) (Debugid & KDBG_CSC_MASK)