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>
44 #include <dlfcn.h> /* DLL */
48 #include <ddb/db_access.h>
49 #include <ddb/db_sym.h>
50 #include <ddb/db_variables.h>
52 static int db_dr(struct db_variable *vp, db_expr_t *valuep, int op);
55 * Machine register set.
57 struct db_variable db_regs[] = {
58 { "cs", &ddb_regs.tf_cs, NULL },
59 { "ds", &ddb_regs.tf_ds, NULL },
60 { "es", &ddb_regs.tf_es, NULL },
61 { "fs", &ddb_regs.tf_fs, NULL },
62 { "gs", &ddb_regs.tf_gs, NULL },
63 { "ss", &ddb_regs.tf_ss, NULL },
64 { "eax", &ddb_regs.tf_eax, NULL },
65 { "ecx", &ddb_regs.tf_ecx, NULL },
66 { "edx", &ddb_regs.tf_edx, NULL },
67 { "ebx", &ddb_regs.tf_ebx, NULL },
68 { "esp", &ddb_regs.tf_esp, NULL },
69 { "ebp", &ddb_regs.tf_ebp, NULL },
70 { "esi", &ddb_regs.tf_esi, NULL },
71 { "edi", &ddb_regs.tf_edi, NULL },
72 { "eip", &ddb_regs.tf_eip, NULL },
73 { "efl", &ddb_regs.tf_eflags, NULL },
74 { "dr0", NULL, db_dr },
75 { "dr1", NULL, db_dr },
76 { "dr2", NULL, db_dr },
77 { "dr3", NULL, db_dr },
78 { "dr4", NULL, db_dr },
79 { "dr5", NULL, db_dr },
80 { "dr6", NULL, db_dr },
81 { "dr7", NULL, db_dr },
83 struct db_variable *db_eregs = db_regs + NELEM(db_regs);
88 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
91 struct i386_frame *f_frame;
101 static void db_nextframe(struct i386_frame **, db_addr_t *);
102 static int db_numargs(struct i386_frame *);
103 static void db_print_stack_entry(const char *, int, char **, int *, db_addr_t);
104 static void dl_symbol_values(int callpc, const char **name);
107 int db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
108 int db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
109 void db_md_list_watchpoints(void);
113 * Figure out how many arguments were passed into the frame at "fp".
116 db_numargs(struct i386_frame *fp)
123 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
125 * XXX etext is wrong for LKMs. We should attempt to interpret
126 * the instruction at the return address in all cases. This
127 * may require better fault handling.
129 if (argp < (int *)btext || argp >= (int *)etext) {
132 inst = db_get_value((int)argp, 4, FALSE);
133 if ((inst & 0xff) == 0x59) /* popl %ecx */
135 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
136 args = ((inst >> 16) & 0xff) / 4;
146 db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
149 db_printf("%s(", name);
152 db_printf("%s=", *argnp++);
153 db_printf("%r", db_get_value((int)argp, 4, FALSE));
159 db_printsym(callpc, DB_STGY_PROC);
164 * Figure out the next frame up in the call stack.
167 db_nextframe(struct i386_frame **fp, db_addr_t *ip)
169 struct trapframe *tf;
173 const char *sym, *name;
175 eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
176 ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
179 * Figure out frame type.
184 sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
185 db_symbol_values(sym, &name, NULL);
186 dl_symbol_values(eip, &name);
188 if (!strcmp(name, "calltrap")) {
190 } else if (!strncmp(name, "Xresume", 7)) {
191 frame_type = INTERRUPT;
192 } else if (!strcmp(name, "_Xsyscall")) {
193 frame_type = SYSCALL;
198 * Normal frames need no special processing.
200 if (frame_type == NORMAL) {
201 *ip = (db_addr_t) eip;
202 *fp = (struct i386_frame *) ebp;
206 db_print_stack_entry(name, 0, 0, 0, eip);
209 * Point to base of trapframe which is just above the
212 tf = (struct trapframe *) ((int)*fp + 8);
215 esp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_esp : (int)&tf->tf_esp;
217 esp = (int)&tf->tf_esp;
219 switch (frame_type) {
225 "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
226 tf->tf_trapno, eip, esp, ebp);
234 "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
235 tf->tf_eax, eip, esp, ebp);
239 tf = (struct trapframe *)((int)*fp + 16);
244 "--- interrupt, eip = %#r, esp = %#r, ebp = %#r ---\n",
252 *ip = (db_addr_t) eip;
253 *fp = (struct i386_frame *) ebp;
257 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
260 struct i386_frame *frame;
270 frame = (struct i386_frame *)BP_REGS(&ddb_regs);
272 frame = (struct i386_frame *)(SP_REGS(&ddb_regs) - 4);
273 callpc = PC_REGS(&ddb_regs);
276 * Look for something that might be a frame pointer, just as
279 frame = (struct i386_frame *)addr;
280 for (i = 0; i < 4096; i += 4) {
281 struct i386_frame *check;
283 check = (struct i386_frame *)db_get_value((int)((char *)&frame->f_frame + i), 4, FALSE);
284 if ((char *)check - (char *)frame >= 0 &&
285 (char *)check - (char *)frame < 4096
289 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
292 db_printf("Unable to find anything that looks like a stack frame\n");
295 frame = (void *)((char *)frame + i);
296 db_printf("Trace beginning at frame %p\n", frame);
297 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, FALSE);
302 struct i386_frame *actframe;
308 char *argnames[MAXNARG], **argnp = NULL;
310 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
311 db_symbol_values(sym, &name, NULL);
312 dl_symbol_values(callpc, &name);
315 * Attempt to determine a (possibly fake) frame that gives
316 * the caller's pc. It may differ from `frame' if the
317 * current function never sets up a standard frame or hasn't
318 * set one up yet or has just discarded one. The last two
319 * cases can be guessed fairly reliably for code generated
320 * by gcc. The first case is too much trouble to handle in
321 * general because the amount of junk on the stack depends
322 * on the pc (the special handling of "calltrap", etc. in
323 * db_nextframe() works because the `next' pc is special).
330 instr = db_get_value(callpc, 4, FALSE);
331 if ((instr & 0x00ffffff) == 0x00e58955) {
332 /* pushl %ebp; movl %esp, %ebp */
333 actframe = (struct i386_frame *)
334 (SP_REGS(&ddb_regs) - 4);
335 } else if ((instr & 0x0000ffff) == 0x0000e589) {
336 /* movl %esp, %ebp */
337 actframe = (struct i386_frame *)
339 if (ddb_regs.tf_ebp == 0) {
340 /* Fake caller's frame better. */
343 } else if ((instr & 0x000000ff) == 0x000000c3) {
345 actframe = (struct i386_frame *)
346 (SP_REGS(&ddb_regs) - 4);
347 } else if (offset == 0) {
348 /* Probably a symbol in assembler code. */
349 actframe = (struct i386_frame *)
350 (SP_REGS(&ddb_regs) - 4);
352 } else if (name != NULL &&
353 strcmp(name, "fork_trampoline") == 0) {
355 * Don't try to walk back on a stack for a
356 * process that hasn't actually been run yet.
358 db_print_stack_entry(name, 0, 0, 0, callpc);
364 argp = &actframe->f_arg0;
366 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
369 narg = db_numargs(frame);
372 db_print_stack_entry(name, narg, argnp, argp, callpc);
374 if (actframe != frame) {
375 /* `frame' belongs to caller. */
377 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
381 db_nextframe(&frame, &callpc);
388 print_backtrace(int count)
392 __asm __volatile("movl %%ebp, %0" : "=r" (ebp));
393 db_stack_trace_cmd(ebp, 1, count, NULL);
397 db_dr(struct db_variable *vp, db_expr_t *valuep, int op)
399 if (op == DB_VAR_GET)
405 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
411 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
417 db_md_list_watchpoints(void)
419 /* no hardware watchpoints in vkernel */
423 * See if dladdr() can get the symbol name via the standard dynamic loader.
427 dl_symbol_values(int callpc, const char **name)
432 if (dladdr((const void *)callpc, &info) != 0) {
433 if (info.dli_saddr <= (const void *)callpc)
434 *name = info.dli_sname;