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