public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 2/3] spu: Use ptid from regcache instead of inferior_ptid
  2017-03-18 17:08 [PATCH 1/3] Use ptid from regcache in almost all remaining nat files Simon Marchi
@ 2017-03-18 17:08 ` Simon Marchi
  2017-03-20 15:54   ` Pedro Alves
  2017-03-18 17:08 ` [PATCH 3/3] windows: Use ptid from regcache in register fetch/store Simon Marchi
  2017-03-20 15:54 ` [PATCH 1/3] Use ptid from regcache in almost all remaining nat files Pedro Alves
  2 siblings, 1 reply; 15+ messages in thread
From: Simon Marchi @ 2017-03-18 17:08 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

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);
+
   /* 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.  */
-- 
2.12.0

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH 1/3] Use ptid from regcache in almost all remaining nat files
@ 2017-03-18 17:08 Simon Marchi
  2017-03-18 17:08 ` [PATCH 2/3] spu: Use ptid from regcache instead of inferior_ptid Simon Marchi
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Simon Marchi @ 2017-03-18 17:08 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

This patch contains almost all of the remaining changes needed to make
to_fetch_registers/to_store_registers/to_prepare_to_store independent of
inferior_ptid.  It contains only some "trivial" changes, the more
complicated ones are in separate patches.

	* i386-linux-nat.c (fetch_register, store_register,
	i386_linux_fetch_inferior_registers,
	i386_linux_store_inferior_registers): Use ptid from regcache.
	* ia64-linux-nat.c (ia64_linux_fetch_register,
	ia64_linux_store_register): Likewise.
	* inf-ptrace.c (inf_ptrace_fetch_register,
	inf_ptrace_store_register): Likewise.
	* m32r-linux-nat.c (m32r_linux_fetch_inferior_registers,
	m32r_linux_store_inferior_registers): Likewise.
	* m68k-bsd-nat.c (m68kbsd_fetch_inferior_registers,
	m68kbsd_store_inferior_registers): Likewise.
	* m68k-linux-nat.c (fetch_register, store_register,
	m68k_linux_fetch_inferior_registers,
	m68k_linux_store_inferior_registers): Likewise.
	* m88k-bsd-nat.c (m88kbsd_fetch_inferior_registers,
	m88kbsd_store_inferior_registers): Likewise.
	* mips-fbsd-nat.c (mips_fbsd_fetch_inferior_registers,
	mips_fbsd_store_inferior_registers): Likewise.
	* mips-linux-nat.c (mips64_linux_regsets_fetch_registers,
	mips64_linux_regsets_store_registers): Likewise.
	* mips-nbsd-nat.c (mipsnbsd_fetch_inferior_registers,
	mipsnbsd_store_inferior_registers): Likewise.
	* mips-obsd-nat.c (mips64obsd_fetch_inferior_registers,
	mips64obsd_store_inferior_registers): Likewise.
	* nto-procfs.c (procfs_fetch_registers, procfs_store_registers):
	Likewise.
	* ppc-fbsd-nat.c (ppcfbsd_fetch_inferior_registers,
	ppcfbsd_store_inferior_registers): Likewise.
	* ppc-linux-nat.c (ppc_linux_fetch_inferior_registers,
	ppc_linux_store_inferior_registers): Likewise.
	* ppc-nbsd-nat.c (ppcnbsd_fetch_inferior_registers,
	ppcnbsd_store_inferior_registers): Likewise.
	* ppc-obsd-nat.c (ppcobsd_fetch_registers,
	ppcobsd_store_registers): Likewise.
	* procfs.c (procfs_fetch_registers, procfs_store_registers):
	Likewise.
	* ravenscar-thread.c (ravenscar_fetch_registers,
	ravenscar_store_registers, ravenscar_prepare_to_store):
	Likewise.
	* record-btrace.c (record_btrace_fetch_registers,
	record_btrace_store_registers, record_btrace_prepare_to_store):
	Likewise.
	* remote-sim.c (gdbsim_fetch_register, gdbsim_store_register):
	Lookup inferior using ptid from regcache, instead of
	current_inferior.
	* remote.c (remote_fetch_registers, remote_store_registers): Use
	ptid from regcache.
	* rs6000-nat.c (fetch_register, store_register): Likewise.
	* s390-linux-nat.c (s390_linux_fetch_inferior_registers,
	s390_linux_store_inferior_registers): Likewise.
	* sh-nbsd-nat.c (shnbsd_fetch_inferior_registers,
	shnbsd_store_inferior_registers): Likewise.
	* sol-thread.c (sol_thread_fetch_registers,
	sol_thread_store_registers): Likewise.
	* sparc-nat.c (sparc_fetch_inferior_registers,
	sparc_store_inferior_registers): Likewise.
	* tilegx-linux-nat.c (fetch_inferior_registers,
	store_inferior_registers): Likewise.
	* vax-bsd-nat.c (vaxbsd_fetch_inferior_registers,
	vaxbsd_store_inferior_registers): Likewise.
	* xtensa-linux-nat.c (fetch_gregs, store_gregs, fetch_xtregs,
	store_xtregs): Likewise.
---
 gdb/i386-linux-nat.c   | 29 +++++++++--------------------
 gdb/ia64-linux-nat.c   | 20 ++++++++------------
 gdb/inf-ptrace.c       | 18 ++++++------------
 gdb/m32r-linux-nat.c   | 14 +++-----------
 gdb/m68k-bsd-nat.c     | 22 ++++++++++------------
 gdb/m68k-linux-nat.c   | 32 ++++++++------------------------
 gdb/m88k-bsd-nat.c     |  6 +++---
 gdb/mips-fbsd-nat.c    | 22 ++++++++++------------
 gdb/mips-linux-nat.c   |  9 +++------
 gdb/mips-nbsd-nat.c    | 22 ++++++++++------------
 gdb/mips64-obsd-nat.c  | 11 +++++------
 gdb/nto-procfs.c       |  7 ++++---
 gdb/ppc-fbsd-nat.c     | 20 ++++++++------------
 gdb/ppc-linux-nat.c    | 15 +++------------
 gdb/ppc-nbsd-nat.c     | 20 ++++++++------------
 gdb/ppc-obsd-nat.c     | 20 ++++++++------------
 gdb/procfs.c           | 14 ++++++++------
 gdb/ravenscar-thread.c | 15 +++++++++------
 gdb/record-btrace.c    |  6 +++---
 gdb/remote-sim.c       |  6 ++++--
 gdb/remote.c           |  4 ++--
 gdb/rs6000-nat.c       | 18 ++++++++----------
 gdb/s390-linux-nat.c   |  5 +++--
 gdb/sh-nbsd-nat.c      | 10 +++++++---
 gdb/sol-thread.c       | 14 ++++++++------
 gdb/sparc-nat.c        | 12 ++++--------
 gdb/tilegx-linux-nat.c | 13 +++----------
 gdb/vax-bsd-nat.c      | 11 +++++------
 gdb/xtensa-linux-nat.c |  8 ++++----
 29 files changed, 174 insertions(+), 249 deletions(-)

diff --git a/gdb/i386-linux-nat.c b/gdb/i386-linux-nat.c
index 2c4963b5f4..b08470f305 100644
--- a/gdb/i386-linux-nat.c
+++ b/gdb/i386-linux-nat.c
@@ -36,6 +36,7 @@
 #include "linux-nat.h"
 #include "x86-linux-nat.h"
 #include "nat/linux-ptrace.h"
+#include "inf-ptrace.h"
 
 /* The register sets used in GNU/Linux ELF core-dumps are identical to
    the register sets in `struct user' that is used for a.out
@@ -94,7 +95,7 @@ int have_ptrace_getfpxregs =
 static void
 fetch_register (struct regcache *regcache, int regno)
 {
-  int tid;
+  pid_t tid;
   int val;
 
   gdb_assert (!have_ptrace_getregs);
@@ -104,10 +105,7 @@ fetch_register (struct regcache *regcache, int regno)
       return;
     }
 
-  /* GNU/Linux LWP ID's are process ID's.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
+  tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   errno = 0;
   val = ptrace (PTRACE_PEEKUSER, tid,
@@ -125,17 +123,14 @@ fetch_register (struct regcache *regcache, int regno)
 static void
 store_register (const struct regcache *regcache, int regno)
 {
-  int tid;
+  pid_t tid;
   int val;
 
   gdb_assert (!have_ptrace_getregs);
   if (i386_linux_gregset_reg_offset[regno] == -1)
     return;
 
-  /* GNU/Linux LWP ID's are process ID's.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
+  tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   errno = 0;
   regcache_raw_collect (regcache, regno, &val);
@@ -455,7 +450,7 @@ static void
 i386_linux_fetch_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regno)
 {
-  int tid;
+  pid_t tid;
 
   /* Use the old method of peeking around in `struct user' if the
      GETREGS request isn't available.  */
@@ -470,10 +465,7 @@ i386_linux_fetch_inferior_registers (struct target_ops *ops,
       return;
     }
 
-  /* GNU/Linux LWP ID's are process ID's.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
+  tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   /* Use the PTRACE_GETFPXREGS request whenever possible, since it
      transfers more registers in one system call, and we'll cache the
@@ -536,7 +528,7 @@ static void
 i386_linux_store_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regno)
 {
-  int tid;
+  pid_t tid;
 
   /* Use the old method of poking around in `struct user' if the
      SETREGS request isn't available.  */
@@ -551,10 +543,7 @@ i386_linux_store_inferior_registers (struct target_ops *ops,
       return;
     }
 
-  /* GNU/Linux LWP ID's are process ID's.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */
+  tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
      transfers more registers in one system call.  But remember that
diff --git a/gdb/ia64-linux-nat.c b/gdb/ia64-linux-nat.c
index e26dd15025..a532b98385 100644
--- a/gdb/ia64-linux-nat.c
+++ b/gdb/ia64-linux-nat.c
@@ -41,6 +41,8 @@
 /* Prototypes for supply_gregset etc.  */
 #include "gregset.h"
 
+#include "inf-ptrace.h"
+
 /* These must match the order of the register names.
 
    Some sort of lookup table is needed because the offsets associated
@@ -696,7 +698,8 @@ ia64_linux_fetch_register (struct regcache *regcache, int regnum)
   CORE_ADDR addr;
   size_t size;
   PTRACE_TYPE_RET *buf;
-  int pid, i;
+  pid_t pid;
+  int i;
 
   /* r0 cannot be fetched but is always zero.  */
   if (regnum == IA64_GR0_REGNUM)
@@ -735,11 +738,7 @@ ia64_linux_fetch_register (struct regcache *regcache, int regnum)
       return;
     }
 
