[PATCH] PPC - Stepping off breakpoints in non-stop mode

Luis Machado luisgpm@linux.vnet.ibm.com
Thu May 29 15:41:00 GMT 2008


> > Using unsigned int, char and unsigned long in a tdep file isn't
> > safe.  Can you switch to gdb_bytes and CORE_ADDR's?  This file
> > is used for cross-debugging.
> 
> Yes, that's true. I'll get this fixed. Thanks!

Attached the updated patch with the types fixed and some additional
comments.

Best regards,
Luis


2008-05-28  Luis Machado  <luisgpm@br.ibm.com>

	* ppc-tdep.h: Define PPC_MAX_INSN_LEN, BRANCH_MASK, B_INSN, BC_INSN,
	LWARX_MASK, LWARX_INSTRUCTION, LDARX_INSTRUCTION, STWCX_MASK,
	STWCX_INSTRUCTION, STDCX_INSTRUCTION, BXL_INSN, BP_MASK and BP_INSN.
	* rs6000-tdep.c (ppc_displaced_step_fixup): New function.
	(deal_with_atomic_sequence): Update BC masks.
	(rs6000_gdbarch_init): Init displaced stepping infra-structure.
	Remove LWARX_MASK, LWARX_INSTRUCTION, LDARX_INSTRUCTION, STWCX_MASK,
	STWCX_INSTRUCTION, STDCX_INSTRUCTION, BC_MASK and BC_INSTRUCTION.

Index: gdb/ppc-tdep.h
===================================================================
--- gdb.orig/ppc-tdep.h	2008-05-28 15:06:16.000000000 -0700
+++ gdb/ppc-tdep.h	2008-05-28 15:09:03.000000000 -0700
@@ -260,10 +260,28 @@
   PPC_NUM_REGS
 };
 
+/* The length of the longest ppc instruction.  */
+#define PPC_MAX_INSN_LEN (4)
 
 /* Instruction size.  */
 #define PPC_INSN_SIZE 4
 
+/* Instruction masks used during single-stepping of atomic sequences.  */
+#define LWARX_MASK 0xfc0007fe
+#define LWARX_INSTRUCTION 0x7c000028
+#define LDARX_INSTRUCTION 0x7c0000A8
+#define STWCX_MASK 0xfc0007ff
+#define STWCX_INSTRUCTION 0x7c00012d
+#define STDCX_INSTRUCTION 0x7c0001ad
+
+/* Instruction masks for displaced stepping.  */
+#define BRANCH_MASK 0xfc000000
+#define BP_MASK 0xFC0007FE
+#define B_INSN 0x48000000
+#define BC_INSN 0x40000000
+#define BXL_INSN 0x4c000000
+#define BP_INSN 0x7C000008
+
 /* Estimate for the maximum number of instrctions in a function epilogue.  */
 #define PPC_MAX_EPILOGUE_INSTRUCTIONS  52
 
Index: gdb/rs6000-tdep.c
===================================================================
--- gdb.orig/rs6000-tdep.c	2008-05-28 15:06:16.000000000 -0700
+++ gdb/rs6000-tdep.c	2008-05-28 17:17:06.000000000 -0700
@@ -841,16 +841,97 @@
     return little_breakpoint;
 }
 
