]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/debug.c
xnu-1486.2.11.tar.gz
[apple/xnu.git] / osfmk / kern / debug.c
1 /*
2 * Copyright (c) 2000-2007 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_kdb.h>
59 #include <mach_kgdb.h>
60 #include <mach_kdp.h>
61
62 #include <kern/cpu_number.h>
63 #include <kern/kalloc.h>
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>
70 #include <kern/clock.h>
71 #include <vm/vm_kern.h>
72 #include <vm/pmap.h>
73 #include <stdarg.h>
74 #if !MACH_KDP
75 #include <kdp/kdp_udp.h>
76 #endif
77
78 #ifdef __ppc__
79 #include <ppc/Firmware.h>
80 #include <ppc/low_trace.h>
81 #endif
82
83 #if defined(__i386__) || defined(__x86_64__)
84 #include <i386/cpu_threads.h>
85 #include <i386/pmCPU.h>
86 #endif
87
88 #include <IOKit/IOPlatformExpert.h>
89
90 #include <sys/kdebug.h>
91 #include <libkern/OSKextLibPrivate.h>
92
93 unsigned int halt_in_debugger = 0;
94 unsigned int switch_debugger = 0;
95 unsigned int current_debugger = 0;
96 unsigned int active_debugger = 0;
97 unsigned int debug_mode=0;
98 unsigned int disable_debug_output = TRUE;
99 unsigned int systemLogDiags = FALSE;
100 unsigned int panicDebugging = FALSE;
101 unsigned int logPanicDataToScreen = FALSE;
102
103 int mach_assert = 1;
104
105 const char *panicstr = (char *) 0;
106 decl_simple_lock_data(,panic_lock)
107 int paniccpu;
108 volatile int panicwait;
109 volatile unsigned int nestedpanic= 0;
110 unsigned int panic_is_inited = 0;
111 unsigned int return_on_panic = 0;
112 unsigned long panic_caller;
113
114 #if CONFIG_EMBEDDED
115 #define DEBUG_BUF_SIZE (PAGE_SIZE)
116 #else
117 #define DEBUG_BUF_SIZE (3 * PAGE_SIZE)
118 #endif
119
120 char debug_buf[DEBUG_BUF_SIZE];
121 char *debug_buf_ptr = debug_buf;
122 unsigned int debug_buf_size = sizeof(debug_buf);
123
124 static char model_name[64];
125
126 struct 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
137 typedef struct pasc pasc_t;
138
139 /* Prevent CPP from breaking the definition below */
140 #if CONFIG_NO_PANIC_STRINGS
141 #undef Assert
142 #endif
143
144 void
145 Assert(
146 const char *file,
147 int line,
148 const char *expression
149 )
150 {
151 int saved_return_on_panic;
152
153 if (!mach_assert) {
154 return;
155 }
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;
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() \
172 MACRO_BEGIN \
173 if (panic_is_inited) \
174 simple_lock(&panic_lock); \
175 MACRO_END
176
177 #define PANIC_UNLOCK() \
178 MACRO_BEGIN \
179 if (panic_is_inited) \
180 simple_unlock(&panic_lock); \
181 MACRO_END
182
183
184 void
185 panic_init(void)
186 {
187 simple_lock_init(&panic_lock, 0);
188 panic_is_inited = 1;
189 panic_caller = 0;
190 }
191
192 void
193 debug_log_init(void)
194 {
195 if (debug_buf_size != 0)
196 return;
197 debug_buf_ptr = debug_buf;
198 debug_buf_size = sizeof(debug_buf);
199 }
200
201 #if defined(__i386__) || defined(__x86_64__)
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
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
217 void _consume_panic_args(int a __unused, ...)
218 {
219 panic(NULL);
220 }
221
222 void
223 panic(const char *str, ...)
224 {
225 va_list listp;
226 spl_t s;
227 thread_t thread;
228 wait_queue_t wq;
229
230
231 if (kdebug_enable)
232 kdbg_dump_trace_to_file("/var/tmp/panic.trace");
233
234 s = splhigh();
235 disable_preemption();
236
237 panic_safe();
238
239 #ifdef __ppc__
240 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
241 #endif
242
243 thread = current_thread(); /* Get failing thread */
244 wq = thread->wait_queue; /* Save the old value */
245 thread->wait_queue = NULL; /* Clear the wait so we do not get double panics when we try locks */
246
247 if( logPanicDataToScreen )
248 disable_debug_output = FALSE;
249
250 debug_mode = TRUE;
251
252 /* panic_caller is initialized to 0. If set, don't change it */
253 if ( ! panic_caller )
254 panic_caller = (unsigned long)(char *)__builtin_return_address(0);
255
256 restart:
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");
272 printf("double panic: We are hanging here...\n");
273 panic_stop();
274 /* NOTREACHED */
275 }
276 }
277 panicstr = str;
278 paniccpu = cpu_number();
279 panicwait = 1;
280
281 PANIC_UNLOCK();
282 kdb_printf("panic(cpu %d caller 0x%lx): ", (unsigned) paniccpu, panic_caller);
283 if (str) {
284 va_start(listp, str);
285 _doprnt(str, &listp, consdebug_putc, 0);
286 va_end(listp);
287 }
288 kdb_printf("\n");
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();
301 thread->wait_queue = wq; /* Restore the wait queue */
302
303 if (return_on_panic) {
304 panic_normal();
305 enable_preemption();
306 splx(s);
307 return;
308 }
309
310 kdb_printf("panic: We are hanging here...\n");
311 panic_stop();
312 /* NOTREACHED */
313 }
314
315 void
316 log(__unused int level, char *fmt, ...)
317 {
318 va_list listp;
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 }
331
332 void
333 debug_putc(char c)
334 {
335 if ((debug_buf_size != 0) &&
336 ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) {
337 *debug_buf_ptr=c;
338 debug_buf_ptr++;
339 }
340 }
341
342 /* In-place packing routines -- inefficient, but they're called at most once.
343 */
344
345 int 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
368 void 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
388 extern void *proc_name_address(void *p);
389
390 static void
391 panic_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
403 unsigned panic_active(void) {
404 return ((panicstr != (char *) 0));
405 }
406
407 void populate_model_name(char *model_string) {
408 strlcpy(model_name, model_string, sizeof(model_name));
409 }
410
411 static 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
423 static 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
430 extern const char version[];
431 extern char osversion[];
432
433 __private_extern__ void panic_display_system_configuration(void) {
434 static boolean_t config_displayed = FALSE;
435
436 panic_display_process_name();
437 if (config_displayed == FALSE) {
438 kdb_printf("\nMac OS version:\n%s\n",
439 (osversion[0] != 0) ? osversion : "Not yet set");
440 kdb_printf("\nKernel version:\n%s\n",version);
441 panic_display_model_name();
442 panic_display_uptime();
443 config_displayed = TRUE;
444 panic_display_zprint();
445 kext_dump_panic_lists(&kdb_log);
446 }
447 }
448
449 extern zone_t first_zone;
450 extern unsigned int num_zones, stack_total;
451
452 #if defined(__i386__) || defined (__x86_64__)
453 extern unsigned int inuse_ptepages_count;
454 #endif
455
456 extern boolean_t panic_include_zprint;
457
458 __private_extern__ void panic_display_zprint()
459 {
460 if(panic_include_zprint == TRUE) {
461
462 unsigned int i;
463 struct zone zone_copy;
464
465 if(first_zone!=NULL) {
466 if(ml_nofault_copy((vm_offset_t)first_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) == sizeof(struct zone)) {
467 for (i = 0; i < num_zones; i++) {
468 if(zone_copy.cur_size > (1024*1024)) {
469 kdb_printf("%.20s:%lu\n",zone_copy.zone_name,(uintptr_t)zone_copy.cur_size);
470 }
471
472 if(zone_copy.next_zone == NULL) {
473 break;
474 }
475
476 if(ml_nofault_copy((vm_offset_t)zone_copy.next_zone, (vm_offset_t)&zone_copy, sizeof(struct zone)) != sizeof(struct zone)) {
477 break;
478 }
479 }
480 }
481 }
482
483 kdb_printf("Kernel Stacks:%lu\n",(uintptr_t)(kernel_stack_size * stack_total));
484
485 #if defined(__i386__) || defined (__x86_64__)
486 kdb_printf("PageTables:%lu\n",(uintptr_t)(PAGE_SIZE * inuse_ptepages_count));
487 #endif
488
489 kdb_printf("Kalloc.Large:%lu\n",(uintptr_t)kalloc_large_total);
490 }
491 }
492
493 #if !MACH_KDP
494 static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}};
495
496 /* XXX ugly forward declares to stop warnings */
497 void *kdp_get_interface(void);
498 void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *);
499 void kdp_set_gateway_mac(void *);
500 void kdp_set_interface(void *);
501 void kdp_register_send_receive(void *, void *);
502 void kdp_unregister_send_receive(void *, void *);
503 void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t);
504 int kdp_stack_snapshot_geterror(void);
505 int kdp_stack_snapshot_bytes_traced(void);
506
507 void *
508 kdp_get_interface( void)
509 {
510 return(void *)0;
511 }
512
513 unsigned int
514 kdp_get_ip_address(void )
515 { return 0; }
516
517 struct ether_addr
518 kdp_get_mac_addr(void)
519 {
520 return kdp_current_mac_address;
521 }
522
523 void
524 kdp_set_ip_and_mac_addresses(
525 __unused struct in_addr *ipaddr,
526 __unused struct ether_addr *macaddr)
527 {}
528
529 void
530 kdp_set_gateway_mac(__unused void *gatewaymac)
531 {}
532
533 void
534 kdp_set_interface(__unused void *ifp)
535 {}
536
537 void
538 kdp_register_send_receive(__unused void *send, __unused void *receive)
539 {}
540
541 void
542 kdp_unregister_send_receive(__unused void *send, __unused void *receive)
543 {}
544
545 void
546 kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf,
547 __unused uint32_t tracebuf_size, __unused uint32_t options)
548 {}
549
550 int
551 kdp_stack_snapshot_geterror(void)
552 {
553 return -1;
554 }
555
556 int
557 kdp_stack_snapshot_bytes_traced(void)
558 {
559 return 0;
560 }
561
562 #endif