[2/2] arm-tdep.c: Refactor displaced stepping relocation functions

Message ID 1456415245-24005-3-git-send-email-simon.marchi@ericsson.com
State New, archived
Headers

Commit Message

Simon Marchi Feb. 25, 2016, 3:47 p.m. UTC
  A small refactor so that arm_process_displaced_insn is the only function
specific to GDB.  All functions called from this one will eventually be
moved to common/, so they need to be free of anything GDB-specific.  I
also renamed those functions from "process_displaced" to "relocate",
since they won't be used exclusively for displaced stepping anymore.

The call tree ends up like this:

  - arm_process_displaced_insn
    - arm_relocate_insn_arm
      ...
    - arm_relocate_insn_thumb_32bit
      ...
    - arm_relocate_insn_thumb_16bit
      ...

gdb/ChangeLog:

	* arm-tdep.c (thumb_process_displaced_16bit_insn): Rename to...
	(arm_relocate_insn_thumb_16bit): ... this, and add return error
	code.
	(thumb_process_displaced_32bit_insn): Rename to...
	(arm_relocate_insn_thumb_32bit): ... this, and add return error
	code.
	(thumb_process_displaced_insn): Remove.
	(arm_relocate_insn_arm): New function, extracted mostly from...
	(arm_process_displaced_insn): ... this.  Refactor to adapt to
	other functions changes.
---
 gdb/arm-tdep.c | 122 ++++++++++++++++++++++++++++++---------------------------
 1 file changed, 64 insertions(+), 58 deletions(-)
  

Comments

