* gdbserver/low-hppabsd.c (read_inferior_memory): Add explicit
[deliverable/binutils-gdb.git] / gdb / rs6000-tdep.c
index ccb4299684b81c2963208d1721ebec5ee7e68b0f..3b4aeb95f6d4f54e546d760c241500782c04ad31 100644 (file)
@@ -1,5 +1,6 @@
 /* Target-dependent code for GDB, the GNU debugger.
-   Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
+   Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
+   1998, 1999, 2000, 2001
    Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -28,8 +29,8 @@
 #include "gdbcmd.h"
 #include "symfile.h"
 #include "objfiles.h"
-#include "xcoffsolib.h"
 #include "arch-utils.h"
+#include "regcache.h"
 
 #include "bfd/libbfd.h"                /* for bfd_default_set_arch_mach */
 #include "coff/internal.h"     /* for libcoff.h */
@@ -118,7 +119,8 @@ void (*rs6000_set_host_arch_hook) (int) = NULL;
 
 static CORE_ADDR branch_dest (int opcode, int instr, CORE_ADDR pc,
                              CORE_ADDR safety);
-static CORE_ADDR skip_prologue (CORE_ADDR, struct rs6000_framedata *);
+static CORE_ADDR skip_prologue (CORE_ADDR, CORE_ADDR,
+                                struct rs6000_framedata *);
 static void frame_get_saved_regs (struct frame_info * fi,
                                  struct rs6000_framedata * fdatap);
 static CORE_ADDR frame_initial_stack_address (struct frame_info *);
@@ -135,7 +137,7 @@ static CORE_ADDR
 rs6000_skip_prologue (CORE_ADDR pc)
 {
   struct rs6000_framedata frame;
-  pc = skip_prologue (pc, &frame);
+  pc = skip_prologue (pc, 0, &frame);
   return pc;
 }
 
@@ -297,7 +299,8 @@ rs6000_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)
 /* AIX does not support PT_STEP. Simulate it. */
 
 void
-rs6000_software_single_step (unsigned int signal, int insert_breakpoints_p)
+rs6000_software_single_step (enum target_signal signal,
+                            int insert_breakpoints_p)
 {
 #define        INSNLEN(OPCODE)  4
 
@@ -380,11 +383,63 @@ rs6000_software_single_step (unsigned int signal, int insert_breakpoints_p)
 
 #define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
 
+/* Limit the number of skipped non-prologue instructions, as the examining
+   of the prologue is expensive.  */
+static int max_skip_non_prologue_insns = 10;
+
+/* Given PC representing the starting address of a function, and
+   LIM_PC which is the (sloppy) limit to which to scan when looking
+   for a prologue, attempt to further refine this limit by using
+   the line data in the symbol table.  If successful, a better guess
+   on where the prologue ends is returned, otherwise the previous
+   value of lim_pc is returned.  */
+static CORE_ADDR
+refine_prologue_limit (CORE_ADDR pc, CORE_ADDR lim_pc)
+{
+  struct symtab_and_line prologue_sal;
+
+  prologue_sal = find_pc_line (pc, 0);
+  if (prologue_sal.line != 0)
+    {
+      int i;
+      CORE_ADDR addr = prologue_sal.end;
+
+      /* Handle the case in which compiler's optimizer/scheduler
+         has moved instructions into the prologue.  We scan ahead
+        in the function looking for address ranges whose corresponding
+        line number is less than or equal to the first one that we
+        found for the function.  (It can be less than when the
+        scheduler puts a body instruction before the first prologue
+        instruction.)  */
+      for (i = 2 * max_skip_non_prologue_insns; 
+           i > 0 && (lim_pc == 0 || addr < lim_pc);
+          i--)
+        {
+         struct symtab_and_line sal;
+
+         sal = find_pc_line (addr, 0);
+         if (sal.line == 0)
+           break;
+         if (sal.line <= prologue_sal.line 
+             && sal.symtab == prologue_sal.symtab)
+           {
+             prologue_sal = sal;
+           }
+         addr = sal.end;
+       }
+
+      if (lim_pc == 0 || prologue_sal.end < lim_pc)
+       lim_pc = prologue_sal.end;
+    }
+  return lim_pc;
+}
+
+
 static CORE_ADDR
-skip_prologue (CORE_ADDR pc, struct rs6000_framedata *fdata)
+skip_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct rs6000_framedata *fdata)
 {
   CORE_ADDR orig_pc = pc;
-  CORE_ADDR last_prologue_pc;
+  CORE_ADDR last_prologue_pc = pc;
   char buf[4];
   unsigned long op;
   long offset = 0;
@@ -394,6 +449,22 @@ skip_prologue (CORE_ADDR pc, struct rs6000_framedata *fdata)
   int framep = 0;
   int minimal_toc_loaded = 0;
   int prev_insn_was_prologue_insn = 1;
+  int num_skip_non_prologue_insns = 0;
+
+  /* Attempt to find the end of the prologue when no limit is specified.
+     Note that refine_prologue_limit() has been written so that it may
+     be used to "refine" the limits of non-zero PC values too, but this
+     is only safe if we 1) trust the line information provided by the
+     compiler and 2) iterate enough to actually find the end of the
+     prologue.  
+     
+     It may become a good idea at some point (for both performance and
+     accuracy) to unconditionally call refine_prologue_limit().  But,
+     until we can make a clear determination that this is beneficial,
+     we'll play it safe and only use it to obtain a limit when none
+     has been specified.  */
+  if (lim_pc == 0)
+    lim_pc = refine_prologue_limit (pc, lim_pc);
 
   memset (fdata, 0, sizeof (struct rs6000_framedata));
   fdata->saved_gpr = -1;
@@ -402,19 +473,22 @@ skip_prologue (CORE_ADDR pc, struct rs6000_framedata *fdata)
   fdata->frameless = 1;
   fdata->nosavedpc = 1;
 
-  pc -= 4;
-  for (;;)
+  for (;; pc += 4)
     {
-      pc += 4;
-
       /* Sometimes it isn't clear if an instruction is a prologue
          instruction or not.  When we encounter one of these ambiguous
         cases, we'll set prev_insn_was_prologue_insn to 0 (false).
         Otherwise, we'll assume that it really is a prologue instruction. */
       if (prev_insn_was_prologue_insn)
        last_prologue_pc = pc;
+
+      /* Stop scanning if we've hit the limit.  */
+      if (lim_pc != 0 && pc >= lim_pc)
+       break;
+
       prev_insn_was_prologue_insn = 1;
 
+      /* Fetch the instruction and convert it to an integer.  */
       if (target_read_memory (pc, buf, 4))
        break;
       op = extract_signed_integer (buf, 4);
@@ -628,7 +702,31 @@ skip_prologue (CORE_ADDR pc, struct rs6000_framedata *fdata)
        }
       else
        {
-         break;
+         /* Not a recognized prologue instruction.
+            Handle optimizer code motions into the prologue by continuing
+            the search if we have no valid frame yet or if the return
+            address is not yet saved in the frame.  */
+         if (fdata->frameless == 0
+             && (lr_reg == -1 || fdata->nosavedpc == 0))
+           break;
+
+         if (op == 0x4e800020          /* blr */
+             || op == 0x4e800420)      /* bctr */
+           /* Do not scan past epilogue in frameless functions or
+              trampolines.  */
+           break;
+         if ((op & 0xf4000000) == 0x40000000) /* bxx */
+           /* Never skip branches. */
+           break;
+
+         if (num_skip_non_prologue_insns++ > max_skip_non_prologue_insns)
+           /* Do not scan too many insns, scanning insns is expensive with
+              remote targets.  */
+           break;
+
+         /* Continue scanning.  */
+         prev_insn_was_prologue_insn = 0;
+         continue;
        }
     }
 
