public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Magne Hov <mhov@undo.io>
To: gdb-patches@sourceware.org
Cc: Magne Hov <mhov@undo.io>
Subject: [PATCH v2 1/2] gdb: keep record of thread numbers for reverse execution targets
Date: Fri,  7 Jul 2023 17:24:50 +0100	[thread overview]
Message-ID: <20230707162451.3605544-2-mhov@undo.io> (raw)
In-Reply-To: <20230707162451.3605544-1-mhov@undo.io>

Targets that support reverse execution may report threads that have
previously been seen to exit.  Currently, GDB assigns a new thread
number every time it sees the same thread (re)appearing, but this is
problematic because it makes the output of `info threads` inconsistent,
and because thread-specific breakpoints depend on stable thread-numbers
in order to stop on the right thread.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23454
---
 gdb/inferior.c |  1 +
 gdb/inferior.h |  7 +++++++
 gdb/thread.c   | 38 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/gdb/inferior.c b/gdb/inferior.c
index eee4785fbf7..b6323110e69 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -258,6 +258,7 @@ inferior::clear_thread_list (bool silent)
 	delete thr;
     });
   ptid_thread_map.clear ();
+  ptid_thread_num_map.clear ();
 }
 
 /* Notify interpreters and observers that inferior INF was removed.  */
diff --git a/gdb/inferior.h b/gdb/inferior.h
index caa8e4d494a..33eb857645e 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -569,6 +569,13 @@ class inferior : public refcounted_object,
   /* The highest thread number this inferior ever had.  */
   int highest_thread_num = 0;
 
+  /* A map of ptid_t to global thread number and per-inferior thread
+     number, used for targets that support reverse execution.  These
+     mappings are maintained for the lifetime of the inferior so that
+     thread numbers can be reused for threads that reappear after
+     having exited.  */
+  std::unordered_map<ptid_t, std::pair<int, int>, hash_ptid> ptid_thread_num_map;
+
   /* State of GDB control of inferior process execution.
      See `struct inferior_control_state'.  */
   inferior_control_state control;
diff --git a/gdb/thread.c b/gdb/thread.c
index 7f7f035b5ab..ef3da68fc30 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -290,6 +290,15 @@ add_thread_silent (process_stratum_target *targ, ptid_t ptid)
 			inf->num, ptid.to_string ().c_str (),
 			targ->shortname ());
 
+ /* Targets that support reverse execution can see the same thread
+    being added multiple times.  If the state for an exited thread is
+    still present in the inferior's thread list it must be cleaned up
+    now before we add a new non-exited entry for the same thread.
+    Targets without reverse execution are not affected by this because
+    they do not reuse thread numbers.  */
+  if (target_can_execute_reverse ())
+    delete_exited_threads ();
+
   /* We may have an old thread with the same id in the thread list.
      If we do, it must be dead, otherwise we wouldn't be adding a new
      thread with the same id.  The OS is reusing this id --- delete
@@ -332,8 +341,33 @@ thread_info::thread_info (struct inferior *inf_, ptid_t ptid_)
 {
   gdb_assert (inf_ != NULL);
 
-  this->global_num = ++highest_thread_num;
-  this->per_inf_num = ++inf_->highest_thread_num;
+  /* Targets that support reverse execution may see the same thread be
+     created multiple times so a historical record must be maintained
+     and queried.  For targets without reverse execution we don't look
+     up historical thread numbers because it leaves us vulnerable to
+     collisions between thread identifiers that have been recycled by
+     the target operating system.  */
+  if (target_can_execute_reverse ())
+    {
+      auto pair = inf_->ptid_thread_num_map.find (ptid_);
+      if (pair != inf_->ptid_thread_num_map.end ())
+	{
+	  this->global_num = pair->second.first;
+	  this->per_inf_num = pair->second.second;
+	}
+      else
+	{
+	  this->global_num = ++highest_thread_num;
+	  this->per_inf_num = ++inf_->highest_thread_num;
+	  inf_->ptid_thread_num_map[ptid_] = std::make_pair (this->global_num,
+							     this->per_inf_num);
+	}
+    }
+  else
+    {
+      this->global_num = ++highest_thread_num;
+      this->per_inf_num = ++inf_->highest_thread_num;
+    }
 
   /* Nothing to follow yet.  */
   this->pending_follow.set_spurious ();
-- 
2.40.1


  reply	other threads:[~2023-07-07 16:24 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-29  8:36 [PATCH 0/2] Improve handling " Magne Hov
2023-06-29  8:36 ` [PATCH 1/2] gdb: keep record " Magne Hov
2023-06-29  9:01   ` Lancelot SIX
2023-06-29  9:38     ` Magne Hov
2023-06-29  8:36 ` [PATCH 2/2] gdb: retain thread-specific breakpoints in " Magne Hov
2023-07-07 16:24 ` [PATCH v2 0/2] Improve handling of thread numbers for " Magne Hov
2023-07-07 16:24   ` Magne Hov [this message]
2023-07-13 12:21     ` [PATCH v2 1/2] gdb: keep record " Bruno Larsen
2023-09-19 16:33     ` Tom Tromey
2023-09-20 16:42       ` Pedro Alves
2023-09-20 17:00         ` Magne Hov
2023-09-20 17:13     ` Pedro Alves
2023-07-07 16:24   ` [PATCH v2 2/2] gdb: retain thread-specific breakpoints in " Magne Hov
2023-07-13 12:22     ` Bruno Larsen
2023-08-18 14:27   ` [PING][PATCH v2 0/2] Improve handling of thread numbers for " Magne Hov
2023-09-18 11:38     ` Magne Hov
2023-09-19 16:34   ` [PATCH " Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230707162451.3605544-2-mhov@undo.io \
    --to=mhov@undo.io \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).