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.
26 * $FreeBSD: src/sys/i386/i386/db_trace.c,v 1.35.2.3 2002/02/21 22:31:25 silby Exp $
27 * $DragonFly: src/sys/platform/pc64/amd64/db_trace.c,v 1.2 2007/09/24 03:24:45 yanyh Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/linker_set.h>
37 #include <machine/cpu.h>
38 #include <machine/md_var.h>
41 #include <vm/vm_param.h>
43 #include <vm/vm_map.h>
45 #include <dlfcn.h> /* DLL */
49 #include <ddb/db_access.h>
50 #include <ddb/db_sym.h>
51 #include <ddb/db_variables.h>
63 * Machine register set.
65 struct db_variable db_regs[] = {
66 { "cs", &ddb_regs.tf_cs, FCN_NULL },
67 /* { "ds", &ddb_regs.tf_ds, FCN_NULL },
68 { "es", &ddb_regs.tf_es, FCN_NULL },
69 { "fs", &ddb_regs.tf_fs, FCN_NULL },
70 { "gs", &ddb_regs.tf_gs, FCN_NULL }, */
71 { "ss", &ddb_regs.tf_ss, FCN_NULL },
72 { "rax", &ddb_regs.tf_rax, FCN_NULL },
73 { "rcx", &ddb_regs.tf_rcx, FCN_NULL },
74 { "rdx", &ddb_regs.tf_rdx, FCN_NULL },
75 { "rbx", &ddb_regs.tf_rbx, FCN_NULL },
76 { "rsp", &ddb_regs.tf_rsp, FCN_NULL },
77 { "rbp", &ddb_regs.tf_rbp, FCN_NULL },
78 { "rsi", &ddb_regs.tf_rsi, FCN_NULL },
79 { "rdi", &ddb_regs.tf_rdi, FCN_NULL },
80 { "rip", &ddb_regs.tf_rip, FCN_NULL },
81 { "rfl", &ddb_regs.tf_rflags, FCN_NULL },
82 { "dr0", NULL, db_dr0 },
83 { "dr1", NULL, db_dr1 },
84 { "dr2", NULL, db_dr2 },
85 { "dr3", NULL, db_dr3 },
86 { "dr4", NULL, db_dr4 },
87 { "dr5", NULL, db_dr5 },
88 { "dr6", NULL, db_dr6 },
89 { "dr7", NULL, db_dr7 },
91 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
96 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
99 struct amd64_frame *f_frame;
109 static void db_nextframe(struct amd64_frame **, db_addr_t *);
110 static int db_numargs(struct amd64_frame *);
111 static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
112 static void dl_symbol_values(int callpc, const char **name);
115 static char *watchtype_str(int type);
116 static int kamd64_set_watch(int watchnum, unsigned int watchaddr,
117 int size, int access, struct dbreg * d);
118 static int kamd64_clr_watch(int watchnum, struct dbreg * d);
119 int db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
120 int db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
121 void db_md_list_watchpoints(void);
125 * Figure out how many arguments were passed into the frame at "fp".
128 db_numargs(struct amd64_frame *fp)
135 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
137 * XXX etext is wrong for LKMs. We should attempt to interpret
138 * the instruction at the return address in all cases. This
139 * may require better fault handling.
141 if (argp < (int *)btext || argp >= (int *)etext) {
144 inst = db_get_value((int)argp, 4, FALSE);
145 if ((inst & 0xff) == 0x59) /* popl %ecx */
147 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
148 args = ((inst >> 16) & 0xff) / 4;
158 db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
161 db_printf("%s(", name);
164 db_printf("%s=", *argnp++);
165 db_printf("%r", db_get_value((int)argp, 4, FALSE));
171 db_printsym(callpc, DB_STGY_PROC);
176 * Figure out the next frame up in the call stack.
179 db_nextframe(struct amd64_frame **fp, db_addr_t *ip)
181 struct trapframe *tf;
185 const char *sym, *name;
187 eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
188 ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
191 * Figure out frame type.
196 sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
197 db_symbol_values(sym, &name, NULL);
198 dl_symbol_values(eip, &name);
200 if (!strcmp(name, "calltrap")) {
202 } else if (!strncmp(name, "Xresume", 7)) {
203 frame_type = INTERRUPT;
204 } else if (!strcmp(name, "_Xsyscall")) {
205 frame_type = SYSCALL;
210 * Normal frames need no special processing.
212 if (frame_type == NORMAL) {
213 *ip = (db_addr_t) eip;
214 *fp = (struct amd64_frame *) ebp;
218 db_print_stack_entry(name, 0, 0, 0, eip);
221 * Point to base of trapframe which is just above the
224 tf = (struct trapframe *) ((int)*fp + 8);
227 esp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_rsp : (int)&tf->tf_rsp;
229 esp = (int)&tf->tf_rsp;
231 switch (frame_type) {
237 "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
238 tf->tf_trapno, eip, esp, ebp);
246 "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
247 tf->tf_rax, eip, esp, ebp);
251 tf = (struct trapframe *)((int)*fp + 16);
256 "--- interrupt, eip = %#r, esp = %#r, ebp = %#r ---\n",
264 *ip = (db_addr_t) eip;
265 *fp = (struct amd64_frame *) ebp;
269 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
272 struct amd64_frame *frame;
282 frame = (struct amd64_frame *)BP_REGS(&ddb_regs);
284 frame = (struct amd64_frame *)(SP_REGS(&ddb_regs) - 4);
285 callpc = PC_REGS(&ddb_regs);
288 * Look for something that might be a frame pointer, just as
291 frame = (struct amd64_frame *)addr;
292 for (i = 0; i < 4096; i += 4) {
293 struct amd64_frame *check;
295 check = (struct amd64_frame *)db_get_value((int)((char *)&frame->f_frame + i), 4, FALSE);
296 if ((char *)check - (char *)frame >= 0 &&
297 (char *)check - (char *)frame < 4096
301 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
304 db_printf("Unable to find anything that looks like a stack frame\n");
307 frame = (void *)((char *)frame + i);
308 db_printf("Trace beginning at frame %p\n", frame);
309 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, FALSE);
314 struct amd64_frame *actframe;
320 char *argnames[MAXNARG], **argnp = NULL;
322 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
323 db_symbol_values(sym, &name, NULL);
324 dl_symbol_values(callpc, &name);
327 * Attempt to determine a (possibly fake) frame that gives
328 * the caller's pc. It may differ from `frame' if the
329 * current function never sets up a standard frame or hasn't
330 * set one up yet or has just discarded one. The last two
331 * cases can be guessed fairly reliably for code generated
332 * by gcc. The first case is too much trouble to handle in
333 * general because the amount of junk on the stack depends
334 * on the pc (the special handling of "calltrap", etc. in
335 * db_nextframe() works because the `next' pc is special).
342 instr = db_get_value(callpc, 4, FALSE);
343 if ((instr & 0x00ffffff) == 0x00e58955) {
344 /* pushl %ebp; movl %esp, %ebp */
345 actframe = (struct amd64_frame *)
346 (SP_REGS(&ddb_regs) - 4);
347 } else if ((instr & 0x0000ffff) == 0x0000e589) {
348 /* movl %esp, %ebp */
349 actframe = (struct amd64_frame *)
351 if (ddb_regs.tf_rbp == 0) {
352 /* Fake caller's frame better. */
355 } else if ((instr & 0x000000ff) == 0x000000c3) {
357 actframe = (struct amd64_frame *)
358 (SP_REGS(&ddb_regs) - 4);
359 } else if (offset == 0) {
360 /* Probably a symbol in assembler code. */
361 actframe = (struct amd64_frame *)
362 (SP_REGS(&ddb_regs) - 4);
364 } else if (name != NULL &&
365 strcmp(name, "fork_trampoline") == 0) {
367 * Don't try to walk back on a stack for a
368 * process that hasn't actually been run yet.
370 db_print_stack_entry(name, 0, 0, 0, callpc);
376 argp = &actframe->f_arg0;
378 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
381 narg = db_numargs(frame);
384 db_print_stack_entry(name, narg, argnp, argp, callpc);
386 if (actframe != frame) {
387 /* `frame' belongs to caller. */
389 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
393 db_nextframe(&frame, &callpc);
400 db_print_backtrace(void)
404 /* __asm __volatile("movl %%ebp, %0" : "=r" (ebp)); */
405 db_stack_trace_cmd(ebp, 1, -1, NULL);
408 #define DB_DRX_FUNC(reg) \
410 db_ ## reg (struct db_variable *vp, db_expr_t *valuep, int op) \
412 if (op == DB_VAR_GET) \
413 *valuep = r ## reg (); \
415 load_ ## reg (*valuep); \
430 kamd64_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
436 if (watchnum == -1) {
437 for (i = 0, mask = 0x3; i < 4; i++, mask <<= 2)
438 if ((d->dr[7] & mask) == 0)
448 size = 1; /* size must be 1 for an execution breakpoint */
450 case DBREG_DR7_WRONLY:
458 * we can watch a 1, 2, or 4 byte sized location
476 /* clear the bits we are about to affect */
477 d->dr[7] &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
479 /* set drN register to the address, N=watchnum */
480 DBREG_DRX(d, watchnum) = watchaddr;
482 /* enable the watchpoint */
483 d->dr[7] |= (0x2 << (watchnum * 2)) | (mask << (watchnum * 4 + 16));
490 kamd64_clr_watch(int watchnum, struct dbreg *d)
492 if (watchnum < 0 || watchnum >= 4)
495 d->dr[7] &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
496 DBREG_DRX(d, watchnum) = 0;
503 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
509 fill_dbregs(NULL, &d);
512 for(i=0; i < 4; i++) {
513 if ((d.dr[7] & (3 << (i * 2))) == 0)
517 if (avail * 4 < size)
520 for (i=0; i < 4 && (size != 0); i++) {
521 if ((d.dr[7] & (3 << (i * 2))) == 0) {
528 kamd64_set_watch(i, addr, wsize, DBREG_DR7_WRONLY, &d);
534 set_dbregs(NULL, &d);
540 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
545 fill_dbregs(NULL, &d);
548 if (d.dr[7] & (3 << (i * 2))) {
549 if ((DBREG_DRX((&d), i) >= addr) &&
550 (DBREG_DRX((&d), i) < addr + size))
551 kamd64_clr_watch(i, &d);
555 set_dbregs(NULL, &d);
561 watchtype_str(int type)
568 case DBREG_DR7_WRONLY:
576 db_md_list_watchpoints(void)
581 fill_dbregs(NULL, &d);
583 db_printf("\nhardware watchpoints:\n");
584 db_printf(" watch status type len address\n"
585 " ----- -------- ---------- --- ----------\n");
586 for (i=0; i < 4; i++) {
587 if (d.dr[7] & (0x03 << (i * 2))) {
589 type = (d.dr[7] >> (16 + (i * 4))) & 3;
590 len = (d.dr[7] >> (16 + (i * 4) + 2)) & 3;
591 db_printf(" %-5d %-8s %10s %3d 0x%08x\n",
592 i, "enabled", watchtype_str(type),
593 len + 1, DBREG_DRX((&d), i));
595 db_printf(" %-5d disabled\n", i);
599 db_printf("\ndebug register values:\n");
600 for (i=0; i < 8; i++)
601 db_printf(" dr%d 0x%08x\n", i, DBREG_DRX((&d),i));
606 * See if dladdr() can get the symbol name via the standard dynamic loader.
610 dl_symbol_values(int callpc, const char **name)
616 if (dladdr((const void *)callpc, &info) != 0) {
617 if (info.dli_saddr <= (const void *)callpc)
618 *name = info.dli_sname;