]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/debug.c
1dd1aee281c1fc2a990b8688d5b51c55ac52f818
[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/kalloc.h>
64 #include <kern/lock.h>
65 #include <kern/spl.h>
66 #include <kern/thread.h>
67 #include <kern/assert.h>
68 #include <kern/sched_prim.h>
69 #include <kern/misc_protos.h>
70 #include <kern/clock.h>
71 #include <vm/vm_kern.h>
72 #include <vm/pmap.h>
73 #include <stdarg.h>
74 #if !MACH_KDP
75 #include <kdp/kdp_udp.h>
76 #endif
77
78 #if defined(__i386__) || defined(__x86_64__)
79 #include <i386/cpu_threads.h>
80 #include <i386/pmCPU.h>
81 #endif
82
83 #include <IOKit/IOPlatformExpert.h>
84 #include <machine/pal_routines.h>
85
86 #include <sys/kdebug.h>
87 #include <libkern/OSKextLibPrivate.h>
88 #include <libkern/OSAtomic.h>
89 #include <libkern/kernel_mach_header.h>
90 #include <uuid/uuid.h>
91
92 unsigned int halt_in_debugger = 0;
93 unsigned int switch_debugger = 0;
94 unsigned int current_debugger = 0;
95 unsigned int active_debugger = 0;
96 unsigned int debug_mode=0;
97 unsigned int disable_debug_output = TRUE;
98 unsigned int systemLogDiags = FALSE;
99 unsigned int panicDebugging = FALSE;
100 unsigned int logPanicDataToScreen = FALSE;
101
102 int mach_assert = 1;
103
104 const char *panicstr = (char *) 0;
105 decl_simple_lock_data(,panic_lock)
106 int paniccpu;
107 volatile int panicwait;
108 volatile unsigned int nestedpanic= 0;
109 unsigned int panic_is_inited = 0;
110 unsigned int return_on_panic = 0;
111 unsigned long panic_caller;
112
113 #if CONFIG_EMBEDDED
114 #define DEBUG_BUF_SIZE (PAGE_SIZE)
115 #else
116 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
117 #endif
118
119 char debug_buf[DEBUG_BUF_SIZE];
120 char *debug_buf_ptr = debug_buf;
121 unsigned int debug_buf_size = sizeof(debug_buf);
122
123 static char model_name[64];
124 /* uuid_string_t */ char kernel_uuid[37];
125
126 struct pasc {
127 unsigned a: 7;
128 unsigned b: 7;
129 unsigned c: 7;
130 unsigned d: 7;
131 unsigned e: 7;
132 unsigned f: 7;
133 unsigned g: 7;
134 unsigned h: 7;
135 } __attribute__((packed));
136
137 typedef struct pasc pasc_t;
138
139 /* Prevent CPP from breaking the definition below */
140 #if CONFIG_NO_PANIC_STRINGS
141 #undef Assert
142 #endif
143
144 void
145 Assert(
146 const char *file,
147 int line,
148 const char *expression
149 )
150 {
151 int saved_return_on_panic;
152
153 if (!mach_assert) {
154 return;
155 }
156
157 saved_return_on_panic = return_on_panic;
158 return_on_panic = 1;
159
160 panic_plain("%s:%d Assertion failed: %s", file, line, expression);
161
162 return_on_panic = saved_return_on_panic;
163 }
164
165 /*
166 * Carefully use the panic_lock. There's always a chance that
167 * somehow we'll call panic before getting to initialize the
168 * panic_lock -- in this case, we'll assume that the world is
169 * in uniprocessor mode and just avoid using the panic lock.
170 */
171 #define PANIC_LOCK() \
172 MACRO_BEGIN \
173 if (panic_is_inited) \
174 simple_lock(&panic_lock); \
175 MACRO_END
176
177 #define PANIC_UNLOCK() \
178 MACRO_BEGIN \
179 if (panic_is_inited) \
180 simple_unlock(&panic_lock); \
181 MACRO_END
182
183
184 void
185 panic_init(void)
186 {
187 unsigned long uuidlen = 0;
188 void *uuid;
189
190 uuid = getuuidfromheader(&_mh_execute_header, &uuidlen);
191 if ((uuid != NULL) && (uuidlen == sizeof(uuid_t))) {
192 uuid_unparse_upper(*(uuid_t *)uuid, kernel_uuid);
193 }
194
195 simple_lock_init(&panic_lock, 0);
196 panic_is_inited = 1;
197 panic_caller = 0;
198 }
199
200 void
201 debug_log_init(void)
202 {
203 if (debug_buf_size != 0)
204 return;
205 debug_buf_ptr = debug_buf;
206 debug_buf_size = sizeof(debug_buf);
207 }
208
209 #if defined(__i386__) || defined(__x86_64__)
210 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
211 #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
212 #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
213 #else
214 #define panic_stop() { while (1) ; }
215 #define panic_safe()
216 #define panic_normal()
217 #endif
218
219 /*
220 * Prevent CPP from breaking the definition below,
221 * since all clients get a #define to prepend line numbers
222 */
223 #undef panic
224
225 void _consume_panic_args(int a __unused, ...)
226 {
227 panic("panic");
228 }
229
230 void
231 panic(const char *str, ...)
232 {
233 va_list listp;
234 spl_t s;
235 thread_t thread;
236 wait_queue_t wq;
237
238 if (kdebug_enable) {
239 ml_set_interrupts_enabled(TRUE);
240 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
241 }
242
243 s = splhigh();
244 disable_preemption();
245
246 #if defined(__i386__) || defined(__x86_64__)
247 /* Attempt to display the unparsed panic string */
248 const char *tstr = str;
249
250 kprintf("Panic initiated, string: ");
251 while (tstr && *tstr)
252 kprintf("%c", *tstr++);
253 kprintf("\n");
254 #endif
255
256 panic_safe();
257
258 thread = current_thread(); /* Get failing thread */
259 wq = thread->wait_queue; /* Save the old value */
260 thread->wait_queue = NULL; /* Clear the wait so we do not get double panics when we try locks */
261
262 if( logPanicDataToScreen )
263 disable_debug_output = FALSE;
264
265 debug_mode = TRUE;
266
267 /* panic_caller is initialized to 0. If set, don't change it */
268 if ( ! panic_caller )
269 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
270
271 restart:
272 PANIC_LOCK();
273 if (panicstr) {
274 if (cpu_number() != paniccpu) {
275 PANIC_UNLOCK();
276 /*
277 * Wait until message has been printed to identify correct
278 * cpu that made the first panic.
279 */
280 while (panicwait)
281 continue;
282 goto restart;
283 } else {
284 nestedpanic +=1;
285 PANIC_UNLOCK();
286 Debugger("double panic");
287 printf("double panic: We are hanging here...\n");
288 panic_stop();
289 /* NOTREACHED */
290 }
291 }
292 panicstr = str;
293 paniccpu = cpu_number();
294 panicwait = 1;
295
296 PANIC_UNLOCK();
297 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
298 if (str) {
299 va_start(listp, str);
300 _doprnt(str, &listp, consdebug_putc, 0);
301 va_end(listp);
302 }
303 kdb_printf("\n");
304
305 /*
306 * Release panicwait indicator so that other cpus may call Debugger().
307 */
308 panicwait = 0;
309 Debugger("panic");
310 /*
311 * Release panicstr so that we can handle normally other panics.
312 */
313 PANIC_LOCK();
314 panicstr = (char *)0;
315 PANIC_UNLOCK();
316 thread->wait_queue = wq; /* Restore the wait queue */
317
318 if (return_on_panic) {
319 panic_normal();
320 enable_preemption();
321 splx(s);
322 return;
323 }
324
325 kdb_printf("panic: We are hanging here...\n");
326 panic_stop();
327 /* NOTREACHED */
328 }
329
330 void
331 log(__unused int level, char *fmt, ...)
332 {
333 va_list listp;
334
335 #ifdef lint
336 level++;
337 #endif /* lint */
338 #ifdef MACH_BSD
339 disable_preemption();
340 va_start(listp, fmt);
341 _doprnt(fmt, &listp, conslog_putc, 0);
342 va_end(listp);
343 enable_preemption();
344 #endif
345 }
346
347 void
348 debug_putc(char c)
349 {
350 if ((debug_buf_size != 0) &&
351 ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
352 *debug_buf_ptr=c;
353 debug_buf_ptr++;
354 }
355 }
356
357 /* In-place packing routines -- inefficient, but they're called at most once.
358 * Assumes "buflen" is a multiple of 8.
359 */
360
361 int packA(char *inbuf, uint32_t length, uint32_t buflen)
362 {
363 unsigned int i, j = 0;
364 pasc_t pack;
365
366 length = MIN(((length + 7) & ~7), buflen);
367
368 for (i = 0; i < length; i+=8)
369 {
370 pack.a = inbuf[i];
371 pack.b = inbuf[i+1];
372 pack.c = inbuf[i+2];
373 pack.d = inbuf[i+3];
374 pack.e = inbuf[i+4];
375 pack.f = inbuf[i+5];
376 pack.g = inbuf[i+6];
377 pack.h = inbuf[i+7];
378 bcopy ((char *) &pack, inbuf + j, 7);
379 j += 7;
380 }
381 return j;
382 }
383
384 void unpackA(char *inbuf, uint32_t length)
385 {
386 pasc_t packs;
387 unsigned i = 0;
388 length = (length * 8)/7;
389
390 while (i < length) {
391 packs = *(pasc_t *)&inbuf[i];
392 bcopy(&inbuf[i+7], &inbuf[i+8], MAX(0, (int) (length - i - 8)));
393 inbuf[i++] = packs.a;
394 inbuf[i++] = packs.b;
395 inbuf[i++] = packs.c;
396 inbuf[i++] = packs.d;
397 inbuf[i++] = packs.e;
398 inbuf[i++] = packs.f;
399 inbuf[i++] = packs.g;
400 inbuf[i++] = packs.h;
401 }
402 }
403
404 extern void *proc_name_address(void *p);
405
406 static void
407 panic_display_process_name(void) {
408 char proc_name[32] = "Unknown";
409 task_t ctask = 0;
410 void *cbsd_info = 0;
411
412 if (ml_nofault_copy((vm_offset_t)&current_thread()->task, (vm_offset_t) &ctask, sizeof(task_t)) == sizeof(task_t))
413 if(ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(&ctask->bsd_info)) == sizeof(&ctask->bsd_info))
414 if (cbsd_info && (ml_nofault_copy((vm_offset_t) proc_name_address(cbsd_info), (vm_offset_t) &proc_name, sizeof(proc_name)) > 0))
415 proc_name[sizeof(proc_name) - 1] = '\0';
416 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name);
417 }
418
419 unsigned panic_active(void) {
420 return ((panicstr != (char *) 0));
421 }
422
423 void populate_model_name(char *model_string) {
424 strlcpy(model_name, model_string, sizeof(model_name));
425 }
426
427 static void panic_display_model_name(void) {
428 char tmp_model_name[sizeof(model_name)];
429
430 if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name))
431 return;
432
433 tmp_model_name[sizeof(tmp_model_name) - 1] = '\0';
434
435 if (tmp_model_name[0] != 0)
436 kdb_printf("System model name: %s\n", tmp_model_name);
437 }
438
439 static void panic_display_kernel_uuid(void) {
440 char tmp_kernel_uuid[sizeof(kernel_uuid)];
441
442 if (ml_nofault_copy((vm_offset_t) &kernel_uuid, (vm_offset_t) &tmp_kernel_uuid, sizeof(kernel_uuid)) != sizeof(kernel_uuid))
443 return;
444
445 if (tmp_kernel_uuid[0] != '\0')
446 kdb_printf("Kernel UUID: %s\n", tmp_kernel_uuid);
447 }
448
449 static void panic_display_uptime(void) {
450 uint64_t uptime;
451 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime);
452
453 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime);
454 }
455
456 extern const char version[];
457 extern char osversion[];
458
459 static volatile uint32_t config_displayed = 0;
460
461 __private_extern__ void panic_display_system_configuration(void) {
462
463 panic_display_process_name();
464 if (OSCompareAndSwap(0, 1, &config_displayed)) {
465 char buf[256];
466 if (strlcpy(buf, PE_boot_args(), sizeof(buf)))
467 kdb_printf("Boot args: %s\n", buf);
468 kdb_printf("\nMac OS version:\n%s\n",
469 (osversion[0] != 0) ? osversion : "Not yet set");
470 kdb_printf("\nKernel version:\n%s\n",version);
471 panic_display_kernel_uuid();
472 panic_display_pal_info();
473 panic_display_model_name();
474 panic_display_uptime();
475 panic_display_zprint();
476 #if CONFIG_ZLEAKS
477 panic_display_ztrace();
478 #endif /* CONFIG_ZLEAKS */
479 kext_dump_panic_lists(&kdb_log);
480 }
481 }
482
483 extern zone_t first_zone;
484 extern unsigned int num_zones, stack_total;
485 extern unsigned long long stack_allocs;
486
487 #if defined(__i386__) || defined (__x86_64__)
488 extern unsigned int inuse_ptepages_count;
489 extern long long alloc_ptepages_count;
490 #endif
491
492 extern boolean_t panic_include_zprint;
493
494 __private_extern__ void panic_display_zprint()
495 {
496 if(panic_include_zprint == TRUE) {
497
498 unsigned int i;
499 struct zone zone_copy;
500
501 if(first_zone!=NULL) {
502 if(ml_nofault_copy((vm_offset_t)first_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) {
503 for (i = 0; i < num_zones; i++) {
504 if(zone_copy.cur_size > (1024*1024)) {
505 kdb_printf("%.20s:%lu\n",zone_copy.zone_name,(uintptr_t)zone_copy.cur_size);
506 }
507
508 if(zone_copy.next_zone == NULL) {
509 break;
510 }
511
512 if(ml_nofault_copy((vm_offset_t)zone_copy.next_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) != sizeof(struct zone)) {
513 break;
514 }
515 }
516 }
517 }
518
519 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size * stack_total));
520
521 #if defined(__i386__) || defined (__x86_64__)
522 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
523 #endif
524
525 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total);
526 }
527 }
528
529 #if CONFIG_ZLEAKS
530 extern boolean_t panic_include_ztrace;
531 extern struct ztrace* top_ztrace;
532 /*
533 * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
534 * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
535 */
536 __private_extern__ void panic_display_ztrace(void)
537 {
538 if(panic_include_ztrace == TRUE) {
539 unsigned int i = 0;
540 struct ztrace top_ztrace_copy;
541
542 /* Make sure not to trip another panic if there's something wrong with memory */
543 if(ml_nofault_copy((vm_offset_t)top_ztrace, (vm_offset_t)&top_ztrace_copy, sizeof(struct ztrace)) == sizeof(struct ztrace)) {
544 kdb_printf("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy.zt_size);
545 /* Print the backtrace addresses */
546 for (i = 0; (i < top_ztrace_copy.zt_depth && i < MAX_ZTRACE_DEPTH) ; i++) {
547 kdb_printf("%p\n", top_ztrace_copy.zt_stack[i]);
548 }
549 /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
550 kmod_panic_dump((vm_offset_t *)&top_ztrace_copy.zt_stack[0], top_ztrace_copy.zt_depth);
551 }
552 else {
553 kdb_printf("\nCan't access top_ztrace...\n");
554 }
555 kdb_printf("\n");
556 }
557 }
558 #endif /* CONFIG_ZLEAKS */
559
560 #if !MACH_KDP
561 static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
562
563 /* XXX ugly forward declares to stop warnings */
564 void *kdp_get_interface(void);
565 void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
566 void kdp_set_gateway_mac(void *);
567 void kdp_set_interface(void *);
568 void kdp_register_send_receive(void *, void *);
569 void kdp_unregister_send_receive(void *, void *);
570 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
571 int kdp_stack_snapshot_geterror(void);
572 int kdp_stack_snapshot_bytes_traced(void);
573
574 void *
575 kdp_get_interface( void)
576 {
577 return(void *)0;
578 }
579
580 unsigned int
581 kdp_get_ip_address(void )
582 { return 0; }
583
584 struct ether_addr
585 kdp_get_mac_addr(void)
586 {
587 return kdp_current_mac_address;
588 }
589
590 void
591 kdp_set_ip_and_mac_addresses(
592 __unused struct in_addr *ipaddr,
593 __unused struct ether_addr *macaddr)
594 {}
595
596 void
597 kdp_set_gateway_mac(__unused void *gatewaymac)
598 {}
599
600 void
601 kdp_set_interface(__unused void *ifp)
602 {}
603
604 void
605 kdp_register_send_receive(__unused void *send, __unused void *receive)
606 {}
607
608 void
609 kdp_unregister_send_receive(__unused void *send, __unused void *receive)
610 {}
611
612 void
613 kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf,
614 __unused uint32_t tracebuf_size, __unused uint32_t options)
615 {}
616
617 int
618 kdp_stack_snapshot_geterror(void)
619 {
620 return -1;
621 }
622
623 int
624 kdp_stack_snapshot_bytes_traced(void)
625 {
626 return 0;
627 }
628
629 #endif