Luis Machado Feb. 26, 2016, 4:11 p.m. UTC | #1
On 02/25/2016 12:47 PM, Simon Marchi wrote:
> A small refactor so that arm_process_displaced_insn is the only function
> specific to GDB.  All functions called from this one will eventually be
> moved to common/, so they need to be free of anything GDB-specific.  I
> also renamed those functions from "process_displaced" to "relocate",
> since they won't be used exclusively for displaced stepping anymore.
>
> The call tree ends up like this:
>
>    - arm_process_displaced_insn
>      - arm_relocate_insn_arm
>        ...
>      - arm_relocate_insn_thumb_32bit
>        ...
>      - arm_relocate_insn_thumb_16bit
>        ...
>
> gdb/ChangeLog:
>
> 	* arm-tdep.c (thumb_process_displaced_16bit_insn): Rename to...
> 	(arm_relocate_insn_thumb_16bit): ... this, and add return error
> 	code.
> 	(thumb_process_displaced_32bit_insn): Rename to...
> 	(arm_relocate_insn_thumb_32bit): ... this, and add return error
> 	code.
> 	(thumb_process_displaced_insn): Remove.
> 	(arm_relocate_insn_arm): New function, extracted mostly from...
> 	(arm_process_displaced_insn): ... this.  Refactor to adapt to
> 	other functions changes.
> ---
>   gdb/arm-tdep.c | 122 ++++++++++++++++++++++++++++++---------------------------
>   1 file changed, 64 insertions(+), 58 deletions(-)
>
> diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
> index 43b61c2..ef48a90 100644
> --- a/gdb/arm-tdep.c
> +++ b/gdb/arm-tdep.c
> @@ -7111,9 +7111,8 @@ thumb_copy_pop_pc_16bit (uint16_t insn1, struct arm_insn_reloc_data *data)
>     return 0;
>   }
>
> -static void
> -thumb_process_displaced_16bit_insn (uint16_t insn1,
> -				    struct arm_insn_reloc_data *data)
> +static int
> +arm_relocate_insn_thumb_16bit (uint16_t insn1, struct arm_insn_reloc_data *data)
>   {
>     unsigned short op_bit_12_15 = bits (insn1, 12, 15);
>     unsigned short op_bit_10_11 = bits (insn1, 10, 11);
> @@ -7202,9 +7201,7 @@ thumb_process_displaced_16bit_insn (uint16_t insn1,
>         err = 1;
>       }
>
> -  if (err)
> -    internal_error (__FILE__, __LINE__,
> -		    _("thumb_process_displaced_16bit_insn: Instruction decode error"));
> +  return err;

Should we keep this internal error message under a different context 
instead of exporting just an error code? Maybe the error code should 
trigger this internal error for GDB?

>   }
>
>   static int
> @@ -7279,9 +7276,9 @@ decode_thumb_32bit_ld_mem_hints (uint16_t insn1, uint16_t insn2,
>     return 0;
>   }
>
> -static void
> -thumb_process_displaced_32bit_insn (uint16_t insn1, uint16_t insn2,
> -				    struct arm_insn_reloc_data *data)
> +static int
> +arm_relocate_insn_thumb_32bit (uint16_t insn1, uint16_t insn2,
> +			       struct arm_insn_reloc_data *data)
>   {
>     int err = 0;
>     unsigned short op = bit (insn2, 15);
> @@ -7393,34 +7390,41 @@ thumb_process_displaced_32bit_insn (uint16_t insn1, uint16_t insn2,
>         err = 1;
>       }
>
> -  if (err)
> -    internal_error (__FILE__, __LINE__,
> -		    _("thumb_process_displaced_32bit_insn: Instruction decode error"));
> +  return err;
>

The above one too?

Otherwise it looks mostly ok to me, though the patch is a little 
convoluted due to code movement.
  
Simon Marchi Feb. 26, 2016, 4:23 p.m. UTC | #2
>> diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
>> index 43b61c2..ef48a90 100644
>> --- a/gdb/arm-tdep.c
>> +++ b/gdb/arm-tdep.c
>> @@ -7111,9 +7111,8 @@ thumb_copy_pop_pc_16bit (uint16_t insn1, struct arm_insn_reloc_data *data)
>>     return 0;
>>   }
>>
>> -static void
>> -thumb_process_displaced_16bit_insn (uint16_t insn1,
>> -				    struct arm_insn_reloc_data *data)
>> +static int
>> +arm_relocate_insn_thumb_16bit (uint16_t insn1, struct arm_insn_reloc_data *data)
>>   {
>>     unsigned short op_bit_12_15 = bits (insn1, 12, 15);
>>     unsigned short op_bit_10_11 = bits (insn1, 10, 11);
>> @@ -7202,9 +7201,7 @@ thumb_process_displaced_16bit_insn (uint16_t insn1,
>>         err = 1;
>>       }
>>
>> -  if (err)
>> -    internal_error (__FILE__, __LINE__,
>> -		    _("thumb_process_displaced_16bit_insn: Instruction decode error"));
>> +  return err;
> 
> Should we keep this internal error message under a different context 
> instead of exporting just an error code? Maybe the error code should 
> trigger this internal error for GDB?

I am not sure I understand your comment. Given this call tree:

- arm_process_displaced_insn
  - arm_relocate_insn_arm
    ...
  - arm_relocate_insn_thumb_32bit
    ...
  - arm_relocate_insn_thumb_16bit
    ...

my patch makes it so that the arm_relocate_insn* functions return an error code, and
arm_process_displaced_insn calls internal_error if an error is returned.  Do you suggest
putting the internal_error calls in the arm_relocate_insn_* functions directly?
  
Luis Machado Feb. 26, 2016, 4:50 p.m. UTC | #3
On 02/26/2016 01:23 PM, Simon Marchi wrote:
>>> diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
>>> index 43b61c2..ef48a90 100644
>>> --- a/gdb/arm-tdep.c
>>> +++ b/gdb/arm-tdep.c
>>> @@ -7111,9 +7111,8 @@ thumb_copy_pop_pc_16bit (uint16_t insn1, struct arm_insn_reloc_data *data)
>>>      return 0;
>>>    }
>>>
>>> -static void
>>> -thumb_process_displaced_16bit_insn (uint16_t insn1,
>>> -				    struct arm_insn_reloc_data *data)
>>> +static int
>>> +arm_relocate_insn_thumb_16bit (uint16_t insn1, struct arm_insn_reloc_data *data)
>>>    {
>>>      unsigned short op_bit_12_15 = bits (insn1, 12, 15);
>>>      unsigned short op_bit_10_11 = bits (insn1, 10, 11);
>>> @@ -7202,9 +7201,7 @@ thumb_process_displaced_16bit_insn (uint16_t insn1,
>>>          err = 1;
>>>        }
>>>
>>> -  if (err)
>>> -    internal_error (__FILE__, __LINE__,
>>> -		    _("thumb_process_displaced_16bit_insn: Instruction decode error"));
>>> +  return err;
>>
>> Should we keep this internal error message under a different context
>> instead of exporting just an error code? Maybe the error code should
>> trigger this internal error for GDB?
>
> I am not sure I understand your comment. Given this call tree:
>
> - arm_process_displaced_insn
>    - arm_relocate_insn_arm
>      ...
>    - arm_relocate_insn_thumb_32bit
>      ...
>    - arm_relocate_insn_thumb_16bit
>      ...
>
> my patch makes it so that the arm_relocate_insn* functions return an error code, and
> arm_process_displaced_insn calls internal_error if an error is returned.  Do you suggest
> putting the internal_error calls in the arm_relocate_insn_* functions directly?
>

No. If we want them shared, i don't think we'd want them to throw 
internal errors.

I was just pointing out the fact that we're losing the function name 
information from arm_relocate_insn_thumb_16bit's and 
thumb_process_displaced_32bit_insn's internal error messages. This 
information may make debugging easier. We would need to throw errors 
with custom messages from within arm_process_displaced_insn in order to 
maintain those names. For example:

"thumb_process_displaced_16bit_insn: Instruction decode error"
"thumb_process_displaced_32bit_insn: Instruction decode error"

Instead of:

"arm_process_displaced_insn: Instruction decode error"

I see the regular non-thumb functions don't throw internal errors 
themselves though. So i'm fine if others think the more specific error 
message is not needed.

Luis
  

Patch

diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index 43b61c2..ef48a90 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -7111,9 +7111,8 @@  thumb_copy_pop_pc_16bit (uint16_t insn1, struct arm_insn_reloc_data *data)
   return 0;
 }
 
