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