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.
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/linker_set.h>
33 #include <sys/sysent.h>
36 #include <machine/cpu.h>
39 #include <vm/vm_param.h>
43 #include <ddb/db_access.h>
44 #include <ddb/db_sym.h>
45 #include <ddb/db_variables.h>
57 * Machine register set.
59 struct db_variable db_regs[] = {
60 { "cs", &ddb_regs.tf_cs, FCN_NULL },
61 { "ds", &ddb_regs.tf_ds, FCN_NULL },
62 { "es", &ddb_regs.tf_es, FCN_NULL },
63 { "fs", &ddb_regs.tf_fs, FCN_NULL },
65 { "gs", &ddb_regs.tf_gs, FCN_NULL },
67 { "ss", &ddb_regs.tf_ss, FCN_NULL },
68 { "eax", &ddb_regs.tf_eax, FCN_NULL },
69 { "ecx", &ddb_regs.tf_ecx, FCN_NULL },
70 { "edx", &ddb_regs.tf_edx, FCN_NULL },
71 { "ebx", &ddb_regs.tf_ebx, FCN_NULL },
72 { "esp", &ddb_regs.tf_esp, FCN_NULL },
73 { "ebp", &ddb_regs.tf_ebp, FCN_NULL },
74 { "esi", &ddb_regs.tf_esi, FCN_NULL },
75 { "edi", &ddb_regs.tf_edi, FCN_NULL },
76 { "eip", &ddb_regs.tf_eip, FCN_NULL },
77 { "efl", &ddb_regs.tf_eflags, FCN_NULL },
78 { "dr0", NULL, db_dr0 },
79 { "dr1", NULL, db_dr1 },
80 { "dr2", NULL, db_dr2 },
81 { "dr3", NULL, db_dr3 },
82 { "dr4", NULL, db_dr4 },
83 { "dr5", NULL, db_dr5 },
84 { "dr6", NULL, db_dr6 },
85 { "dr7", NULL, db_dr7 },
87 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
92 #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK)
95 struct i386_frame *f_frame;
105 static void db_nextframe __P((struct i386_frame **, db_addr_t *, struct proc *));
106 static int db_numargs __P((struct i386_frame *));
107 static void db_print_stack_entry __P((const char *, int, char **, int *, db_addr_t));
108 static void decode_syscall __P((int, struct proc *));
111 * Figure out how many arguments were passed into the frame at "fp".
115 struct i386_frame *fp;
121 argp = (int *)db_get_value((int)&fp->f_retaddr, 4, FALSE);
123 * XXX etext is wrong for LKMs. We should attempt to interpret
124 * the instruction at the return address in all cases. This
125 * may require better fault handling.
127 if (argp < (int *)btext || argp >= (int *)etext) {
130 inst = db_get_value((int)argp, 4, FALSE);
131 if ((inst & 0xff) == 0x59) /* popl %ecx */
133 else if ((inst & 0xffff) == 0xc483) /* addl $Ibs, %esp */
134 args = ((inst >> 16) & 0xff) / 4;
142 db_print_stack_entry(name, narg, argnp, argp, callpc)
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 decode_syscall(number, p)
173 db_printf(" (%d", number);
174 if (p != NULL && 0 <= number && number < p->p_sysent->sv_size) {
175 f = p->p_sysent->sv_table[number].sy_call;
176 sym = db_search_symbol((db_addr_t)f, DB_STGY_ANY, &diff);
177 if (sym != DB_SYM_NULL && diff == 0) {
178 db_symbol_values(sym, &symname, NULL);
179 db_printf(", %s, %s", p->p_sysent->sv_name, symname);
186 * Figure out the next frame up in the call stack.
189 db_nextframe(fp, ip, p)
190 struct i386_frame **fp; /* in/out */
191 db_addr_t *ip; /* out */
192 struct proc *p; /* in */
194 struct trapframe *tf;
198 const char *sym, *name;
200 eip = db_get_value((int) &(*fp)->f_retaddr, 4, FALSE);
201 ebp = db_get_value((int) &(*fp)->f_frame, 4, FALSE);
204 * Figure out frame type.
209 sym = db_search_symbol(eip, DB_STGY_ANY, &offset);
210 db_symbol_values(sym, &name, NULL);
212 if (!strcmp(name, "calltrap")) {
214 } else if (!strncmp(name, "Xresume", 7)) {
215 frame_type = INTERRUPT;
216 } else if (!strcmp(name, "syscall_with_err_pushed")) {
217 frame_type = SYSCALL;
222 * Normal frames need no special processing.
224 if (frame_type == NORMAL) {
225 *ip = (db_addr_t) eip;
226 *fp = (struct i386_frame *) ebp;
230 db_print_stack_entry(name, 0, 0, 0, eip);
233 * Point to base of trapframe which is just above the
236 if (frame_type == INTERRUPT)
237 tf = (struct trapframe *)((int)*fp + 12);
239 tf = (struct trapframe *)((int)*fp + 8);
241 if (INKERNEL((int) tf)) {
242 esp = (ISPL(tf->tf_cs) == SEL_UPL) ?
243 tf->tf_esp : (int)&tf->tf_esp;
247 switch (frame_type) {
249 db_printf("--- trap %#r", tf->tf_trapno);
252 db_printf("--- syscall");
253 decode_syscall(tf->tf_eax, p);
256 db_printf("--- interrupt");
259 panic("The moon has moved again.");
261 db_printf(", eip = %#r, esp = %#r, ebp = %#r ---\n", eip,
265 *ip = (db_addr_t) eip;
266 *fp = (struct i386_frame *) ebp;
270 db_stack_trace_cmd(addr, have_addr, count, modif)
276 struct i386_frame *frame;
289 frame = (struct i386_frame *)ddb_regs.tf_ebp;
291 frame = (struct i386_frame *)(ddb_regs.tf_esp - 4);
292 callpc = (db_addr_t)ddb_regs.tf_eip;
293 } else if (!INKERNEL(addr)) {
294 pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
295 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
296 ((addr >> 16) % 16) * 10000;
298 * The pcb for curproc is not valid at this point,
299 * so fall back to the default case.
301 if (pid == curproc->p_pid) {
303 frame = (struct i386_frame *)ddb_regs.tf_ebp;
305 frame = (struct i386_frame *)
306 (ddb_regs.tf_esp - 4);
307 callpc = (db_addr_t)ddb_regs.tf_eip;
310 /* sx_slock(&allproc_lock); */
311 LIST_FOREACH(p, &allproc, p_list) {
315 /* sx_sunlock(&allproc_lock); */
317 db_printf("pid %d not found\n", pid);
320 if ((p->p_sflag & PS_INMEM) == 0) {
321 db_printf("pid %d swapped out\n", pid);
324 pcb = &p->p_addr->u_pcb;
325 frame = (struct i386_frame *)pcb->pcb_ebp;
327 frame = (struct i386_frame *)
329 callpc = (db_addr_t)pcb->pcb_eip;
333 frame = (struct i386_frame *)addr;
334 callpc = (db_addr_t)db_get_value((int)&frame->f_retaddr, 4, FALSE);
339 struct i386_frame *actframe;
345 char *argnames[MAXNARG], **argnp = NULL;
347 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
348 db_symbol_values(sym, &name, NULL);
351 * Attempt to determine a (possibly fake) frame that gives
352 * the caller's pc. It may differ from `frame' if the
353 * current function never sets up a standard frame or hasn't
354 * set one up yet or has just discarded one. The last two
355 * cases can be guessed fairly reliably for code generated
356 * by gcc. The first case is too much trouble to handle in
357 * general because the amount of junk on the stack depends
358 * on the pc (the special handling of "calltrap", etc. in
359 * db_nextframe() works because the `next' pc is special).
366 instr = db_get_value(callpc, 4, FALSE);
367 if ((instr & 0x00ffffff) == 0x00e58955) {
368 /* pushl %ebp; movl %esp, %ebp */
369 actframe = (struct i386_frame *)
370 (ddb_regs.tf_esp - 4);
371 } else if ((instr & 0x0000ffff) == 0x0000e589) {
372 /* movl %esp, %ebp */
373 actframe = (struct i386_frame *)
375 if (ddb_regs.tf_ebp == 0) {
376 /* Fake caller's frame better. */
379 } else if ((instr & 0x000000ff) == 0x000000c3) {
381 actframe = (struct i386_frame *)
382 (ddb_regs.tf_esp - 4);
383 } else if (offset == 0) {
384 /* Probably a symbol in assembler code. */
385 actframe = (struct i386_frame *)
386 (ddb_regs.tf_esp - 4);
388 } else if (!strcmp(name, "fork_trampoline")) {
390 * Don't try to walk back on a stack for a
391 * process that hasn't actually been run yet.
393 db_print_stack_entry(name, 0, 0, 0, callpc);
399 argp = &actframe->f_arg0;
401 if (sym != NULL && db_sym_numargs(sym, &narg, argnames)) {
404 narg = db_numargs(frame);
407 db_print_stack_entry(name, narg, argnp, argp, callpc);
409 if (actframe != frame) {
410 /* `frame' belongs to caller. */
412 db_get_value((int)&actframe->f_retaddr, 4, FALSE);
416 db_nextframe(&frame, &callpc, p);
418 if (INKERNEL((int) callpc) && !INKERNEL((int) frame)) {
419 sym = db_search_symbol(callpc, DB_STGY_ANY, &offset);
420 db_symbol_values(sym, &name, NULL);
421 db_print_stack_entry(name, 0, 0, 0, callpc);
424 if (!INKERNEL((int) frame)) {
430 #define DB_DRX_FUNC(reg) \
432 db_ ## reg (vp, valuep, op) \
433 struct db_variable *vp; \
434 db_expr_t * valuep; \
437 if (op == DB_VAR_GET) \
438 *valuep = r ## reg (); \
440 load_ ## reg (*valuep); \