]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/debug.c
e031b95fd7d1efe176040ed3b722fb8cd075a17c
[apple/xnu.git] / osfmk / kern / debug.c
1 /*
2 * Copyright (c) 2000-2016 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_kdp.h>
59
60 #include <kern/cpu_number.h>
61 #include <kern/kalloc.h>
62 #include <kern/spl.h>
63 #include <kern/thread.h>
64 #include <kern/assert.h>
65 #include <kern/sched_prim.h>
66 #include <kern/misc_protos.h>
67 #include <kern/clock.h>
68 #include <kern/telemetry.h>
69 #include <kern/ecc.h>
70 #include <kern/kern_cdata.h>
71 #include <kern/zalloc.h>
72 #include <vm/vm_kern.h>
73 #include <vm/pmap.h>
74 #include <stdarg.h>
75 #if !(MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
76 #include <kdp/kdp_udp.h>
77 #endif
78
79 #if defined(__i386__) || defined(__x86_64__)
80 #include <i386/cpu_threads.h>
81 #include <i386/pmCPU.h>
82 #endif
83
84 #include <IOKit/IOPlatformExpert.h>
85 #include <machine/pal_routines.h>
86
87 #include <sys/kdebug.h>
88 #include <libkern/OSKextLibPrivate.h>
89 #include <libkern/OSAtomic.h>
90 #include <libkern/kernel_mach_header.h>
91 #include <uuid/uuid.h>
92 #include <mach_debug/zone_info.h>
93
94 #include <os/log_private.h>
95
96 #if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
97 #include <pexpert/pexpert.h> /* For gPanicBase */
98 #endif
99
100
101 unsigned int halt_in_debugger = 0;
102 unsigned int switch_debugger = 0;
103 unsigned int current_debugger = 0;
104 unsigned int active_debugger = 0;
105 unsigned int debug_mode=0;
106 unsigned int disable_debug_output = TRUE;
107 unsigned int systemLogDiags = FALSE;
108 unsigned int panicDebugging = FALSE;
109 unsigned int logPanicDataToScreen = FALSE;
110 unsigned int kdebug_serial = FALSE;
111 boolean_t lock_panic_mode = FALSE;
112
113 int mach_assert = 1;
114
115 const char *panicstr = (char *) 0;
116 decl_simple_lock_data(,panic_lock)
117 int paniccpu;
118 volatile int panicwait;
119 volatile unsigned int nestedpanic= 0;
120 unsigned int panic_is_inited = 0;
121 unsigned int return_on_panic = 0;
122 unsigned long panic_caller;
123
124 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
125
126 /* debug_buf is directly linked with iBoot panic region for ARM64 targets */
127 #if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
128 char *debug_buf_addr = NULL;
129 char *debug_buf_ptr = NULL;
130 unsigned int debug_buf_size = 0;
131 #else
132 char debug_buf[DEBUG_BUF_SIZE];
133 __used char *debug_buf_addr = debug_buf;
134 char *debug_buf_ptr = debug_buf;
135 unsigned int debug_buf_size = sizeof(debug_buf);
136 #endif
137
138 char *debug_buf_stackshot_start;
139 char *debug_buf_stackshot_end;
140
141 static char model_name[64];
142 unsigned char *kernel_uuid;
143 /* uuid_string_t */ char kernel_uuid_string[37];
144
145 static spl_t panic_prologue(const char *str);
146 static void panic_epilogue(spl_t s);
147
148 struct pasc {
149 unsigned a: 7;
150 unsigned b: 7;
151 unsigned c: 7;
152 unsigned d: 7;
153 unsigned e: 7;
154 unsigned f: 7;
155 unsigned g: 7;
156 unsigned h: 7;
157 } __attribute__((packed));
158
159 typedef struct pasc pasc_t;
160
161 /* Prevent CPP from breaking the definition below */
162 #ifdef CONFIG_NO_PANIC_STRINGS
163 #undef Assert
164 #endif
165
166 int kext_assertions_enable =
167 #if DEBUG || DEVELOPMENT
168 TRUE;
169 #else
170 FALSE;
171 #endif
172
173 void __attribute__((noinline))
174 Assert(
175 const char *file,
176 int line,
177 const char *expression
178 )
179 {
180 int saved_return_on_panic;
181
182 if (!mach_assert) {
183 kprintf("%s:%d non-fatal Assertion: %s", file, line, expression);
184 return;
185 }
186
187 saved_return_on_panic = return_on_panic;
188
189 /*
190 * If we don't have a debugger configured, returning from an
191 * assert is a bad, bad idea; there is no guarantee that we
192 * didn't simply assert before we were able to restart the
193 * platform.
194 */
195 if (current_debugger != NO_CUR_DB)
196 return_on_panic = 1;
197
198 panic_plain("%s:%d Assertion failed: %s", file, line, expression);
199
200 return_on_panic = saved_return_on_panic;
201 }
202
203 /*
204 * Carefully use the panic_lock. There's always a chance that
205 * somehow we'll call panic before getting to initialize the
206 * panic_lock -- in this case, we'll assume that the world is
207 * in uniprocessor mode and just avoid using the panic lock.
208 */
209 #define PANIC_LOCK() \
210 MACRO_BEGIN \
211 if (panic_is_inited) \
212 simple_lock(&panic_lock); \
213 MACRO_END
214
215 #define PANIC_UNLOCK() \
216 MACRO_BEGIN \
217 if (panic_is_inited) \
218 simple_unlock(&panic_lock); \
219 MACRO_END
220
221 void
222 panic_init(void)
223 {
224 unsigned long uuidlen = 0;
225 void *uuid;
226
227 uuid = getuuidfromheader(&_mh_execute_header, &uuidlen);
228 if ((uuid != NULL) && (uuidlen == sizeof(uuid_t))) {
229 kernel_uuid = uuid;
230 uuid_unparse_upper(*(uuid_t *)uuid, kernel_uuid_string);
231 }
232
233 simple_lock_init(&panic_lock, 0);
234 panic_is_inited = 1;
235 panic_caller = 0;
236
237 if (!PE_parse_boot_argn("assertions", &mach_assert, sizeof(mach_assert))) {
238 mach_assert = 1;
239 }
240 }
241
242 void
243 debug_log_init(void)
244 {
245 if (debug_buf_size != 0)
246 return;
247 #if (defined(__arm64__) || defined(NAND_PANIC_DEVICE)) && !defined(LEGACY_PANIC_LOGS)
248 if (!gPanicBase) {
249 printf("debug_log_init: Error!! gPanicBase is still not initialized\n");
250 return;
251 }
252 /* Shift debug buf start location and size by 8 bytes for magic header and crc value */
253 debug_buf_addr = (char*)gPanicBase + 8;
254 debug_buf_ptr = debug_buf_addr;
255 debug_buf_size = gPanicSize - 8;
256 #else
257 debug_buf_addr = debug_buf;
258 debug_buf_ptr = debug_buf;
259 debug_buf_size = sizeof(debug_buf);
260 #endif
261 }
262
263 #if defined(__i386__) || defined(__x86_64__)
264 #define panic_stop() pmCPUHalt(PM_HALT_PANIC)
265 #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
266 #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
267 #else
268 #define panic_stop() { while (1) ; }
269 #define panic_safe()
270 #define panic_normal()
271 #endif
272
273 /*
274 * Prevent CPP from breaking the definition below,
275 * since all clients get a #define to prepend line numbers
276 */
277 #undef panic
278
279 void _consume_panic_args(int a __unused, ...)
280 {
281 panic("panic");
282 }
283
284 extern unsigned int write_trace_on_panic;
285
286 static spl_t
287 panic_prologue(const char *str)
288 {
289 spl_t s;
290
291 if (write_trace_on_panic && kdebug_enable) {
292 if (get_preemption_level() == 0 && !ml_at_interrupt_context()) {
293 ml_set_interrupts_enabled(TRUE);
294 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
295 }
296 }
297
298 s = splhigh();
299 disable_preemption();
300 /* Locking code should relax some checks at panic time */
301 lock_panic_mode = TRUE;
302
303 #if defined(__i386__) || defined(__x86_64__)
304 /* Attempt to display the unparsed panic string */
305 const char *tstr = str;
306
307 kprintf("Panic initiated, string: ");
308 while (tstr && *tstr)
309 kprintf("%c", *tstr++);
310 kprintf("\n");
311 #endif
312
313 panic_safe();
314
315 if( logPanicDataToScreen )
316 disable_debug_output = FALSE;
317
318 debug_mode = TRUE;
319
320 restart:
321 PANIC_LOCK();
322
323 if (panicstr) {
324 if (cpu_number() != paniccpu) {
325 PANIC_UNLOCK();
326 /*
327 * Wait until message has been printed to identify correct
328 * cpu that made the first panic.
329 */
330 while (panicwait)
331 continue;
332 goto restart;
333 } else {
334 nestedpanic +=1;
335 PANIC_UNLOCK();
336 // Other cores will not be resumed on double panic
337 Debugger("double panic");
338 // a printf statement here was removed to avoid a panic-loop caused
339 // by a panic from printf
340 panic_stop();
341 /* NOTREACHED */
342 }
343 }
344 panicstr = str;
345 paniccpu = cpu_number();
346 panicwait = 1;
347
348 PANIC_UNLOCK();
349
350 // halt other cores now in anticipation of the debugger call
351 return(s);
352 }
353
354 #if DEVELOPMENT || DEBUG
355 static void
356 panic_epilogue(spl_t s)
357 #else
358 #if !defined(__i386__) && !defined(__x86_64__)
359 __attribute__((noreturn))
360 #endif
361 static void
362 panic_epilogue(__unused spl_t s)
363 #endif
364 {
365 /*
366 * Release panicstr so that we can handle normally other panics.
367 */
368 PANIC_LOCK();
369 panicstr = (char *)0;
370 PANIC_UNLOCK();
371
372 #if DEVELOPMENT || DEBUG
373 if (return_on_panic) {
374 // resume other cores as we are returning
375 panic_normal();
376 enable_preemption();
377 splx(s);
378 return;
379 }
380 #endif
381 kdb_printf("panic: We are hanging here...\n");
382 panic_stop();
383 /* NOTREACHED */
384 }
385
386 #if !DEVELOPMENT && !DEBUG && !defined(__i386__) && !defined(__x86_64__)
387 __attribute__((noreturn))
388 #endif
389 void
390 panic(const char *str, ...)
391 {
392 va_list listp;
393 spl_t s;
394 boolean_t old_doprnt_hide_pointers = doprnt_hide_pointers;
395
396 #if defined (__x86_64__)
397 plctrace_disable();
398 #endif
399 /* panic_caller is initialized to 0. If set, don't change it */
400 if ( ! panic_caller )
401 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
402
403
404 s = panic_prologue(str);
405
406 /* Never hide pointers from panic logs. */
407 doprnt_hide_pointers = FALSE;
408
409 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
410 if (str) {
411 va_start(listp, str);
412 _doprnt(str, &listp, consdebug_putc, 0);
413 va_end(listp);
414 }
415 kdb_printf("\n");
416
417 /*
418 * Release panicwait indicator so that other cpus may call Debugger().
419 */
420 panicwait = 0;
421 Debugger("panic");
422
423 doprnt_hide_pointers = old_doprnt_hide_pointers;
424
425 panic_epilogue(s);
426 }
427
428 /*
429 * panic_with_options: wraps the panic call in a way that allows us to pass
430 * a bitmask of specific debugger options.
431 */
432 #if !DEVELOPMENT && !DEBUG && !defined(__i386__) && !defined(__x86_64__)
433 __attribute__((noreturn))
434 #endif
435 void
436 panic_with_options(unsigned int reason, void *ctx, uint64_t debugger_options_mask, const char *str, ...)
437 {
438 va_list listp;
439 spl_t s;
440
441
442 /* panic_caller is initialized to 0. If set, don't change it */
443 if ( ! panic_caller )
444 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
445
446 s = panic_prologue(str);
447 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
448 if (str) {
449 va_start(listp, str);
450 _doprnt(str, &listp, consdebug_putc, 0);
451 va_end(listp);
452 }
453 kdb_printf("\n");
454
455 /*
456 * Release panicwait indicator so that other cpus may call Debugger().
457 */
458 panicwait = 0;
459 DebuggerWithContext(reason, ctx, "panic", debugger_options_mask);
460 panic_epilogue(s);
461 }
462
463 #if !DEVELOPMENT && !DEBUG && !defined(__i386__) && !defined(__x86_64__)
464 __attribute__((noreturn))
465 #endif
466 void
467 panic_context(unsigned int reason, void *ctx, const char *str, ...)
468 {
469 va_list listp;
470 spl_t s;
471
472
473 /* panic_caller is initialized to 0. If set, don't change it */
474 if ( ! panic_caller )
475 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
476
477 s = panic_prologue(str);
478 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
479 if (str) {
480 va_start(listp, str);
481 _doprnt(str, &listp, consdebug_putc, 0);
482 va_end(listp);
483 }
484 kdb_printf("\n");
485
486 /*
487 * Release panicwait indicator so that other cpus may call Debugger().
488 */
489 panicwait = 0;
490 DebuggerWithContext(reason, ctx, "panic", DEBUGGER_OPTION_NONE);
491 panic_epilogue(s);
492 }
493
494 __attribute__((noinline,not_tail_called))
495 void log(__unused int level, char *fmt, ...)
496 {
497 void *caller = __builtin_return_address(0);
498 va_list listp;
499 va_list listp2;
500
501
502 #ifdef lint
503 level++;
504 #endif /* lint */
505 #ifdef MACH_BSD
506 va_start(listp, fmt);
507 va_copy(listp2, listp);
508
509 disable_preemption();
510 _doprnt(fmt, &listp, cons_putc_locked, 0);
511 enable_preemption();
512
513 va_end(listp);
514
515 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, listp2, caller);
516 va_end(listp2);
517 #endif
518 }
519
520 /*
521 * Skip appending log messages to the new logging infrastructure in contexts
522 * where safety is uncertain. These contexts include:
523 * - When we're in the debugger
524 * - We're in a panic
525 * - Interrupts are disabled
526 * - Or Pre-emption is disabled
527 * In all the above cases, it is potentially unsafe to log messages.
528 */
529
530 boolean_t oslog_is_safe(void) {
531 return (debug_mode == 0 &&
532 not_in_kdp == 1 &&
533 get_preemption_level() == 0 &&
534 ml_get_interrupts_enabled() == TRUE);
535 }
536
537 void
538 debug_putc(char c)
539 {
540 if ((debug_buf_size != 0) &&
541 ((debug_buf_ptr-debug_buf_addr) < (int)debug_buf_size)) {
542 *debug_buf_ptr=c;
543 debug_buf_ptr++;
544 }
545 }
546
547 /* In-place packing routines -- inefficient, but they're called at most once.
548 * Assumes "buflen" is a multiple of 8.
549 */
550
551 int packA(char *inbuf, uint32_t length, uint32_t buflen)
552 {
553 unsigned int i, j = 0;
554 pasc_t pack;
555
556 length = MIN(((length + 7) & ~7), buflen);
557
558 for (i = 0; i < length; i+=8)
559 {
560 pack.a = inbuf[i];
561 pack.b = inbuf[i+1];
562 pack.c = inbuf[i+2];
563 pack.d = inbuf[i+3];
564 pack.e = inbuf[i+4];
565 pack.f = inbuf[i+5];
566 pack.g = inbuf[i+6];
567 pack.h = inbuf[i+7];
568 bcopy ((char *) &pack, inbuf + j, 7);
569 j += 7;
570 }
571 return j;
572 }
573
574 void unpackA(char *inbuf, uint32_t length)
575 {
576 pasc_t packs;
577 unsigned i = 0;
578 length = (length * 8)/7;
579
580 while (i < length) {
581 packs = *(pasc_t *)&inbuf[i];
582 bcopy(&inbuf[i+7], &inbuf[i+8], MAX(0, (int) (length - i - 8)));
583 inbuf[i++] = packs.a;
584 inbuf[i++] = packs.b;
585 inbuf[i++] = packs.c;
586 inbuf[i++] = packs.d;
587 inbuf[i++] = packs.e;
588 inbuf[i++] = packs.f;
589 inbuf[i++] = packs.g;
590 inbuf[i++] = packs.h;
591 }
592 }
593
594 extern void *proc_name_address(void *p);
595
596 static void
597 panic_display_process_name(void) {
598 /* because of scoping issues len(p_comm) from proc_t is hard coded here */
599 char proc_name[17] = "Unknown";
600 task_t ctask = 0;
601 void *cbsd_info = 0;
602
603 if (ml_nofault_copy((vm_offset_t)&current_thread()->task, (vm_offset_t) &ctask, sizeof(task_t)) == sizeof(task_t))
604 if(ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(cbsd_info)) == sizeof(cbsd_info))
605 if (cbsd_info && (ml_nofault_copy((vm_offset_t) proc_name_address(cbsd_info), (vm_offset_t) &proc_name, sizeof(proc_name)) > 0))
606 proc_name[sizeof(proc_name) - 1] = '\0';
607 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name);
608 }
609
610 unsigned panic_active(void) {
611 return ((panicstr != (char *) 0));
612 }
613
614 void populate_model_name(char *model_string) {
615 strlcpy(model_name, model_string, sizeof(model_name));
616 }
617
618 void panic_display_model_name(void) {
619 char tmp_model_name[sizeof(model_name)];
620
621 if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name))
622 return;
623
624 tmp_model_name[sizeof(tmp_model_name) - 1] = '\0';
625
626 if (tmp_model_name[0] != 0)
627 kdb_printf("System model name: %s\n", tmp_model_name);
628 }
629
630 void panic_display_kernel_uuid(void) {
631 char tmp_kernel_uuid[sizeof(kernel_uuid_string)];
632
633 if (ml_nofault_copy((vm_offset_t) &kernel_uuid_string, (vm_offset_t) &tmp_kernel_uuid, sizeof(kernel_uuid_string)) != sizeof(kernel_uuid_string))
634 return;
635
636 if (tmp_kernel_uuid[0] != '\0')
637 kdb_printf("Kernel UUID: %s\n", tmp_kernel_uuid);
638 }
639
640 void panic_display_kernel_aslr(void) {
641 if (vm_kernel_slide) {
642 kdb_printf("Kernel slide: 0x%016lx\n", (unsigned long) vm_kernel_slide);
643 kdb_printf("Kernel text base: %p\n", (void *) vm_kernel_stext);
644 }
645 }
646
647 void panic_display_hibb(void) {
648 #if defined(__i386__) || defined (__x86_64__)
649 kdb_printf("__HIB text base: %p\n", (void *) vm_hib_base);
650 #endif
651 }
652
653 static void panic_display_uptime(void) {
654 uint64_t uptime;
655 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime);
656
657 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime);
658 }
659
660 extern const char version[];
661 extern char osversion[];
662
663 static volatile uint32_t config_displayed = 0;
664
665 __private_extern__ void panic_display_system_configuration(void) {
666
667 panic_display_process_name();
668 if (OSCompareAndSwap(0, 1, &config_displayed)) {
669 char buf[256];
670 if (strlcpy(buf, PE_boot_args(), sizeof(buf)))
671 kdb_printf("Boot args: %s\n", buf);
672 kdb_printf("\nMac OS version:\n%s\n",
673 (osversion[0] != 0) ? osversion : "Not yet set");
674 kdb_printf("\nKernel version:\n%s\n",version);
675 panic_display_kernel_uuid();
676 panic_display_kernel_aslr();
677 panic_display_hibb();
678 panic_display_pal_info();
679 panic_display_model_name();
680 panic_display_uptime();
681 panic_display_zprint();
682 #if CONFIG_ZLEAKS
683 panic_display_ztrace();
684 #endif /* CONFIG_ZLEAKS */
685 kext_dump_panic_lists(&kdb_log);
686 }
687 }
688
689 extern unsigned int stack_total;
690 extern unsigned long long stack_allocs;
691
692 #if defined(__i386__) || defined (__x86_64__)
693 extern unsigned int inuse_ptepages_count;
694 extern long long alloc_ptepages_count;
695 #endif
696
697 extern boolean_t panic_include_zprint;
698 extern vm_offset_t panic_kext_memory_info;
699 extern vm_size_t panic_kext_memory_size;
700
701 __private_extern__ void panic_display_zprint()
702 {
703 if(panic_include_zprint == TRUE) {
704
705 unsigned int i;
706 struct zone zone_copy;
707
708 kdb_printf("%-20s %10s %10s\n", "Zone Name", "Cur Size", "Free Size");
709 for (i = 0; i < num_zones; i++) {
710 if(ml_nofault_copy((vm_offset_t)(&zone_array[i]), (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) {
711 if(zone_copy.cur_size > (1024*1024)) {
712 kdb_printf("%-20s %10lu %10lu\n",zone_copy.zone_name, (uintptr_t)zone_copy.cur_size,(uintptr_t)(zone_copy.countfree * zone_copy.elem_size));
713 }
714 }
715 }
716
717 kdb_printf("%-20s %10lu\n", "Kernel Stacks", (uintptr_t)(kernel_stack_size * stack_total));
718
719 #if defined(__i386__) || defined (__x86_64__)
720 kdb_printf("%-20s %10lu\n", "PageTables",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
721 #endif
722
723 kdb_printf("%-20s %10lu\n", "Kalloc.Large", (uintptr_t)kalloc_large_total);
724 if (panic_kext_memory_info) {
725 mach_memory_info_t *mem_info = (mach_memory_info_t *)panic_kext_memory_info;
726 kdb_printf("\n%-5s %10s\n", "Kmod", "Size");
727 for (i = 0; i < VM_KERN_MEMORY_COUNT + VM_KERN_COUNTER_COUNT; i++) {
728 if (((mem_info[i].flags & VM_KERN_SITE_TYPE) == VM_KERN_SITE_KMOD) && (mem_info[i].size > (1024 * 1024))) {
729 kdb_printf("%-5lld %10lld\n", mem_info[i].site, mem_info[i].size);
730 }
731 }
732 }
733 }
734 }
735
736 #if CONFIG_ECC_LOGGING
737 __private_extern__ void panic_display_ecc_errors()
738 {
739 uint32_t count = ecc_log_get_correction_count();
740
741 if (count > 0) {
742 kdb_printf("ECC Corrections:%u\n", count);
743 }
744 }
745 #endif /* CONFIG_ECC_LOGGING */
746
747 #if CONFIG_ZLEAKS
748 extern boolean_t panic_include_ztrace;
749 extern struct ztrace* top_ztrace;
750 void panic_print_symbol_name(vm_address_t search);
751
752 /*
753 * Prints the backtrace most suspected of being a leaker, if we paniced in the zone allocator.
754 * top_ztrace and panic_include_ztrace comes from osfmk/kern/zalloc.c
755 */
756 __private_extern__ void panic_display_ztrace(void)
757 {
758 if(panic_include_ztrace == TRUE) {
759 unsigned int i = 0;
760 boolean_t keepsyms = FALSE;
761
762 PE_parse_boot_argn("keepsyms", &keepsyms, sizeof (keepsyms));
763 struct ztrace top_ztrace_copy;
764
765 /* Make sure not to trip another panic if there's something wrong with memory */
766 if(ml_nofault_copy((vm_offset_t)top_ztrace, (vm_offset_t)&top_ztrace_copy, sizeof(struct ztrace)) == sizeof(struct ztrace)) {
767 kdb_printf("\nBacktrace suspected of leaking: (outstanding bytes: %lu)\n", (uintptr_t)top_ztrace_copy.zt_size);
768 /* Print the backtrace addresses */
769 for (i = 0; (i < top_ztrace_copy.zt_depth && i < MAX_ZTRACE_DEPTH) ; i++) {
770 kdb_printf("%p ", top_ztrace_copy.zt_stack[i]);
771 if (keepsyms) {
772 panic_print_symbol_name((vm_address_t)top_ztrace_copy.zt_stack[i]);
773 }
774 kdb_printf("\n");
775 }
776 /* Print any kexts in that backtrace, along with their link addresses so we can properly blame them */
777 kmod_panic_dump((vm_offset_t *)&top_ztrace_copy.zt_stack[0], top_ztrace_copy.zt_depth);
778 }
779 else {
780 kdb_printf("\nCan't access top_ztrace...\n");
781 }
782 kdb_printf("\n");
783 }
784 }
785 #endif /* CONFIG_ZLEAKS */
786
787 #if ! (MACH_KDP && CONFIG_KDP_INTERACTIVE_DEBUGGING)
788 static struct kdp_ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
789
790 /* XXX ugly forward declares to stop warnings */
791 void *kdp_get_interface(void);
792 void kdp_set_ip_and_mac_addresses(struct kdp_in_addr *, struct kdp_ether_addr *);
793 void kdp_set_gateway_mac(void *);
794 void kdp_set_interface(void *);
795 void kdp_register_send_receive(void *, void *);
796 void kdp_unregister_send_receive(void *, void *);
797
798 int kdp_stack_snapshot_geterror(void);
799 uint32_t kdp_stack_snapshot_bytes_traced(void);
800
801 void *
802 kdp_get_interface( void)
803 {
804 return(void *)0;
805 }
806
807 unsigned int
808 kdp_get_ip_address(void )
809 { return 0; }
810
811 struct kdp_ether_addr
812 kdp_get_mac_addr(void)
813 {
814 return kdp_current_mac_address;
815 }
816
817 void
818 kdp_set_ip_and_mac_addresses(
819 __unused struct kdp_in_addr *ipaddr,
820 __unused struct kdp_ether_addr *macaddr)
821 {}
822
823 void
824 kdp_set_gateway_mac(__unused void *gatewaymac)
825 {}
826
827 void
828 kdp_set_interface(__unused void *ifp)
829 {}
830
831 void
832 kdp_register_send_receive(__unused void *send, __unused void *receive)
833 {}
834
835 void
836 kdp_unregister_send_receive(__unused void *send, __unused void *receive)
837 {}
838
839 void kdp_register_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
840 {}
841
842 void kdp_unregister_link(__unused kdp_link_t link, __unused kdp_mode_t mode)
843 {}
844
845 #endif
846
847 #if !CONFIG_TELEMETRY
848 int telemetry_gather(user_addr_t buffer __unused, uint32_t *length __unused, boolean_t mark __unused)
849 {
850 return KERN_NOT_SUPPORTED;
851 }
852 #endif