]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/debug.c
xnu-517.tar.gz
[apple/xnu.git] / osfmk / kern / debug.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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>
67 #include <vm/vm_kern.h>
68 #include <stdarg.h>
69
70 #ifdef __ppc__
71 #include <ppc/Firmware.h>
72 #include <ppc/low_trace.h>
73 #endif
74
75 unsigned int halt_in_debugger = 0;
76 unsigned int switch_debugger = 0;
77 unsigned int current_debugger = 0;
78 unsigned int active_debugger = 0;
79 unsigned int debug_mode=0;
80 unsigned int disableDebugOuput = TRUE;
81 unsigned int systemLogDiags = FALSE;
82 unsigned int logPanicDataToScreen = FALSE;
83 #ifdef __ppc__
84 unsigned int panicDebugging = FALSE;
85 #else
86 unsigned int panicDebugging = TRUE;
87 #endif
88
89 int mach_assert = 1;
90
91 const char *panicstr = (char *) 0;
92 decl_simple_lock_data(,panic_lock)
93 int paniccpu;
94 volatile int panicwait;
95 volatile int nestedpanic= 0;
96 unsigned int panic_is_inited = 0;
97 unsigned int return_on_panic = 0;
98 wait_queue_t save_waits[NCPUS];
99
100 char *debug_buf;
101 char *debug_buf_ptr;
102 unsigned int debug_buf_size = 0;
103
104 void
105 Assert(
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() \
124 MACRO_BEGIN \
125 if (panic_is_inited) \
126 simple_lock(&panic_lock); \
127 MACRO_END
128
129 #define PANIC_UNLOCK() \
130 MACRO_BEGIN \
131 if (panic_is_inited) \
132 simple_unlock(&panic_lock); \
133 MACRO_END
134
135
136 void
137 panic_init(void)
138 {
139 simple_lock_init(&panic_lock, ETAP_NO_TRACE);
140 panic_is_inited = 1;
141 }
142
143 void
144 panic(const char *str, ...)
145 {
146 va_list listp;
147 spl_t s;
148 thread_t thread;
149
150 s = splhigh();
151 disable_preemption();
152
153 #ifdef __ppc__
154 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
155 #endif
156
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
161 if( logPanicDataToScreen )
162 disableDebugOuput = FALSE;
163
164 debug_mode = TRUE;
165 restart:
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");
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();
191 kdb_printf("panic(cpu %d): ", (unsigned) paniccpu);
192 va_start(listp, str);
193 _doprnt(str, &listp, consdebug_putc, 0);
194 va_end(listp);
195 kdb_printf("\n");
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();
208 thread->wait_queue = save_waits[cpu_number()]; /* Restore the wait queue */
209 if (return_on_panic) {
210 enable_preemption();
211 splx(s);
212 return;
213 }
214 kdb_printf("panic: We are hanging here...\n");
215 while(1);
216 /* NOTREACHED */
217 }
218
219 void
220 log(int level, char *fmt, ...)
221 {
222 va_list listp;
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 }
235
236 void
237 debug_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
247 void
248 debug_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 }