[4/7] gdb: make regcache save a thread pointer, not ptid

Message ID 20261029205743.1917668-5-guinevere@redhat.com
State New
Headers
Series Start supporting multi-threaded inferiors with record-full |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 fail Test failed

Commit Message

Guinevere Larsen Oct. 29, 2026, 8:57 p.m. UTC
  Regcaches save a PTID as a way to for GDB to identify the thread that
the cache belongs to. This didn't use to be an issue because, until
now, it was only reasonable to touch regcaches of live threads.
However, when record-full is capable of replaying already-exited
threads, there will be times when we will need to find the thread_info
pointer for an exited thread, and for that case, inferior::find_thread
won't work.

This commit changes regcache so that it now saves a thread
pointer. One simplification that this change makes is that, when a
thread's ptid changes, we no longer need to update the regcache, so
the observer function and selftest can be simply removed. Another
future simplification that can be done is that callers of
regcache::ptid that only want to find a thread pointer can now call
regcache::thread, but this is left for the future to minimize
accidental impact of this specific commit.

This change caused a regression to the get_thread_arch_regcache
selftest, because we now need a real thread in the mock inferior for
each PTID. To fix this, the mock context can now add new threads with
the desired PTIDs. This mostly works, however, properly deleting the
memory for the threads caused the inferior destructor to de-reference
a null pointer as it tried to unlink the threads from the intrusive
list.

Finally, as a sidenote, ravenscar_thread sometimes needs to
temporarily change the ptid of a regcache for some operations that I
haven't quite understood. However, considering that the process for
getting the PTID will return the new one, I believe the change should
be trivial.

This is just some internal change, no user-visible changes are expected.
---
 gdb/ravenscar-thread.c    |  41 ++++++++++-----
 gdb/regcache.c            | 105 ++++----------------------------------
 gdb/regcache.h            |  17 +++---
 gdb/scoped-mock-context.h |  18 +++++++
 4 files changed, 67 insertions(+), 114 deletions(-)
  

Patch

diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index b5e0979a7f1..a5324be19c6 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -602,32 +602,33 @@  ravenscar_arch_ops::store_register (struct regcache *regcache,
 			stack_address);
 }
 
-/* Temporarily set the ptid of a regcache to some other value.  When
-   this object is destroyed, the regcache's original ptid is
+/* Temporarily set the thread of a regcache to some other value.  When
+   this object is destroyed, the regcache's original thread is
    restored.  */
 
-class temporarily_change_regcache_ptid
+class temporarily_change_regcache_thread
 {
 public:
 
-  temporarily_change_regcache_ptid (struct regcache *regcache, ptid_t new_ptid)
+  temporarily_change_regcache_thread (struct regcache *regcache,
+				    thread_info *new_thread)
     : m_regcache (regcache),
-      m_save_ptid (regcache->ptid ())
+      m_save_thread (regcache->thread ())
   {
-    m_regcache->set_ptid (new_ptid);
+    m_regcache->set_thread (new_thread);
   }
 
-  ~temporarily_change_regcache_ptid ()
+  ~temporarily_change_regcache_thread ()
   {
-    m_regcache->set_ptid (m_save_ptid);
+    m_regcache->set_thread (m_save_thread);
   }
 
 private:
 
   /* The regcache.  */
   struct regcache *m_regcache;
-  /* The saved ptid.  */
-  ptid_t m_save_ptid;
+  /* The saved thread.  */
+  thread_info *m_save_thread;
 };
 
 ravenscar_thread_target::fpu_state
@@ -709,7 +710,12 @@  ravenscar_thread_target::fetch_registers (struct regcache *regcache,
 
 	  if (use_beneath)
 	    {
-	      temporarily_change_regcache_ptid changer (regcache, base);
+	      process_stratum_target *proc_target
+		= as_process_stratum_target (this->beneath ());
+	      thread_info *base_thread = proc_target->find_thread (base);
+
+	      temporarily_change_regcache_thread changer (regcache,
+							  base_thread);
 	      beneath ()->fetch_registers (regcache, i);
 	    }
 	  else
@@ -755,7 +761,12 @@  ravenscar_thread_target::store_registers (struct regcache *regcache,
 
 	  if (use_beneath)
 	    {
-	      temporarily_change_regcache_ptid changer (regcache, base);
+	      process_stratum_target *proc_target
+		= as_process_stratum_target (this->beneath ());
+	      thread_info *base_thread = proc_target->find_thread (base);
+
+	      temporarily_change_regcache_thread changer (regcache,
+							  base_thread);
 	      beneath ()->store_registers (regcache, i);
 	    }
 	  else
@@ -776,7 +787,11 @@  ravenscar_thread_target::prepare_to_store (struct regcache *regcache)
       if (task_is_currently_active (ptid))
 	{
 	  ptid_t base = get_base_thread_from_ravenscar_task (ptid);
-	  temporarily_change_regcache_ptid changer (regcache, base);
+	  process_stratum_target *proc_target
+	    = as_process_stratum_target (this->beneath ());
+	  thread_info *tp = proc_target->find_thread (base);
+
+	  temporarily_change_regcache_thread changer (regcache, tp);
 	  beneath ()->prepare_to_store (regcache);
 	}
       else
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 773e8ac37d0..755b92d48ee 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -214,7 +214,7 @@  regcache::regcache (inferior *inf_for_target_calls, gdbarch *gdbarch)
   : detached_regcache (gdbarch, false),
     m_inf_for_target_calls (inf_for_target_calls)
 {
-  m_ptid = minus_one_ptid;
+  m_thread = nullptr;
 }
 
 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
@@ -390,7 +390,7 @@  get_thread_arch_regcache (inferior *inf_for_target_calls, ptid_t ptid,
 
   /* It does not exist, create it.  */
   regcache *new_regcache = new regcache (inf_for_target_calls, arch);
-  new_regcache->set_ptid (ptid);
+  new_regcache->set_thread (inf_for_target_calls->find_thread (ptid));
   /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
      constructor explicitly instead of implicitly.  */
   ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache)));
