]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/debug.c
xnu-1228.7.58.tar.gz
[apple/xnu.git] / osfmk / kern / debug.c
index 49bbc8ca0a61e8e8c53db36127db56767a03ba62..c7e01a134992f1cb91480582776b045f6adcee53 100644 (file)
@@ -1,14 +1,19 @@
 /*
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
  *
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
@@ -18,7 +23,7 @@
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_COPYRIGHT@
  */
 /*
  * @OSF_COPYRIGHT@
 #include <vm/vm_kern.h>
 #include <vm/pmap.h>
 #include <stdarg.h>
 #include <vm/vm_kern.h>
 #include <vm/pmap.h>
 #include <stdarg.h>
+#if !MACH_KDP
+#include <kdp/kdp_udp.h>
+#endif
 
 #ifdef __ppc__
 #include <ppc/Firmware.h>
 #include <ppc/low_trace.h>
 #endif
 
 
 #ifdef __ppc__
 #include <ppc/Firmware.h>
 #include <ppc/low_trace.h>
 #endif
 
+#ifdef __i386__
+#include <i386/cpu_threads.h>
+#include <i386/pmCPU.h>
+#endif
+
+#include <IOKit/IOPlatformExpert.h>
+
 unsigned int   halt_in_debugger = 0;
 unsigned int   switch_debugger = 0;
 unsigned int   current_debugger = 0;
 unsigned int   active_debugger = 0;
 unsigned int   debug_mode=0;
 unsigned int   halt_in_debugger = 0;
 unsigned int   switch_debugger = 0;
 unsigned int   current_debugger = 0;
 unsigned int   active_debugger = 0;
 unsigned int   debug_mode=0;
-unsigned int   disableDebugOuput = TRUE;
+unsigned int   disable_debug_output = TRUE;
 unsigned int   systemLogDiags = FALSE;
 unsigned int   systemLogDiags = FALSE;
-unsigned int    logPanicDataToScreen = FALSE;
 unsigned int   panicDebugging = FALSE;
 unsigned int   panicDebugging = FALSE;
+unsigned int   logPanicDataToScreen = FALSE;
 
 int mach_assert = 1;
 
 
 int mach_assert = 1;
 
@@ -94,19 +109,46 @@ unsigned long              panic_caller;
 char *debug_buf;
 ppnum_t debug_buf_page;
 char *debug_buf_ptr;
 char *debug_buf;
 ppnum_t debug_buf_page;
 char *debug_buf_ptr;
-unsigned int debug_buf_size = 0;
+unsigned int debug_buf_size;
+
+static char model_name[64];
+
+struct pasc {
+  unsigned a: 7;
+  unsigned b: 7;
+  unsigned c: 7;
+  unsigned d: 7;
+  unsigned e: 7;
+  unsigned f: 7;
+  unsigned g: 7;
+  unsigned h: 7;
+}  __attribute__((packed));
+
+typedef struct pasc pasc_t;
 
 void
 Assert(
        const char      *file,
        int             line,
 
 void
 Assert(
        const char      *file,
        int             line,
-       const char      *expression)
+#if CONFIG_NO_PANIC_STRINGS
+       __unused const char     *expression
+#else
+       const char      *expression
+#endif
+      )
 {
 {
+       int saved_return_on_panic;
+
        if (!mach_assert) {
                return;
        }
        if (!mach_assert) {
                return;
        }
-       panic("{%d} Assertion failed: file \"%s\", line %d: %s\n", 
-              cpu_number(), file, line, expression);
+
+       saved_return_on_panic = return_on_panic;
+       return_on_panic = 1;
+
+       panic_plain("%s:%d Assertion failed: %s", file, line, expression);
+
+       return_on_panic = saved_return_on_panic;
 }
 
 /*
 }
 
 /*
@@ -136,6 +178,31 @@ panic_init(void)
        panic_caller = 0;
 }
 
        panic_caller = 0;
 }
 
+void
+debug_log_init(void)
+{
+       if (debug_buf_size != 0)
+               return;
+       if (kmem_alloc(kernel_map, (vm_offset_t *) &debug_buf, PAGE_SIZE)
+                       != KERN_SUCCESS)
+               panic("cannot allocate debug_buf\n");
+       debug_buf_ptr = debug_buf;
+       debug_buf_size = PAGE_SIZE;
+        debug_buf_page = pmap_find_phys(kernel_pmap,
+                                       (addr64_t)(uintptr_t)debug_buf_ptr);
+}
+
+#if __i386__
+#define panic_stop()   pmCPUHalt(PM_HALT_PANIC)
+#define panic_safe()   pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
+#define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
+#else
+#define panic_stop()   { while (1) ; }
+#define panic_safe()
+#define panic_normal()
+#endif
+
+#undef panic(...) 
 void
 panic(const char *str, ...)
 {
 void
 panic(const char *str, ...)
 {
@@ -147,22 +214,24 @@ panic(const char *str, ...)
        s = splhigh();
        disable_preemption();
 
        s = splhigh();
        disable_preemption();
 
+       panic_safe();
+
 #ifdef __ppc__
        lastTrace = LLTraceSet(0);              /* Disable low-level tracing */
 #endif
 
        thread = current_thread();              /* Get failing thread */
        wq = thread->wait_queue;                /* Save the old value */
 #ifdef __ppc__
        lastTrace = LLTraceSet(0);              /* Disable low-level tracing */
 #endif
 
        thread = current_thread();              /* Get failing thread */
        wq = thread->wait_queue;                /* Save the old value */