@@ -700,7 +798,7 @@ rs6000_pop_frame (void)
      saved %pc value in the previous frame. */
 
   addr = get_pc_function_start (frame->pc);
-  (void) skip_prologue (addr, &fdata);
+  (void) skip_prologue (addr, frame->pc, &fdata);
 
   wordsize = TDEP->wordsize;
   if (fdata.frameless)
@@ -1106,7 +1204,7 @@ rs6000_frameless_function_invocation (struct frame_info *fi)
        return 0;
     }
 
-  (void) skip_prologue (func_start, &fdata);
+  (void) skip_prologue (func_start, fi->pc, &fdata);
   return fdata.frameless;
 }
 
@@ -1132,7 +1230,7 @@ rs6000_frame_saved_pc (struct frame_info *fi)
   if (!func_start)
     return 0;
 
-  (void) skip_prologue (func_start, &fdata);
+  (void) skip_prologue (func_start, fi->pc, &fdata);
 
   if (fdata.lr_offset == 0 && fi->next != NULL)
     {
@@ -1167,7 +1265,7 @@ frame_get_saved_regs (struct frame_info *fi, struct rs6000_framedata *fdatap)
   if (fdatap == NULL)
     {
       fdatap = &work_fdata;
-      (void) skip_prologue (get_pc_function_start (fi->pc), fdatap);
+      (void) skip_prologue (get_pc_function_start (fi->pc), fi->pc, fdatap);
     }
 
   frame_saved_regs_zalloc (fi);
@@ -1243,7 +1341,7 @@ frame_initial_stack_address (struct frame_info *fi)
 
   /* find out if this function is using an alloca register.. */
 
-  (void) skip_prologue (get_pc_function_start (fi->pc), &fdata);
+  (void) skip_prologue (get_pc_function_start (fi->pc), fi->pc, &fdata);
 
   /* if saved registers of this frame are not known yet, read and cache them. */
 
@@ -1540,11 +1638,11 @@ rs6000_create_inferior (int pid)
    a function pointer would require allocation of a TOC entry in the
    inferior's memory space, with all its drawbacks.  To be able to
    call C++ virtual methods in the inferior (which are called via
-   function pointers), find_function_addr uses this macro to get the
+   function pointers), find_function_addr uses this function to get the
    function address from a function pointer.  */
 
-/* Return nonzero if ADDR (a function pointer) is in the data space and
-   is therefore a special function pointer.  */
+/* Return real function address if ADDR (a function pointer) is in the data
+   space and is therefore a special function pointer.  */
 
 CORE_ADDR
 rs6000_convert_from_func_ptr_addr (CORE_ADDR addr)
@@ -1944,8 +2042,9 @@ process_note_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
              *os_ident_ptr = ELFOSABI_SOLARIS;
              break;
            default :
-             internal_error (
-               "process_note_abi_sections: unknown OS number %d", os_number);
+             internal_error (__FILE__, __LINE__,
+                             "process_note_abi_sections: unknown OS number %d",
+                             os_number);
              break;
            }
        }
@@ -2195,6 +2294,10 @@ rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
       set_gdbarch_frame_init_saved_regs (gdbarch, rs6000_frame_init_saved_regs);
       set_gdbarch_init_extra_frame_info (gdbarch, rs6000_init_extra_frame_info);
+
+      /* Handle RS/6000 function pointers.  */
+      set_gdbarch_convert_from_func_ptr_addr (gdbarch,
+       rs6000_convert_from_func_ptr_addr);
     }
   set_gdbarch_frame_args_address (gdbarch, rs6000_frame_args_address);
   set_gdbarch_frame_locals_address (gdbarch, rs6000_frame_args_address);
This page took 0.026571 seconds and 4 git commands to generate.