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