2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
63 #include <mach/boolean.h>
64 #include <vm/vm_map.h>
65 #include <kern/thread.h>
66 #include <kern/task.h>
68 #include <machine/asm.h>
69 #include <machine/db_machdep.h>
70 #include <machine/setjmp.h>
71 #include <mach/machine.h>
73 #include <ddb/db_access.h>
74 #include <ddb/db_sym.h>
75 #include <ddb/db_variables.h>
76 #include <ddb/db_command.h>
77 #include <ddb/db_task_thread.h>
78 #include <ddb/db_output.h>
80 extern jmp_buf_t
*db_recover
;
81 extern struct i386_saved_state
*saved_state
[];
83 struct i386_kernel_state ddb_null_kregs
;
89 extern vm_offset_t vm_min_inks_addr
; /* set by db_clone_symtabXXX */
90 #define INKSERVER(va) (((vm_offset_t)(va)) >= vm_min_inks_addr)
92 extern vm_offset_t interrupt_stack
[];
93 #define ININTSTACK(va) \
94 (((vm_offset_t)(va)) >= interrupt_stack[cpu_number()] &&\
95 (((vm_offset_t)(va)) < interrupt_stack[cpu_number()] + \
98 #define INKERNELSTACK(va, th) \
99 (th == THREAD_NULL || \
100 (((vm_offset_t)(va)) >= th->thread->kernel_stack && \
101 (((vm_offset_t)(va)) < th->thread->kernel_stack + \
102 KERNEL_STACK_SIZE)) || \
106 struct i386_frame
*f_frame
;
115 db_addr_t db_user_trap_symbol_value
= 0;
116 db_addr_t db_kernel_trap_symbol_value
= 0;
117 db_addr_t db_interrupt_symbol_value
= 0;
118 db_addr_t db_return_to_iret_symbol_value
= 0;
119 db_addr_t db_syscall_symbol_value
= 0;
120 boolean_t db_trace_symbols_found
= FALSE
;
126 { "ebx", (int)(&((struct i386_kernel_state
*)0)->k_ebx
) },
127 { "esp", (int)(&((struct i386_kernel_state
*)0)->k_esp
) },
128 { "ebp", (int)(&((struct i386_kernel_state
*)0)->k_ebp
) },
129 { "edi", (int)(&((struct i386_kernel_state
*)0)->k_edi
) },
130 { "esi", (int)(&((struct i386_kernel_state
*)0)->k_esi
) },
131 { "eip", (int)(&((struct i386_kernel_state
*)0)->k_eip
) },
137 extern int * db_lookup_i386_kreg(
140 extern int db_i386_reg_value(
141 struct db_variable
* vp
,
144 db_var_aux_param_t ap
);
145 extern void db_find_trace_symbols(void);
146 extern int db_numargs(
147 struct i386_frame
*fp
,
149 extern void db_nextframe(
150 struct i386_frame
**lfp
,
151 struct i386_frame
**fp
,
159 * Machine register set.
161 struct db_variable db_regs
[] = {
162 { "cs", (int *)&ddb_regs
.cs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
163 { "ds", (int *)&ddb_regs
.ds
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
164 { "es", (int *)&ddb_regs
.es
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
165 { "fs", (int *)&ddb_regs
.fs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
166 { "gs", (int *)&ddb_regs
.gs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
167 { "ss", (int *)&ddb_regs
.ss
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
168 { "eax",(int *)&ddb_regs
.eax
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
169 { "ecx",(int *)&ddb_regs
.ecx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
170 { "edx",(int *)&ddb_regs
.edx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
171 { "ebx",(int *)&ddb_regs
.ebx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
172 { "esp",(int *)&ddb_regs
.uesp
,db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
173 { "ebp",(int *)&ddb_regs
.ebp
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
174 { "esi",(int *)&ddb_regs
.esi
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
175 { "edi",(int *)&ddb_regs
.edi
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
176 { "eip",(int *)&ddb_regs
.eip
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
177 { "efl",(int *)&ddb_regs
.efl
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
179 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
186 register struct i386_kregs
*kp
;
188 for (kp
= i386_kregs
; kp
->name
; kp
++) {
189 if (strcmp(name
, kp
->name
) == 0)
190 return((int *)((int)kregp
+ kp
->offset
));
197 struct db_variable
*vp
,
200 db_var_aux_param_t ap
)
204 db_expr_t null_reg
= 0;
205 register thread_t thr_act
= ap
->thr_act
;
206 extern unsigned int_stack_high
;
209 if (db_option(ap
->modif
, 'u')) {
210 if (thr_act
== THREAD_NULL
) {
211 if ((thr_act
= current_thread()) == THREAD_NULL
)
212 db_error("no user registers\n");
214 if (thr_act
== current_thread()) {
215 if (IS_USER_TRAP(&ddb_regs
, &etext
))
217 else if (ddb_regs
.ebp
< int_stack_high
)
218 db_error("cannot get/set user registers in nested interrupt\n");
221 if (thr_act
== THREAD_NULL
|| thr_act
== current_thread()) {
224 if (thr_act
->thread
&&
225 !(thr_act
->thread
->state
& TH_STACK_HANDOFF
) &&
226 thr_act
->thread
->kernel_stack
) {
229 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
230 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
231 cpu_datap(cpu
)->cpu_active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
232 dp
= (int *) (((int)saved_state
[cpu
]) +
233 (((int) vp
->valuep
) -
238 if (dp
== 0 && thr_act
&& thr_act
->thread
)
239 dp
= db_lookup_i386_kreg(vp
->name
,
240 (int *)(STACK_IKS(thr_act
->thread
->kernel_stack
)));
243 } else if (thr_act
->thread
&&
244 (thr_act
->thread
->state
&TH_STACK_HANDOFF
)){
245 /* only EIP is valid */
246 if (vp
->valuep
== (int *) &ddb_regs
.eip
) {
247 dp
= (int *)(&thr_act
->thread
->continuation
);
257 if (!db_option(ap
->modif
, 'u')) {
258 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
259 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
260 cpu_datap(cpu
)->cpu_active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
261 dp
= (int *) (((int)saved_state
[cpu
]) +
262 (((int) vp
->valuep
) -
269 if (!thr_act
|| thr_act
->machine
.pcb
== 0)
270 db_error("no pcb\n");
271 dp
= (int *)((int)(&thr_act
->machine
.pcb
->iss
) +
272 ((int)vp
->valuep
- (int)&ddb_regs
));
275 if (flag
== DB_VAR_SET
)
283 db_find_trace_symbols(void)
286 boolean_t found_some
;
289 if (db_value_of_name(CC_SYM_PREFIX
"user_trap", &value
)) {
290 db_user_trap_symbol_value
= (db_addr_t
) value
;
293 if (db_value_of_name(CC_SYM_PREFIX
"kernel_trap", &value
)) {
294 db_kernel_trap_symbol_value
= (db_addr_t
) value
;
297 if (db_value_of_name(CC_SYM_PREFIX
"interrupt", &value
)) {
298 db_interrupt_symbol_value
= (db_addr_t
) value
;
301 if (db_value_of_name(CC_SYM_PREFIX
"return_to_iret", &value
)) {
302 db_return_to_iret_symbol_value
= (db_addr_t
) value
;
305 if (db_value_of_name(CC_SYM_PREFIX
"syscall", &value
)) {
306 db_syscall_symbol_value
= (db_addr_t
) value
;
310 db_trace_symbols_found
= TRUE
;
314 * Figure out how many arguments were passed into the frame at "fp".
316 int db_numargs_default
= 5;
320 struct i386_frame
*fp
,
328 argp
= (int *)db_get_task_value((int)&fp
->f_retaddr
, 4, FALSE
, task
);
329 if (argp
< (int *)VM_MIN_KERNEL_ADDRESS
|| (char *)argp
> &etext
)
330 args
= db_numargs_default
;
331 else if (!DB_CHECK_ACCESS((int)argp
, 4, task
))
332 args
= db_numargs_default
;
334 inst
= db_get_task_value((int)argp
, 4, FALSE
, task
);
335 if ((inst
& 0xff) == 0x59) /* popl %ecx */
337 else if ((inst
& 0xffff) == 0xc483) /* addl %n, %esp */
338 args
= ((inst
>> 16) & 0xff) / 4;
340 args
= db_numargs_default
;
345 struct interrupt_frame
{
346 struct i386_frame
*if_frame
; /* point to next frame */
347 int if_retaddr
; /* return address to _interrupt */
348 int if_unit
; /* unit number */
349 int if_spl
; /* saved spl */
350 int if_iretaddr
; /* _return_to_{iret,iret_i} */
351 int if_edx
; /* old sp(iret) or saved edx(iret_i) */
352 int if_ecx
; /* saved ecx(iret_i) */
353 int if_eax
; /* saved eax(iret_i) */
354 int if_eip
; /* saved eip(iret_i) */
355 int if_cs
; /* saved cs(iret_i) */
356 int if_efl
; /* saved efl(iret_i) */
360 * Figure out the next frame up in the call stack.
361 * For trap(), we print the address of the faulting instruction and
362 * proceed with the calling frame. We return the ip that faulted.
363 * If the trap was caused by jumping through a bogus pointer, then
364 * the next line in the backtrace will list some random function as
365 * being called. It should get the argument list correct, though.
366 * It might be possible to dig out from the next frame up the name
367 * of the function that faulted, but that could get hairy.
371 struct i386_frame
**lfp
, /* in/out */
372 struct i386_frame
**fp
, /* in/out */
373 db_addr_t
*ip
, /* out */
374 int frame_type
, /* in */
375 thread_t thr_act
) /* in */
377 extern char * trap_type
[];
378 extern int TRAP_TYPES
;
380 struct i386_saved_state
*saved_regs
;
381 struct interrupt_frame
*ifp
;
382 struct i386_interrupt_state
*isp
;
383 task_t task
= (thr_act
!= THREAD_NULL
)? thr_act
->task
: TASK_NULL
;
388 * We know that trap() has 1 argument and we know that
389 * it is an (strcut i386_saved_state *).
391 saved_regs
= (struct i386_saved_state
*)
392 db_get_task_value((int)&((*fp
)->f_arg0
),4,FALSE
,task
);
393 if (saved_regs
->trapno
>= 0 && saved_regs
->trapno
< TRAP_TYPES
) {
394 db_printf(">>>>> %s trap at ",
395 trap_type
[saved_regs
->trapno
]);
397 db_printf(">>>>> trap (number %d) at ",
398 saved_regs
->trapno
& 0xffff);
400 db_task_printsym(saved_regs
->eip
, DB_STGY_PROC
, task
);
401 db_printf(" <<<<<\n");
402 *fp
= (struct i386_frame
*)saved_regs
->ebp
;
403 *ip
= (db_addr_t
)saved_regs
->eip
;
407 db_printf(">>>>> interrupt <<<<<\n");
410 db_printf(">>>>> interrupt at ");
411 ifp
= (struct interrupt_frame
*)(*lfp
);
413 if (ifp
->if_iretaddr
== db_return_to_iret_symbol_value
)
414 *ip
= ((struct i386_interrupt_state
*) ifp
->if_edx
)->eip
;
416 *ip
= (db_addr_t
) ifp
->if_eip
;
417 db_task_printsym(*ip
, DB_STGY_PROC
, task
);
418 db_printf(" <<<<<\n");
421 if (thr_act
!= THREAD_NULL
&& thr_act
->machine
.pcb
) {
422 *ip
= (db_addr_t
) thr_act
->machine
.pcb
->iss
.eip
;
423 *fp
= (struct i386_frame
*) thr_act
->machine
.pcb
->iss
.ebp
;
426 /* falling down for unknown case */
430 db_get_task_value((int)&(*fp
)->f_retaddr
, 4, FALSE
, task
);
432 *fp
= (struct i386_frame
*)
433 db_get_task_value((int)&(*fp
)->f_frame
, 4, FALSE
, task
);
445 struct i386_frame
*frame
, *lastframe
;
447 db_addr_t callpc
, lastcallpc
;
449 boolean_t kernel_only
= TRUE
;
450 boolean_t trace_thread
= FALSE
;
451 boolean_t trace_all_threads
= FALSE
;
456 thread_t th
, top_act
;
460 jmp_buf_t db_jmp_buf
;
461 queue_entry_t act_list
;
463 if (!db_trace_symbols_found
)
464 db_find_trace_symbols();
467 register char *cp
= modif
;
470 while ((c
= *cp
++) != 0) {
474 trace_all_threads
= TRUE
;
482 if (trace_all_threads
) {
483 if (!have_addr
&& !trace_thread
) {
486 act_list
= &(current_task()->thr_acts
);
487 addr
= (db_expr_t
) queue_first(act_list
);
488 } else if (trace_thread
) {
490 if (!db_check_act_address_valid((thread_t
)addr
)) {
491 if (db_lookup_task((task_t
)addr
) == -1)
493 act_list
= &(((task_t
)addr
)->thr_acts
);
494 addr
= (db_expr_t
) queue_first(act_list
);
496 act_list
= &(((thread_t
)addr
)->task
->thr_acts
);
497 thcount
= db_lookup_task_act(((thread_t
)addr
)->task
,
502 if (th
== THREAD_NULL
)
503 th
= current_thread();
504 if (th
== THREAD_NULL
) {
505 db_printf("no active thr_act\n");
509 act_list
= &th
->task
->thr_acts
;
510 addr
= (db_expr_t
) queue_first(act_list
);
519 top_act
= THREAD_NULL
;
524 if (!have_addr
&& !trace_thread
) {
525 frame
= (struct i386_frame
*)ddb_regs
.ebp
;
526 callpc
= (db_addr_t
)ddb_regs
.eip
;
527 th
= current_thread();
528 task
= (th
!= THREAD_NULL
)? th
->task
: TASK_NULL
;
529 } else if (trace_thread
) {
531 th
= (thread_t
) addr
;
532 if (!db_check_act_address_valid(th
))
536 if (th
== THREAD_NULL
)
537 th
= current_thread();
538 if (th
== THREAD_NULL
) {
539 db_printf("no active thread\n");
543 if (trace_all_threads
)
544 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
545 addr
, thcount
, th
->task
->thr_act_count
);
551 if (th
== current_thread()) {
552 frame
= (struct i386_frame
*)ddb_regs
.ebp
;
553 callpc
= (db_addr_t
)ddb_regs
.eip
;
555 if (th
->machine
.pcb
== 0) {
556 db_printf("thread has no pcb\n");
560 register struct i386_saved_state
*iss
=
561 &th
->machine
.pcb
->iss
;
563 db_printf("thread has no shuttle\n");
565 frame
= (struct i386_frame
*) (iss
->ebp
);
566 callpc
= (db_addr_t
) (iss
->eip
);
571 else if ((th
->thread
->state
& TH_STACK_HANDOFF
) ||
572 th
->thread
->kernel_stack
== 0) {
573 register struct i386_saved_state
*iss
=
574 &th
->machine
.pcb
->iss
;
576 db_printf("Continuation ");
577 db_task_printsym((db_expr_t
)th
->thread
->continuation
,
580 frame
= (struct i386_frame
*) (iss
->ebp
);
581 callpc
= (db_addr_t
) (iss
->eip
);
585 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
586 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
587 cpu_datap(cpu
)->cpu_active_thread
== th
->thread
&&
592 if (top_act
!= THREAD_NULL
) {
594 * Trying to get the backtrace of an activation
595 * which is not the top_most one in the RPC chain:
596 * use the activation's pcb.
598 register struct i386_saved_state
*iss
=
599 &th
->machine
.pcb
->iss
;
600 frame
= (struct i386_frame
*) (iss
->ebp
);
601 callpc
= (db_addr_t
) (iss
->eip
);
604 register struct i386_kernel_state
*iks
;
607 iks
= STACK_IKS(th
->thread
->kernel_stack
);
609 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
610 frame
= (struct i386_frame
*) (iks
->k_ebp
);
611 callpc
= (db_addr_t
) (iks
->k_eip
);
614 * The kernel stack has probably been
615 * paged out (swapped out activation).
618 if (r
== 2) /* 'q' from db_more() */
620 db_printf("<kernel stack (0x%x) error "
621 "(probably swapped out)>\n",
627 db_printf(">>>>> active on cpu %d <<<<<\n",
629 frame
= (struct i386_frame
*)
630 saved_state
[cpu
]->ebp
;
631 callpc
= (db_addr_t
) saved_state
[cpu
]->eip
;
637 frame
= (struct i386_frame
*)addr
;
638 th
= (db_default_act
)? db_default_act
: current_thread();
639 task
= (th
!= THREAD_NULL
)? th
->task
: TASK_NULL
;
640 callpc
= (db_addr_t
)db_get_task_value((int)&frame
->f_retaddr
,
643 (user_frame
) ? task
: 0);
646 if (!INKERNELSTACK((unsigned)frame
, th
)) {
647 db_printf(">>>>> user space <<<<<\n");
651 } else if (INKSERVER(callpc
) && INKSERVER(frame
)) {
652 db_printf(">>>>> INKserver space <<<<<\n");
656 lastcallpc
= (db_addr_t
) 0;
657 while (frame_count
-- && frame
!= 0) {
661 db_addr_t call_func
= 0;
664 db_symbol_values(NULL
,
665 db_search_task_symbol_and_line(
671 (user_frame
) ? task
: 0,
673 &name
, (db_expr_t
*)&call_func
);
674 if (user_frame
== 0) {
675 if (call_func
== db_user_trap_symbol_value
||
676 call_func
== db_kernel_trap_symbol_value
) {
679 } else if (call_func
== db_interrupt_symbol_value
) {
680 frame_type
= INTERRUPT
;
682 } else if (call_func
== db_syscall_symbol_value
) {
683 frame_type
= SYSCALL
;
688 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
690 narg
= db_numargs(frame
,
691 (user_frame
) ? task
: 0);
701 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
703 narg
= db_numargs(frame
,
704 (user_frame
) ? task
: 0);
712 if (name
== 0 || offset
> db_maxoff
) {
713 db_printf("0x%x 0x%x(", frame
, callpc
);
716 db_printf("0x%x %s(", frame
, name
);
718 argp
= &frame
->f_arg0
;
723 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
724 value
= db_get_task_value((int)argp
,
727 (user_frame
) ? task
: 0);
730 if (r
== 2) /* 'q' from db_more() */
732 db_printf("... <stack error>)");
734 db_printf("+%x", offset
);
736 db_printf(" [%s", filename
);
738 db_printf(":%d", linenum
);
745 db_printf("%x", value
);
754 db_printf("+%x", offset
);
757 db_printf(" [%s", filename
);
759 db_printf(":%d", linenum
);
766 db_nextframe(&lastframe
, &frame
, &callpc
, frame_type
,
767 (user_frame
) ? th
: THREAD_NULL
);
770 if (th
->lower
!= THREAD_NULL
) {
771 if (top_act
== THREAD_NULL
)
774 db_printf(">>>>> next activation 0x%x ($task%d.%d) <<<<<\n",
776 db_lookup_task(th
->task
),
777 db_lookup_task_act(th
->task
, th
));
778 goto next_activation
;
783 if (!INKERNELSTACK(lastframe
, th
) ||
784 !INKERNELSTACK((unsigned)frame
, th
))
786 if (user_frame
== 1) {
787 db_printf(">>>>> user space <<<<<\n");
790 } else if ((!INKSERVER(lastframe
) || !INKSERVER(lastcallpc
)) &&
791 (INKSERVER(callpc
) && INKSERVER(frame
))) {
792 db_printf(">>>>> inkserver space <<<<<\n");
794 if (frame
<= lastframe
) {
795 if ((INKERNELSTACK(lastframe
, th
) &&
796 !INKERNELSTACK(frame
, th
)) ||
797 (INKSERVER(lastframe
) ^ INKSERVER(frame
)))
799 db_printf("Bad frame pointer: 0x%x\n", frame
);
805 if (trace_all_threads
) {
806 if (top_act
!= THREAD_NULL
)
808 th
= (thread_t
) queue_next(&th
->thr_acts
);
809 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
811 addr
= (db_expr_t
) th
;