+/* Fix up the state of registers and memory after having single-stepped
+   a displaced instruction.  */
+void
+ppc_displaced_step_fixup (struct gdbarch *gdbarch,
+			   struct displaced_step_closure *closure,
+			   CORE_ADDR from, CORE_ADDR to,
+			   struct regcache *regs)
+{
+  /* Since we use simple_displaced_step_copy_insn, our closure is a
+     copy of the instruction.  */
+  ULONGEST *insn  = (ULONGEST *) closure;
+  ULONGEST opcode = 0;
+  LONGEST offset = 0x4; /* Default offset for non PC-relative instructions.  */
+
+  *insn = (*insn >> 32) & 0x00000000ffffffffUL;
+  opcode = *insn & BRANCH_MASK;
+
+  if (debug_displaced)
+    fprintf_unfiltered (gdb_stdlog,
+			"displaced: (ppc) fixup (0x%s, 0x%s)\n",
+			paddr_nz (from), paddr_nz (to));
+
+
+  /* Handle PC-relative branch instructions.  */
+  if ((opcode == B_INSN) || (opcode == BC_INSN) || (opcode == BXL_INSN))
+    {
+      /* LK bit Indicates whether we should set the link register to point
+	 to the next instruction or not.  */
+      gdb_byte link_register_bit = (gdb_byte) (*insn & 0x1);
+      CORE_ADDR current_pc;
+
+      /* Read the current PC value after the instruction has been executed
+	 in a displaced location.  Calculate the offset to be applied to the
+	 original PC value before the displaced stepping.  */
+      regcache_cooked_read_unsigned (regs, gdbarch_pc_regnum (gdbarch),
+				      &current_pc);
+      offset = current_pc - to;
+
+      if (opcode != BXL_INSN)
+	{
+	  /* AA bit indicating whether this is an absolute addressing or
+	     PC-relative. */
+	  gdb_byte absolute_addr_bit = (gdb_byte) (*insn & 0x2);
+
+	  if (!absolute_addr_bit)
+	    {
 
-/* Instruction masks used during single-stepping of atomic sequences.  */
-#define LWARX_MASK 0xfc0007fe
-#define LWARX_INSTRUCTION 0x7c000028
-#define LDARX_INSTRUCTION 0x7c0000A8
-#define STWCX_MASK 0xfc0007ff
-#define STWCX_INSTRUCTION 0x7c00012d
-#define STDCX_INSTRUCTION 0x7c0001ad
-#define BC_MASK 0xfc000000
-#define BC_INSTRUCTION 0x40000000
+	      if (debug_displaced)
+		fprintf_unfiltered (gdb_stdlog,
+			    "displaced: (ppc) branch instruction: 0x%s\n"
+			    "displaced: (ppc) adjusted PC from 0x%s to 0x%s\n",
+			    paddr_nz (*insn), paddr_nz (current_pc),
+			    paddr_nz (from + offset));
+
+	      regcache_cooked_write_unsigned (regs, gdbarch_pc_regnum (gdbarch),
+					      from + offset);
+	    }
+	}
+      else
+	{
+	  /* If we're here, it means we have a branch to LR or CTR.  If the
+	     branch was taken, the offset is probably greater than 4 (the next
+	     instruction), so it's safe to assume that a offset of 4 means we
+	     did not take the branch.  */
+	  if (offset == 4)
+	    regcache_cooked_write_unsigned (regs, gdbarch_pc_regnum (gdbarch),
+					    from + 0x4);
+	}
+
+      if (link_register_bit)
+	{
+
+	  regcache_cooked_write_unsigned (regs,
+					  gdbarch_tdep (gdbarch)->ppc_lr_regnum,
+					  from + 0x4);
+	  if (debug_displaced)
+		fprintf_unfiltered (gdb_stdlog,
+				    "displaced: (ppc) adjusted LR to 0x%s\n",
+				    paddr_nz (from + 0x4));
+
+	}
+    }
+  /* Check for breakpoints in the inferior.  If we've found one, place the PC
+     right at the breakpoint instruction.  */
+  else if ((*insn & BP_MASK) == BP_INSN)
+    regcache_cooked_write_unsigned (regs, gdbarch_pc_regnum (gdbarch), from);
+  else
+  /* Handle any other instructions that do not fit in the categories above.  */
+    regcache_cooked_write_unsigned (regs, gdbarch_pc_regnum (gdbarch),
+				    from + offset);
+}
 
 /* Checks for an atomic sequence of instructions beginning with a LWARX/LDARX
    instruction and ending with a STWCX/STDCX instruction.  If such a sequence
@@ -887,7 +968,7 @@
       /* Assume that there is at most one conditional branch in the atomic
          sequence.  If a conditional branch is found, put a breakpoint in 
          its destination address.  */
-      if ((insn & BC_MASK) == BC_INSTRUCTION)
+      if ((insn & BRANCH_MASK) == BC_INSN)
         {
           int immediate = ((insn & ~3) << 16) >> 16;
           int absolute = ((insn >> 1) & 1);
@@ -3214,6 +3295,17 @@
     /* Put the _Decimal128 pseudo-registers after the SPE registers.  */
     tdep->ppc_dl0_regnum += 32;
 
+  /* Setup displaced stepping.  */
+  set_gdbarch_displaced_step_copy_insn (gdbarch,
+					simple_displaced_step_copy_insn);
+  set_gdbarch_displaced_step_fixup (gdbarch, ppc_displaced_step_fixup);
+  set_gdbarch_displaced_step_free_closure (gdbarch,
+					   simple_displaced_step_free_closure);
+  set_gdbarch_displaced_step_location (gdbarch,
+				       displaced_step_at_entry_point);
+
+  set_gdbarch_max_insn_length (gdbarch, PPC_MAX_INSN_LEN);
+
   return gdbarch;
 }
 




More information about the Gdb-patches mailing list