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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
61 #include <mach/boolean.h>
62 #include <vm/vm_map.h>
63 #include <kern/thread.h>
64 #include <kern/task.h>
66 #include <machine/asm.h>
67 #include <machine/db_machdep.h>
68 #include <machine/setjmp.h>
69 #include <mach/machine.h>
71 #include <ddb/db_access.h>
72 #include <ddb/db_sym.h>
73 #include <ddb/db_variables.h>
74 #include <ddb/db_command.h>
75 #include <ddb/db_task_thread.h>
76 #include <ddb/db_output.h>
78 extern jmp_buf_t
*db_recover
;
79 extern struct i386_saved_state
*saved_state
[];
81 struct i386_kernel_state ddb_null_kregs
;
87 extern vm_offset_t vm_min_inks_addr
; /* set by db_clone_symtabXXX */
88 #define INKSERVER(va) (((vm_offset_t)(va)) >= vm_min_inks_addr)
90 extern vm_offset_t interrupt_stack
[];
91 #define ININTSTACK(va) \
92 (((vm_offset_t)(va)) >= interrupt_stack[cpu_number()] &&\
93 (((vm_offset_t)(va)) < interrupt_stack[cpu_number()] + \
96 #define INKERNELSTACK(va, th) \
97 (th == THREAD_NULL || \
98 (((vm_offset_t)(va)) >= th->thread->kernel_stack && \
99 (((vm_offset_t)(va)) < th->thread->kernel_stack + \
100 KERNEL_STACK_SIZE)) || \
104 struct i386_frame
*f_frame
;
113 db_addr_t db_user_trap_symbol_value
= 0;
114 db_addr_t db_kernel_trap_symbol_value
= 0;
115 db_addr_t db_interrupt_symbol_value
= 0;
116 db_addr_t db_return_to_iret_symbol_value
= 0;
117 db_addr_t db_syscall_symbol_value
= 0;
118 boolean_t db_trace_symbols_found
= FALSE
;
124 { "ebx", (int)(&((struct i386_kernel_state
*)0)->k_ebx
) },
125 { "esp", (int)(&((struct i386_kernel_state
*)0)->k_esp
) },
126 { "ebp", (int)(&((struct i386_kernel_state
*)0)->k_ebp
) },
127 { "edi", (int)(&((struct i386_kernel_state
*)0)->k_edi
) },
128 { "esi", (int)(&((struct i386_kernel_state
*)0)->k_esi
) },
129 { "eip", (int)(&((struct i386_kernel_state
*)0)->k_eip
) },
135 extern int * db_lookup_i386_kreg(
138 extern int db_i386_reg_value(
139 struct db_variable
* vp
,
142 db_var_aux_param_t ap
);
143 extern void db_find_trace_symbols(void);
144 extern int db_numargs(
145 struct i386_frame
*fp
,
147 extern void db_nextframe(
148 struct i386_frame
**lfp
,
149 struct i386_frame
**fp
,
157 * Machine register set.
159 struct db_variable db_regs
[] = {
160 { "cs", (int *)&ddb_regs
.cs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
161 { "ds", (int *)&ddb_regs
.ds
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
162 { "es", (int *)&ddb_regs
.es
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
163 { "fs", (int *)&ddb_regs
.fs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
164 { "gs", (int *)&ddb_regs
.gs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
165 { "ss", (int *)&ddb_regs
.ss
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
166 { "eax",(int *)&ddb_regs
.eax
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
167 { "ecx",(int *)&ddb_regs
.ecx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
168 { "edx",(int *)&ddb_regs
.edx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
169 { "ebx",(int *)&ddb_regs
.ebx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
170 { "esp",(int *)&ddb_regs
.uesp
,db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
171 { "ebp",(int *)&ddb_regs
.ebp
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
172 { "esi",(int *)&ddb_regs
.esi
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
173 { "edi",(int *)&ddb_regs
.edi
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
174 { "eip",(int *)&ddb_regs
.eip
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
175 { "efl",(int *)&ddb_regs
.efl
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
177 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
184 register struct i386_kregs
*kp
;
186 for (kp
= i386_kregs
; kp
->name
; kp
++) {
187 if (strcmp(name
, kp
->name
) == 0)
188 return((int *)((int)kregp
+ kp
->offset
));
195 struct db_variable
*vp
,
198 db_var_aux_param_t ap
)
202 db_expr_t null_reg
= 0;
203 register thread_t thr_act
= ap
->thr_act
;
204 extern unsigned int_stack_high
;
207 if (db_option(ap
->modif
, 'u')) {
208 if (thr_act
== THREAD_NULL
) {
209 if ((thr_act
= current_thread()) == THREAD_NULL
)
210 db_error("no user registers\n");
212 if (thr_act
== current_thread()) {
213 if (IS_USER_TRAP(&ddb_regs
, &etext
))
215 else if (ddb_regs
.ebp
< int_stack_high
)
216 db_error("cannot get/set user registers in nested interrupt\n");
219 if (thr_act
== THREAD_NULL
|| thr_act
== current_thread()) {
222 if (thr_act
->thread
&&
223 !(thr_act
->thread
->state
& TH_STACK_HANDOFF
) &&
224 thr_act
->thread
->kernel_stack
) {
227 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
228 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
229 cpu_datap(cpu
)->cpu_active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
230 dp
= (int *) (((int)saved_state
[cpu
]) +
231 (((int) vp
->valuep
) -
236 if (dp
== 0 && thr_act
&& thr_act
->thread
)
237 dp
= db_lookup_i386_kreg(vp
->name
,
238 (int *)(STACK_IKS(thr_act
->thread
->kernel_stack
)));
241 } else if (thr_act
->thread
&&
242 (thr_act
->thread
->state
&TH_STACK_HANDOFF
)){
243 /* only EIP is valid */
244 if (vp
->valuep
== (int *) &ddb_regs
.eip
) {
245 dp
= (int *)(&thr_act
->thread
->continuation
);
255 if (!db_option(ap
->modif
, 'u')) {
256 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
257 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
258 cpu_datap(cpu
)->cpu_active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
259 dp
= (int *) (((int)saved_state
[cpu
]) +
260 (((int) vp
->valuep
) -
267 if (!thr_act
|| thr_act
->machine
.pcb
== 0)
268 db_error("no pcb\n");
269 dp
= (int *)((int)(&thr_act
->machine
.pcb
->iss
) +
270 ((int)vp
->valuep
- (int)&ddb_regs
));
273 if (flag
== DB_VAR_SET
)
281 db_find_trace_symbols(void)
284 boolean_t found_some
;
287 if (db_value_of_name(CC_SYM_PREFIX
"user_trap", &value
)) {
288 db_user_trap_symbol_value
= (db_addr_t
) value
;
291 if (db_value_of_name(CC_SYM_PREFIX
"kernel_trap", &value
)) {
292 db_kernel_trap_symbol_value
= (db_addr_t
) value
;
295 if (db_value_of_name(CC_SYM_PREFIX
"interrupt", &value
)) {
296 db_interrupt_symbol_value
= (db_addr_t
) value
;
299 if (db_value_of_name(CC_SYM_PREFIX
"return_to_iret", &value
)) {
300 db_return_to_iret_symbol_value
= (db_addr_t
) value
;
303 if (db_value_of_name(CC_SYM_PREFIX
"syscall", &value
)) {
304 db_syscall_symbol_value
= (db_addr_t
) value
;
308 db_trace_symbols_found
= TRUE
;
312 * Figure out how many arguments were passed into the frame at "fp".
314 int db_numargs_default
= 5;
318 struct i386_frame
*fp
,
326 argp
= (int *)db_get_task_value((int)&fp
->f_retaddr
, 4, FALSE
, task
);
327 if (argp
< (int *)VM_MIN_KERNEL_ADDRESS
|| (char *)argp
> &etext
)
328 args
= db_numargs_default
;
329 else if (!DB_CHECK_ACCESS((int)argp
, 4, task
))
330 args
= db_numargs_default
;
332 inst
= db_get_task_value((int)argp
, 4, FALSE
, task
);
333 if ((inst
& 0xff) == 0x59) /* popl %ecx */
335 else if ((inst
& 0xffff) == 0xc483) /* addl %n, %esp */
336 args
= ((inst
>> 16) & 0xff) / 4;
338 args
= db_numargs_default
;
343 struct interrupt_frame
{
344 struct i386_frame
*if_frame
; /* point to next frame */
345 int if_retaddr
; /* return address to _interrupt */
346 int if_unit
; /* unit number */
347 int if_spl
; /* saved spl */
348 int if_iretaddr
; /* _return_to_{iret,iret_i} */
349 int if_edx
; /* old sp(iret) or saved edx(iret_i) */
350 int if_ecx
; /* saved ecx(iret_i) */
351 int if_eax
; /* saved eax(iret_i) */
352 int if_eip
; /* saved eip(iret_i) */
353 int if_cs
; /* saved cs(iret_i) */
354 int if_efl
; /* saved efl(iret_i) */
358 * Figure out the next frame up in the call stack.
359 * For trap(), we print the address of the faulting instruction and
360 * proceed with the calling frame. We return the ip that faulted.
361 * If the trap was caused by jumping through a bogus pointer, then
362 * the next line in the backtrace will list some random function as
363 * being called. It should get the argument list correct, though.
364 * It might be possible to dig out from the next frame up the name
365 * of the function that faulted, but that could get hairy.
369 struct i386_frame
**lfp
, /* in/out */
370 struct i386_frame
**fp
, /* in/out */
371 db_addr_t
*ip
, /* out */
372 int frame_type
, /* in */
373 thread_t thr_act
) /* in */
375 extern char * trap_type
[];
376 extern int TRAP_TYPES
;
378 struct i386_saved_state
*saved_regs
;
379 struct interrupt_frame
*ifp
;
380 struct i386_interrupt_state
*isp
;
381 task_t task
= (thr_act
!= THREAD_NULL
)? thr_act
->task
: TASK_NULL
;
386 * We know that trap() has 1 argument and we know that
387 * it is an (strcut i386_saved_state *).
389 saved_regs
= (struct i386_saved_state
*)
390 db_get_task_value((int)&((*fp
)->f_arg0
),4,FALSE
,task
);
391 if (saved_regs
->trapno
>= 0 && saved_regs
->trapno
< TRAP_TYPES
) {
392 db_printf(">>>>> %s trap at ",
393 trap_type
[saved_regs
->trapno
]);
395 db_printf(">>>>> trap (number %d) at ",
396 saved_regs
->trapno
& 0xffff);
398 db_task_printsym(saved_regs
->eip
, DB_STGY_PROC
, task
);
399 db_printf(" <<<<<\n");
400 *fp
= (struct i386_frame
*)saved_regs
->ebp
;
401 *ip
= (db_addr_t
)saved_regs
->eip
;
405 db_printf(">>>>> interrupt <<<<<\n");
408 db_printf(">>>>> interrupt at ");
409 ifp
= (struct interrupt_frame
*)(*lfp
);
411 if (ifp
->if_iretaddr
== db_return_to_iret_symbol_value
)
412 *ip
= ((struct i386_interrupt_state
*) ifp
->if_edx
)->eip
;
414 *ip
= (db_addr_t
) ifp
->if_eip
;
415 db_task_printsym(*ip
, DB_STGY_PROC
, task
);
416 db_printf(" <<<<<\n");
419 if (thr_act
!= THREAD_NULL
&& thr_act
->machine
.pcb
) {
420 *ip
= (db_addr_t
) thr_act
->machine
.pcb
->iss
.eip
;
421 *fp
= (struct i386_frame
*) thr_act
->machine
.pcb
->iss
.ebp
;
424 /* falling down for unknown case */
428 db_get_task_value((int)&(*fp
)->f_retaddr
, 4, FALSE
, task
);
430 *fp
= (struct i386_frame
*)
431 db_get_task_value((int)&(*fp
)->f_frame
, 4, FALSE
, task
);
443 struct i386_frame
*frame
, *lastframe
;
445 db_addr_t callpc
, lastcallpc
;
447 boolean_t kernel_only
= TRUE
;
448 boolean_t trace_thread
= FALSE
;
449 boolean_t trace_all_threads
= FALSE
;
454 thread_t th
, top_act
;
458 jmp_buf_t db_jmp_buf
;
459 queue_entry_t act_list
;
461 if (!db_trace_symbols_found
)
462 db_find_trace_symbols();
465 register char *cp
= modif
;
468 while ((c
= *cp
++) != 0) {
472 trace_all_threads
= TRUE
;
480 if (trace_all_threads
) {
481 if (!have_addr
&& !trace_thread
) {
484 act_list
= &(current_task()->thr_acts
);
485 addr
= (db_expr_t
) queue_first(act_list
);
486 } else if (trace_thread
) {
488 if (!db_check_act_address_valid((thread_t
)addr
)) {
489 if (db_lookup_task((task_t
)addr
) == -1)
491 act_list
= &(((task_t
)addr
)->thr_acts
);
492 addr
= (db_expr_t
) queue_first(act_list
);
494 act_list
= &(((thread_t
)addr
)->task
->thr_acts
);
495 thcount
= db_lookup_task_act(((thread_t
)addr
)->task
,
500 if (th
== THREAD_NULL
)
501 th
= current_thread();
502 if (th
== THREAD_NULL
) {
503 db_printf("no active thr_act\n");
507 act_list
= &th
->task
->thr_acts
;
508 addr
= (db_expr_t
) queue_first(act_list
);
517 top_act
= THREAD_NULL
;
522 if (!have_addr
&& !trace_thread
) {
523 frame
= (struct i386_frame
*)ddb_regs
.ebp
;
524 callpc
= (db_addr_t
)ddb_regs
.eip
;
525 th
= current_thread();
526 task
= (th
!= THREAD_NULL
)? th
->task
: TASK_NULL
;
527 } else if (trace_thread
) {
529 th
= (thread_t
) addr
;
530 if (!db_check_act_address_valid(th
))
534 if (th
== THREAD_NULL
)
535 th
= current_thread();
536 if (th
== THREAD_NULL
) {
537 db_printf("no active thread\n");
541 if (trace_all_threads
)
542 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
543 addr
, thcount
, th
->task
->thr_act_count
);
549 if (th
== current_thread()) {
550 frame
= (struct i386_frame
*)ddb_regs
.ebp
;
551 callpc
= (db_addr_t
)ddb_regs
.eip
;
553 if (th
->machine
.pcb
== 0) {
554 db_printf("thread has no pcb\n");
558 register struct i386_saved_state
*iss
=
559 &th
->machine
.pcb
->iss
;
561 db_printf("thread has no shuttle\n");
563 frame
= (struct i386_frame
*) (iss
->ebp
);
564 callpc
= (db_addr_t
) (iss
->eip
);
569 else if ((th
->thread
->state
& TH_STACK_HANDOFF
) ||
570 th
->thread
->kernel_stack
== 0) {
571 register struct i386_saved_state
*iss
=
572 &th
->machine
.pcb
->iss
;
574 db_printf("Continuation ");
575 db_task_printsym((db_expr_t
)th
->thread
->continuation
,
578 frame
= (struct i386_frame
*) (iss
->ebp
);
579 callpc
= (db_addr_t
) (iss
->eip
);
583 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
584 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
585 cpu_datap(cpu
)->cpu_active_thread
== th
->thread
&&
590 if (top_act
!= THREAD_NULL
) {
592 * Trying to get the backtrace of an activation
593 * which is not the top_most one in the RPC chain:
594 * use the activation's pcb.
596 register struct i386_saved_state
*iss
=
597 &th
->machine
.pcb
->iss
;
598 frame
= (struct i386_frame
*) (iss
->ebp
);
599 callpc
= (db_addr_t
) (iss
->eip
);
602 register struct i386_kernel_state
*iks
;
605 iks
= STACK_IKS(th
->thread
->kernel_stack
);
607 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
608 frame
= (struct i386_frame
*) (iks
->k_ebp
);
609 callpc
= (db_addr_t
) (iks
->k_eip
);
612 * The kernel stack has probably been
613 * paged out (swapped out activation).
616 if (r
== 2) /* 'q' from db_more() */
618 db_printf("<kernel stack (0x%x) error "
619 "(probably swapped out)>\n",
625 db_printf(">>>>> active on cpu %d <<<<<\n",
627 frame
= (struct i386_frame
*)
628 saved_state
[cpu
]->ebp
;
629 callpc
= (db_addr_t
) saved_state
[cpu
]->eip
;
635 frame
= (struct i386_frame
*)addr
;
636 th
= (db_default_act
)? db_default_act
: current_thread();
637 task
= (th
!= THREAD_NULL
)? th
->task
: TASK_NULL
;
638 callpc
= (db_addr_t
)db_get_task_value((int)&frame
->f_retaddr
,
641 (user_frame
) ? task
: 0);
644 if (!INKERNELSTACK((unsigned)frame
, th
)) {
645 db_printf(">>>>> user space <<<<<\n");
649 } else if (INKSERVER(callpc
) && INKSERVER(frame
)) {
650 db_printf(">>>>> INKserver space <<<<<\n");
654 lastcallpc
= (db_addr_t
) 0;
655 while (frame_count
-- && frame
!= 0) {
659 db_addr_t call_func
= 0;
662 db_symbol_values(NULL
,
663 db_search_task_symbol_and_line(
669 (user_frame
) ? task
: 0,
671 &name
, (db_expr_t
*)&call_func
);
672 if (user_frame
== 0) {
673 if (call_func
== db_user_trap_symbol_value
||
674 call_func
== db_kernel_trap_symbol_value
) {
677 } else if (call_func
== db_interrupt_symbol_value
) {
678 frame_type
= INTERRUPT
;
680 } else if (call_func
== db_syscall_symbol_value
) {
681 frame_type
= SYSCALL
;
686 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
688 narg
= db_numargs(frame
,
689 (user_frame
) ? task
: 0);
699 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
701 narg
= db_numargs(frame
,
702 (user_frame
) ? task
: 0);
710 if (name
== 0 || offset
> db_maxoff
) {
711 db_printf("0x%x 0x%x(", frame
, callpc
);
714 db_printf("0x%x %s(", frame
, name
);
716 argp
= &frame
->f_arg0
;
721 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
722 value
= db_get_task_value((int)argp
,
725 (user_frame
) ? task
: 0);
728 if (r
== 2) /* 'q' from db_more() */
730 db_printf("... <stack error>)");
732 db_printf("+%x", offset
);
734 db_printf(" [%s", filename
);
736 db_printf(":%d", linenum
);
743 db_printf("%x", value
);
752 db_printf("+%x", offset
);
755 db_printf(" [%s", filename
);
757 db_printf(":%d", linenum
);
764 db_nextframe(&lastframe
, &frame
, &callpc
, frame_type
,
765 (user_frame
) ? th
: THREAD_NULL
);
768 if (th
->lower
!= THREAD_NULL
) {
769 if (top_act
== THREAD_NULL
)
772 db_printf(">>>>> next activation 0x%x ($task%d.%d) <<<<<\n",
774 db_lookup_task(th
->task
),
775 db_lookup_task_act(th
->task
, th
));
776 goto next_activation
;
781 if (!INKERNELSTACK(lastframe
, th
) ||
782 !INKERNELSTACK((unsigned)frame
, th
))
784 if (user_frame
== 1) {
785 db_printf(">>>>> user space <<<<<\n");
788 } else if ((!INKSERVER(lastframe
) || !INKSERVER(lastcallpc
)) &&
789 (INKSERVER(callpc
) && INKSERVER(frame
))) {
790 db_printf(">>>>> inkserver space <<<<<\n");
792 if (frame
<= lastframe
) {
793 if ((INKERNELSTACK(lastframe
, th
) &&
794 !INKERNELSTACK(frame
, th
)) ||
795 (INKSERVER(lastframe
) ^ INKSERVER(frame
)))
797 db_printf("Bad frame pointer: 0x%x\n", frame
);
803 if (trace_all_threads
) {
804 if (top_act
!= THREAD_NULL
)
806 th
= (thread_t
) queue_next(&th
->thr_acts
);
807 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
809 addr
= (db_expr_t
) th
;