2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
34 #include <mach/boolean.h>
35 #include <vm/vm_map.h>
36 #include <kern/thread.h>
37 #include <kern/processor.h>
38 #include <kern/task.h>
40 #include <ppc/cpu_internal.h>
41 #include <ppc/exception.h>
42 #include <machine/asm.h>
43 #include <machine/db_machdep.h>
44 #include <machine/setjmp.h>
45 #include <mach/machine.h>
47 #include <ddb/db_access.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/db_variables.h>
50 #include <ddb/db_command.h>
51 #include <ddb/db_task_thread.h>
52 #include <ddb/db_output.h>
54 extern jmp_buf_t
*db_recover
;
56 struct savearea ddb_null_kregs
;
58 extern vm_offset_t vm_min_inks_addr
; /* set by db_clone_symtabXXX */
60 #define DB_NUMARGS_MAX 5
63 #define INFIXEDSTACK(va) 0 \
65 #define INKERNELSTACK(va, th) 1
68 struct db_ppc_frame
*f_frame
;
74 uint32_t f_arg
[DB_NUMARGS_MAX
];
81 db_addr_t db_user_trap_symbol_value
= 0;
82 db_addr_t db_kernel_trap_symbol_value
= 0;
83 db_addr_t db_interrupt_symbol_value
= 0;
84 db_addr_t db_return_to_iret_symbol_value
= 0;
85 db_addr_t db_syscall_symbol_value
= 0;
86 boolean_t db_trace_symbols_found
= FALSE
;
88 extern int db_ppc_reg_value(
89 struct db_variable
* vp
,
92 db_var_aux_param_t ap
);
93 extern void db_find_trace_symbols(void);
94 extern int db_numargs(
95 struct db_ppc_frame
*fp
,
97 extern boolean_t
db_find_arg(
98 struct db_ppc_frame
*frame
,
103 extern void db_nextframe(
104 struct db_ppc_frame
**lfp
,
105 struct db_ppc_frame
**fp
,
108 thread_act_t thr_act
,
114 * Machine register set.
116 struct db_variable db_regs
[] = {
117 /* XXX "pc" is an alias to "srr0"... */
118 { "pc", &ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
119 { "srr0", &ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
120 { "srr1", &ddb_regs
.save_srr1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
121 { "r0", &ddb_regs
.save_r0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
122 { "r1", &ddb_regs
.save_r1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
123 { "r2", &ddb_regs
.save_r2
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
124 { "r3", &ddb_regs
.save_r3
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
125 { "r4", &ddb_regs
.save_r4
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
126 { "r5", &ddb_regs
.save_r5
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
127 { "r6", &ddb_regs
.save_r6
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
128 { "r7", &ddb_regs
.save_r7
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
129 { "r8", &ddb_regs
.save_r8
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
130 { "r9", &ddb_regs
.save_r9
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
131 { "r10", &ddb_regs
.save_r10
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
132 { "r11", &ddb_regs
.save_r11
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
133 { "r12", &ddb_regs
.save_r12
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
134 { "r13", &ddb_regs
.save_r13
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
135 { "r14", &ddb_regs
.save_r14
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
136 { "r15", &ddb_regs
.save_r15
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
137 { "r16", &ddb_regs
.save_r16
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
138 { "r17", &ddb_regs
.save_r17
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
139 { "r18", &ddb_regs
.save_r18
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
140 { "r19", &ddb_regs
.save_r19
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
141 { "r20", &ddb_regs
.save_r20
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
142 { "r21", &ddb_regs
.save_r21
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
143 { "r22", &ddb_regs
.save_r22
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
144 { "r23", &ddb_regs
.save_r23
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
145 { "r24", &ddb_regs
.save_r24
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
146 { "r25", &ddb_regs
.save_r25
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
147 { "r26", &ddb_regs
.save_r26
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
148 { "r27", &ddb_regs
.save_r27
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
149 { "r28", &ddb_regs
.save_r28
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
150 { "r29", &ddb_regs
.save_r29
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
151 { "r30", &ddb_regs
.save_r30
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
152 { "r31", &ddb_regs
.save_r31
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
153 { "cr", &ddb_regs
.save_cr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
154 { "xer", &ddb_regs
.save_xer
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
155 { "lr", &ddb_regs
.save_lr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
156 { "ctr", &ddb_regs
.save_ctr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
158 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
162 struct db_variable
*vp
,
165 db_var_aux_param_t ap
)
168 db_expr_t null_reg
= 0;
171 register thread_act_t thr_act
= ap
->thr_act
;
174 if (db_option(ap
->modif
, 'u')) {
175 if (thr_act
== THR_ACT_NULL
) {
176 if ((thr_act
= current_thread()) == THR_ACT_NULL
)
177 db_error("no user registers\n");
179 if (thr_act
== current_thread()) {
180 if (IS_USER_TRAP((&ddb_regs
))) dp
= vp
->valuep
;
181 else if (INFIXEDSTACK(ddb_regs
.save_r1
))
182 db_error("cannot get/set user registers in nested interrupt\n");
186 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_thread()) {
190 if (thr_act
->kernel_stack
) {
194 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
195 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
196 cpu_to_processor(cpu
)->active_thread
== thr_act
&&
197 PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
) {
199 dp
= (db_expr_t
)(((uint32_t)(PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
)) +
200 (((uint32_t) vp
->valuep
) -
201 (uint32_t) &ddb_regs
));
206 if (dp
== 0) dp
= &null_reg
;
209 /* only PC is valid */
210 if (vp
->valuep
== (int *) &ddb_regs
.save_srr0
) {
211 dp
= (int *)(&thr_act
->continuation
);
221 if (!db_option(ap
->modif
, 'u')) {
222 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
223 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
224 cpu_to_processor(cpu
)->active_thread
== thr_act
&&
225 PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
) {
226 dp
= (int *) (((int)(PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
)) +
227 (((int) vp
->valuep
) - (int) &ddb_regs
));
233 if (!thr_act
|| thr_act
->machine
.pcb
== 0) db_error("no pcb\n");
234 dp
= (int *)((int)thr_act
->machine
.pcb
+ ((int)vp
->valuep
- (int)&ddb_regs
));
238 if(vp
->valuep
== (int *) &ddb_regs
.save_cr
) { /* Is this the CR we are doing? */
239 dp32
= (uint32_t *)dp
; /* Make this easier */
240 if (flag
== DB_VAR_SET
) *dp32
= *valuep
;
241 else *valuep
= *dp32
;
243 else { /* Normal 64-bit registers */
244 if (flag
== DB_VAR_SET
) *dp
= *valuep
;
245 else *valuep
= *(unsigned long long *)dp
;
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
->machine
.pcb
) {
370 *ip
= (db_addr_t
) thr_act
->machine
.pcb
->save_srr0
;
371 *fp
= (struct db_ppc_frame
*) (thr_act
->machine
.pcb
->save_r1
);
374 /* falling down for unknown case */
378 if(!pmap_find_phys(kernel_pmap
, (addr64_t
)*fp
)) { /* Check if this is valid */
379 db_printf("Frame not mapped %08X\n",*fp
); /* Say not found */
380 *fp
= 0; /* Show not found */
381 break; /* Out of here */
386 db_get_task_value((int)&(*fp
)->f_frame
->f_retaddr
,
390 db_get_task_value((int)&(*fp
)->f_retaddr
,
394 *fp
= (struct db_ppc_frame
*)
395 db_get_task_value((int)&(*fp
)->f_frame
, 4, FALSE
, task
);
407 struct db_ppc_frame
*frame
, *lastframe
;
408 db_addr_t callpc
, linkpc
, lastcallpc
;
410 boolean_t kernel_only
= TRUE
;
411 boolean_t trace_thread
= FALSE
;
412 boolean_t trace_all_threads
= FALSE
;
417 thread_act_t th
, top_act
;
421 jmp_buf_t db_jmp_buf
;
422 queue_entry_t act_list
;
424 if (!db_trace_symbols_found
)
425 db_find_trace_symbols();
427 register char *cp
= modif
;
430 while ((c
= *cp
++) != 0) {
434 trace_all_threads
= TRUE
;
442 if (trace_all_threads
) {
443 if (!have_addr
&& !trace_thread
) {
446 act_list
= &(current_task()->threads
);
447 addr
= (db_expr_t
) queue_first(act_list
);
449 else if (trace_thread
) {
451 if (!db_check_act_address_valid((thread_act_t
)addr
)) {
452 if (db_lookup_task((task_t
)addr
) == -1)
454 act_list
= &(((task_t
)addr
)->threads
);
455 addr
= (db_expr_t
) queue_first(act_list
);
458 act_list
= &(((thread_act_t
)addr
)->task
->threads
);
459 thcount
= db_lookup_task_act(((thread_act_t
)addr
)->task
,
465 if (th
== THR_ACT_NULL
)
466 th
= current_thread();
467 if (th
== THR_ACT_NULL
) {
468 db_printf("no active thr_act\n");
472 act_list
= &th
->task
->threads
;
473 addr
= (db_expr_t
) queue_first(act_list
);
482 top_act
= THR_ACT_NULL
;
487 if (!have_addr
&& !trace_thread
) {
488 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
489 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
490 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
491 th
= current_thread();
492 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
494 else if (trace_thread
) {
496 th
= (thread_act_t
) addr
;
497 if (!db_check_act_address_valid(th
))
502 if (th
== THR_ACT_NULL
)
503 th
= current_thread();
504 if (th
== THR_ACT_NULL
) {
505 db_printf("no active thread\n");
509 if (trace_all_threads
)
510 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
511 addr
, thcount
, th
->task
->thread_count
);
518 if (th
== current_thread()) {
519 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
520 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
521 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
524 if (th
->machine
.pcb
== 0) {
525 db_printf("thread has no pcb\n");
528 if (th
->kernel_stack
== 0) {
529 register struct savearea
*pss
=
532 db_printf("Continuation ");
533 db_task_printsym((db_expr_t
)th
->continuation
,
536 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
537 callpc
= (db_addr_t
) (pss
->save_srr0
);
538 linkpc
= (db_addr_t
) (pss
->save_lr
);
543 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
544 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
545 cpu_to_processor(cpu
)->active_thread
== th
&&
546 PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
) {
550 if (top_act
!= THR_ACT_NULL
) {
552 * Trying to get the backtrace of an activation
553 * which is not the top_most one in the RPC chain:
554 * use the activation's pcb.
556 struct savearea
*pss
;
558 pss
= th
->machine
.pcb
;
559 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
560 callpc
= (db_addr_t
) (pss
->save_srr0
);
561 linkpc
= (db_addr_t
) (pss
->save_lr
);
563 if (cpu
== real_ncpus
) {
564 register struct savearea
*iks
;
567 iks
= th
->machine
.pcb
;
569 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
570 frame
= (struct db_ppc_frame
*) (iks
->save_r1
);
571 callpc
= (db_addr_t
) (iks
->save_lr
);
575 * The kernel stack has probably been
576 * paged out (swapped out activation).
579 if (r
== 2) /* 'q' from db_more() */
581 db_printf("<kernel stack (0x%x) error "
582 "(probably swapped out)>\n",
588 db_printf(">>>>> active on cpu %d <<<<<\n",
590 frame
= (struct db_ppc_frame
*)
591 (PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
->save_r1
);
592 callpc
= (db_addr_t
) PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
->save_srr0
;
593 linkpc
= (db_addr_t
) PerProcTable
[cpu
].ppe_vaddr
->db_saved_state
->save_lr
;
599 frame
= (struct db_ppc_frame
*)addr
;
600 th
= (db_default_act
)? db_default_act
: current_thread();
601 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
602 if (frame
->f_frame
) {
603 callpc
= (db_addr_t
)db_get_task_value
604 ((int)&frame
->f_frame
->f_retaddr
,
605 4, FALSE
, (user_frame
) ? task
: 0);
606 callpc
= callpc
-sizeof(callpc
);
612 if (!INKERNELSTACK((unsigned)frame
, th
)) {
613 db_printf(">>>>> user space <<<<<\n");
620 lastcallpc
= (db_addr_t
) 0;
621 while (frame_count
-- && frame
!= 0) {
622 int narg
= DB_NUMARGS_MAX
;
626 db_addr_t call_func
= 0;
630 db_symbol_values(NULL
,
631 db_search_task_symbol_and_line(
632 callpc
, DB_STGY_XTRN
, &offset
, &filename
,
633 &linenum
, (user_frame
) ? task
: 0, &narg
),
634 &name
, (db_expr_t
*)&call_func
);
636 db_find_task_sym_and_offset(callpc
,
637 &name
, &off
, (user_frame
) ? task
: 0);
638 offset
= (db_expr_t
) off
;
641 if (user_frame
== 0) {
643 (call_func
== db_user_trap_symbol_value
||
644 call_func
== db_kernel_trap_symbol_value
)) {
647 } else if (call_func
&&
648 call_func
== db_interrupt_symbol_value
) {
649 frame_type
= INTERRUPT
;
651 } else if (call_func
&&
652 call_func
== db_syscall_symbol_value
) {
653 frame_type
= SYSCALL
;
658 if ((r
= _setjmp(db_recover
= &db_jmp_buf
))
661 narg
= db_numargs(frame
,
662 (user_frame
) ? task
: 0);
672 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
674 narg
= db_numargs(frame
,
675 (user_frame
) ? task
: 0);
683 if (name
== 0 || offset
> db_maxoff
) {
684 db_printf("[%08X]0x%08X(", frame
, callpc
);
686 db_printf("[%08X]%s", frame
, name
);
688 db_printf("+%llx", offset
);
692 narg
= db_numargs(frame
, (user_frame
) ? task
: 0);
694 for (arg
=0; arg
< narg
; arg
++) {
700 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
703 found
= db_find_arg(frame
, lastframe
->f_retaddr
,
704 (user_frame
) ? task
: 0, arg
, &argp
);
706 value
= db_get_task_value(argp
, 4, FALSE
,
707 (user_frame
) ? task
: 0);
710 if (r
== 2) /* 'q' from db_more() */
712 db_printf("... <stack error>)");
718 db_printf("%08X", value
);
721 argp
= argp
+ sizeof(argp
);
733 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
734 db_nextframe(&lastframe
, &frame
, &callpc
, frame_type
,
735 (user_frame
) ? th
: THR_ACT_NULL
, linkpc
);
736 callpc
= callpc
-sizeof(callpc
);
749 if (!INKERNELSTACK(lastframe
, th
) ||
750 !INKERNELSTACK((unsigned)frame
, th
))
752 if (user_frame
== 1) {
753 db_printf(">>>>> user space <<<<<\n");
758 if (frame
<= lastframe
) {
759 if ((INKERNELSTACK(lastframe
, th
) && !INKERNELSTACK(frame
, th
))) continue;
760 db_printf("Bad frame pointer: 0x%x\n", frame
);
766 if (trace_all_threads
) {
767 if (top_act
!= THR_ACT_NULL
)
769 th
= (thread_act_t
) queue_next(&th
->task_threads
);
770 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
772 addr
= (db_expr_t
) th
;