-static void
-thumb_process_displaced_16bit_insn (uint16_t insn1,
-				    struct arm_insn_reloc_data *data)
+static int
+arm_relocate_insn_thumb_16bit (uint16_t insn1, struct arm_insn_reloc_data *data)
 {
   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
@@ -7202,9 +7201,7 @@  thumb_process_displaced_16bit_insn (uint16_t insn1,
       err = 1;
     }
 
-  if (err)
-    internal_error (__FILE__, __LINE__,
-		    _("thumb_process_displaced_16bit_insn: Instruction decode error"));
+  return err;
 }
 
 static int
@@ -7279,9 +7276,9 @@  decode_thumb_32bit_ld_mem_hints (uint16_t insn1, uint16_t insn2,
   return 0;
 }
 
-static void
-thumb_process_displaced_32bit_insn (uint16_t insn1, uint16_t insn2,
-				    struct arm_insn_reloc_data *data)
+static int
+arm_relocate_insn_thumb_32bit (uint16_t insn1, uint16_t insn2,
+			       struct arm_insn_reloc_data *data)
 {
   int err = 0;
   unsigned short op = bit (insn2, 15);
@@ -7393,34 +7390,41 @@  thumb_process_displaced_32bit_insn (uint16_t insn1, uint16_t insn2,
       err = 1;
     }
 
-  if (err)
-    internal_error (__FILE__, __LINE__,
-		    _("thumb_process_displaced_32bit_insn: Instruction decode error"));
+  return err;
 
 }
 