-  /* Cater for systems like GNU/Linux, that implement threads as
-     separate processes.  */
-  pid = ptid_get_lwp (inferior_ptid);
-  if (pid == 0)
-    pid = ptid_get_pid (inferior_ptid);
+  pid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   /* This isn't really an address, but ptrace thinks of it as one.  */
   addr = ia64_register_addr (gdbarch, regnum);
@@ -788,16 +787,13 @@ ia64_linux_store_register (const struct regcache *regcache, int regnum)
   CORE_ADDR addr;
   size_t size;
   PTRACE_TYPE_RET *buf;
-  int pid, i;
+  pid_t pid;
+  int i;
 
   if (ia64_cannot_store_register (gdbarch, regnum))
     return;
 
-  /* Cater for systems like GNU/Linux, that implement threads as
-     separate processes.  */
-  pid = ptid_get_lwp (inferior_ptid);
-  if (pid == 0)
-    pid = ptid_get_pid (inferior_ptid);
+  pid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   /* This isn't really an address, but ptrace thinks of it as one.  */
   addr = ia64_register_addr (gdbarch, regnum);
diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index 431a36b8c7..61d24269a8 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -708,7 +708,8 @@ inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
   CORE_ADDR addr;
   size_t size;
   PTRACE_TYPE_RET *buf;
-  int pid, i;
+  pid_t pid;
+  int i;
 
   /* This isn't really an address, but ptrace thinks of it as one.  */
   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
@@ -719,11 +720,7 @@ inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
       return;
     }
 
-  /* Cater for systems like GNU/Linux, that implement threads as
-     separate processes.  */
-  pid = ptid_get_lwp (inferior_ptid);
-  if (pid == 0)
-    pid = ptid_get_pid (inferior_ptid);
+  pid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   size = register_size (gdbarch, regnum);
   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
@@ -769,7 +766,8 @@ inf_ptrace_store_register (const struct regcache *regcache, int regnum)
   CORE_ADDR addr;
   size_t size;
   PTRACE_TYPE_RET *buf;
-  int pid, i;
+  pid_t pid;
+  int i;
 
   /* This isn't really an address, but ptrace thinks of it as one.  */
   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
@@ -777,11 +775,7 @@ inf_ptrace_store_register (const struct regcache *regcache, int regnum)
       || gdbarch_cannot_store_register (gdbarch, regnum))
     return;
 
-  /* Cater for systems like GNU/Linux, that implement threads as
-     separate processes.  */
-  pid = ptid_get_lwp (inferior_ptid);
-  if (pid == 0)
-    pid = ptid_get_pid (inferior_ptid);
+  pid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   size = register_size (gdbarch, regnum);
   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
diff --git a/gdb/m32r-linux-nat.c b/gdb/m32r-linux-nat.c
index 9d9bef385e..a33de23dc3 100644
--- a/gdb/m32r-linux-nat.c
+++ b/gdb/m32r-linux-nat.c
@@ -26,6 +26,7 @@
 #include "nat/gdb_ptrace.h"
 #include <sys/user.h>
 #include <sys/procfs.h>
+#include "inf-ptrace.h"
 
 /* Prototypes for supply_gregset etc.  */
 #include "gregset.h"
@@ -194,12 +195,7 @@ static void
 m32r_linux_fetch_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regno)
 {
-  int tid;
-
-  /* GNU/Linux LWP ID's are process ID's.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);	/* Not a threaded program.  */
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   /* Use the PTRACE_GETREGS request whenever possible, since it
      transfers more registers in one system call, and we'll cache the
@@ -221,11 +217,7 @@ static void
 m32r_linux_store_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regno)
 {
-  int tid;
-
-  /* GNU/Linux LWP ID's are process ID's.  */
-  if ((tid = ptid_get_lwp (inferior_ptid)) == 0)
-    tid = ptid_get_pid (inferior_ptid);	/* Not a threaded program.  */
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   /* Use the PTRACE_SETREGS request whenever possible, since it
      transfers more registers in one system call.  */
diff --git a/gdb/m68k-bsd-nat.c b/gdb/m68k-bsd-nat.c
index 227da382e6..e0aee05ce5 100644
--- a/gdb/m68k-bsd-nat.c
+++ b/gdb/m68k-bsd-nat.c
@@ -111,12 +111,13 @@ static void
 m68kbsd_fetch_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int regnum)
 {
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
+
   if (regnum == -1 || m68kbsd_gregset_supplies_p (regnum))
     {
       struct reg regs;
 
-      if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));
 
       m68kbsd_supply_gregset (regcache, &regs);
@@ -126,8 +127,7 @@ m68kbsd_fetch_inferior_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get floating point status"));
 
       m68kbsd_supply_fpregset (regcache, &fpregs);
@@ -141,18 +141,18 @@ static void
 m68kbsd_store_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int regnum)
 {
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
+
   if (regnum == -1 || m68kbsd_gregset_supplies_p (regnum))
     {
       struct reg regs;
 
-      if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-                  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
         perror_with_name (_("Couldn't get registers"));
 
       m68kbsd_collect_gregset (regcache, &regs, regnum);
 
-      if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
-	          (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
         perror_with_name (_("Couldn't write registers"));
     }
 
@@ -160,14 +160,12 @@ m68kbsd_store_inferior_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get floating point status"));
 
       m68kbsd_collect_fpregset (regcache, &fpregs, regnum);
 
-      if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't write floating point status"));
     }
 }
diff --git a/gdb/m68k-linux-nat.c b/gdb/m68k-linux-nat.c
index e5182caf39..eb14375322 100644
--- a/gdb/m68k-linux-nat.c
+++ b/gdb/m68k-linux-nat.c
@@ -51,6 +51,8 @@
 /* Defines ps_err_e, struct ps_prochandle.  */
 #include "gdb_proc_service.h"
 
+#include "inf-ptrace.h"
+
 #ifndef PTRACE_GET_THREAD_AREA
 #define PTRACE_GET_THREAD_AREA 25
 #endif
@@ -106,13 +108,7 @@ fetch_register (struct regcache *regcache, int regno)
   long regaddr, val;
   int i;
   gdb_byte buf[M68K_MAX_REGISTER_SIZE];
-  int tid;
-
-  /* Overload thread id onto process id.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);	/* no thread id, just use
-					   process id.  */
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   regaddr = 4 * regmap[regno];
   for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
@@ -159,14 +155,8 @@ store_register (const struct regcache *regcache, int regno)
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   long regaddr, val;
   int i;
-  int tid;
   gdb_byte buf[M68K_MAX_REGISTER_SIZE];
-
-  /* Overload thread id onto process id.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);	/* no thread id, just use
-					   process id.  */
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   regaddr = 4 * regmap[regno];
 
