Don't use NULL where 0 is meant.
[dragonfly/netmp.git] / sys / platform / vkernel / i386 / db_trace.c
blob6954fb09bf02f56683863b7ccdbf10b77cbd3e4b
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
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>
33 #include <sys/lock.h>
34 #include <sys/proc.h>
35 #include <sys/reg.h>
37 #include <machine/cpu.h>
38 #include <machine/md_var.h>
40 #include <vm/vm.h>
41 #include <vm/vm_param.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_map.h>
44 #include <ddb/ddb.h>
45 #include <dlfcn.h> /* DLL */
47 #include <sys/user.h>
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, FCN_NULL },
60 { "ds", &ddb_regs.tf_ds, FCN_NULL },
61 { "es", &ddb_regs.tf_es, FCN_NULL },
62 { "fs", &ddb_regs.tf_fs, FCN_NULL },
63 { "gs", &ddb_regs.tf_gs, FCN_NULL },
64 { "ss", &ddb_regs.tf_ss, FCN_NULL },
65 { "eax", &ddb_regs.tf_eax, FCN_NULL },
66 { "ecx", &ddb_regs.tf_ecx, FCN_NULL },
67 { "edx", &ddb_regs.tf_edx, FCN_NULL },
68 { "ebx", &ddb_regs.tf_ebx, FCN_NULL },
69 { "esp", &ddb_regs.tf_esp, FCN_NULL },
70 { "ebp", &ddb_regs.tf_ebp, FCN_NULL },
71 { "esi", &ddb_regs.tf_esi, FCN_NULL },
72 { "edi", &ddb_regs.tf_edi, FCN_NULL },
73 { "eip", &ddb_regs.tf_eip, FCN_NULL },
74 { "efl", &ddb_regs.tf_eflags, FCN_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]);
87 * Stack trace.
89 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
91 struct i386_frame {
92 struct i386_frame *f_frame;
93 int f_retaddr;
94 int f_arg0;
97 #define NORMAL 0
98 #define TRAP 1
99 #define INTERRUPT 2
100 #define SYSCALL 3
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".
116 static int
117 db_numargs(struct i386_frame *fp)
119 int args;
120 #if 0
121 int *argp;
122 int inst;
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) {
131 args = 5;
132 } else {
133 inst = db_get_value((int)argp, 4, FALSE);
134 if ((inst & 0xff) == 0x59) /* popl %ecx */
135 args = 1;
136 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
137 args = ((inst >> 16) & 0xff) / 4;
138 else
139 args = 5;
141 #endif
142 args = 5;
143 return(args);
146 static void
147 db_print_stack_entry(const char *name, int narg, char **argnp, int *argp,
148 db_addr_t callpc)
150 db_printf("%s(", name);
151 while (narg) {
152 if (argnp)
153 db_printf("%s=", *argnp++);
154 db_printf("%r", db_get_value((int)argp, 4, FALSE));
155 argp++;
156 if (--narg != 0)
157 db_printf(",");
159 db_printf(") at ");
160 db_printsym(callpc, DB_STGY_PROC);
161 db_printf("\n");
165 * Figure out the next frame up in the call stack.
167 static void
168 db_nextframe(struct i386_frame **fp, db_addr_t *ip)
170 struct trapframe *tf;
171 int frame_type;
172 int eip, esp, ebp;
173 db_expr_t offset;
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.
183 frame_type = NORMAL;
185 sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
186 db_symbol_values(sym, &name, NULL);
187 dl_symbol_values(eip, &name);
188 if (name != NULL) {
189 if (!strcmp(name, "calltrap")) {
190 frame_type = TRAP;
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;
204 return;
207 db_print_stack_entry(name, 0, 0, 0, eip);
210 * Point to base of trapframe which is just above the
211 * current frame.
213 tf = (struct trapframe *) ((int)*fp + 8);
215 #if 0
216 esp = (ISPL(tf->tf_cs) == SEL_UPL) ? tf->tf_esp : (int)&tf->tf_esp;
217 #endif
218 esp = (int)&tf->tf_esp;
220 switch (frame_type) {
221 case TRAP:
223 eip = tf->tf_eip;
224 ebp = tf->tf_ebp;
225 db_printf(
226 "--- trap %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
227 tf->tf_trapno, eip, esp, ebp);
229 break;
230 case SYSCALL:
232 eip = tf->tf_eip;
233 ebp = tf->tf_ebp;
234 db_printf(
235 "--- syscall %#r, eip = %#r, esp = %#r, ebp = %#r ---\n",
236 tf->tf_eax, eip, esp, ebp);
238 break;
239 case INTERRUPT:
240 tf = (struct trapframe *)((int)*fp + 16);
242 eip = tf->tf_eip;
243 ebp = tf->tf_ebp;
244 db_printf(
245 "--- interrupt, eip = %#r, esp = %#r, ebp = %#r ---\n",
246 eip, esp, ebp);
248 break;
249 default:
250 break;
253 *ip = (db_addr_t) eip;
254 *fp = (struct i386_frame *) ebp;
257 void
258 db_stack_trace_cmd(db_expr_t addr, boolean_t have_addr, db_expr_t count,
259 char *modif)
261 struct i386_frame *frame;
262 int *argp;
263 db_addr_t callpc;
264 boolean_t first;
265 int i;
267 if (count == -1)
268 count = 1024;
270 if (!have_addr) {
271 frame = (struct i386_frame *)BP_REGS(&ddb_regs);
272 if (frame == NULL)
273 frame = (struct i386_frame *)(SP_REGS(&ddb_regs) - 4);
274 callpc = PC_REGS(&ddb_regs);
275 } else {
277 * Look for something that might be a frame pointer, just as
278 * a convenience.
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
288 break;
290 db_printf("%p does not look like a stack frame, skipping\n", (char *)&frame->f_frame + i);
292 if (i == 4096) {
293 db_printf("Unable to find anything that looks like a stack frame\n");
294 return;
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);
301 first = TRUE;
302 while (count--) {
303 struct i386_frame *actframe;
304 int narg;
305 const char * name;
306 db_expr_t offset;
307 c_db_sym_t sym;
308 #define MAXNARG 16
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).
326 actframe = frame;
327 if (first) {
328 if (!have_addr) {
329 int instr;
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 *)
339 SP_REGS(&ddb_regs);
340 if (ddb_regs.tf_ebp == 0) {
341 /* Fake caller's frame better. */
342 frame = actframe;
344 } else if ((instr & 0x000000ff) == 0x000000c3) {
345 /* ret */
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);
360 break;
362 first = FALSE;
365 argp = &actframe->f_arg0;
366 narg = MAXNARG;
367 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
368 argnp = argnames;
369 } else {
370 narg = db_numargs(frame);
373 db_print_stack_entry(name, narg, argnp, argp, callpc);
375 if (actframe != frame) {
376 /* `frame' belongs to caller. */
377 callpc = (db_addr_t)
378 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
379 continue;
382 db_nextframe(&frame, &callpc);
383 if (frame == 0)
384 break;
388 void
389 db_print_backtrace(void)
391 register_t ebp;
393 __asm __volatile("movl %%ebp, %0" : "=r" (ebp));
394 db_stack_trace_cmd(ebp, 1, -1, NULL);
397 static int
398 db_dr(struct db_variable *vp, db_expr_t *valuep, int op)
400 if (op == DB_VAR_GET)
401 *valuep = 0;
402 return(-1);
406 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
408 return(-1);
412 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
414 return(-1);
417 void
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.
426 static
427 void
428 dl_symbol_values(int callpc, const char **name)
430 Dl_info info;
432 if (*name == NULL) {
433 if (dladdr((const void *)callpc, &info) != 0) {
434 if (info.dli_saddr <= (const void *)callpc)
435 *name = info.dli_sname;