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@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
55 #include <mach/boolean.h>
56 #include <vm/vm_map.h>
57 #include <kern/thread.h>
58 #include <kern/task.h>
60 #include <machine/asm.h>
61 #include <machine/db_machdep.h>
62 #include <machine/setjmp.h>
63 #include <mach/machine.h>
65 #include <ddb/db_access.h>
66 #include <ddb/db_sym.h>
67 #include <ddb/db_variables.h>
68 #include <ddb/db_command.h>
69 #include <ddb/db_task_thread.h>
70 #include <ddb/db_output.h>
72 extern jmp_buf_t
*db_recover
;
73 extern struct i386_saved_state
*saved_state
[];
75 struct i386_kernel_state ddb_null_kregs
;
81 extern vm_offset_t vm_min_inks_addr
; /* set by db_clone_symtabXXX */
82 #define INKSERVER(va) (((vm_offset_t)(va)) >= vm_min_inks_addr)
84 extern vm_offset_t interrupt_stack
[];
85 #define ININTSTACK(va) \
86 (((vm_offset_t)(va)) >= interrupt_stack[cpu_number()] &&\
87 (((vm_offset_t)(va)) < interrupt_stack[cpu_number()] + \
90 #define INKERNELSTACK(va, th) \
91 (th == THREAD_NULL || \
92 (((vm_offset_t)(va)) >= th->thread->kernel_stack && \
93 (((vm_offset_t)(va)) < th->thread->kernel_stack + \
94 KERNEL_STACK_SIZE)) || \
98 struct i386_frame
*f_frame
;
107 db_addr_t db_user_trap_symbol_value
= 0;
108 db_addr_t db_kernel_trap_symbol_value
= 0;
109 db_addr_t db_interrupt_symbol_value
= 0;
110 db_addr_t db_return_to_iret_symbol_value
= 0;
111 db_addr_t db_syscall_symbol_value
= 0;
112 boolean_t db_trace_symbols_found
= FALSE
;
118 { "ebx", (int)(&((struct i386_kernel_state
*)0)->k_ebx
) },
119 { "esp", (int)(&((struct i386_kernel_state
*)0)->k_esp
) },
120 { "ebp", (int)(&((struct i386_kernel_state
*)0)->k_ebp
) },
121 { "edi", (int)(&((struct i386_kernel_state
*)0)->k_edi
) },
122 { "esi", (int)(&((struct i386_kernel_state
*)0)->k_esi
) },
123 { "eip", (int)(&((struct i386_kernel_state
*)0)->k_eip
) },
129 extern int * db_lookup_i386_kreg(
132 extern int db_i386_reg_value(
133 struct db_variable
* vp
,
136 db_var_aux_param_t ap
);
137 extern void db_find_trace_symbols(void);
138 extern int db_numargs(
139 struct i386_frame
*fp
,
141 extern void db_nextframe(
142 struct i386_frame
**lfp
,
143 struct i386_frame
**fp
,
151 * Machine register set.
153 struct db_variable db_regs
[] = {
154 { "cs", (int *)&ddb_regs
.cs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
155 { "ds", (int *)&ddb_regs
.ds
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
156 { "es", (int *)&ddb_regs
.es
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
157 { "fs", (int *)&ddb_regs
.fs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
158 { "gs", (int *)&ddb_regs
.gs
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
159 { "ss", (int *)&ddb_regs
.ss
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
160 { "eax",(int *)&ddb_regs
.eax
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
161 { "ecx",(int *)&ddb_regs
.ecx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
162 { "edx",(int *)&ddb_regs
.edx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
163 { "ebx",(int *)&ddb_regs
.ebx
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
164 { "esp",(int *)&ddb_regs
.uesp
,db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
165 { "ebp",(int *)&ddb_regs
.ebp
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
166 { "esi",(int *)&ddb_regs
.esi
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
167 { "edi",(int *)&ddb_regs
.edi
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
168 { "eip",(int *)&ddb_regs
.eip
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
169 { "efl",(int *)&ddb_regs
.efl
, db_i386_reg_value
, 0, 0, 0, 0, TRUE
},
171 struct db_variable
*db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
178 register struct i386_kregs
*kp
;
180 for (kp
= i386_kregs
; kp
->name
; kp
++) {
181 if (strcmp(name
, kp
->name
) == 0)
182 return((int *)((int)kregp
+ kp
->offset
));
189 struct db_variable
*vp
,
192 db_var_aux_param_t ap
)
196 db_expr_t null_reg
= 0;
197 register thread_t thr_act
= ap
->thr_act
;
198 extern unsigned int_stack_high
;
201 if (db_option(ap
->modif
, 'u')) {
202 if (thr_act
== THREAD_NULL
) {
203 if ((thr_act
= current_thread()) == THREAD_NULL
)
204 db_error("no user registers\n");
206 if (thr_act
== current_thread()) {
207 if (IS_USER_TRAP(&ddb_regs
, &etext
))
209 else if (ddb_regs
.ebp
< int_stack_high
)
210 db_error("cannot get/set user registers in nested interrupt\n");
213 if (thr_act
== THREAD_NULL
|| thr_act
== current_thread()) {
216 if (thr_act
->thread
&&
217 !(thr_act
->thread
->state
& TH_STACK_HANDOFF
) &&
218 thr_act
->thread
->kernel_stack
) {
221 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
222 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
223 cpu_datap(cpu
)->cpu_active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
224 dp
= (int *) (((int)saved_state
[cpu
]) +
225 (((int) vp
->valuep
) -
230 if (dp
== 0 && thr_act
&& thr_act
->thread
)
231 dp
= db_lookup_i386_kreg(vp
->name
,
232 (int *)(STACK_IKS(thr_act
->thread
->kernel_stack
)));
235 } else if (thr_act
->thread
&&
236 (thr_act
->thread
->state
&TH_STACK_HANDOFF
)){
237 /* only EIP is valid */
238 if (vp
->valuep
== (int *) &ddb_regs
.eip
) {
239 dp
= (int *)(&thr_act
->thread
->continuation
);
249 if (!db_option(ap
->modif
, 'u')) {
250 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
251 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
252 cpu_datap(cpu
)->cpu_active_thread
== thr_act
->thread
&& saved_state
[cpu
]) {
253 dp
= (int *) (((int)saved_state
[cpu
]) +
254 (((int) vp
->valuep
) -
261 if (!thr_act
|| thr_act
->machine
.pcb
== 0)
262 db_error("no pcb\n");
263 dp
= (int *)((int)(&thr_act
->machine
.pcb
->iss
) +
264 ((int)vp
->valuep
- (int)&ddb_regs
));
267 if (flag
== DB_VAR_SET
)
275 db_find_trace_symbols(void)
278 boolean_t found_some
;
281 if (db_value_of_name(CC_SYM_PREFIX
"user_trap", &value
)) {
282 db_user_trap_symbol_value
= (db_addr_t
) value
;
285 if (db_value_of_name(CC_SYM_PREFIX
"kernel_trap", &value
)) {
286 db_kernel_trap_symbol_value
= (db_addr_t
) value
;
289 if (db_value_of_name(CC_SYM_PREFIX
"interrupt", &value
)) {
290 db_interrupt_symbol_value
= (db_addr_t
) value
;
293 if (db_value_of_name(CC_SYM_PREFIX
"return_to_iret", &value
)) {
294 db_return_to_iret_symbol_value
= (db_addr_t
) value
;
297 if (db_value_of_name(CC_SYM_PREFIX
"syscall", &value
)) {
298 db_syscall_symbol_value
= (db_addr_t
) value
;
302 db_trace_symbols_found
= TRUE
;
306 * Figure out how many arguments were passed into the frame at "fp".
308 int db_numargs_default
= 5;
312 struct i386_frame
*fp
,
320 argp
= (int *)db_get_task_value((int)&fp
->f_retaddr
, 4, FALSE
, task
);
321 if (argp
< (int *)VM_MIN_KERNEL_ADDRESS
|| (char *)argp
> &etext
)
322 args
= db_numargs_default
;
323 else if (!DB_CHECK_ACCESS((int)argp
, 4, task
))
324 args
= db_numargs_default
;
326 inst
= db_get_task_value((int)argp
, 4, FALSE
, task
);
327 if ((inst
& 0xff) == 0x59) /* popl %ecx */
329 else if ((inst
& 0xffff) == 0xc483) /* addl %n, %esp */
330 args
= ((inst
>> 16) & 0xff) / 4;
332 args
= db_numargs_default
;
337 struct interrupt_frame
{
338 struct i386_frame
*if_frame
; /* point to next frame */
339 int if_retaddr
; /* return address to _interrupt */
340 int if_unit
; /* unit number */
341 int if_spl
; /* saved spl */
342 int if_iretaddr
; /* _return_to_{iret,iret_i} */
343 int if_edx
; /* old sp(iret) or saved edx(iret_i) */
344 int if_ecx
; /* saved ecx(iret_i) */
345 int if_eax
; /* saved eax(iret_i) */
346 int if_eip
; /* saved eip(iret_i) */
347 int if_cs
; /* saved cs(iret_i) */
348 int if_efl
; /* saved efl(iret_i) */
352 * Figure out the next frame up in the call stack.
353 * For trap(), we print the address of the faulting instruction and
354 * proceed with the calling frame. We return the ip that faulted.
355 * If the trap was caused by jumping through a bogus pointer, then
356 * the next line in the backtrace will list some random function as
357 * being called. It should get the argument list correct, though.
358 * It might be possible to dig out from the next frame up the name
359 * of the function that faulted, but that could get hairy.
363 struct i386_frame
**lfp
, /* in/out */
364 struct i386_frame
**fp
, /* in/out */
365 db_addr_t
*ip
, /* out */
366 int frame_type
, /* in */
367 thread_t thr_act
) /* in */
369 extern char * trap_type
[];
370 extern int TRAP_TYPES
;
372 struct i386_saved_state
*saved_regs
;
373 struct interrupt_frame
*ifp
;
374 struct i386_interrupt_state
*isp
;
375 task_t task
= (thr_act
!= THREAD_NULL
)? thr_act
->task
: TASK_NULL
;
380 * We know that trap() has 1 argument and we know that
381 * it is an (strcut i386_saved_state *).
383 saved_regs
= (struct i386_saved_state
*)
384 db_get_task_value((int)&((*fp
)->f_arg0
),4,FALSE
,task
);
385 if (saved_regs
->trapno
>= 0 && saved_regs
->trapno
< TRAP_TYPES
) {
386 db_printf(">>>>> %s trap at ",
387 trap_type
[saved_regs
->trapno
]);
389 db_printf(">>>>> trap (number %d) at ",
390 saved_regs
->trapno
& 0xffff);
392 db_task_printsym(saved_regs
->eip
, DB_STGY_PROC
, task
);
393 db_printf(" <<<<<\n");
394 *fp
= (struct i386_frame
*)saved_regs
->ebp
;
395 *ip
= (db_addr_t
)saved_regs
->eip
;
399 db_printf(">>>>> interrupt <<<<<\n");
402 db_printf(">>>>> interrupt at ");
403 ifp
= (struct interrupt_frame
*)(*lfp
);
405 if (ifp
->if_iretaddr
== db_return_to_iret_symbol_value
)
406 *ip
= ((struct i386_interrupt_state
*) ifp
->if_edx
)->eip
;
408 *ip
= (db_addr_t
) ifp
->if_eip
;
409 db_task_printsym(*ip
, DB_STGY_PROC
, task
);
410 db_printf(" <<<<<\n");
413 if (thr_act
!= THREAD_NULL
&& thr_act
->machine
.pcb
) {
414 *ip
= (db_addr_t
) thr_act
->machine
.pcb
->iss
.eip
;
415 *fp
= (struct i386_frame
*) thr_act
->machine
.pcb
->iss
.ebp
;
418 /* falling down for unknown case */
422 db_get_task_value((int)&(*fp
)->f_retaddr
, 4, FALSE
, task
);
424 *fp
= (struct i386_frame
*)
425 db_get_task_value((int)&(*fp
)->f_frame
, 4, FALSE
, task
);
437 struct i386_frame
*frame
, *lastframe
;
439 db_addr_t callpc
, lastcallpc
;
441 boolean_t kernel_only
= TRUE
;
442 boolean_t trace_thread
= FALSE
;
443 boolean_t trace_all_threads
= FALSE
;
448 thread_t th
, top_act
;
452 jmp_buf_t db_jmp_buf
;
453 queue_entry_t act_list
;
455 if (!db_trace_symbols_found
)
456 db_find_trace_symbols();
459 register char *cp
= modif
;
462 while ((c
= *cp
++) != 0) {
466 trace_all_threads
= TRUE
;
474 if (trace_all_threads
) {
475 if (!have_addr
&& !trace_thread
) {
478 act_list
= &(current_task()->thr_acts
);
479 addr
= (db_expr_t
) queue_first(act_list
);
480 } else if (trace_thread
) {
482 if (!db_check_act_address_valid((thread_t
)addr
)) {
483 if (db_lookup_task((task_t
)addr
) == -1)
485 act_list
= &(((task_t
)addr
)->thr_acts
);
486 addr
= (db_expr_t
) queue_first(act_list
);
488 act_list
= &(((thread_t
)addr
)->task
->thr_acts
);
489 thcount
= db_lookup_task_act(((thread_t
)addr
)->task
,
494 if (th
== THREAD_NULL
)
495 th
= current_thread();
496 if (th
== THREAD_NULL
) {
497 db_printf("no active thr_act\n");
501 act_list
= &th
->task
->thr_acts
;
502 addr
= (db_expr_t
) queue_first(act_list
);
511 top_act
= THREAD_NULL
;
516 if (!have_addr
&& !trace_thread
) {
517 frame
= (struct i386_frame
*)ddb_regs
.ebp
;
518 callpc
= (db_addr_t
)ddb_regs
.eip
;
519 th
= current_thread();
520 task
= (th
!= THREAD_NULL
)? th
->task
: TASK_NULL
;
521 } else if (trace_thread
) {
523 th
= (thread_t
) addr
;
524 if (!db_check_act_address_valid(th
))
528 if (th
== THREAD_NULL
)
529 th
= current_thread();
530 if (th
== THREAD_NULL
) {
531 db_printf("no active thread\n");
535 if (trace_all_threads
)
536 db_printf("---------- Thread 0x%x (#%d of %d) ----------\n",
537 addr
, thcount
, th
->task
->thr_act_count
);
543 if (th
== current_thread()) {
544 frame
= (struct i386_frame
*)ddb_regs
.ebp
;
545 callpc
= (db_addr_t
)ddb_regs
.eip
;
547 if (th
->machine
.pcb
== 0) {
548 db_printf("thread has no pcb\n");
552 register struct i386_saved_state
*iss
=
553 &th
->machine
.pcb
->iss
;
555 db_printf("thread has no shuttle\n");
557 frame
= (struct i386_frame
*) (iss
->ebp
);
558 callpc
= (db_addr_t
) (iss
->eip
);
563 else if ((th
->thread
->state
& TH_STACK_HANDOFF
) ||
564 th
->thread
->kernel_stack
== 0) {
565 register struct i386_saved_state
*iss
=
566 &th
->machine
.pcb
->iss
;
568 db_printf("Continuation ");
569 db_task_printsym((db_expr_t
)th
->thread
->continuation
,
572 frame
= (struct i386_frame
*) (iss
->ebp
);
573 callpc
= (db_addr_t
) (iss
->eip
);
577 for (cpu
= 0; cpu
< real_ncpus
; cpu
++) {
578 if (cpu_datap(cpu
)->cpu_running
== TRUE
&&
579 cpu_datap(cpu
)->cpu_active_thread
== th
->thread
&&
584 if (top_act
!= THREAD_NULL
) {
586 * Trying to get the backtrace of an activation
587 * which is not the top_most one in the RPC chain:
588 * use the activation's pcb.
590 register struct i386_saved_state
*iss
=
591 &th
->machine
.pcb
->iss
;
592 frame
= (struct i386_frame
*) (iss
->ebp
);
593 callpc
= (db_addr_t
) (iss
->eip
);
596 register struct i386_kernel_state
*iks
;
599 iks
= STACK_IKS(th
->thread
->kernel_stack
);
601 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
602 frame
= (struct i386_frame
*) (iks
->k_ebp
);
603 callpc
= (db_addr_t
) (iks
->k_eip
);
606 * The kernel stack has probably been
607 * paged out (swapped out activation).
610 if (r
== 2) /* 'q' from db_more() */
612 db_printf("<kernel stack (0x%x) error "
613 "(probably swapped out)>\n",
619 db_printf(">>>>> active on cpu %d <<<<<\n",
621 frame
= (struct i386_frame
*)
622 saved_state
[cpu
]->ebp
;
623 callpc
= (db_addr_t
) saved_state
[cpu
]->eip
;
629 frame
= (struct i386_frame
*)addr
;
630 th
= (db_default_act
)? db_default_act
: current_thread();
631 task
= (th
!= THREAD_NULL
)? th
->task
: TASK_NULL
;
632 callpc
= (db_addr_t
)db_get_task_value((int)&frame
->f_retaddr
,
635 (user_frame
) ? task
: 0);
638 if (!INKERNELSTACK((unsigned)frame
, th
)) {
639 db_printf(">>>>> user space <<<<<\n");
643 } else if (INKSERVER(callpc
) && INKSERVER(frame
)) {
644 db_printf(">>>>> INKserver space <<<<<\n");
648 lastcallpc
= (db_addr_t
) 0;
649 while (frame_count
-- && frame
!= 0) {
653 db_addr_t call_func
= 0;
656 db_symbol_values(NULL
,
657 db_search_task_symbol_and_line(
663 (user_frame
) ? task
: 0,
665 &name
, (db_expr_t
*)&call_func
);
666 if (user_frame
== 0) {
667 if (call_func
== db_user_trap_symbol_value
||
668 call_func
== db_kernel_trap_symbol_value
) {
671 } else if (call_func
== db_interrupt_symbol_value
) {
672 frame_type
= INTERRUPT
;
674 } else if (call_func
== db_syscall_symbol_value
) {
675 frame_type
= SYSCALL
;
680 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
682 narg
= db_numargs(frame
,
683 (user_frame
) ? task
: 0);
693 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
695 narg
= db_numargs(frame
,
696 (user_frame
) ? task
: 0);
704 if (name
== 0 || offset
> db_maxoff
) {
705 db_printf("0x%x 0x%x(", frame
, callpc
);
708 db_printf("0x%x %s(", frame
, name
);
710 argp
= &frame
->f_arg0
;
715 if ((r
= _setjmp(db_recover
= &db_jmp_buf
)) == 0) {
716 value
= db_get_task_value((int)argp
,
719 (user_frame
) ? task
: 0);
722 if (r
== 2) /* 'q' from db_more() */
724 db_printf("... <stack error>)");
726 db_printf("+%x", offset
);
728 db_printf(" [%s", filename
);
730 db_printf(":%d", linenum
);
737 db_printf("%x", value
);
746 db_printf("+%x", offset
);
749 db_printf(" [%s", filename
);
751 db_printf(":%d", linenum
);
758 db_nextframe(&lastframe
, &frame
, &callpc
, frame_type
,
759 (user_frame
) ? th
: THREAD_NULL
);
762 if (th
->lower
!= THREAD_NULL
) {
763 if (top_act
== THREAD_NULL
)
766 db_printf(">>>>> next activation 0x%x ($task%d.%d) <<<<<\n",
768 db_lookup_task(th
->task
),
769 db_lookup_task_act(th
->task
, th
));
770 goto next_activation
;
775 if (!INKERNELSTACK(lastframe
, th
) ||
776 !INKERNELSTACK((unsigned)frame
, th
))
778 if (user_frame
== 1) {
779 db_printf(">>>>> user space <<<<<\n");
782 } else if ((!INKSERVER(lastframe
) || !INKSERVER(lastcallpc
)) &&
783 (INKSERVER(callpc
) && INKSERVER(frame
))) {
784 db_printf(">>>>> inkserver space <<<<<\n");
786 if (frame
<= lastframe
) {
787 if ((INKERNELSTACK(lastframe
, th
) &&
788 !INKERNELSTACK(frame
, th
)) ||
789 (INKSERVER(lastframe
) ^ INKSERVER(frame
)))
791 db_printf("Bad frame pointer: 0x%x\n", frame
);
797 if (trace_all_threads
) {
798 if (top_act
!= THREAD_NULL
)
800 th
= (thread_t
) queue_next(&th
->thr_acts
);
801 if (! queue_end(act_list
, (queue_entry_t
) th
)) {
803 addr
= (db_expr_t
) th
;