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)))
64 #define INKERNELSTACK(va, th) 1
67 struct db_ppc_frame
*f_frame
;
73 uint32_t f_arg
[DB_NUMARGS_MAX
];
80 db_addr_t db_user_trap_symbol_value
= 0;
81 db_addr_t db_kernel_trap_symbol_value
= 0;
82 db_addr_t db_interrupt_symbol_value
= 0;
83 db_addr_t db_return_to_iret_symbol_value
= 0;
84 db_addr_t db_syscall_symbol_value
= 0;
85 boolean_t db_trace_symbols_found
= FALSE
;
87 extern int db_ppc_reg_value(
88 struct db_variable
* vp
,
91 db_var_aux_param_t ap
);
92 extern void db_find_trace_symbols(void);
93 extern int db_numargs(
94 struct db_ppc_frame
*fp
,
96 extern boolean_t
db_find_arg(
97 struct db_ppc_frame
*frame
,
102 extern void db_nextframe(
103 struct db_ppc_frame
**lfp
,
104 struct db_ppc_frame
**fp
,
107 thread_act_t thr_act
,
113 * Machine register set.
115 struct db_variable db_regs
[] = {
116 /* XXX "pc" is an alias to "srr0"... */
117 { "pc", &ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
118 { "srr0", &ddb_regs
.save_srr0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
119 { "srr1", &ddb_regs
.save_srr1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
120 { "r0", &ddb_regs
.save_r0
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
121 { "r1", &ddb_regs
.save_r1
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
122 { "r2", &ddb_regs
.save_r2
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
123 { "r3", &ddb_regs
.save_r3
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
124 { "r4", &ddb_regs
.save_r4
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
125 { "r5", &ddb_regs
.save_r5
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
126 { "r6", &ddb_regs
.save_r6
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
127 { "r7", &ddb_regs
.save_r7
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
128 { "r8", &ddb_regs
.save_r8
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
129 { "r9", &ddb_regs
.save_r9
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
130 { "r10", &ddb_regs
.save_r10
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
131 { "r11", &ddb_regs
.save_r11
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
132 { "r12", &ddb_regs
.save_r12
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
133 { "r13", &ddb_regs
.save_r13
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
134 { "r14", &ddb_regs
.save_r14
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
135 { "r15", &ddb_regs
.save_r15
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
136 { "r16", &ddb_regs
.save_r16
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
137 { "r17", &ddb_regs
.save_r17
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
138 { "r18", &ddb_regs
.save_r18
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
139 { "r19", &ddb_regs
.save_r19
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
140 { "r20", &ddb_regs
.save_r20
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
141 { "r21", &ddb_regs
.save_r21
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
142 { "r22", &ddb_regs
.save_r22
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
143 { "r23", &ddb_regs
.save_r23
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
144 { "r24", &ddb_regs
.save_r24
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
145 { "r25", &ddb_regs
.save_r25
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
146 { "r26", &ddb_regs
.save_r26
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
147 { "r27", &ddb_regs
.save_r27
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
148 { "r28", &ddb_regs
.save_r28
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
149 { "r29", &ddb_regs
.save_r29
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
150 { "r30", &ddb_regs
.save_r30
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
151 { "r31", &ddb_regs
.save_r31
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
152 { "cr", &ddb_regs
.save_cr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
153 { "xer", &ddb_regs
.save_xer
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
154 { "lr", &ddb_regs
.save_lr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
155 { "ctr", &ddb_regs
.save_ctr
, db_ppc_reg_value
, 0, 0, 0, 0, TRUE
},
157 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
161 struct db_variable
*vp
,
164 db_var_aux_param_t ap
)
167 db_expr_t null_reg
= 0;
170 register thread_act_t thr_act
= ap
->thr_act
;
173 if (db_option(ap
->modif
, 'u')) {
174 if (thr_act
== THR_ACT_NULL
) {
175 if ((thr_act
= current_act()) == THR_ACT_NULL
)
176 db_error("no user registers\n");
178 if (thr_act
== current_act()) {
179 if (IS_USER_TRAP((&ddb_regs
))) dp
= vp
->valuep
;
180 else if (INFIXEDSTACK(ddb_regs
.save_r1
))
181 db_error("cannot get/set user registers in nested interrupt\n");
185 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act()) {
189 if (thr_act
->thread
&&
190 !(thr_act
->thread
->state
& TH_STACK_HANDOFF
) &&
191 thr_act
->thread
->kernel_stack
) {
195 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
196 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
197 cpu_to_processor(cpu
)->cpu_data
->active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
199 dp
= (db_expr_t
)(((uint32_t)saved_state
[cpu
]) +
200 (((uint32_t) vp
->valuep
) -
201 (uint32_t) &ddb_regs
));
206 if (dp
== 0) dp
= &null_reg
;
208 else if (thr_act
->thread
&& (thr_act
->thread
->state
& TH_STACK_HANDOFF
)){
209 /* only PC is valid */
210 if (vp
->valuep
== (int *) &ddb_regs
.save_srr0
) {
211 dp
= (int *)(&thr_act
->thread
->continuation
);
221 if (!db_option(ap
->modif
, 'u')) {
222 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
223 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
224 cpu_to_processor(cpu
)->cpu_data
->active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
225 dp
= (int *) (((int)saved_state
[cpu
]) +
226 (((int) vp
->valuep
) - (int) &ddb_regs
));
232 if (!thr_act
|| thr_act
->mact
.pcb
== 0) db_error("no pcb\n");
233 dp
= (int *)((int)thr_act
->mact
.pcb
+ ((int)vp
->valuep
- (int)&ddb_regs
));
237 if(vp
->valuep
== (int *) &ddb_regs
.save_cr
) { /* Is this the CR we are doing? */
238 dp32
= (uint32_t *)dp
; /* Make this easier */
239 if (flag
== DB_VAR_SET
) *dp32
= *valuep
;
240 else *valuep
= *dp32
;
242 else { /* Normal 64-bit registers */
243 if (flag
== DB_VAR_SET
) *dp
= *valuep
;
244 else *valuep
= *(unsigned long long *)dp
;
252 db_find_trace_symbols(void)
255 boolean_t found_some
;
258 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
259 db_user_trap_symbol_value
= (db_addr_t
) value
;
262 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
263 db_kernel_trap_symbol_value
= (db_addr_t
) value
;
266 if (db_value_of_name(CC_SYM_PREFIX
"ihandler", &value
)) {
267 db_interrupt_symbol_value
= (db_addr_t
) value
;
271 if (db_value_of_name(CC_SYM_PREFIX
"return_to_iret", &value
)) {
272 db_return_to_iret_symbol_value
= (db_addr_t
) value
;
276 if (db_value_of_name(CC_SYM_PREFIX
"thandler", &value
)) {
277 db_syscall_symbol_value
= (db_addr_t
) value
;
281 db_trace_symbols_found
= TRUE
;
286 struct db_ppc_frame
*fp
,
289 return (DB_NUMARGS_MAX
);
294 struct db_ppc_frame
*fp
,
308 db_find_task_sym_and_offset(calleepc
, &name
, &offset
, task
);
309 calleep
= calleepc
-offset
;
311 for (i
= 0; calleep
< calleepc
; i
++, calleep
++) {
312 if (!DB_CHECK_ACCESS((int) calleep
, 4, task
)) {
315 inst
= db_get_task_value(calleep
, 4, FALSE
, task
);
316 if ((inst
& 0xffff0000) == (0x907f0000 + (narg
<< 21)) ||
317 (inst
& 0xffff0000) == (0x90610000 + (narg
<< 21))) {
318 argp
= (db_addr_t
) &(fp
->f_arg
[narg
]);
328 * Figure out the next frame up in the call stack.
329 * For trap(), we print the address of the faulting instruction and
330 * proceed with the calling frame. We return the ip that faulted.
331 * If the trap was caused by jumping through a bogus pointer, then
332 * the next line in the backtrace will list some random function as
333 * being called. It should get the argument list correct, though.
334 * It might be possible to dig out from the next frame up the name
335 * of the function that faulted, but that could get hairy.
339 struct db_ppc_frame
**lfp
, /* in/out */
340 struct db_ppc_frame
**fp
, /* in/out */
341 db_addr_t
*ip
, /* out */
342 int frame_type
, /* in */
343 thread_act_t thr_act
,
344 db_addr_t linkpc
) /* in */
346 extern char * trap_type
[];
347 extern int TRAP_TYPES
;
349 struct savearea
*saved_regs
;
351 task_t task
= (thr_act
!= THR_ACT_NULL
)? thr_act
->task
: TASK_NULL
;
356 db_printf(">>>>> trap <<<<<\n");
361 db_printf(">>>>> interrupt <<<<<\n");
364 db_printf(">>>>> interrupt <<<<<\n");
368 if (thr_act
!= THR_ACT_NULL
&& thr_act
->mact
.pcb
) {
369 *ip
= (db_addr_t
) thr_act
->mact
.pcb
->save_srr0
;
370 *fp
= (struct db_ppc_frame
*) (thr_act
->mact
.pcb
->save_r1
);
373 /* falling down for unknown case */
377 if(!pmap_find_phys(kernel_pmap
, (addr64_t
)*fp
)) { /* Check if this is valid */
378 db_printf("Frame not mapped %08X\n",*fp
); /* Say not found */
379 *fp
= 0; /* Show not found */
380 break; /* Out of here */
385 db_get_task_value((int)&(*fp
)->f_frame
->f_retaddr
,
389 db_get_task_value((int)&(*fp
)->f_retaddr
,
393 *fp
= (struct db_ppc_frame
*)
394 db_get_task_value((int)&(*fp
)->f_frame
, 4, FALSE
, task
);
406 struct db_ppc_frame
*frame
, *lastframe
;
407 db_addr_t callpc
, linkpc
, lastcallpc
;
409 boolean_t kernel_only
= TRUE
;
410 boolean_t trace_thread
= FALSE
;
411 boolean_t trace_all_threads
= FALSE
;
416 thread_act_t th
, top_act
;
420 jmp_buf_t db_jmp_buf
;
421 queue_entry_t act_list
;
423 if (!db_trace_symbols_found
)
424 db_find_trace_symbols();
426 register char *cp
= modif
;
429 while ((c
= *cp
++) != 0) {
433 trace_all_threads
= TRUE
;
441 if (trace_all_threads
) {
442 if (!have_addr
&& !trace_thread
) {
445 act_list
= &(current_task()->thr_acts
);
446 addr
= (db_expr_t
) queue_first(act_list
);
448 else if (trace_thread
) {
450 if (!db_check_act_address_valid((thread_act_t
)addr
)) {
451 if (db_lookup_task((task_t
)addr
) == -1)
453 act_list
= &(((task_t
)addr
)->thr_acts
);
454 addr
= (db_expr_t
) queue_first(act_list
);
457 act_list
= &(((thread_act_t
)addr
)->task
->thr_acts
);
458 thcount
= db_lookup_task_act(((thread_act_t
)addr
)->task
,
464 if (th
== THR_ACT_NULL
)
466 if (th
== THR_ACT_NULL
) {
467 db_printf("no active thr_act\n");
471 act_list
= &th
->task
->thr_acts
;
472 addr
= (db_expr_t
) queue_first(act_list
);
481 top_act
= THR_ACT_NULL
;
486 if (!have_addr
&& !trace_thread
) {
487 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
488 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
489 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
491 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
493 else if (trace_thread
) {
495 th
= (thread_act_t
) addr
;
496 if (!db_check_act_address_valid(th
))
501 if (th
== THR_ACT_NULL
)
503 if (th
== THR_ACT_NULL
) {
504 db_printf("no active thread\n");
508 if (trace_all_threads
)
509 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
510 addr
, thcount
, th
->task
->thr_act_count
);
517 if (th
== current_act()) {
518 frame
= (struct db_ppc_frame
*)(ddb_regs
.save_r1
);
519 callpc
= (db_addr_t
)ddb_regs
.save_srr0
;
520 linkpc
= (db_addr_t
)ddb_regs
.save_lr
;
523 if (th
->mact
.pcb
== 0) {
524 db_printf("thread has no pcb\n");
528 register struct savearea
*pss
=
531 db_printf("thread has no shuttle\n");
534 else if ((th
->thread
->state
& TH_STACK_HANDOFF
) ||
535 th
->thread
->kernel_stack
== 0) {
536 register struct savearea
*pss
=
539 db_printf("Continuation ");
540 db_task_printsym((db_expr_t
)th
->thread
->continuation
,
543 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
544 callpc
= (db_addr_t
) (pss
->save_srr0
);
545 linkpc
= (db_addr_t
) (pss
->save_lr
);
550 for (cpu
= 0; cpu
< NCPUS
; cpu
++) {
551 if (cpu_to_processor(cpu
)->state
== PROCESSOR_RUNNING
&&
552 cpu_to_processor(cpu
)->cpu_data
->active_thread
== th
->thread
&&
557 if (top_act
!= THR_ACT_NULL
) {
559 * Trying to get the backtrace of an activation
560 * which is not the top_most one in the RPC chain:
561 * use the activation's pcb.
563 struct savearea
*pss
;
566 frame
= (struct db_ppc_frame
*) (pss
->save_r1
);
567 callpc
= (db_addr_t
) (pss
->save_srr0
);
568 linkpc
= (db_addr_t
) (pss
->save_lr
);
571 register struct savearea
*iks
;
576 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
577 frame
= (struct db_ppc_frame
*) (iks
->save_r1
);
578 callpc
= (db_addr_t
) (iks
->save_lr
);
582 * The kernel stack has probably been
583 * paged out (swapped out activation).
586 if (r
== 2) /* 'q' from db_more() */
588 db_printf("<kernel stack (0x%x) error "
589 "(probably swapped out)>\n",
595 db_printf(">>>>> active on cpu %d <<<<<\n",
597 frame
= (struct db_ppc_frame
*)
598 (saved_state
[cpu
]->save_r1
);
599 callpc
= (db_addr_t
) saved_state
[cpu
]->save_srr0
;
600 linkpc
= (db_addr_t
) saved_state
[cpu
]->save_lr
;
606 frame
= (struct db_ppc_frame
*)addr
;
607 th
= (db_default_act
)? db_default_act
: current_act();
608 task
= (th
!= THR_ACT_NULL
)? th
->task
: TASK_NULL
;
609 if (frame
->f_frame
) {
610 callpc
= (db_addr_t
)db_get_task_value
611 ((int)&frame
->f_frame
->f_retaddr
,
612 4, FALSE
, (user_frame
) ? task
: 0);
613 callpc
= callpc
-sizeof(callpc
);
619 if (!INKERNELSTACK((unsigned)frame
, th
)) {
620 db_printf(">>>>> user space <<<<<\n");
627 lastcallpc
= (db_addr_t
) 0;
628 while (frame_count
-- && frame
!= 0) {
629 int narg
= DB_NUMARGS_MAX
;
633 db_addr_t call_func
= 0;
637 db_symbol_values(NULL
,
638 db_search_task_symbol_and_line(
639 callpc
, DB_STGY_XTRN
, &offset
, &filename
,
640 &linenum
, (user_frame
) ? task
: 0, &narg
),
641 &name
, (db_expr_t
*)&call_func
);
643 db_find_task_sym_and_offset(callpc
,
644 &name
, &off
, (user_frame
) ? task
: 0);
645 offset
= (db_expr_t
) off
;
648 if (user_frame
== 0) {
650 (call_func
== db_user_trap_symbol_value
||
651 call_func
== db_kernel_trap_symbol_value
)) {
654 } else if (call_func
&&
655 call_func
== db_interrupt_symbol_value
) {
656 frame_type
= INTERRUPT
;
658 } else if (call_func
&&
659 call_func
== db_syscall_symbol_value
) {
660 frame_type
= SYSCALL
;
665 if ((r
= _setjmp(db_recover
= &db_jmp_buf
))
668 narg
= db_numargs(frame
,
669 (user_frame
) ? task
: 0);
679 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
681 narg
= db_numargs(frame
,
682 (user_frame
) ? task
: 0);
690 if (name
== 0 || offset
> db_maxoff
) {
691 db_printf("[%08X]0x%08X(", frame
, callpc
);
693 db_printf("[%08X]%s", frame
, name
);
695 db_printf("+%x", offset
);
699 narg
= db_numargs(frame
, (user_frame
) ? task
: 0);
701 for (arg
=0; arg
< narg
; arg
++) {
707 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
710 found
= db_find_arg(frame
, lastframe
->f_retaddr
,
711 (user_frame
) ? task
: 0, arg
, &argp
);
713 value
= db_get_task_value(argp
, 4, FALSE
,
714 (user_frame
) ? task
: 0);
717 if (r
== 2) /* 'q' from db_more() */
719 db_printf("... <stack error>)");
725 db_printf("%08X", value
);
728 argp
= argp
+ sizeof(argp
);
740 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
741 db_nextframe(&lastframe
, &frame
, &callpc
, frame_type
,
742 (user_frame
) ? th
: THR_ACT_NULL
, linkpc
);
743 callpc
= callpc
-sizeof(callpc
);
753 if (th
->lower
!= THR_ACT_NULL
) {
754 if (top_act
== THR_ACT_NULL
)
757 db_printf(">>>>> next activation 0x%x ($task%d.%d) <<<<<\n",
759 db_lookup_task(th
->task
),
760 db_lookup_task_act(th
->task
, th
));
761 goto next_activation
;
766 if (!INKERNELSTACK(lastframe
, th
) ||
767 !INKERNELSTACK((unsigned)frame
, th
))
769 if (user_frame
== 1) {
770 db_printf(">>>>> user space <<<<<\n");
775 if (frame
<= lastframe
) {
776 if ((INKERNELSTACK(lastframe
, th
) && !INKERNELSTACK(frame
, th
))) continue;
777 db_printf("Bad frame pointer: 0x%x\n", frame
);
783 if (trace_all_threads
) {
784 if (top_act
!= THR_ACT_NULL
)
786 th
= (thread_act_t
) queue_next(&th
->thr_acts
);
787 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
789 addr
= (db_expr_t
) th
;