From: Guinevere Larsen <guinevere@redhat.com>
To: gdb-patches@sourceware.org
Cc: Guinevere Larsen <guinevere@redhat.com>
Subject: [PATCH 3/7] gdb/record: Make GDB automatically switch threads when replaying
Date: Thu, 29 Oct 2026 17:57:39 -0300 [thread overview]
Message-ID: <20261029205743.1917668-4-guinevere@redhat.com> (raw)
In-Reply-To: <20261029205743.1917668-1-guinevere@redhat.com>
The next step in supporting multi-threaded inferiors with record-full
is being able to switch threads automatically while the inferior
replays. To do so, we no longer tell the instruction which regcache
needs to be changed, instead the instruction will fetch it from the
associated thread.
For convenience, the exec_insn method also returns the thread that was
altered by it. record-full's wait function uses that to compare to the
thread where the execution command was issued, and if they're not the
same, it won't count the execution of a step/next as completed (unless
the inferior would be stopped by something else, such as a
breakpoint). If the inferior is stopped by another event, the altered
thread is set as the current thread.
This commit just manages the general case of thread
execution. Recording through thread creation or destruction is not
supported yet, those are left for future commits, to simplify future
bisecting as much as possible.
This commit also updates the tests to check the replayability function.
---
gdb/record-full.c | 60 +++++++++++++------
.../gdb.reverse/pthreads-precsave.exp | 18 +++++-
.../gdb.reverse/pthreads-reverse.exp | 12 ++++
3 files changed, 70 insertions(+), 20 deletions(-)
diff --git a/gdb/record-full.c b/gdb/record-full.c
index 9b84be2e11a..146447adfbb 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -439,9 +439,11 @@ struct record_full_instruction
/* The thread that executed this instruction. */
thread_info *thread = nullptr;
- /* Execute the full instruction. As a side effect, set
- record_full_stop_reason. */
- void exec_insn (regcache *regcache);
+ /* Execute the full instruction. As side effects, may set
+ record_full_stop_reason (if a watchpoint is triggered by
+ executing this instruction). Returns the PTID of the
+ thread that it just modified. */
+ thread_info *exec_insn ();
/* Create a full recorded instruction from a bfd. */
static void from_bfd (bfd *cbfd, asection* osec, int *bfd_offset,
@@ -919,14 +921,16 @@ static enum target_stop_reason record_full_stop_reason
/* Execute one entry in the log by executing all the effects. */
-void record_full_instruction::exec_insn (regcache *regcache)
+thread_info *record_full_instruction::exec_insn ()
{
- /* Assert that the regcache we were given belongs to the correct thread. */
- gdb_assert (thread->ptid == regcache->ptid ());
+ regcache *regcache = get_thread_regcache (thread);
+
pc.execute (regcache);
for (auto &entry : effects)
if (entry.execute (regcache))
record_full_stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
+
+ return thread;
}
static void record_full_restore (struct bfd &cbfd);
@@ -1332,7 +1336,10 @@ record_full_wait_1 (struct target_ops *ops,
{
switch_to_thread (current_inferior ()->process_target (),
record_full_resume_ptid);
- regcache *regcache = get_thread_regcache (inferior_thread ());
+ thread_info *current_thread = inferior_thread ();
+ /* Last thread that was executed, in case we need to switch to it. */
+ thread_info * last_thread = current_thread;
+ regcache *regcache = get_thread_regcache (current_thread);
struct gdbarch *gdbarch = regcache->arch ();
const address_space *aspace = current_inferior ()->aspace.get ();
int continue_flag = 1;
@@ -1377,15 +1384,33 @@ record_full_wait_1 (struct target_ops *ops,
/* Check for beginning and end of log. */
if (execution_direction == EXEC_REVERSE
&& record_full_next_insn < 0)
- break;
+ {
+ if (last_thread != current_thread)
+ {
+ switch_to_thread (last_thread);
+ current_thread->set_state (THREAD_STOPPED);
+ current_thread->set_internal_state (THREAD_INT_STOPPED);
+ }
+ break;
+ }
else if (execution_direction != EXEC_REVERSE
- && record_full_next_insn == record_full_log.size ())
- break;
+ && record_full_next_insn == record_full_log.size ())
+ {
+ if (last_thread != current_thread)
+ {
+ switch_to_thread (last_thread);
+ current_thread->set_state (THREAD_STOPPED);
+ current_thread->set_internal_state (THREAD_INT_STOPPED);
+ }
+ break;
+ }
- record_full_log[record_full_next_insn].exec_insn (regcache);
+ last_thread
+ = record_full_log[record_full_next_insn].exec_insn ();
- /* step */
- if (record_full_resume_step)
+ /* We only count a single step as completed if we stepped the
+ same thread that we started with. */
+ if (record_full_resume_step && current_thread == last_thread)
{
if (record_debug > 1)
gdb_printf (gdb_stdlog,
@@ -2806,7 +2831,7 @@ record_full_base_target::save_record (const char *recfilename)
/* Reverse execute to the begin of record list. */
for (int i = record_full_next_insn - 1; i >= 0; i--)
- record_full_log[i].exec_insn (regcache);
+ record_full_log[i].exec_insn ();
/* Create a table relating this session's global ID and the
PTID of the thread, for reading the corefile in the future. */
@@ -2898,7 +2923,7 @@ record_full_base_target::save_record (const char *recfilename)
record_full_log[i].to_bfd (obfd, osec, &bfd_offset, gdbarch);
/* Execute entry. */
if (i < record_full_next_insn)
- record_full_log[i].exec_insn (regcache);
+ record_full_log[i].exec_insn ();
}
unlink_file.keep ();
@@ -2918,17 +2943,16 @@ record_full_goto_insn (size_t target_insn,
{
scoped_restore restore_operation_disable
= record_full_gdb_operation_disable_set ();
- regcache *regcache = get_thread_regcache (inferior_thread ());
/* Assume everything is valid: we will hit the entry,
and we will not hit the end of the recording. */
if (dir == EXEC_REVERSE)
for (int i = record_full_next_insn; i > target_insn; i--)
- record_full_log[i - 1].exec_insn (regcache);
+ record_full_log[i - 1].exec_insn ();
else
for (int i = record_full_next_insn; i < target_insn; i++)
- record_full_log[i].exec_insn (regcache);
+ record_full_log[i].exec_insn ();
record_full_next_insn = target_insn;
}
diff --git a/gdb/testsuite/gdb.reverse/pthreads-precsave.exp b/gdb/testsuite/gdb.reverse/pthreads-precsave.exp
index 53702ed26fa..c9f69f88a20 100644
--- a/gdb/testsuite/gdb.reverse/pthreads-precsave.exp
+++ b/gdb/testsuite/gdb.reverse/pthreads-precsave.exp
@@ -53,8 +53,9 @@ gdb_test "next" "y = 2.*" "Record thread 2"
gdb_test "thread 1" ".Switching to thread 1.*"
gdb_test "next" "end of main.*" "Record thread 1"
-gdb_test "reverse-next" ".*"
-gdb_test "thread 2" ".*"
+gdb_test "thread 2" ".Switching to thread 2.*" \
+ "Returning to thread 2"
+gdb_test "next" "return .void.*" "record thread 2 some more"
gdb_test "record save $precsave" \
"Saved core file $precsave with execution log\." \
@@ -91,3 +92,16 @@ if { $main_thread == -1 } {
gdb_test "thread" ".*thread is $other_thread.*" \
"identify starting thread"
+gdb_test "reverse-next" ".*Backward execution from here not possible." \
+ "check start of history"
+gdb_test "continue" ".*Following forward execution will be added to history." \
+ "move to the end of history"
+
+gdb_test "thread" ".*thread is $other_thread.*" \
+ "identify last thread"
+gdb_test "thread $main_thread" ".*end of main.*" \
+ "Switch to main thread"
+gdb_test "reverse-next" ".*TAG: PTHREAD SUCCESSFUL.*" \
+ "Return one line in main"
+
+gdb_test "thread $other_thread" "y = 2.*" "check position of other thread"
diff --git a/gdb/testsuite/gdb.reverse/pthreads-reverse.exp b/gdb/testsuite/gdb.reverse/pthreads-reverse.exp
index b85bd572672..1ef1162acd1 100644
--- a/gdb/testsuite/gdb.reverse/pthreads-reverse.exp
+++ b/gdb/testsuite/gdb.reverse/pthreads-reverse.exp
@@ -81,3 +81,15 @@ gdb_test "next" "end of main.*" "Record thread 1"
with_test_prefix "thread 1" {
check_recorded_thread true
}
+
+# Testing replaying through thread switches.
+gdb_test "thread 2" ".Switching to thread 2.*" "Switching for testing purposes"
+gdb_test "reverse-step" "x = 1.*" "step backwards through a thread change"
+gdb_test "continue" ".*thread 1.*" "continue forwards through one thread change"
+
+# Testing additional thread switches.
+gdb_test "thread 2" ".Switching to thread 2.*" \
+ "Record further on thread 2"
+gdb_test "next" "return .void ..*" "record some more on thread 2"
+gdb_test "reverse-continue" "x = 1.*" "continue backwards through history thread change"
+gdb_test "continue" "return .void .*" "continue forwards through multiple thread change"
--
2.56.0
next prev parent reply other threads:[~2026-10-29 20:59 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 20:57 [PATCH 0/7] Start supporting multi-threaded inferiors with record-full Guinevere Larsen
2026-10-29 20:57 ` [PATCH 1/7] gdb/record-full: Add thread pointer to recorded instructions Guinevere Larsen
2026-10-29 20:57 ` [PATCH 2/7] gdb/record: add thread ID to the precsave format Guinevere Larsen
2026-10-29 20:57 ` Guinevere Larsen [this message]
2026-10-29 20:57 ` [PATCH 4/7] gdb: make regcache save a thread pointer, not ptid Guinevere Larsen
2026-10-29 20:57 ` [PATCH 5/7] gdb/record: Record through a thread exiting Guinevere Larsen
2026-10-29 20:57 ` [PATCH 6/7] gdb/record: Loudly warn or fail on dubious situations Guinevere Larsen
2026-10-29 20:57 ` [PATCH 7/7] gdb/record: document recording multi-threaded inferiors Guinevere Larsen
2026-10-30 6:43 ` Eli Zaretskii
2026-10-30 11:32 ` Guinevere Larsen
2026-10-30 12:44 ` Eli Zaretskii
2026-10-30 13:20 ` Guinevere Larsen
2026-10-30 13:38 ` Eli Zaretskii
2026-10-30 13:40 ` Guinevere Larsen
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=20261029205743.1917668-4-guinevere@redhat.com \
--to=guinevere@redhat.com \
--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