]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/debug.c
xnu-1699.22.73.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
6d2010ae
A
238#if defined(__i386__) || defined(__x86_64__)
239 /* Attempt to display the unparsed panic string */
240 const char *tstr = str;
b0d623f7 241
6d2010ae
A
242 kprintf("Panic initiated, string: ");
243 while (tstr && *tstr)
244 kprintf("%c", *tstr++);
245 kprintf("\n");
246#endif
b0d623f7
A
247 if (kdebug_enable)
248 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
249
1c79356b 250 s = splhigh();
55e303ae 251 disable_preemption();
1c79356b 252
2d21ac55
A
253 panic_safe();
254
1c79356b 255 thread = current_thread(); /* Get failing thread */
91447636 256 wq = thread->wait_queue; /* Save the old value */
2d21ac55 257 thread->wait_queue = NULL; /* Clear the wait so we do not get double panics when we try locks */
1c79356b 258
9bccf70c 259 if( logPanicDataToScreen )
2d21ac55 260 disable_debug_output = FALSE;
9bccf70c 261
1c79356b 262 debug_mode = TRUE;
91447636
A
263
264 /* panic_caller is initialized to 0. If set, don't change it */
265 if ( ! panic_caller )
2d21ac55 266 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
91447636 267
1c79356b
A
268restart:
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");
1c79356b 284 printf("double panic: We are hanging here...\n");
2d21ac55 285 panic_stop();
1c79356b
A
286 /* NOTREACHED */
287 }
288 }
289 panicstr = str;
290 paniccpu = cpu_number();
291 panicwait = 1;
292
293 PANIC_UNLOCK();
b0d623f7 294 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
2d21ac55
A
295 if (str) {
296 va_start(listp, str);
297 _doprnt(str, &listp, consdebug_putc, 0);
298 va_end(listp);
299 }
9bccf70c 300 kdb_printf("\n");
1c79356b
A
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();
91447636 313 thread->wait_queue = wq; /* Restore the wait queue */
2d21ac55 314
55e303ae 315 if (return_on_panic) {
2d21ac55 316 panic_normal();
55e303ae
A
317 enable_preemption();
318 splx(s);
1c79356b 319 return;
55e303ae 320 }
2d21ac55 321
9bccf70c 322 kdb_printf("panic: We are hanging here...\n");
2d21ac55 323 panic_stop();
1c79356b
A
324 /* NOTREACHED */
325}
326
327void
2d21ac55 328log(__unused int level, char *fmt, ...)
1c79356b
A
329{
330 va_list listp;
1c79356b
A
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}
9bccf70c 343
9bccf70c
A
344void
345debug_putc(char c)
346{
2d21ac55
A
347 if ((debug_buf_size != 0) &&
348 ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
9bccf70c
A
349 *debug_buf_ptr=c;
350 debug_buf_ptr++;
351 }
352}
2d21ac55
A
353
354/* In-place packing routines -- inefficient, but they're called at most once.
6d2010ae 355 * Assumes "buflen" is a multiple of 8.
2d21ac55
A
356 */
357
358int packA(char *inbuf, uint32_t length, uint32_t buflen)
359{
360 unsigned int i, j = 0;
361 pasc_t pack;
362
6d2010ae 363 length = MIN(((length + 7) & ~7), buflen);
2d21ac55
A
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 }
6d2010ae 378 return j;
2d21ac55
A
379}
380
381void 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
401extern void *proc_name_address(void *p);
402
403static void
404panic_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
416unsigned panic_active(void) {
417 return ((panicstr != (char *) 0));
418}
419
420void populate_model_name(char *model_string) {
421 strlcpy(model_name, model_string, sizeof(model_name));
422}
423
424static 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
6d2010ae
A
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
436static 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;
2d21ac55 441
6d2010ae
A
442 if (tmp_kernel_uuid[0] != '\0')
443 kdb_printf("Kernel UUID: %s\n", tmp_kernel_uuid);
2d21ac55
A
444}
445
c910b4d9
A
446static 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
2d21ac55
A
453extern const char version[];
454extern char osversion[];
455
6d2010ae
A
456static volatile uint32_t config_displayed = 0;
457
2d21ac55 458__private_extern__ void panic_display_system_configuration(void) {
2d21ac55
A
459
460 panic_display_process_name();
6d2010ae
A
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);
2d21ac55
A
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);
6d2010ae
A
468 panic_display_kernel_uuid();
469 panic_display_pal_info();
2d21ac55 470 panic_display_model_name();
c910b4d9 471 panic_display_uptime();
b0d623f7 472 panic_display_zprint();
6d2010ae
A
473#if CONFIG_ZLEAKS
474 panic_display_ztrace();
475#endif /* CONFIG_ZLEAKS */
b0d623f7 476 kext_dump_panic_lists(&kdb_log);
2d21ac55
A
477 }
478}
479
c910b4d9
A
480extern zone_t first_zone;
481extern unsigned int num_zones, stack_total;
6d2010ae 482extern unsigned long long stack_allocs;
c910b4d9 483
b0d623f7 484#if defined(__i386__) || defined (__x86_64__)
c910b4d9 485extern unsigned int inuse_ptepages_count;
6d2010ae 486extern long long alloc_ptepages_count;
c910b4d9
A
487#endif
488
489extern boolean_t panic_include_zprint;
c910b4d9
A
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
b0d623f7
A
516 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size * stack_total));
517
518#if defined(__i386__) || defined (__x86_64__)
c910b4d9
A
519 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
520#endif
b0d623f7 521
c910b4d9
A
522 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total);
523 }
524}
525
6d2010ae
A
526#if CONFIG_ZLEAKS
527extern boolean_t panic_include_ztrace;
528extern 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
2d21ac55
A
557#if !MACH_KDP
558static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
2d21ac55
A
559
560/* XXX ugly forward declares to stop warnings */
561void *kdp_get_interface(void);
562void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
563void kdp_set_gateway_mac(void *);
564void kdp_set_interface(void *);
565void kdp_register_send_receive(void *, void *);
566void kdp_unregister_send_receive(void *, void *);
567void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
568int kdp_stack_snapshot_geterror(void);
569int kdp_stack_snapshot_bytes_traced(void);
570
571void *
572kdp_get_interface( void)
573{
574 return(void *)0;
575}
576
577unsigned int
578kdp_get_ip_address(void )
579{ return 0; }
580
581struct ether_addr
582kdp_get_mac_addr(void)
583{
584 return kdp_current_mac_address;
585}
586
587void
588kdp_set_ip_and_mac_addresses(
589 __unused struct in_addr *ipaddr,
590 __unused struct ether_addr *macaddr)
591{}
592
593void
594kdp_set_gateway_mac(__unused void *gatewaymac)
595{}
596
597void
598kdp_set_interface(__unused void *ifp)
599{}
600
601void
602kdp_register_send_receive(__unused void *send, __unused void *receive)
603{}
604
605void
606kdp_unregister_send_receive(__unused void *send, __unused void *receive)
607{}
608
609void
610kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf,
611 __unused uint32_t tracebuf_size, __unused uint32_t options)
612{}
613
614int
615kdp_stack_snapshot_geterror(void)
616{
617 return -1;
618}
619
620int
621kdp_stack_snapshot_bytes_traced(void)
622{
623 return 0;
624}
625
626#endif