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 <ppc/cpu_internal.h>
35 #include <ppc/exception.h>
36 #include <machine/asm.h>
37 #include <machine/db_machdep.h>
38 #include <machine/setjmp.h>
39 #include <mach/machine.h>
41 #include <ddb/db_access.h>
42 #include <ddb/db_sym.h>
43 #include <ddb/db_variables.h>
44 #include <ddb/db_command.h>
45 #include <ddb/db_task_thread.h>
46 #include <ddb/db_output.h>
48 extern jmp_buf_t
*db_recover
;
50 struct savearea ddb_null_kregs
;
52 extern vm_offset_t vm_min_inks_addr
; /* set by db_clone_symtabXXX */
54 #define DB_NUMARGS_MAX 5
57 #define INFIXEDSTACK(va) 0 \
59 #define INKERNELSTACK(va, th) 1
62 struct db_ppc_frame
*f_frame
;
68 uint32_t f_arg
[DB_NUMARGS_MAX
];
75 db_addr_t db_user_trap_symbol_value
= 0;
76 db_addr_t db_kernel_trap_symbol_value
= 0;
77 db_addr_t db_interrupt_symbol_value
= 0;
78 db_addr_t db_return_to_iret_symbol_value
= 0;
79 db_addr_t db_syscall_symbol_value
= 0;
80 boolean_t db_trace_symbols_found
= FALSE
;
82 extern int db_ppc_reg_value(
83 struct db_variable
* vp
,
86 db_var_aux_param_t ap
);
87 extern void db_find_trace_symbols(void);
88 extern int db_numargs(
89 struct db_ppc_frame
*fp
,
91 extern boolean_t
db_find_arg(
92 struct db_ppc_frame
*frame
,
97 extern void db_nextframe(
98 struct db_ppc_frame
**lfp
,
99 struct db_ppc_frame
**fp
,
102 thread_act_t thr_act
,
108 * Machine register set.
110 struct db_variable db_regs
[] = {
111 /* XXX "pc" is an alias to "srr0"... */
112 { "pc", &ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
113 { "srr0", &ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
114 { "srr1", &ddb_regs
.save_srr1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
115 { "r0", &ddb_regs
.save_r0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
116 { "r1", &ddb_regs
.save_r1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
117 { "r2", &ddb_regs
.save_r2
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
118 { "r3", &ddb_regs
.save_r3
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
119 { "r4", &ddb_regs
.save_r4
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
120 { "r5", &ddb_regs
.save_r5
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
121 { "r6", &ddb_regs
.save_r6
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
122 { "r7", &ddb_regs
.save_r7
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
123 { "r8", &ddb_regs
.save_r8
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
124 { "r9", &ddb_regs
.save_r9
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
125 { "r10", &ddb_regs
.save_r10
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
126 { "r11", &ddb_regs
.save_r11
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
127 { "r12", &ddb_regs
.save_r12
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
128 { "r13", &ddb_regs
.save_r13
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
129 { "r14", &ddb_regs
.save_r14
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
130 { "r15", &ddb_regs
.save_r15
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
131 { "r16", &ddb_regs
.save_r16
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
132 { "r17", &ddb_regs
.save_r17
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
133 { "r18", &ddb_regs
.save_r18
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
134 { "r19", &ddb_regs
.save_r19
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
135 { "r20", &ddb_regs
.save_r20
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
136 { "r21", &ddb_regs
.save_r21
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
137 { "r22", &ddb_regs
.save_r22
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
138 { "r23", &ddb_regs
.save_r23
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
139 { "r24", &ddb_regs
.save_r24
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
140 { "r25", &ddb_regs
.save_r25
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
141 { "r26", &ddb_regs
.save_r26
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
142 { "r27", &ddb_regs
.save_r27
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
143 { "r28", &ddb_regs
.save_r28
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
144 { "r29", &ddb_regs
.save_r29
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
145 { "r30", &ddb_regs
.save_r30
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
146 { "r31", &ddb_regs
.save_r31
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
147 { "cr", &ddb_regs
.save_cr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
148 { "xer", &ddb_regs
.save_xer
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
149 { "lr", &ddb_regs
.save_lr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
150 { "ctr", &ddb_regs
.save_ctr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
152 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
156 struct db_variable
*vp
,
159 db_var_aux_param_t ap
)
162 db_expr_t null_reg
= 0;
165 register thread_act_t thr_act
= ap
->thr_act
;
168 if (db_option(ap
->modif
, 'u')) {
169 if (thr_act
== THR_ACT_NULL
) {
170 if ((thr_act
= current_thread()) == THR_ACT_NULL
)
171 db_error("no user registers\n");
173 if (thr_act
== current_thread()) {
174 if (IS_USER_TRAP((&ddb_regs
))) dp
= vp
->valuep
;
175 else if (INFIXEDSTACK(ddb_regs
.save_r1
))
176 db_error("cannot get/set user registers in nested interrupt\n");
180 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_thread()) {
184 if (thr_act
->kernel_stack
) {
188 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
189 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
190 cpu_to_processor(cpu
)->active_thread
== thr_act
&&
191 PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
) {
193 dp
= (db_expr_t
)(((uint32_t)(PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
)) +
194 (((uint32_t) vp
->valuep
) -
195 (uint32_t) &ddb_regs
));
200 if (dp
== 0) dp
= &null_reg
;
203 /* only PC is valid */
204 if (vp
->valuep
== (int *) &ddb_regs
.save_srr0
) {
205 dp
= (int *)(&thr_act
->continuation
);
215 if (!db_option(ap
->modif
, 'u')) {
216 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
217 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
218 cpu_to_processor(cpu
)->active_thread
== thr_act
&&
219 PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
) {
220 dp
= (int *) (((int)(PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
)) +
221 (((int) vp
->valuep
) - (int) &ddb_regs
));
227 if (!thr_act
|| thr_act
->machine
.pcb
== 0) db_error("no pcb\n");
228 dp
= (int *)((int)thr_act
->machine
.pcb
+ ((int)vp
->valuep
- (int)&ddb_regs
));
232 if(vp
->valuep
== (int *) &ddb_regs
.save_cr
) { /* Is this the CR we are doing? */
233 dp32
= (uint32_t *)dp
; /* Make this easier */
234 if (flag
== DB_VAR_SET
) *dp32
= *valuep
;
235 else *valuep
= *dp32
;
237 else { /* Normal 64-bit registers */
238 if (flag
== DB_VAR_SET
) *dp
= *valuep
;
239 else *valuep
= *(unsigned long long *)dp
;
247 db_find_trace_symbols(void)
250 boolean_t found_some
;
253 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
254 db_user_trap_symbol_value
= (db_addr_t
) value
;
257 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
258 db_kernel_trap_symbol_value
= (db_addr_t
) value
;
261 if (db_value_of_name(CC_SYM_PREFIX
"ihandler", &value
)) {
262 db_interrupt_symbol_value
= (db_addr_t
) value
;
266 if (db_value_of_name(CC_SYM_PREFIX
"return_to_iret", &value
)) {
267 db_return_to_iret_symbol_value
= (db_addr_t
) value
;
271 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
272 db_syscall_symbol_value
= (db_addr_t
) value
;
276 db_trace_symbols_found
= TRUE
;
281 struct db_ppc_frame
*fp
,
284 return (DB_NUMARGS_MAX
);
289 struct db_ppc_frame
*fp
,
303 db_find_task_sym_and_offset(calleepc
, &name
, &offset
, task
);
304 calleep
= calleepc
-offset
;
306 for (i
= 0; calleep
< calleepc
; i
++, calleep
++) {
307 if (!DB_CHECK_ACCESS((int) calleep
, 4, task
)) {
310 inst
= db_get_task_value(calleep
, 4, FALSE
, task
);
311 if ((inst
& 0xffff0000) == (0x907f0000 + (narg
<< 21)) ||
312 (inst
& 0xffff0000) == (0x90610000 + (narg
<< 21))) {
313 argp
= (db_addr_t
) &(fp
->f_arg
[narg
]);
323 * Figure out the next frame up in the call stack.
324 * For trap(), we print the address of the faulting instruction and
325 * proceed with the calling frame. We return the ip that faulted.
326 * If the trap was caused by jumping through a bogus pointer, then
327 * the next line in the backtrace will list some random function as
328 * being called. It should get the argument list correct, though.
329 * It might be possible to dig out from the next frame up the name
330 * of the function that faulted, but that could get hairy.
334 struct db_ppc_frame
**lfp
, /* in/out */
335 struct db_ppc_frame
**fp
, /* in/out */
336 db_addr_t
*ip
, /* out */
337 int frame_type
, /* in */
338 thread_act_t thr_act
,
339 db_addr_t linkpc
) /* in */
341 extern char * trap_type
[];
342 extern int TRAP_TYPES
;
344 struct savearea
*saved_regs
;
346 task_t task
= (thr_act
!= THR_ACT_NULL
)? thr_act
->task
: TASK_NULL
;
351 db_printf(">>>>> trap <<<<<\n");
356 db_printf(">>>>> interrupt <<<<<\n");
359 db_printf(">>>>> interrupt <<<<<\n");
363 if (thr_act
!= THR_ACT_NULL
&& thr_act
->machine
.pcb
) {
364 *ip
= (db_addr_t
) thr_act
->machine
.pcb
->save_srr0
;
365 *fp
= (struct db_ppc_frame
*) (thr_act
->machine
.pcb
->save_r1
);
368 /* falling down for unknown case */
372 if(!pmap_find_phys(kernel_pmap
, (addr64_t
)*fp
)) { /* Check if this is valid */
373 db_printf("Frame not mapped %08X\n",*fp
); /* Say not found */
374 *fp
= 0; /* Show not found */
375 break; /* Out of here */
380 db_get_task_value((int)&(*fp
)->f_frame
->f_retaddr
,
384 db_get_task_value((int)&(*fp
)->f_retaddr
,
388 *fp
= (struct db_ppc_frame
*)
389 db_get_task_value((int)&(*fp
)->f_frame
, 4, FALSE
, task
);
401 struct db_ppc_frame
*frame
, *lastframe
;
402 db_addr_t callpc
, linkpc
, lastcallpc
;
404 boolean_t kernel_only
= TRUE
;
405 boolean_t trace_thread
= FALSE
;
406 boolean_t trace_all_threads
= FALSE
;
411 thread_act_t th
, top_act
;
415 jmp_buf_t db_jmp_buf
;
416 queue_entry_t act_list
;
418 if (!db_trace_symbols_found
)
419 db_find_trace_symbols();
421 register char *cp
= modif
;
424 while ((c
= *cp
++) != 0) {
428 trace_all_threads
= TRUE
;
436 if (trace_all_threads
) {
437 if (!have_addr
&& !trace_thread
) {
440 act_list
= &(current_task()->threads
);
441 addr
= (db_expr_t
) queue_first(act_list
);
443 else if (trace_thread
) {
445 if (!db_check_act_address_valid((thread_act_t
)addr
)) {
446 if (db_lookup_task((task_t
)addr
) == -1)
448 act_list
= &(((task_t
)addr
)->threads
);
449 addr
= (db_expr_t
) queue_first(act_list
);
452 act_list
= &(((thread_act_t
)addr
)->task
->threads
);
453 thcount
= db_lookup_task_act(((thread_act_t
)addr
)->task
,
459 if (th
== THR_ACT_NULL
)
460 th
= current_thread();
461 if (th
== THR_ACT_NULL
) {
462 db_printf("no active thr_act\n");
466 act_list
= &th
->task
->threads
;
467 addr
= (db_expr_t
) queue_first(act_list
);
476 top_act
= THR_ACT_NULL
;
481 if (!have_addr
&& !trace_thread
) {
482 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
483 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
484 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
485 th
= current_thread();
486 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
488 else if (trace_thread
) {
490 th
= (thread_act_t
) addr
;
491 if (!db_check_act_address_valid(th
))
496 if (th
== THR_ACT_NULL
)
497 th
= current_thread();
498 if (th
== THR_ACT_NULL
) {
499 db_printf("no active thread\n");
503 if (trace_all_threads
)
504 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
505 addr
, thcount
, th
->task
->thread_count
);
512 if (th
== current_thread()) {
513 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
514 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
515 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
518 if (th
->machine
.pcb
== 0) {
519 db_printf("thread has no pcb\n");
522 if (th
->kernel_stack
== 0) {
523 register struct savearea
*pss
=
526 db_printf("Continuation ");
527 db_task_printsym((db_expr_t
)th
->continuation
,
530 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
531 callpc
= (db_addr_t
) (pss
->save_srr0
);
532 linkpc
= (db_addr_t
) (pss
->save_lr
);
537 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
538 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
539 cpu_to_processor(cpu
)->active_thread
== th
&&
540 PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
) {
544 if (top_act
!= THR_ACT_NULL
) {
546 * Trying to get the backtrace of an activation
547 * which is not the top_most one in the RPC chain:
548 * use the activation's pcb.
550 struct savearea
*pss
;
552 pss
= th
->machine
.pcb
;
553 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
554 callpc
= (db_addr_t
) (pss
->save_srr0
);
555 linkpc
= (db_addr_t
) (pss
->save_lr
);
557 if (cpu
== real_ncpus
) {
558 register struct savearea
*iks
;
561 iks
= th
->machine
.pcb
;
563 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
564 frame
= (struct db_ppc_frame
*) (iks
->save_r1
);
565 callpc
= (db_addr_t
) (iks
->save_lr
);
569 * The kernel stack has probably been
570 * paged out (swapped out activation).
573 if (r
== 2) /* 'q' from db_more() */
575 db_printf("<kernel stack (0x%x) error "
576 "(probably swapped out)>\n",
582 db_printf(">>>>> active on cpu %d <<<<<\n",
584 frame
= (struct db_ppc_frame
*)
585 (PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
->save_r1
);
586 callpc
= (db_addr_t
) PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
->save_srr0
;
587 linkpc
= (db_addr_t
) PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
->save_lr
;
593 frame
= (struct db_ppc_frame
*)addr
;
594 th
= (db_default_act
)? db_default_act
: current_thread();
595 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
596 if (frame
->f_frame
) {
597 callpc
= (db_addr_t
)db_get_task_value
598 ((int)&frame
->f_frame
->f_retaddr
,
599 4, FALSE
, (user_frame
) ? task
: 0);
600 callpc
= callpc
-sizeof(callpc
);
606 if (!INKERNELSTACK((unsigned)frame
, th
)) {
607 db_printf(">>>>> user space <<<<<\n");
614 lastcallpc
= (db_addr_t
) 0;
615 while (frame_count
-- && frame
!= 0) {
616 int narg
= DB_NUMARGS_MAX
;
620 db_addr_t call_func
= 0;
624 db_symbol_values(NULL
,
625 db_search_task_symbol_and_line(
626 callpc
, DB_STGY_XTRN
, &offset
, &filename
,
627 &linenum
, (user_frame
) ? task
: 0, &narg
),
628 &name
, (db_expr_t
*)&call_func
);
630 db_find_task_sym_and_offset(callpc
,
631 &name
, &off
, (user_frame
) ? task
: 0);
632 offset
= (db_expr_t
) off
;
635 if (user_frame
== 0) {
637 (call_func
== db_user_trap_symbol_value
||
638 call_func
== db_kernel_trap_symbol_value
)) {
641 } else if (call_func
&&
642 call_func
== db_interrupt_symbol_value
) {
643 frame_type
= INTERRUPT
;
645 } else if (call_func
&&
646 call_func
== db_syscall_symbol_value
) {
647 frame_type
= SYSCALL
;
652 if ((r
= _setjmp(db_recover
= &db_jmp_buf
))
655 narg
= db_numargs(frame
,
656 (user_frame
) ? task
: 0);
666 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
668 narg
= db_numargs(frame
,
669 (user_frame
) ? task
: 0);
677 if (name
== 0 || offset
> db_maxoff
) {
678 db_printf("[%08X]0x%08X(", frame
, callpc
);
680 db_printf("[%08X]%s", frame
, name
);
682 db_printf("+%llx", offset
);
686 narg
= db_numargs(frame
, (user_frame
) ? task
: 0);
688 for (arg
=0; arg
< narg
; arg
++) {
694 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
697 found
= db_find_arg(frame
, lastframe
->f_retaddr
,
698 (user_frame
) ? task
: 0, arg
, &argp
);
700 value
= db_get_task_value(argp
, 4, FALSE
,
701 (user_frame
) ? task
: 0);
704 if (r
== 2) /* 'q' from db_more() */
706 db_printf("... <stack error>)");
712 db_printf("%08X", value
);
715 argp
= argp
+ sizeof(argp
);
727 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
728 db_nextframe(&lastframe
, &frame
, &callpc
, frame_type
,
729 (user_frame
) ? th
: THR_ACT_NULL
, linkpc
);
730 callpc
= callpc
-sizeof(callpc
);
743 if (!INKERNELSTACK(lastframe
, th
) ||
744 !INKERNELSTACK((unsigned)frame
, th
))
746 if (user_frame
== 1) {
747 db_printf(">>>>> user space <<<<<\n");
752 if (frame
<= lastframe
) {
753 if ((INKERNELSTACK(lastframe
, th
) && !INKERNELSTACK(frame
, th
))) continue;
754 db_printf("Bad frame pointer: 0x%x\n", frame
);
760 if (trace_all_threads
) {
761 if (top_act
!= THR_ACT_NULL
)
763 th
= (thread_act_t
) queue_next(&th
->task_threads
);
764 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
766 addr
= (db_expr_t
) th
;