-       thread->wait_queue = 0;                 /* Clear the wait so we do not get double panics when we try locks */
+       thread->wait_queue = NULL;              /* Clear the wait so we do not get double panics when we try locks */
 
        if( logPanicDataToScreen )
 
        if( logPanicDataToScreen )
-               disableDebugOuput = FALSE;
+               disable_debug_output = FALSE;
                
        debug_mode = TRUE;
 
        /* panic_caller is initialized to 0.  If set, don't change it */
        if ( ! panic_caller )
                
        debug_mode = TRUE;
 
        /* panic_caller is initialized to 0.  If set, don't change it */
        if ( ! panic_caller )
-               panic_caller = (unsigned long) __builtin_return_address(0);
+               panic_caller = (unsigned long)(char *)__builtin_return_address(0);
        
 restart:
        PANIC_LOCK();
        
 restart:
        PANIC_LOCK();
@@ -181,7 +250,7 @@ restart:
                        PANIC_UNLOCK();
                        Debugger("double panic");
                        printf("double panic:  We are hanging here...\n");
                        PANIC_UNLOCK();
                        Debugger("double panic");
                        printf("double panic:  We are hanging here...\n");
-                       while(1);
+                       panic_stop();
                        /* NOTREACHED */
                }
        }
                        /* NOTREACHED */
                }
        }
@@ -190,10 +259,12 @@ restart:
        panicwait = 1;
 
        PANIC_UNLOCK();
        panicwait = 1;
 
        PANIC_UNLOCK();
-       kdb_printf("panic(cpu %d caller 0x%08X): ", (unsigned) paniccpu, panic_caller);
-       va_start(listp, str);
-       _doprnt(str, &listp, consdebug_putc, 0);
-       va_end(listp);
+       kdb_printf("panic(cpu %d caller 0x%08lX): ", (unsigned) paniccpu, panic_caller);
+       if (str) {
+               va_start(listp, str);
+               _doprnt(str, &listp, consdebug_putc, 0);
+               va_end(listp);
+       }
        kdb_printf("\n");
 
        /*
        kdb_printf("\n");
 
        /*
@@ -208,18 +279,21 @@ restart:
        panicstr = (char *)0;
        PANIC_UNLOCK();
        thread->wait_queue = wq;        /* Restore the wait queue */
        panicstr = (char *)0;
        PANIC_UNLOCK();
        thread->wait_queue = wq;        /* Restore the wait queue */
+
        if (return_on_panic) {
        if (return_on_panic) {
+               panic_normal();
                enable_preemption();
                splx(s);
                return;
        }
                enable_preemption();
                splx(s);
                return;
        }
+
        kdb_printf("panic: We are hanging here...\n");
        kdb_printf("panic: We are hanging here...\n");
-       while(1);
+       panic_stop();
        /* NOTREACHED */
 }
 
 void
        /* NOTREACHED */
 }
 
 void
-log(int level, char *fmt, ...)
+log(__unused int level, char *fmt, ...)
 {
        va_list listp;
 
 {
        va_list listp;
 
@@ -235,23 +309,181 @@ log(int level, char *fmt, ...)
 #endif
 }
 
 #endif
 }
 
