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