2 * Copyright (c) 2004-2005, Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * JNPR: db_trace.c,v 1.8 2007/08/09 11:23:32 katta
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/stack.h>
37 #include <sys/sysent.h>
39 #include <machine/db_machdep.h>
40 #include <machine/md_var.h>
41 #include <machine/mips_opcode.h>
42 #include <machine/pcb.h>
43 #include <machine/trap.h>
46 #include <ddb/db_sym.h>
48 extern char _locore[];
49 extern char _locoreEnd[];
53 * A function using a stack frame has the following instruction as the first
54 * one: [d]addiu sp,sp,-<frame_size>
56 * We make use of this to detect starting address of a function. This works
57 * better than using 'j ra' instruction to signify end of the previous
58 * function (for e.g. functions like boot() or panic() do not actually
59 * emit a 'j ra' instruction).
61 * XXX the abi does not require that the addiu instruction be the first one.
63 #define MIPS_START_OF_FUNCTION(ins) ((((ins) & 0xffff8000) == 0x27bd8000) \
64 || (((ins) & 0xffff8000) == 0x67bd8000))
67 * MIPS ABI 3.0 requires that all functions return using the 'j ra' instruction
69 * XXX gcc doesn't do this for functions with __noreturn__ attribute.
71 #define MIPS_END_OF_FUNCTION(ins) ((ins) == 0x03e00008)
73 #if defined(__mips_n64)
74 # define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \
75 ((vm_offset_t)(reg) >= MIPS_XKPHYS_START))
77 # define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \
78 ((vm_offset_t)(reg) >= MIPS_KSEG0_START))
82 * Functions ``special'' enough to print by name
85 #define Name(_fn) { (void*)_fn, # _fn }
87 #define Name(_fn) { _fn, "_fn"}
95 Name(MipsKernGenException),
96 Name(MipsUserGenException),
106 * Map a function address to a string name, if known; or a hex string.
109 fn_name(uintptr_t addr)
120 sym = db_search_symbol((db_addr_t)addr, DB_STGY_ANY, &diff);
121 db_symbol_values(sym, (const char **)&symname, (db_expr_t *)0);
122 if (symname && diff == 0)
125 for (i = 0; names[i].name; i++)
126 if (names[i].addr == (void *)addr)
127 return (names[i].name);
128 sprintf(buf, "%jx", (uintmax_t)addr);
133 stacktrace_subr(register_t pc, register_t sp, register_t ra,
134 int (*printfn) (const char *,...))
138 * Arrays for a0..a3 registers and flags if content
139 * of these registers is valid, e.g. obtained from the stack
144 unsigned instr, mask;
145 unsigned int frames = 0;
146 int more, stksize, j;
148 /* Jump here when done with a frame, to start a new one */
152 * Invalidate arguments values
158 /* Jump here after a nonstandard (interrupt handler) frame */
161 if (frames++ > 100) {
162 (*printfn) ("\nstackframe count exceeded\n");
163 /* return breaks stackframe-size heuristics with gcc -O2 */
164 goto finish; /* XXX */
166 /* check for bad SP: could foul up next frame */
167 /*XXX MIPS64 bad: this hard-coded SP is lame */
168 if (!MIPS_IS_VALID_KERNELADDR(sp)) {
169 (*printfn) ("SP 0x%jx: not in kernel\n", sp);
174 #define Between(x, y, z) \
175 ( ((x) <= (y)) && ((y) < (z)) )
176 #define pcBetween(a,b) \
177 Between((uintptr_t)a, pc, (uintptr_t)b)
180 * Check for current PC in exception handler code that don't have a
181 * preceding "j ra" at the tail of the preceding function. Depends
182 * on relative ordering of functions in exception.S, swtch.S.
184 if (pcBetween(MipsKernGenException, MipsUserGenException))
185 subr = (uintptr_t)MipsKernGenException;
186 else if (pcBetween(MipsUserGenException, MipsKernIntr))
187 subr = (uintptr_t)MipsUserGenException;
188 else if (pcBetween(MipsKernIntr, MipsUserIntr))
189 subr = (uintptr_t)MipsKernIntr;
190 else if (pcBetween(MipsUserIntr, MipsTLBInvalidException))
191 subr = (uintptr_t)MipsUserIntr;
192 else if (pcBetween(MipsTLBInvalidException, MipsTLBMissException))
193 subr = (uintptr_t)MipsTLBInvalidException;
194 else if (pcBetween(fork_trampoline, savectx))
195 subr = (uintptr_t)fork_trampoline;
196 else if (pcBetween(savectx, cpu_throw))
197 subr = (uintptr_t)savectx;
198 else if (pcBetween(cpu_throw, cpu_switch))
199 subr = (uintptr_t)cpu_throw;
200 else if (pcBetween(cpu_switch, MipsSwitchFPState))
201 subr = (uintptr_t)cpu_switch;
202 else if (pcBetween(_locore, _locoreEnd)) {
203 subr = (uintptr_t)_locore;
207 /* check for bad PC */
208 /*XXX MIPS64 bad: These hard coded constants are lame */
209 if (!MIPS_IS_VALID_KERNELADDR(pc)) {
210 (*printfn) ("PC 0x%jx: not in kernel\n", pc);
215 * Find the beginning of the current subroutine by scanning
216 * backwards from the current PC for the end of the previous
220 va = pc - sizeof(int);
222 instr = kdbpeek((int *)va);
224 if (MIPS_START_OF_FUNCTION(instr))
227 if (MIPS_END_OF_FUNCTION(instr)) {
228 /* skip over branch-delay slot instruction */
229 va += 2 * sizeof(int);
236 /* skip over nulls which might separate .o files */
237 while ((instr = kdbpeek((int *)va)) == 0)
241 /* scan forwards to find stack size and any saved registers */
245 for (va = subr; more; va += sizeof(int),
246 more = (more == 3) ? 3 : more - 1) {
247 /* stop if hit our current position */
250 instr = kdbpeek((int *)va);
252 switch (i.JType.op) {
254 switch (i.RType.func) {
257 more = 2; /* stop after next instruction */
262 more = 1; /* stop now */
273 more = 2; /* stop after next instruction */
280 switch (i.RType.rs) {
283 more = 2; /* stop after next instruction */
288 /* look for saved registers on the stack */
289 if (i.IType.rs != 29)
291 /* only restore the first one */
292 if (mask & (1 << i.IType.rt))
294 mask |= (1 << i.IType.rt);
295 switch (i.IType.rt) {
297 args[0] = kdbpeek((int *)(sp + (short)i.IType.imm));
302 args[1] = kdbpeek((int *)(sp + (short)i.IType.imm));
307 args[2] = kdbpeek((int *)(sp + (short)i.IType.imm));
312 args[3] = kdbpeek((int *)(sp + (short)i.IType.imm));
317 ra = kdbpeek((int *)(sp + (short)i.IType.imm));
322 /* look for saved registers on the stack */
323 if (i.IType.rs != 29)
325 /* only restore the first one */
326 if (mask & (1 << i.IType.rt))
328 mask |= (1 << i.IType.rt);
329 switch (i.IType.rt) {
331 args[0] = kdbpeekd((int *)(sp + (short)i.IType.imm));
336 args[1] = kdbpeekd((int *)(sp + (short)i.IType.imm));
341 args[2] = kdbpeekd((int *)(sp + (short)i.IType.imm));
346 args[3] = kdbpeekd((int *)(sp + (short)i.IType.imm));
351 ra = kdbpeekd((int *)(sp + (short)i.IType.imm));
359 /* look for stack pointer adjustment */
360 if (i.IType.rs != 29 || i.IType.rt != 29)
362 stksize = -((short)i.IType.imm);
367 (*printfn) ("%s+%x (", fn_name(subr), pc - subr);
368 for (j = 0; j < 4; j ++) {
372 (*printfn)("%x", args[j]);
377 (*printfn) (") ra %jx sp %jx sz %d\n", ra, sp, stksize);
380 if (pc == ra && stksize == 0)
381 (*printfn) ("stacktrace: loop!\n");
391 (*printfn) ("pid %d\n", curproc->p_pid);
393 (*printfn) ("curproc NULL\n");
399 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
407 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
415 db_md_list_watchpoints()
422 db_trace_thread (curthread, -1);
427 db_trace_thread(struct thread *thr, int count)
429 register_t pc, ra, sp;
432 if (thr == curthread) {
433 sp = (register_t)(intptr_t)__builtin_frame_address(0);
434 ra = (register_t)(intptr_t)__builtin_return_address(0);
440 "move %0, $31\n" /* get ra */
441 "move $31, %1\n" /* restore ra */
446 ctx = kdb_thr_ctx(thr);
447 sp = (register_t)ctx->pcb_context[PCB_REG_SP];
448 pc = (register_t)ctx->pcb_context[PCB_REG_PC];
449 ra = (register_t)ctx->pcb_context[PCB_REG_RA];
452 stacktrace_subr(pc, sp, ra,
453 (int (*) (const char *, ...))db_printf);
459 db_show_mdpcpu(struct pcpu *pc)
462 db_printf("ipis = 0x%x\n", pc->pc_pending_ipis);
463 db_printf("next ASID = %d\n", pc->pc_next_asid);
464 db_printf("GENID = %d\n", pc->pc_asid_generation);