-void
-debug_log_init(void)
-{
-       if (debug_buf_size != 0)
-               return;
-       if (kmem_alloc(kernel_map, (vm_offset_t *) &debug_buf, PAGE_SIZE) != KERN_SUCCESS)
-               panic("cannot allocate debug_buf \n");
-       debug_buf_ptr = debug_buf;
-       debug_buf_size = PAGE_SIZE;
-        debug_buf_page = pmap_find_phys(kernel_pmap, (addr64_t)(uintptr_t)debug_buf_ptr);
-}
-
 void
 debug_putc(char c)
 {
 void
 debug_putc(char c)
 {
-       if ((debug_buf_size != 0) && ((debug_buf_ptr-debug_buf) < debug_buf_size)) {
+       if ((debug_buf_size != 0) &&
+               ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
                *debug_buf_ptr=c;
                debug_buf_ptr++;
        }
 }
                *debug_buf_ptr=c;
                debug_buf_ptr++;
        }
 }
+
+/* In-place packing routines -- inefficient, but they're called at most once.
+ */
+
+int packA(char *inbuf, uint32_t length, uint32_t buflen)
+{
+  unsigned int i, j = 0;
+  pasc_t pack;
+  
+  length = MIN(((length & ~7) +8), buflen);
+
+  for (i = 0; i < length; i+=8)
+    {
+      pack.a = inbuf[i];
+      pack.b = inbuf[i+1];
+      pack.c = inbuf[i+2];
+      pack.d = inbuf[i+3];
+      pack.e = inbuf[i+4];
+      pack.f = inbuf[i+5];
+      pack.g = inbuf[i+6];
+      pack.h = inbuf[i+7];
+      bcopy ((char *) &pack, inbuf + j, 7);
+      j += 7;
+    }
+  return ((length * 7)/8);
+}
+
+void unpackA(char *inbuf, uint32_t length)
+{
+       pasc_t packs;
+       unsigned i = 0;
+       length = (length * 8)/7;
+
+       while (i < length) {
+         packs = *(pasc_t *)&inbuf[i];
+         bcopy(&inbuf[i+7], &inbuf[i+8], MAX(0, (int) (length - i - 8)));
+         inbuf[i++] = packs.a;
+         inbuf[i++] = packs.b;
+         inbuf[i++] = packs.c;
+         inbuf[i++] = packs.d;
+         inbuf[i++] = packs.e;
+         inbuf[i++] = packs.f;
+         inbuf[i++] = packs.g;
+         inbuf[i++] = packs.h;
+       }
+}
+
+extern void *proc_name_address(void *p);
+
+static void
+panic_display_process_name(void) {
+       char proc_name[32] = "Unknown";
+       task_t ctask = 0;
+       void *cbsd_info = 0;
+
+       if (ml_nofault_copy((vm_offset_t)&current_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 (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);
+}
+
+unsigned       panic_active(void) {
+       return ((panicstr != (char *) 0));
+}
+
+void populate_model_name(char *model_string) {
+       strlcpy(model_name, model_string, sizeof(model_name));
+}
+
+static 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))
+               return;
+
+       model_name[sizeof(model_name) - 1] = '\0';
+
+       if (model_name[0] != 0)
+               kdb_printf("System model name: %s\n", model_name);
+}
+
+extern const char version[];
+extern char osversion[];
+
+__private_extern__ void panic_display_system_configuration(void) {
+       static boolean_t config_displayed = FALSE;
+
+       panic_display_process_name();
+       if (config_displayed == FALSE) {
+               kdb_printf("\nMac OS version:\n%s\n",
+                   (osversion[0] != 0) ? osversion : "Not yet set");
+               kdb_printf("\nKernel version:\n%s\n",version);
+               panic_display_model_name();
+               config_displayed = TRUE;
+       }
+}
+
+#if !MACH_KDP
+static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
+unsigned int not_in_kdp = 1;
+
+/* XXX ugly forward declares to stop warnings */
+void *kdp_get_interface(void);
+void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
+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);
+
+void *
+kdp_get_interface( void)
+{
+        return(void *)0;
+}
+
+unsigned int
+kdp_get_ip_address(void )
+{ return 0; }
+
+struct ether_addr
+kdp_get_mac_addr(void)
+{       
+        return kdp_current_mac_address;
+}
+
+void
+kdp_set_ip_and_mac_addresses(   
+        __unused struct in_addr          *ipaddr,
+        __unused struct ether_addr       *macaddr)
+{}
+
+void
+kdp_set_gateway_mac(__unused void *gatewaymac)
+{}
+
+void
+kdp_set_interface(__unused void *ifp)
+{}
+
+void
+kdp_register_send_receive(__unused void *send, __unused void *receive)
+{}
+
+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)
+{}
+
+int
+kdp_stack_snapshot_geterror(void)
+{       
+        return -1;
+}
+
+int
+kdp_stack_snapshot_bytes_traced(void)
+{       
+        return 0;
+}
+
+#endif