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