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