]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/debug.c
xnu-517.3.15.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;
55e303ae 82unsigned int logPanicDataToScreen = FALSE;
9bccf70c 83#ifdef __ppc__
55e303ae 84 unsigned int panicDebugging = FALSE;
9bccf70c 85#else
55e303ae 86 unsigned int panicDebugging = TRUE;
9bccf70c 87#endif
1c79356b
A
88
89int mach_assert = 1;
90
55e303ae 91const char *panicstr = (char *) 0;
1c79356b
A
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();
55e303ae 151 disable_preemption();
1c79356b 152
9bccf70c
A
153#ifdef __ppc__
154 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
155#endif
156
1c79356b
A
157 thread = current_thread(); /* Get failing thread */
158 save_waits[cpu_number()] = thread->wait_queue; /* Save the old value */
159 thread->wait_queue = 0; /* Clear the wait so we do not get double panics when we try locks */
160
9bccf70c
A
161 if( logPanicDataToScreen )
162 disableDebugOuput = FALSE;
163
1c79356b
A
164 debug_mode = TRUE;
165restart:
166 PANIC_LOCK();
167 if (panicstr) {
168 if (cpu_number() != paniccpu) {
169 PANIC_UNLOCK();
170 /*
171 * Wait until message has been printed to identify correct
172 * cpu that made the first panic.
173 */
174 while (panicwait)
175 continue;
176 goto restart;
177 } else {
178 nestedpanic +=1;
179 PANIC_UNLOCK();
180 Debugger("double panic");
1c79356b
A
181 printf("double panic: We are hanging here...\n");
182 while(1);
183 /* NOTREACHED */
184 }
185 }
186 panicstr = str;
187 paniccpu = cpu_number();
188 panicwait = 1;
189
190 PANIC_UNLOCK();
9bccf70c 191 kdb_printf("panic(cpu %d): ", (unsigned) paniccpu);
1c79356b 192 va_start(listp, str);
9bccf70c 193 _doprnt(str, &listp, consdebug_putc, 0);
1c79356b 194 va_end(listp);
9bccf70c 195 kdb_printf("\n");
1c79356b
A
196
197 /*
198 * Release panicwait indicator so that other cpus may call Debugger().
199 */
200 panicwait = 0;
201 Debugger("panic");
202 /*
203 * Release panicstr so that we can handle normally other panics.
204 */
205 PANIC_LOCK();
206 panicstr = (char *)0;
207 PANIC_UNLOCK();
1c79356b 208 thread->wait_queue = save_waits[cpu_number()]; /* Restore the wait queue */
55e303ae
A
209 if (return_on_panic) {
210 enable_preemption();
211 splx(s);
1c79356b 212 return;
55e303ae 213 }
9bccf70c 214 kdb_printf("panic: We are hanging here...\n");
1c79356b
A
215 while(1);
216 /* NOTREACHED */
217}
218
219void
220log(int level, char *fmt, ...)
221{
222 va_list listp;
1c79356b
A
223
224#ifdef lint
225 level++;
226#endif /* lint */
227#ifdef MACH_BSD
228 disable_preemption();
229 va_start(listp, fmt);
230 _doprnt(fmt, &listp, conslog_putc, 0);
231 va_end(listp);
232 enable_preemption();
233#endif
234}
9bccf70c
A
235
236void
237debug_log_init(void)
238{
239 if (debug_buf_size != 0)
240 return;
241 if (kmem_alloc(kernel_map, (vm_offset_t *) &debug_buf, PAGE_SIZE) != KERN_SUCCESS)
242 panic("cannot allocate debug_buf \n");
243 debug_buf_ptr = debug_buf;
244 debug_buf_size = PAGE_SIZE;
245}
246
247void
248debug_putc(char c)
249{
250 if ((debug_buf_size != 0) && ((debug_buf_ptr-debug_buf) < debug_buf_size)) {
251 *debug_buf_ptr=c;
252 debug_buf_ptr++;
253 }
254}