Index: head/contrib/gdb/gdb/arm-tdep.c =================================================================== --- head/contrib/gdb/gdb/arm-tdep.c (revision 298357) +++ head/contrib/gdb/gdb/arm-tdep.c (revision 298358) @@ -1,2998 +1,3000 @@ /* Common target dependent code for GDB on ARM systems. Copyright 1988, 1989, 1991, 1992, 1993, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GDB. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include /* XXX for isupper () */ #include "defs.h" #include "frame.h" #include "inferior.h" #include "gdbcmd.h" #include "gdbcore.h" #include "gdb_string.h" #include "dis-asm.h" /* For register styles. */ #include "regcache.h" #include "doublest.h" #include "value.h" #include "arch-utils.h" #include "osabi.h" #include "frame-unwind.h" #include "frame-base.h" #include "trad-frame.h" #include "arm-tdep.h" #include "gdb/sim-arm.h" #include "elf-bfd.h" #include "coff/internal.h" #include "elf/arm.h" #include "gdb_assert.h" static int arm_debug; /* Each OS has a different mechanism for accessing the various registers stored in the sigcontext structure. SIGCONTEXT_REGISTER_ADDRESS should be defined to the name (or function pointer) which may be used to determine the addresses of the various saved registers in the sigcontext structure. For the ARM target, there are three parameters to this function. The first is the pc value of the frame under consideration, the second the stack pointer of this frame, and the last is the register number to fetch. If the tm.h file does not define this macro, then it's assumed that no mechanism is needed and we define SIGCONTEXT_REGISTER_ADDRESS to be 0. When it comes time to multi-arching this code, see the identically named machinery in ia64-tdep.c for an example of how it could be done. It should not be necessary to modify the code below where this macro is used. */ #ifdef SIGCONTEXT_REGISTER_ADDRESS #ifndef SIGCONTEXT_REGISTER_ADDRESS_P #define SIGCONTEXT_REGISTER_ADDRESS_P() 1 #endif #else #define SIGCONTEXT_REGISTER_ADDRESS(SP,PC,REG) 0 #define SIGCONTEXT_REGISTER_ADDRESS_P() 0 #endif /* Macros for setting and testing a bit in a minimal symbol that marks it as Thumb function. The MSB of the minimal symbol's "info" field is used for this purpose. MSYMBOL_SET_SPECIAL Actually sets the "special" bit. MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */ #define MSYMBOL_SET_SPECIAL(msym) \ MSYMBOL_INFO (msym) = (char *) (((long) MSYMBOL_INFO (msym)) \ | 0x80000000) #define MSYMBOL_IS_SPECIAL(msym) \ (((long) MSYMBOL_INFO (msym) & 0x80000000) != 0) /* The list of available "set arm ..." and "show arm ..." commands. */ static struct cmd_list_element *setarmcmdlist = NULL; static struct cmd_list_element *showarmcmdlist = NULL; /* The type of floating-point to use. Keep this in sync with enum arm_float_model, and the help string in _initialize_arm_tdep. */ static const char *fp_model_strings[] = { "auto", "softfpa", "fpa", "softvfp", "vfp" }; /* A variable that can be configured by the user. */ static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO; static const char *current_fp_model = "auto"; /* Number of different reg name sets (options). */ static int num_disassembly_options; /* We have more registers than the disassembler as gdb can print the value of special registers as well. The general register names are overwritten by whatever is being used by the disassembler at the moment. We also adjust the case of cpsr and fps. */ /* Initial value: Register names used in ARM's ISA documentation. */ static char * arm_register_name_strings[] = {"r0", "r1", "r2", "r3", /* 0 1 2 3 */ "r4", "r5", "r6", "r7", /* 4 5 6 7 */ "r8", "r9", "r10", "r11", /* 8 9 10 11 */ "r12", "sp", "lr", "pc", /* 12 13 14 15 */ "f0", "f1", "f2", "f3", /* 16 17 18 19 */ "f4", "f5", "f6", "f7", /* 20 21 22 23 */ "fps", "cpsr" }; /* 24 25 */ static char **arm_register_names = arm_register_name_strings; /* Valid register name styles. */ static const char **valid_disassembly_styles; /* Disassembly style to use. Default to "std" register names. */ static const char *disassembly_style; /* Index to that option in the opcodes table. */ static int current_option; /* This is used to keep the bfd arch_info in sync with the disassembly style. */ static void set_disassembly_style_sfunc(char *, int, struct cmd_list_element *); static void set_disassembly_style (void); static void convert_from_extended (const struct floatformat *, const void *, void *); static void convert_to_extended (const struct floatformat *, void *, const void *); struct arm_prologue_cache { /* The stack pointer at the time this frame was created; i.e. the caller's stack pointer when this function was called. It is used to identify this frame. */ CORE_ADDR prev_sp; /* The frame base for this frame is just prev_sp + frame offset - frame size. FRAMESIZE is the size of this stack frame, and FRAMEOFFSET if the initial offset from the stack pointer (this frame's stack pointer, not PREV_SP) to the frame base. */ int framesize; int frameoffset; /* The register used to hold the frame pointer for this frame. */ int framereg; /* Saved register offsets. */ struct trad_frame_saved_reg *saved_regs; }; /* Addresses for calling Thumb functions have the bit 0 set. Here are some macros to test, set, or clear bit 0 of addresses. */ #define IS_THUMB_ADDR(addr) ((addr) & 1) #define MAKE_THUMB_ADDR(addr) ((addr) | 1) #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1) /* Set to true if the 32-bit mode is in use. */ int arm_apcs_32 = 1; /* Determine if the program counter specified in MEMADDR is in a Thumb function. */ int arm_pc_is_thumb (CORE_ADDR memaddr) { struct minimal_symbol *sym; /* If bit 0 of the address is set, assume this is a Thumb address. */ if (IS_THUMB_ADDR (memaddr)) return 1; /* Thumb functions have a "special" bit set in minimal symbols. */ sym = lookup_minimal_symbol_by_pc (memaddr); if (sym) { return (MSYMBOL_IS_SPECIAL (sym)); } else { return 0; } } /* Remove useless bits from addresses in a running program. */ static CORE_ADDR arm_addr_bits_remove (CORE_ADDR val) { if (arm_apcs_32) return (val & (arm_pc_is_thumb (val) ? 0xfffffffe : 0xfffffffc)); else return (val & 0x03fffffc); } /* When reading symbols, we need to zap the low bit of the address, which may be set to 1 for Thumb functions. */ static CORE_ADDR arm_smash_text_address (CORE_ADDR val) { return val & ~1; } /* Immediately after a function call, return the saved pc. Can't always go through the frames for this because on some machines the new frame is not set up until the new function executes some instructions. */ static CORE_ADDR arm_saved_pc_after_call (struct frame_info *frame) { return ADDR_BITS_REMOVE (read_register (ARM_LR_REGNUM)); } /* Determine whether the function invocation represented by FI has a frame on the stack associated with it. If it does return zero, otherwise return 1. */ static int arm_frameless_function_invocation (struct frame_info *fi) { CORE_ADDR func_start, after_prologue; int frameless; /* Sometimes we have functions that do a little setup (like saving the vN registers with the stmdb instruction, but DO NOT set up a frame. The symbol table will report this as a prologue. However, it is important not to try to parse these partial frames as frames, or we will get really confused. So I will demand 3 instructions between the start & end of the prologue before I call it a real prologue, i.e. at least mov ip, sp, stmdb sp!, {} sub sp, ip, #4. */ func_start = (get_frame_func (fi) + FUNCTION_START_OFFSET); after_prologue = SKIP_PROLOGUE (func_start); /* There are some frameless functions whose first two instructions follow the standard APCS form, in which case after_prologue will be func_start + 8. */ frameless = (after_prologue < func_start + 12); return frameless; } /* A typical Thumb prologue looks like this: push {r7, lr} add sp, sp, #-28 add r7, sp, #12 Sometimes the latter instruction may be replaced by: mov r7, sp or like this: push {r7, lr} mov r7, sp sub sp, #12 or, on tpcs, like this: sub sp,#16 push {r7, lr} (many instructions) mov r7, sp sub sp, #12 There is always one instruction of three classes: 1 - push 2 - setting of r7 3 - adjusting of sp When we have found at least one of each class we are done with the prolog. Note that the "sub sp, #NN" before the push does not count. */ static CORE_ADDR thumb_skip_prologue (CORE_ADDR pc, CORE_ADDR func_end) { CORE_ADDR current_pc; /* findmask: bit 0 - push { rlist } bit 1 - mov r7, sp OR add r7, sp, #imm (setting of r7) bit 2 - sub sp, #simm OR add sp, #simm (adjusting of sp) */ int findmask = 0; for (current_pc = pc; current_pc + 2 < func_end && current_pc < pc + 40; current_pc += 2) { unsigned short insn = read_memory_unsigned_integer (current_pc, 2); if ((insn & 0xfe00) == 0xb400) /* push { rlist } */ { findmask |= 1; /* push found */ } else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR sub sp, #simm */ { if ((findmask & 1) == 0) /* before push ? */ continue; else findmask |= 4; /* add/sub sp found */ } else if ((insn & 0xff00) == 0xaf00) /* add r7, sp, #imm */ { findmask |= 2; /* setting of r7 found */ } else if (insn == 0x466f) /* mov r7, sp */ { findmask |= 2; /* setting of r7 found */ } else if (findmask == (4+2+1)) { /* We have found one of each type of prologue instruction */ break; } else /* Something in the prolog that we don't care about or some instruction from outside the prolog scheduled here for optimization. */ continue; } return current_pc; } /* Advance the PC across any function entry prologue instructions to reach some "real" code. The APCS (ARM Procedure Call Standard) defines the following prologue: mov ip, sp [stmfd sp!, {a1,a2,a3,a4}] stmfd sp!, {...,fp,ip,lr,pc} [stfe f7, [sp, #-12]!] [stfe f6, [sp, #-12]!] [stfe f5, [sp, #-12]!] [stfe f4, [sp, #-12]!] sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn */ static CORE_ADDR arm_skip_prologue (CORE_ADDR pc) { unsigned long inst; CORE_ADDR skip_pc; CORE_ADDR func_addr, func_end = 0; char *func_name; struct symtab_and_line sal; /* If we're in a dummy frame, don't even try to skip the prologue. */ if (DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0)) return pc; /* See what the symbol table says. */ if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end)) { struct symbol *sym; /* Found a function. */ sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL, NULL); if (sym && SYMBOL_LANGUAGE (sym) != language_asm) { /* Don't use this trick for assembly source files. */ sal = find_pc_line (func_addr, 0); if ((sal.line != 0) && (sal.end < func_end)) return sal.end; } } /* Check if this is Thumb code. */ if (arm_pc_is_thumb (pc)) return thumb_skip_prologue (pc, func_end); /* Can't find the prologue end in the symbol table, try it the hard way by disassembling the instructions. */ /* Like arm_scan_prologue, stop no later than pc + 64. */ if (func_end == 0 || func_end > pc + 64) func_end = pc + 64; for (skip_pc = pc; skip_pc < func_end; skip_pc += 4) { inst = read_memory_integer (skip_pc, 4); /* "mov ip, sp" is no longer a required part of the prologue. */ if (inst == 0xe1a0c00d) /* mov ip, sp */ continue; if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */ continue; if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */ continue; /* Some prologues begin with "str lr, [sp, #-4]!". */ if (inst == 0xe52de004) /* str lr, [sp, #-4]! */ continue; if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */ continue; if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */ continue; /* Any insns after this point may float into the code, if it makes for better instruction scheduling, so we skip them only if we find them, but still consider the function to be frame-ful. */ /* We may have either one sfmfd instruction here, or several stfe insns, depending on the version of floating point code we support. */ if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, , [sp]! */ continue; if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */ continue; if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */ continue; if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */ continue; if ((inst & 0xffffc000) == 0xe54b0000 || /* strb r(0123),[r11,#-nn] */ (inst & 0xffffc0f0) == 0xe14b00b0 || /* strh r(0123),[r11,#-nn] */ (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */ continue; if ((inst & 0xffffc000) == 0xe5cd0000 || /* strb r(0123),[sp,#nn] */ (inst & 0xffffc0f0) == 0xe1cd00b0 || /* strh r(0123),[sp,#nn] */ (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */ continue; /* Un-recognized instruction; stop scanning. */ break; } return skip_pc; /* End of prologue */ } /* *INDENT-OFF* */ /* Function: thumb_scan_prologue (helper function for arm_scan_prologue) This function decodes a Thumb function prologue to determine: 1) the size of the stack frame 2) which registers are saved on it 3) the offsets of saved regs 4) the offset from the stack pointer to the frame pointer A typical Thumb function prologue would create this stack frame (offsets relative to FP) old SP -> 24 stack parameters 20 LR 16 R7 R7 -> 0 local variables (16 bytes) SP -> -12 additional stack space (12 bytes) The frame size would thus be 36 bytes, and the frame offset would be 12 bytes. The frame register is R7. The comments for thumb_skip_prolog() describe the algorithm we use to detect the end of the prolog. */ /* *INDENT-ON* */ static void thumb_scan_prologue (CORE_ADDR prev_pc, struct arm_prologue_cache *cache) { CORE_ADDR prologue_start; CORE_ADDR prologue_end; CORE_ADDR current_pc; /* Which register has been copied to register n? */ int saved_reg[16]; /* findmask: bit 0 - push { rlist } bit 1 - mov r7, sp OR add r7, sp, #imm (setting of r7) bit 2 - sub sp, #simm OR add sp, #simm (adjusting of sp) */ int findmask = 0; int i; if (find_pc_partial_function (prev_pc, NULL, &prologue_start, &prologue_end)) { struct symtab_and_line sal = find_pc_line (prologue_start, 0); if (sal.line == 0) /* no line info, use current PC */ prologue_end = prev_pc; else if (sal.end < prologue_end) /* next line begins after fn end */ prologue_end = sal.end; /* (probably means no prologue) */ } else /* We're in the boondocks: allow for 16 pushes, an add, and "mv fp,sp". */ prologue_end = prologue_start + 40; prologue_end = min (prologue_end, prev_pc); /* Initialize the saved register map. When register H is copied to register L, we will put H in saved_reg[L]. */ for (i = 0; i < 16; i++) saved_reg[i] = i; /* Search the prologue looking for instructions that set up the frame pointer, adjust the stack pointer, and save registers. Do this until all basic prolog instructions are found. */ cache->framesize = 0; for (current_pc = prologue_start; (current_pc < prologue_end) && ((findmask & 7) != 7); current_pc += 2) { unsigned short insn; int regno; int offset; insn = read_memory_unsigned_integer (current_pc, 2); if ((insn & 0xfe00) == 0xb400) /* push { rlist } */ { int mask; findmask |= 1; /* push found */ /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says whether to save LR (R14). */ mask = (insn & 0xff) | ((insn & 0x100) << 6); /* Calculate offsets of saved R0-R7 and LR. */ for (regno = ARM_LR_REGNUM; regno >= 0; regno--) if (mask & (1 << regno)) { cache->framesize += 4; cache->saved_regs[saved_reg[regno]].addr = -cache->framesize; /* Reset saved register map. */ saved_reg[regno] = regno; } } else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR sub sp, #simm */ { if ((findmask & 1) == 0) /* before push? */ continue; else findmask |= 4; /* add/sub sp found */ offset = (insn & 0x7f) << 2; /* get scaled offset */ if (insn & 0x80) /* is it signed? (==subtracting) */ { cache->frameoffset += offset; offset = -offset; } cache->framesize -= offset; } else if ((insn & 0xff00) == 0xaf00) /* add r7, sp, #imm */ { findmask |= 2; /* setting of r7 found */ cache->framereg = THUMB_FP_REGNUM; /* get scaled offset */ cache->frameoffset = (insn & 0xff) << 2; } else if (insn == 0x466f) /* mov r7, sp */ { findmask |= 2; /* setting of r7 found */ cache->framereg = THUMB_FP_REGNUM; cache->frameoffset = 0; saved_reg[THUMB_FP_REGNUM] = ARM_SP_REGNUM; } else if ((insn & 0xffc0) == 0x4640) /* mov r0-r7, r8-r15 */ { int lo_reg = insn & 7; /* dest. register (r0-r7) */ int hi_reg = ((insn >> 3) & 7) + 8; /* source register (r8-15) */ saved_reg[lo_reg] = hi_reg; /* remember hi reg was saved */ } else /* Something in the prolog that we don't care about or some instruction from outside the prolog scheduled here for optimization. */ continue; } } /* This function decodes an ARM function prologue to determine: 1) the size of the stack frame 2) which registers are saved on it 3) the offsets of saved regs 4) the offset from the stack pointer to the frame pointer This information is stored in the "extra" fields of the frame_info. There are two basic forms for the ARM prologue. The fixed argument function call will look like: mov ip, sp stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #4 [sub sp, sp, #4] Which would create this stack frame (offsets relative to FP): IP -> 4 (caller's stack) FP -> 0 PC (points to address of stmfd instruction + 8 in callee) -4 LR (return address in caller) -8 IP (copy of caller's SP) -12 FP (caller's FP) SP -> -28 Local variables The frame size would thus be 32 bytes, and the frame offset would be 28 bytes. The stmfd call can also save any of the vN registers it plans to use, which increases the frame size accordingly. Note: The stored PC is 8 off of the STMFD instruction that stored it because the ARM Store instructions always store PC + 8 when you read the PC register. A variable argument function call will look like: mov ip, sp stmfd sp!, {a1, a2, a3, a4} stmfd sp!, {fp, ip, lr, pc} sub fp, ip, #20 Which would create this stack frame (offsets relative to FP): IP -> 20 (caller's stack) 16 A4 12 A3 8 A2 4 A1 FP -> 0 PC (points to address of stmfd instruction + 8 in callee) -4 LR (return address in caller) -8 IP (copy of caller's SP) -12 FP (caller's FP) SP -> -28 Local variables The frame size would thus be 48 bytes, and the frame offset would be 28 bytes. There is another potential complication, which is that the optimizer will try to separate the store of fp in the "stmfd" instruction from the "sub fp, ip, #NN" instruction. Almost anything can be there, so we just key on the stmfd, and then scan for the "sub fp, ip, #NN"... Also, note, the original version of the ARM toolchain claimed that there should be an instruction at the end of the prologue. I have never seen GCC produce this, and the ARM docs don't mention it. We still test for it below in case it happens... */ static void arm_scan_prologue (struct frame_info *next_frame, struct arm_prologue_cache *cache) { int regno, sp_offset, fp_offset, ip_offset; CORE_ADDR prologue_start, prologue_end, current_pc; CORE_ADDR prev_pc = frame_pc_unwind (next_frame); /* Assume there is no frame until proven otherwise. */ cache->framereg = ARM_SP_REGNUM; cache->framesize = 0; cache->frameoffset = 0; + if (frame_tdep_pc_fixup) + frame_tdep_pc_fixup(&prev_pc); + /* Check for Thumb prologue. */ if (arm_pc_is_thumb (prev_pc)) { thumb_scan_prologue (prev_pc, cache); return; } /* Find the function prologue. If we can't find the function in the symbol table, peek in the stack frame to find the PC. */ if (find_pc_partial_function (prev_pc, NULL, &prologue_start, &prologue_end)) { /* One way to find the end of the prologue (which works well for unoptimized code) is to do the following: struct symtab_and_line sal = find_pc_line (prologue_start, 0); if (sal.line == 0) prologue_end = prev_pc; else if (sal.end < prologue_end) prologue_end = sal.end; This mechanism is very accurate so long as the optimizer doesn't move any instructions from the function body into the prologue. If this happens, sal.end will be the last instruction in the first hunk of prologue code just before the first instruction that the scheduler has moved from the body to the prologue. In order to make sure that we scan all of the prologue instructions, we use a slightly less accurate mechanism which may scan more than necessary. To help compensate for this lack of accuracy, the prologue scanning loop below contains several clauses which'll cause the loop to terminate early if an implausible prologue instruction is encountered. The expression prologue_start + 64 is a suitable endpoint since it accounts for the largest possible prologue plus up to five instructions inserted by the scheduler. */ if (prologue_end > prologue_start + 64) { prologue_end = prologue_start + 64; /* See above. */ } } else { /* We have no symbol information. Our only option is to assume this function has a standard stack frame and the normal frame register. Then, we can find the value of our frame pointer on entrance to the callee (or at the present moment if this is the innermost frame). The value stored there should be the address of the stmfd + 8. */ CORE_ADDR frame_loc; LONGEST return_value; frame_loc = frame_unwind_register_unsigned (next_frame, ARM_FP_REGNUM); if (!safe_read_memory_integer (frame_loc, 4, &return_value)) return; else { prologue_start = ADDR_BITS_REMOVE (return_value) - 8; prologue_end = prologue_start + 64; /* See above. */ } } if (prev_pc < prologue_end) prologue_end = prev_pc; /* Now search the prologue looking for instructions that set up the frame pointer, adjust the stack pointer, and save registers. Be careful, however, and if it doesn't look like a prologue, don't try to scan it. If, for instance, a frameless function begins with stmfd sp!, then we will tell ourselves there is a frame, which will confuse stack traceback, as well as "finish" and other operations that rely on a knowledge of the stack traceback. In the APCS, the prologue should start with "mov ip, sp" so if we don't see this as the first insn, we will stop. [Note: This doesn't seem to be true any longer, so it's now an optional part of the prologue. - Kevin Buettner, 2001-11-20] [Note further: The "mov ip,sp" only seems to be missing in frameless functions at optimization level "-O2" or above, in which case it is often (but not always) replaced by "str lr, [sp, #-4]!". - Michael Snyder, 2002-04-23] */ sp_offset = fp_offset = ip_offset = 0; for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 4) { unsigned int insn = read_memory_unsigned_integer (current_pc, 4); if (insn == 0xe1a0c00d) /* mov ip, sp */ { ip_offset = 0; continue; } else if ((insn & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */ { unsigned imm = insn & 0xff; /* immediate value */ unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */ imm = (imm >> rot) | (imm << (32 - rot)); ip_offset = imm; continue; } else if ((insn & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */ { unsigned imm = insn & 0xff; /* immediate value */ unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */ imm = (imm >> rot) | (imm << (32 - rot)); ip_offset = -imm; continue; } else if (insn == 0xe52de004) /* str lr, [sp, #-4]! */ { sp_offset -= 4; cache->saved_regs[ARM_LR_REGNUM].addr = sp_offset; continue; } else if ((insn & 0xffff0000) == 0xe92d0000) /* stmfd sp!, {..., fp, ip, lr, pc} or stmfd sp!, {a1, a2, a3, a4} */ { int mask = insn & 0xffff; /* Calculate offsets of saved registers. */ for (regno = ARM_PC_REGNUM; regno >= 0; regno--) if (mask & (1 << regno)) { sp_offset -= 4; cache->saved_regs[regno].addr = sp_offset; } } else if ((insn & 0xffffc000) == 0xe54b0000 || /* strb rx,[r11,#-n] */ (insn & 0xffffc0f0) == 0xe14b00b0 || /* strh rx,[r11,#-n] */ (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */ { /* No need to add this to saved_regs -- it's just an arg reg. */ continue; } else if ((insn & 0xffffc000) == 0xe5cd0000 || /* strb rx,[sp,#n] */ (insn & 0xffffc0f0) == 0xe1cd00b0 || /* strh rx,[sp,#n] */ (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */ { /* No need to add this to saved_regs -- it's just an arg reg. */ continue; } else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */ { unsigned imm = insn & 0xff; /* immediate value */ unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */ imm = (imm >> rot) | (imm << (32 - rot)); fp_offset = -imm + ip_offset; cache->framereg = ARM_FP_REGNUM; } else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */ { unsigned imm = insn & 0xff; /* immediate value */ unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */ imm = (imm >> rot) | (imm << (32 - rot)); sp_offset -= imm; } else if ((insn & 0xffff7fff) == 0xed6d0103) /* stfe f?, [sp, -#c]! */ { sp_offset -= 12; regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07); cache->saved_regs[regno].addr = sp_offset; } else if ((insn & 0xffbf0fff) == 0xec2d0200) /* sfmfd f0, 4, [sp!] */ { int n_saved_fp_regs; unsigned int fp_start_reg, fp_bound_reg; if ((insn & 0x800) == 0x800) /* N0 is set */ { if ((insn & 0x40000) == 0x40000) /* N1 is set */ n_saved_fp_regs = 3; else n_saved_fp_regs = 1; } else { if ((insn & 0x40000) == 0x40000) /* N1 is set */ n_saved_fp_regs = 2; else n_saved_fp_regs = 4; } fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7); fp_bound_reg = fp_start_reg + n_saved_fp_regs; for (; fp_start_reg < fp_bound_reg; fp_start_reg++) { sp_offset -= 12; cache->saved_regs[fp_start_reg++].addr = sp_offset; } } else if ((insn & 0xf0000000) != 0xe0000000) break; /* Condition not true, exit early */ else if ((insn & 0xfe200000) == 0xe8200000) /* ldm? */ break; /* Don't scan past a block load */ else /* The optimizer might shove anything into the prologue, so we just skip what we don't recognize. */ continue; } /* The frame size is just the negative of the offset (from the original SP) of the last thing thing we pushed on the stack. The frame offset is [new FP] - [new SP]. */ cache->framesize = -sp_offset; if (cache->framereg == ARM_FP_REGNUM) cache->frameoffset = fp_offset - sp_offset; else cache->frameoffset = 0; } static struct arm_prologue_cache * arm_make_prologue_cache (struct frame_info *next_frame) { int reg; struct arm_prologue_cache *cache; CORE_ADDR unwound_fp; cache = frame_obstack_zalloc (sizeof (struct arm_prologue_cache)); cache->saved_regs = trad_frame_alloc_saved_regs (next_frame); arm_scan_prologue (next_frame, cache); - unwound_fp = frame_unwind_register_unsigned (next_frame, cache->framereg); if (unwound_fp == 0) return cache; cache->prev_sp = unwound_fp + cache->framesize - cache->frameoffset; /* Calculate actual addresses of saved registers using offsets determined by arm_scan_prologue. */ for (reg = 0; reg < NUM_REGS; reg++) if (trad_frame_addr_p (cache->saved_regs, reg)) cache->saved_regs[reg].addr += cache->prev_sp; return cache; } /* Our frame ID for a normal frame is the current function's starting PC and the caller's SP when we were called. */ static void arm_prologue_this_id (struct frame_info *next_frame, void **this_cache, struct frame_id *this_id) { struct arm_prologue_cache *cache; struct frame_id id; CORE_ADDR func; if (*this_cache == NULL) *this_cache = arm_make_prologue_cache (next_frame); cache = *this_cache; func = frame_func_unwind (next_frame); /* This is meant to halt the backtrace at "_start". Make sure we don't halt it at a generic dummy frame. */ if (func <= LOWEST_PC) return; /* If we've hit a wall, stop. */ if (cache->prev_sp == 0) return; id = frame_id_build (cache->prev_sp, func); /* Check that we're not going round in circles with the same frame ID (but avoid applying the test to sentinel frames which do go round in circles). */ if (frame_relative_level (next_frame) >= 0 && get_frame_type (next_frame) == NORMAL_FRAME && frame_id_eq (get_frame_id (next_frame), id)) return; *this_id = id; } static void arm_prologue_prev_register (struct frame_info *next_frame, void **this_cache, int prev_regnum, int *optimized, enum lval_type *lvalp, CORE_ADDR *addrp, int *realnump, void *valuep) { struct arm_prologue_cache *cache; if (*this_cache == NULL) *this_cache = arm_make_prologue_cache (next_frame); cache = *this_cache; /* If we are asked to unwind the PC, then we need to return the LR instead. The saved value of PC points into this frame's prologue, not the next frame's resume location. */ if (prev_regnum == ARM_PC_REGNUM) prev_regnum = ARM_LR_REGNUM; /* SP is generally not saved to the stack, but this frame is identified by NEXT_FRAME's stack pointer at the time of the call. The value was already reconstructed into PREV_SP. */ if (prev_regnum == ARM_SP_REGNUM) { *lvalp = not_lval; if (valuep) store_unsigned_integer (valuep, 4, cache->prev_sp); return; } trad_frame_prev_register (next_frame, cache->saved_regs, prev_regnum, optimized, lvalp, addrp, realnump, valuep); } struct frame_unwind arm_prologue_unwind = { NORMAL_FRAME, arm_prologue_this_id, arm_prologue_prev_register }; static const struct frame_unwind * arm_prologue_unwind_sniffer (struct frame_info *next_frame) { return &arm_prologue_unwind; } static CORE_ADDR arm_normal_frame_base (struct frame_info *next_frame, void **this_cache) { struct arm_prologue_cache *cache; if (*this_cache == NULL) *this_cache = arm_make_prologue_cache (next_frame); cache = *this_cache; return cache->prev_sp + cache->frameoffset - cache->framesize; } struct frame_base arm_normal_base = { &arm_prologue_unwind, arm_normal_frame_base, arm_normal_frame_base, arm_normal_frame_base }; static struct arm_prologue_cache * arm_make_sigtramp_cache (struct frame_info *next_frame) { struct arm_prologue_cache *cache; int reg; cache = frame_obstack_zalloc (sizeof (struct arm_prologue_cache)); cache->prev_sp = frame_unwind_register_unsigned (next_frame, ARM_SP_REGNUM); cache->saved_regs = trad_frame_alloc_saved_regs (next_frame); for (reg = 0; reg < NUM_REGS; reg++) cache->saved_regs[reg].addr = SIGCONTEXT_REGISTER_ADDRESS (cache->prev_sp, frame_pc_unwind (next_frame), reg); /* FIXME: What about thumb mode? */ cache->framereg = ARM_SP_REGNUM; cache->prev_sp = read_memory_integer (cache->saved_regs[cache->framereg].addr, register_size (current_gdbarch, cache->framereg)); return cache; } static void arm_sigtramp_this_id (struct frame_info *next_frame, void **this_cache, struct frame_id *this_id) { struct arm_prologue_cache *cache; if (*this_cache == NULL) *this_cache = arm_make_sigtramp_cache (next_frame); cache = *this_cache; /* FIXME drow/2003-07-07: This isn't right if we single-step within the sigtramp frame; the PC should be the beginning of the trampoline. */ *this_id = frame_id_build (cache->prev_sp, frame_pc_unwind (next_frame)); } static void arm_sigtramp_prev_register (struct frame_info *next_frame, void **this_cache, int prev_regnum, int *optimized, enum lval_type *lvalp, CORE_ADDR *addrp, int *realnump, void *valuep) { struct arm_prologue_cache *cache; if (*this_cache == NULL) *this_cache = arm_make_sigtramp_cache (next_frame); cache = *this_cache; trad_frame_prev_register (next_frame, cache->saved_regs, prev_regnum, optimized, lvalp, addrp, realnump, valuep); } struct frame_unwind arm_sigtramp_unwind = { SIGTRAMP_FRAME, arm_sigtramp_this_id, arm_sigtramp_prev_register }; static const struct frame_unwind * arm_sigtramp_unwind_sniffer (struct frame_info *next_frame) { /* Note: If an ARM PC_IN_SIGTRAMP method ever needs to compare against the name of the function, the code below will have to be changed to first fetch the name of the function and then pass this name to PC_IN_SIGTRAMP. */ if (SIGCONTEXT_REGISTER_ADDRESS_P () && PC_IN_SIGTRAMP (frame_pc_unwind (next_frame), (char *) 0)) return &arm_sigtramp_unwind; return NULL; } /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that dummy frame. The frame ID's base needs to match the TOS value saved by save_dummy_frame_tos() and returned from arm_push_dummy_call, and the PC needs to match the dummy frame's breakpoint. */ static struct frame_id arm_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame) { return frame_id_build (frame_unwind_register_unsigned (next_frame, ARM_SP_REGNUM), frame_pc_unwind (next_frame)); } /* Given THIS_FRAME, find the previous frame's resume PC (which will be used to construct the previous frame's ID, after looking up the containing function). */ static CORE_ADDR arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame) { CORE_ADDR pc; pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM); return IS_THUMB_ADDR (pc) ? UNMAKE_THUMB_ADDR (pc) : pc; } static CORE_ADDR arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame) { return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM); } /* DEPRECATED_CALL_DUMMY_WORDS: This sequence of words is the instructions mov lr,pc mov pc,r4 illegal Note this is 12 bytes. */ static LONGEST arm_call_dummy_words[] = { 0xe1a0e00f, 0xe1a0f004, 0xe7ffdefe }; /* When arguments must be pushed onto the stack, they go on in reverse order. The code below implements a FILO (stack) to do this. */ struct stack_item { int len; struct stack_item *prev; void *data; }; static struct stack_item * push_stack_item (struct stack_item *prev, void *contents, int len) { struct stack_item *si; si = xmalloc (sizeof (struct stack_item)); si->data = xmalloc (len); si->len = len; si->prev = prev; memcpy (si->data, contents, len); return si; } static struct stack_item * pop_stack_item (struct stack_item *si) { struct stack_item *dead = si; si = si->prev; xfree (dead->data); xfree (dead); return si; } /* We currently only support passing parameters in integer registers. This conforms with GCC's default model. Several other variants exist and we should probably support some of them based on the selected ABI. */ static CORE_ADDR arm_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr, struct regcache *regcache, CORE_ADDR bp_addr, int nargs, struct value **args, CORE_ADDR sp, int struct_return, CORE_ADDR struct_addr) { int argnum; int argreg; int nstack; struct stack_item *si = NULL; /* Set the return address. For the ARM, the return breakpoint is always at BP_ADDR. */ /* XXX Fix for Thumb. */ regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr); /* Walk through the list of args and determine how large a temporary stack is required. Need to take care here as structs may be passed on the stack, and we have to to push them. */ nstack = 0; argreg = ARM_A1_REGNUM; nstack = 0; /* Some platforms require a double-word aligned stack. Make sure sp is correctly aligned before we start. We always do this even if it isn't really needed -- it can never hurt things. */ sp &= ~(CORE_ADDR)(2 * DEPRECATED_REGISTER_SIZE - 1); /* The struct_return pointer occupies the first parameter passing register. */ if (struct_return) { if (arm_debug) fprintf_unfiltered (gdb_stdlog, "struct return in %s = 0x%s\n", REGISTER_NAME (argreg), paddr (struct_addr)); regcache_cooked_write_unsigned (regcache, argreg, struct_addr); argreg++; } for (argnum = 0; argnum < nargs; argnum++) { int len; struct type *arg_type; struct type *target_type; enum type_code typecode; char *val; arg_type = check_typedef (VALUE_TYPE (args[argnum])); len = TYPE_LENGTH (arg_type); target_type = TYPE_TARGET_TYPE (arg_type); typecode = TYPE_CODE (arg_type); val = VALUE_CONTENTS (args[argnum]); /* If the argument is a pointer to a function, and it is a Thumb function, create a LOCAL copy of the value and set the THUMB bit in it. */ if (TYPE_CODE_PTR == typecode && target_type != NULL && TYPE_CODE_FUNC == TYPE_CODE (target_type)) { CORE_ADDR regval = extract_unsigned_integer (val, len); if (arm_pc_is_thumb (regval)) { val = alloca (len); store_unsigned_integer (val, len, MAKE_THUMB_ADDR (regval)); } } /* Copy the argument to general registers or the stack in register-sized pieces. Large arguments are split between registers and stack. */ while (len > 0) { int partial_len = len < DEPRECATED_REGISTER_SIZE ? len : DEPRECATED_REGISTER_SIZE; if (argreg <= ARM_LAST_ARG_REGNUM) { /* The argument is being passed in a general purpose register. */ CORE_ADDR regval = extract_unsigned_integer (val, partial_len); if (arm_debug) fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n", argnum, REGISTER_NAME (argreg), phex (regval, DEPRECATED_REGISTER_SIZE)); regcache_cooked_write_unsigned (regcache, argreg, regval); argreg++; } else { /* Push the arguments onto the stack. */ if (arm_debug) fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n", argnum, nstack); si = push_stack_item (si, val, DEPRECATED_REGISTER_SIZE); nstack += DEPRECATED_REGISTER_SIZE; } len -= partial_len; val += partial_len; } } /* If we have an odd number of words to push, then decrement the stack by one word now, so first stack argument will be dword aligned. */ if (nstack & 4) sp -= 4; while (si) { sp -= si->len; write_memory (sp, si->data, si->len); si = pop_stack_item (si); } /* Finally, update teh SP register. */ regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp); return sp; } static void print_fpu_flags (int flags) { if (flags & (1 << 0)) fputs ("IVO ", stdout); if (flags & (1 << 1)) fputs ("DVZ ", stdout); if (flags & (1 << 2)) fputs ("OFL ", stdout); if (flags & (1 << 3)) fputs ("UFL ", stdout); if (flags & (1 << 4)) fputs ("INX ", stdout); putchar ('\n'); } /* Print interesting information about the floating point processor (if present) or emulator. */ static void arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file, struct frame_info *frame, const char *args) { unsigned long status = read_register (ARM_FPS_REGNUM); int type; type = (status >> 24) & 127; printf ("%s FPU type %d\n", (status & (1 << 31)) ? "Hardware" : "Software", type); fputs ("mask: ", stdout); print_fpu_flags (status >> 16); fputs ("flags: ", stdout); print_fpu_flags (status); } /* Return the GDB type object for the "standard" data type of data in register N. */ static struct type * arm_register_type (struct gdbarch *gdbarch, int regnum) { if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS) { if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) return builtin_type_arm_ext_big; else return builtin_type_arm_ext_littlebyte_bigword; } else return builtin_type_int32; } /* Index within `registers' of the first byte of the space for register N. */ static int arm_register_byte (int regnum) { if (regnum < ARM_F0_REGNUM) return regnum * INT_REGISTER_SIZE; else if (regnum < ARM_PS_REGNUM) return (NUM_GREGS * INT_REGISTER_SIZE + (regnum - ARM_F0_REGNUM) * FP_REGISTER_SIZE); else return (NUM_GREGS * INT_REGISTER_SIZE + NUM_FREGS * FP_REGISTER_SIZE + (regnum - ARM_FPS_REGNUM) * STATUS_REGISTER_SIZE); } /* Map GDB internal REGNUM onto the Arm simulator register numbers. */ static int arm_register_sim_regno (int regnum) { int reg = regnum; gdb_assert (reg >= 0 && reg < NUM_REGS); if (reg < NUM_GREGS) return SIM_ARM_R0_REGNUM + reg; reg -= NUM_GREGS; if (reg < NUM_FREGS) return SIM_ARM_FP0_REGNUM + reg; reg -= NUM_FREGS; if (reg < NUM_SREGS) return SIM_ARM_FPS_REGNUM + reg; reg -= NUM_SREGS; internal_error (__FILE__, __LINE__, "Bad REGNUM %d", regnum); } /* NOTE: cagney/2001-08-20: Both convert_from_extended() and convert_to_extended() use floatformat_arm_ext_littlebyte_bigword. It is thought that this is is the floating-point register format on little-endian systems. */ static void convert_from_extended (const struct floatformat *fmt, const void *ptr, void *dbl) { DOUBLEST d; if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d); else floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword, ptr, &d); floatformat_from_doublest (fmt, &d, dbl); } static void convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr) { DOUBLEST d; floatformat_to_doublest (fmt, ptr, &d); if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl); else floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword, &d, dbl); } static int condition_true (unsigned long cond, unsigned long status_reg) { if (cond == INST_AL || cond == INST_NV) return 1; switch (cond) { case INST_EQ: return ((status_reg & FLAG_Z) != 0); case INST_NE: return ((status_reg & FLAG_Z) == 0); case INST_CS: return ((status_reg & FLAG_C) != 0); case INST_CC: return ((status_reg & FLAG_C) == 0); case INST_MI: return ((status_reg & FLAG_N) != 0); case INST_PL: return ((status_reg & FLAG_N) == 0); case INST_VS: return ((status_reg & FLAG_V) != 0); case INST_VC: return ((status_reg & FLAG_V) == 0); case INST_HI: return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C); case INST_LS: return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C); case INST_GE: return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0)); case INST_LT: return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0)); case INST_GT: return (((status_reg & FLAG_Z) == 0) && (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0))); case INST_LE: return (((status_reg & FLAG_Z) != 0) || (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0))); } return 1; } /* Support routines for single stepping. Calculate the next PC value. */ #define submask(x) ((1L << ((x) + 1)) - 1) #define bit(obj,st) (((obj) >> (st)) & 1) #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st))) #define sbits(obj,st,fn) \ ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st)))) #define BranchDest(addr,instr) \ ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2))) #define ARM_PC_32 1 static unsigned long shifted_reg_val (unsigned long inst, int carry, unsigned long pc_val, unsigned long status_reg) { unsigned long res, shift; int rm = bits (inst, 0, 3); unsigned long shifttype = bits (inst, 5, 6); if (bit (inst, 4)) { int rs = bits (inst, 8, 11); shift = (rs == 15 ? pc_val + 8 : read_register (rs)) & 0xFF; } else shift = bits (inst, 7, 11); res = (rm == 15 ? ((pc_val | (ARM_PC_32 ? 0 : status_reg)) + (bit (inst, 4) ? 12 : 8)) : read_register (rm)); switch (shifttype) { case 0: /* LSL */ res = shift >= 32 ? 0 : res << shift; break; case 1: /* LSR */ res = shift >= 32 ? 0 : res >> shift; break; case 2: /* ASR */ if (shift >= 32) shift = 31; res = ((res & 0x80000000L) ? ~((~res) >> shift) : res >> shift); break; case 3: /* ROR/RRX */ shift &= 31; if (shift == 0) res = (res >> 1) | (carry ? 0x80000000L : 0); else res = (res >> shift) | (res << (32 - shift)); break; } return res & 0xffffffff; } /* Return number of 1-bits in VAL. */ static int bitcount (unsigned long val) { int nbits; for (nbits = 0; val != 0; nbits++) val &= val - 1; /* delete rightmost 1-bit in val */ return nbits; } CORE_ADDR thumb_get_next_pc (CORE_ADDR pc) { unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */ unsigned short inst1 = read_memory_integer (pc, 2); CORE_ADDR nextpc = pc + 2; /* default is next instruction */ unsigned long offset; if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */ { CORE_ADDR sp; /* Fetch the saved PC from the stack. It's stored above all of the other registers. */ offset = bitcount (bits (inst1, 0, 7)) * DEPRECATED_REGISTER_SIZE; sp = read_register (ARM_SP_REGNUM); nextpc = (CORE_ADDR) read_memory_integer (sp + offset, 4); nextpc = ADDR_BITS_REMOVE (nextpc); if (nextpc == pc) error ("Infinite loop detected"); } else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */ { unsigned long status = read_register (ARM_PS_REGNUM); unsigned long cond = bits (inst1, 8, 11); if (cond != 0x0f && condition_true (cond, status)) /* 0x0f = SWI */ nextpc = pc_val + (sbits (inst1, 0, 7) << 1); } else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */ { nextpc = pc_val + (sbits (inst1, 0, 10) << 1); } else if ((inst1 & 0xf800) == 0xf000) /* long branch with link, and blx */ { unsigned short inst2 = read_memory_integer (pc + 2, 2); offset = (sbits (inst1, 0, 10) << 12) + (bits (inst2, 0, 10) << 1); nextpc = pc_val + offset; /* For BLX make sure to clear the low bits. */ if (bits (inst2, 11, 12) == 1) nextpc = nextpc & 0xfffffffc; } else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */ { if (bits (inst1, 3, 6) == 0x0f) nextpc = pc_val; else nextpc = read_register (bits (inst1, 3, 6)); nextpc = ADDR_BITS_REMOVE (nextpc); if (nextpc == pc) error ("Infinite loop detected"); } return nextpc; } CORE_ADDR arm_get_next_pc (CORE_ADDR pc) { unsigned long pc_val; unsigned long this_instr; unsigned long status; CORE_ADDR nextpc; if (arm_pc_is_thumb (pc)) return thumb_get_next_pc (pc); pc_val = (unsigned long) pc; this_instr = read_memory_integer (pc, 4); status = read_register (ARM_PS_REGNUM); nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */ if (condition_true (bits (this_instr, 28, 31), status)) { switch (bits (this_instr, 24, 27)) { case 0x0: case 0x1: /* data processing */ case 0x2: case 0x3: { unsigned long operand1, operand2, result = 0; unsigned long rn; int c; if (bits (this_instr, 12, 15) != 15) break; if (bits (this_instr, 22, 25) == 0 && bits (this_instr, 4, 7) == 9) /* multiply */ error ("Illegal update to pc in instruction"); /* BX , BLX */ if (bits (this_instr, 4, 28) == 0x12fff1 || bits (this_instr, 4, 28) == 0x12fff3) { rn = bits (this_instr, 0, 3); result = (rn == 15) ? pc_val + 8 : read_register (rn); nextpc = (CORE_ADDR) ADDR_BITS_REMOVE (result); if (nextpc == pc) error ("Infinite loop detected"); return nextpc; } /* Multiply into PC */ c = (status & FLAG_C) ? 1 : 0; rn = bits (this_instr, 16, 19); operand1 = (rn == 15) ? pc_val + 8 : read_register (rn); if (bit (this_instr, 25)) { unsigned long immval = bits (this_instr, 0, 7); unsigned long rotate = 2 * bits (this_instr, 8, 11); operand2 = ((immval >> rotate) | (immval << (32 - rotate))) & 0xffffffff; } else /* operand 2 is a shifted register */ operand2 = shifted_reg_val (this_instr, c, pc_val, status); switch (bits (this_instr, 21, 24)) { case 0x0: /*and */ result = operand1 & operand2; break; case 0x1: /*eor */ result = operand1 ^ operand2; break; case 0x2: /*sub */ result = operand1 - operand2; break; case 0x3: /*rsb */ result = operand2 - operand1; break; case 0x4: /*add */ result = operand1 + operand2; break; case 0x5: /*adc */ result = operand1 + operand2 + c; break; case 0x6: /*sbc */ result = operand1 - operand2 + c; break; case 0x7: /*rsc */ result = operand2 - operand1 + c; break; case 0x8: case 0x9: case 0xa: case 0xb: /* tst, teq, cmp, cmn */ result = (unsigned long) nextpc; break; case 0xc: /*orr */ result = operand1 | operand2; break; case 0xd: /*mov */ /* Always step into a function. */ result = operand2; break; case 0xe: /*bic */ result = operand1 & ~operand2; break; case 0xf: /*mvn */ result = ~operand2; break; } nextpc = (CORE_ADDR) ADDR_BITS_REMOVE (result); if (nextpc == pc) error ("Infinite loop detected"); break; } case 0x4: case 0x5: /* data transfer */ case 0x6: case 0x7: if (bit (this_instr, 20)) { /* load */ if (bits (this_instr, 12, 15) == 15) { /* rd == pc */ unsigned long rn; unsigned long base; if (bit (this_instr, 22)) error ("Illegal update to pc in instruction"); /* byte write to PC */ rn = bits (this_instr, 16, 19); base = (rn == 15) ? pc_val + 8 : read_register (rn); if (bit (this_instr, 24)) { /* pre-indexed */ int c = (status & FLAG_C) ? 1 : 0; unsigned long offset = (bit (this_instr, 25) ? shifted_reg_val (this_instr, c, pc_val, status) : bits (this_instr, 0, 11)); if (bit (this_instr, 23)) base += offset; else base -= offset; } nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) base, 4); nextpc = ADDR_BITS_REMOVE (nextpc); if (nextpc == pc) error ("Infinite loop detected"); } } break; case 0x8: case 0x9: /* block transfer */ if (bit (this_instr, 20)) { /* LDM */ if (bit (this_instr, 15)) { /* loading pc */ int offset = 0; if (bit (this_instr, 23)) { /* up */ unsigned long reglist = bits (this_instr, 0, 14); offset = bitcount (reglist) * 4; if (bit (this_instr, 24)) /* pre */ offset += 4; } else if (bit (this_instr, 24)) offset = -4; { unsigned long rn_val = read_register (bits (this_instr, 16, 19)); nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) (rn_val + offset), 4); } nextpc = ADDR_BITS_REMOVE (nextpc); if (nextpc == pc) error ("Infinite loop detected"); } } break; case 0xb: /* branch & link */ case 0xa: /* branch */ { nextpc = BranchDest (pc, this_instr); /* BLX */ if (bits (this_instr, 28, 31) == INST_NV) nextpc |= bit (this_instr, 24) << 1; nextpc = ADDR_BITS_REMOVE (nextpc); if (nextpc == pc) error ("Infinite loop detected"); break; } case 0xc: case 0xd: case 0xe: /* coproc ops */ case 0xf: /* SWI */ break; default: fprintf_filtered (gdb_stderr, "Bad bit-field extraction\n"); return (pc); } } return nextpc; } /* single_step() is called just before we want to resume the inferior, if we want to single-step it but there is no hardware or kernel single-step support. We find the target of the coming instruction and breakpoint it. single_step() is also called just after the inferior stops. If we had set up a simulated single-step, we undo our damage. */ static void arm_software_single_step (enum target_signal sig, int insert_bpt) { static int next_pc; /* State between setting and unsetting. */ static char break_mem[BREAKPOINT_MAX]; /* Temporary storage for mem@bpt */ if (insert_bpt) { next_pc = arm_get_next_pc (read_register (ARM_PC_REGNUM)); target_insert_breakpoint (next_pc, break_mem); } else target_remove_breakpoint (next_pc, break_mem); } #include "bfd-in2.h" #include "libcoff.h" static int gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info) { if (arm_pc_is_thumb (memaddr)) { static asymbol *asym; static combined_entry_type ce; static struct coff_symbol_struct csym; static struct bfd fake_bfd; static bfd_target fake_target; if (csym.native == NULL) { /* Create a fake symbol vector containing a Thumb symbol. This is solely so that the code in print_insn_little_arm() and print_insn_big_arm() in opcodes/arm-dis.c will detect the presence of a Thumb symbol and switch to decoding Thumb instructions. */ fake_target.flavour = bfd_target_coff_flavour; fake_bfd.xvec = &fake_target; ce.u.syment.n_sclass = C_THUMBEXTFUNC; csym.native = &ce; csym.symbol.the_bfd = &fake_bfd; csym.symbol.name = "fake"; asym = (asymbol *) & csym; } memaddr = UNMAKE_THUMB_ADDR (memaddr); info->symbols = &asym; } else info->symbols = NULL; if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) return print_insn_big_arm (memaddr, info); else return print_insn_little_arm (memaddr, info); } /* The following define instruction sequences that will cause ARM cpu's to take an undefined instruction trap. These are used to signal a breakpoint to GDB. The newer ARMv4T cpu's are capable of operating in ARM or Thumb modes. A different instruction is required for each mode. The ARM cpu's can also be big or little endian. Thus four different instructions are needed to support all cases. Note: ARMv4 defines several new instructions that will take the undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does not in fact add the new instructions. The new undefined instructions in ARMv4 are all instructions that had no defined behaviour in earlier chips. There is no guarantee that they will raise an exception, but may be treated as NOP's. In practice, it may only safe to rely on instructions matching: 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 C C C C 0 1 1 x x x x x x x x x x x x x x x x x x x x 1 x x x x Even this may only true if the condition predicate is true. The following use a condition predicate of ALWAYS so it is always TRUE. There are other ways of forcing a breakpoint. GNU/Linux, RISC iX, and NetBSD all use a software interrupt rather than an undefined instruction to force a trap. This can be handled by by the abi-specific code during establishment of the gdbarch vector. */ /* NOTE rearnsha 2002-02-18: for now we allow a non-multi-arch gdb to override these definitions. */ #ifndef ARM_LE_BREAKPOINT #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7} #endif #ifndef ARM_BE_BREAKPOINT #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE} #endif #ifndef THUMB_LE_BREAKPOINT #define THUMB_LE_BREAKPOINT {0xfe,0xdf} #endif #ifndef THUMB_BE_BREAKPOINT #define THUMB_BE_BREAKPOINT {0xdf,0xfe} #endif static const char arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT; static const char arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT; static const char arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT; static const char arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT; /* Determine the type and size of breakpoint to insert at PCPTR. Uses the program counter value to determine whether a 16-bit or 32-bit breakpoint should be used. It returns a pointer to a string of bytes that encode a breakpoint instruction, stores the length of the string to *lenptr, and adjusts the program counter (if necessary) to point to the actual memory location where the breakpoint should be inserted. */ /* XXX ??? from old tm-arm.h: if we're using RDP, then we're inserting breakpoints and storing their handles instread of what was in memory. It is nice that this is the same size as a handle - otherwise remote-rdp will have to change. */ static const unsigned char * arm_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr) { struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); if (arm_pc_is_thumb (*pcptr)) { *pcptr = UNMAKE_THUMB_ADDR (*pcptr); *lenptr = tdep->thumb_breakpoint_size; return tdep->thumb_breakpoint; } else { *lenptr = tdep->arm_breakpoint_size; return tdep->arm_breakpoint; } } /* Extract from an array REGBUF containing the (raw) register state a function return value of type TYPE, and copy that, in virtual format, into VALBUF. */ static void arm_extract_return_value (struct type *type, struct regcache *regs, void *dst) { bfd_byte *valbuf = dst; if (TYPE_CODE_FLT == TYPE_CODE (type)) { switch (arm_get_fp_model (current_gdbarch)) { case ARM_FLOAT_FPA: { /* The value is in register F0 in internal format. We need to extract the raw value and then convert it to the desired internal type. */ bfd_byte tmpbuf[FP_REGISTER_SIZE]; regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf); convert_from_extended (floatformat_from_type (type), tmpbuf, valbuf); } break; case ARM_FLOAT_SOFT_FPA: case ARM_FLOAT_SOFT_VFP: regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf); if (TYPE_LENGTH (type) > 4) regcache_cooked_read (regs, ARM_A1_REGNUM + 1, valbuf + INT_REGISTER_SIZE); break; default: internal_error (__FILE__, __LINE__, "arm_extract_return_value: Floating point model not supported"); break; } } else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_CHAR || TYPE_CODE (type) == TYPE_CODE_BOOL || TYPE_CODE (type) == TYPE_CODE_PTR || TYPE_CODE (type) == TYPE_CODE_REF || TYPE_CODE (type) == TYPE_CODE_ENUM) { /* If the the type is a plain integer, then the access is straight-forward. Otherwise we have to play around a bit more. */ int len = TYPE_LENGTH (type); int regno = ARM_A1_REGNUM; ULONGEST tmp; while (len > 0) { /* By using store_unsigned_integer we avoid having to do anything special for small big-endian values. */ regcache_cooked_read_unsigned (regs, regno++, &tmp); store_unsigned_integer (valbuf, (len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len), tmp); len -= INT_REGISTER_SIZE; valbuf += INT_REGISTER_SIZE; } } else { /* For a structure or union the behaviour is as if the value had been stored to word-aligned memory and then loaded into registers with 32-bit load instruction(s). */ int len = TYPE_LENGTH (type); int regno = ARM_A1_REGNUM; bfd_byte tmpbuf[INT_REGISTER_SIZE]; while (len > 0) { regcache_cooked_read (regs, regno++, tmpbuf); memcpy (valbuf, tmpbuf, len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len); len -= INT_REGISTER_SIZE; valbuf += INT_REGISTER_SIZE; } } } /* Extract from an array REGBUF containing the (raw) register state the address in which a function should return its structure value. */ static CORE_ADDR arm_extract_struct_value_address (struct regcache *regcache) { ULONGEST ret; regcache_cooked_read_unsigned (regcache, ARM_A1_REGNUM, &ret); return ret; } /* Will a function return an aggregate type in memory or in a register? Return 0 if an aggregate type can be returned in a register, 1 if it must be returned in memory. */ static int arm_use_struct_convention (int gcc_p, struct type *type) { int nRc; enum type_code code; CHECK_TYPEDEF (type); /* In the ARM ABI, "integer" like aggregate types are returned in registers. For an aggregate type to be integer like, its size must be less than or equal to DEPRECATED_REGISTER_SIZE and the offset of each addressable subfield must be zero. Note that bit fields are not addressable, and all addressable subfields of unions always start at offset zero. This function is based on the behaviour of GCC 2.95.1. See: gcc/arm.c: arm_return_in_memory() for details. Note: All versions of GCC before GCC 2.95.2 do not set up the parameters correctly for a function returning the following structure: struct { float f;}; This should be returned in memory, not a register. Richard Earnshaw sent me a patch, but I do not know of any way to detect if a function like the above has been compiled with the correct calling convention. */ /* All aggregate types that won't fit in a register must be returned in memory. */ if (TYPE_LENGTH (type) > DEPRECATED_REGISTER_SIZE) { return 1; } /* The only aggregate types that can be returned in a register are structs and unions. Arrays must be returned in memory. */ code = TYPE_CODE (type); if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code)) { return 1; } /* Assume all other aggregate types can be returned in a register. Run a check for structures, unions and arrays. */ nRc = 0; if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code)) { int i; /* Need to check if this struct/union is "integer" like. For this to be true, its size must be less than or equal to DEPRECATED_REGISTER_SIZE and the offset of each addressable subfield must be zero. Note that bit fields are not addressable, and unions always start at offset zero. If any of the subfields is a floating point type, the struct/union cannot be an integer type. */ /* For each field in the object, check: 1) Is it FP? --> yes, nRc = 1; 2) Is it addressable (bitpos != 0) and not packed (bitsize == 0)? --> yes, nRc = 1 */ for (i = 0; i < TYPE_NFIELDS (type); i++) { enum type_code field_type_code; field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, i))); /* Is it a floating point type field? */ if (field_type_code == TYPE_CODE_FLT) { nRc = 1; break; } /* If bitpos != 0, then we have to care about it. */ if (TYPE_FIELD_BITPOS (type, i) != 0) { /* Bitfields are not addressable. If the field bitsize is zero, then the field is not packed. Hence it cannot be a bitfield or any other packed type. */ if (TYPE_FIELD_BITSIZE (type, i) == 0) { nRc = 1; break; } } } } return nRc; } /* Write into appropriate registers a function return value of type TYPE, given in virtual format. */ static void arm_store_return_value (struct type *type, struct regcache *regs, const void *src) { const bfd_byte *valbuf = src; if (TYPE_CODE (type) == TYPE_CODE_FLT) { char buf[MAX_REGISTER_SIZE]; switch (arm_get_fp_model (current_gdbarch)) { case ARM_FLOAT_FPA: convert_to_extended (floatformat_from_type (type), buf, valbuf); regcache_cooked_write (regs, ARM_F0_REGNUM, buf); break; case ARM_FLOAT_SOFT_FPA: case ARM_FLOAT_SOFT_VFP: regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf); if (TYPE_LENGTH (type) > 4) regcache_cooked_write (regs, ARM_A1_REGNUM + 1, valbuf + INT_REGISTER_SIZE); break; default: internal_error (__FILE__, __LINE__, "arm_store_return_value: Floating point model not supported"); break; } } else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_CHAR || TYPE_CODE (type) == TYPE_CODE_BOOL || TYPE_CODE (type) == TYPE_CODE_PTR || TYPE_CODE (type) == TYPE_CODE_REF || TYPE_CODE (type) == TYPE_CODE_ENUM) { if (TYPE_LENGTH (type) <= 4) { /* Values of one word or less are zero/sign-extended and returned in r0. */ bfd_byte tmpbuf[INT_REGISTER_SIZE]; LONGEST val = unpack_long (type, valbuf); store_signed_integer (tmpbuf, INT_REGISTER_SIZE, val); regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf); } else { /* Integral values greater than one word are stored in consecutive registers starting with r0. This will always be a multiple of the regiser size. */ int len = TYPE_LENGTH (type); int regno = ARM_A1_REGNUM; while (len > 0) { regcache_cooked_write (regs, regno++, valbuf); len -= INT_REGISTER_SIZE; valbuf += INT_REGISTER_SIZE; } } } else { /* For a structure or union the behaviour is as if the value had been stored to word-aligned memory and then loaded into registers with 32-bit load instruction(s). */ int len = TYPE_LENGTH (type); int regno = ARM_A1_REGNUM; bfd_byte tmpbuf[INT_REGISTER_SIZE]; while (len > 0) { memcpy (tmpbuf, valbuf, len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len); regcache_cooked_write (regs, regno++, tmpbuf); len -= INT_REGISTER_SIZE; valbuf += INT_REGISTER_SIZE; } } } static int arm_get_longjmp_target (CORE_ADDR *pc) { CORE_ADDR jb_addr; char buf[INT_REGISTER_SIZE]; struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); jb_addr = read_register (ARM_A1_REGNUM); if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf, INT_REGISTER_SIZE)) return 0; *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE); return 1; } /* Return non-zero if the PC is inside a thumb call thunk. */ int arm_in_call_stub (CORE_ADDR pc, char *name) { CORE_ADDR start_addr; /* Find the starting address of the function containing the PC. If the caller didn't give us a name, look it up at the same time. */ if (0 == find_pc_partial_function (pc, name ? NULL : &name, &start_addr, NULL)) return 0; return strncmp (name, "_call_via_r", 11) == 0; } /* If PC is in a Thumb call or return stub, return the address of the target PC, which is in a register. The thunk functions are called _called_via_xx, where x is the register name. The possible names are r0-r9, sl, fp, ip, sp, and lr. */ CORE_ADDR arm_skip_stub (CORE_ADDR pc) { char *name; CORE_ADDR start_addr; /* Find the starting address and name of the function containing the PC. */ if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0) return 0; /* Call thunks always start with "_call_via_". */ if (strncmp (name, "_call_via_", 10) == 0) { /* Use the name suffix to determine which register contains the target PC. */ static char *table[15] = {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "sl", "fp", "ip", "sp", "lr" }; int regno; for (regno = 0; regno <= 14; regno++) if (strcmp (&name[10], table[regno]) == 0) return read_register (regno); } return 0; /* not a stub */ } static void set_arm_command (char *args, int from_tty) { printf_unfiltered ("\"set arm\" must be followed by an apporpriate subcommand.\n"); help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout); } static void show_arm_command (char *args, int from_tty) { cmd_show_list (showarmcmdlist, from_tty, ""); } enum arm_float_model arm_get_fp_model (struct gdbarch *gdbarch) { if (arm_fp_model == ARM_FLOAT_AUTO) return gdbarch_tdep (gdbarch)->fp_model; return arm_fp_model; } static void arm_set_fp (struct gdbarch *gdbarch) { enum arm_float_model fp_model = arm_get_fp_model (gdbarch); if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE && (fp_model == ARM_FLOAT_SOFT_FPA || fp_model == ARM_FLOAT_FPA)) { set_gdbarch_double_format (gdbarch, &floatformat_ieee_double_littlebyte_bigword); set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_littlebyte_bigword); } else { set_gdbarch_double_format (gdbarch, &floatformat_ieee_double_little); set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_little); } } static void set_fp_model_sfunc (char *args, int from_tty, struct cmd_list_element *c) { enum arm_float_model fp_model; for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++) if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0) { arm_fp_model = fp_model; break; } if (fp_model == ARM_FLOAT_LAST) internal_error (__FILE__, __LINE__, "Invalid fp model accepted: %s.", current_fp_model); if (gdbarch_bfd_arch_info (current_gdbarch)->arch == bfd_arch_arm) arm_set_fp (current_gdbarch); } static void show_fp_model (char *args, int from_tty, struct cmd_list_element *c) { struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); if (arm_fp_model == ARM_FLOAT_AUTO && gdbarch_bfd_arch_info (current_gdbarch)->arch == bfd_arch_arm) printf_filtered (" - the default for the current ABI is \"%s\".\n", fp_model_strings[tdep->fp_model]); } /* If the user changes the register disassembly style used for info register and other commands, we have to also switch the style used in opcodes for disassembly output. This function is run in the "set arm disassembly" command, and does that. */ static void set_disassembly_style_sfunc (char *args, int from_tty, struct cmd_list_element *c) { set_disassembly_style (); } /* Return the ARM register name corresponding to register I. */ static const char * arm_register_name (int i) { return arm_register_names[i]; } static void set_disassembly_style (void) { const char *setname, *setdesc, **regnames; int numregs, j; /* Find the style that the user wants in the opcodes table. */ int current = 0; numregs = get_arm_regnames (current, &setname, &setdesc, ®names); while ((disassembly_style != setname) && (current < num_disassembly_options)) get_arm_regnames (++current, &setname, &setdesc, ®names); current_option = current; /* Fill our copy. */ for (j = 0; j < numregs; j++) arm_register_names[j] = (char *) regnames[j]; /* Adjust case. */ if (isupper (*regnames[ARM_PC_REGNUM])) { arm_register_names[ARM_FPS_REGNUM] = "FPS"; arm_register_names[ARM_PS_REGNUM] = "CPSR"; } else { arm_register_names[ARM_FPS_REGNUM] = "fps"; arm_register_names[ARM_PS_REGNUM] = "cpsr"; } /* Synchronize the disassembler. */ set_arm_regname_option (current); } /* arm_othernames implements the "othernames" command. This is deprecated by the "set arm disassembly" command. */ static void arm_othernames (char *names, int n) { /* Circle through the various flavors. */ current_option = (current_option + 1) % num_disassembly_options; disassembly_style = valid_disassembly_styles[current_option]; set_disassembly_style (); } /* Test whether the coff symbol specific value corresponds to a Thumb function. */ static int coff_sym_is_thumb (int val) { return (val == C_THUMBEXT || val == C_THUMBSTAT || val == C_THUMBEXTFUNC || val == C_THUMBSTATFUNC || val == C_THUMBLABEL); } /* arm_coff_make_msymbol_special() arm_elf_make_msymbol_special() These functions test whether the COFF or ELF symbol corresponds to an address in thumb code, and set a "special" bit in a minimal symbol to indicate that it does. */ static void arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym) { /* Thumb symbols are of type STT_LOPROC, (synonymous with STT_ARM_TFUNC). */ if (ELF_ST_TYPE (((elf_symbol_type *)sym)->internal_elf_sym.st_info) == STT_LOPROC) MSYMBOL_SET_SPECIAL (msym); } static void arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym) { if (coff_sym_is_thumb (val)) MSYMBOL_SET_SPECIAL (msym); } static void arm_write_pc (CORE_ADDR pc, ptid_t ptid) { write_register_pid (ARM_PC_REGNUM, pc, ptid); /* If necessary, set the T bit. */ if (arm_apcs_32) { CORE_ADDR val = read_register_pid (ARM_PS_REGNUM, ptid); if (arm_pc_is_thumb (pc)) write_register_pid (ARM_PS_REGNUM, val | 0x20, ptid); else write_register_pid (ARM_PS_REGNUM, val & ~(CORE_ADDR) 0x20, ptid); } } static enum gdb_osabi arm_elf_osabi_sniffer (bfd *abfd) { unsigned int elfosabi, eflags; enum gdb_osabi osabi = GDB_OSABI_UNKNOWN; elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI]; switch (elfosabi) { case ELFOSABI_NONE: /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the file are conforming to the base specification for that machine (there are no OS-specific extensions). In order to determine the real OS in use we must look for OS notes that have been added. */ bfd_map_over_sections (abfd, generic_elf_osabi_sniff_abi_tag_sections, &osabi); if (osabi == GDB_OSABI_UNKNOWN) { /* Existing ARM tools don't set this field, so look at the EI_FLAGS field for more information. */ eflags = EF_ARM_EABI_VERSION(elf_elfheader(abfd)->e_flags); switch (eflags) { case EF_ARM_EABI_VER1: osabi = GDB_OSABI_ARM_EABI_V1; break; case EF_ARM_EABI_VER2: osabi = GDB_OSABI_ARM_EABI_V2; break; case EF_ARM_EABI_VER3: case EF_ARM_EABI_VER4: case EF_ARM_EABI_VER5: /* * GDB does not support these EABI versions. Fallback * to the highest known to make the KGDB working with * kernel ELF image. */ osabi = GDB_OSABI_ARM_EABI_V2; printf ("\n%s:%d " "arm_elf_osabi_sniffer: Unsupported ARM EABI " "version 0x%x, falling back to 0x%x\n", __FILE__, __LINE__, eflags, EF_ARM_EABI_VER2); break; case EF_ARM_EABI_UNKNOWN: /* Assume GNU tools. */ osabi = GDB_OSABI_ARM_APCS; break; default: internal_error (__FILE__, __LINE__, "arm_elf_osabi_sniffer: Unknown ARM EABI " "version 0x%x", eflags); } } break; case ELFOSABI_ARM: /* GNU tools use this value. Check note sections in this case, as well. */ bfd_map_over_sections (abfd, generic_elf_osabi_sniff_abi_tag_sections, &osabi); if (osabi == GDB_OSABI_UNKNOWN) { /* Assume APCS ABI. */ osabi = GDB_OSABI_ARM_APCS; } break; case ELFOSABI_FREEBSD: osabi = GDB_OSABI_FREEBSD_ELF; break; case ELFOSABI_NETBSD: osabi = GDB_OSABI_NETBSD_ELF; break; case ELFOSABI_LINUX: osabi = GDB_OSABI_LINUX; break; } return osabi; } /* Initialize the current architecture based on INFO. If possible, re-use an architecture from ARCHES, which is a list of architectures already created during this debugging session. Called e.g. at program startup, when reading a core file, and when reading a binary file. */ static struct gdbarch * arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) { struct gdbarch_tdep *tdep; struct gdbarch *gdbarch; /* Try to deterimine the ABI of the object we are loading. */ if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN) { switch (bfd_get_flavour (info.abfd)) { case bfd_target_aout_flavour: /* Assume it's an old APCS-style ABI. */ info.osabi = GDB_OSABI_ARM_APCS; break; case bfd_target_coff_flavour: /* Assume it's an old APCS-style ABI. */ /* XXX WinCE? */ info.osabi = GDB_OSABI_ARM_APCS; break; default: /* Leave it as "unknown". */ break; } } /* If there is already a candidate, use it. */ arches = gdbarch_list_lookup_by_info (arches, &info); if (arches != NULL) return arches->gdbarch; tdep = xmalloc (sizeof (struct gdbarch_tdep)); gdbarch = gdbarch_alloc (&info, tdep); /* We used to default to FPA for generic ARM, but almost nobody uses that now, and we now provide a way for the user to force the model. So default to the most useful variant. */ tdep->fp_model = ARM_FLOAT_SOFT_FPA; /* Breakpoints. */ switch (info.byte_order) { case BFD_ENDIAN_BIG: tdep->arm_breakpoint = arm_default_arm_be_breakpoint; tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint); tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint; tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint); break; case BFD_ENDIAN_LITTLE: tdep->arm_breakpoint = arm_default_arm_le_breakpoint; tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint); tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint; tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint); break; default: internal_error (__FILE__, __LINE__, "arm_gdbarch_init: bad byte order for float format"); } /* On ARM targets char defaults to unsigned. */ set_gdbarch_char_signed (gdbarch, 0); /* This should be low enough for everything. */ tdep->lowest_pc = 0x20; tdep->jb_pc = -1; /* Longjump support not enabled by default. */ set_gdbarch_deprecated_call_dummy_words (gdbarch, arm_call_dummy_words); set_gdbarch_deprecated_sizeof_call_dummy_words (gdbarch, 0); set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call); set_gdbarch_write_pc (gdbarch, arm_write_pc); /* Frame handling. */ set_gdbarch_unwind_dummy_id (gdbarch, arm_unwind_dummy_id); set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc); set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp); set_gdbarch_deprecated_frameless_function_invocation (gdbarch, arm_frameless_function_invocation); frame_base_set_default (gdbarch, &arm_normal_base); /* Address manipulation. */ set_gdbarch_smash_text_address (gdbarch, arm_smash_text_address); set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove); /* Advance PC across function entry code. */ set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue); /* Get the PC when a frame might not be available. */ set_gdbarch_deprecated_saved_pc_after_call (gdbarch, arm_saved_pc_after_call); /* The stack grows downward. */ set_gdbarch_inner_than (gdbarch, core_addr_lessthan); /* Breakpoint manipulation. */ set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc); /* Information about registers, etc. */ set_gdbarch_print_float_info (gdbarch, arm_print_float_info); set_gdbarch_deprecated_fp_regnum (gdbarch, ARM_FP_REGNUM); /* ??? */ set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM); set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM); set_gdbarch_deprecated_register_byte (gdbarch, arm_register_byte); set_gdbarch_deprecated_register_bytes (gdbarch, (NUM_GREGS * INT_REGISTER_SIZE + NUM_FREGS * FP_REGISTER_SIZE + NUM_SREGS * STATUS_REGISTER_SIZE)); set_gdbarch_num_regs (gdbarch, NUM_GREGS + NUM_FREGS + NUM_SREGS); set_gdbarch_register_type (gdbarch, arm_register_type); /* Internal <-> external register number maps. */ set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno); /* Integer registers are 4 bytes. */ set_gdbarch_deprecated_register_size (gdbarch, 4); set_gdbarch_register_name (gdbarch, arm_register_name); /* Returning results. */ set_gdbarch_extract_return_value (gdbarch, arm_extract_return_value); set_gdbarch_store_return_value (gdbarch, arm_store_return_value); set_gdbarch_use_struct_convention (gdbarch, arm_use_struct_convention); set_gdbarch_deprecated_extract_struct_value_address (gdbarch, arm_extract_struct_value_address); /* Single stepping. */ /* XXX For an RDI target we should ask the target if it can single-step. */ set_gdbarch_software_single_step (gdbarch, arm_software_single_step); /* Disassembly. */ set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm); /* Minsymbol frobbing. */ set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special); set_gdbarch_coff_make_msymbol_special (gdbarch, arm_coff_make_msymbol_special); /* Hook in the ABI-specific overrides, if they have been registered. */ gdbarch_init_osabi (info, gdbarch); /* Add some default predicates. */ frame_unwind_append_sniffer (gdbarch, arm_sigtramp_unwind_sniffer); frame_unwind_append_sniffer (gdbarch, arm_prologue_unwind_sniffer); /* Now we have tuned the configuration, set a few final things, based on what the OS ABI has told us. */ if (tdep->jb_pc >= 0) set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target); /* Floating point sizes and format. */ switch (info.byte_order) { case BFD_ENDIAN_BIG: set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_big); set_gdbarch_double_format (gdbarch, &floatformat_ieee_double_big); set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_big); break; case BFD_ENDIAN_LITTLE: set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_little); arm_set_fp (gdbarch); break; default: internal_error (__FILE__, __LINE__, "arm_gdbarch_init: bad byte order for float format"); } return gdbarch; } static void arm_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file) { struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); if (tdep == NULL) return; fprintf_unfiltered (file, "arm_dump_tdep: Lowest pc = 0x%lx", (unsigned long) tdep->lowest_pc); } static void arm_init_abi_eabi_v1 (struct gdbarch_info info, struct gdbarch *gdbarch) { /* Place-holder. */ } static void arm_init_abi_eabi_v2 (struct gdbarch_info info, struct gdbarch *gdbarch) { /* Place-holder. */ } static void arm_init_abi_apcs (struct gdbarch_info info, struct gdbarch *gdbarch) { /* Place-holder. */ } extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */ void _initialize_arm_tdep (void) { struct ui_file *stb; long length; struct cmd_list_element *new_set, *new_show; const char *setname; const char *setdesc; const char **regnames; int numregs, i, j; static char *helptext; gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep); /* Register an ELF OS ABI sniffer for ARM binaries. */ gdbarch_register_osabi_sniffer (bfd_arch_arm, bfd_target_elf_flavour, arm_elf_osabi_sniffer); /* Register some ABI variants for embedded systems. */ gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_ARM_EABI_V1, arm_init_abi_eabi_v1); gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_ARM_EABI_V2, arm_init_abi_eabi_v2); gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_ARM_APCS, arm_init_abi_apcs); /* Get the number of possible sets of register names defined in opcodes. */ num_disassembly_options = get_arm_regname_num_options (); /* Add root prefix command for all "set arm"/"show arm" commands. */ add_prefix_cmd ("arm", no_class, set_arm_command, "Various ARM-specific commands.", &setarmcmdlist, "set arm ", 0, &setlist); add_prefix_cmd ("arm", no_class, show_arm_command, "Various ARM-specific commands.", &showarmcmdlist, "show arm ", 0, &showlist); /* Sync the opcode insn printer with our register viewer. */ parse_arm_disassembler_option ("reg-names-std"); /* Begin creating the help text. */ stb = mem_fileopen (); fprintf_unfiltered (stb, "Set the disassembly style.\n" "The valid values are:\n"); /* Initialize the array that will be passed to add_set_enum_cmd(). */ valid_disassembly_styles = xmalloc ((num_disassembly_options + 1) * sizeof (char *)); for (i = 0; i < num_disassembly_options; i++) { numregs = get_arm_regnames (i, &setname, &setdesc, ®names); valid_disassembly_styles[i] = setname; fprintf_unfiltered (stb, "%s - %s\n", setname, setdesc); /* Copy the default names (if found) and synchronize disassembler. */ if (!strcmp (setname, "std")) { disassembly_style = setname; current_option = i; for (j = 0; j < numregs; j++) arm_register_names[j] = (char *) regnames[j]; set_arm_regname_option (i); } } /* Mark the end of valid options. */ valid_disassembly_styles[num_disassembly_options] = NULL; /* Finish the creation of the help text. */ fprintf_unfiltered (stb, "The default is \"std\"."); helptext = ui_file_xstrdup (stb, &length); ui_file_delete (stb); /* Add the deprecated disassembly-flavor command. */ new_set = add_set_enum_cmd ("disassembly-flavor", no_class, valid_disassembly_styles, &disassembly_style, helptext, &setlist); set_cmd_sfunc (new_set, set_disassembly_style_sfunc); deprecate_cmd (new_set, "set arm disassembly"); deprecate_cmd (add_show_from_set (new_set, &showlist), "show arm disassembly"); /* And now add the new interface. */ new_set = add_set_enum_cmd ("disassembler", no_class, valid_disassembly_styles, &disassembly_style, helptext, &setarmcmdlist); set_cmd_sfunc (new_set, set_disassembly_style_sfunc); add_show_from_set (new_set, &showarmcmdlist); add_setshow_cmd_full ("apcs32", no_class, var_boolean, (char *) &arm_apcs_32, "Set usage of ARM 32-bit mode.", "Show usage of ARM 32-bit mode.", NULL, NULL, &setlist, &showlist, &new_set, &new_show); deprecate_cmd (new_set, "set arm apcs32"); deprecate_cmd (new_show, "show arm apcs32"); add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32, "Set usage of ARM 32-bit mode. " "When off, a 26-bit PC will be used.", "Show usage of ARM 32-bit mode. " "When off, a 26-bit PC will be used.", NULL, NULL, &setarmcmdlist, &showarmcmdlist); /* Add a command to allow the user to force the FPU model. */ new_set = add_set_enum_cmd ("fpu", no_class, fp_model_strings, ¤t_fp_model, "Set the floating point type.\n" "auto - Determine the FP typefrom the OS-ABI.\n" "softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n" "fpa - FPA co-processor (GCC compiled).\n" "softvfp - Software FP with pure-endian doubles.\n" "vfp - VFP co-processor.", &setarmcmdlist); set_cmd_sfunc (new_set, set_fp_model_sfunc); set_cmd_sfunc (add_show_from_set (new_set, &showarmcmdlist), show_fp_model); /* Add the deprecated "othernames" command. */ deprecate_cmd (add_com ("othernames", class_obscure, arm_othernames, "Switch to the next set of register names."), "set arm disassembly"); /* Debugging flag. */ add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug, "Set ARM debugging. " "When on, arm-specific debugging is enabled.", "Show ARM debugging. " "When on, arm-specific debugging is enabled.", NULL, NULL, &setdebuglist, &showdebuglist); } Index: head/contrib/gdb/gdb/frame.c =================================================================== --- head/contrib/gdb/gdb/frame.c (revision 298357) +++ head/contrib/gdb/gdb/frame.c (revision 298358) @@ -1,2380 +1,2384 @@ /* Cache and manage frames for GDB, the GNU debugger. Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GDB. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "defs.h" #include "frame.h" #include "target.h" #include "value.h" #include "inferior.h" /* for inferior_ptid */ #include "regcache.h" #include "gdb_assert.h" #include "gdb_string.h" #include "user-regs.h" #include "gdb_obstack.h" #include "dummy-frame.h" #include "sentinel-frame.h" #include "gdbcore.h" #include "annotate.h" #include "language.h" #include "frame-unwind.h" #include "frame-base.h" #include "command.h" #include "gdbcmd.h" /* We keep a cache of stack frames, each of which is a "struct frame_info". The innermost one gets allocated (in wait_for_inferior) each time the inferior stops; current_frame points to it. Additional frames get allocated (in get_prev_frame) as needed, and are chained through the next and prev fields. Any time that the frame cache becomes invalid (most notably when we execute something, but also if we change how we interpret the frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything which reads new symbols)), we should call reinit_frame_cache. */ struct frame_info { /* Level of this frame. The inner-most (youngest) frame is at level 0. As you move towards the outer-most (oldest) frame, the level increases. This is a cached value. It could just as easily be computed by counting back from the selected frame to the inner most frame. */ /* NOTE: cagney/2002-04-05: Perhaphs a level of ``-1'' should be reserved to indicate a bogus frame - one that has been created just to keep GDB happy (GDB always needs a frame). For the moment leave this as speculation. */ int level; /* The frame's type. */ /* FIXME: cagney/2003-04-02: Should instead be returning ->unwind->type. Unfortunately, legacy code is still explicitly setting the type using the method deprecated_set_frame_type. Eliminate that method and this field can be eliminated. */ enum frame_type type; /* For each register, address of where it was saved on entry to the frame, or zero if it was not saved on entry to this frame. This includes special registers such as pc and fp saved in special ways in the stack frame. The SP_REGNUM is even more special, the address here is the sp for the previous frame, not the address where the sp was saved. */ /* Allocated by frame_saved_regs_zalloc () which is called / initialized by DEPRECATED_FRAME_INIT_SAVED_REGS(). */ CORE_ADDR *saved_regs; /*NUM_REGS + NUM_PSEUDO_REGS*/ /* Anything extra for this structure that may have been defined in the machine dependent files. */ /* Allocated by frame_extra_info_zalloc () which is called / initialized by DEPRECATED_INIT_EXTRA_FRAME_INFO */ struct frame_extra_info *extra_info; /* The frame's low-level unwinder and corresponding cache. The low-level unwinder is responsible for unwinding register values for the previous frame. The low-level unwind methods are selected based on the presence, or otherwize, of register unwind information such as CFI. */ void *prologue_cache; const struct frame_unwind *unwind; /* Cached copy of the previous frame's resume address. */ struct { int p; CORE_ADDR value; } prev_pc; /* Cached copy of the previous frame's function address. */ struct { CORE_ADDR addr; int p; } prev_func; /* This frame's ID. */ struct { int p; struct frame_id value; } this_id; /* The frame's high-level base methods, and corresponding cache. The high level base methods are selected based on the frame's debug info. */ const struct frame_base *base; void *base_cache; /* Pointers to the next (down, inner, younger) and previous (up, outer, older) frame_info's in the frame cache. */ struct frame_info *next; /* down, inner, younger */ int prev_p; struct frame_info *prev; /* up, outer, older */ }; /* Flag to control debugging. */ static int frame_debug; /* Flag to indicate whether backtraces should stop at main et.al. */ static int backtrace_past_main; static unsigned int backtrace_limit = UINT_MAX; +int (*frame_tdep_pc_fixup)(CORE_ADDR *pc); void fprint_frame_id (struct ui_file *file, struct frame_id id) { fprintf_unfiltered (file, "{stack=0x%s,code=0x%s,special=0x%s}", paddr_nz (id.stack_addr), paddr_nz (id.code_addr), paddr_nz (id.special_addr)); } static void fprint_frame_type (struct ui_file *file, enum frame_type type) { switch (type) { case UNKNOWN_FRAME: fprintf_unfiltered (file, "UNKNOWN_FRAME"); return; case NORMAL_FRAME: fprintf_unfiltered (file, "NORMAL_FRAME"); return; case DUMMY_FRAME: fprintf_unfiltered (file, "DUMMY_FRAME"); return; case SIGTRAMP_FRAME: fprintf_unfiltered (file, "SIGTRAMP_FRAME"); return; default: fprintf_unfiltered (file, ""); return; }; } static void fprint_frame (struct ui_file *file, struct frame_info *fi) { if (fi == NULL) { fprintf_unfiltered (file, ""); return; } fprintf_unfiltered (file, "{"); fprintf_unfiltered (file, "level=%d", fi->level); fprintf_unfiltered (file, ","); fprintf_unfiltered (file, "type="); fprint_frame_type (file, fi->type); fprintf_unfiltered (file, ","); fprintf_unfiltered (file, "unwind="); if (fi->unwind != NULL) gdb_print_host_address (fi->unwind, file); else fprintf_unfiltered (file, ""); fprintf_unfiltered (file, ","); fprintf_unfiltered (file, "pc="); if (fi->next != NULL && fi->next->prev_pc.p) fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_pc.value)); else fprintf_unfiltered (file, ""); fprintf_unfiltered (file, ","); fprintf_unfiltered (file, "id="); if (fi->this_id.p) fprint_frame_id (file, fi->this_id.value); else fprintf_unfiltered (file, ""); fprintf_unfiltered (file, ","); fprintf_unfiltered (file, "func="); if (fi->next != NULL && fi->next->prev_func.p) fprintf_unfiltered (file, "0x%s", paddr_nz (fi->next->prev_func.addr)); else fprintf_unfiltered (file, ""); fprintf_unfiltered (file, "}"); } /* Return a frame uniq ID that can be used to, later, re-find the frame. */ struct frame_id get_frame_id (struct frame_info *fi) { if (fi == NULL) { return null_frame_id; } if (!fi->this_id.p) { gdb_assert (!legacy_frame_p (current_gdbarch)); if (frame_debug) fprintf_unfiltered (gdb_stdlog, "{ get_frame_id (fi=%d) ", fi->level); /* Find the unwinder. */ if (fi->unwind == NULL) { fi->unwind = frame_unwind_find_by_frame (fi->next); /* FIXME: cagney/2003-04-02: Rather than storing the frame's type in the frame, the unwinder's type should be returned directly. Unfortunately, legacy code, called by legacy_get_prev_frame, explicitly set the frames type using the method deprecated_set_frame_type(). */ fi->type = fi->unwind->type; } /* Find THIS frame's ID. */ fi->unwind->this_id (fi->next, &fi->prologue_cache, &fi->this_id.value); fi->this_id.p = 1; if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame_id (gdb_stdlog, fi->this_id.value); fprintf_unfiltered (gdb_stdlog, " }\n"); } } return fi->this_id.value; } const struct frame_id null_frame_id; /* All zeros. */ struct frame_id frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr, CORE_ADDR special_addr) { struct frame_id id; id.stack_addr = stack_addr; id.code_addr = code_addr; id.special_addr = special_addr; return id; } struct frame_id frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr) { return frame_id_build_special (stack_addr, code_addr, 0); } int frame_id_p (struct frame_id l) { int p; /* The .code can be NULL but the .stack cannot. */ p = (l.stack_addr != 0); if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "{ frame_id_p (l="); fprint_frame_id (gdb_stdlog, l); fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", p); } return p; } int frame_id_eq (struct frame_id l, struct frame_id r) { int eq; if (l.stack_addr == 0 || r.stack_addr == 0) /* Like a NaN, if either ID is invalid, the result is false. */ eq = 0; else if (l.stack_addr != r.stack_addr) /* If .stack addresses are different, the frames are different. */ eq = 0; else if (l.code_addr == 0 || r.code_addr == 0) /* A zero code addr is a wild card, always succeed. */ eq = 1; else if (l.code_addr != r.code_addr) /* If .code addresses are different, the frames are different. */ eq = 0; else if (l.special_addr == 0 || r.special_addr == 0) /* A zero special addr is a wild card (or unused), always succeed. */ eq = 1; else if (l.special_addr == r.special_addr) /* Frames are equal. */ eq = 1; else /* No luck. */ eq = 0; if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "{ frame_id_eq (l="); fprint_frame_id (gdb_stdlog, l); fprintf_unfiltered (gdb_stdlog, ",r="); fprint_frame_id (gdb_stdlog, r); fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", eq); } return eq; } int frame_id_inner (struct frame_id l, struct frame_id r) { int inner; if (l.stack_addr == 0 || r.stack_addr == 0) /* Like NaN, any operation involving an invalid ID always fails. */ inner = 0; else /* Only return non-zero when strictly inner than. Note that, per comment in "frame.h", there is some fuzz here. Frameless functions are not strictly inner than (same .stack but different .code and/or .special address). */ inner = INNER_THAN (l.stack_addr, r.stack_addr); if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "{ frame_id_inner (l="); fprint_frame_id (gdb_stdlog, l); fprintf_unfiltered (gdb_stdlog, ",r="); fprint_frame_id (gdb_stdlog, r); fprintf_unfiltered (gdb_stdlog, ") -> %d }\n", inner); } return inner; } struct frame_info * frame_find_by_id (struct frame_id id) { struct frame_info *frame; /* ZERO denotes the null frame, let the caller decide what to do about it. Should it instead return get_current_frame()? */ if (!frame_id_p (id)) return NULL; for (frame = get_current_frame (); frame != NULL; frame = get_prev_frame (frame)) { struct frame_id this = get_frame_id (frame); if (frame_id_eq (id, this)) /* An exact match. */ return frame; if (frame_id_inner (id, this)) /* Gone to far. */ return NULL; /* Either, we're not yet gone far enough out along the frame chain (inner(this,id), or we're comparing frameless functions (same .base, different .func, no test available). Struggle on until we've definitly gone to far. */ } return NULL; } CORE_ADDR frame_pc_unwind (struct frame_info *this_frame) { if (!this_frame->prev_pc.p) { CORE_ADDR pc; if (gdbarch_unwind_pc_p (current_gdbarch)) { /* The right way. The `pure' way. The one true way. This method depends solely on the register-unwind code to determine the value of registers in THIS frame, and hence the value of this frame's PC (resume address). A typical implementation is no more than: frame_unwind_register (this_frame, ISA_PC_REGNUM, buf); return extract_unsigned_integer (buf, size of ISA_PC_REGNUM); Note: this method is very heavily dependent on a correct register-unwind implementation, it pays to fix that method first; this method is frame type agnostic, since it only deals with register values, it works with any frame. This is all in stark contrast to the old FRAME_SAVED_PC which would try to directly handle all the different ways that a PC could be unwound. */ pc = gdbarch_unwind_pc (current_gdbarch, this_frame); } else if (this_frame->level < 0) { /* FIXME: cagney/2003-03-06: Old code and and a sentinel frame. Do like was always done. Fetch the PC's value direct from the global registers array (via read_pc). This assumes that this frame belongs to the current global register cache. The assumption is dangerous. */ pc = read_pc (); } else if (DEPRECATED_FRAME_SAVED_PC_P ()) { /* FIXME: cagney/2003-03-06: Old code, but not a sentinel frame. Do like was always done. Note that this method, unlike unwind_pc(), tries to handle all the different frame cases directly. It fails. */ pc = DEPRECATED_FRAME_SAVED_PC (this_frame); } else internal_error (__FILE__, __LINE__, "No gdbarch_unwind_pc method"); this_frame->prev_pc.value = pc; this_frame->prev_pc.p = 1; if (frame_debug) fprintf_unfiltered (gdb_stdlog, "{ frame_pc_unwind (this_frame=%d) -> 0x%s }\n", this_frame->level, paddr_nz (this_frame->prev_pc.value)); } return this_frame->prev_pc.value; } CORE_ADDR frame_func_unwind (struct frame_info *fi) { if (!fi->prev_func.p) { /* Make certain that this, and not the adjacent, function is found. */ CORE_ADDR addr_in_block = frame_unwind_address_in_block (fi); fi->prev_func.p = 1; fi->prev_func.addr = get_pc_function_start (addr_in_block); if (frame_debug) fprintf_unfiltered (gdb_stdlog, "{ frame_func_unwind (fi=%d) -> 0x%s }\n", fi->level, paddr_nz (fi->prev_func.addr)); } return fi->prev_func.addr; } CORE_ADDR get_frame_func (struct frame_info *fi) { return frame_func_unwind (fi->next); } static int do_frame_unwind_register (void *src, int regnum, void *buf) { frame_unwind_register (src, regnum, buf); return 1; } void frame_pop (struct frame_info *this_frame) { struct regcache *scratch_regcache; struct cleanup *cleanups; if (DEPRECATED_POP_FRAME_P ()) { /* A legacy architecture that has implemented a custom pop function. All new architectures should instead be using the generic code below. */ DEPRECATED_POP_FRAME; } else { /* Make a copy of all the register values unwound from this frame. Save them in a scratch buffer so that there isn't a race betweening trying to extract the old values from the current_regcache while, at the same time writing new values into that same cache. */ struct regcache *scratch = regcache_xmalloc (current_gdbarch); struct cleanup *cleanups = make_cleanup_regcache_xfree (scratch); regcache_save (scratch, do_frame_unwind_register, this_frame); /* FIXME: cagney/2003-03-16: It should be possible to tell the target's register cache that it is about to be hit with a burst register transfer and that the sequence of register writes should be batched. The pair target_prepare_to_store() and target_store_registers() kind of suggest this functionality. Unfortunately, they don't implement it. Their lack of a formal definition can lead to targets writing back bogus values (arguably a bug in the target code mind). */ /* Now copy those saved registers into the current regcache. Here, regcache_cpy() calls regcache_restore(). */ regcache_cpy (current_regcache, scratch); do_cleanups (cleanups); } /* We've made right mess of GDB's local state, just discard everything. */ flush_cached_frames (); } void frame_register_unwind (struct frame_info *frame, int regnum, int *optimizedp, enum lval_type *lvalp, CORE_ADDR *addrp, int *realnump, void *bufferp) { struct frame_unwind_cache *cache; if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "\ { frame_register_unwind (frame=%d,regnum=%d(%s),...) ", frame->level, regnum, frame_map_regnum_to_name (frame, regnum)); } /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates that the value proper does not need to be fetched. */ gdb_assert (optimizedp != NULL); gdb_assert (lvalp != NULL); gdb_assert (addrp != NULL); gdb_assert (realnump != NULL); /* gdb_assert (bufferp != NULL); */ /* NOTE: cagney/2002-11-27: A program trying to unwind a NULL frame is broken. There is always a frame. If there, for some reason, isn't, there is some pretty busted code as it should have detected the problem before calling here. */ gdb_assert (frame != NULL); /* Find the unwinder. */ if (frame->unwind == NULL) { frame->unwind = frame_unwind_find_by_frame (frame->next); /* FIXME: cagney/2003-04-02: Rather than storing the frame's type in the frame, the unwinder's type should be returned directly. Unfortunately, legacy code, called by legacy_get_prev_frame, explicitly set the frames type using the method deprecated_set_frame_type(). */ frame->type = frame->unwind->type; } /* Ask this frame to unwind its register. See comment in "frame-unwind.h" for why NEXT frame and this unwind cace are passed in. */ frame->unwind->prev_register (frame->next, &frame->prologue_cache, regnum, optimizedp, lvalp, addrp, realnump, bufferp); if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "->"); fprintf_unfiltered (gdb_stdlog, " *optimizedp=%d", (*optimizedp)); fprintf_unfiltered (gdb_stdlog, " *lvalp=%d", (int) (*lvalp)); fprintf_unfiltered (gdb_stdlog, " *addrp=0x%s", paddr_nz ((*addrp))); fprintf_unfiltered (gdb_stdlog, " *bufferp="); if (bufferp == NULL) fprintf_unfiltered (gdb_stdlog, ""); else { int i; const unsigned char *buf = bufferp; fprintf_unfiltered (gdb_stdlog, "["); for (i = 0; i < register_size (current_gdbarch, regnum); i++) fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]); fprintf_unfiltered (gdb_stdlog, "]"); } fprintf_unfiltered (gdb_stdlog, " }\n"); } } void frame_register (struct frame_info *frame, int regnum, int *optimizedp, enum lval_type *lvalp, CORE_ADDR *addrp, int *realnump, void *bufferp) { /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates that the value proper does not need to be fetched. */ gdb_assert (optimizedp != NULL); gdb_assert (lvalp != NULL); gdb_assert (addrp != NULL); gdb_assert (realnump != NULL); /* gdb_assert (bufferp != NULL); */ /* Ulgh! Old code that, for lval_register, sets ADDRP to the offset of the register in the register cache. It should instead return the REGNUM corresponding to that register. Translate the . */ if (DEPRECATED_GET_SAVED_REGISTER_P ()) { DEPRECATED_GET_SAVED_REGISTER (bufferp, optimizedp, addrp, frame, regnum, lvalp); /* Compute the REALNUM if the caller wants it. */ if (*lvalp == lval_register) { int regnum; for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++) { if (*addrp == register_offset_hack (current_gdbarch, regnum)) { *realnump = regnum; return; } } internal_error (__FILE__, __LINE__, "Failed to compute the register number corresponding" " to 0x%s", paddr_d (*addrp)); } *realnump = -1; return; } /* Obtain the register value by unwinding the register from the next (more inner frame). */ gdb_assert (frame != NULL && frame->next != NULL); frame_register_unwind (frame->next, regnum, optimizedp, lvalp, addrp, realnump, bufferp); } void frame_unwind_register (struct frame_info *frame, int regnum, void *buf) { int optimized; CORE_ADDR addr; int realnum; enum lval_type lval; frame_register_unwind (frame, regnum, &optimized, &lval, &addr, &realnum, buf); } void get_frame_register (struct frame_info *frame, int regnum, void *buf) { frame_unwind_register (frame->next, regnum, buf); } LONGEST frame_unwind_register_signed (struct frame_info *frame, int regnum) { char buf[MAX_REGISTER_SIZE]; frame_unwind_register (frame, regnum, buf); return extract_signed_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum)); } LONGEST get_frame_register_signed (struct frame_info *frame, int regnum) { return frame_unwind_register_signed (frame->next, regnum); } ULONGEST frame_unwind_register_unsigned (struct frame_info *frame, int regnum) { char buf[MAX_REGISTER_SIZE]; frame_unwind_register (frame, regnum, buf); return extract_unsigned_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum)); } ULONGEST get_frame_register_unsigned (struct frame_info *frame, int regnum) { return frame_unwind_register_unsigned (frame->next, regnum); } void frame_unwind_unsigned_register (struct frame_info *frame, int regnum, ULONGEST *val) { char buf[MAX_REGISTER_SIZE]; frame_unwind_register (frame, regnum, buf); (*val) = extract_unsigned_integer (buf, DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum)); } void put_frame_register (struct frame_info *frame, int regnum, const void *buf) { struct gdbarch *gdbarch = get_frame_arch (frame); int realnum; int optim; enum lval_type lval; CORE_ADDR addr; frame_register (frame, regnum, &optim, &lval, &addr, &realnum, NULL); if (optim) error ("Attempt to assign to a value that was optimized out."); switch (lval) { case lval_memory: { /* FIXME: write_memory doesn't yet take constant buffers. Arrrg! */ char tmp[MAX_REGISTER_SIZE]; memcpy (tmp, buf, register_size (gdbarch, regnum)); write_memory (addr, tmp, register_size (gdbarch, regnum)); break; } case lval_register: regcache_cooked_write (current_regcache, realnum, buf); break; default: error ("Attempt to assign to an unmodifiable value."); } } /* frame_register_read () Find and return the value of REGNUM for the specified stack frame. The number of bytes copied is DEPRECATED_REGISTER_RAW_SIZE (REGNUM). Returns 0 if the register value could not be found. */ int frame_register_read (struct frame_info *frame, int regnum, void *myaddr) { int optimized; enum lval_type lval; CORE_ADDR addr; int realnum; frame_register (frame, regnum, &optimized, &lval, &addr, &realnum, myaddr); /* FIXME: cagney/2002-05-15: This test, is just bogus. It indicates that the target failed to supply a value for a register because it was "not available" at this time. Problem is, the target still has the register and so get saved_register() may be returning a value saved on the stack. */ if (register_cached (regnum) < 0) return 0; /* register value not available */ return !optimized; } /* Map between a frame register number and its name. A frame register space is a superset of the cooked register space --- it also includes builtin registers. */ int frame_map_name_to_regnum (struct frame_info *frame, const char *name, int len) { return user_reg_map_name_to_regnum (get_frame_arch (frame), name, len); } const char * frame_map_regnum_to_name (struct frame_info *frame, int regnum) { return user_reg_map_regnum_to_name (get_frame_arch (frame), regnum); } /* Create a sentinel frame. */ static struct frame_info * create_sentinel_frame (struct regcache *regcache) { struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info); frame->type = NORMAL_FRAME; frame->level = -1; /* Explicitly initialize the sentinel frame's cache. Provide it with the underlying regcache. In the future additional information, such as the frame's thread will be added. */ frame->prologue_cache = sentinel_frame_cache (regcache); /* For the moment there is only one sentinel frame implementation. */ frame->unwind = sentinel_frame_unwind; /* Link this frame back to itself. The frame is self referential (the unwound PC is the same as the pc), so make it so. */ frame->next = frame; /* Make the sentinel frame's ID valid, but invalid. That way all comparisons with it should fail. */ frame->this_id.p = 1; frame->this_id.value = null_frame_id; if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "{ create_sentinel_frame (...) -> "); fprint_frame (gdb_stdlog, frame); fprintf_unfiltered (gdb_stdlog, " }\n"); } return frame; } /* Info about the innermost stack frame (contents of FP register) */ static struct frame_info *current_frame; /* Cache for frame addresses already read by gdb. Valid only while inferior is stopped. Control variables for the frame cache should be local to this module. */ static struct obstack frame_cache_obstack; void * frame_obstack_zalloc (unsigned long size) { void *data = obstack_alloc (&frame_cache_obstack, size); memset (data, 0, size); return data; } CORE_ADDR * frame_saved_regs_zalloc (struct frame_info *fi) { fi->saved_regs = (CORE_ADDR *) frame_obstack_zalloc (SIZEOF_FRAME_SAVED_REGS); return fi->saved_regs; } CORE_ADDR * deprecated_get_frame_saved_regs (struct frame_info *fi) { return fi->saved_regs; } /* Return the innermost (currently executing) stack frame. This is split into two functions. The function unwind_to_current_frame() is wrapped in catch exceptions so that, even when the unwind of the sentinel frame fails, the function still returns a stack frame. */ static int unwind_to_current_frame (struct ui_out *ui_out, void *args) { struct frame_info *frame = get_prev_frame (args); /* A sentinel frame can fail to unwind, eg, because it's PC value lands in somewhere like start. */ if (frame == NULL) return 1; current_frame = frame; return 0; } struct frame_info * get_current_frame (void) { /* First check, and report, the lack of registers. Having GDB report "No stack!" or "No memory" when the target doesn't even have registers is very confusing. Besides, "printcmd.exp" explicitly checks that ``print $pc'' with no registers prints "No registers". */ if (!target_has_registers) error ("No registers."); if (!target_has_stack) error ("No stack."); if (!target_has_memory) error ("No memory."); if (current_frame == NULL) { struct frame_info *sentinel_frame = create_sentinel_frame (current_regcache); if (catch_exceptions (uiout, unwind_to_current_frame, sentinel_frame, NULL, RETURN_MASK_ERROR) != 0) { /* Oops! Fake a current frame? Is this useful? It has a PC of zero, for instance. */ current_frame = sentinel_frame; } } return current_frame; } /* The "selected" stack frame is used by default for local and arg access. May be zero, for no selected frame. */ struct frame_info *deprecated_selected_frame; /* Return the selected frame. Always non-null (unless there isn't an inferior sufficient for creating a frame) in which case an error is thrown. */ struct frame_info * get_selected_frame (void) { if (deprecated_selected_frame == NULL) /* Hey! Don't trust this. It should really be re-finding the last selected frame of the currently selected thread. This, though, is better than nothing. */ select_frame (get_current_frame ()); /* There is always a frame. */ gdb_assert (deprecated_selected_frame != NULL); return deprecated_selected_frame; } /* This is a variant of get_selected_frame which can be called when the inferior does not have a frame; in that case it will return NULL instead of calling error (). */ struct frame_info * deprecated_safe_get_selected_frame (void) { if (!target_has_registers || !target_has_stack || !target_has_memory) return NULL; return get_selected_frame (); } /* Select frame FI (or NULL - to invalidate the current frame). */ void select_frame (struct frame_info *fi) { struct symtab *s; deprecated_selected_frame = fi; /* NOTE: cagney/2002-05-04: FI can be NULL. This occures when the frame is being invalidated. */ if (selected_frame_level_changed_hook) selected_frame_level_changed_hook (frame_relative_level (fi)); /* FIXME: kseitz/2002-08-28: It would be nice to call selected_frame_level_changed_event right here, but due to limitations in the current interfaces, we would end up flooding UIs with events because select_frame is used extensively internally. Once we have frame-parameterized frame (and frame-related) commands, the event notification can be moved here, since this function will only be called when the users selected frame is being changed. */ /* Ensure that symbols for this frame are read in. Also, determine the source language of this frame, and switch to it if desired. */ if (fi) { /* We retrieve the frame's symtab by using the frame PC. However we cannot use the frame pc as is, because it usually points to the instruction following the "call", which is sometimes the first instruction of another function. So we rely on get_frame_address_in_block() which provides us with a PC which is guaranteed to be inside the frame's code block. */ s = find_pc_symtab (get_frame_address_in_block (fi)); if (s && s->language != current_language->la_language && s->language != language_unknown && language_mode == language_mode_auto) { set_language (s->language); } } } /* Return the register saved in the simplistic ``saved_regs'' cache. If the value isn't here AND a value is needed, try the next inner most frame. */ static void legacy_saved_regs_prev_register (struct frame_info *next_frame, void **this_prologue_cache, int regnum, int *optimizedp, enum lval_type *lvalp, CORE_ADDR *addrp, int *realnump, void *bufferp) { /* HACK: New code is passed the next frame and this cache. Unfortunately, old code expects this frame. Since this is a backward compatibility hack, cheat by walking one level along the prologue chain to the frame the old code expects. Do not try this at home. Professional driver, closed course. */ struct frame_info *frame = next_frame->prev; gdb_assert (frame != NULL); if (deprecated_get_frame_saved_regs (frame) == NULL) { /* If nothing's initialized the saved regs, do it now. */ gdb_assert (DEPRECATED_FRAME_INIT_SAVED_REGS_P ()); DEPRECATED_FRAME_INIT_SAVED_REGS (frame); gdb_assert (deprecated_get_frame_saved_regs (frame) != NULL); } if (deprecated_get_frame_saved_regs (frame) != NULL && deprecated_get_frame_saved_regs (frame)[regnum] != 0) { if (regnum == SP_REGNUM) { /* SP register treated specially. */ *optimizedp = 0; *lvalp = not_lval; *addrp = 0; *realnump = -1; if (bufferp != NULL) /* NOTE: cagney/2003-05-09: In-lined store_address with it's body - store_unsigned_integer. */ store_unsigned_integer (bufferp, DEPRECATED_REGISTER_RAW_SIZE (regnum), deprecated_get_frame_saved_regs (frame)[regnum]); } else { /* Any other register is saved in memory, fetch it but cache a local copy of its value. */ *optimizedp = 0; *lvalp = lval_memory; *addrp = deprecated_get_frame_saved_regs (frame)[regnum]; *realnump = -1; if (bufferp != NULL) { #if 1 /* Save each register value, as it is read in, in a frame based cache. */ void **regs = (*this_prologue_cache); if (regs == NULL) { int sizeof_cache = ((NUM_REGS + NUM_PSEUDO_REGS) * sizeof (void *)); regs = frame_obstack_zalloc (sizeof_cache); (*this_prologue_cache) = regs; } if (regs[regnum] == NULL) { regs[regnum] = frame_obstack_zalloc (DEPRECATED_REGISTER_RAW_SIZE (regnum)); read_memory (deprecated_get_frame_saved_regs (frame)[regnum], regs[regnum], DEPRECATED_REGISTER_RAW_SIZE (regnum)); } memcpy (bufferp, regs[regnum], DEPRECATED_REGISTER_RAW_SIZE (regnum)); #else /* Read the value in from memory. */ read_memory (deprecated_get_frame_saved_regs (frame)[regnum], bufferp, DEPRECATED_REGISTER_RAW_SIZE (regnum)); #endif } } return; } /* No luck. Assume this and the next frame have the same register value. Pass the unwind request down the frame chain to the next frame. Hopefully that frame will find the register's location. */ frame_register_unwind (next_frame, regnum, optimizedp, lvalp, addrp, realnump, bufferp); } static void legacy_saved_regs_this_id (struct frame_info *next_frame, void **this_prologue_cache, struct frame_id *id) { /* A developer is trying to bring up a new architecture, help them by providing a default unwinder that refuses to unwind anything (the ID is always NULL). In the case of legacy code, legacy_get_prev_frame() will have previously set ->this_id.p, so this code won't be called. */ (*id) = null_frame_id; } const struct frame_unwind legacy_saved_regs_unwinder = { /* Not really. It gets overridden by legacy_get_prev_frame. */ UNKNOWN_FRAME, legacy_saved_regs_this_id, legacy_saved_regs_prev_register }; const struct frame_unwind *legacy_saved_regs_unwind = &legacy_saved_regs_unwinder; /* Function: deprecated_generic_get_saved_register Find register number REGNUM relative to FRAME and put its (raw, target format) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable was optimized out (and thus can't be fetched). Note that this is never set to anything other than zero in this implementation. Set *LVAL to lval_memory, lval_register, or not_lval, depending on whether the value was fetched from memory, from a register, or in a strange and non-modifiable way (e.g. a frame pointer which was calculated rather than fetched). We will use not_lval for values fetched from generic dummy frames. Set *ADDRP to the address, either in memory or as a DEPRECATED_REGISTER_BYTE offset into the registers array. If the value is stored in a dummy frame, set *ADDRP to zero. The argument RAW_BUFFER must point to aligned memory. */ void deprecated_generic_get_saved_register (char *raw_buffer, int *optimized, CORE_ADDR *addrp, struct frame_info *frame, int regnum, enum lval_type *lval) { if (!target_has_registers) error ("No registers."); /* Normal systems don't optimize out things with register numbers. */ if (optimized != NULL) *optimized = 0; if (addrp) /* default assumption: not found in memory */ *addrp = 0; /* Note: since the current frame's registers could only have been saved by frames INTERIOR TO the current frame, we skip examining the current frame itself: otherwise, we would be getting the previous frame's registers which were saved by the current frame. */ if (frame != NULL) { for (frame = get_next_frame (frame); frame_relative_level (frame) >= 0; frame = get_next_frame (frame)) { if (get_frame_type (frame) == DUMMY_FRAME) { if (lval) /* found it in a CALL_DUMMY frame */ *lval = not_lval; if (raw_buffer) /* FIXME: cagney/2002-06-26: This should be via the gdbarch_register_read() method so that it, on the fly, constructs either a raw or pseudo register from the raw register cache. */ regcache_raw_read (deprecated_find_dummy_frame_regcache (get_frame_pc (frame), get_frame_base (frame)), regnum, raw_buffer); return; } DEPRECATED_FRAME_INIT_SAVED_REGS (frame); if (deprecated_get_frame_saved_regs (frame) != NULL && deprecated_get_frame_saved_regs (frame)[regnum] != 0) { if (lval) /* found it saved on the stack */ *lval = lval_memory; if (regnum == SP_REGNUM) { if (raw_buffer) /* SP register treated specially */ /* NOTE: cagney/2003-05-09: In-line store_address with it's body - store_unsigned_integer. */ store_unsigned_integer (raw_buffer, DEPRECATED_REGISTER_RAW_SIZE (regnum), deprecated_get_frame_saved_regs (frame)[regnum]); } else { if (addrp) /* any other register */ *addrp = deprecated_get_frame_saved_regs (frame)[regnum]; if (raw_buffer) read_memory (deprecated_get_frame_saved_regs (frame)[regnum], raw_buffer, DEPRECATED_REGISTER_RAW_SIZE (regnum)); } return; } } } /* If we get thru the loop to this point, it means the register was not saved in any frame. Return the actual live-register value. */ if (lval) /* found it in a live register */ *lval = lval_register; if (addrp) *addrp = DEPRECATED_REGISTER_BYTE (regnum); if (raw_buffer) deprecated_read_register_gen (regnum, raw_buffer); } /* Determine the frame's type based on its PC. */ static enum frame_type frame_type_from_pc (CORE_ADDR pc) { /* FIXME: cagney/2002-11-24: Can't yet directly call pc_in_dummy_frame() as some architectures don't set PC_IN_CALL_DUMMY() to generic_pc_in_call_dummy() (remember the latter is implemented by simply calling pc_in_dummy_frame). */ if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES && DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0)) return DUMMY_FRAME; else { char *name; find_pc_partial_function (pc, &name, NULL, NULL); if (PC_IN_SIGTRAMP (pc, name)) return SIGTRAMP_FRAME; else return NORMAL_FRAME; } } /* Create an arbitrary (i.e. address specified by user) or innermost frame. Always returns a non-NULL value. */ struct frame_info * create_new_frame (CORE_ADDR addr, CORE_ADDR pc) { struct frame_info *fi; if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "{ create_new_frame (addr=0x%s, pc=0x%s) ", paddr_nz (addr), paddr_nz (pc)); } fi = frame_obstack_zalloc (sizeof (struct frame_info)); fi->next = create_sentinel_frame (current_regcache); /* Select/initialize both the unwind function and the frame's type based on the PC. */ fi->unwind = frame_unwind_find_by_frame (fi->next); if (fi->unwind->type != UNKNOWN_FRAME) fi->type = fi->unwind->type; else fi->type = frame_type_from_pc (pc); fi->this_id.p = 1; deprecated_update_frame_base_hack (fi, addr); deprecated_update_frame_pc_hack (fi, pc); if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ()) DEPRECATED_INIT_EXTRA_FRAME_INFO (0, fi); if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, fi); fprintf_unfiltered (gdb_stdlog, " }\n"); } return fi; } /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the innermost frame). Be careful to not fall off the bottom of the frame chain and onto the sentinel frame. */ struct frame_info * get_next_frame (struct frame_info *this_frame) { if (this_frame->level > 0) return this_frame->next; else return NULL; } /* Flush the entire frame cache. */ void flush_cached_frames (void) { /* Since we can't really be sure what the first object allocated was */ obstack_free (&frame_cache_obstack, 0); obstack_init (&frame_cache_obstack); current_frame = NULL; /* Invalidate cache */ select_frame (NULL); annotate_frames_invalid (); if (frame_debug) fprintf_unfiltered (gdb_stdlog, "{ flush_cached_frames () }\n"); } /* Flush the frame cache, and start a new one if necessary. */ void reinit_frame_cache (void) { flush_cached_frames (); /* FIXME: The inferior_ptid test is wrong if there is a corefile. */ if (PIDGET (inferior_ptid) != 0) { select_frame (get_current_frame ()); } } /* Create the previous frame using the deprecated methods INIT_EXTRA_INFO, INIT_FRAME_PC and INIT_FRAME_PC_FIRST. */ static struct frame_info * legacy_get_prev_frame (struct frame_info *this_frame) { CORE_ADDR address = 0; struct frame_info *prev; int fromleaf; /* Don't frame_debug print legacy_get_prev_frame() here, just confuses the output. */ /* Allocate the new frame. There is no reason to worry about memory leaks, should the remainder of the function fail. The allocated memory will be quickly reclaimed when the frame cache is flushed, and the `we've been here before' check, in get_prev_frame will stop repeated memory allocation calls. */ prev = FRAME_OBSTACK_ZALLOC (struct frame_info); prev->level = this_frame->level + 1; /* Do not completely wire it in to the frame chain. Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along frame->prev to pull some fancy tricks (of course such code is, by definition, recursive). On the other hand, methods, such as get_frame_pc() and get_frame_base() rely on being able to walk along the frame chain. Make certain that at least they work by providing that link. Of course things manipulating prev can't go back. */ prev->next = this_frame; /* NOTE: cagney/2002-11-18: Should have been correctly setting the frame's type here, before anything else, and not last, at the bottom of this function. The various DEPRECATED_INIT_EXTRA_FRAME_INFO, DEPRECATED_INIT_FRAME_PC, DEPRECATED_INIT_FRAME_PC_FIRST and DEPRECATED_FRAME_INIT_SAVED_REGS methods are full of work-arounds that handle the frame not being correctly set from the start. Unfortunately those same work-arounds rely on the type defaulting to NORMAL_FRAME. Ulgh! The new frame code does not have this problem. */ prev->type = UNKNOWN_FRAME; /* A legacy frame's ID is always computed here. Mark it as valid. */ prev->this_id.p = 1; /* Handle sentinel frame unwind as a special case. */ if (this_frame->level < 0) { /* Try to unwind the PC. If that doesn't work, assume we've reached the oldest frame and simply return. Is there a better sentinal value? The unwound PC value is then used to initialize the new previous frame's type. Note that the pc-unwind is intentionally performed before the frame chain. This is ok since, for old targets, both frame_pc_unwind (nee, DEPRECATED_FRAME_SAVED_PC) and DEPRECATED_FRAME_CHAIN()) assume THIS_FRAME's data structures have already been initialized (using DEPRECATED_INIT_EXTRA_FRAME_INFO) and hence the call order doesn't matter. By unwinding the PC first, it becomes possible to, in the case of a dummy frame, avoid also unwinding the frame ID. This is because (well ignoring the PPC) a dummy frame can be located using THIS_FRAME's frame ID. */ deprecated_update_frame_pc_hack (prev, frame_pc_unwind (this_frame)); if (get_frame_pc (prev) == 0) { /* The allocated PREV_FRAME will be reclaimed when the frame obstack is next purged. */ if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, NULL); fprintf_unfiltered (gdb_stdlog, " // unwound legacy PC zero }\n"); } return NULL; } /* Set the unwind functions based on that identified PC. Ditto for the "type" but strongly prefer the unwinder's frame type. */ prev->unwind = frame_unwind_find_by_frame (prev->next); if (prev->unwind->type == UNKNOWN_FRAME) prev->type = frame_type_from_pc (get_frame_pc (prev)); else prev->type = prev->unwind->type; /* Find the prev's frame's ID. */ if (prev->type == DUMMY_FRAME && gdbarch_unwind_dummy_id_p (current_gdbarch)) { /* When unwinding a normal frame, the stack structure is determined by analyzing the frame's function's code (be it using brute force prologue analysis, or the dwarf2 CFI). In the case of a dummy frame, that simply isn't possible. The The PC is either the program entry point, or some random address on the stack. Trying to use that PC to apply standard frame ID unwind techniques is just asking for trouble. */ /* Use an architecture specific method to extract the prev's dummy ID from the next frame. Note that this method uses frame_register_unwind to obtain the register values needed to determine the dummy frame's ID. */ prev->this_id.value = gdbarch_unwind_dummy_id (current_gdbarch, this_frame); } else { /* We're unwinding a sentinel frame, the PC of which is pointing at a stack dummy. Fake up the dummy frame's ID using the same sequence as is found a traditional unwinder. Once all architectures supply the unwind_dummy_id method, this code can go away. */ prev->this_id.value = frame_id_build (deprecated_read_fp (), read_pc ()); } /* Check that the unwound ID is valid. */ if (!frame_id_p (prev->this_id.value)) { if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, NULL); fprintf_unfiltered (gdb_stdlog, " // unwound legacy ID invalid }\n"); } return NULL; } /* Check that the new frame isn't inner to (younger, below, next) the old frame. If that happens the frame unwind is going backwards. */ /* FIXME: cagney/2003-02-25: Ignore the sentinel frame since that doesn't have a valid frame ID. Should instead set the sentinel frame's frame ID to a `sentinel'. Leave it until after the switch to storing the frame ID, instead of the frame base, in the frame object. */ /* Link it in. */ this_frame->prev = prev; /* FIXME: cagney/2002-01-19: This call will go away. Instead of initializing extra info, all frames will use the frame_cache (passed to the unwind functions) to store additional frame info. Unfortunately legacy targets can't use legacy_get_prev_frame() to unwind the sentinel frame and, consequently, are forced to take this code path and rely on the below call to DEPRECATED_INIT_EXTRA_FRAME_INFO to initialize the inner-most frame. */ if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ()) { DEPRECATED_INIT_EXTRA_FRAME_INFO (0, prev); } if (prev->type == NORMAL_FRAME) prev->this_id.value.code_addr = get_pc_function_start (prev->this_id.value.code_addr); if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, prev); fprintf_unfiltered (gdb_stdlog, " } // legacy innermost frame\n"); } return prev; } /* This code only works on normal frames. A sentinel frame, where the level is -1, should never reach this code. */ gdb_assert (this_frame->level >= 0); /* On some machines it is possible to call a function without setting up a stack frame for it. On these machines, we define this macro to take two args; a frameinfo pointer identifying a frame and a variable to set or clear if it is or isn't leafless. */ /* Still don't want to worry about this except on the innermost frame. This macro will set FROMLEAF if THIS_FRAME is a frameless function invocation. */ if (this_frame->level == 0) /* FIXME: 2002-11-09: Frameless functions can occure anywhere in the frame chain, not just the inner most frame! The generic, per-architecture, frame code should handle this and the below should simply be removed. */ fromleaf = (DEPRECATED_FRAMELESS_FUNCTION_INVOCATION_P () && DEPRECATED_FRAMELESS_FUNCTION_INVOCATION (this_frame)); else fromleaf = 0; if (fromleaf) /* A frameless inner-most frame. The `FP' (which isn't an architecture frame-pointer register!) of the caller is the same as the callee. */ /* FIXME: 2002-11-09: There isn't any reason to special case this edge condition. Instead the per-architecture code should hande it locally. */ /* FIXME: cagney/2003-06-16: This returns the inner most stack address for the previous frame, that, however, is wrong. It should be the inner most stack address for the previous to previous frame. This is because it is the previous to previous frame's innermost stack address that is constant through out the lifetime of the previous frame (trust me :-). */ address = get_frame_base (this_frame); else { /* Two macros defined in tm.h specify the machine-dependent actions to be performed here. First, get the frame's chain-pointer. If that is zero, the frame is the outermost frame or a leaf called by the outermost frame. This means that if start calls main without a frame, we'll return 0 (which is fine anyway). Nope; there's a problem. This also returns when the current routine is a leaf of main. This is unacceptable. We move this to after the ffi test; I'd rather have backtraces from start go curfluy than have an abort called from main not show main. */ if (DEPRECATED_FRAME_CHAIN_P ()) address = DEPRECATED_FRAME_CHAIN (this_frame); else { /* Someone is part way through coverting an old architecture to the new frame code. Implement FRAME_CHAIN the way the new frame will. */ /* Find PREV frame's unwinder. */ prev->unwind = frame_unwind_find_by_frame (this_frame->next); /* FIXME: cagney/2003-04-02: Rather than storing the frame's type in the frame, the unwinder's type should be returned directly. Unfortunately, legacy code, called by legacy_get_prev_frame, explicitly set the frames type using the method deprecated_set_frame_type(). */ prev->type = prev->unwind->type; /* Find PREV frame's ID. */ prev->unwind->this_id (this_frame, &prev->prologue_cache, &prev->this_id.value); prev->this_id.p = 1; address = prev->this_id.value.stack_addr; } if (!legacy_frame_chain_valid (address, this_frame)) { if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, NULL); fprintf_unfiltered (gdb_stdlog, " // legacy frame chain invalid }\n"); } return NULL; } } if (address == 0) { if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, NULL); fprintf_unfiltered (gdb_stdlog, " // legacy frame chain NULL }\n"); } return NULL; } /* Link in the already allocated prev frame. */ this_frame->prev = prev; deprecated_update_frame_base_hack (prev, address); /* This change should not be needed, FIXME! We should determine whether any targets *need* DEPRECATED_INIT_FRAME_PC to happen after DEPRECATED_INIT_EXTRA_FRAME_INFO and come up with a simple way to express what goes on here. DEPRECATED_INIT_EXTRA_FRAME_INFO is called from two places: create_new_frame (where the PC is already set up) and here (where it isn't). DEPRECATED_INIT_FRAME_PC is only called from here, always after DEPRECATED_INIT_EXTRA_FRAME_INFO. The catch is the MIPS, where DEPRECATED_INIT_EXTRA_FRAME_INFO requires the PC value (which hasn't been set yet). Some other machines appear to require DEPRECATED_INIT_EXTRA_FRAME_INFO before they can do DEPRECATED_INIT_FRAME_PC. Phoo. We shouldn't need DEPRECATED_INIT_FRAME_PC_FIRST to add more complication to an already overcomplicated part of GDB. gnu@cygnus.com, 15Sep92. Assuming that some machines need DEPRECATED_INIT_FRAME_PC after DEPRECATED_INIT_EXTRA_FRAME_INFO, one possible scheme: SETUP_INNERMOST_FRAME(): Default version is just create_new_frame (deprecated_read_fp ()), read_pc ()). Machines with extra frame info would do that (or the local equivalent) and then set the extra fields. SETUP_ARBITRARY_FRAME(argc, argv): Only change here is that create_new_frame would no longer init extra frame info; SETUP_ARBITRARY_FRAME would have to do that. INIT_PREV_FRAME(fromleaf, prev) Replace DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC. This should also return a flag saying whether to keep the new frame, or whether to discard it, because on some machines (e.g. mips) it is really awkward to have DEPRECATED_FRAME_CHAIN_VALID called BEFORE DEPRECATED_INIT_EXTRA_FRAME_INFO (there is no good way to get information deduced in DEPRECATED_FRAME_CHAIN_VALID into the extra fields of the new frame). std_frame_pc(fromleaf, prev) This is the default setting for INIT_PREV_FRAME. It just does what the default DEPRECATED_INIT_FRAME_PC does. Some machines will call it from INIT_PREV_FRAME (either at the beginning, the end, or in the middle). Some machines won't use it. kingdon@cygnus.com, 13Apr93, 31Jan94, 14Dec94. */ /* NOTE: cagney/2002-11-09: Just ignore the above! There is no reason for things to be this complicated. The trick is to assume that there is always a frame. Instead of special casing the inner-most frame, create fake frame (containing the hardware registers) that is inner to the user-visible inner-most frame (...) and then unwind from that. That way architecture code can use use the standard frame_XX_unwind() functions and not differentiate between the inner most and any other case. Since there is always a frame to unwind from, there is always somewhere (THIS_FRAME) to store all the info needed to construct a new (previous) frame without having to first create it. This means that the convolution below - needing to carefully order a frame's initialization - isn't needed. The irony here though, is that DEPRECATED_FRAME_CHAIN(), at least for a more up-to-date architecture, always calls FRAME_SAVED_PC(), and FRAME_SAVED_PC() computes the PC but without first needing the frame! Instead of the convolution below, we could have simply called FRAME_SAVED_PC() and been done with it! Note that FRAME_SAVED_PC() is being superseed by frame_pc_unwind() and that function does have somewhere to cache that PC value. */ if (DEPRECATED_INIT_FRAME_PC_FIRST_P ()) deprecated_update_frame_pc_hack (prev, DEPRECATED_INIT_FRAME_PC_FIRST (fromleaf, prev)); if (DEPRECATED_INIT_EXTRA_FRAME_INFO_P ()) DEPRECATED_INIT_EXTRA_FRAME_INFO (fromleaf, prev); /* This entry is in the frame queue now, which is good since FRAME_SAVED_PC may use that queue to figure out its value (see tm-sparc.h). We want the pc saved in the inferior frame. */ if (DEPRECATED_INIT_FRAME_PC_P ()) deprecated_update_frame_pc_hack (prev, DEPRECATED_INIT_FRAME_PC (fromleaf, prev)); /* If ->frame and ->pc are unchanged, we are in the process of getting ourselves into an infinite backtrace. Some architectures check this in DEPRECATED_FRAME_CHAIN or thereabouts, but it seems like there is no reason this can't be an architecture-independent check. */ if (get_frame_base (prev) == get_frame_base (this_frame) && get_frame_pc (prev) == get_frame_pc (this_frame)) { this_frame->prev = NULL; obstack_free (&frame_cache_obstack, prev); if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, NULL); fprintf_unfiltered (gdb_stdlog, " // legacy this.id == prev.id }\n"); } return NULL; } /* Initialize the code used to unwind the frame PREV based on the PC (and probably other architectural information). The PC lets you check things like the debug info at that point (dwarf2cfi?) and use that to decide how the frame should be unwound. If there isn't a FRAME_CHAIN, the code above will have already done this. */ if (prev->unwind == NULL) prev->unwind = frame_unwind_find_by_frame (prev->next); /* If the unwinder provides a frame type, use it. Otherwize continue on to that heuristic mess. */ if (prev->unwind->type != UNKNOWN_FRAME) { prev->type = prev->unwind->type; if (prev->type == NORMAL_FRAME) /* FIXME: cagney/2003-06-16: would get_frame_pc() be better? */ prev->this_id.value.code_addr = get_pc_function_start (prev->this_id.value.code_addr); if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, prev); fprintf_unfiltered (gdb_stdlog, " } // legacy with unwound type\n"); } return prev; } /* NOTE: cagney/2002-11-18: The code segments, found in create_new_frame and get_prev_frame(), that initializes the frames type is subtly different. The latter only updates ->type when it encounters a SIGTRAMP_FRAME or DUMMY_FRAME. This stops get_prev_frame() overriding the frame's type when the INIT code has previously set it. This is really somewhat bogus. The initialization, as seen in create_new_frame(), should occur before the INIT function has been called. */ if (DEPRECATED_USE_GENERIC_DUMMY_FRAMES && (DEPRECATED_PC_IN_CALL_DUMMY_P () ? DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (prev), 0, 0) : pc_in_dummy_frame (get_frame_pc (prev)))) prev->type = DUMMY_FRAME; else { /* FIXME: cagney/2002-11-10: This should be moved to before the INIT code above so that the INIT code knows what the frame's type is (in fact, for a [generic] dummy-frame, the type can be set and then the entire initialization can be skipped. Unforunatly, its the INIT code that sets the PC (Hmm, catch 22). */ char *name; find_pc_partial_function (get_frame_pc (prev), &name, NULL, NULL); if (PC_IN_SIGTRAMP (get_frame_pc (prev), name)) prev->type = SIGTRAMP_FRAME; /* FIXME: cagney/2002-11-11: Leave prev->type alone. Some architectures are forcing the frame's type in INIT so we don't want to override it here. Remember, NORMAL_FRAME == 0, so it all works (just :-/). Once this initialization is moved to the start of this function, all this nastness will go away. */ } if (prev->type == NORMAL_FRAME) prev->this_id.value.code_addr = get_pc_function_start (prev->this_id.value.code_addr); if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, prev); fprintf_unfiltered (gdb_stdlog, " } // legacy with confused type\n"); } return prev; } /* Return a structure containing various interesting information about the frame that called THIS_FRAME. Returns NULL if there is no such frame. This function tests some target-independent conditions that should terminate the frame chain, such as unwinding past main(). It should not contain any target-dependent tests, such as checking whether the program-counter is zero. */ struct frame_info * get_prev_frame (struct frame_info *this_frame) { struct frame_info *prev_frame; if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "{ get_prev_frame (this_frame="); if (this_frame != NULL) fprintf_unfiltered (gdb_stdlog, "%d", this_frame->level); else fprintf_unfiltered (gdb_stdlog, ""); fprintf_unfiltered (gdb_stdlog, ") "); } /* Return the inner-most frame, when the caller passes in NULL. */ /* NOTE: cagney/2002-11-09: Not sure how this would happen. The caller should have previously obtained a valid frame using get_selected_frame() and then called this code - only possibility I can think of is code behaving badly. NOTE: cagney/2003-01-10: Talk about code behaving badly. Check block_innermost_frame(). It does the sequence: frame = NULL; while (1) { frame = get_prev_frame (frame); .... }. Ulgh! Why it couldn't be written better, I don't know. NOTE: cagney/2003-01-11: I suspect what is happening is block_innermost_frame() is, when the target has no state (registers, memory, ...), still calling this function. The assumption being that this function will return NULL indicating that a frame isn't possible, rather than checking that the target has state and then calling get_current_frame() and get_prev_frame(). This is a guess mind. */ if (this_frame == NULL) { /* NOTE: cagney/2002-11-09: There was a code segment here that would error out when CURRENT_FRAME was NULL. The comment that went with it made the claim ... ``This screws value_of_variable, which just wants a nice clean NULL return from block_innermost_frame if there are no frames. I don't think I've ever seen this message happen otherwise. And returning NULL here is a perfectly legitimate thing to do.'' Per the above, this code shouldn't even be called with a NULL THIS_FRAME. */ return current_frame; } /* There is always a frame. If this assertion fails, suspect that something should be calling get_selected_frame() or get_current_frame(). */ gdb_assert (this_frame != NULL); /* Make sure we pass an address within THIS_FRAME's code block to inside_main_func. Otherwise, we might stop unwinding at a function which has a call instruction as its last instruction if that function immediately precedes main(). */ if (this_frame->level >= 0 && !backtrace_past_main && inside_main_func (get_frame_address_in_block (this_frame))) /* Don't unwind past main(), bug always unwind the sentinel frame. Note, this is done _before_ the frame has been marked as previously unwound. That way if the user later decides to allow unwinds past main(), that just happens. */ { if (frame_debug) fprintf_unfiltered (gdb_stdlog, "-> NULL // inside main func }\n"); return NULL; } if (this_frame->level > backtrace_limit) { error ("Backtrace limit of %d exceeded", backtrace_limit); } /* If we're already inside the entry function for the main objfile, then it isn't valid. Don't apply this test to a dummy frame - dummy frame PC's typically land in the entry func. Don't apply this test to the sentinel frame. Sentinel frames should always be allowed to unwind. */ /* NOTE: cagney/2003-02-25: Don't enable until someone has found hard evidence that this is needed. */ /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func - wasn't checking for "main" in the minimal symbols. With that fixed asm-source tests now stop in "main" instead of halting the backtrace in wierd and wonderful ways somewhere inside the entry file. Suspect that deprecated_inside_entry_file and inside_entry_func tests were added to work around that (now fixed) case. */ /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right) suggested having the inside_entry_func test use the inside_main_func msymbol trick (along with entry_point_address I guess) to determine the address range of the start function. That should provide a far better stopper than the current heuristics. */ /* NOTE: cagney/2003-07-15: Need to add a "set backtrace beyond-entry-func" command so that this can be selectively disabled. */ if (0 #if 0 && backtrace_beyond_entry_func #endif && this_frame->type != DUMMY_FRAME && this_frame->level >= 0 && inside_entry_func (this_frame)) { if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, NULL); fprintf_unfiltered (gdb_stdlog, "// inside entry func }\n"); } return NULL; } /* Assume that the only way to get a zero PC is through something like a SIGSEGV or a dummy frame, and hence that NORMAL frames will never unwind a zero PC. */ if (this_frame->level > 0 && get_frame_type (this_frame) == NORMAL_FRAME && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME && get_frame_pc (this_frame) == 0) { if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, this_frame->prev); fprintf_unfiltered (gdb_stdlog, " // zero PC \n"); } return NULL; } /* Only try to do the unwind once. */ if (this_frame->prev_p) { if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, this_frame->prev); fprintf_unfiltered (gdb_stdlog, " // cached \n"); } return this_frame->prev; } this_frame->prev_p = 1; /* If we're inside the entry file, it isn't valid. Don't apply this test to a dummy frame - dummy frame PC's typically land in the entry file. Don't apply this test to the sentinel frame. Sentinel frames should always be allowed to unwind. */ /* NOTE: drow/2002-12-25: should there be a way to disable this check? It assumes a single small entry file, and the way some debug readers (e.g. dbxread) figure out which object is the entry file is somewhat hokey. */ /* NOTE: cagney/2003-01-10: If there is a way of disabling this test then it should probably be moved to before the ->prev_p test, above. */ /* NOTE: vinschen/2003-04-01: Disabled. It turns out that the call to deprecated_inside_entry_file destroys a meaningful backtrace under some conditions. E. g. the backtrace tests in the asm-source testcase are broken for some targets. In this test the functions are all implemented as part of one file and the testcase is not necessarily linked with a start file (depending on the target). What happens is, that the first frame is printed normaly and following frames are treated as being inside the enttry file then. This way, only the #0 frame is printed in the backtrace output. */ if (0 && this_frame->type != DUMMY_FRAME && this_frame->level >= 0 && deprecated_inside_entry_file (get_frame_pc (this_frame))) { if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, NULL); fprintf_unfiltered (gdb_stdlog, " // inside entry file }\n"); } return NULL; } /* If any of the old frame initialization methods are around, use the legacy get_prev_frame method. */ if (legacy_frame_p (current_gdbarch)) { prev_frame = legacy_get_prev_frame (this_frame); return prev_frame; } /* Check that this frame's ID was valid. If it wasn't, don't try to unwind to the prev frame. Be careful to not apply this test to the sentinel frame. */ if (this_frame->level >= 0 && !frame_id_p (get_frame_id (this_frame))) { if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, NULL); fprintf_unfiltered (gdb_stdlog, " // this ID is NULL }\n"); } return NULL; } /* Check that this frame's ID isn't inner to (younger, below, next) the next frame. This happens when a frame unwind goes backwards. Since the sentinel frame doesn't really exist, don't compare the inner-most against that sentinel. */ if (this_frame->level > 0 && frame_id_inner (get_frame_id (this_frame), get_frame_id (this_frame->next))) error ("Previous frame inner to this frame (corrupt stack?)"); /* Check that this and the next frame are not identical. If they are, there is most likely a stack cycle. As with the inner-than test above, avoid comparing the inner-most and sentinel frames. */ if (this_frame->level > 0 && frame_id_eq (get_frame_id (this_frame), get_frame_id (this_frame->next))) error ("Previous frame identical to this frame (corrupt stack?)"); /* Allocate the new frame but do not wire it in to the frame chain. Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along frame->next to pull some fancy tricks (of course such code is, by definition, recursive). Try to prevent it. There is no reason to worry about memory leaks, should the remainder of the function fail. The allocated memory will be quickly reclaimed when the frame cache is flushed, and the `we've been here before' check above will stop repeated memory allocation calls. */ prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info); prev_frame->level = this_frame->level + 1; /* Don't yet compute ->unwind (and hence ->type). It is computed on-demand in get_frame_type, frame_register_unwind, and get_frame_id. */ /* Don't yet compute the frame's ID. It is computed on-demand by get_frame_id(). */ /* The unwound frame ID is validate at the start of this function, as part of the logic to decide if that frame should be further unwound, and not here while the prev frame is being created. Doing this makes it possible for the user to examine a frame that has an invalid frame ID. Some very old VAX code noted: [...] For the sake of argument, suppose that the stack is somewhat trashed (which is one reason that "info frame" exists). So, return 0 (indicating we don't know the address of the arglist) if we don't know what frame this frame calls. */ /* Link it in. */ this_frame->prev = prev_frame; prev_frame->next = this_frame; if (frame_debug) { fprintf_unfiltered (gdb_stdlog, "-> "); fprint_frame (gdb_stdlog, prev_frame); fprintf_unfiltered (gdb_stdlog, " }\n"); } return prev_frame; } CORE_ADDR get_frame_pc (struct frame_info *frame) { gdb_assert (frame->next != NULL); return frame_pc_unwind (frame->next); } /* Return an address of that falls within the frame's code block. */ CORE_ADDR frame_unwind_address_in_block (struct frame_info *next_frame) { /* A draft address. */ CORE_ADDR pc = frame_pc_unwind (next_frame); + + if ((frame_tdep_pc_fixup != NULL) && (frame_tdep_pc_fixup(&pc) == 0)) + return pc; /* If THIS frame is not inner most (i.e., NEXT isn't the sentinel), and NEXT is `normal' (i.e., not a sigtramp, dummy, ....) THIS frame's PC ends up pointing at the instruction fallowing the "call". Adjust that PC value so that it falls on the call instruction (which, hopefully, falls within THIS frame's code block. So far it's proved to be a very good approximation. See get_frame_type for why ->type can't be used. */ if (next_frame->level >= 0 && get_frame_type (next_frame) == NORMAL_FRAME) --pc; return pc; } CORE_ADDR get_frame_address_in_block (struct frame_info *this_frame) { return frame_unwind_address_in_block (this_frame->next); } static int pc_notcurrent (struct frame_info *frame) { /* If FRAME is not the innermost frame, that normally means that FRAME->pc points at the return instruction (which is *after* the call instruction), and we want to get the line containing the call (because the call is where the user thinks the program is). However, if the next frame is either a SIGTRAMP_FRAME or a DUMMY_FRAME, then the next frame will contain a saved interrupt PC and such a PC indicates the current (rather than next) instruction/line, consequently, for such cases, want to get the line containing fi->pc. */ struct frame_info *next = get_next_frame (frame); int notcurrent = (next != NULL && get_frame_type (next) == NORMAL_FRAME); return notcurrent; } void find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal) { (*sal) = find_pc_line (get_frame_pc (frame), pc_notcurrent (frame)); } /* Per "frame.h", return the ``address'' of the frame. Code should really be using get_frame_id(). */ CORE_ADDR get_frame_base (struct frame_info *fi) { return get_frame_id (fi).stack_addr; } /* High-level offsets into the frame. Used by the debug info. */ CORE_ADDR get_frame_base_address (struct frame_info *fi) { if (get_frame_type (fi) != NORMAL_FRAME) return 0; if (fi->base == NULL) fi->base = frame_base_find_by_frame (fi->next); /* Sneaky: If the low-level unwind and high-level base code share a common unwinder, let them share the prologue cache. */ if (fi->base->unwind == fi->unwind) return fi->base->this_base (fi->next, &fi->prologue_cache); return fi->base->this_base (fi->next, &fi->base_cache); } CORE_ADDR get_frame_locals_address (struct frame_info *fi) { void **cache; if (get_frame_type (fi) != NORMAL_FRAME) return 0; /* If there isn't a frame address method, find it. */ if (fi->base == NULL) fi->base = frame_base_find_by_frame (fi->next); /* Sneaky: If the low-level unwind and high-level base code share a common unwinder, let them share the prologue cache. */ if (fi->base->unwind == fi->unwind) cache = &fi->prologue_cache; else cache = &fi->base_cache; return fi->base->this_locals (fi->next, cache); } CORE_ADDR get_frame_args_address (struct frame_info *fi) { void **cache; if (get_frame_type (fi) != NORMAL_FRAME) return 0; /* If there isn't a frame address method, find it. */ if (fi->base == NULL) fi->base = frame_base_find_by_frame (fi->next); /* Sneaky: If the low-level unwind and high-level base code share a common unwinder, let them share the prologue cache. */ if (fi->base->unwind == fi->unwind) cache = &fi->prologue_cache; else cache = &fi->base_cache; return fi->base->this_args (fi->next, cache); } /* Level of the selected frame: 0 for innermost, 1 for its caller, ... or -1 for a NULL frame. */ int frame_relative_level (struct frame_info *fi) { if (fi == NULL) return -1; else return fi->level; } enum frame_type get_frame_type (struct frame_info *frame) { /* Some targets still don't use [generic] dummy frames. Catch them here. */ if (!DEPRECATED_USE_GENERIC_DUMMY_FRAMES && deprecated_frame_in_dummy (frame)) return DUMMY_FRAME; /* Some legacy code, e.g, mips_init_extra_frame_info() wants to determine the frame's type prior to it being completely initialized. Don't attempt to lazily initialize ->unwind for legacy code. It will be initialized in legacy_get_prev_frame(). */ if (frame->unwind == NULL && !legacy_frame_p (current_gdbarch)) { /* Initialize the frame's unwinder because it is that which provides the frame's type. */ frame->unwind = frame_unwind_find_by_frame (frame->next); /* FIXME: cagney/2003-04-02: Rather than storing the frame's type in the frame, the unwinder's type should be returned directly. Unfortunately, legacy code, called by legacy_get_prev_frame, explicitly set the frames type using the method deprecated_set_frame_type(). */ frame->type = frame->unwind->type; } if (frame->type == UNKNOWN_FRAME) return NORMAL_FRAME; else return frame->type; } void deprecated_set_frame_type (struct frame_info *frame, enum frame_type type) { /* Arrrg! See comment in "frame.h". */ frame->type = type; } struct frame_extra_info * get_frame_extra_info (struct frame_info *fi) { return fi->extra_info; } struct frame_extra_info * frame_extra_info_zalloc (struct frame_info *fi, long size) { fi->extra_info = frame_obstack_zalloc (size); return fi->extra_info; } void deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc) { if (frame_debug) fprintf_unfiltered (gdb_stdlog, "{ deprecated_update_frame_pc_hack (frame=%d,pc=0x%s) }\n", frame->level, paddr_nz (pc)); /* NOTE: cagney/2003-03-11: Some architectures (e.g., Arm) are maintaining a locally allocated frame object. Since such frame's are not in the frame chain, it isn't possible to assume that the frame has a next. Sigh. */ if (frame->next != NULL) { /* While we're at it, update this frame's cached PC value, found in the next frame. Oh for the day when "struct frame_info" is opaque and this hack on hack can just go away. */ frame->next->prev_pc.value = pc; frame->next->prev_pc.p = 1; } } void deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base) { if (frame_debug) fprintf_unfiltered (gdb_stdlog, "{ deprecated_update_frame_base_hack (frame=%d,base=0x%s) }\n", frame->level, paddr_nz (base)); /* See comment in "frame.h". */ frame->this_id.value.stack_addr = base; } struct frame_info * deprecated_frame_xmalloc_with_cleanup (long sizeof_saved_regs, long sizeof_extra_info) { struct frame_info *frame = XMALLOC (struct frame_info); memset (frame, 0, sizeof (*frame)); frame->this_id.p = 1; make_cleanup (xfree, frame); if (sizeof_saved_regs > 0) { frame->saved_regs = xcalloc (1, sizeof_saved_regs); make_cleanup (xfree, frame->saved_regs); } if (sizeof_extra_info > 0) { frame->extra_info = xcalloc (1, sizeof_extra_info); make_cleanup (xfree, frame->extra_info); } return frame; } /* Memory access methods. */ void get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr, void *buf, int len) { read_memory (addr, buf, len); } LONGEST get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr, int len) { return read_memory_integer (addr, len); } ULONGEST get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr, int len) { return read_memory_unsigned_integer (addr, len); } /* Architecture method. */ struct gdbarch * get_frame_arch (struct frame_info *this_frame) { return current_gdbarch; } /* Stack pointer methods. */ CORE_ADDR get_frame_sp (struct frame_info *this_frame) { return frame_sp_unwind (this_frame->next); } CORE_ADDR frame_sp_unwind (struct frame_info *next_frame) { /* Normality, an architecture that provides a way of obtaining any frame inner-most address. */ if (gdbarch_unwind_sp_p (current_gdbarch)) return gdbarch_unwind_sp (current_gdbarch, next_frame); /* Things are looking grim. If it's the inner-most frame and there is a TARGET_READ_SP then that can be used. */ if (next_frame->level < 0 && TARGET_READ_SP_P ()) return TARGET_READ_SP (); /* Now things are really are grim. Hope that the value returned by the SP_REGNUM register is meaningful. */ if (SP_REGNUM >= 0) { ULONGEST sp; frame_unwind_unsigned_register (next_frame, SP_REGNUM, &sp); return sp; } internal_error (__FILE__, __LINE__, "Missing unwind SP method"); } int legacy_frame_p (struct gdbarch *current_gdbarch) { if (DEPRECATED_INIT_FRAME_PC_P () || DEPRECATED_INIT_FRAME_PC_FIRST_P () || DEPRECATED_INIT_EXTRA_FRAME_INFO_P () || DEPRECATED_FRAME_CHAIN_P ()) /* No question, it's a legacy frame. */ return 1; if (gdbarch_unwind_dummy_id_p (current_gdbarch)) /* No question, it's not a legacy frame (provided none of the deprecated methods checked above are present that is). */ return 0; if (DEPRECATED_TARGET_READ_FP_P () || DEPRECATED_FP_REGNUM >= 0) /* Assume it's legacy. If you're trying to convert a legacy frame target to the new mechanism, get rid of these. legacy get_prev_frame requires these when unwind_frame_id isn't available. */ return 1; /* Default to assuming that it's brand new code, and hence not legacy. Force it down the non-legacy path so that the new code uses the new frame mechanism from day one. Dummy frame's won't work very well but we can live with that. */ return 0; } extern initialize_file_ftype _initialize_frame; /* -Wmissing-prototypes */ static struct cmd_list_element *set_backtrace_cmdlist; static struct cmd_list_element *show_backtrace_cmdlist; static void set_backtrace_cmd (char *args, int from_tty) { help_list (set_backtrace_cmdlist, "set backtrace ", -1, gdb_stdout); } static void show_backtrace_cmd (char *args, int from_tty) { cmd_show_list (show_backtrace_cmdlist, from_tty, ""); } void _initialize_frame (void) { obstack_init (&frame_cache_obstack); add_prefix_cmd ("backtrace", class_maintenance, set_backtrace_cmd, "\ Set backtrace specific variables.\n\ Configure backtrace variables such as the backtrace limit", &set_backtrace_cmdlist, "set backtrace ", 0/*allow-unknown*/, &setlist); add_prefix_cmd ("backtrace", class_maintenance, show_backtrace_cmd, "\ Show backtrace specific variables\n\ Show backtrace variables such as the backtrace limit", &show_backtrace_cmdlist, "show backtrace ", 0/*allow-unknown*/, &showlist); add_setshow_boolean_cmd ("past-main", class_obscure, &backtrace_past_main, "\ Set whether backtraces should continue past \"main\".\n\ Normally the caller of \"main\" is not of interest, so GDB will terminate\n\ the backtrace at \"main\". Set this variable if you need to see the rest\n\ of the stack trace.", "\ Show whether backtraces should continue past \"main\".\n\ Normally the caller of \"main\" is not of interest, so GDB will terminate\n\ the backtrace at \"main\". Set this variable if you need to see the rest\n\ of the stack trace.", NULL, NULL, &set_backtrace_cmdlist, &show_backtrace_cmdlist); add_setshow_uinteger_cmd ("limit", class_obscure, &backtrace_limit, "\ Set an upper bound on the number of backtrace levels.\n\ No more than the specified number of frames can be displayed or examined.\n\ Zero is unlimited.", "\ Show the upper bound on the number of backtrace levels.", NULL, NULL, &set_backtrace_cmdlist, &show_backtrace_cmdlist); /* Debug this files internals. */ add_show_from_set (add_set_cmd ("frame", class_maintenance, var_zinteger, &frame_debug, "Set frame debugging.\n\ When non-zero, frame specific internal debugging is enabled.", &setdebuglist), &showdebuglist); } Index: head/contrib/gdb/gdb/frame.h =================================================================== --- head/contrib/gdb/gdb/frame.h (revision 298357) +++ head/contrib/gdb/gdb/frame.h (revision 298358) @@ -1,705 +1,707 @@ /* Definitions for dealing with stack frames, for GDB, the GNU debugger. Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GDB. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #if !defined (FRAME_H) #define FRAME_H 1 /* The following is the intended naming schema for frame functions. It isn't 100% consistent, but it is aproaching that. Frame naming schema: Prefixes: get_frame_WHAT...(): Get WHAT from the THIS frame (functionaly equivalent to THIS->next->unwind->what) frame_unwind_WHAT...(): Unwind THIS frame's WHAT from the NEXT frame. put_frame_WHAT...(): Put a value into this frame (unsafe, need to invalidate the frame / regcache afterwards) (better name more strongly hinting at its unsafeness) safe_....(): Safer version of various functions, doesn't throw an error (leave this for later?). Returns non-zero if the fetch succeeds. Return a freshly allocated error message? Suffixes: void /frame/_WHAT(): Read WHAT's value into the buffer parameter. ULONGEST /frame/_WHAT_unsigned(): Return an unsigned value (the alternative is *frame_unsigned_WHAT). LONGEST /frame/_WHAT_signed(): Return WHAT signed value. What: /frame/_memory* (frame, coreaddr, len [, buf]): Extract/return *memory. /frame/_register* (frame, regnum [, buf]): extract/return register. CORE_ADDR /frame/_{pc,sp,...} (frame): Resume address, innner most stack *address, ... */ struct symtab_and_line; struct frame_unwind; struct frame_base; struct block; struct gdbarch; struct ui_file; /* A legacy unwinder to prop up architectures using the old style saved regs array. */ extern const struct frame_unwind *legacy_saved_regs_unwind; /* The frame object. */ struct frame_info; /* The frame object's ID. This provides a per-frame unique identifier that can be used to relocate a `struct frame_info' after a target resume or a frame cache destruct. It of course assumes that the inferior hasn't unwound the stack past that frame. */ struct frame_id { /* The frame's stack address. This shall be constant through out the lifetime of a frame. Note that this requirement applies to not just the function body, but also the prologue and (in theory at least) the epilogue. Since that value needs to fall either on the boundary, or within the frame's address range, the frame's outer-most address (the inner-most address of the previous frame) is used. Watch out for all the legacy targets that still use the function pointer register or stack pointer register. They are wrong. */ CORE_ADDR stack_addr; /* The frame's code address. This shall be constant through out the lifetime of the frame. While the PC (a.k.a. resume address) changes as the function is executed, this code address cannot. Typically, it is set to the address of the entry point of the frame's function (as returned by frame_func_unwind(). */ CORE_ADDR code_addr; /* The frame's special address. This shall be constant through out the lifetime of the frame. This is used for architectures that may have frames that do not change the stack but are still distinct and have some form of distinct identifier (e.g. the ia64 which uses a 2nd stack for registers). This field is treated as unordered - i.e. will not be used in frame ordering comparisons such as frame_id_inner(). A zero in this field will be treated as a wild-card when comparing frames for equality. */ CORE_ADDR special_addr; }; /* Methods for constructing and comparing Frame IDs. NOTE: Given stackless functions A and B, where A calls B (and hence B is inner-to A). The relationships: !eq(A,B); !eq(B,A); !inner(A,B); !inner(B,A); all hold. This is because, while B is inner-to A, B is not strictly inner-to A. Being stackless, they have an identical .stack_addr value, and differ only by their unordered .code_addr and/or .special_addr values. Because frame_id_inner is only used as a safety net (e.g., detect a corrupt stack) the lack of strictness is not a problem. Code needing to determine an exact relationship between two frames must instead use frame_id_eq and frame_id_unwind. For instance, in the above, to determine that A stepped-into B, the equation "A.id != B.id && A.id == id_unwind (B)" can be used. */ /* For convenience. All fields are zero. */ extern const struct frame_id null_frame_id; /* Construct a frame ID. The first parameter is the frame's constant stack address (typically the outer-bound), and the second the frame's constant code address (typically the entry point) (or zero, to indicate a wild card). The special identifier address is defaulted to zero. */ extern struct frame_id frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr); /* Construct a special frame ID. The first parameter is the frame's constant stack address (typically the outer-bound), the second is the frame's constant code address (typically the entry point) (or zero, to indicate a wild card), and the third parameter is the frame's special identifier address (or zero to indicate a wild card or unused default). */ extern struct frame_id frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr, CORE_ADDR special_addr); /* Returns non-zero when L is a valid frame (a valid frame has a non-zero .base). */ extern int frame_id_p (struct frame_id l); /* Returns non-zero when L and R identify the same frame, or, if either L or R have a zero .func, then the same frame base. */ extern int frame_id_eq (struct frame_id l, struct frame_id r); /* Returns non-zero when L is strictly inner-than R (they have different frame .bases). Neither L, nor R can be `null'. See note above about frameless functions. */ extern int frame_id_inner (struct frame_id l, struct frame_id r); /* Write the internal representation of a frame ID on the specified stream. */ extern void fprint_frame_id (struct ui_file *file, struct frame_id id); /* For every stopped thread, GDB tracks two frames: current and selected. Current frame is the inner most frame of the selected thread. Selected frame is the one being examined by the the GDB CLI (selected using `up', `down', ...). The frames are created on-demand (via get_prev_frame()) and then held in a frame cache. */ /* FIXME: cagney/2002-11-28: Er, there is a lie here. If you do the sequence: `thread 1; up; thread 2; thread 1' you loose thread 1's selected frame. At present GDB only tracks the selected frame of the current thread. But be warned, that might change. */ /* FIXME: cagney/2002-11-14: At any time, only one thread's selected and current frame can be active. Switching threads causes gdb to discard all that cached frame information. Ulgh! Instead, current and selected frame should be bound to a thread. */ /* On demand, create the inner most frame using information found in the inferior. If the inner most frame can't be created, throw an error. */ extern struct frame_info *get_current_frame (void); /* Invalidates the frame cache (this function should have been called invalidate_cached_frames). FIXME: cagney/2002-11-28: The only difference between flush_cached_frames() and reinit_frame_cache() is that the latter explicitly sets the selected frame back to the current frame there isn't any real difference (except that one delays the selection of a new frame). Code can instead simply rely on get_selected_frame() to reinit's the selected frame as needed. As for invalidating the cache, there should be two methods one that reverts the thread's selected frame back to current frame (for when the inferior resumes) and one that does not (for when the user modifies the target invalidating the frame cache). */ extern void flush_cached_frames (void); extern void reinit_frame_cache (void); /* On demand, create the selected frame and then return it. If the selected frame can not be created, this function throws an error. */ /* FIXME: cagney/2002-11-28: At present, when there is no selected frame, this function always returns the current (inner most) frame. It should instead, when a thread has previously had its frame selected (but not resumed) and the frame cache invalidated, find and then return that thread's previously selected frame. */ extern struct frame_info *get_selected_frame (void); /* Select a specific frame. NULL, apparently implies re-select the inner most frame. */ extern void select_frame (struct frame_info *); /* Given a FRAME, return the next (more inner, younger) or previous (more outer, older) frame. */ extern struct frame_info *get_prev_frame (struct frame_info *); extern struct frame_info *get_next_frame (struct frame_info *); /* Given a frame's ID, relocate the frame. Returns NULL if the frame is not found. */ extern struct frame_info *frame_find_by_id (struct frame_id id); /* Base attributes of a frame: */ /* The frame's `resume' address. Where the program will resume in this frame. This replaced: frame->pc; */ extern CORE_ADDR get_frame_pc (struct frame_info *); /* An address (not necessarily alligned to an instruction boundary) that falls within THIS frame's code block. When a function call is the last statement in a block, the return address for the call may land at the start of the next block. Similarly, if a no-return function call is the last statement in the function, the return address may end up pointing beyond the function, and possibly at the start of the next function. These methods make an allowance for this. For call frames, this function returns the frame's PC-1 which "should" be an address in the frame's block. */ extern CORE_ADDR get_frame_address_in_block (struct frame_info *this_frame); extern CORE_ADDR frame_unwind_address_in_block (struct frame_info *next_frame); /* The frame's inner-most bound. AKA the stack-pointer. Confusingly known as top-of-stack. */ extern CORE_ADDR get_frame_sp (struct frame_info *); extern CORE_ADDR frame_sp_unwind (struct frame_info *); /* Following on from the `resume' address. Return the entry point address of the function containing that resume address, or zero if that function isn't known. */ extern CORE_ADDR frame_func_unwind (struct frame_info *fi); extern CORE_ADDR get_frame_func (struct frame_info *fi); /* Closely related to the resume address, various symbol table attributes that are determined by the PC. Note that for a normal frame, the PC refers to the resume address after the return, and not the call instruction. In such a case, the address is adjusted so that it (approximatly) identifies the call site (and not return site). NOTE: cagney/2002-11-28: The frame cache could be used to cache the computed value. Working on the assumption that the bottle-neck is in the single step code, and that code causes the frame cache to be constantly flushed, caching things in a frame is probably of little benefit. As they say `show us the numbers'. NOTE: cagney/2002-11-28: Plenty more where this one came from: find_frame_block(), find_frame_partial_function(), find_frame_symtab(), find_frame_function(). Each will need to be carefully considered to determine if the real intent was for it to apply to the PC or the adjusted PC. */ extern void find_frame_sal (struct frame_info *frame, struct symtab_and_line *sal); /* Return the frame base (what ever that is) (DEPRECATED). Old code was trying to use this single method for two conflicting purposes. Such code needs to be updated to use either of: get_frame_id: A low level frame unique identifier, that consists of both a stack and a function address, that can be used to uniquely identify a frame. This value is determined by the frame's low-level unwinder, the stack part [typically] being the top-of-stack of the previous frame, and the function part being the function's start address. Since the correct identification of a frameless function requires both the a stack and function address, the old get_frame_base method was not sufficient. get_frame_base_address: get_frame_locals_address: get_frame_args_address: A set of high-level debug-info dependant addresses that fall within the frame. These addresses almost certainly will not match the stack address part of a frame ID (as returned by get_frame_base). This replaced: frame->frame; */ extern CORE_ADDR get_frame_base (struct frame_info *); /* Return the per-frame unique identifer. Can be used to relocate a frame after a frame cache flush (and other similar operations). If FI is NULL, return the null_frame_id. */ extern struct frame_id get_frame_id (struct frame_info *fi); /* Assuming that a frame is `normal', return its base-address, or 0 if the information isn't available. NOTE: This address is really only meaningful to the frame's high-level debug info. */ extern CORE_ADDR get_frame_base_address (struct frame_info *); /* Assuming that a frame is `normal', return the base-address of the local variables, or 0 if the information isn't available. NOTE: This address is really only meaningful to the frame's high-level debug info. Typically, the argument and locals share a single base-address. */ extern CORE_ADDR get_frame_locals_address (struct frame_info *); /* Assuming that a frame is `normal', return the base-address of the parameter list, or 0 if that information isn't available. NOTE: This address is really only meaningful to the frame's high-level debug info. Typically, the argument and locals share a single base-address. */ extern CORE_ADDR get_frame_args_address (struct frame_info *); /* The frame's level: 0 for innermost, 1 for its caller, ...; or -1 for an invalid frame). */ extern int frame_relative_level (struct frame_info *fi); /* Return the frame's type. Some are real, some are signal trampolines, and some are completely artificial (dummy). */ enum frame_type { /* The frame's type hasn't yet been defined. This is a catch-all for legacy code that uses really strange technicques, such as deprecated_set_frame_type, to set the frame's type. New code should not use this value. */ UNKNOWN_FRAME, /* A true stack frame, created by the target program during normal execution. */ NORMAL_FRAME, /* A fake frame, created by GDB when performing an inferior function call. */ DUMMY_FRAME, /* In a signal handler, various OSs handle this in various ways. The main thing is that the frame may be far from normal. */ SIGTRAMP_FRAME }; extern enum frame_type get_frame_type (struct frame_info *); /* FIXME: cagney/2002-11-10: Some targets want to directly mark a frame as being of a specific type. This shouldn't be necessary. PC_IN_SIGTRAMP() indicates a SIGTRAMP_FRAME and DEPRECATED_PC_IN_CALL_DUMMY() indicates a DUMMY_FRAME. I suspect the real problem here is that get_prev_frame() only sets initialized after DEPRECATED_INIT_EXTRA_FRAME_INFO as been called. Consequently, some targets found that the frame's type was wrong and tried to fix it. The correct fix is to modify get_prev_frame() so that it initializes the frame's type before calling any other functions. */ extern void deprecated_set_frame_type (struct frame_info *, enum frame_type type); /* Unwind the stack frame so that the value of REGNUM, in the previous (up, older) frame is returned. If VALUEP is NULL, don't fetch/compute the value. Instead just return the location of the value. */ extern void frame_register_unwind (struct frame_info *frame, int regnum, int *optimizedp, enum lval_type *lvalp, CORE_ADDR *addrp, int *realnump, void *valuep); /* Fetch a register from this, or unwind a register from the next frame. Note that the get_frame methods are wrappers to frame->next->unwind. They all [potentially] throw an error if the fetch fails. */ extern void frame_unwind_register (struct frame_info *frame, int regnum, void *buf); extern void get_frame_register (struct frame_info *frame, int regnum, void *buf); extern LONGEST frame_unwind_register_signed (struct frame_info *frame, int regnum); extern LONGEST get_frame_register_signed (struct frame_info *frame, int regnum); extern ULONGEST frame_unwind_register_unsigned (struct frame_info *frame, int regnum); extern ULONGEST get_frame_register_unsigned (struct frame_info *frame, int regnum); /* Use frame_unwind_register_signed. */ extern void frame_unwind_unsigned_register (struct frame_info *frame, int regnum, ULONGEST *val); /* Get the value of the register that belongs to this FRAME. This function is a wrapper to the call sequence ``frame_unwind_register (get_next_frame (FRAME))''. As per frame_register_unwind(), if VALUEP is NULL, the registers value is not fetched/computed. */ extern void frame_register (struct frame_info *frame, int regnum, int *optimizedp, enum lval_type *lvalp, CORE_ADDR *addrp, int *realnump, void *valuep); /* The reverse. Store a register value relative to the specified frame. Note: this call makes the frame's state undefined. The register and frame caches must be flushed. */ extern void put_frame_register (struct frame_info *frame, int regnum, const void *buf); /* Map between a frame register number and its name. A frame register space is a superset of the cooked register space --- it also includes builtin registers. If NAMELEN is negative, use the NAME's length when doing the comparison. */ extern int frame_map_name_to_regnum (struct frame_info *frame, const char *name, int namelen); extern const char *frame_map_regnum_to_name (struct frame_info *frame, int regnum); /* Unwind the PC. Strictly speaking return the resume address of the calling frame. For GDB, `pc' is the resume address and not a specific register. */ extern CORE_ADDR frame_pc_unwind (struct frame_info *frame); /* Discard the specified frame. Restoring the registers to the state of the caller. */ extern void frame_pop (struct frame_info *frame); /* Return memory from the specified frame. A frame knows its thread / LWP and hence can find its way down to a target. The assumption here is that the current and previous frame share a common address space. If the memory read fails, these methods throw an error. NOTE: cagney/2003-06-03: Should there be unwind versions of these methods? That isn't clear. Can code, for instance, assume that this and the previous frame's memory or architecture are identical? If architecture / memory changes are always separated by special adaptor frames this should be ok. */ extern void get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr, void *buf, int len); extern LONGEST get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR memaddr, int len); extern ULONGEST get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR memaddr, int len); /* Return this frame's architecture. */ extern struct gdbarch *get_frame_arch (struct frame_info *this_frame); /* Values for the source flag to be used in print_frame_info_base(). */ enum print_what { /* Print only the source line, like in stepi. */ SRC_LINE = -1, /* Print only the location, i.e. level, address (sometimes) function, args, file, line, line num. */ LOCATION, /* Print both of the above. */ SRC_AND_LOC, /* Print location only, but always include the address. */ LOC_AND_ADDRESS }; /* Allocate additional space for appendices to a struct frame_info. NOTE: Much of GDB's code works on the assumption that the allocated saved_regs[] array is the size specified below. If you try to make that array smaller, GDB will happily walk off its end. */ #ifdef SIZEOF_FRAME_SAVED_REGS #error "SIZEOF_FRAME_SAVED_REGS can not be re-defined" #endif #define SIZEOF_FRAME_SAVED_REGS \ (sizeof (CORE_ADDR) * (NUM_REGS+NUM_PSEUDO_REGS)) /* Allocate zero initialized memory from the frame cache obstack. Appendices to the frame info (such as the unwind cache) should allocate memory using this method. */ extern void *frame_obstack_zalloc (unsigned long size); #define FRAME_OBSTACK_ZALLOC(TYPE) ((TYPE *) frame_obstack_zalloc (sizeof (TYPE))) #define FRAME_OBSTACK_CALLOC(NUMBER,TYPE) ((TYPE *) frame_obstack_zalloc ((NUMBER) * sizeof (TYPE))) /* If legacy_frame_chain_valid() returns zero it means that the given frame is the outermost one and has no caller. This method has been superseeded by the per-architecture frame_unwind_pc() (returns 0 to indicate an invalid return address) and per-frame this_id() (returns a NULL frame ID to indicate an invalid frame). */ extern int legacy_frame_chain_valid (CORE_ADDR, struct frame_info *); extern void generic_save_dummy_frame_tos (CORE_ADDR sp); extern struct block *get_frame_block (struct frame_info *, CORE_ADDR *addr_in_block); /* Return the `struct block' that belongs to the selected thread's selected frame. If the inferior has no state, return NULL. NOTE: cagney/2002-11-29: No state? Does the inferior have any execution state (a core file does, an executable does not). At present the code tests `target_has_stack' but I'm left wondering if it should test `target_has_registers' or, even, a merged target_has_state. Should it look at the most recently specified SAL? If the target has no state, should this function try to extract a block from the most recently selected SAL? That way `list foo' would give it some sort of reference point. Then again, perhaphs that would confuse things. Calls to this function can be broken down into two categories: Code that uses the selected block as an additional, but optional, data point; Code that uses the selected block as a prop, when it should have the relevant frame/block/pc explicitly passed in. The latter can be eliminated by correctly parameterizing the code, the former though is more interesting. Per the "address" command, it occures in the CLI code and makes it possible for commands to work, even when the inferior has no state. */ extern struct block *get_selected_block (CORE_ADDR *addr_in_block); extern struct symbol *get_frame_function (struct frame_info *); extern CORE_ADDR get_pc_function_start (CORE_ADDR); extern int legacy_frameless_look_for_prologue (struct frame_info *); extern struct frame_info *find_relative_frame (struct frame_info *, int *); extern void show_and_print_stack_frame (struct frame_info *fi, int level, int source); extern void print_stack_frame (struct frame_info *, int, int); extern void show_stack_frame (struct frame_info *); extern void print_frame_info (struct frame_info *, int, int, int); extern void show_frame_info (struct frame_info *, int, int, int); extern struct frame_info *block_innermost_frame (struct block *); /* NOTE: cagney/2002-09-13: There is no need for this function. */ extern CORE_ADDR deprecated_read_register_dummy (CORE_ADDR pc, CORE_ADDR fp, int); extern void generic_push_dummy_frame (void); extern void generic_pop_current_frame (void (*)(struct frame_info *)); extern void generic_pop_dummy_frame (void); extern int generic_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp); /* NOTE: cagney/2002-06-26: Targets should no longer use this function. Instead, the contents of a dummy frames registers can be obtained by applying: frame_register_unwind to the dummy frame; or frame_register_unwind() to the next outer frame. */ extern char *deprecated_generic_find_dummy_frame (CORE_ADDR pc, CORE_ADDR fp); /* The DEPRECATED_GET_SAVED_REGISTER architecture interface is entirely redundant. New architectures should implement per-frame unwinders (ref "frame-unwind.h"). */ extern void deprecated_generic_get_saved_register (char *, int *, CORE_ADDR *, struct frame_info *, int, enum lval_type *); extern void generic_save_call_dummy_addr (CORE_ADDR lo, CORE_ADDR hi); /* FIXME: cagney/2003-02-02: Should be deprecated or replaced with a function called get_frame_register_p(). This slightly weird (and older) variant of get_frame_register() returns zero (indicating the register is unavailable) if either: the register isn't cached; or the register has been optimized out. Problem is, neither check is exactly correct. A register can't be optimized out (it may not have been saved as part of a function call); The fact that a register isn't in the register cache doesn't mean that the register isn't available (it could have been fetched from memory). */ extern int frame_register_read (struct frame_info *frame, int regnum, void *buf); /* From stack.c. */ extern void args_info (char *, int); extern void locals_info (char *, int); extern void (*selected_frame_level_changed_hook) (int); extern void return_command (char *, int); /* NOTE: cagney/2002-11-27: You might think that the below global can simply be replaced by a call to either get_selected_frame() or select_frame(). Unfortunately, it isn't that easy. The relevant code needs to be audited to determine if it is possible (or pratical) to instead pass the applicable frame in as a parameter. For instance, DEPRECATED_DO_REGISTERS_INFO() relied on the deprecated_selected_frame global, while its replacement, PRINT_REGISTERS_INFO(), is parameterized with the selected frame. The only real exceptions occure at the edge (in the CLI code) where user commands need to pick up the selected frame before proceeding. This is important. GDB is trying to stamp out the hack: saved_frame = deprecated_selected_frame; deprecated_selected_frame = ...; hack_using_global_selected_frame (); deprecated_selected_frame = saved_frame; Take care! */ extern struct frame_info *deprecated_selected_frame; /* NOTE: drow/2003-09-06: This function is "a step sideways" for uses of deprecated_selected_frame. They should be fixed as above, but meanwhile, we needed a solution for cases where functions are called with a NULL frame meaning either "the program is not running" or "use the selected frame". Lazy building of deprecated_selected_frame confuses the situation, because now deprecated_selected_frame can be NULL even when the inferior is running. This function calls get_selected_frame if the inferior should have a frame, or returns NULL otherwise. */ extern struct frame_info *deprecated_safe_get_selected_frame (void); /* Create a frame using the specified BASE and PC. */ extern struct frame_info *create_new_frame (CORE_ADDR base, CORE_ADDR pc); /* Create/access the frame's `extra info'. The extra info is used by older code to store information such as the analyzed prologue. The zalloc() should only be called by the INIT_EXTRA_INFO method. */ extern struct frame_extra_info *frame_extra_info_zalloc (struct frame_info *fi, long size); extern struct frame_extra_info *get_frame_extra_info (struct frame_info *fi); /* Create/access the frame's `saved_regs'. The saved regs are used by older code to store the address of each register (except for SP_REGNUM where the value of the register in the previous frame is stored). */ extern CORE_ADDR *frame_saved_regs_zalloc (struct frame_info *); extern CORE_ADDR *deprecated_get_frame_saved_regs (struct frame_info *); /* FIXME: cagney/2002-12-06: Has the PC in the current frame changed? "infrun.c", Thanks to DECR_PC_AFTER_BREAK, can change the PC after the initial frame create. This puts things back in sync. This replaced: frame->pc = ....; */ extern void deprecated_update_frame_pc_hack (struct frame_info *frame, CORE_ADDR pc); /* FIXME: cagney/2002-12-18: Has the frame's base changed? Or to be more exact, was that initial guess at the frame's base as returned by deprecated_read_fp() wrong? If it was, fix it. This shouldn't be necessary since the code should be getting the frame's base correct from the outset. This replaced: frame->frame = ....; */ extern void deprecated_update_frame_base_hack (struct frame_info *frame, CORE_ADDR base); /* FIXME: cagney/2003-01-05: Allocate a frame, along with the saved_regs and extra_info. Set up cleanups for all three. Same as for deprecated_frame_xmalloc, targets are calling this when creating a scratch `struct frame_info'. The frame overhaul makes this unnecessary since all frame queries are parameterized with a common cache parameter and a frame. */ extern struct frame_info *deprecated_frame_xmalloc_with_cleanup (long sizeof_saved_regs, long sizeof_extra_info); /* Return non-zero if the architecture is relying on legacy frame code. */ extern int legacy_frame_p (struct gdbarch *gdbarch); +extern int (*frame_tdep_pc_fixup)(CORE_ADDR *pc); + #endif /* !defined (FRAME_H) */ Index: head/gnu/usr.bin/gdb/kgdb/kgdb.h =================================================================== --- head/gnu/usr.bin/gdb/kgdb/kgdb.h (revision 298357) +++ head/gnu/usr.bin/gdb/kgdb/kgdb.h (revision 298358) @@ -1,78 +1,81 @@ /* * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _KGDB_H_ #define _KGDB_H_ struct thread_info; extern kvm_t *kvm; struct kthr { struct kthr *next; uintptr_t paddr; uintptr_t kaddr; uintptr_t kstack; uintptr_t pcb; int tid; int pid; int cpu; }; extern struct kthr *curkthr; void initialize_kld_target(void); void initialize_kgdb_target(void); void kgdb_dmesg(void); CORE_ADDR kgdb_trgt_core_pcb(u_int); CORE_ADDR kgdb_trgt_stop_pcb(u_int, u_int); void kgdb_trgt_new_objfile(struct objfile *); void kgdb_trgt_fetch_registers(int); void kgdb_trgt_store_registers(int); void kld_init(void); void kld_new_objfile(struct objfile *); frame_unwind_sniffer_ftype kgdb_trgt_trapframe_sniffer; struct kthr *kgdb_thr_first(void); struct kthr *kgdb_thr_init(void); struct kthr *kgdb_thr_lookup_tid(int); struct kthr *kgdb_thr_lookup_pid(int); struct kthr *kgdb_thr_lookup_paddr(uintptr_t); struct kthr *kgdb_thr_lookup_taddr(uintptr_t); struct kthr *kgdb_thr_next(struct kthr *); struct kthr *kgdb_thr_select(struct kthr *); char *kgdb_thr_extra_thread_info(int); CORE_ADDR kgdb_lookup(const char *sym); CORE_ADDR kgdb_parse_1(const char *, int); #define kgdb_parse(exp) kgdb_parse_1((exp), 0) #define kgdb_parse_quiet(exp) kgdb_parse_1((exp), 1) +extern int (*arm_tdep_pc_fixup)(CORE_ADDR *pc); +int kgdb_trgt_pc_fixup(CORE_ADDR *pc); + #endif /* _KGDB_H_ */ Index: head/gnu/usr.bin/gdb/kgdb/main.c =================================================================== --- head/gnu/usr.bin/gdb/kgdb/main.c (revision 298357) +++ head/gnu/usr.bin/gdb/kgdb/main.c (revision 298358) @@ -1,481 +1,483 @@ /* * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* libgdb stuff. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include extern frame_unwind_sniffer_ftype *kgdb_sniffer_kluge; #include "kgdb.h" static int dumpnr; static int quiet; static int verbose; static char crashdir[PATH_MAX]; static char *kernel; static char *remote; static char *vmcore; static struct ui_file *parse_gdberr; static void (*kgdb_new_objfile_chain)(struct objfile * objfile); static void usage(void) { fprintf(stderr, "usage: %s [-afqvw] [-b rate] [-d crashdir] [-c core | -n dumpnr | -r device]\n" "\t[kernel [core]]\n", getprogname()); exit(1); } static void kernel_from_dumpnr(int nr) { char path[PATH_MAX]; FILE *info; char *s; struct stat st; int l; /* * If there's a kernel image right here in the crash directory, then * use it. The kernel image is either called kernel. or is in a * subdirectory kernel. and called kernel. The latter allows us * to collect the modules in the same place. */ snprintf(path, sizeof(path), "%s/kernel.%d", crashdir, nr); if (stat(path, &st) == 0) { if (S_ISREG(st.st_mode)) { kernel = strdup(path); return; } if (S_ISDIR(st.st_mode)) { snprintf(path, sizeof(path), "%s/kernel.%d/kernel", crashdir, nr); if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) { kernel = strdup(path); return; } } } /* * No kernel image here. Parse the dump header. The kernel object * directory can be found there and we probably have the kernel * image still in it. The object directory may also have a kernel * with debugging info (called kernel.debug). If we have a debug * kernel, use it. */ snprintf(path, sizeof(path), "%s/info.%d", crashdir, nr); info = fopen(path, "r"); if (info == NULL) { warn("%s", path); return; } while (fgets(path, sizeof(path), info) != NULL) { l = strlen(path); if (l > 0 && path[l - 1] == '\n') path[--l] = '\0'; if (strncmp(path, " ", 4) == 0) { s = strchr(path, ':'); s = (s == NULL) ? path + 4 : s + 1; l = snprintf(path, sizeof(path), "%s/kernel.debug", s); if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) { path[l - 6] = '\0'; if (stat(path, &st) == -1 || !S_ISREG(st.st_mode)) break; } kernel = strdup(path); break; } } fclose(info); } static void kgdb_new_objfile(struct objfile *objfile) { static int once = 1; kld_new_objfile(objfile); kgdb_trgt_new_objfile(objfile); if (kgdb_new_objfile_chain != NULL) kgdb_new_objfile_chain(objfile); if (once && objfile != NULL && objfile == symfile_objfile) { /* * The initial kernel has just been loaded. Start the * remote target if we have one. */ once = 0; if (remote != NULL) push_remote_target (remote, 0); } } /* * Parse an expression and return its value. If 'quiet' is true, then * any error messages from the parser are masked. */ CORE_ADDR kgdb_parse_1(const char *exp, int quiet) { struct ui_file *old_stderr; struct cleanup *old_chain; struct expression *expr; struct value *val; char *s; CORE_ADDR n; old_stderr = gdb_stderr; if (quiet) gdb_stderr = parse_gdberr; n = 0; s = xstrdup(exp); old_chain = make_cleanup(xfree, s); if (gdb_parse_exp_1(&s, NULL, 0, &expr) && *s == '\0') { make_cleanup(free_current_contents, &expr); if (gdb_evaluate_expression(expr, &val)) n = value_as_address(val); } do_cleanups(old_chain); gdb_stderr = old_stderr; return (n); } #define MSGBUF_SEQ_TO_POS(size, seq) ((seq) % (size)) void kgdb_dmesg(void) { CORE_ADDR bufp; int size, rseq, wseq; char c; /* * Display the unread portion of the message buffer. This gives the * user a some initial data to work from. */ if (quiet) return; bufp = kgdb_parse("msgbufp->msg_ptr"); size = (int)kgdb_parse("msgbufp->msg_size"); if (bufp == 0 || size == 0) return; rseq = (int)kgdb_parse("msgbufp->msg_rseq"); wseq = (int)kgdb_parse("msgbufp->msg_wseq"); rseq = MSGBUF_SEQ_TO_POS(size, rseq); wseq = MSGBUF_SEQ_TO_POS(size, wseq); if (rseq == wseq) return; printf("\nUnread portion of the kernel message buffer:\n"); while (rseq < wseq) { read_memory(bufp + rseq, &c, 1); putchar(c); rseq++; if (rseq == size) rseq = 0; } if (c != '\n') putchar('\n'); putchar('\n'); } static void kgdb_init(char *argv0 __unused) { parse_gdberr = mem_fileopen(); set_prompt("(kgdb) "); initialize_kgdb_target(); initialize_kld_target(); kgdb_new_objfile_chain = target_new_objfile_hook; target_new_objfile_hook = kgdb_new_objfile; } /* * Remote targets can support any number of syntaxes and we want to * support them all with one addition: we support specifying a device * node for a serial device without the "/dev/" prefix. * * What we do is to stat(2) the existing remote target first. If that * fails, we try it with "/dev/" prepended. If that succeeds we use * the resulting path, otherwise we use the original target. If * either stat(2) succeeds make sure the file is either a character * device or a FIFO. */ static void verify_remote(void) { char path[PATH_MAX]; struct stat st; if (stat(remote, &st) != 0) { snprintf(path, sizeof(path), "/dev/%s", remote); if (stat(path, &st) != 0) return; free(remote); remote = strdup(path); } if (!S_ISCHR(st.st_mode) && !S_ISFIFO(st.st_mode)) errx(1, "%s: not a special file, FIFO or socket", remote); } static void add_arg(struct captured_main_args *args, char *arg) { args->argc++; args->argv = reallocf(args->argv, (args->argc + 1) * sizeof(char *)); if (args->argv == NULL) err(1, "Out of memory building argument list"); args->argv[args->argc] = arg; } int main(int argc, char *argv[]) { char path[PATH_MAX]; struct stat st; struct captured_main_args args; char *s; int a, ch; dumpnr = -1; strlcpy(crashdir, "/var/crash", sizeof(crashdir)); s = getenv("KGDB_CRASH_DIR"); if (s != NULL) strlcpy(crashdir, s, sizeof(crashdir)); /* Convert long options into short options. */ for (a = 1; a < argc; a++) { s = argv[a]; if (s[0] == '-') { s++; /* Long options take either 1 or 2 dashes. */ if (s[0] == '-') s++; if (strcmp(s, "quiet") == 0) argv[a] = "-q"; else if (strcmp(s, "fullname") == 0) argv[a] = "-f"; } } quiet = 0; memset (&args, 0, sizeof args); args.use_windows = 0; args.interpreter_p = INTERP_CONSOLE; args.argv = malloc(sizeof(char *)); args.argv[0] = argv[0]; while ((ch = getopt(argc, argv, "ab:c:d:fn:qr:vw")) != -1) { switch (ch) { case 'a': annotation_level++; break; case 'b': { int i; char *p; i = strtol(optarg, &p, 0); if (*p != '\0' || p == optarg) warnx("warning: could not set baud rate to `%s'.\n", optarg); else baud_rate = i; break; } case 'c': /* use given core file. */ if (vmcore != NULL) { warnx("option %c: can only be specified once", optopt); usage(); /* NOTREACHED */ } vmcore = strdup(optarg); break; case 'd': /* lookup dumps in given directory. */ strlcpy(crashdir, optarg, sizeof(crashdir)); break; case 'f': annotation_level = 1; break; case 'n': /* use dump with given number. */ dumpnr = strtol(optarg, &s, 0); if (dumpnr < 0 || *s != '\0') { warnx("option %c: invalid kernel dump number", optopt); usage(); /* NOTREACHED */ } break; case 'q': quiet = 1; add_arg(&args, "-q"); break; case 'r': /* use given device for remote session. */ if (remote != NULL) { warnx("option %c: can only be specified once", optopt); usage(); /* NOTREACHED */ } remote = strdup(optarg); break; case 'v': /* increase verbosity. */ verbose++; break; case 'w': /* core file is writeable. */ add_arg(&args, "--write"); break; case '?': default: usage(); } } if (((vmcore != NULL) ? 1 : 0) + ((dumpnr >= 0) ? 1 : 0) + ((remote != NULL) ? 1 : 0) > 1) { warnx("options -c, -n and -r are mutually exclusive"); usage(); /* NOTREACHED */ } if (verbose > 1) warnx("using %s as the crash directory", crashdir); if (argc > optind) kernel = strdup(argv[optind++]); if (argc > optind && (dumpnr >= 0 || remote != NULL)) { warnx("options -n and -r do not take a core file. Ignored"); optind = argc; } if (dumpnr >= 0) { snprintf(path, sizeof(path), "%s/vmcore.%d", crashdir, dumpnr); if (stat(path, &st) == -1) err(1, "%s", path); if (!S_ISREG(st.st_mode)) errx(1, "%s: not a regular file", path); vmcore = strdup(path); } else if (remote != NULL) { verify_remote(); } else if (argc > optind) { if (vmcore == NULL) vmcore = strdup(argv[optind++]); if (argc > optind) warnx("multiple core files specified. Ignored"); } else if (vmcore == NULL && kernel == NULL) { vmcore = strdup(_PATH_MEM); kernel = strdup(getbootfile()); } if (verbose) { if (vmcore != NULL) warnx("core file: %s", vmcore); if (remote != NULL) warnx("device file: %s", remote); if (kernel != NULL) warnx("kernel image: %s", kernel); } /* A remote target requires an explicit kernel argument. */ if (remote != NULL && kernel == NULL) { warnx("remote debugging requires a kernel"); usage(); /* NOTREACHED */ } /* If we don't have a kernel image yet, try to find one. */ if (kernel == NULL) { if (dumpnr >= 0) kernel_from_dumpnr(dumpnr); if (kernel == NULL) errx(1, "couldn't find a suitable kernel image"); if (verbose) warnx("kernel image: %s", kernel); } add_arg(&args, kernel); if (vmcore != NULL) add_arg(&args, vmcore); /* The libgdb code uses optind too. Reset it... */ optind = 0; /* Terminate argv list. */ add_arg(&args, NULL); init_ui_hook = kgdb_init; - +#if TARGET_CPUARCH == arm + frame_tdep_pc_fixup = kgdb_trgt_pc_fixup; +#endif kgdb_sniffer_kluge = kgdb_trgt_trapframe_sniffer; return (gdb_main(&args)); } Index: head/gnu/usr.bin/gdb/kgdb/trgt_arm.c =================================================================== --- head/gnu/usr.bin/gdb/kgdb/trgt_arm.c (revision 298357) +++ head/gnu/usr.bin/gdb/kgdb/trgt_arm.c (revision 298358) @@ -1,235 +1,286 @@ /* * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #ifndef CROSS_DEBUGGER #include #include #include #endif #include #include #include #include #include #include #include #include #include #include #include "kgdb.h" CORE_ADDR kgdb_trgt_core_pcb(u_int cpuid) { #ifndef CROSS_DEBUGGER return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb))); #else return -1; #endif } void kgdb_trgt_fetch_registers(int regno __unused) { #ifndef CROSS_DEBUGGER struct kthr *kt; struct pcb pcb; int i; kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid)); if (kt == NULL) return; if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) { warnx("kvm_read: %s", kvm_geterr(kvm)); memset(&pcb, 0, sizeof(pcb)); } for (i = ARM_A1_REGNUM + 4; i <= ARM_SP_REGNUM; i++) { supply_register(i, (char *)&pcb.pcb_regs.sf_r4 + (i - (ARM_A1_REGNUM + 4 )) * 4); } supply_register(ARM_PC_REGNUM, (char *)&pcb.pcb_regs.sf_pc); supply_register(ARM_LR_REGNUM, (char *)&pcb.pcb_regs.sf_lr); #endif } void kgdb_trgt_store_registers(int regno __unused) { fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__); } void kgdb_trgt_new_objfile(struct objfile *objfile) { } #ifndef CROSS_DEBUGGER struct kgdb_frame_cache { CORE_ADDR fp; CORE_ADDR sp; + CORE_ADDR pc; }; static int kgdb_trgt_frame_offset[26] = { offsetof(struct trapframe, tf_r0), offsetof(struct trapframe, tf_r1), offsetof(struct trapframe, tf_r2), offsetof(struct trapframe, tf_r3), offsetof(struct trapframe, tf_r4), offsetof(struct trapframe, tf_r5), offsetof(struct trapframe, tf_r6), offsetof(struct trapframe, tf_r7), offsetof(struct trapframe, tf_r8), offsetof(struct trapframe, tf_r9), offsetof(struct trapframe, tf_r10), offsetof(struct trapframe, tf_r11), offsetof(struct trapframe, tf_r12), offsetof(struct trapframe, tf_svc_sp), offsetof(struct trapframe, tf_svc_lr), offsetof(struct trapframe, tf_pc), -1, -1, -1, -1, -1, -1, -1, -1, -1, offsetof(struct trapframe, tf_spsr) }; static struct kgdb_frame_cache * kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache) { char buf[MAX_REGISTER_SIZE]; struct kgdb_frame_cache *cache; cache = *this_cache; if (cache == NULL) { cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache); *this_cache = cache; frame_unwind_register(next_frame, ARM_SP_REGNUM, buf); cache->sp = extract_unsigned_integer(buf, register_size(current_gdbarch, ARM_SP_REGNUM)); frame_unwind_register(next_frame, ARM_FP_REGNUM, buf); cache->fp = extract_unsigned_integer(buf, register_size(current_gdbarch, ARM_FP_REGNUM)); + cache->pc = frame_func_unwind(next_frame); } return (cache); } static int is_undef; static void kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache, struct frame_id *this_id) { struct kgdb_frame_cache *cache; cache = kgdb_trgt_frame_cache(next_frame, this_cache); - *this_id = frame_id_build(cache->fp, 0); + *this_id = frame_id_build(cache->sp, cache->pc); } static void kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame, void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp, CORE_ADDR *addrp, int *realnump, void *valuep) { char dummy_valuep[MAX_REGISTER_SIZE]; struct kgdb_frame_cache *cache; int ofs, regsz; - int is_undefined = 0; + CORE_ADDR sp; regsz = register_size(current_gdbarch, regnum); if (valuep == NULL) valuep = dummy_valuep; memset(valuep, 0, regsz); *optimizedp = 0; *addrp = 0; *lvalp = not_lval; *realnump = -1; ofs = (regnum >= 0 && regnum <= ARM_PS_REGNUM) ? kgdb_trgt_frame_offset[regnum] : -1; if (ofs == -1) return; cache = kgdb_trgt_frame_cache(next_frame, this_cache); + sp = cache->sp; - if (is_undef && (regnum == ARM_SP_REGNUM || regnum == ARM_PC_REGNUM)) { - *addrp = cache->sp + offsetof(struct trapframe, tf_spsr); - target_read_memory(*addrp, valuep, regsz); - is_undefined = 1; - ofs = kgdb_trgt_frame_offset[ARM_SP_REGNUM]; - - } - *addrp = cache->sp + ofs; + ofs = kgdb_trgt_frame_offset[regnum]; + *addrp = sp + ofs; *lvalp = lval_memory; target_read_memory(*addrp, valuep, regsz); - - if (is_undefined) { - *addrp = *(unsigned int *)valuep + (regnum == ARM_SP_REGNUM ? - 0 : 8); - target_read_memory(*addrp, valuep, regsz); - - } } static const struct frame_unwind kgdb_trgt_trapframe_unwind = { UNKNOWN_FRAME, &kgdb_trgt_trapframe_this_id, &kgdb_trgt_trapframe_prev_register }; #endif const struct frame_unwind * kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame) { #ifndef CROSS_DEBUGGER char *pname; CORE_ADDR pc; pc = frame_pc_unwind(next_frame); pname = NULL; find_pc_partial_function(pc, &pname, NULL, NULL); if (pname == NULL) { is_undef = 0; return (NULL); } if (!strcmp(pname, "undefinedinstruction")) is_undef = 1; if (strcmp(pname, "Laddress_exception_entry") == 0 || strcmp(pname, "undefined_entry") == 0 || strcmp(pname, "exception_exit") == 0 || strcmp(pname, "Laddress_exception_msg") == 0 || strcmp(pname, "irq_entry") == 0) return (&kgdb_trgt_trapframe_unwind); if (!strcmp(pname, "undefinedinstruction")) is_undef = 1; else is_undef = 0; #endif return (NULL); +} + +/* + * This function ensures, that the PC is inside the + * function section which is understood by GDB. + * + * Return 0 when fixup is necessary, -1 otherwise. + */ +int +kgdb_trgt_pc_fixup(CORE_ADDR *pc) +{ +#ifndef CROSS_DEBUGGER + struct minimal_symbol *msymbol; + int valpc; + + /* + * exception_exit and swi_exit are special. These functions + * are artificially injected into the stack to be executed + * as the last entry in calling chain when all functions exit. + * Treat them differently. + */ + msymbol = lookup_minimal_symbol_by_pc(*pc); + if (msymbol != NULL) { + if (strcmp(DEPRECATED_SYMBOL_NAME(msymbol), "exception_exit") == 0) + return (0); + if (strcmp(DEPRECATED_SYMBOL_NAME(msymbol), "swi_exit") == 0) + return (0); + } + + /* + * kdb_enter contains an invalid instruction which is supposed + * to generate a trap. BFD does not understand it and treats + * this part of function as a separate function. Move PC + * two instruction earlier to be inside kdb_enter section. + */ + target_read_memory(*pc - 4, (char*)&valpc, 4); + if (valpc == 0xe7ffffff) { + *pc = *pc - 8; + return (0); + } + + /* + * When the panic/vpanic is the last (noreturn) function, + * the bottom of the calling function looks as below. + * mov lr, pc + * b panic + * Normally, GDB is not able to detect function boundaries, + * so move the PC two instruction earlier where it can deal + * with it. + * Match this pair of instructions: mov lr, pc followed with + * non-linked branch. + */ + if ((valpc & 0xff000000) == 0xea000000) { + target_read_memory(*pc - 8, (char*)&valpc, 4); + if (valpc == 0xe1a0e00f) { + *pc -= 8; + return (0); + } + } +#endif + return (-1); }