2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
36 #include <machine/cpu.h>
37 #include <machine/frame.h>
38 #include <machine/md_var.h>
39 #include <machine/pcb.h>
40 #include <machine/stack.h>
43 #include <vm/vm_param.h>
47 #include <ddb/db_access.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/db_variables.h>
51 static db_varfcn_t db_esp;
52 static db_varfcn_t db_frame;
53 static db_varfcn_t db_frame_seg;
54 static db_varfcn_t db_gs;
55 static db_varfcn_t db_ss;
58 * Machine register set.
60 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x)
61 struct db_variable db_regs[] = {
62 { "cs", DB_OFFSET(tf_cs), db_frame_seg },
63 { "ds", DB_OFFSET(tf_ds), db_frame_seg },
64 { "es", DB_OFFSET(tf_es), db_frame_seg },
65 { "fs", DB_OFFSET(tf_fs), db_frame_seg },
66 { "gs", NULL, db_gs },
67 { "ss", NULL, db_ss },
68 { "eax", DB_OFFSET(tf_eax), db_frame },
69 { "ecx", DB_OFFSET(tf_ecx), db_frame },
70 { "edx", DB_OFFSET(tf_edx), db_frame },
71 { "ebx", DB_OFFSET(tf_ebx), db_frame },
72 { "esp", NULL, db_esp },
73 { "ebp", DB_OFFSET(tf_ebp), db_frame },
74 { "esi", DB_OFFSET(tf_esi), db_frame },
75 { "edi", DB_OFFSET(tf_edi), db_frame },
76 { "eip", DB_OFFSET(tf_eip), db_frame },
77 { "efl", DB_OFFSET(tf_eflags), db_frame },
79 struct db_variable *db_eregs = db_regs + nitems(db_regs);
82 get_esp(struct trapframe *tf)
84 return (TF_HAS_STACKREGS(tf) ? tf->tf_esp : (intptr_t)&tf->tf_esp);
88 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
92 if (kdb_frame == NULL)
95 reg = (int *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
104 db_frame_seg(struct db_variable *vp, db_expr_t *valuep, int op)
106 struct trapframe_vm86 *tfp;
110 if (kdb_frame == NULL)
113 off = (intptr_t)vp->valuep;
114 if (kdb_frame->tf_eflags & PSL_VM) {
115 tfp = (void *)kdb_frame;
116 switch ((intptr_t)vp->valuep) {
117 case (intptr_t)DB_OFFSET(tf_cs):
118 reg = (uint16_t *)&tfp->tf_cs;
120 case (intptr_t)DB_OFFSET(tf_ds):
121 reg = (uint16_t *)&tfp->tf_vm86_ds;
123 case (intptr_t)DB_OFFSET(tf_es):
124 reg = (uint16_t *)&tfp->tf_vm86_es;
126 case (intptr_t)DB_OFFSET(tf_fs):
127 reg = (uint16_t *)&tfp->tf_vm86_fs;
131 reg = (uint16_t *)((uintptr_t)kdb_frame + off);
132 if (op == DB_VAR_GET)
140 db_esp(struct db_variable *vp, db_expr_t *valuep, int op)
143 if (kdb_frame == NULL)
146 if (op == DB_VAR_GET)
147 *valuep = get_esp(kdb_frame);
148 else if (TF_HAS_STACKREGS(kdb_frame))
149 kdb_frame->tf_esp = *valuep;
154 db_gs(struct db_variable *vp, db_expr_t *valuep, int op)
156 struct trapframe_vm86 *tfp;
158 if (kdb_frame != NULL && kdb_frame->tf_eflags & PSL_VM) {
159 tfp = (void *)kdb_frame;
160 if (op == DB_VAR_GET)
161 *valuep = tfp->tf_vm86_gs;
163 tfp->tf_vm86_gs = *valuep;
166 if (op == DB_VAR_GET)
174 db_ss(struct db_variable *vp, db_expr_t *valuep, int op)
177 if (kdb_frame == NULL)
180 if (op == DB_VAR_GET)
181 *valuep = TF_HAS_STACKREGS(kdb_frame) ? kdb_frame->tf_ss :
183 else if (TF_HAS_STACKREGS(kdb_frame))
184 kdb_frame->tf_ss = *valuep;
192 #define DOUBLE_FAULT 4
194 static void db_nextframe(struct i386_frame **, db_addr_t *, struct thread *);
195 static int db_numargs(struct i386_frame *);
196 static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t,
200 * Figure out how many arguments were passed into the frame at "fp".
203 db_numargs(struct i386_frame *fp)
209 argp = (char *)db_get_value((int)&fp->f_retaddr, 4, false);
211 * XXX etext is wrong for LKMs. We should attempt to interpret
212 * the instruction at the return address in all cases. This
213 * may require better fault handling.
215 if (argp < btext || argp >= etext) {
219 inst = db_get_value((int)argp, 4, false);
220 if ((inst & 0xff) == 0x59) /* popl %ecx */
222 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
223 args = ((inst >> 16) & 0xff) / 4;
224 else if ((inst & 0xf8ff) == 0xc089) { /* movl %eax, %Reg */
234 db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
235 db_addr_t callpc, void *frame)
237 int n = narg >= 0 ? narg : 5;
239 db_printf("%s(", name);
242 db_printf("%s=", *argnp++);
243 db_printf("%r", db_get_value((int)argp, 4, false));
251 db_printsym(callpc, DB_STGY_PROC);
253 db_printf("/frame 0x%r", (register_t)frame);
258 * Figure out the next frame up in the call stack.
261 db_nextframe(struct i386_frame **fp, db_addr_t *ip, struct thread *td)
263 struct trapframe *tf;
270 eip = db_get_value((int) &(*fp)->f_retaddr, 4, false);
271 ebp = db_get_value((int) &(*fp)->f_frame, 4, false);
274 * Figure out frame type. We look at the address just before
275 * the saved instruction pointer as the saved EIP is after the
276 * call function, and if the function being called is marked as
277 * dead (such as panic() at the end of dblfault_handler()), then
278 * the instruction at the saved EIP will be part of a different
279 * function (syscall() in this example) rather than the one that
280 * actually made the call.
284 if (eip >= PMAP_TRM_MIN_ADDRESS) {
285 sym = db_search_symbol(eip - 1 - setidt_disp, DB_STGY_ANY,
288 sym = db_search_symbol(eip - 1, DB_STGY_ANY, &offset);
290 db_symbol_values(sym, &name, NULL);
292 if (strcmp(name, "calltrap") == 0 ||
293 strcmp(name, "fork_trampoline") == 0)
295 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
296 strncmp(name, "Xapic_isr", 9) == 0) {
297 frame_type = INTERRUPT;
298 } else if (strcmp(name, "Xlcall_syscall") == 0 ||
299 strcmp(name, "Xint0x80_syscall") == 0)
300 frame_type = SYSCALL;
301 else if (strcmp(name, "dblfault_handler") == 0)
302 frame_type = DOUBLE_FAULT;
303 else if (strcmp(name, "Xtimerint") == 0 ||
304 strcmp(name, "Xxen_intr_upcall") == 0)
305 frame_type = INTERRUPT;
306 else if (strcmp(name, "Xcpustop") == 0 ||
307 strcmp(name, "Xrendezvous") == 0 ||
308 strcmp(name, "Xipi_intr_bitmap_handler") == 0) {
310 frame_type = INTERRUPT;
315 * Normal frames need no special processing.
317 if (frame_type == NORMAL) {
318 *ip = (db_addr_t) eip;
319 *fp = (struct i386_frame *) ebp;
323 db_print_stack_entry(name, 0, 0, 0, eip, &(*fp)->f_frame);
326 * For a double fault, we have to snag the values from the
327 * previous TSS since a double fault uses a task gate to
328 * switch to a known good state.
330 if (frame_type == DOUBLE_FAULT) {
331 esp = PCPU_GET(common_tssp)->tss_esp;
332 eip = PCPU_GET(common_tssp)->tss_eip;
333 ebp = PCPU_GET(common_tssp)->tss_ebp;
335 "--- trap 0x17, eip = %#r, esp = %#r, ebp = %#r ---\n",
337 *ip = (db_addr_t) eip;
338 *fp = (struct i386_frame *) ebp;
343 * Point to base of trapframe which is just above the current
344 * frame. Pointer to it was put into %ebp by the kernel entry
347 tf = (struct trapframe *)(*fp)->f_frame;
350 * This can be the case for e.g. fork_trampoline, last frame
351 * of a kernel thread stack.
356 db_printf("--- kthread start\n");
363 switch (frame_type) {
365 db_printf("--- trap %#r", tf->tf_trapno);
368 db_printf("--- syscall");
369 db_decode_syscall(td, tf->tf_eax);
372 db_printf("--- interrupt");
375 panic("The moon has moved again.");
377 db_printf(", eip = %#r, esp = %#r, ebp = %#r ---\n", eip, esp, ebp);
380 * Detect the last (trap) frame on the kernel stack, where we
381 * entered kernel from usermode. Terminate tracing in this
384 switch (frame_type) {
387 if (!TRAPF_USERMODE(tf))
396 *ip = (db_addr_t) eip;
397 *fp = (struct i386_frame *) ebp;
401 db_backtrace(struct thread *td, struct trapframe *tf, struct i386_frame *frame,
402 db_addr_t pc, register_t sp, int count)
404 struct i386_frame *actframe;
406 char *argnames[MAXNARG], **argnp = NULL;
414 if (db_segsize(tf) == 16) {
416 "--- 16-bit%s, cs:eip = %#x:%#x, ss:esp = %#x:%#x, ebp = %#x, tf = %p ---\n",
417 (tf->tf_eflags & PSL_VM) ? " (vm86)" : "",
418 tf->tf_cs, tf->tf_eip,
419 TF_HAS_STACKREGS(tf) ? tf->tf_ss : rss(),
420 TF_HAS_STACKREGS(tf) ? tf->tf_esp : (intptr_t)&tf->tf_esp,
425 /* 'frame' can be null initially. Just print the pc then. */
430 * If an indirect call via an invalid pointer caused a trap,
431 * %pc contains the invalid address while the return address
432 * of the unlucky caller has been saved by CPU on the stack
433 * just before the trap frame. In this case, try to recover
434 * the caller's address so that the first frame is assigned
435 * to the right spot in the right function, for that is where
436 * the failure actually happened.
438 * This trick depends on the fault address stashed in tf_err
439 * by trap_fatal() before entering KDB.
441 if (kdb_frame && pc == kdb_frame->tf_err) {
443 * Find where the trap frame actually ends.
444 * It won't contain tf_esp or tf_ss unless crossing rings.
446 if (TF_HAS_STACKREGS(kdb_frame))
447 instr = (int)(kdb_frame + 1);
449 instr = (int)&kdb_frame->tf_esp;
450 pc = db_get_value(instr, 4, false);
457 while (count-- && !db_pager_quit) {
458 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
459 db_symbol_values(sym, &name, NULL);
462 * Attempt to determine a (possibly fake) frame that gives
463 * the caller's pc. It may differ from `frame' if the
464 * current function never sets up a standard frame or hasn't
465 * set one up yet or has just discarded one. The last two
466 * cases can be guessed fairly reliably for code generated
467 * by gcc. The first case is too much trouble to handle in
468 * general because the amount of junk on the stack depends
469 * on the pc (the special handling of "calltrap", etc. in
470 * db_nextframe() works because the `next' pc is special).
475 if (sym == C_DB_SYM_NULL && sp != 0) {
477 * If a symbol couldn't be found, we've probably
478 * jumped to a bogus location, so try and use
479 * the return address to find our caller.
481 db_print_stack_entry(name, 0, 0, 0, pc,
483 pc = db_get_value(sp, 4, false);
484 if (db_search_symbol(pc, DB_STGY_PROC,
485 &offset) == C_DB_SYM_NULL)
488 } else if (tf != NULL) {
489 instr = db_get_value(pc, 4, false);
490 if ((instr & 0xffffff) == 0x00e58955) {
491 /* pushl %ebp; movl %esp, %ebp */
492 actframe = (void *)(get_esp(tf) - 4);
493 } else if ((instr & 0xffff) == 0x0000e589) {
494 /* movl %esp, %ebp */
495 actframe = (void *)get_esp(tf);
496 if (tf->tf_ebp == 0) {
497 /* Fake frame better. */
500 } else if ((instr & 0xff) == 0x000000c3) {
502 actframe = (void *)(get_esp(tf) - 4);
503 } else if (offset == 0) {
504 /* Probably an assembler symbol. */
505 actframe = (void *)(get_esp(tf) - 4);
507 } else if (strcmp(name, "fork_trampoline") == 0) {
509 * Don't try to walk back on a stack for a
510 * process that hasn't actually been run yet.
512 db_print_stack_entry(name, 0, 0, 0, pc,
518 argp = &actframe->f_arg0;
520 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
523 narg = db_numargs(frame);
526 db_print_stack_entry(name, narg, argnp, argp, pc, actframe);
528 if (actframe != frame) {
529 /* `frame' belongs to caller. */
531 db_get_value((int)&actframe->f_retaddr, 4, false);
535 db_nextframe(&frame, &pc, td);
539 * 'frame' can be null here, either because it was initially
540 * null or because db_nextframe() found no frame.
541 * db_nextframe() may also have found a non-kernel frame.
542 * !INKERNEL() classifies both. Stop tracing if either,
543 * after printing the pc if it is the kernel.
545 if (frame == NULL || frame <= actframe) {
547 sym = db_search_symbol(pc, DB_STGY_ANY,
549 db_symbol_values(sym, &name, NULL);
550 db_print_stack_entry(name, 0, 0, 0, pc, frame);
562 struct i386_frame *frame;
566 __asm __volatile("movl %%ebp,%0" : "=r" (ebp));
567 frame = (struct i386_frame *)ebp;
568 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, false);
569 frame = frame->f_frame;
570 db_backtrace(curthread, NULL, frame, callpc, 0, -1);
574 db_trace_thread(struct thread *thr, int count)
577 struct trapframe *tf;
579 ctx = kdb_thr_ctx(thr);
580 tf = thr == kdb_thread ? kdb_frame : NULL;
581 return (db_backtrace(thr, tf, (struct i386_frame *)ctx->pcb_ebp,
582 ctx->pcb_eip, ctx->pcb_esp, count));
586 db_md_list_watchpoints(void)
589 dbreg_list_watchpoints();