]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/debug.c
b6d146746e1f6985524c906d8dca9edb0e3c892a
[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 defined(__i386__) || defined(__x86_64__)
239 /* Attempt to display the unparsed panic string */
240 const char *tstr = str;
241
242 kprintf("Panic initiated, string: ");
243 while (tstr && *tstr)
244 kprintf("%c", *tstr++);
245 kprintf("\n");
246 #endif
247 if (kdebug_enable)
248 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
249
250 s = splhigh();
251 disable_preemption();
252
253 panic_safe();
254
255 thread = current_thread(); /* Get failing thread */
256 wq = thread->wait_queue; /* Save the old value */
257 thread->wait_queue = NULL; /* Clear the wait so we do not get double panics when we try locks */
258
259 if( logPanicDataToScreen )
260 disable_debug_output = FALSE;
261
262 debug_mode = TRUE;
263
264 /* panic_caller is initialized to 0. If set, don't change it */
265 if ( ! panic_caller )
266 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
267
268 restart:
269 PANIC_LOCK();
270 if (panicstr) {
271 if (cpu_number() != paniccpu) {
272 PANIC_UNLOCK();
273 /*
274 * Wait until message has been printed to identify correct
275 * cpu that made the first panic.
276 */
277 while (panicwait)
278 continue;
279 goto restart;
280 } else {
281 nestedpanic +=1;
282 PANIC_UNLOCK();
283 Debugger("double panic");
284 printf("double panic: We are hanging here...\n");
285 panic_stop();
286 /* NOTREACHED */
287 }
288 }
289 panicstr = str;
290 paniccpu = cpu_number();
291 panicwait = 1;
292
293 PANIC_UNLOCK();
294 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
295 if (str) {
296 va_start(listp, str);
297 _doprnt(str, &listp, consdebug_putc, 0);
298 va_end(listp);
299 }
300 kdb_printf("\n");
301
302 /*
303 * Release panicwait indicator so that other cpus may call Debugger().
304 */
305 panicwait = 0;
306 Debugger("panic");
307 /*
308 * Release panicstr so that we can handle normally other panics.
309 */
310 PANIC_LOCK();
311 panicstr = (char *)0;
312 PANIC_UNLOCK();
313 thread->wait_queue = wq; /* Restore the wait queue */
314
315 if (return_on_panic) {
316 panic_normal();
317 enable_preemption();
318 splx(s);
319 return;
320 }
321
322 kdb_printf("panic: We are hanging here...\n");
323 panic_stop();
324 /* NOTREACHED */
325 }
326
327 void
328 log(__unused int level, char *fmt, ...)
329 {
330 va_list listp;
331
332 #ifdef lint
333 level++;
334 #endif /* lint */
335 #ifdef MACH_BSD
336 disable_preemption();
337 va_start(listp, fmt);
338 _doprnt(fmt, &listp, conslog_putc, 0);
339 va_end(listp);
340 enable_preemption();
341 #endif
342 }
343
344 void
345 debug_putc(char c)
346 {
347 if ((debug_buf_size != 0) &&
348 ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
349 *debug_buf_ptr=c;
350 debug_buf_ptr++;
351 }
352 }
353
354 /* In-place packing routines -- inefficient, but they're called at most once.
355 * Assumes "buflen" is a multiple of 8.
356 */
357
358 int packA(char *inbuf, uint32_t length, uint32_t buflen)
359 {
360 unsigned int i, j = 0;
361 pasc_t pack;
362
363 length = MIN(((length + 7) & ~7), buflen);
364
365 for (i = 0; i < length; i+=8)
366 {
367 pack.a = inbuf[i];
368 pack.b = inbuf[i+1];
369 pack.c = inbuf[i+2];
370 pack.d = inbuf[i+3];
371 pack.e = inbuf[i+4];
372 pack.f = inbuf[i+5];
373 pack.g = inbuf[i+6];
374 pack.h = inbuf[i+7];
375 bcopy ((char *) &pack, inbuf + j, 7);
376 j += 7;
377 }
378 return j;
379 }
380
381 void unpackA(char *inbuf, uint32_t length)
382 {
383 pasc_t packs;
384 unsigned i = 0;
385 length = (length * 8)/7;
386
387 while (i < length) {
388 packs = *(pasc_t *)&inbuf[i];
389 bcopy(&inbuf[i+7], &inbuf[i+8], MAX(0, (int) (length - i - 8)));
390 inbuf[i++] = packs.a;
391 inbuf[i++] = packs.b;
392 inbuf[i++] = packs.c;
393 inbuf[i++] = packs.d;
394 inbuf[i++] = packs.e;
395 inbuf[i++] = packs.f;
396 inbuf[i++] = packs.g;
397 inbuf[i++] = packs.h;
398 }
399 }
400
401 extern void *proc_name_address(void *p);
402
403 static void
404 panic_display_process_name(void) {
405 char proc_name[32] = "Unknown";
406 task_t ctask = 0;
407 void *cbsd_info = 0;
408
409 if (ml_nofault_copy((vm_offset_t)&current_thread()->task, (vm_offset_t) &ctask, sizeof(task_t)) == sizeof(task_t))
410 if(ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(&ctask->bsd_info)) == sizeof(&ctask->bsd_info))
411 if (cbsd_info && (ml_nofault_copy((vm_offset_t) proc_name_address(cbsd_info), (vm_offset_t) &proc_name, sizeof(proc_name)) > 0))
412 proc_name[sizeof(proc_name) - 1] = '\0';
413 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name);
414 }
415
416 unsigned panic_active(void) {
417 return ((panicstr != (char *) 0));
418 }
419
420 void populate_model_name(char *model_string) {
421 strlcpy(model_name, model_string, sizeof(model_name));
422 }
423
424 static void panic_display_model_name(void) {
425 char tmp_model_name[sizeof(model_name)];
426
427 if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name))
428 return;
429
430 tmp_model_name[sizeof(tmp_model_name) - 1] = '\0';
431
432 if (tmp_model_name[0] != 0)
433 kdb_printf("System model name: %s\n", tmp_model_name);
434 }
435
436 static void panic_display_kernel_uuid(void) {
437 char tmp_kernel_uuid[sizeof(kernel_uuid)];
438
439 if (ml_nofault_copy((vm_offset_t) &kernel_uuid, (vm_offset_t) &tmp_kernel_uuid, sizeof(kernel_uuid)) != sizeof(kernel_uuid))
440 return;
441
442 if (tmp_kernel_uuid[0] != '\0')
443 kdb_printf("Kernel UUID: %s\n", tmp_kernel_uuid);
444 }
445
446 static void panic_display_uptime(void) {
447 uint64_t uptime;
448 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime);
449
450 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime);
451 }
452
453 extern const char version[];
454 extern char osversion[];
455
456 static volatile uint32_t config_displayed = 0;
457
458 __private_extern__ void panic_display_system_configuration(void) {
459
460 panic_display_process_name();
461 if (OSCompareAndSwap(0, 1, &config_displayed)) {
462 char buf[256];
463 if (strlcpy(buf, PE_boot_args(), sizeof(buf)))
464 kdb_printf("Boot args: %s\n", buf);
465 kdb_printf("\nMac OS version:\n%s\n",
466 (osversion[0] != 0) ? osversion : "Not yet set");
467 kdb_printf("\nKernel version:\n%s\n",version);
468 panic_display_kernel_uuid();
469 panic_display_pal_info();
470 panic_display_model_name();
471 panic_display_uptime();
472 panic_display_zprint();
473 #if CONFIG_ZLEAKS
474 panic_display_ztrace();
475 #endif /* CONFIG_ZLEAKS */
476 kext_dump_panic_lists(&kdb_log);
477 }
478 }
479
480 extern zone_t first_zone;
481 extern unsigned int num_zones, stack_total;
482 extern unsigned long long stack_allocs;
483
484 #if defined(__i386__) || defined (__x86_64__)
485 extern unsigned int inuse_ptepages_count;
486 extern long long alloc_ptepages_count;
487 #endif
488
489 extern boolean_t panic_include_zprint;
490
491 __private_extern__ void panic_display_zprint()
492 {
493 if(panic_include_zprint == TRUE) {
494
495 unsigned int i;
496 struct zone zone_copy;
497
498 if(first_zone!=NULL) {
499 if(ml_nofault_copy((vm_offset_t)first_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) {
500 for (i = 0; i < num_zones; i++) {
501 if(zone_copy.cur_size > (1024*1024)) {
502 kdb_printf("%.20s:%lu\n",zone_copy.zone_name,(uintptr_t)zone_copy.cur_size);
503 }
504
505 if(zone_copy.next_zone == NULL) {
506 break;
507 }
508
509 if(ml_nofault_copy((vm_offset_t)zone_copy.next_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) != sizeof(struct zone)) {
510 break;
511 }
512 }
513 }
514 }
515
516 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size * stack_total));
517
518 #if defined(__i386__) || defined (__x86_64__)
519 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
520 #endif
521
522 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total);
523 }
524 }
525
526 #if CONFIG_ZLEAKS
527 extern boolean_t panic_include_ztrace;
528 extern struct ztrace* top_ztrace;
529 /*
530 * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
531 * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
532 */
533 __private_extern__ void panic_display_ztrace(void)
534 {
535 if(panic_include_ztrace == TRUE) {
536 unsigned int i = 0;
537 struct ztrace top_ztrace_copy;
538
539 /* Make sure not to trip another panic if there's something wrong with memory */
540 if(ml_nofault_copy((vm_offset_t)top_ztrace, (vm_offset_t)&top_ztrace_copy, sizeof(struct ztrace)) == sizeof(struct ztrace)) {
541 kdb_printf("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy.zt_size);
542 /* Print the backtrace addresses */
543 for (i = 0; (i < top_ztrace_copy.zt_depth && i < MAX_ZTRACE_DEPTH) ; i++) {
544 kdb_printf("%p\n", top_ztrace_copy.zt_stack[i]);
545 }
546 /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
547 kmod_panic_dump((vm_offset_t *)&top_ztrace_copy.zt_stack[0], top_ztrace_copy.zt_depth);
548 }
549 else {
550 kdb_printf("\nCan't access top_ztrace...\n");
551 }
552 kdb_printf("\n");
553 }
554 }
555 #endif /* CONFIG_ZLEAKS */
556
557 #if !MACH_KDP
558 static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
559
560 /* XXX ugly forward declares to stop warnings */
561 void *kdp_get_interface(void);
562 void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
563 void kdp_set_gateway_mac(void *);
564 void kdp_set_interface(void *);
565 void kdp_register_send_receive(void *, void *);
566 void kdp_unregister_send_receive(void *, void *);
567 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
568 int kdp_stack_snapshot_geterror(void);
569 int kdp_stack_snapshot_bytes_traced(void);
570
571 void *
572 kdp_get_interface( void)
573 {
574 return(void *)0;
575 }
576
577 unsigned int
578 kdp_get_ip_address(void )
579 { return 0; }
580
581 struct ether_addr
582 kdp_get_mac_addr(void)
583 {
584 return kdp_current_mac_address;
585 }
586
587 void
588 kdp_set_ip_and_mac_addresses(
589 __unused struct in_addr *ipaddr,
590 __unused struct ether_addr *macaddr)
591 {}
592
593 void
594 kdp_set_gateway_mac(__unused void *gatewaymac)
595 {}
596
597 void
598 kdp_set_interface(__unused void *ifp)
599 {}
600
601 void
602 kdp_register_send_receive(__unused void *send, __unused void *receive)
603 {}
604
605 void
606 kdp_unregister_send_receive(__unused void *send, __unused void *receive)
607 {}
608
609 void
610 kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf,
611 __unused uint32_t tracebuf_size, __unused uint32_t options)
612 {}
613
614 int
615 kdp_stack_snapshot_geterror(void)
616 {
617 return -1;
618 }
619
620 int
621 kdp_stack_snapshot_bytes_traced(void)
622 {
623 return 0;
624 }
625
626 #endif