@@ -455,37 +455,6 @@  regcache_observer_target_changed (struct target_ops *target)
   registers_changed ();
 }
 
-/* Update regcaches related to OLD_PTID to now use NEW_PTID.  */
-static void
-regcache_thread_ptid_changed (process_stratum_target *target,
-			      ptid_t old_ptid, ptid_t new_ptid)
-{
-  /* Look up map for target.  */
-  auto pid_ptid_regc_map_it = regcaches.find (target);
-  if (pid_ptid_regc_map_it == regcaches.end ())
-    return;
-
- /* Look up map for pid.  */
-  pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
-  auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ());
-  if (ptid_regc_map_it == pid_ptid_regc_map.end ())
-    return;
-
-  /* Update all regcaches belonging to old_ptid.  */
-  ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
-  auto range = ptid_regc_map.equal_range (old_ptid);
-  for (auto it = range.first; it != range.second;)
-    {
-      regcache_up rc = std::move (it->second);
-      rc->set_ptid (new_ptid);
-
-      /* Remove old before inserting new, to avoid rehashing,
-	 which would invalidate iterators.  */
-      it = ptid_regc_map.erase (it);
-      ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc)));
-    }
-}
-
 /* Low level examining and depositing of registers.
 
    The caller is responsible for making sure that the inferior is
@@ -1703,6 +1672,12 @@  struct regcache_test_data
     : test_ctx_1 (current_inferior ()->arch ()),
       test_ctx_2 (current_inferior ()->arch ())
   {
+    /* Ensure we have all the threads we need to run the regcache tests.  */
+    std::vector<ptid_t> ptids
+      = {ptid_t (1,2), ptid_t (1,3),
+	 ptid_t (2,1), ptid_t (2,2), ptid_t (2, 3)};
+    test_ctx_1.create_additional_threads (ptids.data (), ptids.size ());
+    test_ctx_2.create_additional_threads (ptids.data (), ptids.size ());
     /* Ensure the regcaches container is empty at the start.  */
     registers_changed ();
   }
@@ -1945,7 +1920,7 @@  cooked_read_test (struct gdbarch *gdbarch)
 				    &mockctx.mock_inferior, gdbarch)))
 	 ->second;
 
-  readwrite.set_ptid (mockctx.mock_ptid);
+  readwrite.set_thread (&mockctx.mock_thread);
 
   gdb::byte_vector buf (register_size (gdbarch, nonzero_regnum));
   readwrite.raw_read (nonzero_regnum, buf);
@@ -2072,7 +2047,7 @@  cooked_write_test (struct gdbarch *gdbarch)
 				    &ctx.mock_inferior, gdbarch)))
 	 ->second;
 
-  readwrite.set_ptid (ctx.mock_ptid);
+  readwrite.set_thread (&ctx.mock_thread);
   const int num_regs = gdbarch_num_cooked_regs (gdbarch);
 
   for (auto regnum = 0; regnum < num_regs; regnum++)
@@ -2155,62 +2130,6 @@  cooked_write_test (struct gdbarch *gdbarch)
 
   regcaches.erase (&ctx.mock_target);
 }
