Fix inferior memory reading in GDBServer for arm/aarch32
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-arm-low.c
index 57cb8551e365e3e9eaba5012f31d5f19a7726121..fc2b34870a14b00fb201103521e6204bf01e7815 100644 (file)
@@ -1,5 +1,5 @@
 /* GNU/Linux/ARM specific low level interface, for the remote server for GDB.
-   Copyright (C) 1995-2016 Free Software Foundation, Inc.
+   Copyright (C) 1995-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -147,8 +147,7 @@ static ULONGEST get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr,
 static CORE_ADDR get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
                                                CORE_ADDR val);
 
-static CORE_ADDR get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self,
-                                              CORE_ADDR pc);
+static CORE_ADDR get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
 
 static int get_next_pcs_is_thumb (struct arm_get_next_pcs *self);
 
@@ -157,7 +156,8 @@ static struct arm_get_next_pcs_ops get_next_pcs_ops = {
   get_next_pcs_read_memory_unsigned_integer,
   get_next_pcs_syscall_next_pc,
   get_next_pcs_addr_bits_remove,
-  get_next_pcs_is_thumb
+  get_next_pcs_is_thumb,
+  arm_linux_get_next_pcs_fixup,
 };
 
 static int
@@ -262,14 +262,16 @@ get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr,
 {
   ULONGEST res;
 
-  (*the_target->read_memory) (memaddr, (unsigned char *) &res, len);
+  res = 0;
+  target_read_memory (memaddr, (unsigned char *) &res, len);
+
   return res;
 }
 
 /* Fetch the thread-local storage pointer for libthread_db.  */
 
 ps_err_e
-ps_get_thread_area (const struct ps_prochandle *ph,
+ps_get_thread_area (struct ps_prochandle *ph,
                    lwpid_t lwpid, int idx, void **base)
 {
   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
@@ -750,16 +752,20 @@ arm_prepare_to_resume (struct lwp_info *lwp)
       }
 }
 
-/* Find the next pc for a sigreturn or rt_sigreturn syscall.
+/* Find the next pc for a sigreturn or rt_sigreturn syscall.  In
+   addition, set IS_THUMB depending on whether we will return to ARM
+   or Thumb code.
    See arm-linux.h for stack layout details.  */
 static CORE_ADDR
-arm_sigreturn_next_pc (struct regcache *regcache, int svc_number)
+arm_sigreturn_next_pc (struct regcache *regcache, int svc_number,
+                      int *is_thumb)
 {
   unsigned long sp;
   unsigned long sp_data;
   /* Offset of PC register.  */
   int pc_offset = 0;
   CORE_ADDR next_pc = 0;
+  uint32_t cpsr;
 
   gdb_assert (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn);
 
@@ -771,15 +777,20 @@ arm_sigreturn_next_pc (struct regcache *regcache, int svc_number)
 
   (*the_target->read_memory) (sp + pc_offset, (unsigned char *) &next_pc, 4);
 
+  /* Set IS_THUMB according the CPSR saved on the stack.  */
+  (*the_target->read_memory) (sp + pc_offset + 4, (unsigned char *) &cpsr, 4);
+  *is_thumb = ((cpsr & CPSR_T) != 0);
+
   return next_pc;
 }
 
 /* When PC is at a syscall instruction, return the PC of the next
    instruction to be executed.  */
 static CORE_ADDR
-get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self, CORE_ADDR pc)
+get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
 {
   CORE_ADDR next_pc = 0;
+  CORE_ADDR pc = regcache_read_pc (self->regcache);
   int is_thumb = arm_is_thumb_mode ();
   ULONGEST svc_number = 0;
   struct regcache *regcache = self->regcache;
@@ -794,7 +805,7 @@ get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self, CORE_ADDR pc)
       unsigned long this_instr;
       unsigned long svc_operand;
 
-      (*the_target->read_memory) (pc, (unsigned char *) &this_instr, 4);
+      target_read_memory (pc, (unsigned char *) &this_instr, 4);
       svc_operand = (0x00ffffff & this_instr);
 
       if (svc_operand)  /* OABI.  */
@@ -812,7 +823,9 @@ get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self, CORE_ADDR pc)
   /* This is a sigreturn or sigreturn_rt syscall.  */
   if (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn)
     {
-      next_pc = arm_sigreturn_next_pc (regcache, svc_number);
+      /* SIGRETURN or RT_SIGRETURN may affect the arm thumb mode, so
+        update IS_THUMB.   */
+      next_pc = arm_sigreturn_next_pc (regcache, svc_number, &is_thumb);
     }
 
   /* Addresses for calling Thumb functions have the bit 0 set.  */
@@ -913,7 +926,7 @@ arm_arch_setup (void)
 /* Fetch the next possible PCs after the current instruction executes.  */
 
 static VEC (CORE_ADDR) *
-arm_gdbserver_get_next_pcs (CORE_ADDR pc, struct regcache *regcache)
+arm_gdbserver_get_next_pcs (struct regcache *regcache)
 {
   struct arm_get_next_pcs next_pcs_ctx;
   VEC (CORE_ADDR) *next_pcs = NULL;
@@ -926,7 +939,7 @@ arm_gdbserver_get_next_pcs (CORE_ADDR pc, struct regcache *regcache)
                         1,
                         regcache);
 
-  next_pcs = arm_get_next_pcs (&next_pcs_ctx, pc);
+  next_pcs = arm_get_next_pcs (&next_pcs_ctx);
 
   return next_pcs;
 }
@@ -939,6 +952,40 @@ arm_supports_hardware_single_step (void)
   return 0;
 }
 
+/* Implementation of linux_target_ops method "get_syscall_trapinfo".  */
+
+static void
+arm_get_syscall_trapinfo (struct regcache *regcache, int *sysno)
+{
+  if (arm_is_thumb_mode ())
+    collect_register_by_name (regcache, "r7", sysno);
+  else
+    {
+      unsigned long pc;
+      unsigned long insn;
+
+      collect_register_by_name (regcache, "pc", &pc);
+
+      if ((*the_target->read_memory) (pc - 4, (unsigned char *) &insn, 4))
+       *sysno = UNKNOWN_SYSCALL;
+      else
+       {
+         unsigned long svc_operand = (0x00ffffff & insn);
+
+         if (svc_operand)
+           {
+             /* OABI */
+             *sysno = svc_operand - 0x900000;
+           }
+         else
+           {
+             /* EABI */
+             collect_register_by_name (regcache, "r7", sysno);
+           }
+       }
+    }
+}
+
 /* Register sets without using PTRACE_GETREGSET.  */
 
 static struct regset_info arm_regsets[] = {
@@ -1019,7 +1066,8 @@ struct linux_target_ops the_low_target = {
   NULL, /* get_min_fast_tracepoint_insn_len */
   NULL, /* supports_range_stepping */
   arm_breakpoint_kind_from_current_state,
-  arm_supports_hardware_single_step
+  arm_supports_hardware_single_step,
+  arm_get_syscall_trapinfo,
 };
 
 void
This page took 0.028141 seconds and 4 git commands to generate.