]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/debug.c
xnu-124.13.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 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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>
64#include <stdarg.h>
65
66unsigned int halt_in_debugger = 0;
67unsigned int switch_debugger = 0;
68unsigned int current_debugger = 0;
69unsigned int active_debugger = 0;
70unsigned int debug_mode=0;
71unsigned int disableDebugOuput = TRUE;
72unsigned int systemLogDiags = FALSE;
73
74int mach_assert = 1;
75
76const char *panicstr;
77decl_simple_lock_data(,panic_lock)
78int paniccpu;
79volatile int panicwait;
80volatile int nestedpanic= 0;
81unsigned int panic_is_inited = 0;
82unsigned int return_on_panic = 0;
83wait_queue_t save_waits[NCPUS];
84
85void
86Assert(
87 const char *file,
88 int line,
89 const char *expression)
90{
91 if (!mach_assert) {
92 return;
93 }
94 panic("{%d} Assertion failed: file \"%s\", line %d: %s\n",
95 cpu_number(), file, line, expression);
96}
97
98/*
99 * Carefully use the panic_lock. There's always a chance that
100 * somehow we'll call panic before getting to initialize the
101 * panic_lock -- in this case, we'll assume that the world is
102 * in uniprocessor mode and just avoid using the panic lock.
103 */
104#define PANIC_LOCK() \
105MACRO_BEGIN \
106 if (panic_is_inited) \
107 simple_lock(&panic_lock); \
108MACRO_END
109
110#define PANIC_UNLOCK() \
111MACRO_BEGIN \
112 if (panic_is_inited) \
113 simple_unlock(&panic_lock); \
114MACRO_END
115
116
117void
118panic_init(void)
119{
120 simple_lock_init(&panic_lock, ETAP_NO_TRACE);
121 panic_is_inited = 1;
122}
123
124void
125panic(const char *str, ...)
126{
127 va_list listp;
128 spl_t s;
129 thread_t thread;
130
131 s = splhigh();
132
133 thread = current_thread(); /* Get failing thread */
134 save_waits[cpu_number()] = thread->wait_queue; /* Save the old value */
135 thread->wait_queue = 0; /* Clear the wait so we do not get double panics when we try locks */
136
137 mp_disable_preemption();
138 disableDebugOuput = FALSE;
139 debug_mode = TRUE;
140restart:
141 PANIC_LOCK();
142 if (panicstr) {
143 if (cpu_number() != paniccpu) {
144 PANIC_UNLOCK();
145 /*
146 * Wait until message has been printed to identify correct
147 * cpu that made the first panic.
148 */
149 while (panicwait)
150 continue;
151 goto restart;
152 } else {
153 nestedpanic +=1;
154 PANIC_UNLOCK();
155 Debugger("double panic");
156 mp_enable_preemption();
157 splx(s);
158 printf("double panic: We are hanging here...\n");
159 while(1);
160 /* NOTREACHED */
161 }
162 }
163 panicstr = str;
164 paniccpu = cpu_number();
165 panicwait = 1;
166
167 PANIC_UNLOCK();
168 printf("panic(cpu %d): ", (unsigned) paniccpu);
169 va_start(listp, str);
170 _doprnt(str, &listp, cnputc, 0);
171 va_end(listp);
172 printf("\n");
173
174 /*
175 * Release panicwait indicator so that other cpus may call Debugger().
176 */
177 panicwait = 0;
178 Debugger("panic");
179 /*
180 * Release panicstr so that we can handle normally other panics.
181 */
182 PANIC_LOCK();
183 panicstr = (char *)0;
184 PANIC_UNLOCK();
185 mp_enable_preemption();
186 splx(s);
187 thread->wait_queue = save_waits[cpu_number()]; /* Restore the wait queue */
188 if (return_on_panic)
189 return;
190 printf("panic: We are hanging here...\n");
191 while(1);
192 /* NOTREACHED */
193}
194
195void
196log(int level, char *fmt, ...)
197{
198 va_list listp;
199 extern void conslog_putc(char);
200
201#ifdef lint
202 level++;
203#endif /* lint */
204#ifdef MACH_BSD
205 disable_preemption();
206 va_start(listp, fmt);
207 _doprnt(fmt, &listp, conslog_putc, 0);
208 va_end(listp);
209 enable_preemption();
210#endif
211}