]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/debug.c
xnu-1504.15.3.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
9bccf70c
A
78#ifdef __ppc__
79#include <ppc/Firmware.h>
80#include <ppc/low_trace.h>
81#endif
82
b0d623f7 83#if defined(__i386__) || defined(__x86_64__)
2d21ac55
A
84#include <i386/cpu_threads.h>
85#include <i386/pmCPU.h>
86#endif
87
88#include <IOKit/IOPlatformExpert.h>
89
b0d623f7
A
90#include <sys/kdebug.h>
91#include <libkern/OSKextLibPrivate.h>
92
1c79356b
A
93unsigned int halt_in_debugger = 0;
94unsigned int switch_debugger = 0;
95unsigned int current_debugger = 0;
96unsigned int active_debugger = 0;
97unsigned int debug_mode=0;
2d21ac55 98unsigned int disable_debug_output = TRUE;
1c79356b 99unsigned int systemLogDiags = FALSE;
91447636 100unsigned int panicDebugging = FALSE;
2d21ac55 101unsigned int logPanicDataToScreen = FALSE;
1c79356b
A
102
103int mach_assert = 1;
104
55e303ae 105const char *panicstr = (char *) 0;
1c79356b
A
106decl_simple_lock_data(,panic_lock)
107int paniccpu;
108volatile int panicwait;
91447636 109volatile unsigned int nestedpanic= 0;
1c79356b
A
110unsigned int panic_is_inited = 0;
111unsigned int return_on_panic = 0;
91447636 112unsigned long panic_caller;
1c79356b 113
c910b4d9
A
114#if CONFIG_EMBEDDED
115#define DEBUG_BUF_SIZE (PAGE_SIZE)
116#else
117#define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
118#endif
119
120char debug_buf[DEBUG_BUF_SIZE];
121char *debug_buf_ptr = debug_buf;
122unsigned int debug_buf_size = sizeof(debug_buf);
2d21ac55
A
123
124static char model_name[64];
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{
91447636 187 simple_lock_init(&panic_lock, 0);
1c79356b 188 panic_is_inited = 1;
91447636 189 panic_caller = 0;
1c79356b
A
190}
191
2d21ac55
A
192void
193debug_log_init(void)
194{
195 if (debug_buf_size != 0)
196 return;
2d21ac55 197 debug_buf_ptr = debug_buf;
c910b4d9 198 debug_buf_size = sizeof(debug_buf);
2d21ac55
A
199}
200
b0d623f7 201#if defined(__i386__) || defined(__x86_64__)
2d21ac55
A
202#define panic_stop() pmCPUHalt(PM_HALT_PANIC)
203#define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE)
204#define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL)
205#else
206#define panic_stop() { while (1) ; }
207#define panic_safe()
208#define panic_normal()
209#endif
210
b0d623f7
A
211/*
212 * Prevent CPP from breaking the definition below,
213 * since all clients get a #define to prepend line numbers
214 */
215#undef panic
216
217void _consume_panic_args(int a __unused, ...)
218{
219 panic(NULL);
220}
221
1c79356b
A
222void
223panic(const char *str, ...)
224{
225 va_list listp;
226 spl_t s;
227 thread_t thread;
91447636 228 wait_queue_t wq;
1c79356b 229
b0d623f7
A
230
231 if (kdebug_enable)
232 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
233
1c79356b 234 s = splhigh();
55e303ae 235 disable_preemption();
1c79356b 236
2d21ac55
A
237 panic_safe();
238
9bccf70c
A
239#ifdef __ppc__
240 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
241#endif
242
1c79356b 243 thread = current_thread(); /* Get failing thread */
91447636 244 wq = thread->wait_queue; /* Save the old value */
2d21ac55 245 thread->wait_queue = NULL; /* Clear the wait so we do not get double panics when we try locks */
1c79356b 246
9bccf70c 247 if( logPanicDataToScreen )
2d21ac55 248 disable_debug_output = FALSE;
9bccf70c 249
1c79356b 250 debug_mode = TRUE;
91447636
A
251
252 /* panic_caller is initialized to 0. If set, don't change it */
253 if ( ! panic_caller )
2d21ac55 254 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
91447636 255
1c79356b
A
256restart:
257 PANIC_LOCK();
258 if (panicstr) {
259 if (cpu_number() != paniccpu) {
260 PANIC_UNLOCK();
261 /*
262 * Wait until message has been printed to identify correct
263 * cpu that made the first panic.
264 */
265 while (panicwait)
266 continue;
267 goto restart;
268 } else {
269 nestedpanic +=1;
270 PANIC_UNLOCK();
271 Debugger("double panic");
1c79356b 272 printf("double panic: We are hanging here...\n");
2d21ac55 273 panic_stop();
1c79356b
A
274 /* NOTREACHED */
275 }
276 }
277 panicstr = str;
278 paniccpu = cpu_number();
279 panicwait = 1;
280
281 PANIC_UNLOCK();
b0d623f7 282 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
2d21ac55
A
283 if (str) {
284 va_start(listp, str);
285 _doprnt(str, &listp, consdebug_putc, 0);
286 va_end(listp);
287 }
9bccf70c 288 kdb_printf("\n");
1c79356b
A
289
290 /*
291 * Release panicwait indicator so that other cpus may call Debugger().
292 */
293 panicwait = 0;
294 Debugger("panic");
295 /*
296 * Release panicstr so that we can handle normally other panics.
297 */
298 PANIC_LOCK();
299 panicstr = (char *)0;
300 PANIC_UNLOCK();
91447636 301 thread->wait_queue = wq; /* Restore the wait queue */
2d21ac55 302
55e303ae 303 if (return_on_panic) {
2d21ac55 304 panic_normal();
55e303ae
A
305 enable_preemption();
306 splx(s);
1c79356b 307 return;
55e303ae 308 }
2d21ac55 309
9bccf70c 310 kdb_printf("panic: We are hanging here...\n");
2d21ac55 311 panic_stop();
1c79356b
A
312 /* NOTREACHED */
313}
314
315void
2d21ac55 316log(__unused int level, char *fmt, ...)
1c79356b
A
317{
318 va_list listp;
1c79356b
A
319
320#ifdef lint
321 level++;
322#endif /* lint */
323#ifdef MACH_BSD
324 disable_preemption();
325 va_start(listp, fmt);
326 _doprnt(fmt, &listp, conslog_putc, 0);
327 va_end(listp);
328 enable_preemption();
329#endif
330}
9bccf70c 331
9bccf70c
A
332void
333debug_putc(char c)
334{
2d21ac55
A
335 if ((debug_buf_size != 0) &&
336 ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
9bccf70c
A
337 *debug_buf_ptr=c;
338 debug_buf_ptr++;
339 }
340}
2d21ac55
A
341
342/* In-place packing routines -- inefficient, but they're called at most once.
343 */
344
345int packA(char *inbuf, uint32_t length, uint32_t buflen)
346{
347 unsigned int i, j = 0;
348 pasc_t pack;
349
350 length = MIN(((length & ~7) +8), buflen);
351
352 for (i = 0; i < length; i+=8)
353 {
354 pack.a = inbuf[i];
355 pack.b = inbuf[i+1];
356 pack.c = inbuf[i+2];
357 pack.d = inbuf[i+3];
358 pack.e = inbuf[i+4];
359 pack.f = inbuf[i+5];
360 pack.g = inbuf[i+6];
361 pack.h = inbuf[i+7];
362 bcopy ((char *) &pack, inbuf + j, 7);
363 j += 7;
364 }
365 return ((length * 7)/8);
366}
367
368void unpackA(char *inbuf, uint32_t length)
369{
370 pasc_t packs;
371 unsigned i = 0;
372 length = (length * 8)/7;
373
374 while (i < length) {
375 packs = *(pasc_t *)&inbuf[i];
376 bcopy(&inbuf[i+7], &inbuf[i+8], MAX(0, (int) (length - i - 8)));
377 inbuf[i++] = packs.a;
378 inbuf[i++] = packs.b;
379 inbuf[i++] = packs.c;
380 inbuf[i++] = packs.d;
381 inbuf[i++] = packs.e;
382 inbuf[i++] = packs.f;
383 inbuf[i++] = packs.g;
384 inbuf[i++] = packs.h;
385 }
386}
387
388extern void *proc_name_address(void *p);
389
390static void
391panic_display_process_name(void) {
392 char proc_name[32] = "Unknown";
393 task_t ctask = 0;
394 void *cbsd_info = 0;
395
396 if (ml_nofault_copy((vm_offset_t)&current_thread()->task, (vm_offset_t) &ctask, sizeof(task_t)) == sizeof(task_t))
397 if(ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(&ctask->bsd_info)) == sizeof(&ctask->bsd_info))
398 if (cbsd_info && (ml_nofault_copy((vm_offset_t) proc_name_address(cbsd_info), (vm_offset_t) &proc_name, sizeof(proc_name)) > 0))
399 proc_name[sizeof(proc_name) - 1] = '\0';
400 kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name);
401}
402
403unsigned panic_active(void) {
404 return ((panicstr != (char *) 0));
405}
406
407void populate_model_name(char *model_string) {
408 strlcpy(model_name, model_string, sizeof(model_name));
409}
410
411static void panic_display_model_name(void) {
412 char tmp_model_name[sizeof(model_name)];
413
414 if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name))
415 return;
416
417 model_name[sizeof(model_name) - 1] = '\0';
418
419 if (model_name[0] != 0)
420 kdb_printf("System model name: %s\n", model_name);
421}
422
c910b4d9
A
423static void panic_display_uptime(void) {
424 uint64_t uptime;
425 absolutetime_to_nanoseconds(mach_absolute_time(), &uptime);
426
427 kdb_printf("\nSystem uptime in nanoseconds: %llu\n", uptime);
428}
429
2d21ac55
A
430extern const char version[];
431extern char osversion[];
432
433__private_extern__ void panic_display_system_configuration(void) {
b7266188 434 static volatile boolean_t config_displayed = FALSE;
2d21ac55
A
435
436 panic_display_process_name();
437 if (config_displayed == FALSE) {
b7266188 438 config_displayed = TRUE;
2d21ac55
A
439 kdb_printf("\nMac OS version:\n%s\n",
440 (osversion[0] != 0) ? osversion : "Not yet set");
441 kdb_printf("\nKernel version:\n%s\n",version);
442 panic_display_model_name();
c910b4d9 443 panic_display_uptime();
b7266188
A
444#if defined(__i386__) || defined(__x86_64__)
445 pmap_pagetable_corruption_msg_log(&kdb_printf);
446#endif /* i386 || x86_64 */
b0d623f7
A
447 panic_display_zprint();
448 kext_dump_panic_lists(&kdb_log);
2d21ac55
A
449 }
450}
451
c910b4d9
A
452extern zone_t first_zone;
453extern unsigned int num_zones, stack_total;
454
b0d623f7 455#if defined(__i386__) || defined (__x86_64__)
c910b4d9
A
456extern unsigned int inuse_ptepages_count;
457#endif
458
459extern boolean_t panic_include_zprint;
c910b4d9
A
460
461__private_extern__ void panic_display_zprint()
462{
463 if(panic_include_zprint == TRUE) {
464
465 unsigned int i;
466 struct zone zone_copy;
467
468 if(first_zone!=NULL) {
469 if(ml_nofault_copy((vm_offset_t)first_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) {
470 for (i = 0; i < num_zones; i++) {
471 if(zone_copy.cur_size > (1024*1024)) {
472 kdb_printf("%.20s:%lu\n",zone_copy.zone_name,(uintptr_t)zone_copy.cur_size);
473 }
474
475 if(zone_copy.next_zone == NULL) {
476 break;
477 }
478
479 if(ml_nofault_copy((vm_offset_t)zone_copy.next_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) != sizeof(struct zone)) {
480 break;
481 }
482 }
483 }
484 }
485
b0d623f7
A
486 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size * stack_total));
487
488#if defined(__i386__) || defined (__x86_64__)
c910b4d9
A
489 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
490#endif
b0d623f7 491
c910b4d9
A
492 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total);
493 }
494}
495
2d21ac55
A
496#if !MACH_KDP
497static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
2d21ac55
A
498
499/* XXX ugly forward declares to stop warnings */
500void *kdp_get_interface(void);
501void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
502void kdp_set_gateway_mac(void *);
503void kdp_set_interface(void *);
504void kdp_register_send_receive(void *, void *);
505void kdp_unregister_send_receive(void *, void *);
506void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
507int kdp_stack_snapshot_geterror(void);
508int kdp_stack_snapshot_bytes_traced(void);
509
510void *
511kdp_get_interface( void)
512{
513 return(void *)0;
514}
515
516unsigned int
517kdp_get_ip_address(void )
518{ return 0; }
519
520struct ether_addr
521kdp_get_mac_addr(void)
522{
523 return kdp_current_mac_address;
524}
525
526void
527kdp_set_ip_and_mac_addresses(
528 __unused struct in_addr *ipaddr,
529 __unused struct ether_addr *macaddr)
530{}
531
532void
533kdp_set_gateway_mac(__unused void *gatewaymac)
534{}
535
536void
537kdp_set_interface(__unused void *ifp)
538{}
539
540void
541kdp_register_send_receive(__unused void *send, __unused void *receive)
542{}
543
544void
545kdp_unregister_send_receive(__unused void *send, __unused void *receive)
546{}
547
548void
549kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf,
550 __unused uint32_t tracebuf_size, __unused uint32_t options)
551{}
552
553int
554kdp_stack_snapshot_geterror(void)
555{
556 return -1;
557}
558
559int
560kdp_stack_snapshot_bytes_traced(void)
561{
562 return 0;
563}
564
565#endif