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)))
63 #define INKERNELSTACK(va, th) \
64 (th == THR_ACT_NULL || \
65 (((vm_offset_t)(va)) >= th->thread->kernel_stack && \
66 (((vm_offset_t)(va)) < th->thread->kernel_stack + \
67 KERNEL_STACK_SIZE)) || \
70 #define INKERNELSTACK(va, th) 1
76 struct db_ppc_frame
*f_frame
;
82 db_addr_t f_arg
[DB_NUMARGS_MAX
];
90 db_addr_t db_user_trap_symbol_value
= 0;
91 db_addr_t db_kernel_trap_symbol_value
= 0;
92 db_addr_t db_interrupt_symbol_value
= 0;
93 db_addr_t db_return_to_iret_symbol_value
= 0;
94 db_addr_t db_syscall_symbol_value
= 0;
95 boolean_t db_trace_symbols_found
= FALSE
;
97 extern int db_ppc_reg_value(
98 struct db_variable
* vp
,
101 db_var_aux_param_t ap
);
102 extern void db_find_trace_symbols(void);
103 extern int db_numargs(
104 struct db_ppc_frame
*fp
,
106 extern boolean_t
db_find_arg(
107 struct db_ppc_frame
*frame
,
112 extern void db_nextframe(
113 struct db_ppc_frame
**lfp
,
114 struct db_ppc_frame
**fp
,
117 thread_act_t thr_act
,
123 * Machine register set.
125 struct db_variable db_regs
[] = {
126 /* XXX "pc" is an alias to "srr0"... */
127 { "pc", (int *)&ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
128 { "srr0", (int *)&ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
129 { "srr1", (int *)&ddb_regs
.save_srr1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
130 { "r0", (int *)&ddb_regs
.save_r0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
131 { "r1", (int *)&ddb_regs
.save_r1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
132 { "r2", (int *)&ddb_regs
.save_r2
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
133 { "r3", (int *)&ddb_regs
.save_r3
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
134 { "r4", (int *)&ddb_regs
.save_r4
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
135 { "r5", (int *)&ddb_regs
.save_r5
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
136 { "r6", (int *)&ddb_regs
.save_r6
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
137 { "r7", (int *)&ddb_regs
.save_r7
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
138 { "r8", (int *)&ddb_regs
.save_r8
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
139 { "r9", (int *)&ddb_regs
.save_r9
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
140 { "r10", (int *)&ddb_regs
.save_r10
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
141 { "r11", (int *)&ddb_regs
.save_r11
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
142 { "r12", (int *)&ddb_regs
.save_r12
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
143 { "r13", (int *)&ddb_regs
.save_r13
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
144 { "r14", (int *)&ddb_regs
.save_r14
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
145 { "r15", (int *)&ddb_regs
.save_r15
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
146 { "r16", (int *)&ddb_regs
.save_r16
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
147 { "r17", (int *)&ddb_regs
.save_r17
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
148 { "r18", (int *)&ddb_regs
.save_r18
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
149 { "r19", (int *)&ddb_regs
.save_r19
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
150 { "r20", (int *)&ddb_regs
.save_r20
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
151 { "r21", (int *)&ddb_regs
.save_r21
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
152 { "r22", (int *)&ddb_regs
.save_r22
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
153 { "r23", (int *)&ddb_regs
.save_r23
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
154 { "r24", (int *)&ddb_regs
.save_r24
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
155 { "r25", (int *)&ddb_regs
.save_r25
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
156 { "r26", (int *)&ddb_regs
.save_r26
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
157 { "r27", (int *)&ddb_regs
.save_r27
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
158 { "r28", (int *)&ddb_regs
.save_r28
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
159 { "r29", (int *)&ddb_regs
.save_r29
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
160 { "r30", (int *)&ddb_regs
.save_r30
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
161 { "r31", (int *)&ddb_regs
.save_r31
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
162 { "cr", (int *)&ddb_regs
.save_cr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
163 { "xer", (int *)&ddb_regs
.save_xer
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
164 { "lr", (int *)&ddb_regs
.save_lr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
165 { "ctr", (int *)&ddb_regs
.save_ctr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
167 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
171 struct db_variable
*vp
,
174 db_var_aux_param_t ap
)
177 db_expr_t null_reg
= 0;
178 register thread_act_t thr_act
= ap
->thr_act
;
181 if (db_option(ap
->modif
, 'u')) {
182 if (thr_act
== THR_ACT_NULL
) {
183 if ((thr_act
= current_act()) == THR_ACT_NULL
)
184 db_error("no user registers\n");
186 if (thr_act
== current_act()) {
187 if (IS_USER_TRAP((&ddb_regs
)))
189 else if (INFIXEDSTACK(ddb_regs
.save_r1
))
190 db_error("cannot get/set user registers in nested interrupt\n");
193 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act()) {
196 if (thr_act
->thread
&&
197 !(thr_act
->thread
->state
& TH_STACK_HANDOFF
) &&
198 thr_act
->thread
->kernel_stack
) {
201 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
202 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
203 cpu_to_processor(cpu
)->cpu_data
->active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
204 dp
= (int *) (((int)saved_state
[cpu
]) +
205 (((int) vp
->valuep
) -
213 } else if (thr_act
->thread
&&
214 (thr_act
->thread
->state
&TH_STACK_HANDOFF
)){
215 /* only PC is valid */
216 if (vp
->valuep
== (int *) &ddb_regs
.save_srr0
) {
217 dp
= (int *)(&thr_act
->thread
->continuation
);
227 if (!db_option(ap
->modif
, 'u')) {
228 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
229 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
230 cpu_to_processor(cpu
)->cpu_data
->active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
231 dp
= (int *) (((int)saved_state
[cpu
]) +
232 (((int) vp
->valuep
) -
239 if (!thr_act
|| thr_act
->mact
.pcb
== 0)
240 db_error("no pcb\n");
241 dp
= (int *)((int)thr_act
->mact
.pcb
+
242 ((int)vp
->valuep
- (int)&ddb_regs
));
245 if (flag
== DB_VAR_SET
)
253 db_find_trace_symbols(void)
256 boolean_t found_some
;
259 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
260 db_user_trap_symbol_value
= (db_addr_t
) value
;
263 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
264 db_kernel_trap_symbol_value
= (db_addr_t
) value
;
267 if (db_value_of_name(CC_SYM_PREFIX
"ihandler", &value
)) {
268 db_interrupt_symbol_value
= (db_addr_t
) value
;
272 if (db_value_of_name(CC_SYM_PREFIX
"return_to_iret", &value
)) {
273 db_return_to_iret_symbol_value
= (db_addr_t
) value
;
277 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
278 db_syscall_symbol_value
= (db_addr_t
) value
;
282 db_trace_symbols_found
= TRUE
;
287 struct db_ppc_frame
*fp
,
290 return (DB_NUMARGS_MAX
);
295 struct db_ppc_frame
*fp
,
309 db_find_task_sym_and_offset(calleepc
, &name
, &offset
, task
);
310 calleep
= calleepc
-offset
;
312 for (i
= 0; calleep
< calleepc
; i
++, calleep
++) {
313 if (!DB_CHECK_ACCESS((int) calleep
, 4, task
)) {
316 inst
= db_get_task_value(calleep
, 4, FALSE
, task
);
317 if ((inst
& 0xffff0000) == (0x907f0000 + (narg
<< 21)) ||
318 (inst
& 0xffff0000) == (0x90610000 + (narg
<< 21))) {
319 argp
= (db_addr_t
) &(fp
->f_arg
[narg
]);
329 * Figure out the next frame up in the call stack.
330 * For trap(), we print the address of the faulting instruction and
331 * proceed with the calling frame. We return the ip that faulted.
332 * If the trap was caused by jumping through a bogus pointer, then
333 * the next line in the backtrace will list some random function as
334 * being called. It should get the argument list correct, though.
335 * It might be possible to dig out from the next frame up the name
336 * of the function that faulted, but that could get hairy.
340 struct db_ppc_frame
**lfp
, /* in/out */
341 struct db_ppc_frame
**fp
, /* in/out */
342 db_addr_t
*ip
, /* out */
343 int frame_type
, /* in */
344 thread_act_t thr_act
,
345 db_addr_t linkpc
) /* in */
347 extern char * trap_type
[];
348 extern int TRAP_TYPES
;
350 struct savearea
*saved_regs
;
352 task_t task
= (thr_act
!= THR_ACT_NULL
)? thr_act
->task
: TASK_NULL
;
357 db_printf(">>>>> trap <<<<<\n");
362 db_printf(">>>>> interrupt <<<<<\n");
365 db_printf(">>>>> interrupt <<<<<\n");
369 if (thr_act
!= THR_ACT_NULL
&& thr_act
->mact
.pcb
) {
370 *ip
= (db_addr_t
) thr_act
->mact
.pcb
->save_srr0
;
371 *fp
= (struct db_ppc_frame
*) (thr_act
->mact
.pcb
->save_r1
);
374 /* falling down for unknown case */
379 db_get_task_value((int)&(*fp
)->f_frame
->f_retaddr
,
383 db_get_task_value((int)&(*fp
)->f_retaddr
,
387 *fp
= (struct db_ppc_frame
*)
388 db_get_task_value((int)&(*fp
)->f_frame
, 4, FALSE
, task
);
400 struct db_ppc_frame
*frame
, *lastframe
;
401 db_addr_t callpc
, linkpc
, lastcallpc
;
403 boolean_t kernel_only
= TRUE
;
404 boolean_t trace_thread
= FALSE
;
405 boolean_t trace_all_threads
= FALSE
;
410 thread_act_t th
, top_act
;
414 jmp_buf_t db_jmp_buf
;
415 queue_entry_t act_list
;
417 if (!db_trace_symbols_found
)
418 db_find_trace_symbols();
420 register char *cp
= modif
;
423 while ((c
= *cp
++) != 0) {
427 trace_all_threads
= TRUE
;
435 if (trace_all_threads
) {
436 if (!have_addr
&& !trace_thread
) {
439 act_list
= &(current_task()->thr_acts
);
440 addr
= (db_expr_t
) queue_first(act_list
);
442 else if (trace_thread
) {
444 if (!db_check_act_address_valid((thread_act_t
)addr
)) {
445 if (db_lookup_task((task_t
)addr
) == -1)
447 act_list
= &(((task_t
)addr
)->thr_acts
);
448 addr
= (db_expr_t
) queue_first(act_list
);
451 act_list
= &(((thread_act_t
)addr
)->task
->thr_acts
);
452 thcount
= db_lookup_task_act(((thread_act_t
)addr
)->task
,
458 if (th
== THR_ACT_NULL
)
460 if (th
== THR_ACT_NULL
) {
461 db_printf("no active thr_act\n");
465 act_list
= &th
->task
->thr_acts
;
466 addr
= (db_expr_t
) queue_first(act_list
);
475 top_act
= THR_ACT_NULL
;
480 if (!have_addr
&& !trace_thread
) {
481 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
482 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
483 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
485 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
487 else if (trace_thread
) {
489 th
= (thread_act_t
) addr
;
490 if (!db_check_act_address_valid(th
))
495 if (th
== THR_ACT_NULL
)
497 if (th
== THR_ACT_NULL
) {
498 db_printf("no active thread\n");
502 if (trace_all_threads
)
503 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
504 addr
, thcount
, th
->task
->thr_act_count
);
511 if (th
== current_act()) {
512 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
513 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
514 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
517 if (th
->mact
.pcb
== 0) {
518 db_printf("thread has no pcb\n");
522 register struct savearea
*pss
=
525 db_printf("thread has no shuttle\n");
528 else if ((th
->thread
->state
& TH_STACK_HANDOFF
) ||
529 th
->thread
->kernel_stack
== 0) {
530 register struct savearea
*pss
=
533 db_printf("Continuation ");
534 db_task_printsym((db_expr_t
)th
->thread
->continuation
,
537 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
538 callpc
= (db_addr_t
) (pss
->save_srr0
);
539 linkpc
= (db_addr_t
) (pss
->save_lr
);
544 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
545 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
546 cpu_to_processor(cpu
)->cpu_data
->active_thread
== th
->thread
&&
551 if (top_act
!= THR_ACT_NULL
) {
553 * Trying to get the backtrace of an activation
554 * which is not the top_most one in the RPC chain:
555 * use the activation's pcb.
557 struct savearea
*pss
;
560 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
561 callpc
= (db_addr_t
) (pss
->save_srr0
);
562 linkpc
= (db_addr_t
) (pss
->save_lr
);
565 register struct savearea
*iks
;
570 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
571 frame
= (struct db_ppc_frame
*) (iks
->save_r1
);
572 callpc
= (db_addr_t
) (iks
->save_lr
);
576 * The kernel stack has probably been
577 * paged out (swapped out activation).
580 if (r
== 2) /* 'q' from db_more() */
582 db_printf("<kernel stack (0x%x) error "
583 "(probably swapped out)>\n",
589 db_printf(">>>>> active on cpu %d <<<<<\n",
591 frame
= (struct db_ppc_frame
*)
592 (saved_state
[cpu
]->save_r1
);
593 callpc
= (db_addr_t
) saved_state
[cpu
]->save_srr0
;
594 linkpc
= (db_addr_t
) saved_state
[cpu
]->save_lr
;
600 frame
= (struct db_ppc_frame
*)addr
;
601 th
= (db_default_act
)? db_default_act
: current_act();
602 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
603 if (frame
->f_frame
) {
604 callpc
= (db_addr_t
)db_get_task_value
605 ((int)&frame
->f_frame
->f_retaddr
,
606 4, FALSE
, (user_frame
) ? task
: 0);
607 callpc
= callpc
-sizeof(callpc
);
613 if (!INKERNELSTACK((unsigned)frame
, th
)) {
614 db_printf(">>>>> user space <<<<<\n");
621 lastcallpc
= (db_addr_t
) 0;
622 while (frame_count
-- && frame
!= 0) {
623 int narg
= DB_NUMARGS_MAX
;
627 db_addr_t call_func
= 0;
631 db_symbol_values(NULL
,
632 db_search_task_symbol_and_line(
633 callpc
, DB_STGY_XTRN
, &offset
, &filename
,
634 &linenum
, (user_frame
) ? task
: 0, &narg
),
635 &name
, (db_expr_t
*)&call_func
);
637 db_find_task_sym_and_offset(callpc
,
638 &name
, &off
, (user_frame
) ? task
: 0);
639 offset
= (db_expr_t
) off
;
642 if (user_frame
== 0) {
644 (call_func
== db_user_trap_symbol_value
||
645 call_func
== db_kernel_trap_symbol_value
)) {
648 } else if (call_func
&&
649 call_func
== db_interrupt_symbol_value
) {
650 frame_type
= INTERRUPT
;
652 } else if (call_func
&&
653 call_func
== db_syscall_symbol_value
) {
654 frame_type
= SYSCALL
;
659 if ((r
= _setjmp(db_recover
= &db_jmp_buf
))
662 narg
= db_numargs(frame
,
663 (user_frame
) ? task
: 0);
673 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
675 narg
= db_numargs(frame
,
676 (user_frame
) ? task
: 0);
684 if (name
== 0 || offset
> db_maxoff
) {
685 db_printf("[%08X]0x%08X(", frame
, callpc
);
687 db_printf("[%08X]%s", frame
, name
);
689 db_printf("+%x", offset
);
693 narg
= db_numargs(frame
, (user_frame
) ? task
: 0);
695 for (arg
=0; arg
< narg
; arg
++) {
701 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
704 found
= db_find_arg(frame
, lastframe
->f_retaddr
,
705 (user_frame
) ? task
: 0, arg
, &argp
);
707 value
= db_get_task_value(argp
, 4, FALSE
,
708 (user_frame
) ? task
: 0);
711 if (r
== 2) /* 'q' from db_more() */
713 db_printf("... <stack error>)");
719 db_printf("%08X", value
);
722 argp
= argp
+ sizeof(argp
);
734 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
735 db_nextframe(&lastframe
, &frame
, &callpc
, frame_type
,
736 (user_frame
) ? th
: THR_ACT_NULL
, linkpc
);
737 callpc
= callpc
-sizeof(callpc
);
747 if (th
->lower
!= THR_ACT_NULL
) {
748 if (top_act
== THR_ACT_NULL
)
751 db_printf(">>>>> next activation 0x%x ($task%d.%d) <<<<<\n",
753 db_lookup_task(th
->task
),
754 db_lookup_task_act(th
->task
, th
));
755 goto next_activation
;
760 if (!INKERNELSTACK(lastframe
, th
) ||
761 !INKERNELSTACK((unsigned)frame
, th
))
763 if (user_frame
== 1) {
764 db_printf(">>>>> user space <<<<<\n");
769 if (frame
<= lastframe
) {
770 if ((INKERNELSTACK(lastframe
, th
) && !INKERNELSTACK(frame
, th
))) continue;
771 db_printf("Bad frame pointer: 0x%x\n", frame
);
777 if (trace_all_threads
) {
778 if (top_act
!= THR_ACT_NULL
)
780 th
= (thread_act_t
) queue_next(&th
->thr_acts
);
781 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
783 addr
= (db_expr_t
) th
;