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/vkernel/i386/db_trace.c,v 1.9 2008/02/03 15:10:26 nth 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>
53 static int db_dr(struct db_variable *vp, db_expr_t *valuep, int op);
56 * Machine register set.
58 struct db_variable db_regs[] = {
59 { "cs", &ddb_regs.tf_cs, NULL },
60 { "ds", &ddb_regs.tf_ds, NULL },
61 { "es", &ddb_regs.tf_es, NULL },
62 { "fs", &ddb_regs.tf_fs, NULL },
63 { "gs", &ddb_regs.tf_gs, NULL },
64 { "ss", &ddb_regs.tf_ss, NULL },
65 { "eax", &ddb_regs.tf_eax, NULL },
66 { "ecx", &ddb_regs.tf_ecx, NULL },
67 { "edx", &ddb_regs.tf_edx, NULL },
68 { "ebx", &ddb_regs.tf_ebx, NULL },
69 { "esp", &ddb_regs.tf_esp, NULL },
70 { "ebp", &ddb_regs.tf_ebp, NULL },
71 { "esi", &ddb_regs.tf_esi, NULL },
72 { "edi", &ddb_regs.tf_edi, NULL },
73 { "eip", &ddb_regs.tf_eip, NULL },
74 { "efl", &ddb_regs.tf_eflags, NULL },
75 { "dr0", NULL, db_dr },
76 { "dr1", NULL, db_dr },
77 { "dr2", NULL, db_dr },
78 { "dr3", NULL, db_dr },
79 { "dr4", NULL, db_dr },
80 { "dr5", NULL, db_dr },
81 { "dr6", NULL, db_dr },
82 { "dr7", NULL, db_dr },
84 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
89 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
92 struct i386_frame *f_frame;
102 static void db_nextframe(struct i386_frame **, db_addr_t *);
103 static int db_numargs(struct i386_frame *);
104 static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
105 static void dl_symbol_values(int callpc, const char **name);
108 int db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
109 int db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
110 void db_md_list_watchpoints(void);
114 * Figure out how many arguments were passed into the frame at "fp".
117 db_numargs(struct i386_frame *fp)
124 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
126 * XXX etext is wrong for LKMs. We should attempt to interpret
127 * the instruction at the return address in all cases. This
128 * may require better fault handling.
130 if (argp < (int *)btext || argp >= (int *)etext) {
133 inst = db_get_value((int)argp, 4, FALSE);
134 if ((inst & 0xff) == 0x59) /* popl %ecx */
136 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
137 args = ((inst >> 16) & 0xff) / 4;
147 db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
150 db_printf("%s(", name);
153 db_printf("%s=", *argnp++);
154 db_printf("%r", db_get_value((int)argp, 4, FALSE));
160 db_printsym(callpc, DB_STGY_PROC);
165 * Figure out the next frame up in the call stack.
168 db_nextframe(struct i386_frame **fp, db_addr_t *ip)
170 struct trapframe *tf;
174 const char *sym, *name;
176 eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
177 ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
180 * Figure out frame type.
185 sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
186 db_symbol_values(sym, &name, NULL);
187 dl_symbol_values(eip, &name);
189 if (!strcmp(name, "calltrap")) {
191 } else if (!strncmp(name, "Xresume", 7)) {
192 frame_type = INTERRUPT;
193 } else if (!strcmp(name, "_Xsyscall")) {
194 frame_type = SYSCALL;
199 * Normal frames need no special processing.
201 if (frame_type == NORMAL) {
202 *ip = (db_addr_t) eip;
203 *fp = (struct i386_frame *) ebp;
207 db_print_stack_entry(name, 0, 0, 0, eip);
210 * Point to base of trapframe which is just above the
213 tf = (struct trapframe *) ((int)*fp + 8);
216 esp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_esp : (int)&tf->tf_esp;
218 esp = (int)&tf->tf_esp;
220 switch (frame_type) {
226 "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
227 tf->tf_trapno, eip, esp, ebp);
235 "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
236 tf->tf_eax, eip, esp, ebp);
240 tf = (struct trapframe *)((int)*fp + 16);
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);
277 * Look for something that might be a frame pointer, just as
280 frame = (struct i386_frame *)addr;
281 for (i = 0; i < 4096; i += 4) {
282 struct i386_frame *check;
284 check = (struct i386_frame *)db_get_value((int)((char *)&frame->f_frame + i), 4, FALSE);
285 if ((char *)check - (char *)frame >= 0 &&
286 (char *)check - (char *)frame < 4096
290 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
293 db_printf("Unable to find anything that looks like a stack frame\n");
296 frame = (void *)((char *)frame + i);
297 db_printf("Trace beginning at frame %p\n", frame);
298 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, FALSE);
303 struct i386_frame *actframe;
309 char *argnames[MAXNARG], **argnp = NULL;
311 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
312 db_symbol_values(sym, &name, NULL);
313 dl_symbol_values(callpc, &name);
316 * Attempt to determine a (possibly fake) frame that gives
317 * the caller's pc. It may differ from `frame' if the
318 * current function never sets up a standard frame or hasn't
319 * set one up yet or has just discarded one. The last two
320 * cases can be guessed fairly reliably for code generated
321 * by gcc. The first case is too much trouble to handle in
322 * general because the amount of junk on the stack depends
323 * on the pc (the special handling of "calltrap", etc. in
324 * db_nextframe() works because the `next' pc is special).
331 instr = db_get_value(callpc, 4, FALSE);
332 if ((instr & 0x00ffffff) == 0x00e58955) {
333 /* pushl %ebp; movl %esp, %ebp */
334 actframe = (struct i386_frame *)
335 (SP_REGS(&ddb_regs) - 4);
336 } else if ((instr & 0x0000ffff) == 0x0000e589) {
337 /* movl %esp, %ebp */
338 actframe = (struct i386_frame *)
340 if (ddb_regs.tf_ebp == 0) {
341 /* Fake caller's frame better. */
344 } else if ((instr & 0x000000ff) == 0x000000c3) {
346 actframe = (struct i386_frame *)
347 (SP_REGS(&ddb_regs) - 4);
348 } else if (offset == 0) {
349 /* Probably a symbol in assembler code. */
350 actframe = (struct i386_frame *)
351 (SP_REGS(&ddb_regs) - 4);
353 } else if (name != NULL &&
354 strcmp(name, "fork_trampoline") == 0) {
356 * Don't try to walk back on a stack for a
357 * process that hasn't actually been run yet.
359 db_print_stack_entry(name, 0, 0, 0, callpc);
365 argp = &actframe->f_arg0;
367 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
370 narg = db_numargs(frame);
373 db_print_stack_entry(name, narg, argnp, argp, callpc);
375 if (actframe != frame) {
376 /* `frame' belongs to caller. */
378 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
382 db_nextframe(&frame, &callpc);
389 print_backtrace(void)
393 __asm __volatile("movl %%ebp, %0" : "=r" (ebp));
394 db_stack_trace_cmd(ebp, 1, -1, NULL);
398 db_dr(struct db_variable *vp, db_expr_t *valuep, int op)
400 if (op == DB_VAR_GET)
406 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
412 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
418 db_md_list_watchpoints(void)
420 /* no hardware watchpoints in vkernel */
424 * See if dladdr() can get the symbol name via the standard dynamic loader.
428 dl_symbol_values(int callpc, const char **name)
433 if (dladdr((const void *)callpc, &info) != 0) {
434 if (info.dli_saddr <= (const void *)callpc)
435 *name = info.dli_sname;