-
-/* Verify that when two threads with the same ptid exist (from two different
-   targets) and one of them changes ptid, we only update the appropriate
-   regcaches.  */
-
-static void
-regcache_thread_ptid_changed ()
-{
-  /* This test relies on the global regcache list to initially be empty.  */
-  registers_changed ();
-
-  /* Any arch will do.  */
-  gdbarch *arch = current_inferior ()->arch ();
-
-  /* Prepare two targets with one thread each, with the same ptid.  */
-  scoped_mock_context<test_target_ops> target1 (arch);
-  scoped_mock_context<test_target_ops> target2 (arch);
-
-  ptid_t old_ptid (111, 222);
-  ptid_t new_ptid (111, 333);
-
-  target1.mock_inferior.pid = old_ptid.pid ();
-  target1.mock_thread.ptid = old_ptid;
-  target1.mock_inferior.ptid_thread_map.clear ();
-  target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread;
-
-  target2.mock_inferior.pid = old_ptid.pid ();
-  target2.mock_thread.ptid = old_ptid;
-  target2.mock_inferior.ptid_thread_map.clear ();
-  target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread;
-
-  gdb_assert (regcaches.empty ());
-
-  /* Populate the regcaches container.  */
-  get_thread_arch_regcache (&target1.mock_inferior, old_ptid, arch);
-  get_thread_arch_regcache (&target2.mock_inferior, old_ptid, arch);
-
-  gdb_assert (regcaches.size () == 2);
-  gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
-  gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0);
-  gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
-  gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
-
-  thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
-
-  gdb_assert (regcaches.size () == 2);
-  gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0);
-  gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1);
-  gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
-  gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
-
-  /* Leave the regcache list empty.  */
-  registers_changed ();
-  gdb_assert (regcaches.empty ());
-}
-
 } /* namespace selftests */
 #endif /* GDB_SELF_TEST */
 
@@ -2220,8 +2139,6 @@  INIT_GDB_FILE (regcache)
 
   gdb::observers::target_changed.attach (regcache_observer_target_changed,
 					 "regcache");
-  gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed,
-					      "regcache");
 
   cmd_list_element *maintenance_flush_register_cache_cmd
     = add_cmd ("register-cache", class_maintenance, reg_flush_command,
@@ -2249,7 +2166,5 @@  INIT_GDB_FILE (regcache)
 					 selftests::cooked_read_test);
   selftests::register_test_foreach_arch ("regcache::cooked_write_test",
 					 selftests::cooked_write_test);
-  selftests::register_test ("regcache_thread_ptid_changed",
-			    selftests::regcache_thread_ptid_changed);
 #endif
 }
diff --git a/gdb/regcache.h b/gdb/regcache.h
index 66e78a7c2fb..a69ed8a8401 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -24,11 +24,11 @@ 
 #include "gdbsupport/common-regcache.h"
 #include "gdbsupport/function-view.h"
 #include "gdbsupport/traits.h"
+#include "gdbthread.h"
 
 struct regcache;
 struct regset;
 struct gdbarch;
-class thread_info;
 struct process_stratum_target;
 struct inferior;
 class ui_out;
@@ -449,14 +449,19 @@  class regcache : public detached_regcache
 
   ptid_t ptid () const
   {
-    gdb_assert (m_ptid != minus_one_ptid);
+    return thread ()->ptid;
+  }
+
+  thread_info *thread () const
+  {
+    gdb_assert (m_thread != nullptr);
 
-    return m_ptid;
+    return m_thread;
   }
 
-  void set_ptid (const ptid_t ptid)
+  void set_thread (thread_info *tp)
   {
-    this->m_ptid = ptid;
+    this->m_thread = tp;
   }
 
   /* Return a string with the contents of a register, suitable for debug output.  */
@@ -497,7 +502,7 @@  class regcache : public detached_regcache
 
   /* If this is a read-write cache, which thread's registers is
      it connected to?  */
-  ptid_t m_ptid;
+  thread_info *m_thread;
 
   friend regcache *get_thread_arch_regcache (inferior *inf_for_target_calls,
 					     ptid_t ptid, gdbarch *gdbarch);
diff --git a/gdb/scoped-mock-context.h b/gdb/scoped-mock-context.h
index 165abef5063..ce98f51dceb 100644
--- a/gdb/scoped-mock-context.h
+++ b/gdb/scoped-mock-context.h
@@ -41,6 +41,8 @@  struct scoped_mock_context
   program_space mock_pspace {new_address_space ()};
   inferior mock_inferior {mock_ptid.pid ()};
   thread_info mock_thread {&mock_inferior, mock_ptid};
+  thread_info **additional_threads = nullptr;
+  int num_additional_threads = 0;
 
   scoped_restore_current_pspace_and_thread restore_pspace_thread;
 
@@ -72,6 +74,22 @@  struct scoped_mock_context
   {
     inferior_list.erase (inferior_list.iterator_to (mock_inferior));
     mock_inferior.pop_all_targets_at_and_above (process_stratum);
+    /* Unfortunately, here, we may leak some memory from additional_threads
+       If we delete them now, we will get a segmentation fault when deleting
+       the inferior.  Since this is only used when executing some selftests
+       it shouldn't be a big deal.  */
+  }
+
+  void create_additional_threads (ptid_t *ptids, int num)
+  {
+    num_additional_threads = num;
+    additional_threads = new thread_info *[num];
+    for (int i = 0; i < num; i++)
+      {
+	additional_threads[i] = new thread_info (&mock_inferior, ptids[i]);
+	mock_inferior.thread_list.push_back (*additional_threads[i]);
+	mock_inferior.ptid_thread_map[ptids[i]] = additional_threads[i];
+      }
   }
 };