2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
31 #include <mach/boolean.h>
32 #include <vm/vm_map.h>
33 #include <kern/thread.h>
34 #include <kern/processor.h>
35 #include <kern/task.h>
37 #include <machine/asm.h>
38 #include <machine/db_machdep.h>
39 #include <machine/setjmp.h>
40 #include <mach/machine.h>
42 #include <ddb/db_access.h>
43 #include <ddb/db_sym.h>
44 #include <ddb/db_variables.h>
45 #include <ddb/db_command.h>
46 #include <ddb/db_task_thread.h>
47 #include <ddb/db_output.h>
49 extern jmp_buf_t
*db_recover
;
50 extern struct savearea
*saved_state
[];
52 struct savearea ddb_null_kregs
;
54 extern vm_offset_t vm_min_inks_addr
; /* set by db_clone_symtabXXX */
56 #define DB_NUMARGS_MAX 5
59 extern char FixedStackStart
[], FixedStackEnd
[];
60 #define INFIXEDSTACK(va) \
61 ((((vm_offset_t)(va)) >= (vm_offset_t)&FixedStackStart) && \
62 (((vm_offset_t)(va)) < ((vm_offset_t)&FixedStackEnd)))
66 #define INKERNELSTACK(va, th) \
67 (th == THR_ACT_NULL || \
68 (((vm_offset_t)(va)) >= th->thread->kernel_stack && \
69 (((vm_offset_t)(va)) < th->thread->kernel_stack + \
70 KERNEL_STACK_SIZE)) || \
73 #define INKERNELSTACK(va, th) 1
79 struct db_ppc_frame
*f_frame
;
85 db_addr_t f_arg
[DB_NUMARGS_MAX
];
93 db_addr_t db_user_trap_symbol_value
= 0;
94 db_addr_t db_kernel_trap_symbol_value
= 0;
95 db_addr_t db_interrupt_symbol_value
= 0;
96 db_addr_t db_return_to_iret_symbol_value
= 0;
97 db_addr_t db_syscall_symbol_value
= 0;
98 boolean_t db_trace_symbols_found
= FALSE
;
100 extern int db_ppc_reg_value(
101 struct db_variable
* vp
,
104 db_var_aux_param_t ap
);
105 extern void db_find_trace_symbols(void);
106 extern int db_numargs(
107 struct db_ppc_frame
*fp
,
109 extern boolean_t
db_find_arg(
110 struct db_ppc_frame
*frame
,
115 extern void db_nextframe(
116 struct db_ppc_frame
**lfp
,
117 struct db_ppc_frame
**fp
,
120 thread_act_t thr_act
,
126 * Machine register set.
128 struct db_variable db_regs
[] = {
129 /* XXX "pc" is an alias to "srr0"... */
130 { "pc", (int *)&ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
131 { "srr0", (int *)&ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
132 { "srr1", (int *)&ddb_regs
.save_srr1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
133 { "r0", (int *)&ddb_regs
.save_r0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
134 { "r1", (int *)&ddb_regs
.save_r1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
135 { "r2", (int *)&ddb_regs
.save_r2
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
136 { "r3", (int *)&ddb_regs
.save_r3
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
137 { "r4", (int *)&ddb_regs
.save_r4
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
138 { "r5", (int *)&ddb_regs
.save_r5
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
139 { "r6", (int *)&ddb_regs
.save_r6
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
140 { "r7", (int *)&ddb_regs
.save_r7
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
141 { "r8", (int *)&ddb_regs
.save_r8
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
142 { "r9", (int *)&ddb_regs
.save_r9
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
143 { "r10", (int *)&ddb_regs
.save_r10
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
144 { "r11", (int *)&ddb_regs
.save_r11
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
145 { "r12", (int *)&ddb_regs
.save_r12
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
146 { "r13", (int *)&ddb_regs
.save_r13
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
147 { "r14", (int *)&ddb_regs
.save_r14
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
148 { "r15", (int *)&ddb_regs
.save_r15
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
149 { "r16", (int *)&ddb_regs
.save_r16
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
150 { "r17", (int *)&ddb_regs
.save_r17
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
151 { "r18", (int *)&ddb_regs
.save_r18
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
152 { "r19", (int *)&ddb_regs
.save_r19
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
153 { "r20", (int *)&ddb_regs
.save_r20
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
154 { "r21", (int *)&ddb_regs
.save_r21
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
155 { "r22", (int *)&ddb_regs
.save_r22
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
156 { "r23", (int *)&ddb_regs
.save_r23
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
157 { "r24", (int *)&ddb_regs
.save_r24
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
158 { "r25", (int *)&ddb_regs
.save_r25
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
159 { "r26", (int *)&ddb_regs
.save_r26
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
160 { "r27", (int *)&ddb_regs
.save_r27
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
161 { "r28", (int *)&ddb_regs
.save_r28
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
162 { "r29", (int *)&ddb_regs
.save_r29
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
163 { "r30", (int *)&ddb_regs
.save_r30
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
164 { "r31", (int *)&ddb_regs
.save_r31
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
165 { "cr", (int *)&ddb_regs
.save_cr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
166 { "xer", (int *)&ddb_regs
.save_xer
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
167 { "lr", (int *)&ddb_regs
.save_lr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
168 { "ctr", (int *)&ddb_regs
.save_ctr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
170 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
174 struct db_variable
*vp
,
177 db_var_aux_param_t ap
)
180 db_expr_t null_reg
= 0;
181 register thread_act_t thr_act
= ap
->thr_act
;
184 if (db_option(ap
->modif
, 'u')) {
185 if (thr_act
== THR_ACT_NULL
) {
186 if ((thr_act
= current_act()) == THR_ACT_NULL
)
187 db_error("no user registers\n");
189 if (thr_act
== current_act()) {
190 if (IS_USER_TRAP((&ddb_regs
)))
192 else if (INFIXEDSTACK(ddb_regs
.save_r1
))
193 db_error("cannot get/set user registers in nested interrupt\n");
196 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act()) {
199 if (thr_act
->thread
&&
200 !(thr_act
->thread
->state
& TH_STACK_HANDOFF
) &&
201 thr_act
->thread
->kernel_stack
) {
204 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
205 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
206 cpu_to_processor(cpu
)->cpu_data
->active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
207 dp
= (int *) (((int)saved_state
[cpu
]) +
208 (((int) vp
->valuep
) -
216 } else if (thr_act
->thread
&&
217 (thr_act
->thread
->state
&TH_STACK_HANDOFF
)){
218 /* only PC is valid */
219 if (vp
->valuep
== (int *) &ddb_regs
.save_srr0
) {
220 dp
= (int *)(&thr_act
->thread
->continuation
);
230 if (!db_option(ap
->modif
, 'u')) {
231 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
232 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
233 cpu_to_processor(cpu
)->cpu_data
->active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
234 dp
= (int *) (((int)saved_state
[cpu
]) +
235 (((int) vp
->valuep
) -
242 if (!thr_act
|| thr_act
->mact
.pcb
== 0)
243 db_error("no pcb\n");
244 dp
= (int *)((int)thr_act
->mact
.pcb
+
245 ((int)vp
->valuep
- (int)&ddb_regs
));
248 if (flag
== DB_VAR_SET
)
256 db_find_trace_symbols(void)
259 boolean_t found_some
;
262 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
263 db_user_trap_symbol_value
= (db_addr_t
) value
;
266 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
267 db_kernel_trap_symbol_value
= (db_addr_t
) value
;
270 if (db_value_of_name(CC_SYM_PREFIX
"ihandler", &value
)) {
271 db_interrupt_symbol_value
= (db_addr_t
) value
;
275 if (db_value_of_name(CC_SYM_PREFIX
"return_to_iret", &value
)) {
276 db_return_to_iret_symbol_value
= (db_addr_t
) value
;
280 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
281 db_syscall_symbol_value
= (db_addr_t
) value
;
285 db_trace_symbols_found
= TRUE
;
290 struct db_ppc_frame
*fp
,
293 return (DB_NUMARGS_MAX
);
298 struct db_ppc_frame
*fp
,
312 db_find_task_sym_and_offset(calleepc
, &name
, &offset
, task
);
313 calleep
= calleepc
-offset
;
315 for (i
= 0; calleep
< calleepc
; i
++, calleep
++) {
316 if (!DB_CHECK_ACCESS((int) calleep
, 4, task
)) {
319 inst
= db_get_task_value(calleep
, 4, FALSE
, task
);
320 if ((inst
& 0xffff0000) == (0x907f0000 + (narg
<< 21)) ||
321 (inst
& 0xffff0000) == (0x90610000 + (narg
<< 21))) {
322 argp
= (db_addr_t
) &(fp
->f_arg
[narg
]);
332 * Figure out the next frame up in the call stack.
333 * For trap(), we print the address of the faulting instruction and
334 * proceed with the calling frame. We return the ip that faulted.
335 * If the trap was caused by jumping through a bogus pointer, then
336 * the next line in the backtrace will list some random function as
337 * being called. It should get the argument list correct, though.
338 * It might be possible to dig out from the next frame up the name
339 * of the function that faulted, but that could get hairy.
343 struct db_ppc_frame
**lfp
, /* in/out */
344 struct db_ppc_frame
**fp
, /* in/out */
345 db_addr_t
*ip
, /* out */
346 int frame_type
, /* in */
347 thread_act_t thr_act
,
348 db_addr_t linkpc
) /* in */
350 extern char * trap_type
[];
351 extern int TRAP_TYPES
;
353 struct savearea
*saved_regs
;
355 task_t task
= (thr_act
!= THR_ACT_NULL
)? thr_act
->task
: TASK_NULL
;
360 db_printf(">>>>> trap <<<<<\n");
365 db_printf(">>>>> interrupt <<<<<\n");
368 db_printf(">>>>> interrupt <<<<<\n");
372 if (thr_act
!= THR_ACT_NULL
&& thr_act
->mact
.pcb
) {
373 *ip
= (db_addr_t
) thr_act
->mact
.pcb
->save_srr0
;
374 *fp
= (struct db_ppc_frame
*) (thr_act
->mact
.pcb
->save_r1
);
377 /* falling down for unknown case */
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()->thr_acts
);
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
)->thr_acts
);
451 addr
= (db_expr_t
) queue_first(act_list
);
454 act_list
= &(((thread_act_t
)addr
)->task
->thr_acts
);
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
->thr_acts
;
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
->thr_act_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
)->cpu_data
->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
->thr_acts
);
784 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
786 addr
= (db_expr_t
) th
;