2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
28 #include <mach/boolean.h>
29 #include <vm/vm_map.h>
30 #include <kern/thread.h>
31 #include <kern/processor.h>
32 #include <kern/task.h>
34 #include <machine/asm.h>
35 #include <machine/db_machdep.h>
36 #include <machine/setjmp.h>
37 #include <mach/machine.h>
39 #include <ddb/db_access.h>
40 #include <ddb/db_sym.h>
41 #include <ddb/db_variables.h>
42 #include <ddb/db_command.h>
43 #include <ddb/db_task_thread.h>
44 #include <ddb/db_output.h>
46 extern jmp_buf_t
*db_recover
;
47 extern struct savearea
*saved_state
[];
49 struct savearea ddb_null_kregs
;
51 extern vm_offset_t vm_min_inks_addr
; /* set by db_clone_symtabXXX */
53 #define DB_NUMARGS_MAX 5
56 extern char FixedStackStart
[], FixedStackEnd
[];
57 #define INFIXEDSTACK(va) \
58 ((((vm_offset_t)(va)) >= (vm_offset_t)&FixedStackStart) && \
59 (((vm_offset_t)(va)) < ((vm_offset_t)&FixedStackEnd)))
61 #define INKERNELSTACK(va, th) 1
64 struct db_ppc_frame
*f_frame
;
70 uint32_t f_arg
[DB_NUMARGS_MAX
];
77 db_addr_t db_user_trap_symbol_value
= 0;
78 db_addr_t db_kernel_trap_symbol_value
= 0;
79 db_addr_t db_interrupt_symbol_value
= 0;
80 db_addr_t db_return_to_iret_symbol_value
= 0;
81 db_addr_t db_syscall_symbol_value
= 0;
82 boolean_t db_trace_symbols_found
= FALSE
;
84 extern int db_ppc_reg_value(
85 struct db_variable
* vp
,
88 db_var_aux_param_t ap
);
89 extern void db_find_trace_symbols(void);
90 extern int db_numargs(
91 struct db_ppc_frame
*fp
,
93 extern boolean_t
db_find_arg(
94 struct db_ppc_frame
*frame
,
99 extern void db_nextframe(
100 struct db_ppc_frame
**lfp
,
101 struct db_ppc_frame
**fp
,
104 thread_act_t thr_act
,
110 * Machine register set.
112 struct db_variable db_regs
[] = {
113 /* XXX "pc" is an alias to "srr0"... */
114 { "pc", &ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
115 { "srr0", &ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
116 { "srr1", &ddb_regs
.save_srr1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
117 { "r0", &ddb_regs
.save_r0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
118 { "r1", &ddb_regs
.save_r1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
119 { "r2", &ddb_regs
.save_r2
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
120 { "r3", &ddb_regs
.save_r3
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
121 { "r4", &ddb_regs
.save_r4
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
122 { "r5", &ddb_regs
.save_r5
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
123 { "r6", &ddb_regs
.save_r6
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
124 { "r7", &ddb_regs
.save_r7
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
125 { "r8", &ddb_regs
.save_r8
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
126 { "r9", &ddb_regs
.save_r9
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
127 { "r10", &ddb_regs
.save_r10
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
128 { "r11", &ddb_regs
.save_r11
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
129 { "r12", &ddb_regs
.save_r12
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
130 { "r13", &ddb_regs
.save_r13
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
131 { "r14", &ddb_regs
.save_r14
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
132 { "r15", &ddb_regs
.save_r15
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
133 { "r16", &ddb_regs
.save_r16
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
134 { "r17", &ddb_regs
.save_r17
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
135 { "r18", &ddb_regs
.save_r18
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
136 { "r19", &ddb_regs
.save_r19
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
137 { "r20", &ddb_regs
.save_r20
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
138 { "r21", &ddb_regs
.save_r21
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
139 { "r22", &ddb_regs
.save_r22
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
140 { "r23", &ddb_regs
.save_r23
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
141 { "r24", &ddb_regs
.save_r24
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
142 { "r25", &ddb_regs
.save_r25
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
143 { "r26", &ddb_regs
.save_r26
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
144 { "r27", &ddb_regs
.save_r27
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
145 { "r28", &ddb_regs
.save_r28
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
146 { "r29", &ddb_regs
.save_r29
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
147 { "r30", &ddb_regs
.save_r30
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
148 { "r31", &ddb_regs
.save_r31
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
149 { "cr", &ddb_regs
.save_cr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
150 { "xer", &ddb_regs
.save_xer
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
151 { "lr", &ddb_regs
.save_lr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
152 { "ctr", &ddb_regs
.save_ctr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
154 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
158 struct db_variable
*vp
,
161 db_var_aux_param_t ap
)
164 db_expr_t null_reg
= 0;
167 register thread_act_t thr_act
= ap
->thr_act
;
170 if (db_option(ap
->modif
, 'u')) {
171 if (thr_act
== THR_ACT_NULL
) {
172 if ((thr_act
= current_act()) == THR_ACT_NULL
)
173 db_error("no user registers\n");
175 if (thr_act
== current_act()) {
176 if (IS_USER_TRAP((&ddb_regs
))) dp
= vp
->valuep
;
177 else if (INFIXEDSTACK(ddb_regs
.save_r1
))
178 db_error("cannot get/set user registers in nested interrupt\n");
182 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act()) {
186 if (thr_act
->thread
&&
187 !(thr_act
->thread
->state
& TH_STACK_HANDOFF
) &&
188 thr_act
->thread
->kernel_stack
) {
192 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
193 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
194 cpu_to_processor(cpu
)->active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
196 dp
= (db_expr_t
)(((uint32_t)saved_state
[cpu
]) +
197 (((uint32_t) vp
->valuep
) -
198 (uint32_t) &ddb_regs
));
203 if (dp
== 0) dp
= &null_reg
;
205 else if (thr_act
->thread
&& (thr_act
->thread
->state
& TH_STACK_HANDOFF
)){
206 /* only PC is valid */
207 if (vp
->valuep
== (int *) &ddb_regs
.save_srr0
) {
208 dp
= (int *)(&thr_act
->thread
->continuation
);
218 if (!db_option(ap
->modif
, 'u')) {
219 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
220 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
221 cpu_to_processor(cpu
)->active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
222 dp
= (int *) (((int)saved_state
[cpu
]) +
223 (((int) vp
->valuep
) - (int) &ddb_regs
));
229 if (!thr_act
|| thr_act
->mact
.pcb
== 0) db_error("no pcb\n");
230 dp
= (int *)((int)thr_act
->mact
.pcb
+ ((int)vp
->valuep
- (int)&ddb_regs
));
234 if(vp
->valuep
== (int *) &ddb_regs
.save_cr
) { /* Is this the CR we are doing? */
235 dp32
= (uint32_t *)dp
; /* Make this easier */
236 if (flag
== DB_VAR_SET
) *dp32
= *valuep
;
237 else *valuep
= *dp32
;
239 else { /* Normal 64-bit registers */
240 if (flag
== DB_VAR_SET
) *dp
= *valuep
;
241 else *valuep
= *(unsigned long long *)dp
;
249 db_find_trace_symbols(void)
252 boolean_t found_some
;
255 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
256 db_user_trap_symbol_value
= (db_addr_t
) value
;
259 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
260 db_kernel_trap_symbol_value
= (db_addr_t
) value
;
263 if (db_value_of_name(CC_SYM_PREFIX
"ihandler", &value
)) {
264 db_interrupt_symbol_value
= (db_addr_t
) value
;
268 if (db_value_of_name(CC_SYM_PREFIX
"return_to_iret", &value
)) {
269 db_return_to_iret_symbol_value
= (db_addr_t
) value
;
273 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
274 db_syscall_symbol_value
= (db_addr_t
) value
;
278 db_trace_symbols_found
= TRUE
;
283 struct db_ppc_frame
*fp
,
286 return (DB_NUMARGS_MAX
);
291 struct db_ppc_frame
*fp
,
305 db_find_task_sym_and_offset(calleepc
, &name
, &offset
, task
);
306 calleep
= calleepc
-offset
;
308 for (i
= 0; calleep
< calleepc
; i
++, calleep
++) {
309 if (!DB_CHECK_ACCESS((int) calleep
, 4, task
)) {
312 inst
= db_get_task_value(calleep
, 4, FALSE
, task
);
313 if ((inst
& 0xffff0000) == (0x907f0000 + (narg
<< 21)) ||
314 (inst
& 0xffff0000) == (0x90610000 + (narg
<< 21))) {
315 argp
= (db_addr_t
) &(fp
->f_arg
[narg
]);
325 * Figure out the next frame up in the call stack.
326 * For trap(), we print the address of the faulting instruction and
327 * proceed with the calling frame. We return the ip that faulted.
328 * If the trap was caused by jumping through a bogus pointer, then
329 * the next line in the backtrace will list some random function as
330 * being called. It should get the argument list correct, though.
331 * It might be possible to dig out from the next frame up the name
332 * of the function that faulted, but that could get hairy.
336 struct db_ppc_frame
**lfp
, /* in/out */
337 struct db_ppc_frame
**fp
, /* in/out */
338 db_addr_t
*ip
, /* out */
339 int frame_type
, /* in */
340 thread_act_t thr_act
,
341 db_addr_t linkpc
) /* in */
343 extern char * trap_type
[];
344 extern int TRAP_TYPES
;
346 struct savearea
*saved_regs
;
348 task_t task
= (thr_act
!= THR_ACT_NULL
)? thr_act
->task
: TASK_NULL
;
353 db_printf(">>>>> trap <<<<<\n");
358 db_printf(">>>>> interrupt <<<<<\n");
361 db_printf(">>>>> interrupt <<<<<\n");
365 if (thr_act
!= THR_ACT_NULL
&& thr_act
->mact
.pcb
) {
366 *ip
= (db_addr_t
) thr_act
->mact
.pcb
->save_srr0
;
367 *fp
= (struct db_ppc_frame
*) (thr_act
->mact
.pcb
->save_r1
);
370 /* falling down for unknown case */
374 if(!pmap_find_phys(kernel_pmap
, (addr64_t
)*fp
)) { /* Check if this is valid */
375 db_printf("Frame not mapped %08X\n",*fp
); /* Say not found */
376 *fp
= 0; /* Show not found */
377 break; /* Out of here */
382 db_get_task_value((int)&(*fp
)->f_frame
->f_retaddr
,
386 db_get_task_value((int)&(*fp
)->f_retaddr
,
390 *fp
= (struct db_ppc_frame
*)
391 db_get_task_value((int)&(*fp
)->f_frame
, 4, FALSE
, task
);
403 struct db_ppc_frame
*frame
, *lastframe
;
404 db_addr_t callpc
, linkpc
, lastcallpc
;
406 boolean_t kernel_only
= TRUE
;
407 boolean_t trace_thread
= FALSE
;
408 boolean_t trace_all_threads
= FALSE
;
413 thread_act_t th
, top_act
;
417 jmp_buf_t db_jmp_buf
;
418 queue_entry_t act_list
;
420 if (!db_trace_symbols_found
)
421 db_find_trace_symbols();
423 register char *cp
= modif
;
426 while ((c
= *cp
++) != 0) {
430 trace_all_threads
= TRUE
;
438 if (trace_all_threads
) {
439 if (!have_addr
&& !trace_thread
) {
442 act_list
= &(current_task()->threads
);
443 addr
= (db_expr_t
) queue_first(act_list
);
445 else if (trace_thread
) {
447 if (!db_check_act_address_valid((thread_act_t
)addr
)) {
448 if (db_lookup_task((task_t
)addr
) == -1)
450 act_list
= &(((task_t
)addr
)->threads
);
451 addr
= (db_expr_t
) queue_first(act_list
);
454 act_list
= &(((thread_act_t
)addr
)->task
->threads
);
455 thcount
= db_lookup_task_act(((thread_act_t
)addr
)->task
,
461 if (th
== THR_ACT_NULL
)
463 if (th
== THR_ACT_NULL
) {
464 db_printf("no active thr_act\n");
468 act_list
= &th
->task
->threads
;
469 addr
= (db_expr_t
) queue_first(act_list
);
478 top_act
= THR_ACT_NULL
;
483 if (!have_addr
&& !trace_thread
) {
484 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
485 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
486 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
488 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
490 else if (trace_thread
) {
492 th
= (thread_act_t
) addr
;
493 if (!db_check_act_address_valid(th
))
498 if (th
== THR_ACT_NULL
)
500 if (th
== THR_ACT_NULL
) {
501 db_printf("no active thread\n");
505 if (trace_all_threads
)
506 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
507 addr
, thcount
, th
->task
->thread_count
);
514 if (th
== current_act()) {
515 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
516 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
517 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
520 if (th
->mact
.pcb
== 0) {
521 db_printf("thread has no pcb\n");
525 register struct savearea
*pss
=
528 db_printf("thread has no shuttle\n");
531 else if ((th
->thread
->state
& TH_STACK_HANDOFF
) ||
532 th
->thread
->kernel_stack
== 0) {
533 register struct savearea
*pss
=
536 db_printf("Continuation ");
537 db_task_printsym((db_expr_t
)th
->thread
->continuation
,
540 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
541 callpc
= (db_addr_t
) (pss
->save_srr0
);
542 linkpc
= (db_addr_t
) (pss
->save_lr
);
547 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
548 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
549 cpu_to_processor(cpu
)->active_thread
== th
->thread
&&
554 if (top_act
!= THR_ACT_NULL
) {
556 * Trying to get the backtrace of an activation
557 * which is not the top_most one in the RPC chain:
558 * use the activation's pcb.
560 struct savearea
*pss
;
563 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
564 callpc
= (db_addr_t
) (pss
->save_srr0
);
565 linkpc
= (db_addr_t
) (pss
->save_lr
);
568 register struct savearea
*iks
;
573 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
574 frame
= (struct db_ppc_frame
*) (iks
->save_r1
);
575 callpc
= (db_addr_t
) (iks
->save_lr
);
579 * The kernel stack has probably been
580 * paged out (swapped out activation).
583 if (r
== 2) /* 'q' from db_more() */
585 db_printf("<kernel stack (0x%x) error "
586 "(probably swapped out)>\n",
592 db_printf(">>>>> active on cpu %d <<<<<\n",
594 frame
= (struct db_ppc_frame
*)
595 (saved_state
[cpu
]->save_r1
);
596 callpc
= (db_addr_t
) saved_state
[cpu
]->save_srr0
;
597 linkpc
= (db_addr_t
) saved_state
[cpu
]->save_lr
;
603 frame
= (struct db_ppc_frame
*)addr
;
604 th
= (db_default_act
)? db_default_act
: current_act();
605 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
606 if (frame
->f_frame
) {
607 callpc
= (db_addr_t
)db_get_task_value
608 ((int)&frame
->f_frame
->f_retaddr
,
609 4, FALSE
, (user_frame
) ? task
: 0);
610 callpc
= callpc
-sizeof(callpc
);
616 if (!INKERNELSTACK((unsigned)frame
, th
)) {
617 db_printf(">>>>> user space <<<<<\n");
624 lastcallpc
= (db_addr_t
) 0;
625 while (frame_count
-- && frame
!= 0) {
626 int narg
= DB_NUMARGS_MAX
;
630 db_addr_t call_func
= 0;
634 db_symbol_values(NULL
,
635 db_search_task_symbol_and_line(
636 callpc
, DB_STGY_XTRN
, &offset
, &filename
,
637 &linenum
, (user_frame
) ? task
: 0, &narg
),
638 &name
, (db_expr_t
*)&call_func
);
640 db_find_task_sym_and_offset(callpc
,
641 &name
, &off
, (user_frame
) ? task
: 0);
642 offset
= (db_expr_t
) off
;
645 if (user_frame
== 0) {
647 (call_func
== db_user_trap_symbol_value
||
648 call_func
== db_kernel_trap_symbol_value
)) {
651 } else if (call_func
&&
652 call_func
== db_interrupt_symbol_value
) {
653 frame_type
= INTERRUPT
;
655 } else if (call_func
&&
656 call_func
== db_syscall_symbol_value
) {
657 frame_type
= SYSCALL
;
662 if ((r
= _setjmp(db_recover
= &db_jmp_buf
))
665 narg
= db_numargs(frame
,
666 (user_frame
) ? task
: 0);
676 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
678 narg
= db_numargs(frame
,
679 (user_frame
) ? task
: 0);
687 if (name
== 0 || offset
> db_maxoff
) {
688 db_printf("[%08X]0x%08X(", frame
, callpc
);
690 db_printf("[%08X]%s", frame
, name
);
692 db_printf("+%x", offset
);
696 narg
= db_numargs(frame
, (user_frame
) ? task
: 0);
698 for (arg
=0; arg
< narg
; arg
++) {
704 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
707 found
= db_find_arg(frame
, lastframe
->f_retaddr
,
708 (user_frame
) ? task
: 0, arg
, &argp
);
710 value
= db_get_task_value(argp
, 4, FALSE
,
711 (user_frame
) ? task
: 0);
714 if (r
== 2) /* 'q' from db_more() */
716 db_printf("... <stack error>)");
722 db_printf("%08X", value
);
725 argp
= argp
+ sizeof(argp
);
737 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
738 db_nextframe(&lastframe
, &frame
, &callpc
, frame_type
,
739 (user_frame
) ? th
: THR_ACT_NULL
, linkpc
);
740 callpc
= callpc
-sizeof(callpc
);
750 if (th
->lower
!= THR_ACT_NULL
) {
751 if (top_act
== THR_ACT_NULL
)
754 db_printf(">>>>> next activation 0x%x ($task%d.%d) <<<<<\n",
756 db_lookup_task(th
->task
),
757 db_lookup_task_act(th
->task
, th
));
758 goto next_activation
;
763 if (!INKERNELSTACK(lastframe
, th
) ||
764 !INKERNELSTACK((unsigned)frame
, th
))
766 if (user_frame
== 1) {
767 db_printf(">>>>> user space <<<<<\n");
772 if (frame
<= lastframe
) {
773 if ((INKERNELSTACK(lastframe
, th
) && !INKERNELSTACK(frame
, th
))) continue;
774 db_printf("Bad frame pointer: 0x%x\n", frame
);
780 if (trace_all_threads
) {
781 if (top_act
!= THR_ACT_NULL
)
783 th
= (thread_act_t
) queue_next(&th
->task_threads
);
784 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
786 addr
= (db_expr_t
) th
;