1/* 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29#ifndef _KERN_DEBUG_H_ 30#define _KERN_DEBUG_H_ 31 32#include <sys/cdefs.h> 33#include <stdint.h> 34#include <uuid/uuid.h> 35 36#ifndef XNU_KERNEL_PRIVATE 37#include <TargetConditionals.h> 38#endif 39 40#ifdef __APPLE_API_PRIVATE 41#ifdef __APPLE_API_UNSTABLE 42 43struct thread_snapshot { 44 uint32_t snapshot_magic; 45 uint32_t nkern_frames; 46 uint32_t nuser_frames; 47 uint64_t wait_event; 48 uint64_t continuation; 49 uint64_t thread_id; 50 uint64_t user_time; 51 uint64_t system_time; 52 int32_t state; 53 int32_t priority; // static priority 54 int32_t sched_pri; // scheduled (current) priority 55 int32_t sched_flags; // scheduler flags 56 char ss_flags; 57} __attribute__ ((packed)); 58 59struct task_snapshot { 60 uint32_t snapshot_magic; 61 int32_t pid; 62 uint64_t uniqueid; 63 uint64_t user_time_in_terminated_threads; 64 uint64_t system_time_in_terminated_threads; 65 uint8_t shared_cache_identifier[16]; 66 uint64_t shared_cache_slide; 67 uint32_t nloadinfos; 68 int suspend_count; 69 int task_size; // pages 70 int faults; // number of page faults 71 int pageins; // number of actual pageins 72 int cow_faults; // number of copy-on-write faults 73 uint32_t ss_flags; 74 /* We restrict ourselves to a statically defined 75 * (current as of 2009) length for the 76 * p_comm string, due to scoping issues (osfmk/bsd and user/kernel 77 * binary compatibility). 78 */ 79 char p_comm[17]; 80 uint32_t was_throttled; 81 uint32_t did_throttle; 82 uint32_t latency_qos; 83} __attribute__ ((packed)); 84 85struct micro_snapshot { 86 uint32_t snapshot_magic; 87 uint32_t ms_cpu; /* cpu number this snapshot was recorded on */ 88 uint64_t ms_time; /* time at sample (seconds) */ 89 uint64_t ms_time_microsecs; 90 uint8_t ms_flags; 91 uint16_t ms_opaque_flags; /* managed by external entity, e.g. fdrmicrod */ 92} __attribute__ ((packed)); 93 94struct mem_and_io_snapshot { 95 uint32_t snapshot_magic; 96 uint32_t free_pages; 97 uint32_t active_pages; 98 uint32_t inactive_pages; 99 uint32_t purgeable_pages; 100 uint32_t wired_pages; 101 uint32_t speculative_pages; 102 uint32_t throttled_pages; 103 uint32_t filebacked_pages; 104 uint32_t compressions; 105 uint32_t decompressions; 106 uint32_t compressor_size; 107 int busy_buffer_count; 108 uint32_t pages_wanted; 109 uint32_t pages_reclaimed; 110 uint8_t pages_wanted_reclaimed_valid; // did mach_vm_pressure_monitor succeed? 111} __attribute__((packed)); 112 113struct stack_snapshot_frame32 { 114 uint32_t lr; 115 uint32_t sp; 116}; 117 118struct stack_snapshot_frame64 { 119 uint64_t lr; 120 uint64_t sp; 121}; 122 123struct _dyld_cache_header 124{ 125 char magic[16]; // e.g. "dyld_v0 i386" 126 uint32_t mappingOffset; // file offset to first dyld_cache_mapping_info 127 uint32_t mappingCount; // number of dyld_cache_mapping_info entries 128 uint32_t imagesOffset; // file offset to first dyld_cache_image_info 129 uint32_t imagesCount; // number of dyld_cache_image_info entries 130 uint64_t dyldBaseAddress; // base address of dyld when cache was built 131 uint64_t codeSignatureOffset; // file offset of code signature blob 132 uint64_t codeSignatureSize; // size of code signature blob (zero means to end of file) 133 uint64_t slideInfoOffset; // file offset of kernel slid info 134 uint64_t slideInfoSize; // size of kernel slid info 135 uint64_t localSymbolsOffset; // file offset of where local symbols are stored 136 uint64_t localSymbolsSize; // size of local symbols information 137 uint8_t uuid[16]; // unique value for each shared cache file 138}; 139 140struct dyld_uuid_info_32 { 141 uint32_t imageLoadAddress; /* base address image is mapped at */ 142 uuid_t imageUUID; 143}; 144 145struct dyld_uuid_info_64 { 146 uint64_t imageLoadAddress; /* base address image is mapped at */ 147 uuid_t imageUUID; 148}; 149 150enum micro_snapshot_flags { 151 kInterruptRecord = 0x1, 152 kTimerArmingRecord = 0x2, 153 kUserMode = 0x4, /* interrupted usermode, or armed by usermode */ 154}; 155 156/* 157 * Flags used in the following assortment of snapshots. 158 */ 159enum generic_snapshot_flags { 160 kUser64_p = 0x1, 161 kKernel64_p = 0x2 162}; 163 164 enum task_snapshot_flags { 165 kTaskRsrcFlagged = 0x4, // In the EXC_RESOURCE danger zone? 166 kTerminatedSnapshot = 0x8, 167 kPidSuspended = 0x10, // true for suspended task 168 kFrozen = 0x20, // true for hibernated task (along with pidsuspended) 169 kTaskDarwinBG = 0x40, 170 kTaskExtDarwinBG = 0x80, 171 kTaskVisVisible = 0x100, 172 kTaskVisNonvisible = 0x200, 173 kTaskIsForeground = 0x400, 174 kTaskIsBoosted = 0x800, 175 kTaskIsSuppressed = 0x1000, 176 kTaskIsTimerThrottled = 0x2000 /* deprecated */ 177 }; 178 179enum thread_snapshot_flags { 180 kHasDispatchSerial = 0x4, 181 kStacksPCOnly = 0x8, /* Stack traces have no frame pointers. */ 182 kThreadDarwinBG = 0x10 /* Thread is darwinbg */ 183}; 184 185#define VM_PRESSURE_TIME_WINDOW 5 /* seconds */ 186 187enum { 188 STACKSHOT_GET_DQ = 0x01, 189 STACKSHOT_SAVE_LOADINFO = 0x02, 190 STACKSHOT_GET_GLOBAL_MEM_STATS = 0x04, 191 STACKSHOT_SAVE_KEXT_LOADINFO = 0x08, 192 STACKSHOT_GET_MICROSTACKSHOT = 0x10, 193 STACKSHOT_GLOBAL_MICROSTACKSHOT_ENABLE = 0x20, 194 STACKSHOT_GLOBAL_MICROSTACKSHOT_DISABLE = 0x40, 195 STACKSHOT_SET_MICROSTACKSHOT_MARK = 0x80, 196 STACKSHOT_SAVE_KERNEL_FRAMES_ONLY = 0x100, 197 STACKSHOT_GET_BOOT_PROFILE = 0x200, 198}; 199 200#define STACKSHOT_THREAD_SNAPSHOT_MAGIC 0xfeedface 201#define STACKSHOT_TASK_SNAPSHOT_MAGIC 0xdecafbad 202#define STACKSHOT_MEM_AND_IO_SNAPSHOT_MAGIC 0xbfcabcde 203#define STACKSHOT_MICRO_SNAPSHOT_MAGIC 0x31c54011 204 205#endif /* __APPLE_API_UNSTABLE */ 206#endif /* __APPLE_API_PRIVATE */ 207 208#ifdef KERNEL_PRIVATE 209 210extern unsigned int systemLogDiags; 211extern char debug_buf[]; 212extern unsigned int debug_boot_arg; 213extern unsigned char *kernel_uuid; 214extern char kernel_uuid_string[]; 215 216#ifdef MACH_KERNEL_PRIVATE 217 218extern unsigned int halt_in_debugger; 219 220extern unsigned int switch_debugger; 221 222extern unsigned int current_debugger; 223#define NO_CUR_DB 0x0 224#define KDP_CUR_DB 0x1 225#define KDB_CUR_DB 0x2 226 227extern unsigned int active_debugger; 228extern unsigned int debug_mode; 229extern unsigned int disable_debug_output; 230 231extern unsigned int panicDebugging; 232extern unsigned int logPanicDataToScreen; 233 234extern int db_run_mode; 235 236/* modes the system may be running in */ 237 238#define STEP_NONE 0 239#define STEP_ONCE 1 240#define STEP_RETURN 2 241#define STEP_CALLT 3 242#define STEP_CONTINUE 4 243#define STEP_INVISIBLE 5 244#define STEP_COUNT 6 245#define STEP_TRACE 7 /* Show all calls to functions and returns */ 246 247extern const char *panicstr; 248extern volatile unsigned int nestedpanic; 249extern int unsigned long panic_caller; 250 251extern char *debug_buf_ptr; 252extern unsigned int debug_buf_size; 253 254extern void debug_log_init(void); 255extern void debug_putc(char); 256 257extern void panic_init(void); 258 259int packA(char *inbuf, uint32_t length, uint32_t buflen); 260void unpackA(char *inbuf, uint32_t length); 261 262void panic_display_system_configuration(void); 263void panic_display_zprint(void); 264void panic_display_kernel_aslr(void); 265#if CONFIG_ZLEAKS 266void panic_display_ztrace(void); 267#endif /* CONFIG_ZLEAKS */ 268#endif /* MACH_KERNEL_PRIVATE */ 269 270#define DB_HALT 0x1 271#define DB_PRT 0x2 272#define DB_NMI 0x4 273#define DB_KPRT 0x8 274#define DB_KDB 0x10 275#define DB_SLOG 0x20 276#define DB_ARP 0x40 277#define DB_KDP_BP_DIS 0x80 278#define DB_LOG_PI_SCRN 0x100 279#define DB_KDP_GETC_ENA 0x200 280 281#define DB_KERN_DUMP_ON_PANIC 0x400 /* Trigger core dump on panic*/ 282#define DB_KERN_DUMP_ON_NMI 0x800 /* Trigger core dump on NMI */ 283#define DB_DBG_POST_CORE 0x1000 /*Wait in debugger after NMI core */ 284#define DB_PANICLOG_DUMP 0x2000 /* Send paniclog on panic,not core*/ 285#define DB_REBOOT_POST_CORE 0x4000 /* Attempt to reboot after 286 * post-panic crashdump/paniclog 287 * dump. 288 */ 289#define DB_NMI_BTN_ENA 0x8000 /* Enable button to directly trigger NMI */ 290 291#if DEBUG 292/* 293 * For the DEBUG kernel, support the following: 294 * sysctl -w debug.kprint_syscall=<syscall_mask> 295 * sysctl -w debug.kprint_syscall_process=<p_comm> 296 * <syscall_mask> should be an OR of the masks below 297 * for UNIX, MACH, MDEP, or IPC. This debugging aid 298 * assumes the task/process is locked/wired and will 299 * not go away during evaluation. If no process is 300 * specified, all processes will be traced 301 */ 302extern int debug_kprint_syscall; 303extern int debug_kprint_current_process(const char **namep); 304#define DEBUG_KPRINT_SYSCALL_PREDICATE_INTERNAL(mask, namep) \ 305 ( (debug_kprint_syscall & (mask)) && debug_kprint_current_process(namep) ) 306#define DEBUG_KPRINT_SYSCALL_MASK(mask, fmt, args...) do { \ 307 const char *dks_name = NULL; \ 308 if (DEBUG_KPRINT_SYSCALL_PREDICATE_INTERNAL(mask, &dks_name)) { \ 309 kprintf("[%s%s%p]" fmt, dks_name ? dks_name : "", \ 310 dks_name ? "@" : "", current_thread(), args); \ 311 } \ 312 } while (0) 313#else /* !DEBUG */ 314#define DEBUG_KPRINT_SYSCALL_PREDICATE_INTERNAL(mask, namep) (0) 315#define DEBUG_KPRINT_SYSCALL_MASK(mask, fmt, args...) do { } while (0) /* kprintf(fmt, args) */ 316#endif /* !DEBUG */ 317 318enum { 319 DEBUG_KPRINT_SYSCALL_UNIX_MASK = 1 << 0, 320 DEBUG_KPRINT_SYSCALL_MACH_MASK = 1 << 1, 321 DEBUG_KPRINT_SYSCALL_MDEP_MASK = 1 << 2, 322 DEBUG_KPRINT_SYSCALL_IPC_MASK = 1 << 3 323}; 324 325#define DEBUG_KPRINT_SYSCALL_PREDICATE(mask) \ 326 DEBUG_KPRINT_SYSCALL_PREDICATE_INTERNAL(mask, NULL) 327#define DEBUG_KPRINT_SYSCALL_UNIX(fmt, args...) \ 328 DEBUG_KPRINT_SYSCALL_MASK(DEBUG_KPRINT_SYSCALL_UNIX_MASK,fmt,args) 329#define DEBUG_KPRINT_SYSCALL_MACH(fmt, args...) \ 330 DEBUG_KPRINT_SYSCALL_MASK(DEBUG_KPRINT_SYSCALL_MACH_MASK,fmt,args) 331#define DEBUG_KPRINT_SYSCALL_MDEP(fmt, args...) \ 332 DEBUG_KPRINT_SYSCALL_MASK(DEBUG_KPRINT_SYSCALL_MDEP_MASK,fmt,args) 333#define DEBUG_KPRINT_SYSCALL_IPC(fmt, args...) \ 334 DEBUG_KPRINT_SYSCALL_MASK(DEBUG_KPRINT_SYSCALL_IPC_MASK,fmt,args) 335 336#endif /* KERNEL_PRIVATE */ 337 338__BEGIN_DECLS 339 340extern void panic(const char *string, ...) __printflike(1,2); 341 342#if KERNEL_PRIVATE 343void _consume_panic_args(int, ...); 344void panic_context(unsigned int reason, void *ctx, const char *string, ...); 345#endif 346 347#if CONFIG_NO_PANIC_STRINGS 348#if KERNEL_PRIVATE 349#define panic_plain(x, ...) _consume_panic_args( 0, ## __VA_ARGS__ ) 350#define panic(x, ...) _consume_panic_args( 0, ## __VA_ARGS__ ) 351#else 352#define panic_plain(...) (panic)((char *)0) 353#define panic(...) (panic)((char *)0) 354#endif 355#else /* CONFIGS_NO_PANIC_STRINGS */ 356#define panic_plain(ex, ...) \ 357 (panic)(ex, ## __VA_ARGS__) 358#define __STRINGIFY(x) #x 359#define LINE_NUMBER(x) __STRINGIFY(x) 360#define PANIC_LOCATION __FILE__ ":" LINE_NUMBER(__LINE__) 361#define panic(ex, ...) \ 362 (panic)(# ex "@" PANIC_LOCATION, ## __VA_ARGS__) 363#endif /* CONFIGS_NO_PANIC_STRINGS */ 364 365void populate_model_name(char *); 366unsigned panic_active(void); 367__END_DECLS 368 369#endif /* _KERN_DEBUG_H_ */ 370