2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef _KERN_DEBUG_H_
30 #define _KERN_DEBUG_H_
32 #include <sys/cdefs.h>
34 #include <uuid/uuid.h>
36 #ifndef XNU_KERNEL_PRIVATE
37 #include <TargetConditionals.h>
40 #ifdef __APPLE_API_PRIVATE
41 #ifdef __APPLE_API_UNSTABLE
43 struct thread_snapshot
{
44 uint32_t snapshot_magic
;
45 uint32_t nkern_frames
;
46 uint32_t nuser_frames
;
48 uint64_t continuation
;
53 int32_t priority
; // static priority
54 int32_t sched_pri
; // scheduled (current) priority
55 int32_t sched_flags
; // scheduler flags
57 } __attribute__ ((packed
));
59 struct task_snapshot
{
60 uint32_t snapshot_magic
;
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
;
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
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).
80 uint32_t was_throttled
;
81 uint32_t did_throttle
;
83 } __attribute__ ((packed
));
85 struct 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
;
91 uint16_t ms_opaque_flags
; /* managed by external entity, e.g. fdrmicrod */
92 } __attribute__ ((packed
));
94 struct mem_and_io_snapshot
{
95 uint32_t snapshot_magic
;
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
));
113 struct stack_snapshot_frame32
{
118 struct stack_snapshot_frame64
{
123 struct _dyld_cache_header
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
140 struct dyld_uuid_info_32
{
141 uint32_t imageLoadAddress
; /* base address image is mapped at */
145 struct dyld_uuid_info_64
{
146 uint64_t imageLoadAddress
; /* base address image is mapped at */
150 enum micro_snapshot_flags
{
151 kInterruptRecord
= 0x1,
152 kTimerArmingRecord
= 0x2,
153 kUserMode
= 0x4, /* interrupted usermode, or armed by usermode */
157 * Flags used in the following assortment of snapshots.
159 enum generic_snapshot_flags
{
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 */
179 enum thread_snapshot_flags
{
180 kHasDispatchSerial
= 0x4,
181 kStacksPCOnly
= 0x8, /* Stack traces have no frame pointers. */
182 kThreadDarwinBG
= 0x10 /* Thread is darwinbg */
185 #define VM_PRESSURE_TIME_WINDOW 5 /* seconds */
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,
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
205 #endif /* __APPLE_API_UNSTABLE */
206 #endif /* __APPLE_API_PRIVATE */
208 #ifdef KERNEL_PRIVATE
210 extern unsigned int systemLogDiags
;
211 extern char debug_buf
[];
212 extern unsigned int debug_boot_arg
;
213 extern unsigned char *kernel_uuid
;
214 extern char kernel_uuid_string
[];
216 #ifdef MACH_KERNEL_PRIVATE
218 extern unsigned int halt_in_debugger
;
220 extern unsigned int switch_debugger
;
222 extern unsigned int current_debugger
;
223 #define NO_CUR_DB 0x0
224 #define KDP_CUR_DB 0x1
225 #define KDB_CUR_DB 0x2
227 extern unsigned int active_debugger
;
228 extern unsigned int debug_mode
;
229 extern unsigned int disable_debug_output
;
231 extern unsigned int panicDebugging
;
232 extern unsigned int logPanicDataToScreen
;
234 extern int db_run_mode
;
236 /* modes the system may be running in */
240 #define STEP_RETURN 2
242 #define STEP_CONTINUE 4
243 #define STEP_INVISIBLE 5
245 #define STEP_TRACE 7 /* Show all calls to functions and returns */
247 extern const char *panicstr
;
248 extern volatile unsigned int nestedpanic
;
249 extern int unsigned long panic_caller
;
251 extern char *debug_buf_ptr
;
252 extern unsigned int debug_buf_size
;
254 extern void debug_log_init(void);
255 extern void debug_putc(char);
257 extern void panic_init(void);
259 int packA(char *inbuf
, uint32_t length
, uint32_t buflen
);
260 void unpackA(char *inbuf
, uint32_t length
);
262 void panic_display_system_configuration(void);
263 void panic_display_zprint(void);
264 void panic_display_kernel_aslr(void);
266 void panic_display_ztrace(void);
267 #endif /* CONFIG_ZLEAKS */
268 #endif /* MACH_KERNEL_PRIVATE */
277 #define DB_KDP_BP_DIS 0x80
278 #define DB_LOG_PI_SCRN 0x100
279 #define DB_KDP_GETC_ENA 0x200
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
289 #define DB_NMI_BTN_ENA 0x8000 /* Enable button to directly trigger NMI */
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
302 extern int debug_kprint_syscall
;
303 extern 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); \
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) */
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
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)
336 #endif /* KERNEL_PRIVATE */
340 extern void panic(const char *string
, ...) __printflike(1,2);
343 void _consume_panic_args(int, ...);
344 void panic_context(unsigned int reason
, void *ctx
, const char *string
, ...);
347 #if CONFIG_NO_PANIC_STRINGS
349 #define panic_plain(x, ...) _consume_panic_args( 0, ## __VA_ARGS__ )
350 #define panic(x, ...) _consume_panic_args( 0, ## __VA_ARGS__ )
352 #define panic_plain(...) (panic)((char *)0)
353 #define panic(...) (panic)((char *)0)
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 */
365 void populate_model_name(char *);
366 unsigned panic_active(void);
369 #endif /* _KERN_DEBUG_H_ */