diff --git a/gdb/amd64-fbsd-tdep.c b/gdb/amd64-fbsd-tdep.c
index b8ef25821b..d4c81de26e 100644
--- a/gdb/amd64-fbsd-tdep.c
+++ b/gdb/amd64-fbsd-tdep.c
@@ -31,6 +31,7 @@
 #include "bsd-uthread.h"
 #include "fbsd-tdep.h"
 #include "solib-svr4.h"
+#include "inferior.h"
 
 /* Support for signal handlers.  */
 
@@ -226,11 +227,13 @@ amd64fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
 }
 
 static void
-amd64fbsd_supply_uthread (struct regcache *regcache,
+amd64fbsd_supply_uthread (struct regcache *regcache, ptid_t ptid,
 			  int regnum, CORE_ADDR addr)
 {
   gdb_byte buf[8];
   int i;
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -243,14 +246,18 @@ amd64fbsd_supply_uthread (struct regcache *regcache,
 	  regcache_raw_supply (regcache, i, buf);
 	}
     }
+
+  do_cleanups (cleanup);
 }
 
 static void
-amd64fbsd_collect_uthread (const struct regcache *regcache,
+amd64fbsd_collect_uthread (const struct regcache *regcache, ptid_t ptid,
 			   int regnum, CORE_ADDR addr)
 {
   gdb_byte buf[8];
   int i;
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -263,6 +270,8 @@ amd64fbsd_collect_uthread (const struct regcache *regcache,
 	  write_memory (addr + amd64fbsd_jmp_buf_reg_offset[i], buf, 8);
 	}
     }
+
+  do_cleanups (cleanup);
 }
 
 static void
diff --git a/gdb/amd64-obsd-tdep.c b/gdb/amd64-obsd-tdep.c
index 72895b6fc6..ac39c51b3f 100644
--- a/gdb/amd64-obsd-tdep.c
+++ b/gdb/amd64-obsd-tdep.c
@@ -34,6 +34,7 @@
 #include "i387-tdep.h"
 #include "solib-svr4.h"
 #include "bsd-uthread.h"
+#include "inferior.h"
 
 /* Support for signal handlers.  */
 
@@ -217,7 +218,7 @@ static int amd64obsd_uthread_reg_offset[] =
 #define AMD64OBSD_UTHREAD_RSP_OFFSET	400
 
 static void
-amd64obsd_supply_uthread (struct regcache *regcache,
+amd64obsd_supply_uthread (struct regcache *regcache, ptid_t ptid,
 			  int regnum, CORE_ADDR addr)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
@@ -226,6 +227,8 @@ amd64obsd_supply_uthread (struct regcache *regcache,
   CORE_ADDR sp = 0;
   gdb_byte buf[8];
   int i;
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -258,10 +261,12 @@ amd64obsd_supply_uthread (struct regcache *regcache,
 	  regcache_raw_supply (regcache, i, buf);
 	}
     }
+
+  do_cleanups (cleanup);
 }
 
 static void
-amd64obsd_collect_uthread (const struct regcache *regcache,
+amd64obsd_collect_uthread (const struct regcache *regcache, ptid_t ptid,
 			   int regnum, CORE_ADDR addr)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
@@ -270,6 +275,8 @@ amd64obsd_collect_uthread (const struct regcache *regcache,
   CORE_ADDR sp = 0;
   gdb_byte buf[8];
   int i;
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -306,6 +313,8 @@ amd64obsd_collect_uthread (const struct regcache *regcache,
 	  write_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
 	}
     }
+
+  do_cleanups (cleanup);
 }
 /* Kernel debugging support.  */
 