-static void
-thumb_process_displaced_insn (CORE_ADDR from, struct arm_insn_reloc_data *data)
+static int
+arm_relocate_insn_arm (uint32_t insn, struct arm_insn_reloc_data *data)
 {
-  enum bfd_endian byte_order_for_code
-    = gdbarch_byte_order_for_code (data->gdbarch);
-  uint16_t insn1
-    = read_memory_unsigned_integer (from, 2, byte_order_for_code);
+  int err = 1;
 
-  if (debug_displaced)
-    fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
-			"at %.8lx\n", insn1, (unsigned long) from);
-
-  data->dsc->is_thumb = 1;
-  data->dsc->insn_size = thumb_insn_size (insn1);
-  if (thumb_insn_size (insn1) == 4)
+  if ((insn & 0xf0000000) == 0xf0000000)
+    err = arm_decode_unconditional (insn, data);
+  else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
     {
-      uint16_t insn2
-	= read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
-      thumb_process_displaced_32bit_insn (insn1, insn2, data);
+    case 0x0: case 0x1: case 0x2: case 0x3:
+      err = arm_decode_dp_misc (insn, data);
+      break;
+
+    case 0x4: case 0x5: case 0x6:
+      err = arm_decode_ld_st_word_ubyte (insn, data);
+      break;
+
+    case 0x7:
+      err = arm_decode_media (insn, data);
+      break;
+
+    case 0x8: case 0x9: case 0xa: case 0xb:
+      err = arm_decode_b_bl_ldmstm (insn, data);
+      break;
+
+    case 0xc: case 0xd: case 0xe: case 0xf:
+      err = arm_decode_svc_copro (insn, data);
+      break;
     }
-  else
-    thumb_process_displaced_16bit_insn (insn1, data);
+
+  return err;
 }
 
 void
@@ -7430,7 +7434,6 @@  arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 {
   int err = 0;
   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
-  uint32_t insn;
   struct arm_insn_reloc_data reloc_data = {
     dsc,
     gdbarch,
@@ -7445,40 +7448,43 @@  arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
   dsc->cleanup = NULL;
   dsc->wrote_to_pc = 0;
 
-  if (!displaced_in_arm_mode (regs))
-    return thumb_process_displaced_insn (from, &reloc_data);
+  if (displaced_in_arm_mode (regs))
+    {
+      uint32_t insn
+	= read_memory_unsigned_integer (from, 4, byte_order_for_code);
 
-  dsc->is_thumb = 0;
-  dsc->insn_size = 4;
-  insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
-  if (debug_displaced)
-    fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
-			"at %.8lx\n", (unsigned long) insn,
-			(unsigned long) from);
+      if (debug_displaced)
+        fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
+			    "at %.8lx\n", (unsigned long) insn,
+			    (unsigned long) from);
 
-  if ((insn & 0xf0000000) == 0xf0000000)
-    err = arm_decode_unconditional (insn, &reloc_data);
-  else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
+      dsc->is_thumb = 0;
+      dsc->insn_size = 4;
+
+      err = arm_relocate_insn_arm (insn, &reloc_data);
+    }
+  else
     {
-    case 0x0: case 0x1: case 0x2: case 0x3:
-      err = arm_decode_dp_misc (insn, &reloc_data);
-      break;
+      uint16_t insn1
+	= read_memory_unsigned_integer (from, 2, byte_order_for_code);
+      unsigned int insn_size = thumb_insn_size (insn1);
 
-    case 0x4: case 0x5: case 0x6:
-      err = arm_decode_ld_st_word_ubyte (insn, &reloc_data);
-      break;
+      if (debug_displaced)
+        fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
+			    "at %.8lx\n", insn1, (unsigned long) from);
 
-    case 0x7:
-      err = arm_decode_media (insn, &reloc_data);
-      break;
+      dsc->is_thumb = 1;
+      dsc->insn_size = insn_size;
 
-    case 0x8: case 0x9: case 0xa: case 0xb:
-      err = arm_decode_b_bl_ldmstm (insn, &reloc_data);
-      break;
+      if (insn_size == 4)
+        {
+          uint16_t insn2
+	    = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
 
-    case 0xc: case 0xd: case 0xe: case 0xf:
-      err = arm_decode_svc_copro (insn, &reloc_data);
-      break;
+	  err = arm_relocate_insn_thumb_32bit (insn1, insn2, &reloc_data);
+        }
+      else
+        err = arm_relocate_insn_thumb_16bit (insn1, &reloc_data);
     }
 
   if (err)