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 $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/linker_set.h>
36 #include <machine/cpu.h>
37 #include <machine/md_var.h>
40 #include <vm/vm_param.h>
42 #include <vm/vm_map.h>
47 #include <ddb/db_access.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/db_variables.h>
61 * Machine register set.
63 struct db_variable db_regs
[] = {
64 { "cs", &ddb_regs
.tf_cs
, NULL
},
65 { "ds", &ddb_regs
.tf_ds
, NULL
},
66 { "es", &ddb_regs
.tf_es
, NULL
},
67 { "fs", &ddb_regs
.tf_fs
, NULL
},
68 { "gs", &ddb_regs
.tf_gs
, NULL
},
69 { "ss", &ddb_regs
.tf_ss
, NULL
},
70 { "eax", &ddb_regs
.tf_eax
, NULL
},
71 { "ecx", &ddb_regs
.tf_ecx
, NULL
},
72 { "edx", &ddb_regs
.tf_edx
, NULL
},
73 { "ebx", &ddb_regs
.tf_ebx
, NULL
},
74 { "esp", &ddb_regs
.tf_esp
, NULL
},
75 { "ebp", &ddb_regs
.tf_ebp
, NULL
},
76 { "esi", &ddb_regs
.tf_esi
, NULL
},
77 { "edi", &ddb_regs
.tf_edi
, NULL
},
78 { "eip", &ddb_regs
.tf_eip
, NULL
},
79 { "efl", &ddb_regs
.tf_eflags
, NULL
},
80 { "dr0", NULL
, db_dr0
},
81 { "dr1", NULL
, db_dr1
},
82 { "dr2", NULL
, db_dr2
},
83 { "dr3", NULL
, db_dr3
},
84 { "dr4", NULL
, db_dr4
},
85 { "dr5", NULL
, db_dr5
},
86 { "dr6", NULL
, db_dr6
},
87 { "dr7", NULL
, db_dr7
},
89 struct db_variable
*db_eregs
= db_regs
+ NELEM(db_regs
);
94 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
97 struct i386_frame
*f_frame
;
107 static void db_nextframe(struct i386_frame
**, db_addr_t
*);
108 static int db_numargs(struct i386_frame
*);
109 static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t
);
112 static char *watchtype_str(int type
);
113 static int ki386_set_watch(int watchnum
, unsigned int watchaddr
,
114 int size
, int access
, struct dbreg
* d
);
115 static int ki386_clr_watch(int watchnum
, struct dbreg
* d
);
116 int db_md_set_watchpoint(db_expr_t addr
, db_expr_t size
);
117 int db_md_clr_watchpoint(db_expr_t addr
, db_expr_t size
);
118 void db_md_list_watchpoints(void);
122 * Figure out how many arguments were passed into the frame at "fp".
125 db_numargs(struct i386_frame
*fp
)
131 argp
= (int *)db_get_value((int)&fp
->f_retaddr
, 4, FALSE
);
133 * XXX etext is wrong for LKMs. We should attempt to interpret
134 * the instruction at the return address in all cases. This
135 * may require better fault handling.
137 if (argp
< (int *)btext
|| argp
>= (int *)etext
) {
140 inst
= db_get_value((int)argp
, 4, FALSE
);
141 if ((inst
& 0xff) == 0x59) /* popl %ecx */
143 else if ((inst
& 0xffff) == 0xc483) /* addl $Ibs, %esp */
144 args
= ((inst
>> 16) & 0xff) / 4;
152 db_print_stack_entry(const char *name
, int narg
, char **argnp
, int *argp
,
155 db_printf("%s(", name
);
158 db_printf("%s=", *argnp
++);
159 db_printf("%r", db_get_value((int)argp
, 4, FALSE
));
165 db_printsym(callpc
, DB_STGY_PROC
);
170 * Figure out the next frame up in the call stack.
173 db_nextframe(struct i386_frame
**fp
, db_addr_t
*ip
)
175 struct trapframe
*tf
;
179 const char *sym
, *name
;
181 eip
= db_get_value((int) &(*fp
)->f_retaddr
, 4, FALSE
);
182 ebp
= db_get_value((int) &(*fp
)->f_frame
, 4, FALSE
);
185 * Figure out frame type.
190 sym
= db_search_symbol(eip
, DB_STGY_ANY
, &offset
);
191 db_symbol_values(sym
, &name
, NULL
);
193 if (!strcmp(name
, "calltrap")) {
195 } else if (!strncmp(name
, "Xresume", 7)) {
196 frame_type
= INTERRUPT
;
197 } else if (!strcmp(name
, "_Xsyscall")) {
198 frame_type
= SYSCALL
;
203 * Normal frames need no special processing.
205 if (frame_type
== NORMAL
) {
206 *ip
= (db_addr_t
) eip
;
207 *fp
= (struct i386_frame
*) ebp
;
211 db_print_stack_entry(name
, 0, 0, 0, eip
);
214 * Point to base of trapframe which is just above the
217 tf
= (struct trapframe
*) ((int)*fp
+ 8);
219 esp
= (ISPL(tf
->tf_cs
) == SEL_UPL
) ? tf
->tf_esp
: (int)&tf
->tf_esp
;
220 switch (frame_type
) {
222 if (INKERNEL((int) tf
)) {
226 "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
227 tf
->tf_trapno
, eip
, esp
, ebp
);
231 if (INKERNEL((int) tf
)) {
235 "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
236 tf
->tf_eax
, eip
, esp
, ebp
);
240 tf
= (struct trapframe
*)((int)*fp
+ 16);
241 if (INKERNEL((int) tf
)) {
245 "--- interrupt, eip = %#r, esp = %#r, ebp = %#r ---\n",
253 *ip
= (db_addr_t
) eip
;
254 *fp
= (struct i386_frame
*) ebp
;
258 db_stack_trace_cmd(db_expr_t addr
, boolean_t have_addr
, db_expr_t count
,
261 struct i386_frame
*frame
;
271 frame
= (struct i386_frame
*)BP_REGS(&ddb_regs
);
273 frame
= (struct i386_frame
*)(SP_REGS(&ddb_regs
) - 4);
274 callpc
= PC_REGS(&ddb_regs
);
275 } else if (!INKERNEL(addr
)) {
277 pid
= (addr
% 16) + ((addr
>> 4) % 16) * 10 +
278 ((addr
>> 8) % 16) * 100 + ((addr
>> 12) % 16) * 1000 +
279 ((addr
>> 16) % 16) * 10000;
281 * The pcb for curproc is not valid at this point,
282 * so fall back to the default case.
284 if ((curproc
!= NULL
) && (pid
== curproc
->p_pid
)) {
285 frame
= (struct i386_frame
*)BP_REGS(&ddb_regs
);
287 frame
= (struct i386_frame
*)
288 (SP_REGS(&ddb_regs
) - 4);
289 callpc
= PC_REGS(&ddb_regs
);
297 db_printf("pid %d not found\n", pid
);
300 if ((p
->p_flag
& P_SWAPPEDOUT
)) {
301 db_printf("pid %d swapped out\n", pid
);
304 pcb
= p
->p_thread
->td_pcb
;
305 frame
= (struct i386_frame
*)pcb
->pcb_ebp
;
307 frame
= (struct i386_frame
*)
309 callpc
= (db_addr_t
)pcb
->pcb_eip
;
313 db_printf("no kernel stack address\n");
318 * Look for something that might be a frame pointer, just as
321 frame
= (struct i386_frame
*)addr
;
322 for (i
= 0; i
< 4096; i
+= 4) {
323 struct i386_frame
*check
;
325 check
= (struct i386_frame
*)db_get_value((int)((char *)&frame
->f_frame
+ i
), 4, FALSE
);
326 if ((char *)check
- (char *)frame
>= 0 &&
327 (char *)check
- (char *)frame
< 4096
331 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame
->f_frame
+ i
);
334 db_printf("Unable to find anything that looks like a stack frame\n");
337 frame
= (void *)((char *)frame
+ i
);
338 db_printf("Trace beginning at frame %p\n", frame
);
339 callpc
= (db_addr_t
)db_get_value((int)&frame
->f_retaddr
, 4, FALSE
);
344 struct i386_frame
*actframe
;
350 char *argnames
[MAXNARG
], **argnp
= NULL
;
352 sym
= db_search_symbol(callpc
, DB_STGY_ANY
, &offset
);
353 db_symbol_values(sym
, &name
, NULL
);
356 * Attempt to determine a (possibly fake) frame that gives
357 * the caller's pc. It may differ from `frame' if the
358 * current function never sets up a standard frame or hasn't
359 * set one up yet or has just discarded one. The last two
360 * cases can be guessed fairly reliably for code generated
361 * by gcc. The first case is too much trouble to handle in
362 * general because the amount of junk on the stack depends
363 * on the pc (the special handling of "calltrap", etc. in
364 * db_nextframe() works because the `next' pc is special).
371 instr
= db_get_value(callpc
, 4, FALSE
);
372 if ((instr
& 0x00ffffff) == 0x00e58955) {
373 /* pushl %ebp; movl %esp, %ebp */
374 actframe
= (struct i386_frame
*)
375 (SP_REGS(&ddb_regs
) - 4);
376 } else if ((instr
& 0x0000ffff) == 0x0000e589) {
377 /* movl %esp, %ebp */
378 actframe
= (struct i386_frame
*)
380 if (ddb_regs
.tf_ebp
== 0) {
381 /* Fake caller's frame better. */
384 } else if ((instr
& 0x000000ff) == 0x000000c3) {
386 actframe
= (struct i386_frame
*)
387 (SP_REGS(&ddb_regs
) - 4);
388 } else if (offset
== 0) {
389 /* Probably a symbol in assembler code. */
390 actframe
= (struct i386_frame
*)
391 (SP_REGS(&ddb_regs
) - 4);
393 } else if (name
!= NULL
&&
394 strcmp(name
, "fork_trampoline") == 0) {
396 * Don't try to walk back on a stack for a
397 * process that hasn't actually been run yet.
399 db_print_stack_entry(name
, 0, 0, 0, callpc
);
405 argp
= &actframe
->f_arg0
;
407 if (sym
!= NULL
&& db_sym_numargs(sym
, &narg
, argnames
)) {
410 narg
= db_numargs(frame
);
413 db_print_stack_entry(name
, narg
, argnp
, argp
, callpc
);
415 if (actframe
!= frame
) {
416 /* `frame' belongs to caller. */
418 db_get_value((int)&actframe
->f_retaddr
, 4, FALSE
);
422 db_nextframe(&frame
, &callpc
);
424 if (INKERNEL((int) callpc
) && !INKERNEL((int) frame
)) {
425 sym
= db_search_symbol(callpc
, DB_STGY_ANY
, &offset
);
426 db_symbol_values(sym
, &name
, NULL
);
427 db_print_stack_entry(name
, 0, 0, 0, callpc
);
430 if (!INKERNEL((int) frame
)) {
437 print_backtrace(int count
)
441 __asm
__volatile("movl %%ebp, %0" : "=r" (ebp
));
442 db_stack_trace_cmd(ebp
, 1, count
, NULL
);
445 #define DB_DRX_FUNC(reg) \
447 db_ ## reg (struct db_variable *vp, db_expr_t *valuep, int op) \
449 if (op == DB_VAR_GET) \
450 *valuep = r ## reg (); \
452 load_ ## reg (*valuep); \
467 ki386_set_watch(int watchnum
, unsigned int watchaddr
, int size
, int access
,
473 if (watchnum
== -1) {
474 for (i
= 0, mask
= 0x3; i
< 4; i
++, mask
<<= 2)
475 if ((d
->dr7
& mask
) == 0)
485 size
= 1; /* size must be 1 for an execution breakpoint */
487 case DBREG_DR7_WRONLY
:
495 * we can watch a 1, 2, or 4 byte sized location
513 /* clear the bits we are about to affect */
514 d
->dr7
&= ~((0x3 << (watchnum
* 2)) | (0x0f << (watchnum
* 4 + 16)));
516 /* set drN register to the address, N=watchnum */
517 DBREG_DRX(d
, watchnum
) = watchaddr
;
519 /* enable the watchpoint */
520 d
->dr7
|= (0x2 << (watchnum
* 2)) | (mask
<< (watchnum
* 4 + 16));
527 ki386_clr_watch(int watchnum
, struct dbreg
*d
)
529 if (watchnum
< 0 || watchnum
>= 4)
532 d
->dr7
&= ~((0x3 << (watchnum
* 2)) | (0x0f << (watchnum
* 4 + 16)));
533 DBREG_DRX(d
, watchnum
) = 0;
540 db_md_set_watchpoint(db_expr_t addr
, db_expr_t size
)
546 fill_dbregs(NULL
, &d
);
549 for(i
=0; i
< 4; i
++) {
550 if ((d
.dr7
& (3 << (i
* 2))) == 0)
554 if (avail
* 4 < size
)
557 for (i
=0; i
< 4 && (size
!= 0); i
++) {
558 if ((d
.dr7
& (3 << (i
* 2))) == 0) {
565 ki386_set_watch(i
, addr
, wsize
, DBREG_DR7_WRONLY
, &d
);
571 set_dbregs(NULL
, &d
);
577 db_md_clr_watchpoint(db_expr_t addr
, db_expr_t size
)
582 fill_dbregs(NULL
, &d
);
585 if (d
.dr7
& (3 << (i
* 2))) {
586 if ((DBREG_DRX((&d
), i
) >= addr
) &&
587 (DBREG_DRX((&d
), i
) < addr
+ size
))
588 ki386_clr_watch(i
, &d
);
592 set_dbregs(NULL
, &d
);
598 watchtype_str(int type
)
605 case DBREG_DR7_WRONLY
:
613 db_md_list_watchpoints(void)
618 fill_dbregs(NULL
, &d
);
620 db_printf("\nhardware watchpoints:\n");
621 db_printf(" watch status type len address\n"
622 " ----- -------- ---------- --- ----------\n");
623 for (i
=0; i
< 4; i
++) {
624 if (d
.dr7
& (0x03 << (i
* 2))) {
626 type
= (d
.dr7
>> (16 + (i
* 4))) & 3;
627 len
= (d
.dr7
>> (16 + (i
* 4) + 2)) & 3;
628 db_printf(" %-5d %-8s %10s %3d 0x%08x\n",
629 i
, "enabled", watchtype_str(type
),
630 len
+ 1, DBREG_DRX((&d
), i
));
632 db_printf(" %-5d disabled\n", i
);
636 db_printf("\ndebug register values:\n");
637 for (i
=0; i
< 8; i
++)
638 db_printf(" dr%d 0x%08x\n", i
, DBREG_DRX((&d
),i
));