X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/39236c6e673c41db228275375ab7fdb0f837b292..743345f9a4b36f7e2f9ba37691e70c50baecb56e:/osfmk/kern/debug.c?ds=inline diff --git a/osfmk/kern/debug.c b/osfmk/kern/debug.c index 7ee11d7a9..e031b95fd 100644 --- a/osfmk/kern/debug.c +++ b/osfmk/kern/debug.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000-2013 Apple Inc. All rights reserved. + * Copyright (c) 2000-2016 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * @@ -59,7 +59,6 @@ #include #include -#include #include #include #include @@ -67,10 +66,13 @@ #include #include #include +#include +#include +#include #include #include #include -#if !MACH_KDP +#if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING) #include #endif @@ -87,6 +89,14 @@ #include #include #include +#include + +#include + +#if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS) +#include /* For gPanicBase */ +#endif + unsigned int halt_in_debugger = 0; unsigned int switch_debugger = 0; @@ -97,6 +107,8 @@ unsigned int disable_debug_output = TRUE; unsigned int systemLogDiags = FALSE; unsigned int panicDebugging = FALSE; unsigned int logPanicDataToScreen = FALSE; +unsigned int kdebug_serial = FALSE; +boolean_t lock_panic_mode = FALSE; int mach_assert = 1; @@ -111,9 +123,20 @@ unsigned long panic_caller; #define DEBUG_BUF_SIZE (3 * PAGE_SIZE) +/* debug_buf is directly linked with iBoot panic region for ARM64 targets */ +#if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS) +char *debug_buf_addr = NULL; +char *debug_buf_ptr = NULL; +unsigned int debug_buf_size = 0; +#else char debug_buf[DEBUG_BUF_SIZE]; +__used char *debug_buf_addr = debug_buf; char *debug_buf_ptr = debug_buf; unsigned int debug_buf_size = sizeof(debug_buf); +#endif + +char *debug_buf_stackshot_start; +char *debug_buf_stackshot_end; static char model_name[64]; unsigned char *kernel_uuid; @@ -136,11 +159,18 @@ struct pasc { typedef struct pasc pasc_t; /* Prevent CPP from breaking the definition below */ -#if CONFIG_NO_PANIC_STRINGS +#ifdef CONFIG_NO_PANIC_STRINGS #undef Assert #endif -void +int kext_assertions_enable = +#if DEBUG || DEVELOPMENT + TRUE; +#else + FALSE; +#endif + +void __attribute__((noinline)) Assert( const char *file, int line, @@ -150,6 +180,7 @@ Assert( int saved_return_on_panic; if (!mach_assert) { + kprintf("%s:%d non-fatal Assertion: %s", file, line, expression); return; } @@ -202,6 +233,10 @@ panic_init(void) simple_lock_init(&panic_lock, 0); panic_is_inited = 1; panic_caller = 0; + + if (!PE_parse_boot_argn("assertions", &mach_assert, sizeof(mach_assert))) { + mach_assert = 1; + } } void @@ -209,8 +244,20 @@ debug_log_init(void) { if (debug_buf_size != 0) return; +#if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS) + if (!gPanicBase) { + printf("debug_log_init: Error!! gPanicBase is still not initialized\n"); + return; + } + /* Shift debug buf start location and size by 8 bytes for magic header and crc value */ + debug_buf_addr = (char*)gPanicBase + 8; + debug_buf_ptr = debug_buf_addr; + debug_buf_size = gPanicSize - 8; +#else + debug_buf_addr = debug_buf; debug_buf_ptr = debug_buf; debug_buf_size = sizeof(debug_buf); +#endif } #if defined(__i386__) || defined(__x86_64__) @@ -234,12 +281,14 @@ void _consume_panic_args(int a __unused, ...) panic("panic"); } +extern unsigned int write_trace_on_panic; + static spl_t panic_prologue(const char *str) { spl_t s; - if (kdebug_enable) { + if (write_trace_on_panic && kdebug_enable) { if (get_preemption_level() == 0 && !ml_at_interrupt_context()) { ml_set_interrupts_enabled(TRUE); kdbg_dump_trace_to_file("/var/tmp/panic.trace"); @@ -248,6 +297,8 @@ panic_prologue(const char *str) s = splhigh(); disable_preemption(); + /* Locking code should relax some checks at panic time */ + lock_panic_mode = TRUE; #if defined(__i386__) || defined(__x86_64__) /* Attempt to display the unparsed panic string */ @@ -282,8 +333,10 @@ restart: } else { nestedpanic +=1; PANIC_UNLOCK(); + // Other cores will not be resumed on double panic Debugger("double panic"); - printf("double panic: We are hanging here...\n"); + // a printf statement here was removed to avoid a panic-loop caused + // by a panic from printf panic_stop(); /* NOTREACHED */ } @@ -293,12 +346,21 @@ restart: panicwait = 1; PANIC_UNLOCK(); + + // halt other cores now in anticipation of the debugger call return(s); } - +#if DEVELOPMENT || DEBUG static void panic_epilogue(spl_t s) +#else +#if !defined(__i386__) && !defined(__x86_64__) +__attribute__((noreturn)) +#endif +static void +panic_epilogue(__unused spl_t s) +#endif { /* * Release panicstr so that we can handle normally other panics. @@ -307,29 +369,43 @@ panic_epilogue(spl_t s) panicstr = (char *)0; PANIC_UNLOCK(); +#if DEVELOPMENT || DEBUG if (return_on_panic) { + // resume other cores as we are returning panic_normal(); enable_preemption(); splx(s); return; } +#endif kdb_printf("panic: We are hanging here...\n"); panic_stop(); /* NOTREACHED */ } +#if !DEVELOPMENT && !DEBUG && !defined(__i386__) && !defined(__x86_64__) +__attribute__((noreturn)) +#endif void panic(const char *str, ...) { va_list listp; spl_t s; + boolean_t old_doprnt_hide_pointers = doprnt_hide_pointers; - +#if defined (__x86_64__) + plctrace_disable(); +#endif /* panic_caller is initialized to 0. If set, don't change it */ if ( ! panic_caller ) panic_caller = (unsigned long)(char *)__builtin_return_address(0); - + + s = panic_prologue(str); + + /* Never hide pointers from panic logs. */ + doprnt_hide_pointers = FALSE; + kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller); if (str) { va_start(listp, str); @@ -343,9 +419,50 @@ panic(const char *str, ...) */ panicwait = 0; Debugger("panic"); + + doprnt_hide_pointers = old_doprnt_hide_pointers; + panic_epilogue(s); } +/* + * panic_with_options: wraps the panic call in a way that allows us to pass + * a bitmask of specific debugger options. + */ +#if !DEVELOPMENT && !DEBUG && !defined(__i386__) && !defined(__x86_64__) +__attribute__((noreturn)) +#endif +void +panic_with_options(unsigned int reason, void *ctx, uint64_t debugger_options_mask, const char *str, ...) +{ + va_list listp; + spl_t s; + + + /* panic_caller is initialized to 0. If set, don't change it */ + if ( ! panic_caller ) + panic_caller = (unsigned long)(char *)__builtin_return_address(0); + + s = panic_prologue(str); + kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller); + if (str) { + va_start(listp, str); + _doprnt(str, &listp, consdebug_putc, 0); + va_end(listp); + } + kdb_printf("\n"); + + /* + * Release panicwait indicator so that other cpus may call Debugger(). + */ + panicwait = 0; + DebuggerWithContext(reason, ctx, "panic", debugger_options_mask); + panic_epilogue(s); +} + +#if !DEVELOPMENT && !DEBUG && !defined(__i386__) && !defined(__x86_64__) +__attribute__((noreturn)) +#endif void panic_context(unsigned int reason, void *ctx, const char *str, ...) { @@ -370,32 +487,58 @@ panic_context(unsigned int reason, void *ctx, const char *str, ...) * Release panicwait indicator so that other cpus may call Debugger(). */ panicwait = 0; - DebuggerWithContext(reason, ctx, "panic"); + DebuggerWithContext(reason, ctx, "panic", DEBUGGER_OPTION_NONE); panic_epilogue(s); } -void -log(__unused int level, char *fmt, ...) +__attribute__((noinline,not_tail_called)) +void log(__unused int level, char *fmt, ...) { + void *caller = __builtin_return_address(0); va_list listp; + va_list listp2; + #ifdef lint level++; #endif /* lint */ #ifdef MACH_BSD - disable_preemption(); va_start(listp, fmt); - _doprnt(fmt, &listp, conslog_putc, 0); - va_end(listp); + va_copy(listp2, listp); + + disable_preemption(); + _doprnt(fmt, &listp, cons_putc_locked, 0); enable_preemption(); + + va_end(listp); + + os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller); + va_end(listp2); #endif } +/* + * Skip appending log messages to the new logging infrastructure in contexts + * where safety is uncertain. These contexts include: + * - When we're in the debugger + * - We're in a panic + * - Interrupts are disabled + * - Or Pre-emption is disabled + * In all the above cases, it is potentially unsafe to log messages. + */ + +boolean_t oslog_is_safe(void) { + return (debug_mode == 0 && + not_in_kdp == 1 && + get_preemption_level() == 0 && + ml_get_interrupts_enabled() == TRUE); +} + void debug_putc(char c) { if ((debug_buf_size != 0) && - ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) { + ((debug_buf_ptr-debug_buf_addr) < (int)debug_buf_size)) { *debug_buf_ptr=c; debug_buf_ptr++; } @@ -452,12 +595,13 @@ extern void *proc_name_address(void *p); static void panic_display_process_name(void) { - char proc_name[32] = "Unknown"; + /* because of scoping issues len(p_comm) from proc_t is hard coded here */ + char proc_name[17] = "Unknown"; task_t ctask = 0; void *cbsd_info = 0; if (ml_nofault_copy((vm_offset_t)¤t_thread()->task, (vm_offset_t) &ctask, sizeof(task_t)) == sizeof(task_t)) - if(ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(&ctask->bsd_info)) == sizeof(&ctask->bsd_info)) + if(ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(cbsd_info)) == sizeof(cbsd_info)) if (cbsd_info && (ml_nofault_copy((vm_offset_t) proc_name_address(cbsd_info), (vm_offset_t) &proc_name, sizeof(proc_name)) > 0)) proc_name[sizeof(proc_name) - 1] = '\0'; kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name); @@ -471,7 +615,7 @@ void populate_model_name(char *model_string) { strlcpy(model_name, model_string, sizeof(model_name)); } -static void panic_display_model_name(void) { +void panic_display_model_name(void) { char tmp_model_name[sizeof(model_name)]; if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name)) @@ -483,7 +627,7 @@ static void panic_display_model_name(void) { kdb_printf("System model name: %s\n", tmp_model_name); } -static void panic_display_kernel_uuid(void) { +void panic_display_kernel_uuid(void) { char tmp_kernel_uuid[sizeof(kernel_uuid_string)]; if (ml_nofault_copy((vm_offset_t) &kernel_uuid_string, (vm_offset_t) &tmp_kernel_uuid, sizeof(kernel_uuid_string)) != sizeof(kernel_uuid_string)) @@ -500,6 +644,12 @@ void panic_display_kernel_aslr(void) { } } +void panic_display_hibb(void) { +#if defined(__i386__) || defined (__x86_64__) + kdb_printf("__HIB text base: %p\n", (void *) vm_hib_base); +#endif +} + static void panic_display_uptime(void) { uint64_t uptime; absolutetime_to_nanoseconds(mach_absolute_time(), &uptime); @@ -524,6 +674,7 @@ __private_extern__ void panic_display_system_configuration(void) { kdb_printf("\nKernel version:\n%s\n",version); panic_display_kernel_uuid(); panic_display_kernel_aslr(); + panic_display_hibb(); panic_display_pal_info(); panic_display_model_name(); panic_display_uptime(); @@ -535,8 +686,7 @@ __private_extern__ void panic_display_system_configuration(void) { } } -extern zone_t first_zone; -extern unsigned int num_zones, stack_total; +extern unsigned int stack_total; extern unsigned long long stack_allocs; #if defined(__i386__) || defined (__x86_64__) @@ -545,6 +695,8 @@ extern long long alloc_ptepages_count; #endif extern boolean_t panic_include_zprint; +extern vm_offset_t panic_kext_memory_info; +extern vm_size_t panic_kext_memory_size; __private_extern__ void panic_display_zprint() { @@ -553,37 +705,50 @@ __private_extern__ void panic_display_zprint() unsigned int i; struct zone zone_copy; - if(first_zone!=NULL) { - if(ml_nofault_copy((vm_offset_t)first_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) { - for (i = 0; i < num_zones; i++) { - if(zone_copy.cur_size > (1024*1024)) { - kdb_printf("%.20s:%lu\n",zone_copy.zone_name,(uintptr_t)zone_copy.cur_size); - } - - if(zone_copy.next_zone == NULL) { - break; - } - - if(ml_nofault_copy((vm_offset_t)zone_copy.next_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) != sizeof(struct zone)) { - break; - } + kdb_printf("%-20s %10s %10s\n", "Zone Name", "Cur Size", "Free Size"); + for (i = 0; i < num_zones; i++) { + if(ml_nofault_copy((vm_offset_t)(&zone_array[i]), (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) { + if(zone_copy.cur_size > (1024*1024)) { + kdb_printf("%-20s %10lu %10lu\n",zone_copy.zone_name, (uintptr_t)zone_copy.cur_size,(uintptr_t)(zone_copy.countfree * zone_copy.elem_size)); } - } + } } - kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size * stack_total)); + kdb_printf("%-20s %10lu\n", "Kernel Stacks", (uintptr_t)(kernel_stack_size * stack_total)); #if defined(__i386__) || defined (__x86_64__) - kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count)); + kdb_printf("%-20s %10lu\n", "PageTables",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count)); #endif - kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total); + kdb_printf("%-20s %10lu\n", "Kalloc.Large", (uintptr_t)kalloc_large_total); + if (panic_kext_memory_info) { + mach_memory_info_t *mem_info = (mach_memory_info_t *)panic_kext_memory_info; + kdb_printf("\n%-5s %10s\n", "Kmod", "Size"); + for (i = 0; i < VM_KERN_MEMORY_COUNT + VM_KERN_COUNTER_COUNT; i++) { + if (((mem_info[i].flags & VM_KERN_SITE_TYPE) == VM_KERN_SITE_KMOD) && (mem_info[i].size > (1024 * 1024))) { + kdb_printf("%-5lld %10lld\n", mem_info[i].site, mem_info[i].size); + } + } + } } } +#if CONFIG_ECC_LOGGING +__private_extern__ void panic_display_ecc_errors() +{ + uint32_t count = ecc_log_get_correction_count(); + + if (count > 0) { + kdb_printf("ECC Corrections:%u\n", count); + } +} +#endif /* CONFIG_ECC_LOGGING */ + #if CONFIG_ZLEAKS extern boolean_t panic_include_ztrace; extern struct ztrace* top_ztrace; +void panic_print_symbol_name(vm_address_t search); + /* * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator. * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c @@ -592,6 +757,9 @@ __private_extern__ void panic_display_ztrace(void) { if(panic_include_ztrace == TRUE) { unsigned int i = 0; + boolean_t keepsyms = FALSE; + + PE_parse_boot_argn("keepsyms", &keepsyms, sizeof (keepsyms)); struct ztrace top_ztrace_copy; /* Make sure not to trip another panic if there's something wrong with memory */ @@ -599,7 +767,11 @@ __private_extern__ void panic_display_ztrace(void) kdb_printf("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy.zt_size); /* Print the backtrace addresses */ for (i = 0; (i < top_ztrace_copy.zt_depth && i < MAX_ZTRACE_DEPTH) ; i++) { - kdb_printf("%p\n", top_ztrace_copy.zt_stack[i]); + kdb_printf("%p ", top_ztrace_copy.zt_stack[i]); + if (keepsyms) { + panic_print_symbol_name((vm_address_t)top_ztrace_copy.zt_stack[i]); + } + kdb_printf("\n"); } /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */ kmod_panic_dump((vm_offset_t *)&top_ztrace_copy.zt_stack[0], top_ztrace_copy.zt_depth); @@ -612,7 +784,7 @@ __private_extern__ void panic_display_ztrace(void) } #endif /* CONFIG_ZLEAKS */ -#if !MACH_KDP +#if ! (MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING) static struct kdp_ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}}; /* XXX ugly forward declares to stop warnings */ @@ -622,9 +794,9 @@ void kdp_set_gateway_mac(void *); void kdp_set_interface(void *); void kdp_register_send_receive(void *, void *); void kdp_unregister_send_receive(void *, void *); -void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t); + int kdp_stack_snapshot_geterror(void); -int kdp_stack_snapshot_bytes_traced(void); +uint32_t kdp_stack_snapshot_bytes_traced(void); void * kdp_get_interface( void) @@ -664,22 +836,11 @@ void kdp_unregister_send_receive(__unused void *send, __unused void *receive) {} -void -kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf, - __unused uint32_t tracebuf_size, __unused uint32_t options) +void kdp_register_link(__unused kdp_link_t link, __unused kdp_mode_t mode) {} -int -kdp_stack_snapshot_geterror(void) -{ - return -1; -} - -int -kdp_stack_snapshot_bytes_traced(void) -{ - return 0; -} +void kdp_unregister_link(__unused kdp_link_t link, __unused kdp_mode_t mode) +{} #endif