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/pc32/i386/db_trace.c,v 1.11 2005/11/14 18:50:03 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/linker_set.h>
36 #include <machine/cpu.h>
37 #include <machine/md_var.h>
38 #include <machine/reg.h>
41 #include <vm/vm_param.h>
43 #include <vm/vm_map.h>
48 #include <ddb/db_access.h>
49 #include <ddb/db_sym.h>
50 #include <ddb/db_variables.h>
62 * Machine register set.
64 struct db_variable db_regs[] = {
65 { "cs", &ddb_regs.tf_cs, FCN_NULL },
66 { "ds", &ddb_regs.tf_ds, FCN_NULL },
67 { "es", &ddb_regs.tf_es, FCN_NULL },
68 { "fs", &ddb_regs.tf_fs, FCN_NULL },
70 { "gs", &ddb_regs.tf_gs, FCN_NULL },
72 { "ss", &ddb_regs.tf_ss, FCN_NULL },
73 { "eax", &ddb_regs.tf_eax, FCN_NULL },
74 { "ecx", &ddb_regs.tf_ecx, FCN_NULL },
75 { "edx", &ddb_regs.tf_edx, FCN_NULL },
76 { "ebx", &ddb_regs.tf_ebx, FCN_NULL },
77 { "esp", &ddb_regs.tf_esp, FCN_NULL },
78 { "ebp", &ddb_regs.tf_ebp, FCN_NULL },
79 { "esi", &ddb_regs.tf_esi, FCN_NULL },
80 { "edi", &ddb_regs.tf_edi, FCN_NULL },
81 { "eip", &ddb_regs.tf_eip, FCN_NULL },
82 { "efl", &ddb_regs.tf_eflags, FCN_NULL },
83 { "dr0", NULL, db_dr0 },
84 { "dr1", NULL, db_dr1 },
85 { "dr2", NULL, db_dr2 },
86 { "dr3", NULL, db_dr3 },
87 { "dr4", NULL, db_dr4 },
88 { "dr5", NULL, db_dr5 },
89 { "dr6", NULL, db_dr6 },
90 { "dr7", NULL, db_dr7 },
92 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
97 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
100 struct i386_frame *f_frame;
110 static void db_nextframe(struct i386_frame **, db_addr_t *);
111 static int db_numargs(struct i386_frame *);
112 static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
115 static char *watchtype_str(int type);
116 int i386_set_watch(int watchnum, unsigned int watchaddr,
117 int size, int access, struct dbreg * d);
118 int i386_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 i386_frame *fp)
134 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
136 * XXX etext is wrong for LKMs. We should attempt to interpret
137 * the instruction at the return address in all cases. This
138 * may require better fault handling.
140 if (argp < (int *)btext || argp >= (int *)etext) {
143 inst = db_get_value((int)argp, 4, FALSE);
144 if ((inst & 0xff) == 0x59) /* popl %ecx */
146 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
147 args = ((inst >> 16) & 0xff) / 4;
155 db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
158 db_printf("%s(", name);
161 db_printf("%s=", *argnp++);
162 db_printf("%r", db_get_value((int)argp, 4, FALSE));
168 db_printsym(callpc, DB_STGY_PROC);
173 * Figure out the next frame up in the call stack.
176 db_nextframe(struct i386_frame **fp, db_addr_t *ip)
178 struct trapframe *tf;
182 const char *sym, *name;
184 eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
185 ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
188 * Figure out frame type.
193 sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
194 db_symbol_values(sym, &name, NULL);
196 if (!strcmp(name, "calltrap")) {
198 } else if (!strncmp(name, "Xresume", 7)) {
199 frame_type = INTERRUPT;
200 } else if (!strcmp(name, "_Xsyscall")) {
201 frame_type = SYSCALL;
206 * Normal frames need no special processing.
208 if (frame_type == NORMAL) {
209 *ip = (db_addr_t) eip;
210 *fp = (struct i386_frame *) ebp;
214 db_print_stack_entry(name, 0, 0, 0, eip);
217 * Point to base of trapframe which is just above the
220 tf = (struct trapframe *) ((int)*fp + 8);
222 esp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_esp : (int)&tf->tf_esp;
223 switch (frame_type) {
225 if (INKERNEL((int) tf)) {
229 "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
230 tf->tf_trapno, eip, esp, ebp);
234 if (INKERNEL((int) tf)) {
238 "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
239 tf->tf_eax, eip, esp, ebp);
243 tf = (struct trapframe *)((int)*fp + 16);
244 if (INKERNEL((int) tf)) {
248 "--- interrupt, eip = %#r, esp = %#r, ebp = %#r ---\n",
256 *ip = (db_addr_t) eip;
257 *fp = (struct i386_frame *) ebp;
261 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
264 struct i386_frame *frame;
277 frame = (struct i386_frame *)BP_REGS(&ddb_regs);
279 frame = (struct i386_frame *)(SP_REGS(&ddb_regs) - 4);
280 callpc = PC_REGS(&ddb_regs);
281 } else if (!INKERNEL(addr)) {
282 pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
283 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
284 ((addr >> 16) % 16) * 10000;
286 * The pcb for curproc is not valid at this point,
287 * so fall back to the default case.
289 if ((curproc != NULL) && (pid == curproc->p_pid)) {
290 frame = (struct i386_frame *)BP_REGS(&ddb_regs);
292 frame = (struct i386_frame *)
293 (SP_REGS(&ddb_regs) - 4);
294 callpc = PC_REGS(&ddb_regs);
297 /* sx_slock(&allproc_lock); */
298 FOREACH_PROC_IN_SYSTEM(p) {
302 /* sx_sunlock(&allproc_lock); */
304 db_printf("pid %d not found\n", pid);
307 if ((p->p_flag & P_SWAPPEDOUT)) {
308 db_printf("pid %d swapped out\n", pid);
311 pcb = p->p_thread->td_pcb;
312 frame = (struct i386_frame *)pcb->pcb_ebp;
314 frame = (struct i386_frame *)
316 callpc = (db_addr_t)pcb->pcb_eip;
320 * Look for something that might be a frame pointer, just as
323 frame = (struct i386_frame *)addr;
324 for (i = 0; i < 4096; i += 4) {
325 struct i386_frame *check;
327 check = (struct i386_frame *)db_get_value((int)((char *)&frame->f_frame + i), 4, FALSE);
328 if ((char *)check - (char *)frame >= 0 &&
329 (char *)check - (char *)frame < 4096
333 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
336 db_printf("Unable to find anything that looks like a stack frame\n");
339 frame = (void *)((char *)frame + i);
340 db_printf("Trace beginning at frame %p\n", frame);
341 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, FALSE);
346 struct i386_frame *actframe;
352 char *argnames[MAXNARG], **argnp = NULL;
354 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
355 db_symbol_values(sym, &name, NULL);
358 * Attempt to determine a (possibly fake) frame that gives
359 * the caller's pc. It may differ from `frame' if the
360 * current function never sets up a standard frame or hasn't
361 * set one up yet or has just discarded one. The last two
362 * cases can be guessed fairly reliably for code generated
363 * by gcc. The first case is too much trouble to handle in
364 * general because the amount of junk on the stack depends
365 * on the pc (the special handling of "calltrap", etc. in
366 * db_nextframe() works because the `next' pc is special).
373 instr = db_get_value(callpc, 4, FALSE);
374 if ((instr & 0x00ffffff) == 0x00e58955) {
375 /* pushl %ebp; movl %esp, %ebp */
376 actframe = (struct i386_frame *)
377 (SP_REGS(&ddb_regs) - 4);
378 } else if ((instr & 0x0000ffff) == 0x0000e589) {
379 /* movl %esp, %ebp */
380 actframe = (struct i386_frame *)
382 if (ddb_regs.tf_ebp == 0) {
383 /* Fake caller's frame better. */
386 } else if ((instr & 0x000000ff) == 0x000000c3) {
388 actframe = (struct i386_frame *)
389 (SP_REGS(&ddb_regs) - 4);
390 } else if (offset == 0) {
391 /* Probably a symbol in assembler code. */
392 actframe = (struct i386_frame *)
393 (SP_REGS(&ddb_regs) - 4);
395 } else if (!strcmp(name, "fork_trampoline")) {
397 * Don't try to walk back on a stack for a
398 * process that hasn't actually been run yet.
400 db_print_stack_entry(name, 0, 0, 0, callpc);
406 argp = &actframe->f_arg0;
408 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
411 narg = db_numargs(frame);
414 db_print_stack_entry(name, narg, argnp, argp, callpc);
416 if (actframe != frame) {
417 /* `frame' belongs to caller. */
419 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
423 db_nextframe(&frame, &callpc);
425 if (INKERNEL((int) callpc) && !INKERNEL((int) frame)) {
426 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
427 db_symbol_values(sym, &name, NULL);
428 db_print_stack_entry(name, 0, 0, 0, callpc);
431 if (!INKERNEL((int) frame)) {
438 db_print_backtrace(void)
442 __asm __volatile("movl %%ebp, %0" : "=r" (ebp));
443 db_stack_trace_cmd(ebp, 1, -1, NULL);
446 #define DB_DRX_FUNC(reg) \
448 db_ ## reg (struct db_variable *vp, db_expr_t *valuep, int op) \
450 if (op == DB_VAR_GET) \
451 *valuep = r ## reg (); \
453 load_ ## reg (*valuep); \
469 i386_set_watch(int watchnum, unsigned int watchaddr, int size, int access,
475 if (watchnum == -1) {
476 for (i = 0, mask = 0x3; i < 4; i++, mask <<= 2)
477 if ((d->dr7 & mask) == 0)
487 size = 1; /* size must be 1 for an execution breakpoint */
489 case DBREG_DR7_WRONLY:
497 * we can watch a 1, 2, or 4 byte sized location
515 /* clear the bits we are about to affect */
516 d->dr7 &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
518 /* set drN register to the address, N=watchnum */
519 DBREG_DRX(d, watchnum) = watchaddr;
521 /* enable the watchpoint */
522 d->dr7 |= (0x2 << (watchnum * 2)) | (mask << (watchnum * 4 + 16));
529 i386_clr_watch(int watchnum, struct dbreg *d)
531 if (watchnum < 0 || watchnum >= 4)
534 d->dr7 &= ~((0x3 << (watchnum * 2)) | (0x0f << (watchnum * 4 + 16)));
535 DBREG_DRX(d, watchnum) = 0;
542 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
548 fill_dbregs(NULL, &d);
551 for(i=0; i < 4; i++) {
552 if ((d.dr7 & (3 << (i * 2))) == 0)
556 if (avail * 4 < size)
559 for (i=0; i < 4 && (size != 0); i++) {
560 if ((d.dr7 & (3 << (i * 2))) == 0) {
567 i386_set_watch(i, addr, wsize, DBREG_DR7_WRONLY, &d);
573 set_dbregs(NULL, &d);
579 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
584 fill_dbregs(NULL, &d);
587 if (d.dr7 & (3 << (i * 2))) {
588 if ((DBREG_DRX((&d), i) >= addr) &&
589 (DBREG_DRX((&d), i) < addr + size))
590 i386_clr_watch(i, &d);
594 set_dbregs(NULL, &d);
600 watchtype_str(int type)
607 case DBREG_DR7_WRONLY:
615 db_md_list_watchpoints(void)
620 fill_dbregs(NULL, &d);
622 db_printf("\nhardware watchpoints:\n");
623 db_printf(" watch status type len address\n"
624 " ----- -------- ---------- --- ----------\n");
625 for (i=0; i < 4; i++) {
626 if (d.dr7 & (0x03 << (i * 2))) {
628 type = (d.dr7 >> (16 + (i * 4))) & 3;
629 len = (d.dr7 >> (16 + (i * 4) + 2)) & 3;
630 db_printf(" %-5d %-8s %10s %3d 0x%08x\n",
631 i, "enabled", watchtype_str(type),
632 len + 1, DBREG_DRX((&d), i));
634 db_printf(" %-5d disabled\n", i);
638 db_printf("\ndebug register values:\n");
639 for (i=0; i < 8; i++)
640 db_printf(" dr%d 0x%08x\n", i, DBREG_DRX((&d),i));