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/asm.h>
40 #include <machine/db_machdep.h>
41 #include <machine/md_var.h>
42 #include <machine/mips_opcode.h>
43 #include <machine/pcb.h>
44 #include <machine/trap.h>
47 #include <ddb/db_sym.h>
49 extern char _locore[];
50 extern char _locoreEnd[];
54 * A function using a stack frame has the following instruction as the first
55 * one: [d]addiu sp,sp,-<frame_size>
57 * We make use of this to detect starting address of a function. This works
58 * better than using 'j ra' instruction to signify end of the previous
59 * function (for e.g. functions like boot() or panic() do not actually
60 * emit a 'j ra' instruction).
62 * XXX the abi does not require that the addiu instruction be the first one.
64 #define MIPS_START_OF_FUNCTION(ins) ((((ins) & 0xffff8000) == 0x27bd8000) \
65 || (((ins) & 0xffff8000) == 0x67bd8000))
68 * MIPS ABI 3.0 requires that all functions return using the 'j ra' instruction
70 * XXX gcc doesn't do this for functions with __noreturn__ attribute.
72 #define MIPS_END_OF_FUNCTION(ins) ((ins) == 0x03e00008)
74 #if defined(__mips_n64)
75 # define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \
76 ((vm_offset_t)(reg) >= MIPS_XKPHYS_START))
78 # define MIPS_IS_VALID_KERNELADDR(reg) ((((reg) & 3) == 0) && \
79 ((vm_offset_t)(reg) >= MIPS_KSEG0_START))
83 * Functions ``special'' enough to print by name
86 #define Name(_fn) { (void*)_fn, # _fn }
88 #define Name(_fn) { _fn, "_fn"}
96 Name(MipsKernGenException),
97 Name(MipsUserGenException),
107 * Map a function address to a string name, if known; or a hex string.
110 fn_name(uintptr_t addr)
121 sym = db_search_symbol((db_addr_t)addr, DB_STGY_ANY, &diff);
122 db_symbol_values(sym, &symname, NULL);
123 if (symname && diff == 0)
126 for (i = 0; names[i].name; i++)
127 if (names[i].addr == (void *)addr)
128 return (names[i].name);
129 sprintf(buf, "%jx", (uintmax_t)addr);
134 stacktrace_subr(register_t pc, register_t sp, register_t ra,
135 int (*printfn) (const char *,...))
139 * Arrays for a0..a3 registers and flags if content
140 * of these registers is valid, e.g. obtained from the stack
145 unsigned instr, mask;
146 unsigned int frames = 0;
147 int more, stksize, j;
150 /* Jump here when done with a frame, to start a new one */
154 * Invalidate arguments values
163 if (frames++ > 100) {
164 (*printfn) ("\nstackframe count exceeded\n");
165 /* return breaks stackframe-size heuristics with gcc -O2 */
166 goto finish; /* XXX */
168 /* check for bad SP: could foul up next frame */
169 /*XXX MIPS64 bad: this hard-coded SP is lame */
170 if (!MIPS_IS_VALID_KERNELADDR(sp)) {
171 (*printfn) ("SP 0x%jx: not in kernel\n", sp);
176 #define Between(x, y, z) \
177 ( ((x) <= (y)) && ((y) < (z)) )
178 #define pcBetween(a,b) \
179 Between((uintptr_t)a, pc, (uintptr_t)b)
182 * Check for current PC in exception handler code that don't have a
183 * preceding "j ra" at the tail of the preceding function. Depends
184 * on relative ordering of functions in exception.S, swtch.S.
186 if (pcBetween(MipsKernGenException, MipsUserGenException))
187 subr = (uintptr_t)MipsKernGenException;
188 else if (pcBetween(MipsUserGenException, MipsKernIntr))
189 subr = (uintptr_t)MipsUserGenException;
190 else if (pcBetween(MipsKernIntr, MipsUserIntr))
191 subr = (uintptr_t)MipsKernIntr;
192 else if (pcBetween(MipsUserIntr, MipsTLBInvalidException))
193 subr = (uintptr_t)MipsUserIntr;
194 else if (pcBetween(MipsTLBInvalidException, MipsTLBMissException))
195 subr = (uintptr_t)MipsTLBInvalidException;
196 else if (pcBetween(fork_trampoline, savectx))
197 subr = (uintptr_t)fork_trampoline;
198 else if (pcBetween(savectx, cpu_throw))
199 subr = (uintptr_t)savectx;
200 else if (pcBetween(cpu_throw, cpu_switch))
201 subr = (uintptr_t)cpu_throw;
202 else if (pcBetween(cpu_switch, MipsSwitchFPState))
203 subr = (uintptr_t)cpu_switch;
204 else if (pcBetween(_locore, _locoreEnd)) {
205 subr = (uintptr_t)_locore;
209 /* check for bad PC */
210 /*XXX MIPS64 bad: These hard coded constants are lame */
211 if (!MIPS_IS_VALID_KERNELADDR(pc)) {
212 (*printfn) ("PC 0x%jx: not in kernel\n", pc);
218 * For a kernel stack overflow, skip to the output and
219 * afterwards pull the previous registers out of the trapframe
220 * instead of decoding the function prologue.
222 if (pc == (uintptr_t)MipsKStackOverflow)
226 * Find the beginning of the current subroutine by scanning
227 * backwards from the current PC for the end of the previous
231 va = pc - sizeof(int);
233 instr = kdbpeek((int *)va);
235 if (MIPS_START_OF_FUNCTION(instr))
238 if (MIPS_END_OF_FUNCTION(instr)) {
239 /* skip over branch-delay slot instruction */
240 va += 2 * sizeof(int);
247 /* skip over nulls which might separate .o files */
248 while ((instr = kdbpeek((int *)va)) == 0)
252 /* scan forwards to find stack size and any saved registers */
256 for (va = subr; more; va += sizeof(int),
257 more = (more == 3) ? 3 : more - 1) {
258 /* stop if hit our current position */
261 instr = kdbpeek((int *)va);
263 switch (i.JType.op) {
265 switch (i.RType.func) {
268 more = 2; /* stop after next instruction */
273 more = 1; /* stop now */
284 more = 2; /* stop after next instruction */
291 switch (i.RType.rs) {
294 more = 2; /* stop after next instruction */
299 /* look for saved registers on the stack */
300 if (i.IType.rs != 29)
303 * only restore the first one except RA for
304 * MipsKernGenException case
306 if (mask & (1 << i.IType.rt)) {
307 if (subr == (uintptr_t)MipsKernGenException &&
309 next_ra = kdbpeek((int *)(sp +
310 (short)i.IType.imm));
313 mask |= (1 << i.IType.rt);
314 switch (i.IType.rt) {
316 args[0] = kdbpeek((int *)(sp + (short)i.IType.imm));
321 args[1] = kdbpeek((int *)(sp + (short)i.IType.imm));
326 args[2] = kdbpeek((int *)(sp + (short)i.IType.imm));
331 args[3] = kdbpeek((int *)(sp + (short)i.IType.imm));
336 ra = kdbpeek((int *)(sp + (short)i.IType.imm));
341 /* look for saved registers on the stack */
342 if (i.IType.rs != 29)
344 /* only restore the first one */
345 if (mask & (1 << i.IType.rt))
347 mask |= (1 << i.IType.rt);
348 switch (i.IType.rt) {
350 args[0] = kdbpeekd((int *)(sp + (short)i.IType.imm));
355 args[1] = kdbpeekd((int *)(sp + (short)i.IType.imm));
360 args[2] = kdbpeekd((int *)(sp + (short)i.IType.imm));
365 args[3] = kdbpeekd((int *)(sp + (short)i.IType.imm));
370 ra = kdbpeekd((int *)(sp + (short)i.IType.imm));
378 /* look for stack pointer adjustment */
379 if (i.IType.rs != 29 || i.IType.rt != 29)
381 stksize = -((short)i.IType.imm);
386 (*printfn) ("%s+%x (", fn_name(subr), pc - subr);
387 for (j = 0; j < 4; j ++) {
391 (*printfn)("%jx", (uintmax_t)(u_register_t)args[j]);
396 (*printfn) (") ra %jx sp %jx sz %d\n",
397 (uintmax_t)(u_register_t) ra,
398 (uintmax_t)(u_register_t) sp,
401 if (pc == (uintptr_t)MipsKStackOverflow) {
402 #define TF_REG(base, reg) ((base) + CALLFRAME_SIZ + ((reg) * SZREG))
403 #if defined(__mips_n64) || defined(__mips_n32)
404 pc = kdbpeekd((int *)TF_REG(sp, PC));
405 ra = kdbpeekd((int *)TF_REG(sp, RA));
406 sp = kdbpeekd((int *)TF_REG(sp, SP));
408 pc = kdbpeek((int *)TF_REG(sp, PC));
409 ra = kdbpeek((int *)TF_REG(sp, RA));
410 sp = kdbpeek((int *)TF_REG(sp, SP));
413 (*printfn) ("--- Kernel Stack Overflow ---\n");
416 if (pc == ra && stksize == 0)
417 (*printfn) ("stacktrace: loop!\n");
427 (*printfn) ("pid %d\n", curproc->p_pid);
429 (*printfn) ("curproc NULL\n");
435 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
443 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
451 db_md_list_watchpoints()
458 register_t pc, ra, sp;
460 sp = (register_t)(intptr_t)__builtin_frame_address(0);
461 ra = (register_t)(intptr_t)__builtin_return_address(0);
467 "move %0, $31\n" /* get ra */
468 "move $31, %1\n" /* restore ra */
471 stacktrace_subr(pc, sp, ra, db_printf);
476 db_trace_thread(struct thread *thr, int count)
478 register_t pc, ra, sp;
481 ctx = kdb_thr_ctx(thr);
482 sp = (register_t)ctx->pcb_context[PCB_REG_SP];
483 pc = (register_t)ctx->pcb_context[PCB_REG_PC];
484 ra = (register_t)ctx->pcb_context[PCB_REG_RA];
485 stacktrace_subr(pc, sp, ra, db_printf);
491 db_show_mdpcpu(struct pcpu *pc)
494 db_printf("ipis = 0x%x\n", pc->pc_pending_ipis);
495 db_printf("next ASID = %d\n", pc->pc_next_asid);
496 db_printf("GENID = %d\n", pc->pc_asid_generation);