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/kalloc.h>
64 #include <kern/lock.h>
66 #include <kern/thread.h>
67 #include <kern/assert.h>
68 #include <kern/sched_prim.h>
69 #include <kern/misc_protos.h>
70 #include <kern/clock.h>
71 #include <vm/vm_kern.h>
75 #include <kdp/kdp_udp.h>
79 #include <ppc/Firmware.h>
80 #include <ppc/low_trace.h>
83 #if defined(__i386__) || defined(__x86_64__)
84 #include <i386/cpu_threads.h>
85 #include <i386/pmCPU.h>
88 #include <IOKit/IOPlatformExpert.h>
90 #include <sys/kdebug.h>
91 #include <libkern/OSKextLibPrivate.h>
93 unsigned int halt_in_debugger
= 0;
94 unsigned int switch_debugger
= 0;
95 unsigned int current_debugger
= 0;
96 unsigned int active_debugger
= 0;
97 unsigned int debug_mode
=0;
98 unsigned int disable_debug_output
= TRUE
;
99 unsigned int systemLogDiags
= FALSE
;
100 unsigned int panicDebugging
= FALSE
;
101 unsigned int logPanicDataToScreen
= FALSE
;
105 const char *panicstr
= (char *) 0;
106 decl_simple_lock_data(,panic_lock
)
108 volatile int panicwait
;
109 volatile unsigned int nestedpanic
= 0;
110 unsigned int panic_is_inited
= 0;
111 unsigned int return_on_panic
= 0;
112 unsigned long panic_caller
;
115 #define DEBUG_BUF_SIZE (PAGE_SIZE)
117 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
120 char debug_buf
[DEBUG_BUF_SIZE
];
121 char *debug_buf_ptr
= debug_buf
;
122 unsigned int debug_buf_size
= sizeof(debug_buf
);
124 static char model_name
[64];
135 } __attribute__((packed
));
137 typedef struct pasc pasc_t
;
139 /* Prevent CPP from breaking the definition below */
140 #if CONFIG_NO_PANIC_STRINGS
148 const char *expression
151 int saved_return_on_panic
;
157 saved_return_on_panic
= return_on_panic
;
160 panic_plain("%s:%d Assertion failed: %s", file
, line
, expression
);
162 return_on_panic
= saved_return_on_panic
;
166 * Carefully use the panic_lock. There's always a chance that
167 * somehow we'll call panic before getting to initialize the
168 * panic_lock -- in this case, we'll assume that the world is
169 * in uniprocessor mode and just avoid using the panic lock.
171 #define PANIC_LOCK() \
173 if (panic_is_inited) \
174 simple_lock(&panic_lock); \
177 #define PANIC_UNLOCK() \
179 if (panic_is_inited) \
180 simple_unlock(&panic_lock); \
187 simple_lock_init(&panic_lock
, 0);
195 if (debug_buf_size
!= 0)
197 debug_buf_ptr
= debug_buf
;
198 debug_buf_size
= sizeof(debug_buf
);
201 #if defined(__i386__) || defined(__x86_64__)
202 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
203 #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
204 #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
206 #define panic_stop() { while (1) ; }
208 #define panic_normal()
212 * Prevent CPP from breaking the definition below,
213 * since all clients get a #define to prepend line numbers
217 void _consume_panic_args(int a __unused
, ...)
223 panic(const char *str
, ...)
232 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
235 disable_preemption();
240 lastTrace
= LLTraceSet(0); /* Disable low-level tracing */
243 thread
= current_thread(); /* Get failing thread */
244 wq
= thread
->wait_queue
; /* Save the old value */
245 thread
->wait_queue
= NULL
; /* Clear the wait so we do not get double panics when we try locks */
247 if( logPanicDataToScreen
)
248 disable_debug_output
= FALSE
;
252 /* panic_caller is initialized to 0. If set, don't change it */
253 if ( ! panic_caller
)
254 panic_caller
= (unsigned long)(char *)__builtin_return_address(0);
259 if (cpu_number() != paniccpu
) {
262 * Wait until message has been printed to identify correct
263 * cpu that made the first panic.
271 Debugger("double panic");
272 printf("double panic: We are hanging here...\n");
278 paniccpu
= cpu_number();
282 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu
, panic_caller
);
284 va_start(listp
, str
);
285 _doprnt(str
, &listp
, consdebug_putc
, 0);
291 * Release panicwait indicator so that other cpus may call Debugger().
296 * Release panicstr so that we can handle normally other panics.
299 panicstr
= (char *)0;
301 thread
->wait_queue
= wq
; /* Restore the wait queue */
303 if (return_on_panic
) {
310 kdb_printf("panic: We are hanging here...\n");
316 log(__unused
int level
, char *fmt
, ...)
324 disable_preemption();
325 va_start(listp
, fmt
);
326 _doprnt(fmt
, &listp
, conslog_putc
, 0);
335 if ((debug_buf_size
!= 0) &&
336 ((debug_buf_ptr
-debug_buf
) < (int)debug_buf_size
)) {
342 /* In-place packing routines -- inefficient, but they're called at most once.
345 int packA(char *inbuf
, uint32_t length
, uint32_t buflen
)
347 unsigned int i
, j
= 0;
350 length
= MIN(((length
& ~7) +8), buflen
);
352 for (i
= 0; i
< length
; i
+=8)
362 bcopy ((char *) &pack
, inbuf
+ j
, 7);
365 return ((length
* 7)/8);
368 void unpackA(char *inbuf
, uint32_t length
)
372 length
= (length
* 8)/7;
375 packs
= *(pasc_t
*)&inbuf
[i
];
376 bcopy(&inbuf
[i
+7], &inbuf
[i
+8], MAX(0, (int) (length
- i
- 8)));
377 inbuf
[i
++] = packs
.a
;
378 inbuf
[i
++] = packs
.b
;
379 inbuf
[i
++] = packs
.c
;
380 inbuf
[i
++] = packs
.d
;
381 inbuf
[i
++] = packs
.e
;
382 inbuf
[i
++] = packs
.f
;
383 inbuf
[i
++] = packs
.g
;
384 inbuf
[i
++] = packs
.h
;
388 extern void *proc_name_address(void *p
);
391 panic_display_process_name(void) {
392 char proc_name
[32] = "Unknown";
396 if (ml_nofault_copy((vm_offset_t
)¤t_thread()->task
, (vm_offset_t
) &ctask
, sizeof(task_t
)) == sizeof(task_t
))
397 if(ml_nofault_copy((vm_offset_t
)&ctask
->bsd_info
, (vm_offset_t
)&cbsd_info
, sizeof(&ctask
->bsd_info
)) == sizeof(&ctask
->bsd_info
))
398 if (cbsd_info
&& (ml_nofault_copy((vm_offset_t
) proc_name_address(cbsd_info
), (vm_offset_t
) &proc_name
, sizeof(proc_name
)) > 0))
399 proc_name
[sizeof(proc_name
) - 1] = '\0';
400 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name
);
403 unsigned panic_active(void) {
404 return ((panicstr
!= (char *) 0));
407 void populate_model_name(char *model_string
) {
408 strlcpy(model_name
, model_string
, sizeof(model_name
));
411 static void panic_display_model_name(void) {
412 char tmp_model_name
[sizeof(model_name
)];
414 if (ml_nofault_copy((vm_offset_t
) &model_name
, (vm_offset_t
) &tmp_model_name
, sizeof(model_name
)) != sizeof(model_name
))
417 model_name
[sizeof(model_name
) - 1] = '\0';
419 if (model_name
[0] != 0)
420 kdb_printf("System model name: %s\n", model_name
);
423 static void panic_display_uptime(void) {
425 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime
);
427 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime
);
430 extern const char version
[];
431 extern char osversion
[];
433 __private_extern__
void panic_display_system_configuration(void) {
434 static boolean_t config_displayed
= FALSE
;
436 panic_display_process_name();
437 if (config_displayed
== FALSE
) {
438 kdb_printf("\nMac OS version:\n%s\n",
439 (osversion
[0] != 0) ? osversion
: "Not yet set");
440 kdb_printf("\nKernel version:\n%s\n",version
);
441 panic_display_model_name();
442 panic_display_uptime();
443 config_displayed
= TRUE
;
444 panic_display_zprint();
445 kext_dump_panic_lists(&kdb_log
);
449 extern zone_t first_zone
;
450 extern unsigned int num_zones
, stack_total
;
452 #if defined(__i386__) || defined (__x86_64__)
453 extern unsigned int inuse_ptepages_count
;
456 extern boolean_t panic_include_zprint
;
458 __private_extern__
void panic_display_zprint()
460 if(panic_include_zprint
== TRUE
) {
463 struct zone zone_copy
;
465 if(first_zone
!=NULL
) {
466 if(ml_nofault_copy((vm_offset_t
)first_zone
, (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) == sizeof(struct zone
)) {
467 for (i
= 0; i
< num_zones
; i
++) {
468 if(zone_copy
.cur_size
> (1024*1024)) {
469 kdb_printf("%.20s:%lu\n",zone_copy
.zone_name
,(uintptr_t)zone_copy
.cur_size
);
472 if(zone_copy
.next_zone
== NULL
) {
476 if(ml_nofault_copy((vm_offset_t
)zone_copy
.next_zone
, (vm_offset_t
)&zone_copy
, sizeof(struct zone
)) != sizeof(struct zone
)) {
483 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size
* stack_total
));
485 #if defined(__i386__) || defined (__x86_64__)
486 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE
* inuse_ptepages_count
));
489 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total
);
494 static struct ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
496 /* XXX ugly forward declares to stop warnings */
497 void *kdp_get_interface(void);
498 void kdp_set_ip_and_mac_addresses(struct in_addr
*, struct ether_addr
*);
499 void kdp_set_gateway_mac(void *);
500 void kdp_set_interface(void *);
501 void kdp_register_send_receive(void *, void *);
502 void kdp_unregister_send_receive(void *, void *);
503 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
504 int kdp_stack_snapshot_geterror(void);
505 int kdp_stack_snapshot_bytes_traced(void);
508 kdp_get_interface( void)
514 kdp_get_ip_address(void )
518 kdp_get_mac_addr(void)
520 return kdp_current_mac_address
;
524 kdp_set_ip_and_mac_addresses(
525 __unused
struct in_addr
*ipaddr
,
526 __unused
struct ether_addr
*macaddr
)
530 kdp_set_gateway_mac(__unused
void *gatewaymac
)
534 kdp_set_interface(__unused
void *ifp
)
538 kdp_register_send_receive(__unused
void *send
, __unused
void *receive
)
542 kdp_unregister_send_receive(__unused
void *send
, __unused
void *receive
)
546 kdp_snapshot_preflight(__unused
int pid
, __unused
void * tracebuf
,
547 __unused
uint32_t tracebuf_size
, __unused
uint32_t options
)
551 kdp_stack_snapshot_geterror(void)
557 kdp_stack_snapshot_bytes_traced(void)