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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
57 #include <mach_assert.h>
59 #include <mach_kgdb.h>
62 #include <kern/cpu_number.h>
63 #include <kern/lock.h>
65 #include <kern/thread.h>
66 #include <kern/assert.h>
67 #include <kern/sched_prim.h>
68 #include <kern/misc_protos.h>
69 #include <vm/vm_kern.h>
73 #include <kdp/kdp_udp.h>
77 #include <ppc/Firmware.h>
78 #include <ppc/low_trace.h>
82 #include <i386/cpu_threads.h>
83 #include <i386/pmCPU.h>
86 #include <IOKit/IOPlatformExpert.h>
88 unsigned int halt_in_debugger
= 0;
89 unsigned int switch_debugger
= 0;
90 unsigned int current_debugger
= 0;
91 unsigned int active_debugger
= 0;
92 unsigned int debug_mode
=0;
93 unsigned int disable_debug_output
= TRUE
;
94 unsigned int systemLogDiags
= FALSE
;
95 unsigned int panicDebugging
= FALSE
;
96 unsigned int logPanicDataToScreen
= FALSE
;
100 const char *panicstr
= (char *) 0;
101 decl_simple_lock_data(,panic_lock
)
103 volatile int panicwait
;
104 volatile unsigned int nestedpanic
= 0;
105 unsigned int panic_is_inited
= 0;
106 unsigned int return_on_panic
= 0;
107 unsigned long panic_caller
;
110 ppnum_t debug_buf_page
;
112 unsigned int debug_buf_size
;
114 static char model_name
[64];
125 } __attribute__((packed
));
127 typedef struct pasc pasc_t
;
133 #if CONFIG_NO_PANIC_STRINGS
134 __unused
const char *expression
136 const char *expression
140 int saved_return_on_panic
;
146 saved_return_on_panic
= return_on_panic
;
149 panic_plain("%s:%d Assertion failed: %s", file
, line
, expression
);
151 return_on_panic
= saved_return_on_panic
;
155 * Carefully use the panic_lock. There's always a chance that
156 * somehow we'll call panic before getting to initialize the
157 * panic_lock -- in this case, we'll assume that the world is
158 * in uniprocessor mode and just avoid using the panic lock.
160 #define PANIC_LOCK() \
162 if (panic_is_inited) \
163 simple_lock(&panic_lock); \
166 #define PANIC_UNLOCK() \
168 if (panic_is_inited) \
169 simple_unlock(&panic_lock); \
176 simple_lock_init(&panic_lock
, 0);
184 if (debug_buf_size
!= 0)
186 if (kmem_alloc(kernel_map
, (vm_offset_t
*) &debug_buf
, PAGE_SIZE
)
188 panic("cannot allocate debug_buf\n");
189 debug_buf_ptr
= debug_buf
;
190 debug_buf_size
= PAGE_SIZE
;
191 debug_buf_page
= pmap_find_phys(kernel_pmap
,
192 (addr64_t
)(uintptr_t)debug_buf_ptr
);
196 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
197 #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
198 #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
200 #define panic_stop() { while (1) ; }
202 #define panic_normal()
207 panic(const char *str
, ...)
215 disable_preemption();
220 lastTrace
= LLTraceSet(0); /* Disable low-level tracing */
223 thread
= current_thread(); /* Get failing thread */
224 wq
= thread
->wait_queue
; /* Save the old value */
225 thread
->wait_queue
= NULL
; /* Clear the wait so we do not get double panics when we try locks */
227 if( logPanicDataToScreen
)
228 disable_debug_output
= FALSE
;
232 /* panic_caller is initialized to 0. If set, don't change it */
233 if ( ! panic_caller
)
234 panic_caller
= (unsigned long)(char *)__builtin_return_address(0);
239 if (cpu_number() != paniccpu
) {
242 * Wait until message has been printed to identify correct
243 * cpu that made the first panic.
251 Debugger("double panic");
252 printf("double panic: We are hanging here...\n");
258 paniccpu
= cpu_number();
262 kdb_printf("panic(cpu %d caller 0x%08lX): ", (unsigned) paniccpu
, panic_caller
);
264 va_start(listp
, str
);
265 _doprnt(str
, &listp
, consdebug_putc
, 0);
271 * Release panicwait indicator so that other cpus may call Debugger().
276 * Release panicstr so that we can handle normally other panics.
279 panicstr
= (char *)0;
281 thread
->wait_queue
= wq
; /* Restore the wait queue */
283 if (return_on_panic
) {
290 kdb_printf("panic: We are hanging here...\n");
296 log(__unused
int level
, char *fmt
, ...)
304 disable_preemption();
305 va_start(listp
, fmt
);
306 _doprnt(fmt
, &listp
, conslog_putc
, 0);
315 if ((debug_buf_size
!= 0) &&
316 ((debug_buf_ptr
-debug_buf
) < (int)debug_buf_size
)) {
322 /* In-place packing routines -- inefficient, but they're called at most once.
325 int packA(char *inbuf
, uint32_t length
, uint32_t buflen
)
327 unsigned int i
, j
= 0;
330 length
= MIN(((length
& ~7) +8), buflen
);
332 for (i
= 0; i
< length
; i
+=8)
342 bcopy ((char *) &pack
, inbuf
+ j
, 7);
345 return ((length
* 7)/8);
348 void unpackA(char *inbuf
, uint32_t length
)
352 length
= (length
* 8)/7;
355 packs
= *(pasc_t
*)&inbuf
[i
];
356 bcopy(&inbuf
[i
+7], &inbuf
[i
+8], MAX(0, (int) (length
- i
- 8)));
357 inbuf
[i
++] = packs
.a
;
358 inbuf
[i
++] = packs
.b
;
359 inbuf
[i
++] = packs
.c
;
360 inbuf
[i
++] = packs
.d
;
361 inbuf
[i
++] = packs
.e
;
362 inbuf
[i
++] = packs
.f
;
363 inbuf
[i
++] = packs
.g
;
364 inbuf
[i
++] = packs
.h
;
368 extern void *proc_name_address(void *p
);
371 panic_display_process_name(void) {
372 char proc_name
[32] = "Unknown";
376 if (ml_nofault_copy((vm_offset_t
)¤t_thread()->task
, (vm_offset_t
) &ctask
, sizeof(task_t
)) == sizeof(task_t
))
377 if(ml_nofault_copy((vm_offset_t
)&ctask
->bsd_info
, (vm_offset_t
)&cbsd_info
, sizeof(&ctask
->bsd_info
)) == sizeof(&ctask
->bsd_info
))
378 if (cbsd_info
&& (ml_nofault_copy((vm_offset_t
) proc_name_address(cbsd_info
), (vm_offset_t
) &proc_name
, sizeof(proc_name
)) > 0))
379 proc_name
[sizeof(proc_name
) - 1] = '\0';
380 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name
);
383 unsigned panic_active(void) {
384 return ((panicstr
!= (char *) 0));
387 void populate_model_name(char *model_string
) {
388 strlcpy(model_name
, model_string
, sizeof(model_name
));
391 static void panic_display_model_name(void) {
392 char tmp_model_name
[sizeof(model_name
)];
394 if (ml_nofault_copy((vm_offset_t
) &model_name
, (vm_offset_t
) &tmp_model_name
, sizeof(model_name
)) != sizeof(model_name
))
397 model_name
[sizeof(model_name
) - 1] = '\0';
399 if (model_name
[0] != 0)
400 kdb_printf("System model name: %s\n", model_name
);
403 extern const char version
[];
404 extern char osversion
[];
406 __private_extern__
void panic_display_system_configuration(void) {
407 static boolean_t config_displayed
= FALSE
;
409 panic_display_process_name();
410 if (config_displayed
== FALSE
) {
411 kdb_printf("\nMac OS version:\n%s\n",
412 (osversion
[0] != 0) ? osversion
: "Not yet set");
413 kdb_printf("\nKernel version:\n%s\n",version
);
414 panic_display_model_name();
415 config_displayed
= TRUE
;
420 static struct ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
421 unsigned int not_in_kdp
= 1;
423 /* XXX ugly forward declares to stop warnings */
424 void *kdp_get_interface(void);
425 void kdp_set_ip_and_mac_addresses(struct in_addr
*, struct ether_addr
*);
426 void kdp_set_gateway_mac(void *);
427 void kdp_set_interface(void *);
428 void kdp_register_send_receive(void *, void *);
429 void kdp_unregister_send_receive(void *, void *);
430 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
431 int kdp_stack_snapshot_geterror(void);
432 int kdp_stack_snapshot_bytes_traced(void);
435 kdp_get_interface( void)
441 kdp_get_ip_address(void )
445 kdp_get_mac_addr(void)
447 return kdp_current_mac_address
;
451 kdp_set_ip_and_mac_addresses(
452 __unused
struct in_addr
*ipaddr
,
453 __unused
struct ether_addr
*macaddr
)
457 kdp_set_gateway_mac(__unused
void *gatewaymac
)
461 kdp_set_interface(__unused
void *ifp
)
465 kdp_register_send_receive(__unused
void *send
, __unused
void *receive
)
469 kdp_unregister_send_receive(__unused
void *send
, __unused
void *receive
)
473 kdp_snapshot_preflight(__unused
int pid
, __unused
void * tracebuf
,
474 __unused
uint32_t tracebuf_size
, __unused
uint32_t options
)
478 kdp_stack_snapshot_geterror(void)
484 kdp_stack_snapshot_bytes_traced(void)