@@ -406,7 +396,7 @@ static void
 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regno)
 {
-  int tid;
+  pid_t tid;
 
   /* Use the old method of peeking around in `struct user' if the
      GETREGS request isn't available.  */
@@ -416,10 +406,7 @@ m68k_linux_fetch_inferior_registers (struct target_ops *ops,
       return;
     }
 
-  /* GNU/Linux LWP ID's are process ID's.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);	/* Not a threaded program.  */
+  tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   /* Use the PTRACE_GETFPXREGS request whenever possible, since it
      transfers more registers in one system call, and we'll cache the
@@ -463,7 +450,7 @@ static void
 m68k_linux_store_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regno)
 {
-  int tid;
+  pid_t tid;
 
   /* Use the old method of poking around in `struct user' if the
      SETREGS request isn't available.  */
@@ -473,10 +460,7 @@ m68k_linux_store_inferior_registers (struct target_ops *ops,
       return;
     }
 
-  /* GNU/Linux LWP ID's are process ID's.  */
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);	/* Not a threaded program.  */
+  tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   /* Use the PTRACE_SETFPREGS requests whenever possible, since it
      transfers more registers in one system call.  But remember that
diff --git a/gdb/m88k-bsd-nat.c b/gdb/m88k-bsd-nat.c
index 72d3630710..9549b8d3a2 100644
--- a/gdb/m88k-bsd-nat.c
+++ b/gdb/m88k-bsd-nat.c
@@ -68,7 +68,7 @@ m88kbsd_fetch_inferior_registers (struct target_ops *ops,
 {
   struct reg regs;
 
-  if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
+  if (ptrace (PT_GETREGS, ptid_get_pid (regcache_get_ptid (regcache)),
 	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
@@ -84,13 +84,13 @@ m88kbsd_store_inferior_registers (struct target_ops *ops,
 {
   struct reg regs;
 
-  if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
+  if (ptrace (PT_GETREGS, ptid_get_pid (regcache_get_ptid (regcache)),
 	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   m88kbsd_collect_gregset (regcache, &regs, regnum);
 
-  if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
+  if (ptrace (PT_SETREGS, ptid_get_pid (regcache_get_ptid (regcache)),
 	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't write registers"));
 }
diff --git a/gdb/mips-fbsd-nat.c b/gdb/mips-fbsd-nat.c
index 0472df88f4..078df52db6 100644
--- a/gdb/mips-fbsd-nat.c
+++ b/gdb/mips-fbsd-nat.c
@@ -47,13 +47,14 @@ static void
 mips_fbsd_fetch_inferior_registers (struct target_ops *ops,
 				    struct regcache *regcache, int regnum)
 {
+  pid_t pid = get_ptrace_pid (regcache_get_ptid (regcache));
+
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   if (regnum == -1 || getregs_supplies (gdbarch, regnum))
     {
       struct reg regs;
 
-      if (ptrace (PT_GETREGS, get_ptrace_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));
 
       mips_fbsd_supply_gregs (regcache, regnum, &regs, sizeof (register_t));
@@ -66,8 +67,7 @@ mips_fbsd_fetch_inferior_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, get_ptrace_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get floating point status"));
 
       mips_fbsd_supply_fpregs (regcache, regnum, &fpregs,
@@ -82,20 +82,20 @@ static void
 mips_fbsd_store_inferior_registers (struct target_ops *ops,
 				    struct regcache *regcache, int regnum)
 {
+  pid_t pid = get_ptrace_pid (regcache_get_ptid (regcache));
+
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   if (regnum == -1 || getregs_supplies (gdbarch, regnum))
     {
       struct reg regs;
 
-      if (ptrace (PT_GETREGS, get_ptrace_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));
 
       mips_fbsd_collect_gregs (regcache, regnum, (char *) &regs,
 			       sizeof (register_t));
 
-      if (ptrace (PT_SETREGS, get_ptrace_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't write registers"));
 
       if (regnum != -1)
@@ -107,15 +107,13 @@ mips_fbsd_store_inferior_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, get_ptrace_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get floating point status"));
 
       mips_fbsd_collect_fpregs (regcache, regnum, (char *) &fpregs,
 				sizeof (f_register_t));
 
-      if (ptrace (PT_SETFPREGS, get_ptrace_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't write floating point status"));
     }
 }
diff --git a/gdb/mips-linux-nat.c b/gdb/mips-linux-nat.c
index 9d22773e9c..9596b49099 100644
--- a/gdb/mips-linux-nat.c
+++ b/gdb/mips-linux-nat.c
@@ -34,6 +34,7 @@
 #include <sgidefs.h>
 #include "nat/gdb_ptrace.h"
 #include <asm/ptrace.h>
+#include "inf-ptrace.h"
 
 #include "nat/mips-linux-watch.h"
 
@@ -244,9 +245,7 @@ mips64_linux_regsets_fetch_registers (struct target_ops *ops,
   else
     is_dsp = 0;
 
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);
+  tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (regno == -1 || (!is_fp && !is_dsp))
     {
@@ -332,9 +331,7 @@ mips64_linux_regsets_store_registers (struct target_ops *ops,
   else
     is_dsp = 0;
 
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);
+  tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (regno == -1 || (!is_fp && !is_dsp))
     {
diff --git a/gdb/mips-nbsd-nat.c b/gdb/mips-nbsd-nat.c
index b7c19dc85f..41e67b7ff5 100644
--- a/gdb/mips-nbsd-nat.c
+++ b/gdb/mips-nbsd-nat.c
@@ -42,13 +42,14 @@ static void
 mipsnbsd_fetch_inferior_registers (struct target_ops *ops,
 				   struct regcache *regcache, int regno)
 {
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
+
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   if (regno == -1 || getregs_supplies (gdbarch, regno))
     {
       struct reg regs;
 
-      if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));
       
       mipsnbsd_supply_reg (regcache, (char *) &regs, regno);
@@ -61,8 +62,7 @@ mipsnbsd_fetch_inferior_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get floating point status"));
 
       mipsnbsd_supply_fpreg (regcache, (char *) &fpregs, regno);
@@ -73,19 +73,19 @@ static void
 mipsnbsd_store_inferior_registers (struct target_ops *ops,
 				   struct regcache *regcache, int regno)
 {
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
+
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   if (regno == -1 || getregs_supplies (gdbarch, regno))
     {
       struct reg regs;
 
-      if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));
 
       mipsnbsd_fill_reg (regcache, (char *) &regs, regno);
 
-      if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid), 
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't write registers"));
 
       if (regno != -1)
@@ -97,14 +97,12 @@ mipsnbsd_store_inferior_registers (struct target_ops *ops,
     {
       struct fpreg fpregs; 
 
-      if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get floating point status"));
 
       mipsnbsd_fill_fpreg (regcache, (char *) &fpregs, regno);
 
-      if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't write floating point status"));
     }
 }
diff --git a/gdb/mips64-obsd-nat.c b/gdb/mips64-obsd-nat.c
index 97af421981..d32af80da0 100644
--- a/gdb/mips64-obsd-nat.c
+++ b/gdb/mips64-obsd-nat.c
@@ -82,9 +82,9 @@ mips64obsd_fetch_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regnum)
 {
   struct reg regs;
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
-  if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   mips64obsd_supply_gregset (regcache, &regs);
@@ -98,15 +98,14 @@ mips64obsd_store_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regnum)
 {
   struct reg regs;
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
-  if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   mips64obsd_collect_gregset (regcache, &regs, regnum);
 
-  if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't write registers"));
 }
 \f
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index 13b5ccf99f..75f92dcf78 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -868,7 +868,7 @@ procfs_fetch_registers (struct target_ops *ops,
   reg;
   int regsize;
 
-  procfs_set_thread (inferior_ptid);
+  procfs_set_thread (regcache_get_ptid (regcache));
   if (devctl (ctl_fd, DCMD_PROC_GETGREG, &reg, sizeof (reg), &regsize) == EOK)
     nto_supply_gregset (regcache, (char *) &reg.greg);
   if (devctl (ctl_fd, DCMD_PROC_GETFPREG, &reg, sizeof (reg), &regsize)
@@ -1350,10 +1350,11 @@ procfs_store_registers (struct target_ops *ops,
   unsigned off;
   int len, regset, regsize, dev_set, err;
   char *data;
+  ptid_t ptid = regcache_get_ptid (regcache);
 
-  if (ptid_equal (inferior_ptid, null_ptid))
+  if (ptid_equal (ptid, null_ptid))
     return;
-  procfs_set_thread (inferior_ptid);
+  procfs_set_thread (ptid);
 
   if (regno == -1)
     {
diff --git a/gdb/ppc-fbsd-nat.c b/gdb/ppc-fbsd-nat.c
index 823c063643..ae5877af9f 100644
--- a/gdb/ppc-fbsd-nat.c
+++ b/gdb/ppc-fbsd-nat.c
@@ -120,9 +120,9 @@ ppcfbsd_fetch_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int regno)
 {
   gdb_gregset_t regs;
+  pid_t pid = ptid_get_lwp (regcache_get_ptid (regcache));
 
-  if (ptrace (PT_GETREGS, ptid_get_lwp (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   supply_gregset (regcache, &regs);
@@ -132,8 +132,7 @@ ppcfbsd_fetch_inferior_registers (struct target_ops *ops,
       const struct regset *fpregset = ppc_fbsd_fpregset ();
       gdb_fpregset_t fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_lwp (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get FP registers"));
 
       ppc_supply_fpregset (fpregset, regcache, regno, &fpregs, sizeof fpregs);
@@ -148,29 +147,26 @@ ppcfbsd_store_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int regno)
 {
   gdb_gregset_t regs;
+  pid_t pid = ptid_get_lwp (regcache_get_ptid (regcache));
 
-  if (ptrace (PT_GETREGS, ptid_get_lwp (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   fill_gregset (regcache, &regs, regno);
 
-  if (ptrace (PT_SETREGS, ptid_get_lwp (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't write registers"));
 
   if (regno == -1 || getfpregs_supplies (get_regcache_arch (regcache), regno))
     {
       gdb_fpregset_t fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_lwp (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get FP registers"));
 
       fill_fpregset (regcache, &fpregs, regno);
 
-      if (ptrace (PT_SETFPREGS, ptid_get_lwp (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't set FP registers"));
     }
 }
diff --git a/gdb/ppc-linux-nat.c b/gdb/ppc-linux-nat.c
index a56d154d31..142fa315b0 100644
--- a/gdb/ppc-linux-nat.c
+++ b/gdb/ppc-linux-nat.c
@@ -34,6 +34,7 @@
 #include <fcntl.h>
 #include <sys/procfs.h>
 #include "nat/gdb_ptrace.h"
+#include "inf-ptrace.h"
 
 /* Prototypes for supply_gregset etc.  */
 #include "gregset.h"
@@ -809,12 +810,7 @@ static void
 ppc_linux_fetch_inferior_registers (struct target_ops *ops,
 				    struct regcache *regcache, int regno)
 {
-  /* Overload thread id onto process id.  */
-  int tid = ptid_get_lwp (inferior_ptid);
-
-  /* No thread id, just use process id.  */
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (regno == -1)
     fetch_ppc_registers (regcache, tid);
@@ -2291,12 +2287,7 @@ static void
 ppc_linux_store_inferior_registers (struct target_ops *ops,
 				    struct regcache *regcache, int regno)
 {
-  /* Overload thread id onto process id.  */
-  int tid = ptid_get_lwp (inferior_ptid);
-
-  /* No thread id, just use process id.  */
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (regno >= 0)
     store_register (regcache, tid, regno);
diff --git a/gdb/ppc-nbsd-nat.c b/gdb/ppc-nbsd-nat.c
index 21d927cc14..b170583f2a 100644
--- a/gdb/ppc-nbsd-nat.c
+++ b/gdb/ppc-nbsd-nat.c
@@ -81,13 +81,13 @@ ppcnbsd_fetch_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int regnum)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
   if (regnum == -1 || getregs_supplies (gdbarch, regnum))
     {
       struct reg regs;
 
-      if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
         perror_with_name (_("Couldn't get registers"));
 
       ppc_supply_gregset (&ppcnbsd_gregset, regcache,
@@ -98,8 +98,7 @@ ppcnbsd_fetch_inferior_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get FP registers"));
 
       ppc_supply_fpregset (&ppcnbsd_fpregset, regcache,
@@ -112,20 +111,19 @@ ppcnbsd_store_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int regnum)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
   if (regnum == -1 || getregs_supplies (gdbarch, regnum))
     {
       struct reg regs;
 
-      if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));
 
       ppc_collect_gregset (&ppcnbsd_gregset, regcache,
 			   regnum, &regs, sizeof regs);
 
-      if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't write registers"));
     }
 
@@ -133,15 +131,13 @@ ppcnbsd_store_inferior_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get FP registers"));
 
       ppc_collect_fpregset (&ppcnbsd_fpregset, regcache,
 			    regnum, &fpregs, sizeof fpregs);
 
-      if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't set FP registers"));
     }
 }
