]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/debug.c
599d1670ea0bb220d2dafb7b3d5ff8db69b1026d
[apple/xnu.git] / osfmk / kern / debug.c
1 /*
2 * Copyright (c) 2000-2007 Apple 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 <kern/clock.h>
70 #include <vm/vm_kern.h>
71 #include <vm/pmap.h>
72 #include <stdarg.h>
73 #if !MACH_KDP
74 #include <kdp/kdp_udp.h>
75 #endif
76
77 #ifdef __ppc__
78 #include <ppc/Firmware.h>
79 #include <ppc/low_trace.h>
80 #endif
81
82 #ifdef __i386__
83 #include <i386/cpu_threads.h>
84 #include <i386/pmCPU.h>
85 #endif
86
87 #include <IOKit/IOPlatformExpert.h>
88
89 unsigned int halt_in_debugger = 0;
90 unsigned int switch_debugger = 0;
91 unsigned int current_debugger = 0;
92 unsigned int active_debugger = 0;
93 unsigned int debug_mode=0;
94 unsigned int disable_debug_output = TRUE;
95 unsigned int systemLogDiags = FALSE;
96 unsigned int panicDebugging = FALSE;
97 unsigned int logPanicDataToScreen = FALSE;
98
99 int mach_assert = 1;
100
101 const char *panicstr = (char *) 0;
102 decl_simple_lock_data(,panic_lock)
103 int paniccpu;
104 volatile int panicwait;
105 volatile unsigned int nestedpanic= 0;
106 unsigned int panic_is_inited = 0;
107 unsigned int return_on_panic = 0;
108 unsigned long panic_caller;
109
110 #if CONFIG_EMBEDDED
111 #define DEBUG_BUF_SIZE (PAGE_SIZE)
112 #else
113 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
114 #endif
115
116 char debug_buf[DEBUG_BUF_SIZE];
117 char *debug_buf_ptr = debug_buf;
118 unsigned int debug_buf_size = sizeof(debug_buf);
119
120 static char model_name[64];
121
122 struct pasc {
123 unsigned a: 7;
124 unsigned b: 7;
125 unsigned c: 7;
126 unsigned d: 7;
127 unsigned e: 7;
128 unsigned f: 7;
129 unsigned g: 7;
130 unsigned h: 7;
131 } __attribute__((packed));
132
133 typedef struct pasc pasc_t;
134
135 void
136 Assert(
137 const char *file,
138 int line,
139 #if CONFIG_NO_PANIC_STRINGS
140 __unused const char *expression
141 #else
142 const char *expression
143 #endif
144 )
145 {
146 int saved_return_on_panic;
147
148 if (!mach_assert) {
149 return;
150 }
151
152 saved_return_on_panic = return_on_panic;
153 return_on_panic = 1;
154
155 panic_plain("%s:%d Assertion failed: %s", file, line, expression);
156
157 return_on_panic = saved_return_on_panic;
158 }
159
160 /*
161 * Carefully use the panic_lock. There's always a chance that
162 * somehow we'll call panic before getting to initialize the
163 * panic_lock -- in this case, we'll assume that the world is
164 * in uniprocessor mode and just avoid using the panic lock.
165 */
166 #define PANIC_LOCK() \
167 MACRO_BEGIN \
168 if (panic_is_inited) \
169 simple_lock(&panic_lock); \
170 MACRO_END
171
172 #define PANIC_UNLOCK() \
173 MACRO_BEGIN \
174 if (panic_is_inited) \
175 simple_unlock(&panic_lock); \
176 MACRO_END
177
178
179 void
180 panic_init(void)
181 {
182 simple_lock_init(&panic_lock, 0);
183 panic_is_inited = 1;
184 panic_caller = 0;
185 }
186
187 void
188 debug_log_init(void)
189 {
190 if (debug_buf_size != 0)
191 return;
192 debug_buf_ptr = debug_buf;
193 debug_buf_size = sizeof(debug_buf);
194 }
195
196 #if __i386__
197 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
198 #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
199 #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
200 #else
201 #define panic_stop() { while (1) ; }
202 #define panic_safe()
203 #define panic_normal()
204 #endif
205
206 #undef panic(...)
207 void
208 panic(const char *str, ...)
209 {
210 va_list listp;
211 spl_t s;
212 thread_t thread;
213 wait_queue_t wq;
214
215 s = splhigh();
216 disable_preemption();
217
218 panic_safe();
219
220 #ifdef __ppc__
221 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
222 #endif
223
224 thread = current_thread(); /* Get failing thread */
225 wq = thread->wait_queue; /* Save the old value */
226 thread->wait_queue = NULL; /* Clear the wait so we do not get double panics when we try locks */
227
228 if( logPanicDataToScreen )
229 disable_debug_output = FALSE;
230
231 debug_mode = TRUE;
232
233 /* panic_caller is initialized to 0. If set, don't change it */
234 if ( ! panic_caller )
235 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
236
237 restart:
238 PANIC_LOCK();
239 if (panicstr) {
240 if (cpu_number() != paniccpu) {
241 PANIC_UNLOCK();
242 /*
243 * Wait until message has been printed to identify correct
244 * cpu that made the first panic.
245 */
246 while (panicwait)
247 continue;
248 goto restart;
249 } else {
250 nestedpanic +=1;
251 PANIC_UNLOCK();
252 Debugger("double panic");
253 printf("double panic: We are hanging here...\n");
254 panic_stop();
255 /* NOTREACHED */
256 }
257 }
258 panicstr = str;
259 paniccpu = cpu_number();
260 panicwait = 1;
261
262 PANIC_UNLOCK();
263 kdb_printf("panic(cpu %d caller 0x%08lX): ", (unsigned) paniccpu, panic_caller);
264 if (str) {
265 va_start(listp, str);
266 _doprnt(str, &listp, consdebug_putc, 0);
267 va_end(listp);
268 }
269 kdb_printf("\n");
270
271 /*
272 * Release panicwait indicator so that other cpus may call Debugger().
273 */
274 panicwait = 0;
275 Debugger("panic");
276 /*
277 * Release panicstr so that we can handle normally other panics.
278 */
279 PANIC_LOCK();
280 panicstr = (char *)0;
281 PANIC_UNLOCK();
282 thread->wait_queue = wq; /* Restore the wait queue */
283
284 if (return_on_panic) {
285 panic_normal();
286 enable_preemption();
287 splx(s);
288 return;
289 }
290
291 kdb_printf("panic: We are hanging here...\n");
292 panic_stop();
293 /* NOTREACHED */
294 }
295
296 void
297 log(__unused int level, char *fmt, ...)
298 {
299 va_list listp;
300
301 #ifdef lint
302 level++;
303 #endif /* lint */
304 #ifdef MACH_BSD
305 disable_preemption();
306 va_start(listp, fmt);
307 _doprnt(fmt, &listp, conslog_putc, 0);
308 va_end(listp);
309 enable_preemption();
310 #endif
311 }
312
313 void
314 debug_putc(char c)
315 {
316 if ((debug_buf_size != 0) &&
317 ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
318 *debug_buf_ptr=c;
319 debug_buf_ptr++;
320 }
321 }
322
323 /* In-place packing routines -- inefficient, but they're called at most once.
324 */
325
326 int packA(char *inbuf, uint32_t length, uint32_t buflen)
327 {
328 unsigned int i, j = 0;
329 pasc_t pack;
330
331 length = MIN(((length & ~7) +8), buflen);
332
333 for (i = 0; i < length; i+=8)
334 {
335 pack.a = inbuf[i];
336 pack.b = inbuf[i+1];
337 pack.c = inbuf[i+2];
338 pack.d = inbuf[i+3];
339 pack.e = inbuf[i+4];
340 pack.f = inbuf[i+5];
341 pack.g = inbuf[i+6];
342 pack.h = inbuf[i+7];
343 bcopy ((char *) &pack, inbuf + j, 7);
344 j += 7;
345 }
346 return ((length * 7)/8);
347 }
348
349 void unpackA(char *inbuf, uint32_t length)
350 {
351 pasc_t packs;
352 unsigned i = 0;
353 length = (length * 8)/7;
354
355 while (i < length) {
356 packs = *(pasc_t *)&inbuf[i];
357 bcopy(&inbuf[i+7], &inbuf[i+8], MAX(0, (int) (length - i - 8)));
358 inbuf[i++] = packs.a;
359 inbuf[i++] = packs.b;
360 inbuf[i++] = packs.c;
361 inbuf[i++] = packs.d;
362 inbuf[i++] = packs.e;
363 inbuf[i++] = packs.f;
364 inbuf[i++] = packs.g;
365 inbuf[i++] = packs.h;
366 }
367 }
368
369 extern void *proc_name_address(void *p);
370
371 static void
372 panic_display_process_name(void) {
373 char proc_name[32] = "Unknown";
374 task_t ctask = 0;
375 void *cbsd_info = 0;
376
377 if (ml_nofault_copy((vm_offset_t)&current_thread()->task, (vm_offset_t) &ctask, sizeof(task_t)) == sizeof(task_t))
378 if(ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(&ctask->bsd_info)) == sizeof(&ctask->bsd_info))
379 if (cbsd_info && (ml_nofault_copy((vm_offset_t) proc_name_address(cbsd_info), (vm_offset_t) &proc_name, sizeof(proc_name)) > 0))
380 proc_name[sizeof(proc_name) - 1] = '\0';
381 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name);
382 }
383
384 unsigned panic_active(void) {
385 return ((panicstr != (char *) 0));
386 }
387
388 void populate_model_name(char *model_string) {
389 strlcpy(model_name, model_string, sizeof(model_name));
390 }
391
392 static void panic_display_model_name(void) {
393 char tmp_model_name[sizeof(model_name)];
394
395 if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name))
396 return;
397
398 model_name[sizeof(model_name) - 1] = '\0';
399
400 if (model_name[0] != 0)
401 kdb_printf("System model name: %s\n", model_name);
402 }
403
404 static void panic_display_uptime(void) {
405 uint64_t uptime;
406 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime);
407
408 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime);
409 }
410
411 extern const char version[];
412 extern char osversion[];
413
414 __private_extern__ void panic_display_system_configuration(void) {
415 static boolean_t config_displayed = FALSE;
416
417 panic_display_process_name();
418 if (config_displayed == FALSE) {
419 kdb_printf("\nMac OS version:\n%s\n",
420 (osversion[0] != 0) ? osversion : "Not yet set");
421 kdb_printf("\nKernel version:\n%s\n",version);
422 panic_display_model_name();
423 panic_display_uptime();
424 config_displayed = TRUE;
425 }
426 }
427
428 extern zone_t first_zone;
429 extern unsigned int num_zones, stack_total;
430
431 #if defined(__i386__)
432 extern unsigned int inuse_ptepages_count;
433 #endif
434
435 extern boolean_t panic_include_zprint;
436 extern vm_size_t kalloc_large_total;
437
438 __private_extern__ void panic_display_zprint()
439 {
440 if(panic_include_zprint == TRUE) {
441
442 unsigned int i;
443 struct zone zone_copy;
444
445 if(first_zone!=NULL) {
446 if(ml_nofault_copy((vm_offset_t)first_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) {
447 for (i = 0; i < num_zones; i++) {
448 if(zone_copy.cur_size > (1024*1024)) {
449 kdb_printf("%.20s:%lu\n",zone_copy.zone_name,(uintptr_t)zone_copy.cur_size);
450 }
451
452 if(zone_copy.next_zone == NULL) {
453 break;
454 }
455
456 if(ml_nofault_copy((vm_offset_t)zone_copy.next_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) != sizeof(struct zone)) {
457 break;
458 }
459 }
460 }
461 }
462
463 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(KERNEL_STACK_SIZE * stack_total));
464 #if defined(__i386__)
465 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
466 #endif
467 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total);
468 }
469 }
470
471 #if !MACH_KDP
472 static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
473 unsigned int not_in_kdp = 1;
474
475 /* XXX ugly forward declares to stop warnings */
476 void *kdp_get_interface(void);
477 void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
478 void kdp_set_gateway_mac(void *);
479 void kdp_set_interface(void *);
480 void kdp_register_send_receive(void *, void *);
481 void kdp_unregister_send_receive(void *, void *);
482 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
483 int kdp_stack_snapshot_geterror(void);
484 int kdp_stack_snapshot_bytes_traced(void);
485
486 void *
487 kdp_get_interface( void)
488 {
489 return(void *)0;
490 }
491
492 unsigned int
493 kdp_get_ip_address(void )
494 { return 0; }
495
496 struct ether_addr
497 kdp_get_mac_addr(void)
498 {
499 return kdp_current_mac_address;
500 }
501
502 void
503 kdp_set_ip_and_mac_addresses(
504 __unused struct in_addr *ipaddr,
505 __unused struct ether_addr *macaddr)
506 {}
507
508 void
509 kdp_set_gateway_mac(__unused void *gatewaymac)
510 {}
511
512 void
513 kdp_set_interface(__unused void *ifp)
514 {}
515
516 void
517 kdp_register_send_receive(__unused void *send, __unused void *receive)
518 {}
519
520 void
521 kdp_unregister_send_receive(__unused void *send, __unused void *receive)
522 {}
523
524 void
525 kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf,
526 __unused uint32_t tracebuf_size, __unused uint32_t options)
527 {}
528
529 int
530 kdp_stack_snapshot_geterror(void)
531 {
532 return -1;
533 }
534
535 int
536 kdp_stack_snapshot_bytes_traced(void)
537 {
538 return 0;
539 }
540
541 #endif