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>
34 #include <sys/sysent.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/reg.h>
41 #include <machine/stack.h>
44 #include <vm/vm_param.h>
48 #include <ddb/db_access.h>
49 #include <ddb/db_sym.h>
50 #include <ddb/db_variables.h>
52 static db_varfcn_t db_esp;
53 static db_varfcn_t db_frame;
54 static db_varfcn_t db_frame_seg;
55 static db_varfcn_t db_gs;
56 static db_varfcn_t db_ss;
59 * Machine register set.
61 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x)
62 struct db_variable db_regs[] = {
63 { "cs", DB_OFFSET(tf_cs), db_frame_seg },
64 { "ds", DB_OFFSET(tf_ds), db_frame_seg },
65 { "es", DB_OFFSET(tf_es), db_frame_seg },
66 { "fs", DB_OFFSET(tf_fs), db_frame_seg },
67 { "gs", NULL, db_gs },
68 { "ss", NULL, db_ss },
69 { "eax", DB_OFFSET(tf_eax), db_frame },
70 { "ecx", DB_OFFSET(tf_ecx), db_frame },
71 { "edx", DB_OFFSET(tf_edx), db_frame },
72 { "ebx", DB_OFFSET(tf_ebx), db_frame },
73 { "esp", NULL, db_esp },
74 { "ebp", DB_OFFSET(tf_ebp), db_frame },
75 { "esi", DB_OFFSET(tf_esi), db_frame },
76 { "edi", DB_OFFSET(tf_edi), db_frame },
77 { "eip", DB_OFFSET(tf_eip), db_frame },
78 { "efl", DB_OFFSET(tf_eflags), db_frame },
80 struct db_variable *db_eregs = db_regs + nitems(db_regs);
83 get_esp(struct trapframe *tf)
85 return (TF_HAS_STACKREGS(tf) ? tf->tf_esp : (intptr_t)&tf->tf_esp);
89 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
93 if (kdb_frame == NULL)
96 reg = (int *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
105 db_frame_seg(struct db_variable *vp, db_expr_t *valuep, int op)
107 struct trapframe_vm86 *tfp;
111 if (kdb_frame == NULL)
114 off = (intptr_t)vp->valuep;
115 if (kdb_frame->tf_eflags & PSL_VM) {
116 tfp = (void *)kdb_frame;
117 switch ((intptr_t)vp->valuep) {
118 case (intptr_t)DB_OFFSET(tf_cs):
119 reg = (uint16_t *)&tfp->tf_cs;
121 case (intptr_t)DB_OFFSET(tf_ds):
122 reg = (uint16_t *)&tfp->tf_vm86_ds;
124 case (intptr_t)DB_OFFSET(tf_es):
125 reg = (uint16_t *)&tfp->tf_vm86_es;
127 case (intptr_t)DB_OFFSET(tf_fs):
128 reg = (uint16_t *)&tfp->tf_vm86_fs;
132 reg = (uint16_t *)((uintptr_t)kdb_frame + off);
133 if (op == DB_VAR_GET)
141 db_esp(struct db_variable *vp, db_expr_t *valuep, int op)
144 if (kdb_frame == NULL)
147 if (op == DB_VAR_GET)
148 *valuep = get_esp(kdb_frame);
149 else if (TF_HAS_STACKREGS(kdb_frame))
150 kdb_frame->tf_esp = *valuep;
155 db_gs(struct db_variable *vp, db_expr_t *valuep, int op)
157 struct trapframe_vm86 *tfp;
159 if (kdb_frame != NULL && kdb_frame->tf_eflags & PSL_VM) {
160 tfp = (void *)kdb_frame;
161 if (op == DB_VAR_GET)
162 *valuep = tfp->tf_vm86_gs;
164 tfp->tf_vm86_gs = *valuep;
167 if (op == DB_VAR_GET)
175 db_ss(struct db_variable *vp, db_expr_t *valuep, int op)
178 if (kdb_frame == NULL)
181 if (op == DB_VAR_GET)
182 *valuep = TF_HAS_STACKREGS(kdb_frame) ? kdb_frame->tf_ss :
184 else if (TF_HAS_STACKREGS(kdb_frame))
185 kdb_frame->tf_ss = *valuep;
193 #define DOUBLE_FAULT 4
194 #define TRAP_INTERRUPT 5
195 #define TRAP_TIMERINT 6
197 static void db_nextframe(struct i386_frame **, db_addr_t *, struct thread *);
198 static int db_numargs(struct i386_frame *);
199 static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t,
201 static void decode_syscall(int, struct thread *);
203 static const char * watchtype_str(int type);
204 int i386_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
206 int i386_clr_watch(int watchnum, struct dbreg *d);
209 * Figure out how many arguments were passed into the frame at "fp".
213 struct i386_frame *fp;
219 argp = (char *)db_get_value((int)&fp->f_retaddr, 4, false);
221 * XXX etext is wrong for LKMs. We should attempt to interpret
222 * the instruction at the return address in all cases. This
223 * may require better fault handling.
225 if (argp < btext || argp >= etext) {
229 inst = db_get_value((int)argp, 4, false);
230 if ((inst & 0xff) == 0x59) /* popl %ecx */
232 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
233 args = ((inst >> 16) & 0xff) / 4;
234 else if ((inst & 0xf8ff) == 0xc089) { /* movl %eax, %Reg */
244 db_print_stack_entry(name, narg, argnp, argp, callpc, frame)
252 int n = narg >= 0 ? narg : 5;
254 db_printf("%s(", name);
257 db_printf("%s=", *argnp++);
258 db_printf("%r", db_get_value((int)argp, 4, false));
266 db_printsym(callpc, DB_STGY_PROC);
268 db_printf("/frame 0x%r", (register_t)frame);
273 decode_syscall(int number, struct thread *td)
281 db_printf(" (%d", number);
282 p = (td != NULL) ? td->td_proc : NULL;
283 if (p != NULL && 0 <= number && number < p->p_sysent->sv_size) {
284 f = p->p_sysent->sv_table[number].sy_call;
285 sym = db_search_symbol((db_addr_t)f, DB_STGY_ANY, &diff);
286 if (sym != DB_SYM_NULL && diff == 0) {
287 db_symbol_values(sym, &symname, NULL);
288 db_printf(", %s, %s", p->p_sysent->sv_name, symname);
295 * Figure out the next frame up in the call stack.
298 db_nextframe(struct i386_frame **fp, db_addr_t *ip, struct thread *td)
300 struct trapframe *tf;
307 eip = db_get_value((int) &(*fp)->f_retaddr, 4, false);
308 ebp = db_get_value((int) &(*fp)->f_frame, 4, false);
311 * Figure out frame type. We look at the address just before
312 * the saved instruction pointer as the saved EIP is after the
313 * call function, and if the function being called is marked as
314 * dead (such as panic() at the end of dblfault_handler()), then
315 * the instruction at the saved EIP will be part of a different
316 * function (syscall() in this example) rather than the one that
317 * actually made the call.
320 if (eip >= PMAP_TRM_MIN_ADDRESS) {
321 sym = db_search_symbol(eip - 1 - setidt_disp, DB_STGY_ANY,
324 sym = db_search_symbol(eip - 1, DB_STGY_ANY, &offset);
326 db_symbol_values(sym, &name, NULL);
328 if (strcmp(name, "calltrap") == 0 ||
329 strcmp(name, "fork_trampoline") == 0)
331 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
332 strncmp(name, "Xapic_isr", 9) == 0)
333 frame_type = INTERRUPT;
334 else if (strcmp(name, "Xlcall_syscall") == 0 ||
335 strcmp(name, "Xint0x80_syscall") == 0)
336 frame_type = SYSCALL;
337 else if (strcmp(name, "dblfault_handler") == 0)
338 frame_type = DOUBLE_FAULT;
339 /* XXX: These are interrupts with trap frames. */
340 else if (strcmp(name, "Xtimerint") == 0)
341 frame_type = TRAP_TIMERINT;
342 else if (strcmp(name, "Xcpustop") == 0 ||
343 strcmp(name, "Xrendezvous") == 0 ||
344 strcmp(name, "Xipi_intr_bitmap_handler") == 0)
345 frame_type = TRAP_INTERRUPT;
349 * Normal frames need no special processing.
351 if (frame_type == NORMAL) {
352 *ip = (db_addr_t) eip;
353 *fp = (struct i386_frame *) ebp;
357 db_print_stack_entry(name, 0, 0, 0, eip, &(*fp)->f_frame);
360 * For a double fault, we have to snag the values from the
361 * previous TSS since a double fault uses a task gate to
362 * switch to a known good state.
364 if (frame_type == DOUBLE_FAULT) {
365 esp = PCPU_GET(common_tssp)->tss_esp;
366 eip = PCPU_GET(common_tssp)->tss_eip;
367 ebp = PCPU_GET(common_tssp)->tss_ebp;
369 "--- trap 0x17, eip = %#r, esp = %#r, ebp = %#r ---\n",
371 *ip = (db_addr_t) eip;
372 *fp = (struct i386_frame *) ebp;
377 * Point to base of trapframe which is just above the
380 if (frame_type == INTERRUPT)
381 tf = (struct trapframe *)((int)*fp + 16);
382 else if (frame_type == TRAP_INTERRUPT)
383 tf = (struct trapframe *)((int)*fp + 8);
385 tf = (struct trapframe *)((int)*fp + 12);
390 switch (frame_type) {
392 db_printf("--- trap %#r", tf->tf_trapno);
395 db_printf("--- syscall");
396 decode_syscall(tf->tf_eax, td);
401 db_printf("--- interrupt");
404 panic("The moon has moved again.");
406 db_printf(", eip = %#r, esp = %#r, ebp = %#r ---\n", eip, esp, ebp);
408 switch (frame_type) {
413 if ((tf->tf_eflags & PSL_VM) != 0 ||
414 (tf->tf_cs & SEL_RPL_MASK) != 0)
422 *ip = (db_addr_t) eip;
423 *fp = (struct i386_frame *) ebp;
427 db_backtrace(struct thread *td, struct trapframe *tf, struct i386_frame *frame,
428 db_addr_t pc, register_t sp, int count)
430 struct i386_frame *actframe;
432 char *argnames[MAXNARG], **argnp = NULL;
440 if (db_segsize(tf) == 16) {
442 "--- 16-bit%s, cs:eip = %#x:%#x, ss:esp = %#x:%#x, ebp = %#x, tf = %p ---\n",
443 (tf->tf_eflags & PSL_VM) ? " (vm86)" : "",
444 tf->tf_cs, tf->tf_eip,
445 TF_HAS_STACKREGS(tf) ? tf->tf_ss : rss(),
446 TF_HAS_STACKREGS(tf) ? tf->tf_esp : (intptr_t)&tf->tf_esp,
451 /* 'frame' can be null initially. Just print the pc then. */
456 * If an indirect call via an invalid pointer caused a trap,
457 * %pc contains the invalid address while the return address
458 * of the unlucky caller has been saved by CPU on the stack
459 * just before the trap frame. In this case, try to recover
460 * the caller's address so that the first frame is assigned
461 * to the right spot in the right function, for that is where
462 * the failure actually happened.
464 * This trick depends on the fault address stashed in tf_err
465 * by trap_fatal() before entering KDB.
467 if (kdb_frame && pc == kdb_frame->tf_err) {
469 * Find where the trap frame actually ends.
470 * It won't contain tf_esp or tf_ss unless crossing rings.
472 if (TF_HAS_STACKREGS(kdb_frame))
473 instr = (int)(kdb_frame + 1);
475 instr = (int)&kdb_frame->tf_esp;
476 pc = db_get_value(instr, 4, false);
483 while (count-- && !db_pager_quit) {
484 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
485 db_symbol_values(sym, &name, NULL);
488 * Attempt to determine a (possibly fake) frame that gives
489 * the caller's pc. It may differ from `frame' if the
490 * current function never sets up a standard frame or hasn't
491 * set one up yet or has just discarded one. The last two
492 * cases can be guessed fairly reliably for code generated
493 * by gcc. The first case is too much trouble to handle in
494 * general because the amount of junk on the stack depends
495 * on the pc (the special handling of "calltrap", etc. in
496 * db_nextframe() works because the `next' pc is special).
501 if (sym == C_DB_SYM_NULL && sp != 0) {
503 * If a symbol couldn't be found, we've probably
504 * jumped to a bogus location, so try and use
505 * the return address to find our caller.
507 db_print_stack_entry(name, 0, 0, 0, pc,
509 pc = db_get_value(sp, 4, false);
510 if (db_search_symbol(pc, DB_STGY_PROC,
511 &offset) == C_DB_SYM_NULL)
514 } else if (tf != NULL) {
515 instr = db_get_value(pc, 4, false);
516 if ((instr & 0xffffff) == 0x00e58955) {
517 /* pushl %ebp; movl %esp, %ebp */
518 actframe = (void *)(get_esp(tf) - 4);
519 } else if ((instr & 0xffff) == 0x0000e589) {
520 /* movl %esp, %ebp */
521 actframe = (void *)get_esp(tf);
522 if (tf->tf_ebp == 0) {
523 /* Fake frame better. */
526 } else if ((instr & 0xff) == 0x000000c3) {
528 actframe = (void *)(get_esp(tf) - 4);
529 } else if (offset == 0) {
530 /* Probably an assembler symbol. */
531 actframe = (void *)(get_esp(tf) - 4);
533 } else if (strcmp(name, "fork_trampoline") == 0) {
535 * Don't try to walk back on a stack for a
536 * process that hasn't actually been run yet.
538 db_print_stack_entry(name, 0, 0, 0, pc,
544 argp = &actframe->f_arg0;
546 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
549 narg = db_numargs(frame);
552 db_print_stack_entry(name, narg, argnp, argp, pc, actframe);
554 if (actframe != frame) {
555 /* `frame' belongs to caller. */
557 db_get_value((int)&actframe->f_retaddr, 4, false);
561 db_nextframe(&frame, &pc, td);
565 * 'frame' can be null here, either because it was initially
566 * null or because db_nextframe() found no frame.
567 * db_nextframe() may also have found a non-kernel frame.
568 * !INKERNEL() classifies both. Stop tracing if either,
569 * after printing the pc if it is the kernel.
571 if (frame == NULL || frame <= actframe) {
572 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
573 db_symbol_values(sym, &name, NULL);
574 db_print_stack_entry(name, 0, 0, 0, pc, frame);
585 struct i386_frame *frame;
589 __asm __volatile("movl %%ebp,%0" : "=r" (ebp));
590 frame = (struct i386_frame *)ebp;
591 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, false);
592 frame = frame->f_frame;
593 db_backtrace(curthread, NULL, frame, callpc, 0, -1);
597 db_trace_thread(struct thread *thr, int count)
600 struct trapframe *tf;
602 ctx = kdb_thr_ctx(thr);
603 tf = thr == kdb_thread ? kdb_frame : NULL;
604 return (db_backtrace(thr, tf, (struct i386_frame *)ctx->pcb_ebp,
605 ctx->pcb_eip, ctx->pcb_esp, count));
609 i386_set_watch(watchnum, watchaddr, size, access, d)
611 unsigned int watchaddr;
618 if (watchnum == -1) {
619 for (i = 0; i < 4; i++)
620 if (!DBREG_DR7_ENABLED(d->dr[7], i))
630 size = 1; /* size must be 1 for an execution breakpoint */
632 case DBREG_DR7_WRONLY:
640 * we can watch a 1, 2, or 4 byte sized location
644 len = DBREG_DR7_LEN_1;
647 len = DBREG_DR7_LEN_2;
650 len = DBREG_DR7_LEN_4;
656 /* clear the bits we are about to affect */
657 d->dr[7] &= ~DBREG_DR7_MASK(watchnum);
659 /* set drN register to the address, N=watchnum */
660 DBREG_DRX(d, watchnum) = watchaddr;
662 /* enable the watchpoint */
663 d->dr[7] |= DBREG_DR7_SET(watchnum, len, access,
664 DBREG_DR7_GLOBAL_ENABLE);
671 i386_clr_watch(watchnum, d)
676 if (watchnum < 0 || watchnum >= 4)
679 d->dr[7] &= ~DBREG_DR7_MASK(watchnum);
680 DBREG_DRX(d, watchnum) = 0;
687 db_md_set_watchpoint(addr, size)
694 fill_dbregs(NULL, &d);
697 for(i = 0; i < 4; i++) {
698 if (!DBREG_DR7_ENABLED(d.dr[7], i))
702 if (avail * 4 < size)
705 for (i = 0; i < 4 && (size > 0); i++) {
706 if (!DBREG_DR7_ENABLED(d.dr[7], i)) {
711 i386_set_watch(i, addr, wsize,
712 DBREG_DR7_WRONLY, &d);
718 set_dbregs(NULL, &d);
725 db_md_clr_watchpoint(addr, size)
732 fill_dbregs(NULL, &d);
734 for(i = 0; i < 4; i++) {
735 if (DBREG_DR7_ENABLED(d.dr[7], i)) {
736 if ((DBREG_DRX((&d), i) >= addr) &&
737 (DBREG_DRX((&d), i) < addr+size))
738 i386_clr_watch(i, &d);
743 set_dbregs(NULL, &d);
754 case DBREG_DR7_EXEC : return "execute"; break;
755 case DBREG_DR7_RDWR : return "read/write"; break;
756 case DBREG_DR7_WRONLY : return "write"; break;
757 default : return "invalid"; break;
763 db_md_list_watchpoints(void)
768 fill_dbregs(NULL, &d);
770 db_printf("\nhardware watchpoints:\n");
771 db_printf(" watch status type len address\n");
772 db_printf(" ----- -------- ---------- --- ----------\n");
773 for (i = 0; i < 4; i++) {
774 if (DBREG_DR7_ENABLED(d.dr[7], i)) {
775 type = DBREG_DR7_ACCESS(d.dr[7], i);
776 len = DBREG_DR7_LEN(d.dr[7], i);
777 db_printf(" %-5d %-8s %10s %3d ",
778 i, "enabled", watchtype_str(type), len + 1);
779 db_printsym((db_addr_t)DBREG_DRX(&d, i), DB_STGY_ANY);
782 db_printf(" %-5d disabled\n", i);
786 db_printf("\ndebug register values:\n");
787 for (i = 0; i < 8; i++)
788 if (i != 4 && i != 5)
789 db_printf(" dr%d 0x%08x\n", i, DBREG_DRX(&d, i));