1 /* $NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $ */
4 * Copyright (c) 2000, 2001 Ben Harris
5 * Copyright (c) 1996 Scott K. Stevens
7 * Mach Operating System
8 * Copyright (c) 1991,1990 Carnegie Mellon University
11 * Permission to use, copy, modify and distribute this software and its
12 * documentation is hereby granted, provided that both the copyright
13 * notice and this permission notice appear in all copies of the
14 * software, derivative works or modified versions, and any portions
15 * thereof, and that both notices appear in supporting documentation.
17 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
19 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
21 * Carnegie Mellon requests users of this software to return to
23 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
24 * School of Computer Science
25 * Carnegie Mellon University
26 * Pittsburgh PA 15213-3890
28 * any improvements or extensions that they make and grant Carnegie the
29 * rights to redistribute these changes.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
40 #include <sys/stack.h>
41 #include <machine/armreg.h>
42 #include <machine/asm.h>
43 #include <machine/cpufunc.h>
44 #include <machine/db_machdep.h>
45 #include <machine/pcb.h>
46 #include <machine/stack.h>
47 #include <machine/vmparam.h>
49 #include <ddb/db_access.h>
50 #include <ddb/db_sym.h>
51 #include <ddb/db_output.h>
55 * Definitions for the instruction interpreter.
57 * The ARM EABI specifies how to perform the frame unwinding in the
58 * Exception Handling ABI for the ARM Architecture document. To perform
59 * the unwind we need to know the initial frame pointer, stack pointer,
60 * link register and program counter. We then find the entry within the
61 * index table that points to the function the program counter is within.
62 * This gives us either a list of three instructions to process, a 31-bit
63 * relative offset to a table of instructions, or a value telling us
64 * we can't unwind any further.
66 * When we have the instructions to process we need to decode them
67 * following table 4 in section 9.3. This describes a collection of bit
68 * patterns to encode that steps to take to update the stack pointer and
69 * link register to the correct values at the start of the function.
72 /* A special case when we are unable to unwind past this function */
73 #define EXIDX_CANTUNWIND 1
75 /* The register names */
82 * These are set in the linker script. Their addresses will be
83 * either the start or end of the exception table or index.
85 extern int extab_start, extab_end, exidx_start, exidx_end;
89 * These are the only entry types that have been seen in the kernel.
91 #define ENTRY_MASK 0xff000000
92 #define ENTRY_ARM_SU16 0x80000000
93 #define ENTRY_ARM_LU16 0x81000000
95 /* Instruction masks. */
96 #define INSN_VSP_MASK 0xc0
97 #define INSN_VSP_SIZE_MASK 0x3f
98 #define INSN_STD_MASK 0xf0
99 #define INSN_STD_DATA_MASK 0x0f
100 #define INSN_POP_TYPE_MASK 0x08
101 #define INSN_POP_COUNT_MASK 0x07
102 #define INSN_VSP_LARGE_INC_MASK 0xff
104 /* Instruction definitions */
105 #define INSN_VSP_INC 0x00
106 #define INSN_VSP_DEC 0x40
107 #define INSN_POP_MASKED 0x80
108 #define INSN_VSP_REG 0x90
109 #define INSN_POP_COUNT 0xa0
110 #define INSN_FINISH 0xb0
111 #define INSN_POP_REGS 0xb1
112 #define INSN_VSP_LARGE_INC 0xb2
114 /* An item in the exception index table */
120 /* The state of the unwind process */
121 struct unwind_state {
122 uint32_t registers[16];
127 uint16_t update_mask;
130 /* Expand a 31-bit signed value to a 32-bit signed value */
131 static __inline int32_t
132 db_expand_prel31(uint32_t prel31)
135 return ((int32_t)(prel31 & 0x7fffffffu) << 1) / 2;
139 * Perform a binary search of the index table to find the function
140 * with the largest address that doesn't exceed addr.
142 static struct unwind_idx *
143 db_find_index(uint32_t addr)
145 unsigned int min, mid, max;
146 struct unwind_idx *start;
147 struct unwind_idx *item;
151 start = (struct unwind_idx *)&exidx_start;
154 max = (&exidx_end - &exidx_start) / 2;
157 mid = min + (max - min + 1) / 2;
161 prel31_addr = db_expand_prel31(item->offset);
162 func_addr = (uint32_t)&item->offset + prel31_addr;
164 if (func_addr <= addr) {
174 /* Reads the next byte from the instruction list */
176 db_unwind_exec_read_byte(struct unwind_state *state)
180 /* Read the unwind instruction */
181 insn = (*state->insn) >> (state->byte * 8);
183 /* Update the location of the next instruction */
184 if (state->byte == 0) {
194 /* Executes the next instruction on the list */
196 db_unwind_exec_insn(struct unwind_state *state)
199 uint32_t *vsp = (uint32_t *)state->registers[SP];
202 /* This should never happen */
203 if (state->entries == 0)
206 /* Read the next instruction */
207 insn = db_unwind_exec_read_byte(state);
209 if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
210 state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
212 } else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
213 state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
215 } else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
216 unsigned int mask, reg;
219 mask = db_unwind_exec_read_byte(state);
220 mask |= (insn & INSN_STD_DATA_MASK) << 8;
222 /* We have a refuse to unwind instruction */
229 /* Load the registers */
230 for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
232 state->registers[reg] = *vsp++;
233 state->update_mask |= 1 << reg;
235 /* If we have updated SP kep its value */
241 } else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
242 ((insn & INSN_STD_DATA_MASK) != 13) &&
243 ((insn & INSN_STD_DATA_MASK) != 15)) {
245 state->registers[SP] =
246 state->registers[insn & INSN_STD_DATA_MASK];
248 } else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
249 unsigned int count, reg;
251 /* Read how many registers to load */
252 count = insn & INSN_POP_COUNT_MASK;
257 /* Pop the registers */
258 for (reg = 4; reg <= 4 + count; reg++) {
259 state->registers[reg] = *vsp++;
260 state->update_mask |= 1 << reg;
263 /* Check if we are in the pop r14 version */
264 if ((insn & INSN_POP_TYPE_MASK) != 0) {
265 state->registers[14] = *vsp++;
268 } else if (insn == INSN_FINISH) {
269 /* Stop processing */
272 } else if ((insn == INSN_POP_REGS)) {
273 unsigned int mask, reg;
275 mask = db_unwind_exec_read_byte(state);
276 if (mask == 0 || (mask & 0xf0) != 0)
282 /* Load the registers */
283 for (reg = 0; mask && reg < 4; mask >>= 1, reg++) {
285 state->registers[reg] = *vsp++;
286 state->update_mask |= 1 << reg;
290 } else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
291 unsigned int uleb128;
293 /* Read the increment value */
294 uleb128 = db_unwind_exec_read_byte(state);
296 state->registers[SP] += 0x204 + (uleb128 << 2);
299 /* We hit a new instruction that needs to be implemented */
300 db_printf("Unhandled instruction %.2x\n", insn);
305 state->registers[SP] = (uint32_t)vsp;
309 db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
310 state->registers[FP], state->registers[SP], state->registers[LR],
311 state->registers[PC]);
317 /* Performs the unwind of a function */
319 db_unwind_tab(struct unwind_state *state)
323 /* Set PC to a known value */
324 state->registers[PC] = 0;
326 /* Read the personality */
327 entry = *state->insn & ENTRY_MASK;
329 if (entry == ENTRY_ARM_SU16) {
332 } else if (entry == ENTRY_ARM_LU16) {
334 state->entries = ((*state->insn >> 16) & 0xFF) + 1;
336 db_printf("Unknown entry: %x\n", entry);
340 while (state->entries > 0) {
341 if (db_unwind_exec_insn(state) != 0)
346 * The program counter was not updated, load it from the link register.
348 if (state->registers[PC] == 0)
349 state->registers[PC] = state->registers[LR];
355 db_stack_trace_cmd(struct unwind_state *state)
357 struct unwind_idx *index;
369 /* Reset the mask of updated registers */
370 state->update_mask = 0;
372 /* The pc value is correct and will be overwritten, save it */
373 state->start_pc = state->registers[PC];
375 /* Find the item to run */
376 index = db_find_index(state->start_pc);
378 if (index->insn != EXIDX_CANTUNWIND) {
379 if (index->insn & (1 << 31)) {
380 /* The data is within the instruction */
381 state->insn = &index->insn;
383 /* A prel31 offset to the unwind table */
384 state->insn = (uint32_t *)
385 ((uintptr_t)&index->insn +
386 db_expand_prel31(index->insn));
388 /* Run the unwind function */
389 finished = db_unwind_tab(state);
392 /* Print the frame details */
393 sym = db_search_symbol(state->start_pc, DB_STGY_ANY, &offset);
394 if (sym == C_DB_SYM_NULL) {
398 db_symbol_values(sym, &name, &value);
399 db_printf("%s() at ", name);
400 db_printsym(state->start_pc, DB_STGY_PROC);
402 db_printf("\t pc = 0x%08x lr = 0x%08x (", state->start_pc,
403 state->registers[LR]);
404 db_printsym(state->registers[LR], DB_STGY_PROC);
406 db_printf("\t sp = 0x%08x fp = 0x%08x",
407 state->registers[SP], state->registers[FP]);
409 /* Don't print the registers we have already printed */
410 upd_mask = state->update_mask &
411 ~((1 << SP) | (1 << FP) | (1 << LR) | (1 << PC));
413 for (i = 0, reg = 0; upd_mask != 0; upd_mask >>= 1, reg++) {
414 if ((upd_mask & 1) != 0) {
415 db_printf("%s%sr%d = 0x%08x", sep,
416 (reg < 10) ? " " : "", reg,
417 state->registers[reg]);
430 * Stop if directed to do so, or if we've unwound back to the
431 * kernel entry point, or if the unwind function didn't change
432 * anything (to avoid getting stuck in this loop forever).
433 * If the latter happens, it's an indication that the unwind
434 * information is incorrect somehow for the function named in
435 * the last frame printed before you see the unwind failure
436 * message (maybe it needs a STOP_UNWINDING).
438 if (index->insn == EXIDX_CANTUNWIND) {
439 db_printf("Unable to unwind further\n");
441 } else if (state->registers[PC] < VM_MIN_KERNEL_ADDRESS) {
442 db_printf("Unable to unwind into user mode\n");
444 } else if (state->update_mask == 0) {
445 db_printf("Unwind failure (no registers changed)\n");
453 * APCS stack frames are awkward beasts, so I don't think even trying to use
454 * a structure to represent them is a good idea.
456 * Here's the diagram from the APCS. Increasing address is _up_ the page.
458 * save code pointer [fp] <- fp points to here
459 * return link value [fp, #-4]
460 * return sp value [fp, #-8]
461 * return fp value [fp, #-12]
474 * The save code pointer points twelve bytes beyond the start of the
475 * code sequence (usually a single STM) that created the stack frame.
476 * We have to disassemble it if we want to know which of the optional
477 * fields are actually present.
480 #ifndef __ARM_EABI__ /* The frame format is differend in AAPCS */
482 db_stack_trace_cmd(db_expr_t addr, db_expr_t count, boolean_t kernel_only)
484 u_int32_t *frame, *lastframe;
491 frame = (u_int32_t *)addr;
493 scp_offset = -(get_pc_str_offset() >> 2);
495 while (count-- && frame != NULL && !db_pager_quit) {
503 * In theory, the SCP isn't guaranteed to be in the function
504 * that generated the stack frame. We hope for the best.
508 sym = db_search_symbol(scp, DB_STGY_ANY, &offset);
509 if (sym == C_DB_SYM_NULL) {
513 db_symbol_values(sym, &name, &value);
514 db_printf("%s() at ", name);
515 db_printsym(scp, DB_STGY_PROC);
518 db_printf("\tscp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
519 db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
522 db_printf("\tscp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
523 db_printsym(frame[FR_RLV], DB_STGY_PROC);
526 db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
528 savecode = ((u_int32_t *)scp)[scp_offset];
529 if ((savecode & 0x0e100000) == 0x08000000) {
530 /* Looks like an STM */
533 for (r = 10; r >= 0; r--) {
534 if (savecode & (1 << r)) {
535 db_printf("%sr%d=0x%08x",
537 sep = (frame - rp) % 4 == 2 ?
546 * Switch to next frame up
548 if (frame[FR_RFP] == 0)
549 break; /* Top of stack */
552 frame = (u_int32_t *)(frame[FR_RFP]);
554 if (INKERNEL((int)frame)) {
555 /* staying in kernel */
556 if (frame <= lastframe) {
557 db_printf("Bad frame pointer: %p\n", frame);
560 } else if (INKERNEL((int)lastframe)) {
561 /* switch from user to kernel */
563 break; /* kernel stack only */
566 if (frame <= lastframe) {
567 db_printf("Bad user frame pointer: %p\n",
578 db_md_list_watchpoints()
583 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
589 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
595 db_trace_thread(struct thread *thr, int count)
598 struct unwind_state state;
602 if (thr != curthread) {
603 ctx = kdb_thr_ctx(thr);
606 state.registers[FP] = ctx->un_32.pcb32_r11;
607 state.registers[SP] = ctx->un_32.pcb32_sp;
608 state.registers[LR] = ctx->un_32.pcb32_lr;
609 state.registers[PC] = ctx->un_32.pcb32_pc;
611 db_stack_trace_cmd(&state);
613 db_stack_trace_cmd(ctx->un_32.pcb32_r11, -1, TRUE);
624 struct unwind_state state;
627 /* Read the stack pointer */
628 __asm __volatile("mov %0, sp" : "=&r" (sp));
630 state.registers[FP] = (uint32_t)__builtin_frame_address(0);
631 state.registers[SP] = sp;
632 state.registers[LR] = (uint32_t)__builtin_return_address(0);
633 state.registers[PC] = (uint32_t)db_trace_self;
635 db_stack_trace_cmd(&state);
639 addr = (db_addr_t)__builtin_frame_address(0);
640 db_stack_trace_cmd(addr, -1, FALSE);