diff --git a/gdb/ppc-obsd-nat.c b/gdb/ppc-obsd-nat.c
index 1a39e46295..20360f53c3 100644
--- a/gdb/ppc-obsd-nat.c
+++ b/gdb/ppc-obsd-nat.c
@@ -75,9 +75,9 @@ ppcobsd_fetch_registers (struct target_ops *ops,
 			 struct regcache *regcache, int regnum)
 {
   struct reg regs;
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
-  if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   ppc_supply_gregset (&ppcobsd_gregset, regcache, -1,
@@ -93,8 +93,7 @@ ppcobsd_fetch_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get floating point status"));
 
       ppc_supply_fpregset (&ppcobsd_fpregset, regcache, -1,
@@ -111,9 +110,9 @@ ppcobsd_store_registers (struct target_ops *ops,
 			 struct regcache *regcache, int regnum)
 {
   struct reg regs;
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
-  if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   ppc_collect_gregset (&ppcobsd_gregset, regcache,
@@ -123,8 +122,7 @@ ppcobsd_store_registers (struct target_ops *ops,
 			regnum, &regs, sizeof regs);
 #endif
 
-  if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't write registers"));
 
 #ifdef PT_GETFPREGS
@@ -133,15 +131,13 @@ ppcobsd_store_registers (struct target_ops *ops,
     {
       struct fpreg fpregs;
 
-      if (ptrace (PT_GETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't get floating point status"));
 
       ppc_collect_fpregset (&ppcobsd_fpregset, regcache,
 			    regnum, &fpregs, sizeof fpregs);
 
-      if (ptrace (PT_SETFPREGS, ptid_get_pid (inferior_ptid),
-		  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+      if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	perror_with_name (_("Couldn't write floating point status"));
     }
 #endif
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 2269016ce8..b860f1699e 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3219,15 +3219,16 @@ procfs_fetch_registers (struct target_ops *ops,
 {
   gdb_gregset_t *gregs;
   procinfo *pi;
-  int pid = ptid_get_pid (inferior_ptid);
-  int tid = ptid_get_lwp (inferior_ptid);
+  ptid_t ptid = regcache_get_ptid (regcache);
+  int pid = ptid_get_pid (ptid);
+  int tid = ptid_get_lwp (ptid);
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
 
   pi = find_procinfo_or_die (pid, tid);
 
   if (pi == NULL)
     error (_("procfs: fetch_registers failed to find procinfo for %s"),
-	   target_pid_to_str (inferior_ptid));
+	   target_pid_to_str (ptid));
 
   gregs = proc_get_gregs (pi);
   if (gregs == NULL)
@@ -3268,15 +3269,16 @@ procfs_store_registers (struct target_ops *ops,
 {
   gdb_gregset_t *gregs;
   procinfo *pi;
-  int pid = ptid_get_pid (inferior_ptid);
-  int tid = ptid_get_lwp (inferior_ptid);
+  ptid_t ptid = regcache_get_ptid (regcache);
+  int pid = ptid_get_pid (ptid);
+  int tid = ptid_get_lwp (ptid);
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
 
   pi = find_procinfo_or_die (pid, tid);
 
   if (pi == NULL)
     error (_("procfs: store_registers: failed to find procinfo for %s"),
-	   target_pid_to_str (inferior_ptid));
+	   target_pid_to_str (ptid));
 
   gregs = proc_get_gregs (pi);
   if (gregs == NULL)
diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index d44c9810f6..d1bd97f3a6 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -268,10 +268,11 @@ ravenscar_fetch_registers (struct target_ops *ops,
                            struct regcache *regcache, int regnum)
 {
   struct target_ops *beneath = find_target_beneath (ops);
+  ptid_t ptid = regcache_get_ptid (regcache);
 
   if (!ravenscar_runtime_initialized ()
-      || ptid_equal (inferior_ptid, base_magic_null_ptid)
-      || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
+      || ptid_equal (ptid, base_magic_null_ptid)
+      || ptid_equal (ptid, ravenscar_running_thread ()))
     beneath->to_fetch_registers (beneath, regcache, regnum);
   else
     {
@@ -288,10 +289,11 @@ ravenscar_store_registers (struct target_ops *ops,
                            struct regcache *regcache, int regnum)
 {
   struct target_ops *beneath = find_target_beneath (ops);
+  ptid_t ptid = regcache_get_ptid (regcache);
 
   if (!ravenscar_runtime_initialized ()
-      || ptid_equal (inferior_ptid, base_magic_null_ptid)
-      || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
+      || ptid_equal (ptid, base_magic_null_ptid)
+      || ptid_equal (ptid, ravenscar_running_thread ()))
     beneath->to_store_registers (beneath, regcache, regnum);
   else
     {
@@ -308,10 +310,11 @@ ravenscar_prepare_to_store (struct target_ops *self,
 			    struct regcache *regcache)
 {
   struct target_ops *beneath = find_target_beneath (self);
+  ptid_t ptid = regcache_get_ptid (regcache);
 
   if (!ravenscar_runtime_initialized ()
-      || ptid_equal (inferior_ptid, base_magic_null_ptid)
-      || ptid_equal (inferior_ptid, ravenscar_running_thread ()))
+      || ptid_equal (ptid, base_magic_null_ptid)
+      || ptid_equal (ptid, ravenscar_running_thread ()))
     beneath->to_prepare_to_store (beneath, regcache);
   else
     {
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index f7683f237f..af025e2a1c 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -1425,7 +1425,7 @@ record_btrace_fetch_registers (struct target_ops *ops,
   struct btrace_insn_iterator *replay;
   struct thread_info *tp;
 
-  tp = find_thread_ptid (inferior_ptid);
+  tp = find_thread_ptid (regcache_get_ptid (regcache));
   gdb_assert (tp != NULL);
 
   replay = tp->btrace.replay;
@@ -1466,7 +1466,7 @@ record_btrace_store_registers (struct target_ops *ops,
   struct target_ops *t;
 
   if (!record_btrace_generating_corefile
-      && record_btrace_is_replaying (ops, inferior_ptid))
+      && record_btrace_is_replaying (ops, regcache_get_ptid (regcache)))
     error (_("Cannot write registers while replaying."));
 
   gdb_assert (may_write_registers != 0);
@@ -1484,7 +1484,7 @@ record_btrace_prepare_to_store (struct target_ops *ops,
   struct target_ops *t;
 
   if (!record_btrace_generating_corefile
-      && record_btrace_is_replaying (ops, inferior_ptid))
+      && record_btrace_is_replaying (ops, regcache_get_ptid (regcache)))
     return;
 
   t = ops->beneath;
diff --git a/gdb/remote-sim.c b/gdb/remote-sim.c
index b0c68c617e..0e8d65779f 100644
--- a/gdb/remote-sim.c
+++ b/gdb/remote-sim.c
@@ -429,8 +429,9 @@ gdbsim_fetch_register (struct target_ops *ops,
 		       struct regcache *regcache, int regno)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  struct inferior *inf = find_inferior_ptid (regcache_get_ptid (regcache));
   struct sim_inferior_data *sim_data
-    = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
+    = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
 
   if (regno == -1)
     {
@@ -505,8 +506,9 @@ gdbsim_store_register (struct target_ops *ops,
 		       struct regcache *regcache, int regno)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
+  struct inferior *inf = find_inferior_ptid (regcache_get_ptid (regcache));
   struct sim_inferior_data *sim_data
-    = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
+    = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
 
   if (regno == -1)
     {
diff --git a/gdb/remote.c b/gdb/remote.c
index 2f34c4c300..9099ff6ccf 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -7711,7 +7711,7 @@ remote_fetch_registers (struct target_ops *ops,
   int i;
 
   set_remote_traceframe ();
-  set_general_thread (inferior_ptid);
+  set_general_thread (regcache_get_ptid (regcache));
 
   if (regnum >= 0)
     {
@@ -7868,7 +7868,7 @@ remote_store_registers (struct target_ops *ops,
   int i;
 
   set_remote_traceframe ();
-  set_general_thread (inferior_ptid);
+  set_general_thread (regcache_get_ptid (regcache));
 
   if (regnum >= 0)
     {
diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
index f42847bfea..8f5d25cdb3 100644
--- a/gdb/rs6000-nat.c
+++ b/gdb/rs6000-nat.c
@@ -164,6 +164,7 @@ fetch_register (struct regcache *regcache, int regno)
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   int addr[MAX_REGISTER_SIZE];
   int nr, isfloat;
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
   /* Retrieved values may be -1, so infer errors from errno.  */
   errno = 0;
@@ -172,7 +173,7 @@ fetch_register (struct regcache *regcache, int regno)
 
   /* Floating-point registers.  */
   if (isfloat)
-    rs6000_ptrace32 (PT_READ_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
+    rs6000_ptrace32 (PT_READ_FPR, pid, addr, nr, 0);
 
   /* Bogus register number.  */
   else if (nr < 0)
@@ -188,15 +189,13 @@ fetch_register (struct regcache *regcache, int regno)
   else
     {
       if (!ARCH64 ())
-	*addr = rs6000_ptrace32 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
-				 (int *) nr, 0, 0);
+	*addr = rs6000_ptrace32 (PT_READ_GPR, pid, (int *) nr, 0, 0);
       else
 	{
 	  /* PT_READ_GPR requires the buffer parameter to point to long long,
 	     even if the register is really only 32 bits.  */
 	  long long buf;
-	  rs6000_ptrace64 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
-			   nr, 0, &buf);
+	  rs6000_ptrace64 (PT_READ_GPR, pid, nr, 0, &buf);
 	  if (register_size (gdbarch, regno) == 8)
 	    memcpy (addr, &buf, 8);
 	  else
@@ -224,6 +223,7 @@ store_register (struct regcache *regcache, int regno)
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   int addr[MAX_REGISTER_SIZE];
   int nr, isfloat;
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
   /* Fetch the register's value from the register cache.  */
   regcache_raw_collect (regcache, regno, addr);
@@ -235,7 +235,7 @@ store_register (struct regcache *regcache, int regno)
 
   /* Floating-point registers.  */
   if (isfloat)
-    rs6000_ptrace32 (PT_WRITE_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
+    rs6000_ptrace32 (PT_WRITE_FPR, pid, addr, nr, 0);
 
   /* Bogus register number.  */
   else if (nr < 0)
@@ -253,8 +253,7 @@ store_register (struct regcache *regcache, int regno)
          the register's value is passed by value, but for 64-bit inferiors,
 	 the address of a buffer containing the value is passed.  */
       if (!ARCH64 ())
-	rs6000_ptrace32 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
-			 (int *) nr, *addr, 0);
+	rs6000_ptrace32 (PT_WRITE_GPR, pid, (int *) nr, *addr, 0);
       else
 	{
 	  /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
@@ -264,8 +263,7 @@ store_register (struct regcache *regcache, int regno)
 	    memcpy (&buf, addr, 8);
 	  else
 	    buf = *addr;
-	  rs6000_ptrace64 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
-			   nr, 0, &buf);
+	  rs6000_ptrace64 (PT_WRITE_GPR, pid, nr, 0, &buf);
 	}
     }
 
diff --git a/gdb/s390-linux-nat.c b/gdb/s390-linux-nat.c
index 2b205df9de..90c73c44cd 100644
--- a/gdb/s390-linux-nat.c
+++ b/gdb/s390-linux-nat.c
@@ -40,6 +40,7 @@
 #include <sys/ucontext.h>
 #include <elf.h>
 #include <algorithm>
+#include "inf-ptrace.h"
 
 /* Per-thread arch-specific data.  */
 
@@ -370,7 +371,7 @@ static void
 s390_linux_fetch_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regnum)
 {
-  int tid = s390_inferior_tid ();
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
     fetch_regs (regcache, tid);
@@ -413,7 +414,7 @@ static void
 s390_linux_store_inferior_registers (struct target_ops *ops,
 				     struct regcache *regcache, int regnum)
 {
-  int tid = s390_inferior_tid ();
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
     store_regs (regcache, tid, regnum);
diff --git a/gdb/sh-nbsd-nat.c b/gdb/sh-nbsd-nat.c
index 90c9689899..ac93dcd59c 100644
--- a/gdb/sh-nbsd-nat.c
+++ b/gdb/sh-nbsd-nat.c
@@ -45,11 +45,13 @@ static void
 shnbsd_fetch_inferior_registers (struct target_ops *ops,
 				 struct regcache *regcache, int regno)
 {
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
+
   if (regno == -1 || GETREGS_SUPPLIES (get_regcache_arch (regcache), regno))
     {
       struct reg inferior_registers;
 
-      if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
+      if (ptrace (PT_GETREGS, pid,
 		  (PTRACE_TYPE_ARG3) &inferior_registers, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));
 
@@ -66,11 +68,13 @@ static void
 shnbsd_store_inferior_registers (struct target_ops *ops,
 				 struct regcache *regcache, int regno)
 {
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
+
   if (regno == -1 || GETREGS_SUPPLIES (get_regcache_arch (regcache), regno))
     {
       struct reg inferior_registers;
 
-      if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
+      if (ptrace (PT_GETREGS, pid,
 		  (PTRACE_TYPE_ARG3) &inferior_registers, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));
 
@@ -78,7 +82,7 @@ shnbsd_store_inferior_registers (struct target_ops *ops,
 				  (char *) &inferior_registers,
 				  SHNBSD_SIZEOF_GREGS);
 
-      if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
+      if (ptrace (PT_SETREGS, pid,
 		  (PTRACE_TYPE_ARG3) &inferior_registers, 0) == -1)
 	perror_with_name (_("Couldn't set registers"));
 
diff --git a/gdb/sol-thread.c b/gdb/sol-thread.c
index a09a3ab9a8..efb3bd0b21 100644
--- a/gdb/sol-thread.c
+++ b/gdb/sol-thread.c
@@ -462,16 +462,17 @@ sol_thread_fetch_registers (struct target_ops *ops,
   gdb_gregset_t *gregset_p = &gregset;
   gdb_fpregset_t *fpregset_p = &fpregset;
   struct target_ops *beneath = find_target_beneath (ops);
+  ptid_t ptid = regcache_get_ptid (regcache);
 
-  if (!ptid_tid_p (inferior_ptid))
+  if (!ptid_tid_p (ptid))
     {
       /* It's an LWP; pass the request on to the layer beneath.  */
       beneath->to_fetch_registers (beneath, regcache, regnum);
       return;
     }
 
-  /* Solaris thread: convert INFERIOR_PTID into a td_thrhandle_t.  */
-  thread = ptid_get_tid (inferior_ptid);
+  /* Solaris thread: convert PTID into a td_thrhandle_t.  */
+  thread = ptid_get_tid (ptid);
   if (thread == 0)
     error (_("sol_thread_fetch_registers: thread == 0"));
 
@@ -514,8 +515,9 @@ sol_thread_store_registers (struct target_ops *ops,
   td_err_e val;
   prgregset_t gregset;
   prfpregset_t fpregset;
+  ptid_t ptid = regcache_get_ptid (regcache);
 
-  if (!ptid_tid_p (inferior_ptid))
+  if (!ptid_tid_p (ptid))
     {
       struct target_ops *beneath = find_target_beneath (ops);
 
@@ -524,8 +526,8 @@ sol_thread_store_registers (struct target_ops *ops,
       return;
     }
 
-  /* Solaris thread: convert INFERIOR_PTID into a td_thrhandle_t.  */
-  thread = ptid_get_tid (inferior_ptid);
+  /* Solaris thread: convert PTID into a td_thrhandle_t.  */
+  thread = ptid_get_tid (ptid);
 
   val = p_td_ta_map_id2thr (main_ta, thread, &thandle);
   if (val != TD_OK)
diff --git a/gdb/sparc-nat.c b/gdb/sparc-nat.c
index b058ccf1e7..b842f2b482 100644
--- a/gdb/sparc-nat.c
+++ b/gdb/sparc-nat.c
@@ -138,7 +138,7 @@ sparc_fetch_inferior_registers (struct target_ops *ops,
 				struct regcache *regcache, int regnum)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
-  int pid;
+  pid_t pid;
 
   /* NOTE: cagney/2002-12-03: This code assumes that the currently
      selected light weight processes' registers can be written
@@ -153,9 +153,7 @@ sparc_fetch_inferior_registers (struct target_ops *ops,
      These functions should instead be paramaterized with an explicit
      object (struct regcache, struct thread_info?) into which the LWPs
      registers can be written.  */
-  pid = ptid_get_lwp (inferior_ptid);
-  if (pid == 0)
-    pid = ptid_get_pid (inferior_ptid);
+  pid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (regnum == SPARC_G0_REGNUM)
     {
@@ -193,13 +191,11 @@ sparc_store_inferior_registers (struct target_ops *ops,
 				struct regcache *regcache, int regnum)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
-  int pid;
+  pid_t pid;
 
   /* NOTE: cagney/2002-12-02: See comment in fetch_inferior_registers
      about threaded assumptions.  */
-  pid = ptid_get_lwp (inferior_ptid);
-  if (pid == 0)
-    pid = ptid_get_pid (inferior_ptid);
+  pid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (regnum == -1 || sparc_gregset_supplies_p (gdbarch, regnum))
     {
diff --git a/gdb/tilegx-linux-nat.c b/gdb/tilegx-linux-nat.c
index c0833cf74b..5cdf74daf2 100644
--- a/gdb/tilegx-linux-nat.c
+++ b/gdb/tilegx-linux-nat.c
@@ -22,6 +22,7 @@
 #include "gdbcore.h"
 #include "regcache.h"
 #include "linux-nat.h"
+#include "inf-ptrace.h"
 
 #include "nat/gdb_ptrace.h"
 
@@ -126,11 +127,7 @@ fetch_inferior_registers (struct target_ops *ops,
 			  struct regcache *regcache, int regnum)
 {
   elf_gregset_t regs;
-  int tid;
-
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (ptrace (PTRACE_GETREGS, tid, 0, (PTRACE_TYPE_ARG3) &regs) < 0)
     perror_with_name (_("Couldn't get registers"));
@@ -146,11 +143,7 @@ store_inferior_registers (struct target_ops *ops,
 			  struct regcache *regcache, int regnum)
 {
   elf_gregset_t regs;
-  int tid;
-
-  tid = ptid_get_lwp (inferior_ptid);
-  if (tid == 0)
-    tid = ptid_get_pid (inferior_ptid);
+  pid_t tid = get_ptrace_pid (regcache_get_ptid (regcache));
 
   if (ptrace (PTRACE_GETREGS, tid, 0, (PTRACE_TYPE_ARG3) &regs) < 0)
     perror_with_name (_("Couldn't get registers"));
diff --git a/gdb/vax-bsd-nat.c b/gdb/vax-bsd-nat.c
index a8bc603679..62953682d3 100644
--- a/gdb/vax-bsd-nat.c
+++ b/gdb/vax-bsd-nat.c
@@ -67,9 +67,9 @@ vaxbsd_fetch_inferior_registers (struct target_ops *ops,
 				 struct regcache *regcache, int regnum)
 {
   struct reg regs;
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
-  if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   vaxbsd_supply_gregset (regcache, &regs);
@@ -83,15 +83,14 @@ vaxbsd_store_inferior_registers (struct target_ops *ops,
 				 struct regcache *regcache, int regnum)
 {
   struct reg regs;
+  pid_t pid = ptid_get_pid (regcache_get_ptid (regcache));
 
-  if (ptrace (PT_GETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
 
   vaxbsd_collect_gregset (regcache, &regs, regnum);
 
-  if (ptrace (PT_SETREGS, ptid_get_pid (inferior_ptid),
-	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+  if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't write registers"));
 }
 \f
diff --git a/gdb/xtensa-linux-nat.c b/gdb/xtensa-linux-nat.c
index c29f0c71e6..69c7eefa66 100644
--- a/gdb/xtensa-linux-nat.c
+++ b/gdb/xtensa-linux-nat.c
@@ -174,7 +174,7 @@ supply_fpregset (struct regcache *regcache,
 static void
 fetch_gregs (struct regcache *regcache, int regnum)
 {
-  int tid = ptid_get_lwp (inferior_ptid);
+  int tid = ptid_get_lwp (regcache_get_ptid (regcache));
   gdb_gregset_t regs;
   int areg;
   
@@ -193,7 +193,7 @@ fetch_gregs (struct regcache *regcache, int regnum)
 static void
 store_gregs (struct regcache *regcache, int regnum)
 {
-  int tid = ptid_get_lwp (inferior_ptid);
+  int tid = ptid_get_lwp (regcache_get_ptid (regcache));
   gdb_gregset_t regs;
   int areg;
 
@@ -221,7 +221,7 @@ static int xtreg_high;
 static void
 fetch_xtregs (struct regcache *regcache, int regnum)
 {
-  int tid = ptid_get_lwp (inferior_ptid);
+  int tid = ptid_get_lwp (regcache_get_ptid (regcache));
   const xtensa_regtable_t *ptr;
   char xtregs [XTENSA_ELF_XTREG_SIZE];
 
@@ -237,7 +237,7 @@ fetch_xtregs (struct regcache *regcache, int regnum)
 static void
 store_xtregs (struct regcache *regcache, int regnum)
 {
-  int tid = ptid_get_lwp (inferior_ptid);
+  int tid = ptid_get_lwp (regcache_get_ptid (regcache));
   const xtensa_regtable_t *ptr;
   char xtregs [XTENSA_ELF_XTREG_SIZE];
 
-- 
2.12.0

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH 3/3] windows: Use ptid from regcache in register fetch/store
  2017-03-18 17:08 [PATCH 1/3] Use ptid from regcache in almost all remaining nat files Simon Marchi
  2017-03-18 17:08 ` [PATCH 2/3] spu: Use ptid from regcache instead of inferior_ptid Simon Marchi
@ 2017-03-18 17:08 ` Simon Marchi
  2017-03-20 15:56   ` Pedro Alves
  2017-03-20 15:54 ` [PATCH 1/3] Use ptid from regcache in almost all remaining nat files Pedro Alves
  2 siblings, 1 reply; 15+ messages in thread
From: Simon Marchi @ 2017-03-18 17:08 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@ericsson.com>

Use the ptid from the regcache so we don't depend on the current value
of the inferior_ptid global.

Also, change how the current thread is passed to sub-functions.  The
windows_fetch_inferior_registers function sets current_thread then calls
do_windows_fetch_inferior_registers, which reads current_thread.  This
very much looks like passing a parameter through a global variable.  I
think it would be more straightforward to pass the thread as a
parameter.

gdb/ChangeLog:

	* windows-nat.c (do_windows_fetch_inferior_registers): Add
	windows_thread_info parameter and use it instead of
	current_thread.
	(windows_fetch_inferior_registers): Don't set current_thread,
	pass the thread to do_windows_fetch_inferior_registers.  Use
	ptid from regcache instead of inferior_ptid.
	(do_windows_store_inferior_registers): Add windows_thread_info
	parameter and use it instead of current_thread.
	(windows_store_inferior_registers): Don't set current_thread,
	pass the thread to do_windows_store_inferior_registers.  Use
	ptid from regcache instead of inferior_ptid.
---
 gdb/windows-nat.c | 43 +++++++++++++++++++++----------------------
 1 file changed, 21 insertions(+), 22 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 9cc755f0d4..32a9ee62cf 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -460,18 +460,15 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code)
 }
 
 static void
-do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
+do_windows_fetch_inferior_registers (struct regcache *regcache,
+				     windows_thread_info *th, int r)
 {
   char *context_offset = ((char *) &current_thread->context) + mappings[r];
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   long l;
 
-  if (!current_thread)
-    return;	/* Windows sometimes uses a non-existent thread id in its
-		   events.  */
-
-  if (current_thread->reload_context)
+  if (th->reload_context)
     {
 #ifdef __CYGWIN__
       if (have_saved_context)
@@ -480,14 +477,13 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
 	     cygwin has informed us that we should consider the signal
 	     to have occurred at another location which is stored in
 	     "saved_context.  */
-	  memcpy (&current_thread->context, &saved_context,
+	  memcpy (&th->context, &saved_context,
 		  __COPY_CONTEXT_SIZE);
 	  have_saved_context = 0;
 	}
       else
 #endif
 	{
-	  windows_thread_info *th = current_thread;
 	  th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
 	  CHECK (GetThreadContext (th->h, &th->context));
 	  /* Copy dr values from that thread.
@@ -503,7 +499,7 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
 	      dr[7] = th->context.Dr7;
 	    }
 	}
-      current_thread->reload_context = 0;
+      th->reload_context = 0;
     }
 
   if (r == I387_FISEG_REGNUM (tdep))
@@ -529,7 +525,7 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
   else
     {
       for (r = 0; r < gdbarch_num_regs (gdbarch); r++)
-	do_windows_fetch_inferior_registers (regcache, r);
+	do_windows_fetch_inferior_registers (regcache, th, r);
     }
 }
 
@@ -537,25 +533,26 @@ static void
 windows_fetch_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int r)
 {
-  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
+  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
+  windows_thread_info *th = thread_rec (pid, TRUE);
+
   /* Check if current_thread exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
-  if (current_thread)
-    do_windows_fetch_inferior_registers (regcache, r);
+  if (th != NULL)
+    do_windows_fetch_inferior_registers (regcache, th, r);
 }
 
 static void
-do_windows_store_inferior_registers (const struct regcache *regcache, int r)
+do_windows_store_inferior_registers (const struct regcache *regcache,
+				     windows_thread_info *th, int r)
 {
-  if (!current_thread)
-    /* Windows sometimes uses a non-existent thread id in its events.  */;
-  else if (r >= 0)
+  if (r >= 0)
     regcache_raw_collect (regcache, r,
-			  ((char *) &current_thread->context) + mappings[r]);
+			  ((char *) &th->context) + mappings[r]);
   else
     {
       for (r = 0; r < gdbarch_num_regs (get_regcache_arch (regcache)); r++)
-	do_windows_store_inferior_registers (regcache, r);
+	do_windows_store_inferior_registers (regcache, th, r);
     }
 }
 
@@ -564,11 +561,13 @@ static void
 windows_store_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int r)
 {
-  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
+  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
+  windows_thread_info *th = thread_rec (pid, TRUE);
+
   /* Check if current_thread exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
-  if (current_thread)
-    do_windows_store_inferior_registers (regcache, r);
+  if (th != NULL)
+    do_windows_store_inferior_registers (regcache, th, r);
 }
 
 /* Encapsulate the information required in a call to
-- 
2.12.0

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/3] spu: Use ptid from regcache instead of inferior_ptid
  2017-03-18 17:08 ` [PATCH 2/3] spu: Use ptid from regcache instead of inferior_ptid Simon Marchi
@ 2017-03-20 15:54   ` Pedro Alves
  2017-03-20 21:50     ` Simon Marchi
  2017-03-20 21:52     ` [PATCH v2 " Simon Marchi
  0 siblings, 2 replies; 15+ messages in thread
From: Pedro Alves @ 2017-03-20 15:54 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 1/3] Use ptid from regcache in almost all remaining nat files
  2017-03-18 17:08 [PATCH 1/3] Use ptid from regcache in almost all remaining nat files Simon Marchi
  2017-03-18 17:08 ` [PATCH 2/3] spu: Use ptid from regcache instead of inferior_ptid Simon Marchi
  2017-03-18 17:08 ` [PATCH 3/3] windows: Use ptid from regcache in register fetch/store Simon Marchi
@ 2017-03-20 15:54 ` Pedro Alves
  2017-03-20 21:39   ` Simon Marchi
  2 siblings, 1 reply; 15+ messages in thread
From: Pedro Alves @ 2017-03-20 15:54 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 03/18/2017 05:07 PM, Simon Marchi wrote:
> This patch contains almost all of the remaining changes needed to make
> to_fetch_registers/to_store_registers/to_prepare_to_store independent of
> inferior_ptid.  It contains only some "trivial" changes, the more
> complicated ones are in separate patches.

LGTM.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 3/3] windows: Use ptid from regcache in register fetch/store
  2017-03-18 17:08 ` [PATCH 3/3] windows: Use ptid from regcache in register fetch/store Simon Marchi
@ 2017-03-20 15:56   ` Pedro Alves
  2017-03-20 22:22     ` Simon Marchi
  0 siblings, 1 reply; 15+ messages in thread
From: Pedro Alves @ 2017-03-20 15:56 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 03/18/2017 05:08 PM, Simon Marchi wrote:
> From: Simon Marchi <simon.marchi@ericsson.com>
> 
> Use the ptid from the regcache so we don't depend on the current value
> of the inferior_ptid global.
> 
> Also, change how the current thread is passed to sub-functions.  The
> windows_fetch_inferior_registers function sets current_thread then calls
> do_windows_fetch_inferior_registers, which reads current_thread.  This
> very much looks like passing a parameter through a global variable.  I
> think it would be more straightforward to pass the thread as a
> parameter.
> 
> gdb/ChangeLog:
> 
> 	* windows-nat.c (do_windows_fetch_inferior_registers): Add
> 	windows_thread_info parameter and use it instead of
> 	current_thread.
> 	(windows_fetch_inferior_registers): Don't set current_thread,
> 	pass the thread to do_windows_fetch_inferior_registers.  Use
> 	ptid from regcache instead of inferior_ptid.
> 	(do_windows_store_inferior_registers): Add windows_thread_info
> 	parameter and use it instead of current_thread.
> 	(windows_store_inferior_registers): Don't set current_thread,
> 	pass the thread to do_windows_store_inferior_registers.  Use
> 	ptid from regcache instead of inferior_ptid.
> ---
>  gdb/windows-nat.c | 43 +++++++++++++++++++++----------------------
>  1 file changed, 21 insertions(+), 22 deletions(-)
> 
> diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
> index 9cc755f0d4..32a9ee62cf 100644
> --- a/gdb/windows-nat.c
> +++ b/gdb/windows-nat.c
> @@ -460,18 +460,15 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code)
>  }
>  
>  static void
> -do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
> +do_windows_fetch_inferior_registers (struct regcache *regcache,
> +				     windows_thread_info *th, int r)
>  {
>    char *context_offset = ((char *) &current_thread->context) + mappings[r];

Is this reference to "current_thread" still correct?

>    struct gdbarch *gdbarch = get_regcache_arch (regcache);
>    struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
>    long l;
>  
> -  if (!current_thread)
> -    return;	/* Windows sometimes uses a non-existent thread id in its
> -		   events.  */
> -
> -  if (current_thread->reload_context)
> +  if (th->reload_context)
>      {
>  #ifdef __CYGWIN__
>        if (have_saved_context)
> @@ -480,14 +477,13 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
>  	     cygwin has informed us that we should consider the signal
>  	     to have occurred at another location which is stored in
>  	     "saved_context.  */
> -	  memcpy (&current_thread->context, &saved_context,
> +	  memcpy (&th->context, &saved_context,
>  		  __COPY_CONTEXT_SIZE);
>  	  have_saved_context = 0;
>  	}
>        else
>  #endif
>  	{
> -	  windows_thread_info *th = current_thread;
>  	  th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
>  	  CHECK (GetThreadContext (th->h, &th->context));
>  	  /* Copy dr values from that thread.
> @@ -503,7 +499,7 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
>  	      dr[7] = th->context.Dr7;
>  	    }
>  	}
> -      current_thread->reload_context = 0;
> +      th->reload_context = 0;
>      }
>  
>    if (r == I387_FISEG_REGNUM (tdep))
> @@ -529,7 +525,7 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
>    else
>      {
>        for (r = 0; r < gdbarch_num_regs (gdbarch); r++)
> -	do_windows_fetch_inferior_registers (regcache, r);
> +	do_windows_fetch_inferior_registers (regcache, th, r);
>      }
>  }
>  
> @@ -537,25 +533,26 @@ static void
>  windows_fetch_inferior_registers (struct target_ops *ops,
>  				  struct regcache *regcache, int r)
>  {
> -  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
> +  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
> +  windows_thread_info *th = thread_rec (pid, TRUE);
> +
>    /* Check if current_thread exists.  Windows sometimes uses a non-existent
>       thread id in its events.  */

The comment is out of date now.

Did you look for the history around these comments?  I wonder whether
these NULL checks still make sense here if we always reference the
regcache's thread.  The equivalent code in gdbserver doesn't seem to
have them.

> -  if (current_thread)
> -    do_windows_fetch_inferior_registers (regcache, r);
> +  if (th != NULL)
> +    do_windows_fetch_inferior_registers (regcache, th, r);
>  }
>  
>  static void
> -do_windows_store_inferior_registers (const struct regcache *regcache, int r)
> +do_windows_store_inferior_registers (const struct regcache *regcache,
> +				     windows_thread_info *th, int r)
>  {
> -  if (!current_thread)
> -    /* Windows sometimes uses a non-existent thread id in its events.  */;
> -  else if (r >= 0)
> +  if (r >= 0)
>      regcache_raw_collect (regcache, r,
> -			  ((char *) &current_thread->context) + mappings[r]);
> +			  ((char *) &th->context) + mappings[r]);
>    else
>      {
>        for (r = 0; r < gdbarch_num_regs (get_regcache_arch (regcache)); r++)
> -	do_windows_store_inferior_registers (regcache, r);
> +	do_windows_store_inferior_registers (regcache, th, r);
>      }
>  }
>  
> @@ -564,11 +561,13 @@ static void
>  windows_store_inferior_registers (struct target_ops *ops,
>  				  struct regcache *regcache, int r)
>  {
> -  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
> +  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
> +  windows_thread_info *th = thread_rec (pid, TRUE);
> +
>    /* Check if current_thread exists.  Windows sometimes uses a non-existent
>       thread id in its events.  */

Ditto.

> -  if (current_thread)
> -    do_windows_store_inferior_registers (regcache, r);
> +  if (th != NULL)
> +    do_windows_store_inferior_registers (regcache, th, r);
>  }
>  
>  /* Encapsulate the information required in a call to
> 

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 1/3] Use ptid from regcache in almost all remaining nat files
  2017-03-20 15:54 ` [PATCH 1/3] Use ptid from regcache in almost all remaining nat files Pedro Alves
@ 2017-03-20 21:39   ` Simon Marchi
  0 siblings, 0 replies; 15+ messages in thread
From: Simon Marchi @ 2017-03-20 21:39 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 17-03-20 11:54 AM, Pedro Alves wrote:
> On 03/18/2017 05:07 PM, Simon Marchi wrote:
>> This patch contains almost all of the remaining changes needed to make
>> to_fetch_registers/to_store_registers/to_prepare_to_store independent of
>> inferior_ptid.  It contains only some "trivial" changes, the more
>> complicated ones are in separate patches.
> 
> LGTM.
> 
> Thanks,
> Pedro Alves
> 

Thanks, this one is pushed.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 2/3] spu: Use ptid from regcache instead of inferior_ptid
  2017-03-20 15:54   ` Pedro Alves
@ 2017-03-20 21:50     ` Simon Marchi
  2017-03-20 21:52     ` [PATCH v2 " Simon Marchi
  1 sibling, 0 replies; 15+ messages in thread
From: Simon Marchi @ 2017-03-20 21:50 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2 2/3] spu: Use ptid from regcache instead of inferior_ptid
  2017-03-20 15:54   ` Pedro Alves
  2017-03-20 21:50     ` Simon Marchi
@ 2017-03-20 21:52     ` Simon Marchi
  2017-03-20 22:06       ` Pedro Alves
  1 sibling, 1 reply; 15+ messages in thread
From: Simon Marchi @ 2017-03-20 21:52 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@polymtl.ca>

New in v2: use scoped_restore to save/restore inferior_ptid.

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 | 10 ++++++++++
 gdb/spu-multiarch.c | 10 ++++++++++
 2 files changed, 20 insertions(+)

diff --git a/gdb/spu-linux-nat.c b/gdb/spu-linux-nat.c
index c5b91222c1..19552fc1fe 100644
--- a/gdb/spu-linux-nat.c
+++ b/gdb/spu-linux-nat.c
@@ -492,6 +492,11 @@ 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.  */
+  scoped_restore save_ptid
+    = make_scoped_restore (&inferior_ptid, regcache_get_ptid (regcache));
+
   /* We must be stopped on a spu_run system call.  */
   if (!parse_spufs_run (&fd, &addr))
     return;
@@ -539,6 +544,11 @@ 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.  */
+  scoped_restore save_ptid
+    = make_scoped_restore (&inferior_ptid, regcache_get_ptid (regcache));
+
   /* We must be stopped on a spu_run system call.  */
   if (!parse_spufs_run (&fd, &addr))
     return;
diff --git a/gdb/spu-multiarch.c b/gdb/spu-multiarch.c
index b99a1a3de3..abc46535cf 100644
--- a/gdb/spu-multiarch.c
+++ b/gdb/spu-multiarch.c
@@ -149,6 +149,11 @@ 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.  */
+  scoped_restore save_ptid
+    = make_scoped_restore (&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)
     {
@@ -203,6 +208,11 @@ 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.  */
+  scoped_restore save_ptid
+    = make_scoped_restore (&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)
     {
-- 
2.11.0

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 2/3] spu: Use ptid from regcache instead of inferior_ptid
  2017-03-20 21:52     ` [PATCH v2 " Simon Marchi
@ 2017-03-20 22:06       ` Pedro Alves
  2017-03-20 22:24         ` Simon Marchi
  0 siblings, 1 reply; 15+ messages in thread
From: Pedro Alves @ 2017-03-20 22:06 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi

On 03/20/2017 09:51 PM, Simon Marchi wrote:
> From: Simon Marchi <simon.marchi@polymtl.ca>
> 
> New in v2: use scoped_restore to save/restore inferior_ptid.
> 
> The implementations of to_fetch_regiters/to_store_registers in the spu

Typo: "regiters".

> 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.

LGTM.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 3/3] windows: Use ptid from regcache in register  fetch/store
  2017-03-20 15:56   ` Pedro Alves
@ 2017-03-20 22:22     ` Simon Marchi
  2017-03-21 14:27       ` Pedro Alves
  0 siblings, 1 reply; 15+ messages in thread
From: Simon Marchi @ 2017-03-20 22:22 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, Simon Marchi

On 2017-03-20 11:56, Pedro Alves wrote:
> On 03/18/2017 05:08 PM, Simon Marchi wrote:
>> From: Simon Marchi <simon.marchi@ericsson.com>
>> 
>> Use the ptid from the regcache so we don't depend on the current value
>> of the inferior_ptid global.
>> 
>> Also, change how the current thread is passed to sub-functions.  The
>> windows_fetch_inferior_registers function sets current_thread then 
>> calls
>> do_windows_fetch_inferior_registers, which reads current_thread.  This
>> very much looks like passing a parameter through a global variable.  I
>> think it would be more straightforward to pass the thread as a
>> parameter.
>> 
>> gdb/ChangeLog:
>> 
>> 	* windows-nat.c (do_windows_fetch_inferior_registers): Add
>> 	windows_thread_info parameter and use it instead of
>> 	current_thread.
>> 	(windows_fetch_inferior_registers): Don't set current_thread,
>> 	pass the thread to do_windows_fetch_inferior_registers.  Use
>> 	ptid from regcache instead of inferior_ptid.
>> 	(do_windows_store_inferior_registers): Add windows_thread_info
>> 	parameter and use it instead of current_thread.
>> 	(windows_store_inferior_registers): Don't set current_thread,
>> 	pass the thread to do_windows_store_inferior_registers.  Use
>> 	ptid from regcache instead of inferior_ptid.
>> ---
>>  gdb/windows-nat.c | 43 +++++++++++++++++++++----------------------
>>  1 file changed, 21 insertions(+), 22 deletions(-)
>> 
>> diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
>> index 9cc755f0d4..32a9ee62cf 100644
>> --- a/gdb/windows-nat.c
>> +++ b/gdb/windows-nat.c
>> @@ -460,18 +460,15 @@ windows_delete_thread (ptid_t ptid, DWORD 
>> exit_code)
>>  }
>> 
>>  static void
>> -do_windows_fetch_inferior_registers (struct regcache *regcache, int 
>> r)
>> +do_windows_fetch_inferior_registers (struct regcache *regcache,
>> +				     windows_thread_info *th, int r)
>>  {
>>    char *context_offset = ((char *) &current_thread->context) + 
>> mappings[r];
> 
> Is this reference to "current_thread" still correct?

Oops, I guess it should be th, like the rest:

   char *context_offset = ((char *) th->context) + mappings[r];

Fixed locally.

>> @@ -537,25 +533,26 @@ static void
>>  windows_fetch_inferior_registers (struct target_ops *ops,
>>  				  struct regcache *regcache, int r)
>>  {
>> -  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
>> +  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
>> +  windows_thread_info *th = thread_rec (pid, TRUE);
>> +
>>    /* Check if current_thread exists.  Windows sometimes uses a 
>> non-existent
>>       thread id in its events.  */
> 
> The comment is out of date now.

Fixed locally.

> Did you look for the history around these comments?  I wonder whether
> these NULL checks still make sense here if we always reference the
> regcache's thread.  The equivalent code in gdbserver doesn't seem to
> have them.

All I know is that this is the patch that introduced them:
https://sourceware.org/ml/gdb-patches/2003-12/msg00479.html

The PR 1048 seems to refer to a pre-bugzilla bug tracking system.  Do we 
still have them somewhere?

 From what I understand, it's the use case where you attach to a process 
whose main thread has already exited.  If the patch introduced these 
NULL checks, I suppose it's because they were necessary back then to 
work around the Windows bug.  I have no idea if they are still 
necessary, or if the Microsoft people fixed it.  In any case, the fact 
of whether the checks are needed is not impacted by the current patch: 
in the end, we call thread_rec with the same pid with which we would 
have called it before, so we should get the same result.

I'll wait for your input on this before sending a new version.

>> @@ -564,11 +561,13 @@ static void
>>  windows_store_inferior_registers (struct target_ops *ops,
>>  				  struct regcache *regcache, int r)
>>  {
>> -  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
>> +  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
>> +  windows_thread_info *th = thread_rec (pid, TRUE);
>> +
>>    /* Check if current_thread exists.  Windows sometimes uses a 
>> non-existent
>>       thread id in its events.  */
> 
> Ditto.

Fixed locally.

Thanks,

Simon

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 2/3] spu: Use ptid from regcache instead of  inferior_ptid
  2017-03-20 22:06       ` Pedro Alves
@ 2017-03-20 22:24         ` Simon Marchi
  0 siblings, 0 replies; 15+ messages in thread
From: Simon Marchi @ 2017-03-20 22:24 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Simon Marchi, gdb-patches

On 2017-03-20 18:06, Pedro Alves wrote:
> On 03/20/2017 09:51 PM, Simon Marchi wrote:
>> From: Simon Marchi <simon.marchi@polymtl.ca>
>> 
>> New in v2: use scoped_restore to save/restore inferior_ptid.
>> 
>> The implementations of to_fetch_regiters/to_store_registers in the spu
> 
> Typo: "regiters".

Oops, fixed.

>> 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.
> 
> LGTM.

Thanks, pushed.

Simon

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 3/3] windows: Use ptid from regcache in register fetch/store
  2017-03-20 22:22     ` Simon Marchi
@ 2017-03-21 14:27       ` Pedro Alves
  2017-03-21 15:24         ` Simon Marchi
  0 siblings, 1 reply; 15+ messages in thread
From: Pedro Alves @ 2017-03-21 14:27 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches, Simon Marchi

On 03/20/2017 10:22 PM, Simon Marchi wrote:

>>>  static void
>>> -do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
>>> +do_windows_fetch_inferior_registers (struct regcache *regcache,
>>> +                     windows_thread_info *th, int r)
>>>  {
>>>    char *context_offset = ((char *) &current_thread->context) +
>>> mappings[r];
>>
>> Is this reference to "current_thread" still correct?
> 
> Oops, I guess it should be th, like the rest:
> 
>   char *context_offset = ((char *) th->context) + mappings[r];
> 
> Fixed locally.

Thanks.

> 
>>> @@ -537,25 +533,26 @@ static void
>>>  windows_fetch_inferior_registers (struct target_ops *ops,
>>>                    struct regcache *regcache, int r)
>>>  {
>>> -  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
>>> +  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
>>> +  windows_thread_info *th = thread_rec (pid, TRUE);
>>> +
>>>    /* Check if current_thread exists.  Windows sometimes uses a
>>> non-existent
>>>       thread id in its events.  */
>>
>> The comment is out of date now.
> 
> Fixed locally.
> 
>> Did you look for the history around these comments?  I wonder whether
>> these NULL checks still make sense here if we always reference the
>> regcache's thread.  The equivalent code in gdbserver doesn't seem to
>> have them.
> 
> All I know is that this is the patch that introduced them:
> https://sourceware.org/ml/gdb-patches/2003-12/msg00479.html
> 
> The PR 1048 seems to refer to a pre-bugzilla bug tracking system.  Do we
> still have them somewhere?

Here:

 https://sourceware.org/gdb/wiki/DeveloperTips?highlight=%28gnats%29#Finding_Gnats_bug_entries_in_the_Bugzilla_database

gnats 1048 + 7105 -> bugzilla 8153:

 https://sourceware.org/bugzilla/show_bug.cgi?id=8153

> 
> From what I understand, it's the use case where you attach to a process
> whose main thread has already exited.  If the patch introduced these
> NULL checks, I suppose it's because they were necessary back then to
> work around the Windows bug.  I have no idea if they are still
> necessary, or if the Microsoft people fixed it.  

[...]

> In any case, the fact
> of whether the checks are needed is not impacted by the current patch:
> in the end, we call thread_rec with the same pid with which we would
> have called it before, so we should get the same result.

You're right.

> 
> I'll wait for your input on this before sending a new version.
> 

I don't have further input.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH 3/3] windows: Use ptid from regcache in register  fetch/store
  2017-03-21 14:27       ` Pedro Alves
@ 2017-03-21 15:24         ` Simon Marchi
  2017-03-21 15:39           ` [pushed] " Simon Marchi
  0 siblings, 1 reply; 15+ messages in thread
From: Simon Marchi @ 2017-03-21 15:24 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, Simon Marchi

On 2017-03-21 10:27, Pedro Alves wrote:
>>> Did you look for the history around these comments?  I wonder whether
>>> these NULL checks still make sense here if we always reference the
>>> regcache's thread.  The equivalent code in gdbserver doesn't seem to
>>> have them.
>> 
>> All I know is that this is the patch that introduced them:
>> https://sourceware.org/ml/gdb-patches/2003-12/msg00479.html
>> 
>> The PR 1048 seems to refer to a pre-bugzilla bug tracking system.  Do 
>> we
>> still have them somewhere?
> 
> Here:
> 
> 
> https://sourceware.org/gdb/wiki/DeveloperTips?highlight=%28gnats%29#Finding_Gnats_bug_entries_in_the_Bugzilla_database
> 
> gnats 1048 + 7105 -> bugzilla 8153:
> 
>  https://sourceware.org/bugzilla/show_bug.cgi?id=8153

Ah, thanks!

>> From what I understand, it's the use case where you attach to a 
>> process
>> whose main thread has already exited.  If the patch introduced these
>> NULL checks, I suppose it's because they were necessary back then to
>> work around the Windows bug.  I have no idea if they are still
>> necessary, or if the Microsoft people fixed it.
> 
> [...]
> 
>> In any case, the fact
>> of whether the checks are needed is not impacted by the current patch:
>> in the end, we call thread_rec with the same pid with which we would
>> have called it before, so we should get the same result.
> 
> You're right.
> 
>> 
>> I'll wait for your input on this before sending a new version.
>> 
> 
> I don't have further input.

Thanks, I'll push the patch including the fixes and send the final 
version for reference.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [pushed] windows: Use ptid from regcache in register fetch/store
  2017-03-21 15:24         ` Simon Marchi
@ 2017-03-21 15:39           ` Simon Marchi
  0 siblings, 0 replies; 15+ messages in thread
From: Simon Marchi @ 2017-03-21 15:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Use the ptid from the regcache so we don't depend on the current value
of the inferior_ptid global.

Also, change how the current thread is passed to sub-functions.  The
windows_fetch_inferior_registers function sets current_thread then calls
do_windows_fetch_inferior_registers, which reads current_thread.  This
very much looks like passing a parameter through a global variable.  I
think it would be more straightforward to pass the thread as a
parameter.

gdb/ChangeLog:

	* windows-nat.c (do_windows_fetch_inferior_registers): Add
	windows_thread_info parameter and use it instead of
	current_thread.
	(windows_fetch_inferior_registers): Don't set current_thread,
	pass the thread to do_windows_fetch_inferior_registers.  Use
	ptid from regcache instead of inferior_ptid.
	(do_windows_store_inferior_registers): Add windows_thread_info
	parameter and use it instead of current_thread.
	(windows_store_inferior_registers): Don't set current_thread,
	pass the thread to do_windows_store_inferior_registers.  Use
	ptid from regcache instead of inferior_ptid.
---
 gdb/ChangeLog     | 14 ++++++++++++++
 gdb/windows-nat.c | 52 ++++++++++++++++++++++++++--------------------------
 2 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 537e5beabc..aebfb75583 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,19 @@
 2017-03-21  Simon Marchi  <simon.marchi@ericsson.com>
 
+	* windows-nat.c (do_windows_fetch_inferior_registers): Add
+	windows_thread_info parameter and use it instead of
+	current_thread.
+	(windows_fetch_inferior_registers): Don't set current_thread,
+	pass the thread to do_windows_fetch_inferior_registers.  Use
+	ptid from regcache instead of inferior_ptid.
+	(do_windows_store_inferior_registers): Add windows_thread_info
+	parameter and use it instead of current_thread.
+	(windows_store_inferior_registers): Don't set current_thread,
+	pass the thread to do_windows_store_inferior_registers.  Use
+	ptid from regcache instead of inferior_ptid.
+
+2017-03-21  Simon Marchi  <simon.marchi@ericsson.com>
+
 	* ser-mingw.c (ser_windows_raw): Remove reference to
 	struct serial::current_timeout.
 
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 9cc755f0d4..76313db989 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -460,18 +460,15 @@ windows_delete_thread (ptid_t ptid, DWORD exit_code)
 }
 
 static void
-do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
+do_windows_fetch_inferior_registers (struct regcache *regcache,
+				     windows_thread_info *th, int r)
 {
-  char *context_offset = ((char *) &current_thread->context) + mappings[r];
+  char *context_offset = ((char *) &th->context) + mappings[r];
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   long l;
 
-  if (!current_thread)
-    return;	/* Windows sometimes uses a non-existent thread id in its
-		   events.  */
-
-  if (current_thread->reload_context)
+  if (th->reload_context)
     {
 #ifdef __CYGWIN__
       if (have_saved_context)
@@ -480,14 +477,13 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
 	     cygwin has informed us that we should consider the signal
 	     to have occurred at another location which is stored in
 	     "saved_context.  */
-	  memcpy (&current_thread->context, &saved_context,
+	  memcpy (&th->context, &saved_context,
 		  __COPY_CONTEXT_SIZE);
 	  have_saved_context = 0;
 	}
       else
 #endif
 	{
-	  windows_thread_info *th = current_thread;
 	  th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
 	  CHECK (GetThreadContext (th->h, &th->context));
 	  /* Copy dr values from that thread.
@@ -503,7 +499,7 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
 	      dr[7] = th->context.Dr7;
 	    }
 	}
-      current_thread->reload_context = 0;
+      th->reload_context = 0;
     }
 
   if (r == I387_FISEG_REGNUM (tdep))
@@ -529,7 +525,7 @@ do_windows_fetch_inferior_registers (struct regcache *regcache, int r)
   else
     {
       for (r = 0; r < gdbarch_num_regs (gdbarch); r++)
-	do_windows_fetch_inferior_registers (regcache, r);
+	do_windows_fetch_inferior_registers (regcache, th, r);
     }
 }
 
@@ -537,38 +533,42 @@ static void
 windows_fetch_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int r)
 {
-  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
-  /* Check if current_thread exists.  Windows sometimes uses a non-existent
+  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
+  windows_thread_info *th = thread_rec (pid, TRUE);
+
+  /* Check if TH exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
-  if (current_thread)
-    do_windows_fetch_inferior_registers (regcache, r);
+  if (th != NULL)
+    do_windows_fetch_inferior_registers (regcache, th, r);
 }
 
 static void
-do_windows_store_inferior_registers (const struct regcache *regcache, int r)
+do_windows_store_inferior_registers (const struct regcache *regcache,
+				     windows_thread_info *th, int r)
 {
-  if (!current_thread)
-    /* Windows sometimes uses a non-existent thread id in its events.  */;
-  else if (r >= 0)
+  if (r >= 0)
     regcache_raw_collect (regcache, r,
-			  ((char *) &current_thread->context) + mappings[r]);
+			  ((char *) &th->context) + mappings[r]);
   else
     {
       for (r = 0; r < gdbarch_num_regs (get_regcache_arch (regcache)); r++)
-	do_windows_store_inferior_registers (regcache, r);
+	do_windows_store_inferior_registers (regcache, th, r);
     }
 }
 
-/* Store a new register value into the current thread context.  */
+/* Store a new register value into the context of the thread tied to
+   REGCACHE.  */
 static void
 windows_store_inferior_registers (struct target_ops *ops,
 				  struct regcache *regcache, int r)
 {
-  current_thread = thread_rec (ptid_get_tid (inferior_ptid), TRUE);
-  /* Check if current_thread exists.  Windows sometimes uses a non-existent
+  DWORD pid = ptid_get_tid (regcache_get_ptid (regcache));
+  windows_thread_info *th = thread_rec (pid, TRUE);
+
+  /* Check if TH exists.  Windows sometimes uses a non-existent
      thread id in its events.  */
-  if (current_thread)
-    do_windows_store_inferior_registers (regcache, r);
+  if (th != NULL)
+    do_windows_store_inferior_registers (regcache, th, r);
 }
 
 /* Encapsulate the information required in a call to
-- 
2.11.0

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2017-03-21 15:39 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-18 17:08 [PATCH 1/3] Use ptid from regcache in almost all remaining nat files Simon Marchi
2017-03-18 17:08 ` [PATCH 2/3] spu: Use ptid from regcache instead of inferior_ptid Simon Marchi
2017-03-20 15:54   ` Pedro Alves
2017-03-20 21:50     ` Simon Marchi
2017-03-20 21:52     ` [PATCH v2 " Simon Marchi
2017-03-20 22:06       ` Pedro Alves
2017-03-20 22:24         ` Simon Marchi
2017-03-18 17:08 ` [PATCH 3/3] windows: Use ptid from regcache in register fetch/store Simon Marchi
2017-03-20 15:56   ` Pedro Alves
2017-03-20 22:22     ` Simon Marchi
2017-03-21 14:27       ` Pedro Alves
2017-03-21 15:24         ` Simon Marchi
2017-03-21 15:39           ` [pushed] " Simon Marchi
2017-03-20 15:54 ` [PATCH 1/3] Use ptid from regcache in almost all remaining nat files Pedro Alves
2017-03-20 21:39   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).