[2/3] spu: Use ptid from regcache instead of inferior_ptid

Message ID 20170318170801.22988-2-simon.marchi@polymtl.ca
State New, archived
Headers

Commit Message

Simon Marchi March 18, 2017, 5:08 p.m. UTC
  The implementations of to_fetch_regiters/to_store_registers in the spu
code use some functions that rely on inferior_ptid.  It's simpler for
now to set/restore inferior_ptid.

	* spu-linux-nat.c (spu_fetch_inferior_registers,
	spu_store_inferior_registers): Use ptid from regcache, set and
	restore inferior_ptid.
	* spu-multiarch.c (spu_fetch_registers, spu_store_registers):
	Likewise.
---
 gdb/spu-linux-nat.c | 24 ++++++++++++++++++++++--
 gdb/spu-multiarch.c | 26 ++++++++++++++++++++++++--
 2 files changed, 46 insertions(+), 4 deletions(-)
  

Comments

Pedro Alves March 20, 2017, 3:54 p.m. UTC | #1
On 03/18/2017 05:08 PM, Simon Marchi wrote:
> The implementations of to_fetch_regiters/to_store_registers in the spu
> code use some functions that rely on inferior_ptid.  It's simpler for
> now to set/restore inferior_ptid.
> 
> 	* spu-linux-nat.c (spu_fetch_inferior_registers,
> 	spu_store_inferior_registers): Use ptid from regcache, set and
> 	restore inferior_ptid.
> 	* spu-multiarch.c (spu_fetch_registers, spu_store_registers):
> 	Likewise.
> ---
>  gdb/spu-linux-nat.c | 24 ++++++++++++++++++++++--
>  gdb/spu-multiarch.c | 26 ++++++++++++++++++++++++--
>  2 files changed, 46 insertions(+), 4 deletions(-)
> 
> diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c
> index c5b91222c1..5ec7b65ad9 100644
> --- a/gdb/spu-linux-nat.c
> +++ b/gdb/spu-linux-nat.c
> @@ -492,9 +492,17 @@ spu_fetch_inferior_registers (struct target_ops *ops,
>    int fd;
>    ULONGEST addr;
>  
> +  /* Since we use functions that rely on inferior_ptid, we need to set and
> +     restore it.  */
> +  struct cleanup *cleanup = save_inferior_ptid ();
> +  inferior_ptid = regcache_get_ptid (regcache);
> +

Use a scoped_restore for all these?  Like:

  scoped_restore save_ptid = make_scoped_restore (&inferior_ptid,
						  regcache_get_ptid (regcache));

Thanks,
Pedro Alves
  
Simon Marchi March 20, 2017, 9:50 p.m. UTC | #2
On 2017-03-20 11:54, Pedro Alves wrote:
> On 03/18/2017 05:08 PM, Simon Marchi wrote:
>> --- a/gdb/spu-linux-nat.c
>> +++ b/gdb/spu-linux-nat.c
>> @@ -492,9 +492,17 @@ spu_fetch_inferior_registers (struct target_ops 
>> *ops,
>>    int fd;
>>    ULONGEST addr;
>> 
>> +  /* Since we use functions that rely on inferior_ptid, we need to 
>> set and
>> +     restore it.  */
>> +  struct cleanup *cleanup = save_inferior_ptid ();
>> +  inferior_ptid = regcache_get_ptid (regcache);
>> +
> 
> Use a scoped_restore for all these?  Like:
> 
>   scoped_restore save_ptid = make_scoped_restore (&inferior_ptid,
> 						  regcache_get_ptid (regcache));

Good point.  I almost made an RAII equivalent of save_inferior_ptid, but 
then decided against it, based on the fact that we don't want to 
encourage people to use it :).  But a scoped_restore is a better choice 
in any case.  I'll send an updated patch.

Thanks,

Simon
  

Patch

diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c
index c5b91222c1..5ec7b65ad9 100644
--- a/gdb/spu-linux-nat.c
+++ b/gdb/spu-linux-nat.c
@@ -492,9 +492,17 @@  spu_fetch_inferior_registers (struct target_ops *ops,
   int fd;
   ULONGEST addr;
 
+  /* Since we use functions that rely on inferior_ptid, we need to set and
+     restore it.  */
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = regcache_get_ptid (regcache);
+
   /* We must be stopped on a spu_run system call.  */
   if (!parse_spufs_run (&fd, &addr))
-    return;
+    {
+      do_cleanups (cleanup);
+      return;
+    }
 
   /* The ID register holds the spufs file handle.  */
   if (regno == -1 || regno == SPU_ID_REGNUM)
@@ -529,6 +537,8 @@  spu_fetch_inferior_registers (struct target_ops *ops,
 	for (i = 0; i < SPU_NUM_GPRS; i++)
 	  regcache_raw_supply (regcache, i, buf + i*16);
     }
+
+  do_cleanups (cleanup);
 }
 
 /* Override the store_inferior_register routine.  */
@@ -539,9 +549,17 @@  spu_store_inferior_registers (struct target_ops *ops,
   int fd;
   ULONGEST addr;
 
+  /* Since we use functions that rely on inferior_ptid, we need to set and
+     restore it.  */
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = regcache_get_ptid (regcache);
+
   /* We must be stopped on a spu_run system call.  */
   if (!parse_spufs_run (&fd, &addr))
-    return;
+    {
+      do_cleanups (cleanup);
+      return;
+    }
 
   /* The NPC register is found at ADDR.  */
   if (regno == -1 || regno == SPU_PC_REGNUM)
@@ -565,6 +583,8 @@  spu_store_inferior_registers (struct target_ops *ops,
       xsnprintf (annex, sizeof annex, "%d/regs", fd);
       spu_proc_xfer_spu (annex, NULL, buf, 0, sizeof buf, &len);
     }
+
+  do_cleanups (cleanup);
 }
 
 /* Override the to_xfer_partial routine.  */
diff --git a/gdb/spu-multiarch.c b/gdb/spu-multiarch.c
index b99a1a3de3..220464ad27 100644
--- a/gdb/spu-multiarch.c
+++ b/gdb/spu-multiarch.c
@@ -149,16 +149,25 @@  spu_fetch_registers (struct target_ops *ops,
   int spufs_fd;
   CORE_ADDR spufs_addr;
 
+  /* Since we use functions that rely on inferior_ptid, we need to set and
+     restore it.  */
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = regcache_get_ptid (regcache);
+
   /* This version applies only if we're currently in spu_run.  */
   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
     {
       ops_beneath->to_fetch_registers (ops_beneath, regcache, regno);
+      do_cleanups (cleanup);
       return;
     }
 
   /* We must be stopped on a spu_run system call.  */
   if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
-    return;
+    {
+      do_cleanups (cleanup);
+      return;
+    }
 
   /* The ID register holds the spufs file handle.  */
   if (regno == -1 || regno == SPU_ID_REGNUM)
@@ -191,6 +200,8 @@  spu_fetch_registers (struct target_ops *ops,
 	for (i = 0; i < SPU_NUM_GPRS; i++)
 	  regcache_raw_supply (regcache, i, buf + i*16);
     }
+
+  do_cleanups (cleanup);
 }
 
 /* Override the to_store_registers routine.  */
@@ -203,16 +214,25 @@  spu_store_registers (struct target_ops *ops,
   int spufs_fd;
   CORE_ADDR spufs_addr;
 
+  /* Since we use functions that rely on inferior_ptid, we need to set and
+     restore it.  */
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = regcache_get_ptid (regcache);
+
   /* This version applies only if we're currently in spu_run.  */
   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
     {
       ops_beneath->to_store_registers (ops_beneath, regcache, regno);
+      do_cleanups (cleanup);
       return;
     }
 
   /* We must be stopped on a spu_run system call.  */
   if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
-    return;
+    {
+      do_cleanups (cleanup);
+      return;
+    }
 
   /* The NPC register is found in PPC memory at SPUFS_ADDR.  */
   if (regno == -1 || regno == SPU_PC_REGNUM)
@@ -238,6 +258,8 @@  spu_store_registers (struct target_ops *ops,
       target_write (ops_beneath, TARGET_OBJECT_SPU, annex,
 		    buf, 0, sizeof buf);
     }
+
+  do_cleanups (cleanup);
 }
 
 /* Override the to_xfer_partial routine.  */