2 * Copyright (c) 2004-2005, Juniper Networks, Inc.
5 * JNPR: db_trace.c,v 1.8 2007/08/09 11:23:32 katta
11 #include <sys/param.h>
12 #include <sys/systm.h>
15 #include <sys/stack.h>
16 #include <sys/sysent.h>
18 #include <machine/db_machdep.h>
19 #include <machine/md_var.h>
20 #include <machine/mips_opcode.h>
21 #include <machine/pcb.h>
22 #include <machine/trap.h>
25 #include <ddb/db_sym.h>
27 extern char _locore[];
28 extern char _locoreEnd[];
32 * A function using a stack frame has the following instruction as the first
33 * one: addiu sp,sp,-<frame_size>
35 * We make use of this to detect starting address of a function. This works
36 * better than using 'j ra' instruction to signify end of the previous
37 * function (for e.g. functions like boot() or panic() do not actually
38 * emit a 'j ra' instruction).
40 * XXX the abi does not require that the addiu instruction be the first one.
42 #define MIPS_START_OF_FUNCTION(ins) (((ins) & 0xffff8000) == 0x27bd8000)
45 * MIPS ABI 3.0 requires that all functions return using the 'j ra' instruction
47 * XXX gcc doesn't do this for functions with __noreturn__ attribute.
49 #define MIPS_END_OF_FUNCTION(ins) ((ins) == 0x03e00008)
52 * kdbpeekD(addr) - skip one word starting at 'addr', then read the second word
54 #define kdbpeekD(addr) kdbpeek(((int *)(addr)) + 1)
57 * Functions ``special'' enough to print by name
60 #define Name(_fn) { (void*)_fn, # _fn }
62 #define Name(_fn) { _fn, "_fn"}
70 Name(MipsKernGenException),
71 Name(MipsUserGenException),
81 * Map a function address to a string name, if known; or a hex string.
84 fn_name(uintptr_t addr)
95 sym = db_search_symbol((db_addr_t)addr, DB_STGY_ANY, &diff);
96 db_symbol_values(sym, (const char **)&symname, (db_expr_t *)0);
97 if (symname && diff == 0)
100 for (i = 0; names[i].name; i++)
101 if (names[i].addr == (void *)addr)
102 return (names[i].name);
103 sprintf(buf, "%jx", (uintmax_t)addr);
108 stacktrace_subr(register_t pc, register_t sp, register_t ra,
109 int (*printfn) (const char *,...))
113 * Arrays for a0..a3 registers and flags if content
114 * of these registers is valid, e.g. obtained from the stack
119 unsigned instr, mask;
120 unsigned int frames = 0;
121 int more, stksize, j;
123 /* Jump here when done with a frame, to start a new one */
127 * Invalidate arguments values
133 /* Jump here after a nonstandard (interrupt handler) frame */
136 if (frames++ > 100) {
137 (*printfn) ("\nstackframe count exceeded\n");
138 /* return breaks stackframe-size heuristics with gcc -O2 */
139 goto finish; /* XXX */
141 /* check for bad SP: could foul up next frame */
142 /*XXX MIPS64 bad: this hard-coded SP is lame */
143 if (sp & 3 || (uintptr_t)sp < 0x80000000u) {
144 (*printfn) ("SP 0x%x: not in kernel\n", sp);
149 #define Between(x, y, z) \
150 ( ((x) <= (y)) && ((y) < (z)) )
151 #define pcBetween(a,b) \
152 Between((uintptr_t)a, pc, (uintptr_t)b)
155 * Check for current PC in exception handler code that don't have a
156 * preceding "j ra" at the tail of the preceding function. Depends
157 * on relative ordering of functions in exception.S, swtch.S.
159 if (pcBetween(MipsKernGenException, MipsUserGenException))
160 subr = (uintptr_t)MipsKernGenException;
161 else if (pcBetween(MipsUserGenException, MipsKernIntr))
162 subr = (uintptr_t)MipsUserGenException;
163 else if (pcBetween(MipsKernIntr, MipsUserIntr))
164 subr = (uintptr_t)MipsKernIntr;
165 else if (pcBetween(MipsUserIntr, MipsTLBInvalidException))
166 subr = (uintptr_t)MipsUserIntr;
167 else if (pcBetween(MipsTLBInvalidException, MipsTLBMissException))
168 subr = (uintptr_t)MipsTLBInvalidException;
169 else if (pcBetween(fork_trampoline, savectx))
170 subr = (uintptr_t)fork_trampoline;
171 else if (pcBetween(savectx, cpu_throw))
172 subr = (uintptr_t)savectx;
173 else if (pcBetween(cpu_throw, cpu_switch))
174 subr = (uintptr_t)cpu_throw;
175 else if (pcBetween(cpu_switch, MipsSwitchFPState))
176 subr = (uintptr_t)cpu_switch;
177 else if (pcBetween(_locore, _locoreEnd)) {
178 subr = (uintptr_t)_locore;
182 /* check for bad PC */
183 /*XXX MIPS64 bad: These hard coded constants are lame */
184 if (pc & 3 || pc < (uintptr_t)0x80000000) {
185 (*printfn) ("PC 0x%x: not in kernel\n", pc);
190 * Find the beginning of the current subroutine by scanning
191 * backwards from the current PC for the end of the previous
195 va = pc - sizeof(int);
197 instr = kdbpeek((int *)va);
199 if (MIPS_START_OF_FUNCTION(instr))
202 if (MIPS_END_OF_FUNCTION(instr)) {
203 /* skip over branch-delay slot instruction */
204 va += 2 * sizeof(int);
211 /* skip over nulls which might separate .o files */
212 while ((instr = kdbpeek((int *)va)) == 0)
216 /* scan forwards to find stack size and any saved registers */
220 for (va = subr; more; va += sizeof(int),
221 more = (more == 3) ? 3 : more - 1) {
222 /* stop if hit our current position */
225 instr = kdbpeek((int *)va);
227 switch (i.JType.op) {
229 switch (i.RType.func) {
232 more = 2; /* stop after next instruction */
237 more = 1; /* stop now */
248 more = 2; /* stop after next instruction */
255 switch (i.RType.rs) {
258 more = 2; /* stop after next instruction */
263 /* look for saved registers on the stack */
264 if (i.IType.rs != 29)
266 /* only restore the first one */
267 if (mask & (1 << i.IType.rt))
269 mask |= (1 << i.IType.rt);
270 switch (i.IType.rt) {
272 args[0] = kdbpeek((int *)(sp + (short)i.IType.imm));
277 args[1] = kdbpeek((int *)(sp + (short)i.IType.imm));
282 args[2] = kdbpeek((int *)(sp + (short)i.IType.imm));
287 args[3] = kdbpeek((int *)(sp + (short)i.IType.imm));
292 ra = kdbpeek((int *)(sp + (short)i.IType.imm));
297 /* look for saved registers on the stack */
298 if (i.IType.rs != 29)
300 /* only restore the first one */
301 if (mask & (1 << i.IType.rt))
303 mask |= (1 << i.IType.rt);
304 switch (i.IType.rt) {
306 args[0] = kdbpeekD((int *)(sp + (short)i.IType.imm));
311 args[1] = kdbpeekD((int *)(sp + (short)i.IType.imm));
316 args[2] = kdbpeekD((int *)(sp + (short)i.IType.imm));
321 args[3] = kdbpeekD((int *)(sp + (short)i.IType.imm));
326 ra = kdbpeekD((int *)(sp + (short)i.IType.imm));
332 /* look for stack pointer adjustment */
333 if (i.IType.rs != 29 || i.IType.rt != 29)
335 stksize = -((short)i.IType.imm);
340 (*printfn) ("%s+%x (", fn_name(subr), pc - subr);
341 for (j = 0; j < 4; j ++) {
345 (*printfn)("%x", args[j]);
350 (*printfn) (") ra %x sp %x sz %d\n", ra, sp, stksize);
353 if (pc == ra && stksize == 0)
354 (*printfn) ("stacktrace: loop!\n");
364 (*printfn) ("pid %d\n", curproc->p_pid);
366 (*printfn) ("curproc NULL\n");
372 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
380 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
388 db_md_list_watchpoints()
395 db_trace_thread (curthread, -1);
400 db_trace_thread(struct thread *thr, int count)
402 register_t pc, ra, sp;
405 if (thr == curthread) {
406 sp = (register_t)(intptr_t)__builtin_frame_address(0);
407 ra = (register_t)(intptr_t)__builtin_return_address(0);
413 "move %0, $31\n" /* get ra */
414 "move $31, %1\n" /* restore ra */
419 ctx = kdb_thr_ctx(thr);
420 sp = (register_t)ctx->pcb_context[PREG_SP];
421 pc = (register_t)ctx->pcb_context[PREG_PC];
422 ra = (register_t)ctx->pcb_context[PREG_RA];
425 stacktrace_subr(pc, sp, ra,
426 (int (*) (const char *, ...))db_printf);
432 db_show_mdpcpu(struct pcpu *pc)
435 db_printf("ipis = 0x%x\n", pc->pc_pending_ipis);
436 db_printf("next ASID = %d\n", pc->pc_next_asid);
437 db_printf("GENID = %d\n", pc->pc_asid_generation);