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 "opt_compat.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/stack.h>
37 #include <sys/sysent.h>
39 #include <machine/cpu.h>
40 #include <machine/md_var.h>
41 #include <machine/pcb.h>
42 #include <machine/reg.h>
43 #include <machine/stack.h>
46 #include <vm/vm_param.h>
50 #include <ddb/db_access.h>
51 #include <ddb/db_sym.h>
52 #include <ddb/db_variables.h>
54 static db_varfcn_t db_dr0;
55 static db_varfcn_t db_dr1;
56 static db_varfcn_t db_dr2;
57 static db_varfcn_t db_dr3;
58 static db_varfcn_t db_dr4;
59 static db_varfcn_t db_dr5;
60 static db_varfcn_t db_dr6;
61 static db_varfcn_t db_dr7;
62 static db_varfcn_t db_frame;
63 static db_varfcn_t db_rsp;
64 static db_varfcn_t db_ss;
67 * Machine register set.
69 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x)
70 struct db_variable db_regs[] = {
71 { "cs", DB_OFFSET(tf_cs), db_frame },
73 { "ds", DB_OFFSET(tf_ds), db_frame },
74 { "es", DB_OFFSET(tf_es), db_frame },
75 { "fs", DB_OFFSET(tf_fs), db_frame },
76 { "gs", DB_OFFSET(tf_gs), db_frame },
78 { "ss", NULL, db_ss },
79 { "rax", DB_OFFSET(tf_rax), db_frame },
80 { "rcx", DB_OFFSET(tf_rcx), db_frame },
81 { "rdx", DB_OFFSET(tf_rdx), db_frame },
82 { "rbx", DB_OFFSET(tf_rbx), db_frame },
83 { "rsp", NULL, db_rsp },
84 { "rbp", DB_OFFSET(tf_rbp), db_frame },
85 { "rsi", DB_OFFSET(tf_rsi), db_frame },
86 { "rdi", DB_OFFSET(tf_rdi), db_frame },
87 { "r8", DB_OFFSET(tf_r8), db_frame },
88 { "r9", DB_OFFSET(tf_r9), db_frame },
89 { "r10", DB_OFFSET(tf_r10), db_frame },
90 { "r11", DB_OFFSET(tf_r11), db_frame },
91 { "r12", DB_OFFSET(tf_r12), db_frame },
92 { "r13", DB_OFFSET(tf_r13), db_frame },
93 { "r14", DB_OFFSET(tf_r14), db_frame },
94 { "r15", DB_OFFSET(tf_r15), db_frame },
95 { "rip", DB_OFFSET(tf_rip), db_frame },
96 { "rflags", DB_OFFSET(tf_rflags), db_frame },
97 #define DB_N_SHOW_REGS 20 /* Don't show registers after here. */
98 { "dr0", NULL, db_dr0 },
99 { "dr1", NULL, db_dr1 },
100 { "dr2", NULL, db_dr2 },
101 { "dr3", NULL, db_dr3 },
102 { "dr4", NULL, db_dr4 },
103 { "dr5", NULL, db_dr5 },
104 { "dr6", NULL, db_dr6 },
105 { "dr7", NULL, db_dr7 },
107 struct db_variable *db_eregs = db_regs + DB_N_SHOW_REGS;
109 #define DB_DRX_FUNC(reg) \
111 db_ ## reg (vp, valuep, op) \
112 struct db_variable *vp; \
113 db_expr_t * valuep; \
116 if (op == DB_VAR_GET) \
117 *valuep = r ## reg (); \
119 load_ ## reg (*valuep); \
133 get_rsp(struct trapframe *tf)
135 return ((ISPL(tf->tf_cs)) ? tf->tf_rsp :
136 (db_expr_t)tf + offsetof(struct trapframe, tf_rsp));
140 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
144 if (kdb_frame == NULL)
147 reg = (long *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
148 if (op == DB_VAR_GET)
156 db_rsp(struct db_variable *vp, db_expr_t *valuep, int op)
159 if (kdb_frame == NULL)
162 if (op == DB_VAR_GET)
163 *valuep = get_rsp(kdb_frame);
164 else if (ISPL(kdb_frame->tf_cs))
165 kdb_frame->tf_rsp = *valuep;
170 db_ss(struct db_variable *vp, db_expr_t *valuep, int op)
173 if (kdb_frame == NULL)
176 if (op == DB_VAR_GET)
177 *valuep = (ISPL(kdb_frame->tf_cs)) ? kdb_frame->tf_ss : rss();
178 else if (ISPL(kdb_frame->tf_cs))
179 kdb_frame->tf_ss = *valuep;
187 #define TRAP_INTERRUPT 5
189 static void db_nextframe(struct amd64_frame **, db_addr_t *, struct thread *);
190 static int db_numargs(struct amd64_frame *);
191 static void db_print_stack_entry(const char *, int, char **, long *, db_addr_t);
192 static void decode_syscall(int, struct thread *);
194 static const char * watchtype_str(int type);
195 int amd64_set_watch(int watchnum, unsigned long watchaddr, int size,
196 int access, struct dbreg *d);
197 int amd64_clr_watch(int watchnum, struct dbreg *d);
200 * Figure out how many arguments were passed into the frame at "fp".
204 struct amd64_frame *fp;
207 return (0); /* regparm, needs dwarf2 info */
213 argp = (long *)db_get_value((long)&fp->f_retaddr, 8, FALSE);
215 * XXX etext is wrong for LKMs. We should attempt to interpret
216 * the instruction at the return address in all cases. This
217 * may require better fault handling.
219 if (argp < (long *)btext || argp >= (long *)etext) {
222 inst = db_get_value((long)argp, 4, FALSE);
223 if ((inst & 0xff) == 0x59) /* popl %ecx */
225 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
226 args = ((inst >> 16) & 0xff) / 4;
235 db_print_stack_entry(name, narg, argnp, argp, callpc)
242 db_printf("%s(", name);
246 db_printf("%s=", *argnp++);
247 db_printf("%lr", (long)db_get_value((long)argp, 8, FALSE));
254 db_printsym(callpc, DB_STGY_PROC);
259 decode_syscall(int number, struct thread *td)
267 db_printf(" (%d", number);
268 p = (td != NULL) ? td->td_proc : NULL;
269 if (p != NULL && 0 <= number && number < p->p_sysent->sv_size) {
270 f = p->p_sysent->sv_table[number].sy_call;
271 sym = db_search_symbol((db_addr_t)f, DB_STGY_ANY, &diff);
272 if (sym != DB_SYM_NULL && diff == 0) {
273 db_symbol_values(sym, &symname, NULL);
274 db_printf(", %s, %s", p->p_sysent->sv_name, symname);
281 * Figure out the next frame up in the call stack.
284 db_nextframe(struct amd64_frame **fp, db_addr_t *ip, struct thread *td)
286 struct trapframe *tf;
293 rip = db_get_value((long) &(*fp)->f_retaddr, 8, FALSE);
294 rbp = db_get_value((long) &(*fp)->f_frame, 8, FALSE);
297 * Figure out frame type. We look at the address just before
298 * the saved instruction pointer as the saved EIP is after the
299 * call function, and if the function being called is marked as
300 * dead (such as panic() at the end of dblfault_handler()), then
301 * the instruction at the saved EIP will be part of a different
302 * function (syscall() in this example) rather than the one that
303 * actually made the call.
306 sym = db_search_symbol(rip - 1, DB_STGY_ANY, &offset);
307 db_symbol_values(sym, &name, NULL);
309 if (strcmp(name, "calltrap") == 0 ||
310 strcmp(name, "fork_trampoline") == 0 ||
311 strcmp(name, "nmi_calltrap") == 0 ||
312 strcmp(name, "Xdblfault") == 0)
314 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
315 strncmp(name, "Xapic_isr", 9) == 0 ||
316 strcmp(name, "Xtimerint") == 0 ||
317 strcmp(name, "Xipi_intr_bitmap_handler") == 0 ||
318 strcmp(name, "Xcpustop") == 0 ||
319 strcmp(name, "Xrendezvous") == 0)
320 frame_type = INTERRUPT;
321 else if (strcmp(name, "Xfast_syscall") == 0)
322 frame_type = SYSCALL;
324 else if (strcmp(name, "Xint0x80_syscall") == 0)
325 frame_type = SYSCALL;
327 /* XXX: These are interrupts with trap frames. */
328 else if (strcmp(name, "Xtimerint") == 0 ||
329 strcmp(name, "Xcpustop") == 0 ||
330 strcmp(name, "Xrendezvous") == 0 ||
331 strcmp(name, "Xipi_intr_bitmap_handler") == 0)
332 frame_type = TRAP_INTERRUPT;
336 * Normal frames need no special processing.
338 if (frame_type == NORMAL) {
339 *ip = (db_addr_t) rip;
340 *fp = (struct amd64_frame *) rbp;
344 db_print_stack_entry(name, 0, 0, 0, rip);
347 * Point to base of trapframe which is just above the
350 tf = (struct trapframe *)((long)*fp + 16);
352 if (INKERNEL((long) tf)) {
356 switch (frame_type) {
358 db_printf("--- trap %#lr", tf->tf_trapno);
361 db_printf("--- syscall");
362 decode_syscall(tf->tf_rax, td);
366 db_printf("--- interrupt");
369 panic("The moon has moved again.");
371 db_printf(", rip = %#lr, rsp = %#lr, rbp = %#lr ---\n", rip,
375 *ip = (db_addr_t) rip;
376 *fp = (struct amd64_frame *) rbp;
380 db_backtrace(struct thread *td, struct trapframe *tf,
381 struct amd64_frame *frame, db_addr_t pc, int count)
383 struct amd64_frame *actframe;
385 char *argnames[MAXNARG], **argnp = NULL;
397 while (count-- && !db_pager_quit) {
398 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
399 db_symbol_values(sym, &name, NULL);
402 * Attempt to determine a (possibly fake) frame that gives
403 * the caller's pc. It may differ from `frame' if the
404 * current function never sets up a standard frame or hasn't
405 * set one up yet or has just discarded one. The last two
406 * cases can be guessed fairly reliably for code generated
407 * by gcc. The first case is too much trouble to handle in
408 * general because the amount of junk on the stack depends
409 * on the pc (the special handling of "calltrap", etc. in
410 * db_nextframe() works because the `next' pc is special).
417 instr = db_get_value(pc, 4, FALSE);
418 if ((instr & 0xffffffff) == 0xe5894855) {
419 /* pushq %rbp; movq %rsp, %rbp */
420 actframe = (void *)(get_rsp(tf) - 8);
421 } else if ((instr & 0xffffff) == 0xe58948) {
422 /* movq %rsp, %rbp */
423 actframe = (void *)get_rsp(tf);
424 if (tf->tf_rbp == 0) {
425 /* Fake frame better. */
428 } else if ((instr & 0xff) == 0xc3) {
430 actframe = (void *)(get_rsp(tf) - 8);
431 } else if (offset == 0) {
432 /* Probably an assembler symbol. */
433 actframe = (void *)(get_rsp(tf) - 8);
435 } else if (strcmp(name, "fork_trampoline") == 0) {
437 * Don't try to walk back on a stack for a
438 * process that hasn't actually been run yet.
440 db_print_stack_entry(name, 0, 0, 0, pc);
446 argp = &actframe->f_arg0;
448 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
451 narg = db_numargs(frame);
454 db_print_stack_entry(name, narg, argnp, argp, pc);
456 if (actframe != frame) {
457 /* `frame' belongs to caller. */
459 db_get_value((long)&actframe->f_retaddr, 8, FALSE);
463 db_nextframe(&frame, &pc, td);
465 if (INKERNEL((long)pc) && !INKERNEL((long)frame)) {
466 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
467 db_symbol_values(sym, &name, NULL);
468 db_print_stack_entry(name, 0, 0, 0, pc);
471 if (!INKERNEL((long) frame)) {
482 struct amd64_frame *frame;
486 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
487 frame = (struct amd64_frame *)rbp;
488 callpc = (db_addr_t)db_get_value((long)&frame->f_retaddr, 8, FALSE);
489 frame = frame->f_frame;
490 db_backtrace(curthread, NULL, frame, callpc, -1);
494 db_trace_thread(struct thread *thr, int count)
498 ctx = kdb_thr_ctx(thr);
499 return (db_backtrace(thr, NULL, (struct amd64_frame *)ctx->pcb_rbp,
500 ctx->pcb_rip, count));
504 amd64_set_watch(watchnum, watchaddr, size, access, d)
506 unsigned long watchaddr;
513 if (watchnum == -1) {
514 for (i = 0; i < 4; i++)
515 if (!DBREG_DR7_ENABLED(d->dr[7], i))
525 size = 1; /* size must be 1 for an execution breakpoint */
527 case DBREG_DR7_WRONLY:
535 * we can watch a 1, 2, 4, or 8 byte sized location
539 len = DBREG_DR7_LEN_1;
542 len = DBREG_DR7_LEN_2;
545 len = DBREG_DR7_LEN_4;
548 len = DBREG_DR7_LEN_8;
554 /* clear the bits we are about to affect */
555 d->dr[7] &= ~DBREG_DR7_MASK(watchnum);
557 /* set drN register to the address, N=watchnum */
558 DBREG_DRX(d, watchnum) = watchaddr;
560 /* enable the watchpoint */
561 d->dr[7] |= DBREG_DR7_SET(watchnum, len, access,
562 DBREG_DR7_GLOBAL_ENABLE);
569 amd64_clr_watch(watchnum, d)
574 if (watchnum < 0 || watchnum >= 4)
577 d->dr[7] &= ~DBREG_DR7_MASK(watchnum);
578 DBREG_DRX(d, watchnum) = 0;
585 db_md_set_watchpoint(addr, size)
592 fill_dbregs(NULL, &d);
595 for(i = 0; i < 4; i++) {
596 if (!DBREG_DR7_ENABLED(d.dr[7], i))
600 if (avail * 8 < size)
603 for (i = 0; i < 4 && (size > 0); i++) {
604 if (!DBREG_DR7_ENABLED(d.dr[7], i)) {
605 if (size >= 8 || (avail == 1 && size > 4))
611 amd64_set_watch(i, addr, wsize,
612 DBREG_DR7_WRONLY, &d);
619 set_dbregs(NULL, &d);
626 db_md_clr_watchpoint(addr, size)
633 fill_dbregs(NULL, &d);
635 for(i = 0; i < 4; i++) {
636 if (DBREG_DR7_ENABLED(d.dr[7], i)) {
637 if ((DBREG_DRX((&d), i) >= addr) &&
638 (DBREG_DRX((&d), i) < addr+size))
639 amd64_clr_watch(i, &d);
644 set_dbregs(NULL, &d);
655 case DBREG_DR7_EXEC : return "execute"; break;
656 case DBREG_DR7_RDWR : return "read/write"; break;
657 case DBREG_DR7_WRONLY : return "write"; break;
658 default : return "invalid"; break;
664 db_md_list_watchpoints()
669 fill_dbregs(NULL, &d);
671 db_printf("\nhardware watchpoints:\n");
672 db_printf(" watch status type len address\n");
673 db_printf(" ----- -------- ---------- --- ------------------\n");
674 for (i = 0; i < 4; i++) {
675 if (DBREG_DR7_ENABLED(d.dr[7], i)) {
676 type = DBREG_DR7_ACCESS(d.dr[7], i);
677 len = DBREG_DR7_LEN(d.dr[7], i);
678 if (len == DBREG_DR7_LEN_8)
682 db_printf(" %-5d %-8s %10s %3d ",
683 i, "enabled", watchtype_str(type), len);
684 db_printsym((db_addr_t)DBREG_DRX((&d), i), DB_STGY_ANY);
687 db_printf(" %-5d disabled\n", i);
691 db_printf("\ndebug register values:\n");
692 for (i = 0; i < 8; i++) {
693 db_printf(" dr%d 0x%016lx\n", i, DBREG_DRX((&d), i));