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