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 <kern/clock.h>
70 #include <vm/vm_kern.h>
74 #include <kdp/kdp_udp.h>
78 #include <ppc/Firmware.h>
79 #include <ppc/low_trace.h>
83 #include <i386/cpu_threads.h>
84 #include <i386/pmCPU.h>
87 #include <IOKit/IOPlatformExpert.h>
89 unsigned int halt_in_debugger
= 0;
90 unsigned int switch_debugger
= 0;
91 unsigned int current_debugger
= 0;
92 unsigned int active_debugger
= 0;
93 unsigned int debug_mode
=0;
94 unsigned int disable_debug_output
= TRUE
;
95 unsigned int systemLogDiags
= FALSE
;
96 unsigned int panicDebugging
= FALSE
;
97 unsigned int logPanicDataToScreen
= FALSE
;
101 const char *panicstr
= (char *) 0;
102 decl_simple_lock_data(,panic_lock
)
104 volatile int panicwait
;
105 volatile unsigned int nestedpanic
= 0;
106 unsigned int panic_is_inited
= 0;
107 unsigned int return_on_panic
= 0;
108 unsigned long panic_caller
;
111 #define DEBUG_BUF_SIZE (PAGE_SIZE)
113 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
116 char debug_buf
[DEBUG_BUF_SIZE
];
117 char *debug_buf_ptr
= debug_buf
;
118 unsigned int debug_buf_size
= sizeof(debug_buf
);
120 static char model_name
[64];
131 } __attribute__((packed
));
133 typedef struct pasc pasc_t
;
139 #if CONFIG_NO_PANIC_STRINGS
140 __unused
const char *expression
142 const char *expression
146 int saved_return_on_panic
;
152 saved_return_on_panic
= return_on_panic
;
155 panic_plain("%s:%d Assertion failed: %s", file
, line
, expression
);
157 return_on_panic
= saved_return_on_panic
;
161 * Carefully use the panic_lock. There's always a chance that
162 * somehow we'll call panic before getting to initialize the
163 * panic_lock -- in this case, we'll assume that the world is
164 * in uniprocessor mode and just avoid using the panic lock.
166 #define PANIC_LOCK() \
168 if (panic_is_inited) \
169 simple_lock(&panic_lock); \
172 #define PANIC_UNLOCK() \
174 if (panic_is_inited) \
175 simple_unlock(&panic_lock); \
182 simple_lock_init(&panic_lock
, 0);
190 if (debug_buf_size
!= 0)
192 debug_buf_ptr
= debug_buf
;
193 debug_buf_size
= sizeof(debug_buf
);
197 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
198 #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
199 #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
201 #define panic_stop() { while (1) ; }
203 #define panic_normal()
208 panic(const char *str
, ...)
216 disable_preemption();
221 lastTrace
= LLTraceSet(0); /* Disable low-level tracing */
224 thread
= current_thread(); /* Get failing thread */
225 wq
= thread
->wait_queue
; /* Save the old value */
226 thread
->wait_queue
= NULL
; /* Clear the wait so we do not get double panics when we try locks */
228 if( logPanicDataToScreen
)
229 disable_debug_output
= FALSE
;
233 /* panic_caller is initialized to 0. If set, don't change it */
234 if ( ! panic_caller
)
235 panic_caller
= (unsigned long)(char *)__builtin_return_address(0);
240 if (cpu_number() != paniccpu
) {
243 * Wait until message has been printed to identify correct
244 * cpu that made the first panic.
252 Debugger("double panic");
253 printf("double panic: We are hanging here...\n");
259 paniccpu
= cpu_number();
263 kdb_printf("panic(cpu %d caller 0x%08lX): ", (unsigned) paniccpu
, panic_caller
);
265 va_start(listp
, str
);
266 _doprnt(str
, &listp
, consdebug_putc
, 0);
272 * Release panicwait indicator so that other cpus may call Debugger().
277 * Release panicstr so that we can handle normally other panics.
280 panicstr
= (char *)0;
282 thread
->wait_queue
= wq
; /* Restore the wait queue */
284 if (return_on_panic
) {
291 kdb_printf("panic: We are hanging here...\n");
297 log(__unused
int level
, char *fmt
, ...)
305 disable_preemption();
306 va_start(listp
, fmt
);
307 _doprnt(fmt
, &listp
, conslog_putc
, 0);
316 if ((debug_buf_size
!= 0) &&
317 ((debug_buf_ptr
-debug_buf
) < (int)debug_buf_size
)) {
323 /* In-place packing routines -- inefficient, but they're called at most once.
326 int packA(char *inbuf
, uint32_t length
, uint32_t buflen
)
328 unsigned int i
, j
= 0;
331 length
= MIN(((length
& ~7) +8), buflen
);
333 for (i
= 0; i
< length
; i
+=8)
343 bcopy ((char *) &pack
, inbuf
+ j
, 7);
346 return ((length
* 7)/8);
349 void unpackA(char *inbuf
, uint32_t length
)
353 length
= (length
* 8)/7;
356 packs
= *(pasc_t
*)&inbuf
[i
];
357 bcopy(&inbuf
[i
+7], &inbuf
[i
+8], MAX(0, (int) (length
- i
- 8)));
358 inbuf
[i
++] = packs
.a
;
359 inbuf
[i
++] = packs
.b
;
360 inbuf
[i
++] = packs
.c
;
361 inbuf
[i
++] = packs
.d
;
362 inbuf
[i
++] = packs
.e
;
363 inbuf
[i
++] = packs
.f
;
364 inbuf
[i
++] = packs
.g
;
365 inbuf
[i
++] = packs
.h
;
369 extern void *proc_name_address(void *p
);
372 panic_display_process_name(void) {
373 char proc_name
[32] = "Unknown";
377 if (ml_nofault_copy((vm_offset_t
)¤t_thread()->task
, (vm_offset_t
) &ctask
, sizeof(task_t
)) == sizeof(task_t
))
378 if(ml_nofault_copy((vm_offset_t
)&ctask
->bsd_info
, (vm_offset_t
)&cbsd_info
, sizeof(&ctask
->bsd_info
)) == sizeof(&ctask
->bsd_info
))
379 if (cbsd_info
&& (ml_nofault_copy((vm_offset_t
) proc_name_address(cbsd_info
), (vm_offset_t
) &proc_name
, sizeof(proc_name
)) > 0))
380 proc_name
[sizeof(proc_name
) - 1] = '\0';
381 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name
);
384 unsigned panic_active(void) {
385 return ((panicstr
!= (char *) 0));
388 void populate_model_name(char *model_string
) {
389 strlcpy(model_name
, model_string
, sizeof(model_name
));
392 static void panic_display_model_name(void) {
393 char tmp_model_name
[sizeof(model_name
)];
395 if (ml_nofault_copy((vm_offset_t
) &model_name
, (vm_offset_t
) &tmp_model_name
, sizeof(model_name
)) != sizeof(model_name
))
398 model_name
[sizeof(model_name
) - 1] = '\0';
400 if (model_name
[0] != 0)
401 kdb_printf("System model name: %s\n", model_name
);
404 static void panic_display_uptime(void) {
406 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime
);
408 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime
);
411 extern const char version
[];
412 extern char osversion
[];
414 __private_extern__
void panic_display_system_configuration(void) {
415 static boolean_t config_displayed
= FALSE
;
417 panic_display_process_name();
418 if (config_displayed
== FALSE
) {
419 kdb_printf("\nMac OS version:\n%s\n",
420 (osversion
[0] != 0) ? osversion
: "Not yet set");
421 kdb_printf("\nKernel version:\n%s\n",version
);
422 panic_display_model_name();
423 panic_display_uptime();
424 config_displayed
= TRUE
;
428 extern zone_t first_zone
;
429 extern unsigned int num_zones
, stack_total
;
431 #if defined(__i386__)
432 extern unsigned int inuse_ptepages_count
;
435 extern boolean_t panic_include_zprint
;
436 extern vm_size_t kalloc_large_total
;
438 __private_extern__
void panic_display_zprint()
440 if(panic_include_zprint
== TRUE
) {
443 struct zone zone_copy
;
445 if(first_zone
!=NULL
) {
446 if(ml_nofault_copy((vm_offset_t
)first_zone
, (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) == sizeof(struct zone
)) {
447 for (i
= 0; i
< num_zones
; i
++) {
448 if(zone_copy
.cur_size
> (1024*1024)) {
449 kdb_printf("%.20s:%lu\n",zone_copy
.zone_name
,(uintptr_t)zone_copy
.cur_size
);
452 if(zone_copy
.next_zone
== NULL
) {
456 if(ml_nofault_copy((vm_offset_t
)zone_copy
.next_zone
, (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) != sizeof(struct zone
)) {
463 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(KERNEL_STACK_SIZE
* stack_total
));
464 #if defined(__i386__)
465 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE
* inuse_ptepages_count
));
467 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total
);
472 static struct ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
473 unsigned int not_in_kdp
= 1;
475 /* XXX ugly forward declares to stop warnings */
476 void *kdp_get_interface(void);
477 void kdp_set_ip_and_mac_addresses(struct in_addr
*, struct ether_addr
*);
478 void kdp_set_gateway_mac(void *);
479 void kdp_set_interface(void *);
480 void kdp_register_send_receive(void *, void *);
481 void kdp_unregister_send_receive(void *, void *);
482 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
483 int kdp_stack_snapshot_geterror(void);
484 int kdp_stack_snapshot_bytes_traced(void);
487 kdp_get_interface( void)
493 kdp_get_ip_address(void )
497 kdp_get_mac_addr(void)
499 return kdp_current_mac_address
;
503 kdp_set_ip_and_mac_addresses(
504 __unused
struct in_addr
*ipaddr
,
505 __unused
struct ether_addr
*macaddr
)
509 kdp_set_gateway_mac(__unused
void *gatewaymac
)
513 kdp_set_interface(__unused
void *ifp
)
517 kdp_register_send_receive(__unused
void *send
, __unused
void *receive
)
521 kdp_unregister_send_receive(__unused
void *send
, __unused
void *receive
)
525 kdp_snapshot_preflight(__unused
int pid
, __unused
void * tracebuf
,
526 __unused
uint32_t tracebuf_size
, __unused
uint32_t options
)
530 kdp_stack_snapshot_geterror(void)
536 kdp_stack_snapshot_bytes_traced(void)