]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/debug.c
xnu-792.18.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 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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>
1c79356b
A
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>
9bccf70c 69#include <vm/vm_kern.h>
91447636 70#include <vm/pmap.h>
1c79356b
A
71#include <stdarg.h>
72
9bccf70c
A
73#ifdef __ppc__
74#include <ppc/Firmware.h>
75#include <ppc/low_trace.h>
76#endif
77
1c79356b
A
78unsigned int halt_in_debugger = 0;
79unsigned int switch_debugger = 0;
80unsigned int current_debugger = 0;
81unsigned int active_debugger = 0;
82unsigned int debug_mode=0;
83unsigned int disableDebugOuput = TRUE;
84unsigned int systemLogDiags = FALSE;
55e303ae 85unsigned int logPanicDataToScreen = FALSE;
91447636 86unsigned int panicDebugging = FALSE;
1c79356b
A
87
88int mach_assert = 1;
89
55e303ae 90const char *panicstr = (char *) 0;
1c79356b
A
91decl_simple_lock_data(,panic_lock)
92int paniccpu;
93volatile int panicwait;
91447636 94volatile unsigned int nestedpanic= 0;
1c79356b
A
95unsigned int panic_is_inited = 0;
96unsigned int return_on_panic = 0;
91447636 97unsigned long panic_caller;
1c79356b 98
9bccf70c 99char *debug_buf;
91447636 100ppnum_t debug_buf_page;
9bccf70c
A
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{
91447636 139 simple_lock_init(&panic_lock, 0);
1c79356b 140 panic_is_inited = 1;
91447636 141 panic_caller = 0;
1c79356b
A
142}
143
144void
145panic(const char *str, ...)
146{
147 va_list listp;
148 spl_t s;
149 thread_t thread;
91447636 150 wait_queue_t wq;
1c79356b
A
151
152 s = splhigh();
55e303ae 153 disable_preemption();
1c79356b 154
9bccf70c
A
155#ifdef __ppc__
156 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
157#endif
158
1c79356b 159 thread = current_thread(); /* Get failing thread */
91447636 160 wq = thread->wait_queue; /* Save the old value */
1c79356b
A
161 thread->wait_queue = 0; /* Clear the wait so we do not get double panics when we try locks */
162
9bccf70c
A
163 if( logPanicDataToScreen )
164 disableDebugOuput = FALSE;
165
1c79356b 166 debug_mode = TRUE;
91447636
A
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
1c79356b
A
172restart:
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");
1c79356b
A
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();
91447636 198 kdb_printf("panic(cpu %d caller 0x%08X): ", (unsigned) paniccpu, panic_caller);
1c79356b 199 va_start(listp, str);
9bccf70c 200 _doprnt(str, &listp, consdebug_putc, 0);
1c79356b 201 va_end(listp);
9bccf70c 202 kdb_printf("\n");
1c79356b
A
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();
91447636 215 thread->wait_queue = wq; /* Restore the wait queue */
55e303ae
A
216 if (return_on_panic) {
217 enable_preemption();
218 splx(s);
1c79356b 219 return;
55e303ae 220 }
9bccf70c 221 kdb_printf("panic: We are hanging here...\n");
1c79356b
A
222 while(1);
223 /* NOTREACHED */
224}
225
226void
227log(int level, char *fmt, ...)
228{
229 va_list listp;
1c79356b
A
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}
9bccf70c
A
242
243void
244debug_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;
91447636 252 debug_buf_page = pmap_find_phys(kernel_pmap, (addr64_t)(uintptr_t)debug_buf_ptr);
9bccf70c
A
253}
254
255void
256debug_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}