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