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