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_frame;
60 static db_varfcn_t db_rsp;
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 },
70 { "ds", DB_OFFSET(tf_ds), db_frame },
71 { "es", DB_OFFSET(tf_es), db_frame },
72 { "fs", DB_OFFSET(tf_fs), db_frame },
73 { "gs", DB_OFFSET(tf_gs), db_frame },
75 { "ss", NULL, db_ss },
76 { "rax", DB_OFFSET(tf_rax), db_frame },
77 { "rcx", DB_OFFSET(tf_rcx), db_frame },
78 { "rdx", DB_OFFSET(tf_rdx), db_frame },
79 { "rbx", DB_OFFSET(tf_rbx), db_frame },
80 { "rsp", NULL, db_rsp },
81 { "rbp", DB_OFFSET(tf_rbp), db_frame },
82 { "rsi", DB_OFFSET(tf_rsi), db_frame },
83 { "rdi", DB_OFFSET(tf_rdi), db_frame },
84 { "r8", DB_OFFSET(tf_r8), db_frame },
85 { "r9", DB_OFFSET(tf_r9), db_frame },
86 { "r10", DB_OFFSET(tf_r10), db_frame },
87 { "r11", DB_OFFSET(tf_r11), db_frame },
88 { "r12", DB_OFFSET(tf_r12), db_frame },
89 { "r13", DB_OFFSET(tf_r13), db_frame },
90 { "r14", DB_OFFSET(tf_r14), db_frame },
91 { "r15", DB_OFFSET(tf_r15), db_frame },
92 { "rip", DB_OFFSET(tf_rip), db_frame },
93 { "rflags", DB_OFFSET(tf_rflags), db_frame },
94 { "dr0", NULL, db_dr0 },
95 { "dr1", NULL, db_dr1 },
96 { "dr2", NULL, db_dr2 },
97 { "dr3", NULL, db_dr3 },
98 { "dr4", NULL, db_dr4 },
99 { "dr5", NULL, db_dr5 },
100 { "dr6", NULL, db_dr6 },
101 { "dr7", NULL, db_dr7 },
103 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
105 #define DB_DRX_FUNC(reg) \
107 db_ ## reg (vp, valuep, op) \
108 struct db_variable *vp; \
109 db_expr_t * valuep; \
112 if (op == DB_VAR_GET) \
113 *valuep = r ## reg (); \
115 load_ ## reg (*valuep); \
129 get_rsp(struct trapframe *tf)
131 return ((ISPL(tf->tf_cs)) ? tf->tf_rsp :
132 (db_expr_t)tf + offsetof(struct trapframe, tf_rsp));
136 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
140 if (kdb_frame == NULL)
143 reg = (long *)((uintptr_t)kdb_frame + (db_expr_t)vp->valuep);
144 if (op == DB_VAR_GET)
152 db_rsp(struct db_variable *vp, db_expr_t *valuep, int op)
155 if (kdb_frame == NULL)
158 if (op == DB_VAR_GET)
159 *valuep = get_rsp(kdb_frame);
160 else if (ISPL(kdb_frame->tf_cs))
161 kdb_frame->tf_rsp = *valuep;
166 db_ss(struct db_variable *vp, db_expr_t *valuep, int op)
169 if (kdb_frame == NULL)
172 if (op == DB_VAR_GET)
173 *valuep = (ISPL(kdb_frame->tf_cs)) ? kdb_frame->tf_ss : rss();
174 else if (ISPL(kdb_frame->tf_cs))
175 kdb_frame->tf_ss = *valuep;
182 #define INKERNEL(va) (((va) >= DMAP_MIN_ADDRESS && (va) < DMAP_MAX_ADDRESS) \
183 || ((va) >= KERNBASE && (va) < VM_MAX_KERNEL_ADDRESS))
186 struct amd64_frame *f_frame;
196 static void db_nextframe(struct amd64_frame **, db_addr_t *, struct thread *);
197 static int db_numargs(struct amd64_frame *);
198 static void db_print_stack_entry(const char *, int, char **, long *, db_addr_t);
199 static void decode_syscall(int, struct thread *);
201 static char * watchtype_str(int type);
202 int amd64_set_watch(int watchnum, unsigned long watchaddr, int size,
203 int access, struct dbreg * d);
204 int amd64_clr_watch(int watchnum, struct dbreg * d);
207 * Figure out how many arguments were passed into the frame at "fp".
211 struct amd64_frame *fp;
214 return (0); /* regparm, needs dwarf2 info */
220 argp = (long *)db_get_value((long)&fp->f_retaddr, 8, FALSE);
222 * XXX etext is wrong for LKMs. We should attempt to interpret
223 * the instruction at the return address in all cases. This
224 * may require better fault handling.
226 if (argp < (long *)btext || argp >= (long *)etext) {
229 inst = db_get_value((long)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;
242 db_print_stack_entry(name, narg, argnp, argp, callpc)
249 db_printf("%s(", name);
253 db_printf("%s=", *argnp++);
254 db_printf("%lr", (long)db_get_value((long)argp, 8, FALSE));
261 db_printsym(callpc, DB_STGY_PROC);
266 decode_syscall(int number, struct thread *td)
274 db_printf(" (%d", number);
275 p = (td != NULL) ? td->td_proc : NULL;
276 if (p != NULL && 0 <= number && number < p->p_sysent->sv_size) {
277 f = p->p_sysent->sv_table[number].sy_call;
278 sym = db_search_symbol((db_addr_t)f, DB_STGY_ANY, &diff);
279 if (sym != DB_SYM_NULL && diff == 0) {
280 db_symbol_values(sym, &symname, NULL);
281 db_printf(", %s, %s", p->p_sysent->sv_name, symname);
288 * Figure out the next frame up in the call stack.
291 db_nextframe(struct amd64_frame **fp, db_addr_t *ip, struct thread *td)
293 struct trapframe *tf;
300 rip = db_get_value((long) &(*fp)->f_retaddr, 8, FALSE);
301 rbp = db_get_value((long) &(*fp)->f_frame, 8, FALSE);
304 * Figure out frame type. We look at the address just before
305 * the saved instruction pointer as the saved EIP is after the
306 * call function, and if the function being called is marked as
307 * dead (such as panic() at the end of dblfault_handler()), then
308 * the instruction at the saved EIP will be part of a different
309 * function (syscall() in this example) rather than the one that
310 * actually made the call.
313 sym = db_search_symbol(rip - 1, DB_STGY_ANY, &offset);
314 db_symbol_values(sym, &name, NULL);
316 if (strcmp(name, "calltrap") == 0 ||
317 strcmp(name, "fork_trampoline") == 0 ||
318 strcmp(name, "nmi_calltrap") == 0)
320 else if (strncmp(name, "Xatpic_intr", 11) == 0 ||
321 strncmp(name, "Xapic_isr", 9) == 0 ||
322 strcmp(name, "Xtimerint") == 0 ||
323 strcmp(name, "Xipi_intr_bitmap_handler") == 0 ||
324 strcmp(name, "Xcpustop") == 0 ||
325 strcmp(name, "Xrendezvous") == 0)
326 frame_type = INTERRUPT;
327 else if (strcmp(name, "Xfast_syscall") == 0)
328 frame_type = SYSCALL;
332 * Normal frames need no special processing.
334 if (frame_type == NORMAL) {
335 *ip = (db_addr_t) rip;
336 *fp = (struct amd64_frame *) rbp;
340 db_print_stack_entry(name, 0, 0, 0, rip);
343 * Point to base of trapframe which is just above the
346 tf = (struct trapframe *)((long)*fp + 16);
348 if (INKERNEL((long) tf)) {
352 switch (frame_type) {
354 db_printf("--- trap %#lr", tf->tf_trapno);
357 db_printf("--- syscall");
358 decode_syscall(tf->tf_rax, td);
361 db_printf("--- interrupt");
364 panic("The moon has moved again.");
366 db_printf(", rip = %#lr, rsp = %#lr, rbp = %#lr ---\n", rip,
370 *ip = (db_addr_t) rip;
371 *fp = (struct amd64_frame *) rbp;
375 db_backtrace(struct thread *td, struct trapframe *tf,
376 struct amd64_frame *frame, db_addr_t pc, int count)
378 struct amd64_frame *actframe;
380 char *argnames[MAXNARG], **argnp = NULL;
393 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
394 while (count-- && !quit) {
395 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
396 db_symbol_values(sym, &name, NULL);
399 * Attempt to determine a (possibly fake) frame that gives
400 * the caller's pc. It may differ from `frame' if the
401 * current function never sets up a standard frame or hasn't
402 * set one up yet or has just discarded one. The last two
403 * cases can be guessed fairly reliably for code generated
404 * by gcc. The first case is too much trouble to handle in
405 * general because the amount of junk on the stack depends
406 * on the pc (the special handling of "calltrap", etc. in
407 * db_nextframe() works because the `next' pc is special).
414 instr = db_get_value(pc, 4, FALSE);
415 if ((instr & 0xffffffff) == 0xe5894855) {
416 /* pushq %rbp; movq %rsp, %rbp */
417 actframe = (void *)(get_rsp(tf) - 8);
418 } else if ((instr & 0xffffff) == 0xe58948) {
419 /* movq %rsp, %rbp */
420 actframe = (void *)get_rsp(tf);
421 if (tf->tf_rbp == 0) {
422 /* Fake frame better. */
425 } else if ((instr & 0xff) == 0xc3) {
427 actframe = (void *)(get_rsp(tf) - 8);
428 } else if (offset == 0) {
429 /* Probably an assembler symbol. */
430 actframe = (void *)(get_rsp(tf) - 8);
432 } else if (strcmp(name, "fork_trampoline") == 0) {
434 * Don't try to walk back on a stack for a
435 * process that hasn't actually been run yet.
437 db_print_stack_entry(name, 0, 0, 0, pc);
443 argp = &actframe->f_arg0;
445 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
448 narg = db_numargs(frame);
451 db_print_stack_entry(name, narg, argnp, argp, pc);
453 if (actframe != frame) {
454 /* `frame' belongs to caller. */
456 db_get_value((long)&actframe->f_retaddr, 8, FALSE);
460 db_nextframe(&frame, &pc, td);
462 if (INKERNEL((long)pc) && !INKERNEL((long)frame)) {
463 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
464 db_symbol_values(sym, &name, NULL);
465 db_print_stack_entry(name, 0, 0, 0, pc);
468 if (!INKERNEL((long) frame)) {
479 struct amd64_frame *frame;
483 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
484 frame = (struct amd64_frame *)rbp;
485 callpc = (db_addr_t)db_get_value((long)&frame->f_retaddr, 8, FALSE);
486 frame = frame->f_frame;
487 db_backtrace(curthread, NULL, frame, callpc, -1);
491 db_trace_thread(struct thread *thr, int count)
495 ctx = kdb_thr_ctx(thr);
496 return (db_backtrace(thr, NULL, (struct amd64_frame *)ctx->pcb_rbp,
497 ctx->pcb_rip, count));
501 stack_save(struct stack *st)
503 struct amd64_frame *frame;
508 __asm __volatile("movq %%rbp,%0" : "=r" (rbp));
509 frame = (struct amd64_frame *)rbp;
511 if (!INKERNEL((long)frame))
513 callpc = frame->f_retaddr;
514 if (!INKERNEL(callpc))
516 if (stack_put(st, callpc) == -1)
518 if (frame->f_frame <= frame ||
519 (vm_offset_t)frame->f_frame >=
520 (vm_offset_t)rbp + KSTACK_PAGES * PAGE_SIZE)
522 frame = frame->f_frame;
527 amd64_set_watch(watchnum, watchaddr, size, access, d)
529 unsigned long watchaddr;
537 if (watchnum == -1) {
538 for (i = 0, mask = 0x3; i < 4; i++, mask <<= 2)
539 if ((d->dr[7] & mask) == 0)
549 size = 1; /* size must be 1 for an execution breakpoint */
551 case DBREG_DR7_WRONLY:
554 default : return (-1);
558 * we can watch a 1, 2, or 4 byte sized location
561 case 1 : mask = 0x00; break;
562 case 2 : mask = 0x01 << 2; break;
563 case 4 : mask = 0x03 << 2; break;
564 default : return (-1);
569 /* clear the bits we are about to affect */
570 d->dr[7] &= ~((0x3 << (watchnum*2)) | (0x0f << (watchnum*4+16)));
572 /* set drN register to the address, N=watchnum */
573 DBREG_DRX(d,watchnum) = watchaddr;
575 /* enable the watchpoint */
576 d->dr[7] |= (0x2 << (watchnum*2)) | (mask << (watchnum*4+16));
583 amd64_clr_watch(watchnum, d)
588 if (watchnum < 0 || watchnum >= 4)
591 d->dr[7] = d->dr[7] & ~((0x3 << (watchnum*2)) | (0x0f << (watchnum*4+16)));
592 DBREG_DRX(d,watchnum) = 0;
599 db_md_set_watchpoint(addr, size)
607 fill_dbregs(NULL, &d);
611 if ((d.dr[7] & (3 << (i*2))) == 0)
618 for (i=0; i<4 && (size != 0); i++) {
619 if ((d.dr[7] & (3<<(i*2))) == 0) {
626 amd64_set_watch(i, addr, wsize,
627 DBREG_DR7_WRONLY, &d);
633 set_dbregs(NULL, &d);
640 db_md_clr_watchpoint(addr, size)
647 fill_dbregs(NULL, &d);
650 if (d.dr[7] & (3 << (i*2))) {
651 if ((DBREG_DRX((&d), i) >= addr) &&
652 (DBREG_DRX((&d), i) < addr+size))
653 amd64_clr_watch(i, &d);
658 set_dbregs(NULL, &d);
670 case DBREG_DR7_EXEC : return "execute"; break;
671 case DBREG_DR7_RDWR : return "read/write"; break;
672 case DBREG_DR7_WRONLY : return "write"; break;
673 default : return "invalid"; break;
679 db_md_list_watchpoints()
684 fill_dbregs(NULL, &d);
686 db_printf("\nhardware watchpoints:\n");
687 db_printf(" watch status type len address\n");
688 db_printf(" ----- -------- ---------- --- ----------\n");
689 for (i=0; i<4; i++) {
690 if (d.dr[7] & (0x03 << (i*2))) {
692 type = (d.dr[7] >> (16+(i*4))) & 3;
693 len = (d.dr[7] >> (16+(i*4)+2)) & 3;
694 db_printf(" %-5d %-8s %10s %3d 0x%016lx\n",
695 i, "enabled", watchtype_str(type),
696 len + 1, DBREG_DRX((&d), i));
699 db_printf(" %-5d disabled\n", i);
703 db_printf("\ndebug register values:\n");
704 for (i=0; i<8; i++) {
705 db_printf(" dr%d 0x%016lx\n", i, DBREG_DRX((&d), i));