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/stack.h>
35 #include <sys/sysent.h>
37 #include <machine/cpu.h>
38 #include <machine/md_var.h>
39 #include <machine/pcb.h>
40 #include <machine/reg.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_dr0;
52 static db_varfcn_t db_dr1;
53 static db_varfcn_t db_dr2;
54 static db_varfcn_t db_dr3;
55 static db_varfcn_t db_dr4;
56 static db_varfcn_t db_dr5;
57 static db_varfcn_t db_dr6;
58 static db_varfcn_t db_dr7;
59 static db_varfcn_t db_esp;
60 static db_varfcn_t db_frame;
61 static db_varfcn_t db_ss;
64 * Machine register set.
66 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x)
67 struct db_variable db_regs[] = {
68 { "cs", DB_OFFSET(tf_cs), db_frame },
69 { "ds", DB_OFFSET(tf_ds), db_frame },
70 { "es", DB_OFFSET(tf_es), db_frame },
71 { "fs", DB_OFFSET(tf_fs), db_frame },
72 { "ss", NULL, db_ss },
73 { "eax", DB_OFFSET(tf_eax), db_frame },
74 { "ecx", DB_OFFSET(tf_ecx), db_frame },
75 { "edx", DB_OFFSET(tf_edx), db_frame },
76 { "ebx", DB_OFFSET(tf_ebx), db_frame },
77 { "esp", NULL, db_esp },
78 { "ebp", DB_OFFSET(tf_ebp), db_frame },
79 { "esi", DB_OFFSET(tf_esi), db_frame },
80 { "edi", DB_OFFSET(tf_edi), db_frame },
81 { "eip", DB_OFFSET(tf_eip), db_frame },
82 { "efl", DB_OFFSET(tf_eflags), db_frame },
83 #define DB_N_SHOW_REGS 15 /* Don't show registers after here. */
84 { "dr0", NULL, db_dr0 },
85 { "dr1", NULL, db_dr1 },
86 { "dr2", NULL, db_dr2 },
87 { "dr3", NULL, db_dr3 },
88 { "dr4", NULL, db_dr4 },
89 { "dr5", NULL, db_dr5 },
90 { "dr6", NULL, db_dr6 },
91 { "dr7", NULL, db_dr7 },
93 struct db_variable *db_eregs = db_regs + DB_N_SHOW_REGS;
95 #define DB_DRX_FUNC(reg) \
97 db_ ## reg (vp, valuep, op) \
98 struct db_variable *vp; \
102 if (op == DB_VAR_GET) \
103 *valuep = r ## reg (); \
105 load_ ## reg (*valuep); \
119 get_esp(struct trapframe *tf)
121 return ((ISPL(tf->tf_cs)) ? tf->tf_esp :
122 (db_expr_t)tf + (uintptr_t)DB_OFFSET(tf_esp));
126 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
130 if (kdb_frame == NULL)
133 reg = (int *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
134 if (op == DB_VAR_GET)
142 db_esp(struct db_variable *vp, db_expr_t *valuep, int op)
145 if (kdb_frame == NULL)
148 if (op == DB_VAR_GET)
149 *valuep = get_esp(kdb_frame);
150 else if (ISPL(kdb_frame->tf_cs))
151 kdb_frame->tf_esp = *valuep;
156 db_ss(struct db_variable *vp, db_expr_t *valuep, int op)
159 if (kdb_frame == NULL)
162 if (op == DB_VAR_GET)
163 *valuep = (ISPL(kdb_frame->tf_cs)) ? kdb_frame->tf_ss : rss();
164 else if (ISPL(kdb_frame->tf_cs))
165 kdb_frame->tf_ss = *valuep;
172 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK && \
173 ((vm_offset_t)(va)) < VM_MAX_KERNEL_ADDRESS)
176 struct i386_frame *f_frame;
185 #define DOUBLE_FAULT 4
186 #define TRAP_INTERRUPT 5
188 static void db_nextframe(struct i386_frame **, db_addr_t *, struct thread *);
189 static int db_numargs(struct i386_frame *);
190 static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
191 static void decode_syscall(int, struct thread *);
193 static const char * watchtype_str(int type);
194 int i386_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
196 int i386_clr_watch(int watchnum, struct dbreg *d);
199 * Figure out how many arguments were passed into the frame at "fp".
203 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(name, narg, argnp, argp, callpc)
241 int n = narg >= 0 ? narg : 5;
243 db_printf("%s(", name);
246 db_printf("%s=", *argnp++);
247 db_printf("%r", db_get_value((int)argp, 4, FALSE));
255 db_printsym(callpc, DB_STGY_PROC);
260 decode_syscall(int number, struct thread *td)
268 db_printf(" (%d", number);
269 p = (td != NULL) ? td->td_proc : NULL;
270 if (p != NULL && 0 <= number && number < p->p_sysent->sv_size) {
271 f = p->p_sysent->sv_table[number].sy_call;
272 sym = db_search_symbol((db_addr_t)f, DB_STGY_ANY, &diff);
273 if (sym != DB_SYM_NULL && diff == 0) {
274 db_symbol_values(sym, &symname, NULL);
275 db_printf(", %s, %s", p->p_sysent->sv_name, symname);
282 * Figure out the next frame up in the call stack.
285 db_nextframe(struct i386_frame **fp, db_addr_t *ip, struct thread *td)
287 struct trapframe *tf;
294 eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
295 ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
298 * Figure out frame type. We look at the address just before
299 * the saved instruction pointer as the saved EIP is after the
300 * call function, and if the function being called is marked as
301 * dead (such as panic() at the end of dblfault_handler()), then
302 * the instruction at the saved EIP will be part of a different
303 * function (syscall() in this example) rather than the one that
304 * actually made the call.
307 sym = db_search_symbol(eip - 1, DB_STGY_ANY, &offset);
308 db_symbol_values(sym, &name, NULL);
310 if (strcmp(name, "calltrap") == 0 ||
311 strcmp(name, "fork_trampoline") == 0)
313 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
314 strncmp(name, "Xapic_isr", 9) == 0)
315 frame_type = INTERRUPT;
316 else if (strcmp(name, "Xlcall_syscall") == 0 ||
317 strcmp(name, "Xint0x80_syscall") == 0)
318 frame_type = SYSCALL;
319 else if (strcmp(name, "dblfault_handler") == 0)
320 frame_type = DOUBLE_FAULT;
321 /* XXX: These are interrupts with trap frames. */
322 else if (strcmp(name, "Xtimerint") == 0 ||
323 strcmp(name, "Xcpustop") == 0 ||
324 strcmp(name, "Xrendezvous") == 0 ||
325 strcmp(name, "Xipi_intr_bitmap_handler") == 0 ||
326 strcmp(name, "Xlazypmap") == 0)
327 frame_type = TRAP_INTERRUPT;
331 * Normal frames need no special processing.
333 if (frame_type == NORMAL) {
334 *ip = (db_addr_t) eip;
335 *fp = (struct i386_frame *) ebp;
339 db_print_stack_entry(name, 0, 0, 0, eip);
342 * For a double fault, we have to snag the values from the
343 * previous TSS since a double fault uses a task gate to
344 * switch to a known good state.
346 if (frame_type == DOUBLE_FAULT) {
347 esp = PCPU_GET(common_tss.tss_esp);
348 eip = PCPU_GET(common_tss.tss_eip);
349 ebp = PCPU_GET(common_tss.tss_ebp);
351 "--- trap 0x17, eip = %#r, esp = %#r, ebp = %#r ---\n",
353 *ip = (db_addr_t) eip;
354 *fp = (struct i386_frame *) ebp;
359 * Point to base of trapframe which is just above the
362 if (frame_type == INTERRUPT)
363 tf = (struct trapframe *)((int)*fp + 16);
365 tf = (struct trapframe *)((int)*fp + 12);
367 if (INKERNEL((int) tf)) {
371 switch (frame_type) {
373 db_printf("--- trap %#r", tf->tf_trapno);
376 db_printf("--- syscall");
377 decode_syscall(tf->tf_eax, td);
381 db_printf("--- interrupt");
384 panic("The moon has moved again.");
386 db_printf(", eip = %#r, esp = %#r, ebp = %#r ---\n", eip,
390 *ip = (db_addr_t) eip;
391 *fp = (struct i386_frame *) ebp;
395 db_backtrace(struct thread *td, struct trapframe *tf, struct i386_frame *frame,
396 db_addr_t pc, int count)
398 struct i386_frame *actframe;
400 char *argnames[MAXNARG], **argnp = NULL;
409 * If an indirect call via an invalid pointer caused a trap,
410 * %pc contains the invalid address while the return address
411 * of the unlucky caller has been saved by CPU on the stack
412 * just before the trap frame. In this case, try to recover
413 * the caller's address so that the first frame is assigned
414 * to the right spot in the right function, for that is where
415 * the failure actually happened.
417 * This trick depends on the fault address stashed in tf_err
418 * by trap_fatal() before entering KDB.
420 if (kdb_frame && pc == kdb_frame->tf_err) {
422 * Find where the trap frame actually ends.
423 * It won't contain tf_esp or tf_ss unless crossing rings.
425 if (ISPL(kdb_frame->tf_cs))
426 instr = (int)(kdb_frame + 1);
428 instr = (int)&kdb_frame->tf_esp;
429 pc = db_get_value(instr, 4, FALSE);
436 while (count-- && !db_pager_quit) {
437 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
438 db_symbol_values(sym, &name, NULL);
441 * Attempt to determine a (possibly fake) frame that gives
442 * the caller's pc. It may differ from `frame' if the
443 * current function never sets up a standard frame or hasn't
444 * set one up yet or has just discarded one. The last two
445 * cases can be guessed fairly reliably for code generated
446 * by gcc. The first case is too much trouble to handle in
447 * general because the amount of junk on the stack depends
448 * on the pc (the special handling of "calltrap", etc. in
449 * db_nextframe() works because the `next' pc is special).
454 instr = db_get_value(pc, 4, FALSE);
455 if ((instr & 0xffffff) == 0x00e58955) {
456 /* pushl %ebp; movl %esp, %ebp */
457 actframe = (void *)(get_esp(tf) - 4);
458 } else if ((instr & 0xffff) == 0x0000e589) {
459 /* movl %esp, %ebp */
460 actframe = (void *)get_esp(tf);
461 if (tf->tf_ebp == 0) {
462 /* Fake frame better. */
465 } else if ((instr & 0xff) == 0x000000c3) {
467 actframe = (void *)(get_esp(tf) - 4);
468 } else if (offset == 0) {
469 /* Probably an assembler symbol. */
470 actframe = (void *)(get_esp(tf) - 4);
472 } else if (strcmp(name, "fork_trampoline") == 0) {
474 * Don't try to walk back on a stack for a
475 * process that hasn't actually been run yet.
477 db_print_stack_entry(name, 0, 0, 0, pc);
483 argp = &actframe->f_arg0;
485 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
488 narg = db_numargs(frame);
491 db_print_stack_entry(name, narg, argnp, argp, pc);
493 if (actframe != frame) {
494 /* `frame' belongs to caller. */
496 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
500 db_nextframe(&frame, &pc, td);
502 if (INKERNEL((int)pc) && !INKERNEL((int) frame)) {
503 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
504 db_symbol_values(sym, &name, NULL);
505 db_print_stack_entry(name, 0, 0, 0, pc);
508 if (!INKERNEL((int) frame)) {
519 struct i386_frame *frame;
523 __asm __volatile("movl %%ebp,%0" : "=r" (ebp));
524 frame = (struct i386_frame *)ebp;
525 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, FALSE);
526 frame = frame->f_frame;
527 db_backtrace(curthread, NULL, frame, callpc, -1);
531 db_trace_thread(struct thread *thr, int count)
535 ctx = kdb_thr_ctx(thr);
536 return (db_backtrace(thr, NULL, (struct i386_frame *)ctx->pcb_ebp,
537 ctx->pcb_eip, count));
541 stack_save(struct stack *st)
543 struct i386_frame *frame;
548 __asm __volatile("movl %%ebp,%0" : "=r" (ebp));
549 frame = (struct i386_frame *)ebp;
551 if (!INKERNEL(frame))
553 callpc = frame->f_retaddr;
554 if (!INKERNEL(callpc))
556 if (stack_put(st, callpc) == -1)
558 if (frame->f_frame <= frame ||
559 (vm_offset_t)frame->f_frame >=
560 (vm_offset_t)ebp + KSTACK_PAGES * PAGE_SIZE)
562 frame = frame->f_frame;
567 i386_set_watch(watchnum, watchaddr, size, access, d)
569 unsigned int watchaddr;
576 if (watchnum == -1) {
577 for (i = 0; i < 4; i++)
578 if (!DBREG_DR7_ENABLED(d->dr[7], i))
588 size = 1; /* size must be 1 for an execution breakpoint */
590 case DBREG_DR7_WRONLY:
598 * we can watch a 1, 2, or 4 byte sized location
602 len = DBREG_DR7_LEN_1;
605 len = DBREG_DR7_LEN_2;
608 len = DBREG_DR7_LEN_4;
614 /* clear the bits we are about to affect */
615 d->dr[7] &= ~DBREG_DR7_MASK(watchnum);
617 /* set drN register to the address, N=watchnum */
618 DBREG_DRX(d, watchnum) = watchaddr;
620 /* enable the watchpoint */
621 d->dr[7] |= DBREG_DR7_SET(watchnum, len, access,
622 DBREG_DR7_GLOBAL_ENABLE);
629 i386_clr_watch(watchnum, d)
634 if (watchnum < 0 || watchnum >= 4)
637 d->dr[7] &= ~DBREG_DR7_MASK(watchnum);
638 DBREG_DRX(d, watchnum) = 0;
645 db_md_set_watchpoint(addr, size)
652 fill_dbregs(NULL, &d);
655 for(i = 0; i < 4; i++) {
656 if (!DBREG_DR7_ENABLED(d.dr[7], i))
660 if (avail * 4 < size)
663 for (i = 0; i < 4 && (size > 0); i++) {
664 if (!DBREG_DR7_ENABLED(d.dr[7], i)) {
669 i386_set_watch(i, addr, wsize,
670 DBREG_DR7_WRONLY, &d);
676 set_dbregs(NULL, &d);
683 db_md_clr_watchpoint(addr, size)
690 fill_dbregs(NULL, &d);
692 for(i = 0; i < 4; i++) {
693 if (DBREG_DR7_ENABLED(d.dr[7], i)) {
694 if ((DBREG_DRX((&d), i) >= addr) &&
695 (DBREG_DRX((&d), i) < addr+size))
696 i386_clr_watch(i, &d);
701 set_dbregs(NULL, &d);
712 case DBREG_DR7_EXEC : return "execute"; break;
713 case DBREG_DR7_RDWR : return "read/write"; break;
714 case DBREG_DR7_WRONLY : return "write"; break;
715 default : return "invalid"; break;
721 db_md_list_watchpoints()
726 fill_dbregs(NULL, &d);
728 db_printf("\nhardware watchpoints:\n");
729 db_printf(" watch status type len address\n");
730 db_printf(" ----- -------- ---------- --- ----------\n");
731 for (i = 0; i < 4; i++) {
732 if (DBREG_DR7_ENABLED(d.dr[7], i)) {
733 type = DBREG_DR7_ACCESS(d.dr[7], i);
734 len = DBREG_DR7_LEN(d.dr[7], i);
735 db_printf(" %-5d %-8s %10s %3d ",
736 i, "enabled", watchtype_str(type), len + 1);
737 db_printsym((db_addr_t)DBREG_DRX((&d), i), DB_STGY_ANY);
740 db_printf(" %-5d disabled\n", i);
744 db_printf("\ndebug register values:\n");
745 for (i = 0; i < 8; i++) {
746 db_printf(" dr%d 0x%08x\n", i, DBREG_DRX((&d), i));