📄 rs6000-tdep.c
字号:
/* Target-dependent code for GDB, the GNU debugger. Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.This file is part of GDB.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "defs.h"#include "frame.h"#include "inferior.h"#include "symtab.h"#include "target.h"#include <sys/param.h>#include <sys/dir.h>#include <sys/user.h>#include <signal.h>#include <sys/ioctl.h>#include <fcntl.h>#include <a.out.h>#include <sys/file.h>#include <sys/stat.h>#include <sys/core.h>#include <sys/ldr.h>extern struct obstack frame_cache_obstack;extern int errno;/* Nonzero if we just simulated a single step break. */int one_stepped;/* Breakpoint shadows for the single step instructions will be kept here. */static struct sstep_breaks { int address; int data;} stepBreaks[2];/* Static function prototypes */static voidadd_text_to_loadinfo PARAMS ((CORE_ADDR textaddr, CORE_ADDR dataaddr));static CORE_ADDRfind_toc_address PARAMS ((CORE_ADDR pc));static CORE_ADDRbranch_dest PARAMS ((int opcode, int instr, CORE_ADDR pc, CORE_ADDR safety));static voidframe_get_cache_fsr PARAMS ((struct frame_info *fi, struct aix_framedata *fdatap));/* * Calculate the destination of a branch/jump. Return -1 if not a branch. */static CORE_ADDRbranch_dest (opcode, instr, pc, safety) int opcode; int instr; CORE_ADDR pc; CORE_ADDR safety;{ register long offset; CORE_ADDR dest; int immediate; int absolute; int ext_op; absolute = (int) ((instr >> 1) & 1); switch (opcode) { case 18 : immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */ case 16 : if (opcode != 18) /* br conditional */ immediate = ((instr & ~3) << 16) >> 16; if (absolute) dest = immediate; else dest = pc + immediate; break; case 19 : ext_op = (instr>>1) & 0x3ff; if (ext_op == 16) /* br conditional register */ dest = read_register (LR_REGNUM) & ~3; else if (ext_op == 528) /* br cond to count reg */ dest = read_register (CTR_REGNUM) & ~3; else return -1; break; default: return -1; } return (dest < TEXT_SEGMENT_BASE) ? safety : dest;}/* AIX does not support PT_STEP. Simulate it. */voidsingle_step (signal) int signal;{#define INSNLEN(OPCODE) 4 static char breakp[] = BREAKPOINT; int ii, insn, ret, loc; int breaks[2], opcode; if (!one_stepped) { loc = read_pc (); ret = read_memory (loc, &insn, sizeof (int)); if (ret) printf ("Error in single_step()!!\n"); breaks[0] = loc + INSNLEN(insn); opcode = insn >> 26; breaks[1] = branch_dest (opcode, insn, loc, breaks[0]); /* Don't put two breakpoints on the same address. */ if (breaks[1] == breaks[0]) breaks[1] = -1; stepBreaks[1].address = -1; for (ii=0; ii < 2; ++ii) { /* ignore invalid breakpoint. */ if ( breaks[ii] == -1) continue; read_memory (breaks[ii], &(stepBreaks[ii].data), sizeof(int)); ret = write_memory (breaks[ii], breakp, sizeof(int)); stepBreaks[ii].address = breaks[ii]; } one_stepped = 1; } else { /* remove step breakpoints. */ for (ii=0; ii < 2; ++ii) if (stepBreaks[ii].address != -1) write_memory (stepBreaks[ii].address, &(stepBreaks[ii].data), sizeof(int)); one_stepped = 0; } errno = 0; /* FIXME, don't ignore errors! */}/* return pc value after skipping a function prologue. */skip_prologue (pc)CORE_ADDR pc;{ unsigned int tmp; unsigned int op; /* FIXME, assumes instruction size matches host int!!! */ if (target_read_memory (pc, (char *)&op, sizeof (op))) return pc; /* Can't access it -- assume no prologue. */ SWAP_TARGET_AND_HOST (&op, sizeof (op)); /* Assume that subsequent fetches can fail with low probability. */ if (op == 0x7c0802a6) { /* mflr r0 */ pc += 4; op = read_memory_integer (pc, 4); } if ((op & 0xfc00003e) == 0x7c000026) { /* mfcr Rx */ pc += 4; op = read_memory_integer (pc, 4); } if ((op & 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */ pc += 4; op = read_memory_integer (pc, 4); /* At this point, make sure this is not a trampoline function (a function that simply calls another functions, and nothing else). If the next is not a nop, this branch was part of the function prologue. */ if (op == 0x4def7b82 || /* crorc 15, 15, 15 */ op == 0x0) return pc - 4; /* don't skip over this branch */ } if ((op & 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */ pc += 4; op = read_memory_integer (pc, 4); } while (((tmp = op >> 16) == 0x9001) || /* st r0, NUM(r1) */ (tmp == 0x9421) || /* stu r1, NUM(r1) */ (op == 0x93e1fffc)) /* st r31,-4(r1) */ { pc += 4; op = read_memory_integer (pc, 4); } while ((tmp = (op >> 22)) == 0x20f) { /* l r31, ... or */ pc += 4; /* l r30, ... */ op = read_memory_integer (pc, 4); } /* store parameters into stack */ while( (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */ (op & 0xfc1f0000) == 0x90010000 || /* st r?, NUM(r1) */ (op & 0xfc000000) == 0xfc000000 || /* frsp, fp?, .. */ (op & 0xd0000000) == 0xd0000000) /* stfs, fp?, .. */ { pc += 4; /* store fpr double */ op = read_memory_integer (pc, 4); } if (op == 0x603f0000) { /* oril r31, r1, 0x0 */ pc += 4; /* this happens if r31 is used as */ op = read_memory_integer (pc, 4); /* frame ptr. (gcc does that) */ tmp = 0; while ((op >> 16) == (0x907f + tmp)) { /* st r3, NUM(r31) */ pc += 4; /* st r4, NUM(r31), ... */ op = read_memory_integer (pc, 4); tmp += 0x20; } }#if 0/* I have problems with skipping over __main() that I need to address * sometime. Previously, I used to use misc_function_vector which * didn't work as well as I wanted to be. -MGO */ /* If the first thing after skipping a prolog is a branch to a function, this might be a call to an initializer in main(), introduced by gcc2. We'd like to skip over it as well. Fortunately, xlc does some extra work before calling a function right after a prologue, thus we can single out such gcc2 behaviour. */ if ((op & 0xfc000001) == 0x48000001) { /* bl foo, an initializer function? */ op = read_memory_integer (pc+4, 4); if (op == 0x4def7b82) { /* cror 0xf, 0xf, 0xf (nop) */ /* check and see if we are in main. If so, skip over this initializer function as well. */ tmp = find_pc_misc_function (pc); if (tmp >= 0 && !strcmp (misc_function_vector [tmp].name, "main")) return pc + 8; } }#endif /* 0 */ return pc;}/************************************************************************* Support for creating pushind a dummy frame into the stack, and popping frames, etc. *************************************************************************//* The total size of dummy frame is 436, which is; 32 gpr's - 128 bytes 32 fpr's - 256 " 7 the rest - 28 " and 24 extra bytes for the callee's link area. The last 24 bytes for the link area might not be necessary, since it will be taken care of by push_arguments(). */#define DUMMY_FRAME_SIZE 436#define DUMMY_FRAME_ADDR_SIZE 10/* Make sure you initialize these in somewhere, in case gdb gives up what it was debugging and starts debugging something else. FIXMEibm */static int dummy_frame_count = 0;static int dummy_frame_size = 0;static CORE_ADDR *dummy_frame_addr = 0;extern int stop_stack_dummy;/* push a dummy frame into stack, save all register. Currently we are saving only gpr's and fpr's, which is not good enough! FIXMEmgo */ voidpush_dummy_frame (){ int sp, pc; /* stack pointer and link register */ int ii; target_fetch_registers (-1); if (dummy_frame_count >= dummy_frame_size) { dummy_frame_size += DUMMY_FRAME_ADDR_SIZE; if (dummy_frame_addr) dummy_frame_addr = (CORE_ADDR*) xrealloc (dummy_frame_addr, sizeof(CORE_ADDR) * (dummy_frame_size)); else dummy_frame_addr = (CORE_ADDR*) xmalloc (sizeof(CORE_ADDR) * (dummy_frame_size)); } sp = read_register(SP_REGNUM); pc = read_register(PC_REGNUM); dummy_frame_addr [dummy_frame_count++] = sp; /* Be careful! If the stack pointer is not decremented first, then kernel thinks he is free to use the space underneath it. And kernel actually uses that area for IPC purposes when executing ptrace(2) calls. So before writing register values into the new frame, decrement and update %sp first in order to secure your frame. */ write_register (SP_REGNUM, sp-DUMMY_FRAME_SIZE); /* gdb relies on the state of current_frame. We'd better update it, otherwise things like do_registers_info() wouldn't work properly! */ flush_cached_frames (); set_current_frame (create_new_frame (sp-DUMMY_FRAME_SIZE, pc)); /* save program counter in link register's space. */ write_memory (sp+8, &pc, 4); /* save all floating point and general purpose registers here. */ /* fpr's, f0..f31 */ for (ii = 0; ii < 32; ++ii) write_memory (sp-8-(ii*8), ®isters[REGISTER_BYTE (31-ii+FP0_REGNUM)], 8); /* gpr's r0..r31 */ for (ii=1; ii <=32; ++ii) write_memory (sp-256-(ii*4), ®isters[REGISTER_BYTE (32-ii)], 4); /* so far, 32*2 + 32 words = 384 bytes have been written. 7 extra registers in our register set: pc, ps, cnd, lr, cnt, xer, mq */ for (ii=1; ii <= (LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) { write_memory (sp-384-(ii*4), ®isters[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4); } /* Save sp or so called back chain right here. */ write_memory (sp-DUMMY_FRAME_SIZE, &sp, 4); sp -= DUMMY_FRAME_SIZE; /* And finally, this is the back chain. */ write_memory (sp+8, &pc, 4);}/* Pop a dummy frame. In rs6000 when we push a dummy frame, we save all of the registers. This is usually done before user calls a function explicitly. After a dummy frame is pushed, some instructions are copied into stack, and stack pointer is decremented even more. Since we don't have a frame pointer to get back to the parent frame of the dummy, we start having trouble poping it. Therefore, we keep a dummy frame stack, keeping addresses of dummy frames as such. When poping happens and when we detect that was a dummy frame, we pop it back to its parent by using dummy frame stack (`dummy_frame_addr' array). FIXME: This whole concept is broken. You should be able to detecta dummy stack frame *on the user's stack itself*. When you do,then you know the format of that stack frame -- including its
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -