]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/debug.c
xnu-792.25.20.tar.gz
[apple/xnu.git] / osfmk / kern / debug.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
6601e61a 4 * @APPLE_LICENSE_HEADER_START@
1c79356b 5 *
6601e61a
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
8f6c56a5 11 *
6601e61a
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6601e61a
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
8f6c56a5 19 *
6601e61a 20 * @APPLE_LICENSE_HEADER_END@
1c79356b
A
21 */
22/*
23 * @OSF_COPYRIGHT@
24 */
25/*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50
51#include <mach_assert.h>
52#include <mach_kdb.h>
53#include <mach_kgdb.h>
54#include <mach_kdp.h>
1c79356b
A
55
56#include <kern/cpu_number.h>
57#include <kern/lock.h>
58#include <kern/spl.h>
59#include <kern/thread.h>
60#include <kern/assert.h>
61#include <kern/sched_prim.h>
62#include <kern/misc_protos.h>
9bccf70c 63#include <vm/vm_kern.h>
91447636 64#include <vm/pmap.h>
1c79356b
A
65#include <stdarg.h>
66
9bccf70c
A
67#ifdef __ppc__
68#include <ppc/Firmware.h>
69#include <ppc/low_trace.h>
70#endif
71
1c79356b
A
72unsigned int halt_in_debugger = 0;
73unsigned int switch_debugger = 0;
74unsigned int current_debugger = 0;
75unsigned int active_debugger = 0;
76unsigned int debug_mode=0;
77unsigned int disableDebugOuput = TRUE;
78unsigned int systemLogDiags = FALSE;
55e303ae 79unsigned int logPanicDataToScreen = FALSE;
91447636 80unsigned int panicDebugging = FALSE;
1c79356b
A
81
82int mach_assert = 1;
83
55e303ae 84const char *panicstr = (char *) 0;
1c79356b
A
85decl_simple_lock_data(,panic_lock)
86int paniccpu;
87volatile int panicwait;
91447636 88volatile unsigned int nestedpanic= 0;
1c79356b
A
89unsigned int panic_is_inited = 0;
90unsigned int return_on_panic = 0;
91447636 91unsigned long panic_caller;
1c79356b 92
9bccf70c 93char *debug_buf;
91447636 94ppnum_t debug_buf_page;
9bccf70c
A
95char *debug_buf_ptr;
96unsigned int debug_buf_size = 0;
97
1c79356b
A
98void
99Assert(
100 const char *file,
101 int line,
102 const char *expression)
103{
104 if (!mach_assert) {
105 return;
106 }
107 panic("{%d} Assertion failed: file \"%s\", line %d: %s\n",
108 cpu_number(), file, line, expression);
109}
110
111/*
112 * Carefully use the panic_lock. There's always a chance that
113 * somehow we'll call panic before getting to initialize the
114 * panic_lock -- in this case, we'll assume that the world is
115 * in uniprocessor mode and just avoid using the panic lock.
116 */
117#define PANIC_LOCK() \
118MACRO_BEGIN \
119 if (panic_is_inited) \
120 simple_lock(&panic_lock); \
121MACRO_END
122
123#define PANIC_UNLOCK() \
124MACRO_BEGIN \
125 if (panic_is_inited) \
126 simple_unlock(&panic_lock); \
127MACRO_END
128
129
130void
131panic_init(void)
132{
91447636 133 simple_lock_init(&panic_lock, 0);
1c79356b 134 panic_is_inited = 1;
91447636 135 panic_caller = 0;
1c79356b
A
136}
137
138void
139panic(const char *str, ...)
140{
141 va_list listp;
142 spl_t s;
143 thread_t thread;
91447636 144 wait_queue_t wq;
1c79356b
A
145
146 s = splhigh();
55e303ae 147 disable_preemption();
1c79356b 148
9bccf70c
A
149#ifdef __ppc__
150 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
151#endif
152
1c79356b 153 thread = current_thread(); /* Get failing thread */
91447636 154 wq = thread->wait_queue; /* Save the old value */
1c79356b
A
155 thread->wait_queue = 0; /* Clear the wait so we do not get double panics when we try locks */
156
9bccf70c
A
157 if( logPanicDataToScreen )
158 disableDebugOuput = FALSE;
159
1c79356b 160 debug_mode = TRUE;
91447636
A
161
162 /* panic_caller is initialized to 0. If set, don't change it */
163 if ( ! panic_caller )
164 panic_caller = (unsigned long) __builtin_return_address(0);
165
1c79356b
A
166restart:
167 PANIC_LOCK();
168 if (panicstr) {
169 if (cpu_number() != paniccpu) {
170 PANIC_UNLOCK();
171 /*
172 * Wait until message has been printed to identify correct
173 * cpu that made the first panic.
174 */
175 while (panicwait)
176 continue;
177 goto restart;
178 } else {
179 nestedpanic +=1;
180 PANIC_UNLOCK();
181 Debugger("double panic");
1c79356b
A
182 printf("double panic: We are hanging here...\n");
183 while(1);
184 /* NOTREACHED */
185 }
186 }
187 panicstr = str;
188 paniccpu = cpu_number();
189 panicwait = 1;
190
191 PANIC_UNLOCK();
91447636 192 kdb_printf("panic(cpu %d caller 0x%08X): ", (unsigned) paniccpu, panic_caller);
1c79356b 193 va_start(listp, str);
9bccf70c 194 _doprnt(str, &listp, consdebug_putc, 0);
1c79356b 195 va_end(listp);
9bccf70c 196 kdb_printf("\n");
1c79356b
A
197
198 /*
199 * Release panicwait indicator so that other cpus may call Debugger().
200 */
201 panicwait = 0;
202 Debugger("panic");
203 /*
204 * Release panicstr so that we can handle normally other panics.
205 */
206 PANIC_LOCK();
207 panicstr = (char *)0;
208 PANIC_UNLOCK();
91447636 209 thread->wait_queue = wq; /* Restore the wait queue */
55e303ae
A
210 if (return_on_panic) {
211 enable_preemption();
212 splx(s);
1c79356b 213 return;
55e303ae 214 }
9bccf70c 215 kdb_printf("panic: We are hanging here...\n");
1c79356b
A
216 while(1);
217 /* NOTREACHED */
218}
219
220void
221log(int level, char *fmt, ...)
222{
223 va_list listp;
1c79356b
A
224
225#ifdef lint
226 level++;
227#endif /* lint */
228#ifdef MACH_BSD
229 disable_preemption();
230 va_start(listp, fmt);
231 _doprnt(fmt, &listp, conslog_putc, 0);
232 va_end(listp);
233 enable_preemption();
234#endif
235}
9bccf70c
A
236
237void
238debug_log_init(void)
239{
240 if (debug_buf_size != 0)
241 return;
242 if (kmem_alloc(kernel_map, (vm_offset_t *) &debug_buf, PAGE_SIZE) != KERN_SUCCESS)
243 panic("cannot allocate debug_buf \n");
244 debug_buf_ptr = debug_buf;
245 debug_buf_size = PAGE_SIZE;
91447636 246 debug_buf_page = pmap_find_phys(kernel_pmap, (addr64_t)(uintptr_t)debug_buf_ptr);
9bccf70c
A
247}
248
249void
250debug_putc(char c)
251{
252 if ((debug_buf_size != 0) && ((debug_buf_ptr-debug_buf) < debug_buf_size)) {
253 *debug_buf_ptr=c;
254 debug_buf_ptr++;
255 }
256}