]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/debug.c
xnu-1699.26.8.tar.gz
[apple/xnu.git] / osfmk / kern / debug.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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>
1c79356b
A
61
62#include <kern/cpu_number.h>
b0d623f7 63#include <kern/kalloc.h>
1c79356b
A
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>
c910b4d9 70#include <kern/clock.h>
9bccf70c 71#include <vm/vm_kern.h>
91447636 72#include <vm/pmap.h>
1c79356b 73#include <stdarg.h>
2d21ac55
A
74#if !MACH_KDP
75#include <kdp/kdp_udp.h>
76#endif
1c79356b 77
b0d623f7 78#if defined(__i386__) || defined(__x86_64__)
2d21ac55
A
79#include <i386/cpu_threads.h>
80#include <i386/pmCPU.h>
81#endif
82
83#include <IOKit/IOPlatformExpert.h>
6d2010ae 84#include <machine/pal_routines.h>
2d21ac55 85
b0d623f7
A
86#include <sys/kdebug.h>
87#include <libkern/OSKextLibPrivate.h>
6d2010ae
A
88#include <libkern/OSAtomic.h>
89#include <libkern/kernel_mach_header.h>
90#include <uuid/uuid.h>
b0d623f7 91
1c79356b
A
92unsigned int halt_in_debugger = 0;
93unsigned int switch_debugger = 0;
94unsigned int current_debugger = 0;
95unsigned int active_debugger = 0;
96unsigned int debug_mode=0;
2d21ac55 97unsigned int disable_debug_output = TRUE;
1c79356b 98unsigned int systemLogDiags = FALSE;
91447636 99unsigned int panicDebugging = FALSE;
2d21ac55 100unsigned int logPanicDataToScreen = FALSE;
1c79356b
A
101
102int mach_assert = 1;
103
55e303ae 104const char *panicstr = (char *) 0;
1c79356b
A
105decl_simple_lock_data(,panic_lock)
106int paniccpu;
107volatile int panicwait;
91447636 108volatile unsigned int nestedpanic= 0;
1c79356b
A
109unsigned int panic_is_inited = 0;
110unsigned int return_on_panic = 0;
91447636 111unsigned long panic_caller;
1c79356b 112
c910b4d9
A
113#if CONFIG_EMBEDDED
114#define DEBUG_BUF_SIZE (PAGE_SIZE)
115#else
116#define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
117#endif
118
119char debug_buf[DEBUG_BUF_SIZE];
120char *debug_buf_ptr = debug_buf;
121unsigned int debug_buf_size = sizeof(debug_buf);
2d21ac55
A
122
123static char model_name[64];
6d2010ae 124/* uuid_string_t */ char kernel_uuid[37];
2d21ac55
A
125
126struct 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
137typedef struct pasc pasc_t;
9bccf70c 138
b0d623f7
A
139/* Prevent CPP from breaking the definition below */
140#if CONFIG_NO_PANIC_STRINGS
141#undef Assert
142#endif
143
1c79356b
A
144void
145Assert(
146 const char *file,
147 int line,
2d21ac55 148 const char *expression
2d21ac55 149 )
1c79356b 150{
2d21ac55
A
151 int saved_return_on_panic;
152
1c79356b
A
153 if (!mach_assert) {
154 return;
155 }
2d21ac55
A
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;
1c79356b
A
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() \
172MACRO_BEGIN \
173 if (panic_is_inited) \
174 simple_lock(&panic_lock); \
175MACRO_END
176
177#define PANIC_UNLOCK() \
178MACRO_BEGIN \
179 if (panic_is_inited) \
180 simple_unlock(&panic_lock); \
181MACRO_END
182
183
184void
185panic_init(void)
186{
6d2010ae
A
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
91447636 195 simple_lock_init(&panic_lock, 0);
1c79356b 196 panic_is_inited = 1;
91447636 197 panic_caller = 0;
1c79356b
A
198}
199
2d21ac55
A
200void
201debug_log_init(void)
202{
203 if (debug_buf_size != 0)
204 return;
2d21ac55 205 debug_buf_ptr = debug_buf;
c910b4d9 206 debug_buf_size = sizeof(debug_buf);
2d21ac55
A
207}
208
b0d623f7 209#if defined(__i386__) || defined(__x86_64__)
2d21ac55
A
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
b0d623f7
A
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
225void _consume_panic_args(int a __unused, ...)
226{
6d2010ae 227 panic("panic");
b0d623f7
A
228}
229
1c79356b
A
230void
231panic(const char *str, ...)
232{
233 va_list listp;
234 spl_t s;
235 thread_t thread;
91447636 236 wait_queue_t wq;
1c79356b 237
7ddcb079
A
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
6d2010ae
A
246#if defined(__i386__) || defined(__x86_64__)
247 /* Attempt to display the unparsed panic string */
248 const char *tstr = str;
b0d623f7 249
6d2010ae
A
250 kprintf("Panic initiated, string: ");
251 while (tstr && *tstr)
252 kprintf("%c", *tstr++);
253 kprintf("\n");
254#endif
1c79356b 255
2d21ac55
A
256 panic_safe();
257
1c79356b 258 thread = current_thread(); /* Get failing thread */
91447636 259 wq = thread->wait_queue; /* Save the old value */
2d21ac55 260 thread->wait_queue = NULL; /* Clear the wait so we do not get double panics when we try locks */
1c79356b 261
9bccf70c 262 if( logPanicDataToScreen )
2d21ac55 263 disable_debug_output = FALSE;
9bccf70c 264
1c79356b 265 debug_mode = TRUE;
91447636
A
266
267 /* panic_caller is initialized to 0. If set, don't change it */
268 if ( ! panic_caller )
2d21ac55 269 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
91447636 270
1c79356b
A
271restart:
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");
1c79356b 287 printf("double panic: We are hanging here...\n");
2d21ac55 288 panic_stop();
1c79356b
A
289 /* NOTREACHED */
290 }
291 }
292 panicstr = str;
293 paniccpu = cpu_number();
294 panicwait = 1;
295
296 PANIC_UNLOCK();
b0d623f7 297 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
2d21ac55
A
298 if (str) {
299 va_start(listp, str);
300 _doprnt(str, &listp, consdebug_putc, 0);
301 va_end(listp);
302 }
9bccf70c 303 kdb_printf("\n");
1c79356b
A
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();
91447636 316 thread->wait_queue = wq; /* Restore the wait queue */
2d21ac55 317
55e303ae 318 if (return_on_panic) {
2d21ac55 319 panic_normal();
55e303ae
A
320 enable_preemption();
321 splx(s);
1c79356b 322 return;
55e303ae 323 }
2d21ac55 324
9bccf70c 325 kdb_printf("panic: We are hanging here...\n");
2d21ac55 326 panic_stop();
1c79356b
A
327 /* NOTREACHED */
328}
329
330void
2d21ac55 331log(__unused int level, char *fmt, ...)
1c79356b
A
332{
333 va_list listp;
1c79356b
A
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}
9bccf70c 346
9bccf70c
A
347void
348debug_putc(char c)
349{
2d21ac55
A
350 if ((debug_buf_size != 0) &&
351 ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
9bccf70c
A
352 *debug_buf_ptr=c;
353 debug_buf_ptr++;
354 }
355}
2d21ac55
A
356
357/* In-place packing routines -- inefficient, but they're called at most once.
6d2010ae 358 * Assumes "buflen" is a multiple of 8.
2d21ac55
A
359 */
360
361int packA(char *inbuf, uint32_t length, uint32_t buflen)
362{
363 unsigned int i, j = 0;
364 pasc_t pack;
365
6d2010ae 366 length = MIN(((length + 7) & ~7), buflen);
2d21ac55
A
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 }
6d2010ae 381 return j;
2d21ac55
A
382}
383
384void 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
404extern void *proc_name_address(void *p);
405
406static void
407panic_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
419unsigned panic_active(void) {
420 return ((panicstr != (char *) 0));
421}
422
423void populate_model_name(char *model_string) {
424 strlcpy(model_name, model_string, sizeof(model_name));
425}
426
427static 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
6d2010ae
A
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
439static 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;
2d21ac55 444
6d2010ae
A
445 if (tmp_kernel_uuid[0] != '\0')
446 kdb_printf("Kernel UUID: %s\n", tmp_kernel_uuid);
2d21ac55
A
447}
448
c910b4d9
A
449static 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
2d21ac55
A
456extern const char version[];
457extern char osversion[];
458
6d2010ae
A
459static volatile uint32_t config_displayed = 0;
460
2d21ac55 461__private_extern__ void panic_display_system_configuration(void) {
2d21ac55
A
462
463 panic_display_process_name();
6d2010ae
A
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);
2d21ac55
A
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);
6d2010ae
A
471 panic_display_kernel_uuid();
472 panic_display_pal_info();
2d21ac55 473 panic_display_model_name();
c910b4d9 474 panic_display_uptime();
b0d623f7 475 panic_display_zprint();
6d2010ae
A
476#if CONFIG_ZLEAKS
477 panic_display_ztrace();
478#endif /* CONFIG_ZLEAKS */
b0d623f7 479 kext_dump_panic_lists(&kdb_log);
2d21ac55
A
480 }
481}
482
c910b4d9
A
483extern zone_t first_zone;
484extern unsigned int num_zones, stack_total;
6d2010ae 485extern unsigned long long stack_allocs;
c910b4d9 486
b0d623f7 487#if defined(__i386__) || defined (__x86_64__)
c910b4d9 488extern unsigned int inuse_ptepages_count;
6d2010ae 489extern long long alloc_ptepages_count;
c910b4d9
A
490#endif
491
492extern boolean_t panic_include_zprint;
c910b4d9
A
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
b0d623f7
A
519 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size * stack_total));
520
521#if defined(__i386__) || defined (__x86_64__)
c910b4d9
A
522 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
523#endif
b0d623f7 524
c910b4d9
A
525 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total);
526 }
527}
528
6d2010ae
A
529#if CONFIG_ZLEAKS
530extern boolean_t panic_include_ztrace;
531extern 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
2d21ac55
A
560#if !MACH_KDP
561static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
2d21ac55
A
562
563/* XXX ugly forward declares to stop warnings */
564void *kdp_get_interface(void);
565void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
566void kdp_set_gateway_mac(void *);
567void kdp_set_interface(void *);
568void kdp_register_send_receive(void *, void *);
569void kdp_unregister_send_receive(void *, void *);
570void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
571int kdp_stack_snapshot_geterror(void);
572int kdp_stack_snapshot_bytes_traced(void);
573
574void *
575kdp_get_interface( void)
576{
577 return(void *)0;
578}
579
580unsigned int
581kdp_get_ip_address(void )
582{ return 0; }
583
584struct ether_addr
585kdp_get_mac_addr(void)
586{
587 return kdp_current_mac_address;
588}
589
590void
591kdp_set_ip_and_mac_addresses(
592 __unused struct in_addr *ipaddr,
593 __unused struct ether_addr *macaddr)
594{}
595
596void
597kdp_set_gateway_mac(__unused void *gatewaymac)
598{}
599
600void
601kdp_set_interface(__unused void *ifp)
602{}
603
604void
605kdp_register_send_receive(__unused void *send, __unused void *receive)
606{}
607
608void
609kdp_unregister_send_receive(__unused void *send, __unused void *receive)
610{}
611
612void
613kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf,
614 __unused uint32_t tracebuf_size, __unused uint32_t options)
615{}
616
617int
618kdp_stack_snapshot_geterror(void)
619{
620 return -1;
621}
622
623int
624kdp_stack_snapshot_bytes_traced(void)
625{
626 return 0;
627}
628
629#endif