diff --git a/gdb/bsd-uthread.c b/gdb/bsd-uthread.c
index 09a9de28ae..2111128f21 100644
--- a/gdb/bsd-uthread.c
+++ b/gdb/bsd-uthread.c
@@ -286,7 +286,8 @@ bsd_uthread_fetch_registers (struct target_ops *ops,
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   struct bsd_uthread_ops *uthread_ops
     = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
-  CORE_ADDR addr = ptid_get_tid (inferior_ptid);
+  ptid_t ptid = inferior_ptid;
+  CORE_ADDR addr = ptid_get_tid (ptid);
   struct target_ops *beneath = find_target_beneath (ops);
   CORE_ADDR active_addr;
 
@@ -302,7 +303,7 @@ bsd_uthread_fetch_registers (struct target_ops *ops,
   if (addr != 0 && addr != active_addr)
     {
       bsd_uthread_check_magic (addr);
-      uthread_ops->supply_uthread (regcache, regnum,
+      uthread_ops->supply_uthread (regcache, ptid, regnum,
 				   addr + bsd_uthread_thread_ctx_offset);
     }
 }
@@ -315,14 +316,15 @@ bsd_uthread_store_registers (struct target_ops *ops,
   struct bsd_uthread_ops *uthread_ops
     = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
   struct target_ops *beneath = find_target_beneath (ops);
-  CORE_ADDR addr = ptid_get_tid (inferior_ptid);
+  ptid_t ptid = inferior_ptid;
+  CORE_ADDR addr = ptid_get_tid (ptid);
   CORE_ADDR active_addr;
 
   active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
   if (addr != 0 && addr != active_addr)
     {
       bsd_uthread_check_magic (addr);
-      uthread_ops->collect_uthread (regcache, regnum,
+      uthread_ops->collect_uthread (regcache, ptid, regnum,
 				    addr + bsd_uthread_thread_ctx_offset);
     }
   else
diff --git a/gdb/bsd-uthread.h b/gdb/bsd-uthread.h
index 4802d019ae..b61415331d 100644
--- a/gdb/bsd-uthread.h
+++ b/gdb/bsd-uthread.h
@@ -20,10 +20,10 @@
 #ifndef BSD_UTHREAD_H
 #define BSD_UTHREAD_H 1
 
-typedef void (*bsd_uthread_supply_register_ftype) (struct regcache *, int,
-						   CORE_ADDR);
+typedef void (*bsd_uthread_supply_register_ftype) (struct regcache *, ptid_t,
+						   int, CORE_ADDR);
 typedef void (*bsd_uthread_collect_register_ftype) (const struct regcache *,
-						    int, CORE_ADDR);
+						    ptid_t ptid, int, CORE_ADDR);
 
 /* Set the function that supplies registers for an inactive thread for
    architecture GDBARCH to FUNC.  */
diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c
index b0bf7270bb..6d8f213489 100644
--- a/gdb/i386-fbsd-tdep.c
+++ b/gdb/i386-fbsd-tdep.c
@@ -31,6 +31,7 @@
 #include "bsd-uthread.h"
 #include "fbsd-tdep.h"
 #include "solib-svr4.h"
+#include "inferior.h"
 
 /* Support for signal handlers.  */
 
@@ -333,11 +334,13 @@ i386fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
 }
 
 static void
-i386fbsd_supply_uthread (struct regcache *regcache,
+i386fbsd_supply_uthread (struct regcache *regcache, ptid_t ptid,
 			 int regnum, CORE_ADDR addr)
 {
   gdb_byte buf[4];
   int i;
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -350,14 +353,18 @@ i386fbsd_supply_uthread (struct regcache *regcache,
 	  regcache_raw_supply (regcache, i, buf);
 	}
     }
+
+  do_cleanups (cleanup);
 }
 
 static void
-i386fbsd_collect_uthread (const struct regcache *regcache,
+i386fbsd_collect_uthread (const struct regcache *regcache, ptid_t ptid,
 			  int regnum, CORE_ADDR addr)
 {
   gdb_byte buf[4];
   int i;
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -370,6 +377,8 @@ i386fbsd_collect_uthread (const struct regcache *regcache,
 	  write_memory (addr + i386fbsd_jmp_buf_reg_offset[i], buf, 4);
 	}
     }
+
+  do_cleanups (cleanup);
 }
 
 static void
diff --git a/gdb/i386-obsd-tdep.c b/gdb/i386-obsd-tdep.c
index 22375c5b75..6d675da59f 100644
--- a/gdb/i386-obsd-tdep.c
+++ b/gdb/i386-obsd-tdep.c
@@ -35,6 +35,7 @@
 #include "i387-tdep.h"
 #include "solib-svr4.h"
 #include "bsd-uthread.h"
+#include "inferior.h"
 
 /* Support for signal handlers.  */
 
@@ -187,7 +188,7 @@ static int i386obsd_uthread_reg_offset[] =
 #define I386OBSD_UTHREAD_ESP_OFFSET	176
 
 static void
-i386obsd_supply_uthread (struct regcache *regcache,
+i386obsd_supply_uthread (struct regcache *regcache, ptid_t ptid,
 			 int regnum, CORE_ADDR addr)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
@@ -196,6 +197,8 @@ i386obsd_supply_uthread (struct regcache *regcache,
   CORE_ADDR sp = 0;
   gdb_byte buf[4];
   int i;
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -228,10 +231,12 @@ i386obsd_supply_uthread (struct regcache *regcache,
 	  regcache_raw_supply (regcache, i, buf);
 	}
     }
+
+  do_cleanups (cleanup);
 }
 
 static void
-i386obsd_collect_uthread (const struct regcache *regcache,
+i386obsd_collect_uthread (const struct regcache *regcache, ptid_t ptid,
 			  int regnum, CORE_ADDR addr)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
@@ -240,6 +245,8 @@ i386obsd_collect_uthread (const struct regcache *regcache,
   CORE_ADDR sp = 0;
   gdb_byte buf[4];
   int i;
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -276,6 +283,8 @@ i386obsd_collect_uthread (const struct regcache *regcache,
 	  write_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
 	}
     }
+
+  do_cleanups (cleanup);
 }
 
 /* Kernel debugging support.  */
diff --git a/gdb/sparc-obsd-tdep.c b/gdb/sparc-obsd-tdep.c
index b7d3d3cf47..1bfcd64c6f 100644
--- a/gdb/sparc-obsd-tdep.c
+++ b/gdb/sparc-obsd-tdep.c
@@ -31,6 +31,7 @@
 #include "sparc-tdep.h"
 #include "solib-svr4.h"
 #include "bsd-uthread.h"
+#include "inferior.h"
 
 /* Signal trampolines.  */
 
@@ -150,13 +151,15 @@ static const struct frame_unwind sparc32obsd_sigtramp_frame_unwind =
 #define SPARC32OBSD_UTHREAD_PC_OFFSET	132
 
 static void
-sparc32obsd_supply_uthread (struct regcache *regcache,
+sparc32obsd_supply_uthread (struct regcache *regcache, ptid_t ptid,
 			    int regnum, CORE_ADDR addr)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   CORE_ADDR fp, fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
   gdb_byte buf[4];
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -192,16 +195,20 @@ sparc32obsd_supply_uthread (struct regcache *regcache,
     }
 
   sparc_supply_rwindow (regcache, fp, regnum);
+
+  do_cleanups (cleanup);
 }
 
 static void
-sparc32obsd_collect_uthread(const struct regcache *regcache,
+sparc32obsd_collect_uthread(const struct regcache *regcache, ptid_t ptid,
 			    int regnum, CORE_ADDR addr)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   CORE_ADDR sp;
   gdb_byte buf[4];
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -228,6 +235,8 @@ sparc32obsd_collect_uthread(const struct regcache *regcache,
   regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
   sp = extract_unsigned_integer (buf, 4, byte_order);
   sparc_collect_rwindow (regcache, sp, regnum);
+
+  do_cleanups (cleanup);
 }
 
 
diff --git a/gdb/sparc64-obsd-tdep.c b/gdb/sparc64-obsd-tdep.c
index 0da50b1dc7..49ca460d03 100644
--- a/gdb/sparc64-obsd-tdep.c
+++ b/gdb/sparc64-obsd-tdep.c
@@ -32,6 +32,7 @@
 #include "sparc64-tdep.h"
 #include "solib-svr4.h"
 #include "bsd-uthread.h"
+#include "inferior.h"
 
 /* Older OpenBSD versions used the traditional NetBSD core file
    format, even for ports that use ELF.  These core files don't use
@@ -320,13 +321,15 @@ static const struct frame_unwind sparc64obsd_trapframe_unwind =
 #define SPARC64OBSD_UTHREAD_PC_OFFSET	240
 
 static void
-sparc64obsd_supply_uthread (struct regcache *regcache,
+sparc64obsd_supply_uthread (struct regcache *regcache, ptid_t ptid,
 			    int regnum, CORE_ADDR addr)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   CORE_ADDR fp, fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;
   gdb_byte buf[8];
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -362,16 +365,20 @@ sparc64obsd_supply_uthread (struct regcache *regcache,
     }
 
   sparc_supply_rwindow (regcache, fp, regnum);
+
+  do_cleanups (cleanup);
 }
 
 static void
-sparc64obsd_collect_uthread(const struct regcache *regcache,
+sparc64obsd_collect_uthread(const struct regcache *regcache, ptid_t ptid,
 			    int regnum, CORE_ADDR addr)
 {
   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   CORE_ADDR sp;
   gdb_byte buf[8];
+  struct cleanup *cleanup = save_inferior_ptid ();
+  inferior_ptid = ptid;
 
   gdb_assert (regnum >= -1);
 
@@ -398,6 +405,8 @@ sparc64obsd_collect_uthread(const struct regcache *regcache,
   regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
   sp = extract_unsigned_integer (buf, 8, byte_order);
   sparc_collect_rwindow (regcache, sp, regnum);
+
+  do_cleanups (cleanup);
 }
 
 
