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