Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH 0/7] Start supporting multi-threaded inferiors with record-full
@ 2026-10-29 20:57 Guinevere Larsen
  2026-10-29 20:57 ` [PATCH 1/7] gdb/record-full: Add thread pointer to recorded instructions Guinevere Larsen
                   ` (6 more replies)
  0 siblings, 7 replies; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-29 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

Record full has never been capable of managing multi-threaded
inferiors, but it never really told the user about that, instead
silently making things fail or infinitely loop. This series finally
changes that, making us truly support multi-threading.

For now, we are only able to record when manually scheduling, because
of issues with how to have multiple instructions at once, how to
manage waits for locks, and especially how to manage deadlocks, if
they should be managed at all. Also, we're unable to record the
creation of a thread, because we need to have a status for "not
created yet", needs to be removed from the inferior list and so
on. The latter is not too hard of an edge case, but I had limited time
to work on this, I decided to keep it for a later series, since the
current one already is an improvement on the situation.

The fourth patch is the most surprising one in my opinion. This is
needed because of the fifth patch, when we start dealing with threads
that have been exited, having the ptid isn't enough to find the thread
pointer in the inferior. In theory it would be possible to remove that
patch, but there are many edge cases to finding the instruction, which
would cause crashes. But I also personally think that using the thread
pointer is cleaner than the PTID, so I didn't try too hard without the
regcache change.

I am about to leave for a vacation, so it will be a while before I am
able to respond to feedback, but I felt it was important to send the
patch now to have a chance of merging this year or early next year.

Guinevere Larsen (7):
  gdb/record-full: Add thread pointer to recorded instructions
  gdb/record: add thread ID to the precsave format
  gdb/record: Make GDB automatically switch threads when replaying
  gdb: make regcache save a thread pointer, not ptid
  gdb/record: Record through a thread exiting
  gdb/record: Loudly warn or fail on dubious situations
  gdb/record: document recording multi-threaded inferiors

 gdb/NEWS                                      |   5 +
 gdb/doc/gdb.texinfo                           |   9 +-
 gdb/gdbthread.h                               |   2 +
 gdb/infcmd.c                                  |   6 +
 gdb/infrun.c                                  |   3 +
 gdb/linux-record.c                            |  18 +-
 gdb/ravenscar-thread.c                        |  41 +-
 gdb/record-full.c                             | 409 ++++++++++++++++--
 gdb/record-full.h                             |  16 +
 gdb/record.c                                  |   6 +
 gdb/regcache.c                                | 105 +----
 gdb/regcache.h                                |  17 +-
 gdb/scoped-mock-context.h                     |  18 +
 .../gdb.reverse/basic-record-full.exp         |   5 +-
 .../gdb.reverse/finish-reverse-next.exp       |  10 +-
 .../gdb.reverse/i386-avx-reverse.exp          |  39 +-
 gdb/testsuite/gdb.reverse/insn-reverse.exp    |   3 +-
 .../gdb.reverse/pthreads-precsave.exp         | 107 +++++
 gdb/testsuite/gdb.reverse/pthreads-reverse.c  |  40 ++
 .../gdb.reverse/pthreads-reverse.exp          | 128 ++++++
 gdb/testsuite/gdb.reverse/waitpid-reverse.c   |   3 +-
 gdb/testsuite/gdb.reverse/waitpid-reverse.exp |  10 +-
 gdb/thread.c                                  |  19 +-
 23 files changed, 827 insertions(+), 192 deletions(-)
 create mode 100644 gdb/testsuite/gdb.reverse/pthreads-precsave.exp
 create mode 100644 gdb/testsuite/gdb.reverse/pthreads-reverse.c
 create mode 100644 gdb/testsuite/gdb.reverse/pthreads-reverse.exp


base-commit: f747b0cd54bf4d2d7e0fcbabcf5b8c3377847b4d
-- 
2.56.0


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

* [PATCH 1/7] gdb/record-full: Add thread pointer to recorded instructions
  2026-10-29 20:57 [PATCH 0/7] Start supporting multi-threaded inferiors with record-full Guinevere Larsen
@ 2026-10-29 20:57 ` Guinevere Larsen
  2026-10-29 20:57 ` [PATCH 2/7] gdb/record: add thread ID to the precsave format Guinevere Larsen
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-29 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

To be able to properly support multi-threaded inferiors with the
record-full subsystem, we need some indication of which thread executed
a given instruction, so that we update the correct register cache.

This commit makes that change, and asserts that the instruction is
changing the right regcache, so that (as of yet) unsupported workflows
are now loudly unsupported, while making no changes for supported
workflows.

It also adds a test that does very basic checking that we record the
information correctly, and that we're capable of executing backwards
while manually switching threads. Support for automatically executing
will be added in a future commit in this series, to minimize changes
in a single commit.
---
 gdb/record-full.c                             | 10 +++
 .../gdb.reverse/i386-avx-reverse.exp          |  3 +-
 gdb/testsuite/gdb.reverse/pthreads-reverse.c  | 40 +++++++++
 .../gdb.reverse/pthreads-reverse.exp          | 83 +++++++++++++++++++
 4 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.reverse/pthreads-reverse.c
 create mode 100644 gdb/testsuite/gdb.reverse/pthreads-reverse.exp

diff --git a/gdb/record-full.c b/gdb/record-full.c
index 7b1d64069c7..66df6c85aa3 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -419,6 +419,7 @@ class record_full_entry
    * sigval: Whether the inferior received a signal while the following
      instruction was being recorded;
    * pc: The Program Counter at the start of the instruction;
+   * thread: The thread that executed the instruction.
    * effects: A list of record_full_entry structures, each of which
      describing one effect that the instruction has on the inferior.
 
@@ -434,6 +435,8 @@ struct record_full_instruction
   std::optional<gdb_signal> sigval;
   std::vector<record_full_entry> effects;
   record_full_reg_entry pc;
+  /* The thread that executed this instruction.  */
+  thread_info *thread = nullptr;
 
   /* Execute the full instruction.  As a side effect, set
      record_full_stop_reason.  */
@@ -647,6 +650,7 @@ record_full_reset_incomplete ()
   record_full_incomplete_instruction.effects.clear ();
   record_full_incomplete_instruction.sigval.reset ();
   record_full_incomplete_instruction.insn_num = 0;
+  record_full_incomplete_instruction.thread = nullptr;
 }
 
 /* See record-full.h.  */
@@ -712,6 +716,7 @@ record_full_save_instruction ()
 {
   ++record_full_insn_count;
   record_full_incomplete_instruction.insn_num = record_full_insn_count;
+  record_full_incomplete_instruction.thread = inferior_thread ();
   record_full_incomplete_instruction.effects.shrink_to_fit ();
   record_full_log.push_back (std::move (record_full_incomplete_instruction));
   record_full_next_insn++;
@@ -914,6 +919,8 @@ static enum target_stop_reason record_full_stop_reason
 
 void record_full_instruction::exec_insn (regcache *regcache)
 {
+  /* Assert that the regcache we were given belongs to the correct thread.  */
+  gdb_assert (thread->ptid == regcache->ptid ());
   pc.execute (regcache);
   for (auto &entry : effects)
     if (entry.execute (regcache))
@@ -2836,6 +2843,9 @@ maintenance_print_record_instruction (const char *args, int from_tty)
 	      gdbarch_register_name (arch, to_print->pc.num));
   value_print (val, gdb_stdout, opts);
   gdb_printf ("\n");
+
+  gdb_printf ("Instruction executed on thread %s\n",
+	      to_print->thread->ptid.to_string ().c_str ());
 }
 
 INIT_GDB_FILE (record_full)
diff --git a/gdb/testsuite/gdb.reverse/i386-avx-reverse.exp b/gdb/testsuite/gdb.reverse/i386-avx-reverse.exp
index a5b0e6612aa..f1012a7a96d 100644
--- a/gdb/testsuite/gdb.reverse/i386-avx-reverse.exp
+++ b/gdb/testsuite/gdb.reverse/i386-avx-reverse.exp
@@ -461,7 +461,8 @@ if {[record_full_function "vzeroupper"] == true} {
 	    "Register ymm13h changed: ${decimal}" \
 	    "Register ymm14h changed: ${decimal}" \
 	    "Register ymm15h changed: 3405707982" \
-	    "Register rip changed: \[^\r\n\]+" ] \
+	    "Register rip changed: \[^\r\n\]+" \
+	     "Instruction executed on thread ${decimal}.${decimal}.0" ] \
 	"verify vzeroupper recording"
 
     gdb_test "record stop" "Process record is stopped.*" \
diff --git a/gdb/testsuite/gdb.reverse/pthreads-reverse.c b/gdb/testsuite/gdb.reverse/pthreads-reverse.c
new file mode 100644
index 00000000000..c172d30d810
--- /dev/null
+++ b/gdb/testsuite/gdb.reverse/pthreads-reverse.c
@@ -0,0 +1,40 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2026 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <pthread.h>
+#include <stdint.h>
+
+static void *
+thread_func (void *arg)
+{
+  int x, y;
+  x = 1;
+  y = 2;
+  return (void *) (uintptr_t) (x + y);
+}
+
+int main () {
+  pthread_t tid;
+
+  if (pthread_create (&tid, NULL, thread_func, NULL) != 0)
+    return 1; /* TAG: THREAD CREATION FAIL.  */
+
+  /* Small delay, just to make it easier to read the results.  */
+  for (int i = 0; i < 10; i++); /* TAG: PTHREAD SUCCESSFUL.  */
+
+  return 0; /* end of main */
+}
diff --git a/gdb/testsuite/gdb.reverse/pthreads-reverse.exp b/gdb/testsuite/gdb.reverse/pthreads-reverse.exp
new file mode 100644
index 00000000000..b85bd572672
--- /dev/null
+++ b/gdb/testsuite/gdb.reverse/pthreads-reverse.exp
@@ -0,0 +1,83 @@
+# Copyright 2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+# This file is part of the GDB testsuite.  It tests GDB's ability to
+# record multithreaded programs.
+
+require supports_reverse
+
+standard_testfile
+
+# Check the thread that a recorded instruction executed in.  EQUAL tells
+# whether we should expect pid and tid to be the same (which is the
+# equivalent of thread 1), or different. The exact numbers aren't important.
+proc_with_prefix check_recorded_thread { equal } {
+    set PID -1
+    set TID -1
+    set LWP -1
+
+    gdb_test_multiple "maint print record-instruction" "get recorded PTID" {
+	-re -wrap "Instruction executed on thread \(${::decimal}\)\.\(${::decimal}\)\.\(${::decimal}\)" {
+	    pass $gdb_test_name
+	    set PID $expect_out(1,string)
+	    set TID $expect_out(2,string)
+	    set LWP $expect_out(3,string)
+	}
+    }
+
+    if { $equal } {
+	gdb_assert { $PID == $TID && $LWP == 0 };
+    } else {
+	gdb_assert { $PID != $TID && $LWP == 0};
+    }
+}
+
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
+    return
+}
+
+runto_main
+
+gdb_test_no_output "set scheduler-locking on"
+
+gdb_breakpoint "thread_func"
+
+# Recording thread creation is not trivial, and so this basic test
+# just skips that complex use case for now.
+if {[gdb_test "next" "TAG: PTHREAD SUCCESSFUL.*" \
+     "next over pthread creation"] != 0} {
+    untested "Couldn't start pthreads"
+    return
+}
+
+gdb_test "thread 2" ".Switching to thread 2 .*"
+gdb_continue_to_breakpoint "thread_func" ".*thread_func.*"
+
+if {[supports_process_record]} {
+    # Activate process record/replay
+    gdb_test_no_output "record" "turn on process record"
+}
+
+gdb_test "next" "y = 2.*" "Record thread 2"
+with_test_prefix "thread 2" {
+    check_recorded_thread false
+}
+
+gdb_test "thread 1" ".Switching to thread 1.*"
+gdb_test "next" "end of main.*" "Record thread 1"
+with_test_prefix "thread 1" {
+    check_recorded_thread true
+}
-- 
2.56.0


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

* [PATCH 2/7] gdb/record: add thread ID to the precsave format
  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 ` Guinevere Larsen
  2026-10-29 20:57 ` [PATCH 3/7] gdb/record: Make GDB automatically switch threads when replaying Guinevere Larsen
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-29 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

With the addition of the thread pointer to the instruction, the
`record save' functionality should now also record this
information. However, the pointer will not be the same across GDB
executions. Even the thread's global ID (GID) won't be the same, but
saving the PTID for every single instruction is overkill.

To solve this, the new recording format starts with a table that
relates GIDs from the current session to PTIDs of the threads, so that
we can store the GIDs for each instruction. With this, the process of
reading an instruction now includes: Reading the old thread GID, looks
in the table for the PTID, then finds the pointer for the thread in
this session.

This changes the precsave format, so older formats will no longer be
supported.  It also adds a test to ensure that GDB is able to generate
a precsave file with multiple threads, and read it correctly.  The
test needs to walk back to the start of the log manually because GDB
isn't able to automatically switch threads yet, so attempting to
"record save" will cause an assert to fail if we're not at the
start. This will be fixed in a following commit in the same series

Another issue that has to be solved is that, until now, we only needed
one detached_regcache to emulate the execution, but now we need
multiple regcaches to emulate all the threads. This commit does that
by adding an std::map between global_num and the detached_regcache, so
that we can easily find it during execution.
---
 gdb/record-full.c                             | 212 ++++++++++++++++--
 .../gdb.reverse/pthreads-precsave.exp         |  93 ++++++++
 2 files changed, 284 insertions(+), 21 deletions(-)
 create mode 100644 gdb/testsuite/gdb.reverse/pthreads-precsave.exp

diff --git a/gdb/record-full.c b/gdb/record-full.c
index 66df6c85aa3..9b84be2e11a 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -52,6 +52,7 @@
 #include <deque>
 #include <signal.h>
 #include <variant>
+#include <map>
 
 /* This module implements "target record-full", also known as "process
    record and replay".  This target sits on top of a "normal" target
@@ -443,7 +444,8 @@ struct record_full_instruction
   void exec_insn (regcache *regcache);
 
   /* Create a full recorded instruction from a bfd.  */
-  static void from_bfd (bfd *cbfd, asection* osec, int *bfd_offset);
+  static void from_bfd (bfd *cbfd, asection* osec, int *bfd_offset,
+			std::map<int32_t, ptid_t>&);
 
   /* Save this instruction to a bfd file.  */
   void to_bfd (gdb_bfd_ref_ptr &obfd, asection *osec, int *bfd_offset,
@@ -462,7 +464,7 @@ struct record_full_core_buf_entry
 };
 
 /* Record buf with core target.  */
-static detached_regcache *record_full_core_regbuf = NULL;
+static std::map<int, detached_regcache *> record_full_core_regbuf;
 static std::vector<target_section> record_full_core_sections;
 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
 
@@ -946,17 +948,22 @@ record_full_async_inferior_event_handler (gdb_client_data data)
 static void
 record_full_core_open_1 (struct bfd &cbfd)
 {
-  regcache *regcache = get_thread_regcache (inferior_thread ());
-  int regnum = gdbarch_num_regs (regcache->arch ());
+  int regnum = gdbarch_num_regs (current_inferior ()->arch ());
   int i;
 
-  /* Get record_full_core_regbuf.  */
-  target_fetch_registers (regcache, -1);
-  record_full_core_regbuf = new detached_regcache (regcache->arch (), false);
+  for (thread_info &tp: current_inferior ()->threads ())
+    {
+      regcache *regcache = get_thread_regcache (&tp);
+      /* Get record_full_core_regbuf.  */
+      target_fetch_registers (regcache, -1);
+      detached_regcache *regbuf = new detached_regcache (regcache->arch (),
+							 false);
 
-  for (i = 0; i < regnum; i ++)
-    record_full_core_regbuf->raw_supply (i, *regcache);
+      for (i = 0; i < regnum; i ++)
+	regbuf->raw_supply (i, *regcache);
 
+      record_full_core_regbuf[tp.global_num] = regbuf;
+    }
   record_full_core_sections = build_section_table (&cbfd);
 
   current_inferior ()->push_target (&record_full_core_ops);
@@ -1032,10 +1039,11 @@ record_full_base_target::close ()
   record_full_reset_history ();
 
   /* Release record_full_core_regbuf.  */
-  if (record_full_core_regbuf)
+  if (!record_full_core_regbuf.empty ())
     {
-      delete record_full_core_regbuf;
-      record_full_core_regbuf = NULL;
+      for (auto &iter: record_full_core_regbuf)
+	delete iter.second;
+      record_full_core_regbuf.clear ();
     }
 
   /* Release record_full_core_buf_list.  */
@@ -2059,16 +2067,19 @@ void
 record_full_core_target::fetch_registers (struct regcache *regcache,
 					  int regno)
 {
+  detached_regcache *regbuf
+    = record_full_core_regbuf[inferior_thread ()->global_num];
+
   if (regno < 0)
     {
       int num = gdbarch_num_regs (regcache->arch ());
       int i;
 
       for (i = 0; i < num; i ++)
-	regcache->raw_supply (i, *record_full_core_regbuf);
+	regcache->raw_supply (i, *regbuf);
     }
   else
-    regcache->raw_supply (regno, *record_full_core_regbuf);
+    regcache->raw_supply (regno, *regbuf);
 }
 
 /* "prepare_to_store" method for prec over corefile.  */
@@ -2084,8 +2095,11 @@ void
 record_full_core_target::store_registers (struct regcache *regcache,
 					  int regno)
 {
+  detached_regcache *regbuf
+    = record_full_core_regbuf[inferior_thread ()->global_num];
+
   if (record_full_gdb_operation_disable)
-    record_full_core_regbuf->raw_supply (regno, *regcache);
+    regbuf->raw_supply (regno, *regcache);
   else
     error (_("You can't do that without a process to debug."));
 }
@@ -2271,6 +2285,37 @@ record_full_core_target::has_execution (inferior *inf)
 	    4 bytes: memory length.
 	    8 bytes: memory address.
 	    n bytes: memory value (n = memory length).
+
+   Version 4 (all numbers are in network order).
+     4 bytes: Magic number (0x20260728).
+       NOTE: be sure to change whenever this file format changes!
+
+    Thread table:
+      4 bytes: number of entries
+      for each entry:
+	4 bytes: GID (Global ID, GDB's internal representation).
+	4 bytes: PID.
+	8 bytes: TID.
+	8 bytes: LWP.
+
+    Records:
+      record_full_instruction:
+	1 byte: signal.
+	4 bytes: number of reg and mem entries for this instruction.
+	4 bytes: instruction sequence number.
+	4 bytes: PC register ID.
+	N bytes: PC address of instruction (N == size of PC).
+	4 bytes: Thread GID.
+	Effects:
+	  record_full_reg:
+	    1 byte: record_type (record_full_reg, see enum record_full_type).
+	    4 bytes: Register ID.
+	    n bytes: register value (n == actual register size).
+	  record_full_mem:
+	    1 byte: record_type (record_full_mem, see enum record_full_type).
+	    4 bytes: memory length.
+	    8 bytes: memory address.
+	    n bytes: memory value (n = memory length).
 */
 
 /* bfdcore_read -- read bytes from a core file section.  */
@@ -2403,11 +2448,13 @@ record_full_entry::from_bfd (bfd *cbfd, asection *osec, int *bfd_offset)
 }
 
 void
-record_full_instruction::from_bfd (bfd *cbfd, asection *osec, int *bfd_offset)
+record_full_instruction::from_bfd (bfd *cbfd, asection *osec, int *bfd_offset,
+				   std::map<int32_t, ptid_t>& thread_table)
 {
   uint32_t eff_count = 0;
   uint8_t sigval;
   uint32_t insn_num;
+  int32_t gid;
 
   /* First read the generic information for an instruction.  */
   bfdcore_read (cbfd, osec, &sigval, sizeof (uint8_t), bfd_offset);
@@ -2415,16 +2462,30 @@ record_full_instruction::from_bfd (bfd *cbfd, asection *osec, int *bfd_offset)
 		bfd_offset);
   bfdcore_read (cbfd, osec, &insn_num, sizeof (uint32_t),
 		bfd_offset);
+  bfdcore_read (cbfd, osec, &gid, sizeof(gid), bfd_offset);
+  gid = netorder32 (gid);
 
   record_full_incomplete_instruction.insn_num = netorder32 (insn_num);
   if (sigval != GDB_SIGNAL_0)
     record_full_incomplete_instruction.sigval = (gdb_signal) sigval;
 
-  record_full_incomplete_instruction.pc
-    = record_full_reg_entry::from_bfd (cbfd, osec, bfd_offset);
+  ptid_t ptid = thread_table[gid];
+  thread_info *t = current_inferior ()->find_thread (ptid);
+  gdb_assert (t != nullptr);
+  record_full_incomplete_instruction.thread = t;
 
   eff_count = netorder32 (eff_count);
 
+  if (record_debug)
+    gdb_printf (gdb_stdlog,
+		"Read instruction number %d, originally in thread %d"
+		" now thread %d.  It has %d effects:\n",
+		record_full_incomplete_instruction.insn_num,
+		gid, t->global_num, eff_count);
+
+  record_full_incomplete_instruction.pc
+    = record_full_reg_entry::from_bfd (cbfd, osec, bfd_offset);
+
   /* This deals with all the side effects.  */
   while (eff_count > 0)
     {
@@ -2434,6 +2495,27 @@ record_full_instruction::from_bfd (bfd *cbfd, asection *osec, int *bfd_offset)
     }
 }
 
+/* Read a ptid_t variable from the core file.  */
+
+static ptid_t
+read_ptid (bfd *cbfd, asection *osec, int *bfd_offset)
+{
+  /* These guarantee the right read size,independent of ptid_t or
+     ULONGEST size changes.  */
+  int32_t pid;
+  int64_t tid;
+  int64_t lwp;
+
+  bfdcore_read (cbfd, osec, &pid, sizeof (pid), bfd_offset);
+  pid = netorder32 (pid);
+  bfdcore_read (cbfd, osec, &tid, sizeof (tid), bfd_offset);
+  tid = netorder64 (tid);
+  bfdcore_read (cbfd, osec, &lwp, sizeof (lwp), bfd_offset);
+  lwp = netorder64 (lwp);
+
+  return ptid_t (pid, lwp, tid);
+}
+
 /* Restore the execution log from core file CBFD.  */
 
 static void
@@ -2480,13 +2562,36 @@ record_full_restore (struct bfd &cbfd)
 
   try
     {
+      std::map <int32_t, ptid_t> thread_table;
+      int32_t thread_count = 0;
+
+      /* First we read all the threads and their GIDs.  Then, when we
+	 read the instructions, we can find the threads based on the
+	 PTID and store the real GID for this gdb session.  */
+      bfdcore_read (&cbfd, osec, &thread_count,
+		    sizeof (thread_count), &bfd_offset);
+      thread_count = netorder32 (thread_count);
+      while (bfd_offset < osec_size
+	     && thread_count > thread_table.size ())
+	{
+	  int32_t gid = 0;
+	  bfdcore_read (&cbfd, osec, &gid, sizeof (gid), &bfd_offset);
+	  thread_table[netorder32 (gid)] = read_ptid (&cbfd, osec, &bfd_offset);
+	}
+
       while (bfd_offset < osec_size)
 	{
 	  record_full_reset_incomplete ();
 
-	  record_full_instruction::from_bfd (&cbfd, osec, &bfd_offset);
+	  record_full_instruction::from_bfd (&cbfd, osec, &bfd_offset,
+					     thread_table);
 
-	  record_full_save_instruction ();
+	  /* We can't use record_full_save_instruction here because it alters
+	     the incomplete instruction, clobbering the current GID.  So we
+	     have to push things to the log manually.  */
+	  record_full_incomplete_instruction.effects.shrink_to_fit ();
+	  record_full_log.push_back (std::move
+					(record_full_incomplete_instruction));
 	}
     }
   catch (const gdb_exception &ex)
@@ -2619,10 +2724,17 @@ record_full_instruction::to_bfd (gdb_bfd_ref_ptr &obfd, asection *osec,
 {
   uint32_t eff_count = (uint32_t) this->effects.size ();
   uint32_t insn_num = this->insn_num;
+  int32_t gid = this->thread->global_num;
   uint8_t sigval = (this->sigval.has_value ())
 		     ? this->sigval.value ()
 		     : GDB_SIGNAL_0;
 
+  if (record_debug)
+    gdb_printf (gdb_stdlog,
+		"\n Writing instruction number %d, with %d effects,"
+		" in thread %d and PC:",
+		insn_num, eff_count, gid);
+
   /* Signal.  */
   bfdcore_write (obfd.get (), osec, &sigval, sizeof (sigval), bfd_offset);
   eff_count = netorder32 (eff_count);
@@ -2633,6 +2745,10 @@ record_full_instruction::to_bfd (gdb_bfd_ref_ptr &obfd, asection *osec,
   /* Instruction number.  */
   bfdcore_write (obfd.get (), osec, &insn_num, sizeof (uint32_t),
 		 bfd_offset);
+  /* Thread Identifier.  */
+  gid = netorder32 (gid);
+  bfdcore_write (obfd.get (), osec, &gid, sizeof(int32_t),
+		 bfd_offset);
 
   this->pc.to_bfd (obfd, osec, bfd_offset);
 
@@ -2642,6 +2758,21 @@ record_full_instruction::to_bfd (gdb_bfd_ref_ptr &obfd, asection *osec,
     }
 }
 
+/* Write a ptid_t entr to the corefile pointed to by CBFD.  */
+
+static void
+write_ptid (const gdb_bfd_ref_ptr &cbfd, asection *osec, int *bfd_offset,
+	    ptid_t ptid)
+{
+  int32_t pid = netorder32 (ptid.pid ());
+  int64_t tid = netorder64 (ptid.tid ());
+  int64_t lwp = netorder64 (ptid.lwp ());
+
+  bfdcore_write (cbfd.get (), osec, &pid, sizeof (pid), bfd_offset);
+  bfdcore_write (cbfd.get (), osec, &tid, sizeof (tid), bfd_offset);
+  bfdcore_write (cbfd.get (), osec, &lwp, sizeof (lwp), bfd_offset);
+}
+
 /* Save the execution log to a file.  We use a modified elf corefile
    format, with an extra section for our data.  */
 
@@ -2677,12 +2808,19 @@ record_full_base_target::save_record (const char *recfilename)
   for (int i = record_full_next_insn - 1; i >= 0; i--)
     record_full_log[i].exec_insn (regcache);
 
+  /* Create a table relating this session's global ID and the
+     PTID of the thread, for reading the corefile in the future.  */
+  std::map<int32_t, ptid_t> thread_table;
+  for (thread_info &thr : all_threads_safe ())
+    thread_table[thr.global_num] = thr.ptid;
+
   /* Compute the size needed for the extra bfd section.  */
   save_size = 4;	/* magic cookie */
   for (int i = record_full_log.size () - 1; i >= 0; i--)
     {
       /* Number of effects of an instruction.  */
-      save_size += sizeof (uint32_t) + sizeof (uint8_t) + sizeof (uint32_t);
+      save_size += sizeof (uint32_t) + sizeof (uint8_t) + sizeof (uint32_t)
+		   + sizeof (uint32_t);
       save_size += 4 + record_full_log[i].pc.len;
       for (auto &entry : record_full_log[i].effects)
 	switch (entry.type ())
@@ -2694,8 +2832,16 @@ record_full_base_target::save_record (const char *recfilename)
 	    save_size += 1 + 4 + 8 + entry.mem ().len;
 	    break;
 	  }
+
+      /* If a thread in the log hasn't been added yet, add it now.  */
+      if (thread_table.count (record_full_log[i].thread->global_num) == 0)
+	thread_table[i] = record_full_log[i].thread->ptid;
     }
 
+  int thread_count = thread_table.size ();
+  /* Space for the thread table.  */
+  save_size += 4 + (4 + 4 + 8 + 8) * thread_count;
+
   /* Make the new bfd section.  */
   osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
 					     SEC_HAS_CONTENTS
@@ -2721,6 +2867,30 @@ record_full_base_target::save_record (const char *recfilename)
 		phex_nz (magic, 4));
   bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
 
+  /* Save the table of threads, so that multi-threaded execution can be
+     correctly reconstructed later.  */
+  if (record_debug)
+    gdb_printf (gdb_stdlog,
+		"  Writing thread table with %d entries:",
+		thread_count);
+
+  thread_count = netorder32 (thread_count);
+  bfdcore_write (obfd.get (), osec, &thread_count,
+		 sizeof (thread_count), &bfd_offset);
+  for (auto &it : thread_table)
+    {
+      int32_t gid = it.first;
+      if (record_debug)
+	gdb_printf (gdb_stdlog,
+		    "    current global_num %d and PID %s",
+		    gid, it.second.to_string ().c_str ());
+
+      gid = netorder32 (gid);
+      bfdcore_write (obfd.get (), osec, &gid, sizeof (it.first),
+		     &bfd_offset);
+      write_ptid (obfd, osec, &bfd_offset, it.second);
+    }
+
   /* Save the entries to recfd and forward execute to the end of
      record list.  */
   for (int i = 0; i < record_full_log.size (); i++)
diff --git a/gdb/testsuite/gdb.reverse/pthreads-precsave.exp b/gdb/testsuite/gdb.reverse/pthreads-precsave.exp
new file mode 100644
index 00000000000..53702ed26fa
--- /dev/null
+++ b/gdb/testsuite/gdb.reverse/pthreads-precsave.exp
@@ -0,0 +1,93 @@
+# Copyright 2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+# This file is part of the GDB testsuite.  It tests GDB's ability to
+# record multithreaded programs, save their logs to a precsave file and
+# load it in a new session.
+
+require supports_reverse
+
+standard_testfile pthreads-reverse.c
+set precsave [standard_output_file pthreads.precsave]
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
+    return
+}
+
+runto_main
+
+gdb_test_no_output "set scheduler-locking on"
+
+gdb_breakpoint "thread_func"
+
+# Recording thread creation is not trivial, and so this basic test
+# just skips that complex use case for now.
+if {[gdb_test "next" "TAG: PTHREAD SUCCESSFUL.*" \
+     "next over pthread creation"] != 0} {
+    untested "Couldn't start pthreads"
+    return
+}
+
+gdb_test "thread 2" ".Switching to thread 2 .*"
+gdb_continue_to_breakpoint "thread_func" ".*thread_func.*"
+
+if {[supports_process_record]} {
+    # Activate process record/replay
+    gdb_test_no_output "record" "turn on process record"
+}
+
+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 "record save $precsave" \
+    "Saved core file $precsave with execution log\." \
+    "save execution log"
+
+clean_restart $testfile
+
+gdb_test "record full restore $precsave" \
+    "Restored records from core file .*" \
+    "reload precord save file"
+
+set main_thread -1
+set other_thread -1
+
+gdb_test_multiple "info threads" "get thread numbers" -lbl {
+    -re "\r\n\[^\r\n\]* \(${::decimal}\) \[^\r\n\]+thread_func\[^\r\n\]*" {
+	set other_thread $expect_out(1,string)
+	exp_continue
+    }
+    -re "\r\n\[^\r\n\]* \(${::decimal}\) \[^\r\n\]+main\[^\r\n\]*" {
+	set main_thread $expect_out(1,string)
+	exp_continue
+    }
+    -re -wrap "" {
+	gdb_assert { $main_thread != -1 } "collected main thread ID"
+	gdb_assert { $other_thread != -1 } "collected other thread ID"
+    }
+}
+
+if { $main_thread == -1 } {
+    untested "couldn't read the threads from the corefile"
+    return
+}
+
+gdb_test "thread" ".*thread is $other_thread.*" \
+    "identify starting thread"
-- 
2.56.0


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

* [PATCH 3/7] gdb/record: Make GDB automatically switch threads when replaying
  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
  2026-10-29 20:57 ` [PATCH 4/7] gdb: make regcache save a thread pointer, not ptid Guinevere Larsen
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-29 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

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


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

* [PATCH 4/7] gdb: make regcache save a thread pointer, not ptid
  2026-10-29 20:57 [PATCH 0/7] Start supporting multi-threaded inferiors with record-full Guinevere Larsen
                   ` (2 preceding siblings ...)
  2026-10-29 20:57 ` [PATCH 3/7] gdb/record: Make GDB automatically switch threads when replaying Guinevere Larsen
@ 2026-10-29 20:57 ` Guinevere Larsen
  2026-10-29 20:57 ` [PATCH 5/7] gdb/record: Record through a thread exiting Guinevere Larsen
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-29 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

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(-)

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];
+      }
   }
 };
 
-- 
2.56.0


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

* [PATCH 5/7] gdb/record: Record through a thread exiting
  2026-10-29 20:57 [PATCH 0/7] Start supporting multi-threaded inferiors with record-full Guinevere Larsen
                   ` (3 preceding siblings ...)
  2026-10-29 20:57 ` [PATCH 4/7] gdb: make regcache save a thread pointer, not ptid Guinevere Larsen
@ 2026-10-29 20:57 ` 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
  6 siblings, 0 replies; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-29 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

This commit makes it possible for GDB's record-full subsystem to
record through a thread exiting. To do so, it introduces a new
function to "mourn" a thread, which saves a detached regcache so that
we can continue fetching and changing registers even though the thread
is not actually live anymore.  It also introduces a way for
set_thread_exited to save the exit code of a thread to record-full, so
that we can replay the exit exactly as it happened.

To be able to replay before the thread was exited, the new function
set_thread_revived is introduced, which re-adds the thread's ptid to
the inferior's live thread map, so that functions iterating over live
threads continue working properly. It can be done trivially since, as
of this commit, it isn't possible to record a thread starting - and
so, no PTID reuse is possible. And even after that use case is added,
a possibly reused PTID will have been removed from the map as it'll
have been un-created by this point, meaning we don't have to worry
about possible PTID collisions in the live thread map.

There are two moments when a thread may need to be revived: Either we
are replaying history, and to reach the next stopping point we'll need
to revive a thread, or a reverse command has just been issued and
we're in a dead thread. For the former, it is trivial to change the
thread state in the record_wait_1 function.

For the second case, ideally reviving the thread would be done in the
resume call. However, resume is only called for live threads, so
unfortunately the most elegant solution isn't available. I went with
what seemed the next-best thing, in infcmd's ensure_valid_thread.

Also, the store_registers and fetch_registers methods needed to be
updated to possibly refer to the detached regcache. It would ideally
use the instruction's thread pointer, instead of the regcache's, but
attempting to do so introduced a lot of odd edge cases that just made
the code much more complicated, so using regcache seemed like a
cleaner solution.

---
 gdb/gdbthread.h                               |   2 +
 gdb/infcmd.c                                  |   6 +
 gdb/infrun.c                                  |   1 +
 gdb/linux-record.c                            |  13 +-
 gdb/record-full.c                             | 131 +++++++++++++++++-
 gdb/record-full.h                             |  16 +++
 .../gdb.reverse/pthreads-reverse.exp          |  33 +++++
 gdb/thread.c                                  |  19 ++-
 8 files changed, 213 insertions(+), 8 deletions(-)

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 51e9ae1b3f0..ea5039c3047 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -728,6 +728,8 @@ extern void set_thread_exited (thread_info *tp,
 			       std::optional<ULONGEST> exit_code = {},
 			       bool silent = false);
 
+extern void set_thread_revived (thread_info *tp);
+
 /* Delete a step_resume_breakpoint from the thread database.  */
 extern void delete_step_resume_breakpoint (struct thread_info *);
 
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index d7544168362..508a61c28c8 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -58,6 +58,7 @@
 #include "cli/cli-style.h"
 #include "gdbsupport/selftest.h"
 #include "finish-thread-state.h"
+#include "record-full.h"
 
 /* Local functions: */
 
@@ -694,6 +695,11 @@ starti_command (const char *args, int from_tty)
 static void
 ensure_valid_thread (void)
 {
+  /* If we are checking the validity of a thread for an execution command,
+     it is possible that record-full needs to revive the thread for execution.
+     Do this here to ensure the thread is in the actual correct state.  */
+  record_full_maybe_update_thread_state (inferior_thread ());
+
   if (inferior_ptid == null_ptid
       || inferior_thread ()->state () == THREAD_EXITED)
     error (_("Cannot execute this command without a live selected thread."));
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 47966cf72db..834d62d271b 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -994,6 +994,7 @@ follow_fork ()
       }
       break;
     case TARGET_WAITKIND_SPURIOUS:
+    case TARGET_WAITKIND_IGNORE:
       /* Nothing to follow.  */
       break;
     default:
diff --git a/gdb/linux-record.c b/gdb/linux-record.c
index d129ba8df72..b98bc557ee2 100644
--- a/gdb/linux-record.c
+++ b/gdb/linux-record.c
@@ -25,6 +25,7 @@
 #include "record-full.h"
 #include "linux-record.h"
 #include "gdbarch.h"
+#include "inferior.h"
 
 /* These macros are the values of the first argument of system call
    "sys_ptrace".  The values of these macros were obtained from Linux
@@ -248,6 +249,7 @@ record_linux_system_call (enum gdb_syscall syscall,
   ULONGEST tmpulongest;
   CORE_ADDR tmpaddr;
   int tmpint;
+  int num_active_threads;
 
   switch (syscall)
     {
@@ -255,9 +257,14 @@ record_linux_system_call (enum gdb_syscall syscall,
       break;
 
     case gdb_sys_exit:
-      if (yquery (_("The next instruction is syscall exit.  "
-		    "It will make the program exit.  "
-		    "Do you want to stop the program?")))
+      for (thread_info &_ : current_inferior ()->non_exited_threads ())
+	num_active_threads++;
+
+      if (num_active_threads > 1)
+	record_full_mourn_thread (regcache);
+      else if (yquery (_("The next instruction is syscall exit.  "
+			 "It will make the program exit.  "
+			 "Do you want to stop the program?")))
 	return 1;
       break;
 
diff --git a/gdb/record-full.c b/gdb/record-full.c
index 146447adfbb..e77b89d035d 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -439,6 +439,9 @@ struct record_full_instruction
   /* The thread that executed this instruction.  */
   thread_info *thread = nullptr;
 
+  /* True if this instruction caused a thread to exit.  */
+  std::optional<ULONGEST> thread_exit;
+
   /* 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
@@ -470,6 +473,10 @@ static std::map<int, detached_regcache *> record_full_core_regbuf;
 static std::vector<target_section> record_full_core_sections;
 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
 
+/* This is used when a thread has exited, so that we can pretend like
+   we brought it back to life for replaying purposes.  */
+static std::map<int, detached_regcache*> dead_thread_registers;
+
 /* The following variables are used for managing the history of executed
    instructions from the inferior.
 
@@ -559,6 +566,7 @@ class record_full_target final : public record_full_base_target
   const target_info &info () const override
   { return record_full_target_info; }
 
+  void fetch_registers (regcache *regcache, int regno) override;
   void resume (ptid_t, int, enum gdb_signal) override;
   void disconnect (const char *, int) override;
   void detach (inferior *, int) override;
@@ -646,6 +654,67 @@ record_full_target::kill ()
   record_kill (this);
 }
 
+void
+record_full_target::fetch_registers (regcache *regcache, int regno)
+{
+  thread_info *tp = regcache->thread () ;
+
+  if (dead_thread_registers.count (tp->global_num) == 0)
+    {
+      this->beneath ()->fetch_registers (regcache, regno);
+      return ;
+    }
+
+  detached_regcache *zombie_cache = dead_thread_registers[tp->global_num];
+  if (regno < 0)
+    {
+      int num_regs = gdbarch_num_regs (regcache->arch ());
+
+      for (int i = 0; i < num_regs; i++)
+	regcache->raw_supply (i, *zombie_cache);
+    }
+  else
+    regcache->raw_supply (regno, *zombie_cache);
+}
+
+void
+record_full_maybe_update_thread_state (thread_info *tp)
+{
+  if (tp == nullptr)
+    return;
+
+  if (execution_direction == EXEC_REVERSE
+      && tp->state () == THREAD_EXITED)
+    set_thread_revived (tp);
+}
+
+/* See record-full.h.  */
+
+void
+record_full_mourn_thread (regcache *regcache)
+{
+  detached_regcache *zombie_cache
+    = new detached_regcache (regcache->arch (), false);
+  int num_regs = gdbarch_num_regs (regcache->arch ());
+
+  for (int i = 0; i < num_regs; i++)
+    zombie_cache->raw_supply (i, *regcache);
+
+  dead_thread_registers[inferior_thread ()->global_num] = zombie_cache;
+
+  /* Mark the current incomplete instruction as the one that kills
+     the thread.  */
+  record_full_incomplete_instruction.thread_exit = 0;
+
+  return;
+}
+
+void
+record_full_set_exit_code (std::optional<ULONGEST>& code)
+{
+  record_full_log.back ().thread_exit = code;
+}
+
 /* Reset the incomplete instruction.  */
 
 static void
@@ -655,6 +724,7 @@ record_full_reset_incomplete ()
   record_full_incomplete_instruction.sigval.reset ();
   record_full_incomplete_instruction.insn_num = 0;
   record_full_incomplete_instruction.thread = nullptr;
+  record_full_incomplete_instruction.thread_exit.reset ();
 }
 
 /* See record-full.h.  */
@@ -679,6 +749,16 @@ record_full_is_replaying ()
 }
 
 
+/* See record-full.h.  */
+bool
+record_full_is_recording ()
+{
+  auto target = dynamic_cast<record_full_target *>
+		  (current_inferior ()->target_at (record_stratum));
+
+  return target != nullptr && !RECORD_FULL_IS_REPLAY;
+}
+
 /* Command lists for "set/show record full".  */
 static struct cmd_list_element *set_record_full_cmdlist;
 static struct cmd_list_element *show_record_full_cmdlist;
@@ -1217,6 +1297,13 @@ record_full_wait_1 (struct target_ops *ops,
 
   if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
     {
+      /* If we wait for minus_one_ptid (that is, any thread), we
+	 actually just want to wait for the thread that was originally
+	 resumed.  That's because otherwise, we won't abort the
+	 execution command once a thread exits.  */
+      if (ptid == minus_one_ptid)
+	ptid = record_full_resume_ptid;
+
       if (record_full_resume_step)
 	{
 	  /* This is a single step.  */
@@ -1343,6 +1430,7 @@ record_full_wait_1 (struct target_ops *ops,
       struct gdbarch *gdbarch = regcache->arch ();
       const address_space *aspace = current_inferior ()->aspace.get ();
       int continue_flag = 1;
+      bool thread_exited = false;
 
       try
 	{
@@ -1405,9 +1493,23 @@ record_full_wait_1 (struct target_ops *ops,
 		  break;
 		}
 
+	      record_full_instruction &curr_insn
+		= record_full_log[record_full_next_insn];
+
+	      if (curr_insn.thread_exit.has_value ())
+		record_full_maybe_update_thread_state (curr_insn.thread);
+
 	      last_thread
 	       = record_full_log[record_full_next_insn].exec_insn ();
 
+	      if (execution_direction == EXEC_FORWARD
+		  && curr_insn.thread_exit.has_value ())
+		{
+		  status->set_thread_exited (curr_insn.thread_exit.value ());
+		  continue_flag = 0;
+		  thread_exited = true;
+		}
+
 	      /* 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)
@@ -1459,9 +1561,18 @@ record_full_wait_1 (struct target_ops *ops,
 	    }
 	  else if (record_full_next_insn >= record_full_log.size ())
 	    {
-	      gdb_assert (execution_direction == EXEC_FORWARD);
-	      record_full_next_insn = record_full_log.size ();
-	      status->set_no_history ();
+	      /* If a thread has exited as the last instruction, the exit
+		 is a more important event, but we must notify the user of
+		 no further record history, otherwise they might try to
+		 continue executing and accidentally record further.  */
+	      if (thread_exited)
+		interps_notify_no_history ();
+	      else
+		{
+		  gdb_assert (execution_direction == EXEC_FORWARD);
+		  record_full_next_insn = record_full_log.size ();
+		  status->set_no_history ();
+		}
 	    }
 	  /* Reset the current instruction to point to the one to be replayed
 	     moving forward.  */
@@ -1655,7 +1766,16 @@ record_full_target::store_registers (struct regcache *regcache, int regno)
 
       record_full_registers_change (regcache, regno);
     }
-  this->beneath ()->store_registers (regcache, regno);
+  else
+    {
+      thread_info *tp = current_inferior ()->find_thread (regcache->ptid ());
+
+      if (RECORD_FULL_IS_REPLAY
+	  && dead_thread_registers.count (tp->global_num) == 1)
+	dead_thread_registers[tp->global_num]->raw_supply (regno, *regcache);
+      else
+	this->beneath ()->store_registers (regcache, regno);
+    }
 }
 
 /* "xfer_partial" method.  Behavior is conditional on
@@ -3040,6 +3160,9 @@ maintenance_print_record_instruction (const char *args, int from_tty)
 
   gdb_printf ("Instruction executed on thread %s\n",
 	      to_print->thread->ptid.to_string ().c_str ());
+
+  if (to_print->thread_exit.has_value ())
+    gdb_printf ("It caused the thread to exit\n");
 }
 
 INIT_GDB_FILE (record_full)
diff --git a/gdb/record-full.h b/gdb/record-full.h
index c327d879a8a..0e50bb7a751 100644
--- a/gdb/record-full.h
+++ b/gdb/record-full.h
@@ -39,14 +39,30 @@ enum record_result
   RECORD_UNKNOWN = 1
 };
 
+/* This is called when a thread is about to exit.  We save all the necessary
+   information to pretend that we revived a thread when the user steps
+   backwards later.  */
+extern void record_full_mourn_thread (regcache *regcache);
+
+/* Set the exit code for the thread that just exited.  This changes the last
+   finished instruction, as opposed to changing the unfinished one.  */
+extern void record_full_set_exit_code (std::optional<ULONGEST>& code);
+
 extern int record_full_arch_list_add_reg (struct regcache *regcache, int num);
 extern int record_full_arch_list_add_mem (CORE_ADDR addr, int len);
 
+/* Check if the thread TP should have its state updated, such as being
+   revived.  */
+extern void record_full_maybe_update_thread_state (thread_info *tp);
+
 /* Returns true if the process record target is open.  */
 extern int record_full_is_used (void);
 
 /* Whether the inferior is being replayed, or is executing normally.  */
 extern bool record_full_is_replaying ();
+/* Whether the inferior is being recorded (i.e., played forward regularly
+   with record_full in the target stack), or not.  */
+extern bool record_full_is_recording ();
 
 extern scoped_restore_tmpl<int> record_full_gdb_operation_disable_set ();
 
diff --git a/gdb/testsuite/gdb.reverse/pthreads-reverse.exp b/gdb/testsuite/gdb.reverse/pthreads-reverse.exp
index 1ef1162acd1..62a1e75d4ca 100644
--- a/gdb/testsuite/gdb.reverse/pthreads-reverse.exp
+++ b/gdb/testsuite/gdb.reverse/pthreads-reverse.exp
@@ -93,3 +93,36 @@ gdb_test "thread 2" ".Switching to 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"
+
+# Test recording a thread exit and reversing to before it was dead.
+gdb_test "continue" "No unwaited-for children left." "record to thread exit."
+gdb_test "reverse-next" "Reached end of recorded history; stopping.*" \
+    "next back through thread exit"
+
+gdb_test "continue" "Reached end of recorded history; stopping.*" \
+    "continue to end of recording"
+gdb_test "reverse-continue" "Reached end of recorded history; stopping.*" \
+    "continue back through thread exit"
+
+gdb_test "continue" "Reached end of recorded history; stopping.*" \
+    "continue to end of recording again"
+gdb_test "reverse-step" "thread_func .arg=0x0.*" \
+    "Step back through thread exit"
+
+# Test recording some more after the thread exit, and then replaying to before
+# it exited, all in one go.
+
+
+gdb_test "continue" "Reached end of recorded history; stopping.*" \
+    "continue to end of recording to record more"
+gdb_test "thread 1" ".Switching to thread 1.*" "return to main function"
+gdb_test "next"
+gdb_test "reverse-continue" "Reached end of recorded history; stopping.*" \
+    "continue from different thread through thread exit"
+
+gdb_test "continue" "No unwaited-for children left.*" \
+    "continue to thread exit for 'next' test"
+gdb_test "t 1"
+gdb_test "next" "Reached end of recorded history;.*" "Next to end of history"
+gdb_test "reverse-next" "end of main.*" "stop right before thread exit"
+gdb_test "reverse-next" "TAG: PTHREAD SUCCESSFUL.*" "next past thread exit"
diff --git a/gdb/thread.c b/gdb/thread.c
index e14254619e3..637ae6e44a1 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -250,6 +250,22 @@ set_thread_exited (thread_info *tp, std::optional<ULONGEST> exit_code,
       size_t nr_deleted = tp->inf->ptid_thread_map.erase (tp->ptid);
       gdb_assert (nr_deleted == 1);
     }
+
+  if (exit_code.has_value () && record_full_is_recording ())
+    record_full_set_exit_code (exit_code);
+}
+
+void
+set_thread_revived (thread_info *tp)
+{
+  gdb_assert (tp->state () == THREAD_EXITED);
+
+  tp->set_state (THREAD_STOPPED);
+  tp->set_internal_state (THREAD_INT_STOPPED);
+
+  /* Re-add the thread to the inferior's ptid_t map, so that all of GDB
+     is aware that the thread is supposed to be alive.  */
+  tp->inf->ptid_thread_map.insert ({ tp->ptid, tp });
 }
 
 void
@@ -1018,7 +1034,8 @@ validate_registers_access (void)
   thread_info *tp = inferior_thread ();
 
   /* Don't try to read from a dead thread.  */
-  if (tp->state () == THREAD_EXITED)
+  if (tp->state () == THREAD_EXITED
+      && execution_direction != EXEC_REVERSE)
     error (_("The current thread has terminated"));
 
   /* ... or from a spinning thread.  FIXME: This isn't actually fully
-- 
2.56.0


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

* [PATCH 6/7] gdb/record: Loudly warn or fail on dubious situations
  2026-10-29 20:57 [PATCH 0/7] Start supporting multi-threaded inferiors with record-full Guinevere Larsen
                   ` (4 preceding siblings ...)
  2026-10-29 20:57 ` [PATCH 5/7] gdb/record: Record through a thread exiting Guinevere Larsen
@ 2026-10-29 20:57 ` Guinevere Larsen
  2026-10-29 20:57 ` [PATCH 7/7] gdb/record: document recording multi-threaded inferiors Guinevere Larsen
  6 siblings, 0 replies; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-29 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

There are some situations in which the user may try to do some things
that record-full doesn't quite support. Previously, we'd just chug
along, with either poor results or even corrupting the state of the
inferior.

One of these situations would be stopping recording in the past. In
situations like these, if the inferior was dealing with resources like
sockets, connections or files, the inferior could be confused about
the situation, such as rewriting things in a file or re-sending
information to servers. And now that GDB supports reviving threads,
the user could return to a moment before the thread exited, so the
program believes a thread exists but the kernel has made it exit
already.  However, there are also reasonable situations where a user
can do that, such as when none of those resources are changed and the
user wishes to supply a different value and examine the execution. So
we now query the user on whether they are sure they want to do this.

This change required many updates to the testsuite, as it turns out
there are several points at which we delete the recording in the
past. These are trivial changes.

Another unsupported use case is recording the creation of a
thread. There's no mechanism to return to a stage before the thread
existed, for example. And more pressing, when creating a corefile with
the history log, we return to the start of history and dump the
current stage, which means the thread may not be dumped, or regcache
may assert. This commit identifies the syscalls that can create a
thread and error, causing recording to stop.

This caused an issue to the waitpid-reverse.exp test. In my machine,
despite using fork, we were triggering the error in the clone
syscall. To avoid any possible issues, since the point of the test is
only checking the waitpid syscall, not our ability to record a fork,
the testcase was slightly changed to start recording after the fork
call.

Finally, a user could try to use the `set scheduler-locking' command
while replaying the inferior. Until now, it wasn't a big deal, as not
supporting multi-threaded inferiors in the first place was a more
pressing issue. However, now that we do support it, we should be more
careful about what we let the user select. For now, there's no way for
the record subsystem to manage multiple threads executing at once, and
so, we warn at any point when we could be recording or
replaying.
---
 gdb/infrun.c                                  |  2 ++
 gdb/linux-record.c                            |  5 ++-
 gdb/record.c                                  |  6 ++++
 .../gdb.reverse/basic-record-full.exp         |  5 +--
 .../gdb.reverse/finish-reverse-next.exp       | 10 +++---
 .../gdb.reverse/i386-avx-reverse.exp          | 36 ++++++++++---------
 gdb/testsuite/gdb.reverse/insn-reverse.exp    |  3 +-
 gdb/testsuite/gdb.reverse/waitpid-reverse.c   |  3 +-
 gdb/testsuite/gdb.reverse/waitpid-reverse.exp | 10 +++---
 9 files changed, 49 insertions(+), 31 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 834d62d271b..4d096a859cb 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -2407,6 +2407,8 @@ set_schedlock_func (const char *args, int from_tty, struct cmd_list_element *c)
       error (_("Target '%s' cannot support this command."),
 	     target_shortname ());
     }
+  else if (record_full_is_replaying () || record_full_is_recording ())
+    error (_("Changing the scheduler will have no effect while replaying."));
 }
 
 /* True if execution commands resume all threads of all processes by
diff --git a/gdb/linux-record.c b/gdb/linux-record.c
index b98bc557ee2..b1ac5bb26b2 100644
--- a/gdb/linux-record.c
+++ b/gdb/linux-record.c
@@ -1181,10 +1181,13 @@ Do you want to stop the program?"),
 
     case gdb_sys_fsync:
     case gdb_sys_sigreturn:
-    case gdb_sys_clone:
     case gdb_sys_setdomainname:
       break;
 
+    case gdb_sys_clone:
+      error (_("Recording the creation of a thread is unsupported."));
+      break;
+
     case gdb_sys_newuname:
       if (record_mem_at_reg (regcache, tdep->arg1,
 			     tdep->size_new_utsname))
diff --git a/gdb/record.c b/gdb/record.c
index 35f9f0eb1e4..00299e87854 100644
--- a/gdb/record.c
+++ b/gdb/record.c
@@ -28,6 +28,7 @@
 #include "disasm.h"
 #include "interps.h"
 #include "top.h"
+#include "record-full.h"
 
 
 /* This is the debug switch for process record.  */
@@ -314,6 +315,11 @@ static void
 cmd_record_stop (const char *args, int from_tty)
 {
   struct target_ops *t = require_record_target ();
+  if (record_full_is_replaying ()
+      && !query (_("Removing the history at this point may leave the inferior "
+		  "in an irrecoverable state.  Are you sure?")))
+    error (_("Not deleting the history"));
+
   bool thread_moved = record_stop (t);
   record_unpush (t);
 
diff --git a/gdb/testsuite/gdb.reverse/basic-record-full.exp b/gdb/testsuite/gdb.reverse/basic-record-full.exp
index 67655417db2..8943dc8bd50 100644
--- a/gdb/testsuite/gdb.reverse/basic-record-full.exp
+++ b/gdb/testsuite/gdb.reverse/basic-record-full.exp
@@ -36,6 +36,7 @@ gdb_test_no_output "record" "turn on process record"
 gdb_test "show exec-dir" "Forward." "Before any change"
 gdb_test_no_output "set exec-direction reverse"
 gdb_test "show exec-dir" "Reverse." "Setting direction by hand"
-gdb_test "recor stop" \
-    "Process record is stopped and all execution logs are deleted."
+gdb_test "record stop" \
+    "Process record is stopped and all execution logs are deleted." \
+    "stopping record" ".*Are you sure?.*" "y"
 gdb_test "show exec-dir" "Forward." "After stopping record"
diff --git a/gdb/testsuite/gdb.reverse/finish-reverse-next.exp b/gdb/testsuite/gdb.reverse/finish-reverse-next.exp
index 8d43a170821..5300380fe73 100644
--- a/gdb/testsuite/gdb.reverse/finish-reverse-next.exp
+++ b/gdb/testsuite/gdb.reverse/finish-reverse-next.exp
@@ -85,8 +85,8 @@ gdb_test "reverse-next" ".*b = 5;.*" "reverse next 2, at b = 5, call from LEP"
 gdb_test "reverse-continue" ".*" "setup for test 2"
 
 # Turn off record to clear logs and turn on again
-gdb_test "record stop"  "Process record is stopped.*" \
-    "turn off process record for test1"
+gdb_test "record stop" ".*" \
+    "turn off process record for test1" ".*Are you sure?.*" "y"
 gdb_test_no_output "record" "turn on process record for test2"
 
 
@@ -115,7 +115,7 @@ gdb_test "reverse-continue" ".*" "setup for test 3"
 
 # Turn off record to clear logs and turn on again
 gdb_test "record stop"  "Process record is stopped.*" \
-    "turn off process record for test2"
+    "turn off process record for test2" ".*Are you sure?.*" "y"
 gdb_test_no_output "record" "turn on process record for test3"
 
 
@@ -145,7 +145,7 @@ gdb_test "reverse-continue" ".*" "setup for test 4"
 
 # Turn off record to clear logs and turn on again
 gdb_test "record stop"  "Process record is stopped.*" \
-    "turn off process record for test3"
+    "turn off process record for test3" ".*Are you sure?.*" "y"
 gdb_test_no_output "record" "turn on process record for test4"
 
 ### TEST 4: reverse finish from between the GEP and LEP in
@@ -178,7 +178,7 @@ gdb_test "reverse-continue" ".*" "setup for test 5"
 
 # Turn off record to clear logs and turn on again
 gdb_test "record stop"  "Process record is stopped.*" \
-    "turn off process record for test4"
+    "turn off process record for test4" ".*Are you sure?.*" "y"
 gdb_test_no_output "record" "turn on process record for test5"
 
 
diff --git a/gdb/testsuite/gdb.reverse/i386-avx-reverse.exp b/gdb/testsuite/gdb.reverse/i386-avx-reverse.exp
index f1012a7a96d..a97cc1f66f7 100644
--- a/gdb/testsuite/gdb.reverse/i386-avx-reverse.exp
+++ b/gdb/testsuite/gdb.reverse/i386-avx-reverse.exp
@@ -132,6 +132,8 @@ proc record_full_function {function} {
 	}
     }
 
+    # Since there was no backwards execution here, there shouldn't be a
+    # query from GDB.
     gdb_test "record stop" "Process record is stopped.*" \
 	"delete failed record history"
     # If we didn't exit early, the temporary breakpoint at the end of
@@ -313,7 +315,7 @@ if {[record_full_function "vmov"] == true} {
 
     # Stop recording to get a clean history.
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for vmov_test"
+	"delete history for vmov_test" ".*Are you sure?.*" "y"
 } else {
     untested "could not record vmov_test"
 }
@@ -379,7 +381,7 @@ if {[record_full_function "vpunpck"] == true} {
     # Move to the end of vmov_test to set up next.
     # Stop recording in case of recording errors.
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for vpunpck_test"
+	"delete history for vpunpck_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't test vpunpck tests"
 }
@@ -425,7 +427,7 @@ if {[record_full_function "vpbroadcast"] == true} {
     test_one_register "vpbroadcastb" "ymm0"  "0x0, 0x0"
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for vpbroadcast_test"
+	"delete history for vpbroadcast_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run vpbroadcast tests"
 }
@@ -495,7 +497,7 @@ if {[record_full_function "vpor_xor"] == true} {
     test_one_register "vpxor" "ymm0" "0x0, 0x12345" "second: "
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for vpor_xor_test"
+	"delete history for vpor_xor_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run vpor_xor tests"
 }
@@ -540,7 +542,7 @@ if {[record_full_function "vpcmpeq"] == true} {
     test_one_register "vpcmpeqb" "ymm0" "0x12345, 0x12345" "xmm: "
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for vpcmpeq_test"
+	"delete history for vpcmpeq_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run vpcmpeq tests"
 }
@@ -561,7 +563,7 @@ if {[record_full_function "vpmovmskb"] == true} {
     test_one_general_register "vpmovmskb" "rax" "0x0"
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for vpmovmskb_test"
+	"delete history for vpmovmskb_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run vpmovmskb tests"
 }
@@ -700,7 +702,7 @@ if {[record_full_function "arith"] == true} {
     test_one_register "vaddps" "ymm15" "0x0, 0x0" "xmm operation: "
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for arith_test"
+	"delete history for arith_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run arith tests"
 }
@@ -735,7 +737,7 @@ if {[record_full_function "vaddsubpd"] == true} {
        "0xcafeface, 0xcafeface" "ymm0: "
 
     gdb_test "record stop" "Process record is stopped.*" \
-       "delete history for vaddsubpd_test"
+       "delete history for vaddsubpd_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run vaddsubpd tests"
 }
@@ -770,7 +772,7 @@ if {[record_full_function "vaddsubps"] == true} {
        "0xcafeface, 0xcafeface" "ymm2: "
 
     gdb_test "record stop" "Process record is stopped.*" \
-       "delete history for vaddsubps_test"
+       "delete history for vaddsubps_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run vaddsubps tests"
 }
@@ -861,7 +863,7 @@ if {[record_full_function "shift"] == true} {
     test_one_register "vpsllw" "ymm0" "0x0, 0x0" "from constant: "
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for shift_test"
+	"delete history for shift_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run shift tests"
 }
@@ -915,7 +917,7 @@ if {[record_full_function "shuffle"] == true} {
     test_one_register "vpshufb" "ymm0" "0x0, 0x0" "low register: "
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for shuffle_test"
+	"delete history for shuffle_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run shuffle tests"
 }
@@ -985,7 +987,7 @@ if {[record_full_function "permute"] == true} {
     test_one_register "vperm2f128" "ymm0" "0x0, 0x0"
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for permute_test"
+	"delete history for permute_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run permute tests"
 }
@@ -1040,7 +1042,7 @@ if {[record_full_function "extract_insert"] == true} {
 	"0x0, 0x0"
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for extract_insert_test"
+	"delete history for extract_insert_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run extract_insert tests"
 }
@@ -1080,7 +1082,7 @@ if {[record_full_function "blend"] == true} {
     test_one_register "vblendps" "ymm0" "0x0, 0x0"
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for blend_test"
+	"delete history for blend_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run blend tests"
 }
@@ -1103,7 +1105,7 @@ if {[record_full_function "compare"] == true} {
     test_one_general_register "vcomisd" "eflags" "0x202"
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for compare_test"
+	"delete history for compare_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run compare tests"
 }
@@ -1139,7 +1141,7 @@ if {[record_full_function "pack"] == true} {
     test_one_register "vpacksswb" "ymm0" "0x0, 0x0"
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for pack_test"
+	"delete history for pack_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run pack tests"
 }
@@ -1190,7 +1192,7 @@ if {[record_full_function "convert"] == true} {
     test_one_register "vcvtdq2ps" "ymm0" "0x0, 0x0"
 
     gdb_test "record stop" "Process record is stopped.*" \
-	"delete history for convert_test"
+	"delete history for convert_test" ".*Are you sure?.*" "y"
 } else {
     untested "couldn't run convert tests"
 }
diff --git a/gdb/testsuite/gdb.reverse/insn-reverse.exp b/gdb/testsuite/gdb.reverse/insn-reverse.exp
index 5b4f6a28f7a..df7daddcef2 100644
--- a/gdb/testsuite/gdb.reverse/insn-reverse.exp
+++ b/gdb/testsuite/gdb.reverse/insn-reverse.exp
@@ -124,7 +124,8 @@ proc test { func testcase_nr } {
 		}
 	    }
 	}
-	gdb_test "record stop"
+	gdb_test "record stop" ".*" "finishing proc" \
+	    ".*Are you sure?.*" "y"
     }
 }
 
diff --git a/gdb/testsuite/gdb.reverse/waitpid-reverse.c b/gdb/testsuite/gdb.reverse/waitpid-reverse.c
index 5925ad73f5d..b44ee2cdee7 100644
--- a/gdb/testsuite/gdb.reverse/waitpid-reverse.c
+++ b/gdb/testsuite/gdb.reverse/waitpid-reverse.c
@@ -34,8 +34,9 @@ int status = -1;
 int
 main (void)
 {
+  pid_t pid = fork ();
   marker1 ();
-  if (!fork ())
+  if (pid == 0)
     _exit (123);
   else
     waitpid (-1, &status, 0);
diff --git a/gdb/testsuite/gdb.reverse/waitpid-reverse.exp b/gdb/testsuite/gdb.reverse/waitpid-reverse.exp
index dc213259bf0..68108d86aff 100644
--- a/gdb/testsuite/gdb.reverse/waitpid-reverse.exp
+++ b/gdb/testsuite/gdb.reverse/waitpid-reverse.exp
@@ -31,6 +31,12 @@ if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
 
 runto_main
 
+gdb_test "break marker1" \
+    "Breakpoint $decimal at $hex: file .*$srcfile, line $decimal.*" \
+    "set breakpoint at marker1"
+
+gdb_continue_to_breakpoint "marker1" ".*$srcfile:.*"
+
 if {[supports_process_record]} {
     # Activate process record/replay
     gdb_test_no_output "record" "turn on process record"
@@ -42,10 +48,6 @@ gdb_test "break marker2" \
 
 gdb_continue_to_breakpoint "marker2" ".*$srcfile:.*"
 
-gdb_test "break marker1" \
-    "Breakpoint $decimal at $hex: file .*$srcfile, line $decimal.*" \
-    "set breakpoint at marker1"
-
 gdb_test "reverse-continue" ".*$srcfile:$decimal.*" "reverse to marker1"
 
 # If the variable was recorded properly on syscall, the old contents (-1)
-- 
2.56.0


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

* [PATCH 7/7] gdb/record: document recording multi-threaded inferiors
  2026-10-29 20:57 [PATCH 0/7] Start supporting multi-threaded inferiors with record-full Guinevere Larsen
                   ` (5 preceding siblings ...)
  2026-10-29 20:57 ` [PATCH 6/7] gdb/record: Loudly warn or fail on dubious situations Guinevere Larsen
@ 2026-10-29 20:57 ` Guinevere Larsen
  2026-10-30  6:43   ` Eli Zaretskii
  6 siblings, 1 reply; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-29 20:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

This commit adds an entry to the NEWS file about the support for
recording multi-threaded programs, as well as a quick explanation of
supporting it and the limitations to the documentation.
---
 gdb/NEWS            | 5 +++++
 gdb/doc/gdb.texinfo | 9 ++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 7303ec0e460..02a6b0d7df6 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -44,6 +44,11 @@
   source is modified.  Source tracking can be enabled with 'set
   breakpoint source-tracking enabled on'.
 
+* record-full can now record inferiors that have multiple threads, with
+  some caveats.  First and most important, the scheduler locking must be
+  set to "on", and the user must execute each thread individually.
+  Additionally, no new threads may be created during the recording.
+
 * New targets
 
 i386 Windows/MSVC               i[34567]86-*-windows-msvc
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 66152d2582e..5888691a4e5 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -8057,11 +8057,18 @@ program counter register) and the memory of the inferior are still
 changed as they normally would.  Their contents are taken from the
 execution log.
 
+If the inferior has multiple threads, they will be replayed in the
+order they were recorded in, ignoring settings that change the
+scheduler.
+
 @cindex record mode
 If the record for the next instruction is not in the execution log,
 @value{GDBN} will debug in @dfn{record mode}.  In this mode, the
 inferior executes normally, and @value{GDBN} records the execution log
-for future replay.
+for future replay.  For multi-threaded inferiors, the user must
+perform the scheduling by hand, executing each thread manually for the
+desired amount of time before switching to a different thread.  As of
+now GDB is unable to record the creation of new threads.
 
 The process record and replay target supports reverse execution
 (@pxref{Reverse Execution}), even if the platform on which the
-- 
2.56.0


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

* Re: [PATCH 7/7] gdb/record: document recording multi-threaded inferiors
  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
  0 siblings, 1 reply; 14+ messages in thread
From: Eli Zaretskii @ 2026-10-30  6:43 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches

> From: Guinevere Larsen <guinevere@redhat.com>
> Cc: Guinevere Larsen <guinevere@redhat.com>
> Date: Thu, 29 Oct 2026 17:57:43 -0300
> 
> This commit adds an entry to the NEWS file about the support for
> recording multi-threaded programs, as well as a quick explanation of
> supporting it and the limitations to the documentation.
> ---
>  gdb/NEWS            | 5 +++++
>  gdb/doc/gdb.texinfo | 9 ++++++++-
>  2 files changed, 13 insertions(+), 1 deletion(-)

This is okay, but I wonder why the manual doesn't mention the caveat
which NEWS spells out.  Conversely, the manual mentions one more
caveat: "as of now GDB is unable to record the creation of new
threads", but NEWS doesn't.  Should we make these two places convey
the same information?

Thanks.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>

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

* Re: [PATCH 7/7] gdb/record: document recording multi-threaded inferiors
  2026-10-30  6:43   ` Eli Zaretskii
@ 2026-10-30 11:32     ` Guinevere Larsen
  2026-10-30 12:44       ` Eli Zaretskii
  0 siblings, 1 reply; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-30 11:32 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches


On 10/30/26 3:43 AM, Eli Zaretskii wrote:
>> From: Guinevere Larsen <guinevere@redhat.com>
>> Cc: Guinevere Larsen <guinevere@redhat.com>
>> Date: Thu, 29 Oct 2026 17:57:43 -0300
>>
>> This commit adds an entry to the NEWS file about the support for
>> recording multi-threaded programs, as well as a quick explanation of
>> supporting it and the limitations to the documentation.
>> ---
>>   gdb/NEWS            | 5 +++++
>>   gdb/doc/gdb.texinfo | 9 ++++++++-
>>   2 files changed, 13 insertions(+), 1 deletion(-)
> This is okay, but I wonder why the manual doesn't mention the caveat
> which NEWS spells out.  Conversely, the manual mentions one more
> caveat: "as of now GDB is unable to record the creation of new
> threads", but NEWS doesn't.  Should we make these two places convey
> the same information?

Both entries do say the same information, just in different ways.

The NEWS does say that GDB can't record the creation of new threads, in 
here:
+ Additionally, no new threads may be created during the recording.

And this entry in the NEWS:
+ First and most important, the scheduler locking must be set to "on", 
and the user must execute each thread individually
Is equivalent to the following information in the documentation:
+ For multi-threaded inferiors, the user must perform the scheduling by 
hand, executing each thread manually for the desired amount of time 
before switching to a different thread

This is what "scheduler-locking on" does, but spelled out in case a user 
isn't familiar with the workings of the command. If you think there's a 
clearer way to say it, I'm all ears, as I know my familiarity with the 
code makes it hard to estimate what is simple/obvious knowledge.

-- 
Cheers,
Guinevere Larsen
it/its
she/her (deprecated)

>   
> Thanks.
>
> Reviewed-By: Eli Zaretskii <eliz@gnu.org>


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

* Re: [PATCH 7/7] gdb/record: document recording multi-threaded inferiors
  2026-10-30 11:32     ` Guinevere Larsen
@ 2026-10-30 12:44       ` Eli Zaretskii
  2026-10-30 13:20         ` Guinevere Larsen
  0 siblings, 1 reply; 14+ messages in thread
From: Eli Zaretskii @ 2026-10-30 12:44 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches

> Date: Fri, 30 Oct 2026 08:32:55 -0300
> Cc: gdb-patches@sourceware.org
> From: Guinevere Larsen <guinevere@redhat.com>
> 
> 
> On 10/30/26 3:43 AM, Eli Zaretskii wrote:
> >> From: Guinevere Larsen <guinevere@redhat.com>
> >> Cc: Guinevere Larsen <guinevere@redhat.com>
> >> Date: Thu, 29 Oct 2026 17:57:43 -0300
> >>
> >> This commit adds an entry to the NEWS file about the support for
> >> recording multi-threaded programs, as well as a quick explanation of
> >> supporting it and the limitations to the documentation.
> >> ---
> >>   gdb/NEWS            | 5 +++++
> >>   gdb/doc/gdb.texinfo | 9 ++++++++-
> >>   2 files changed, 13 insertions(+), 1 deletion(-)
> > This is okay, but I wonder why the manual doesn't mention the caveat
> > which NEWS spells out.  Conversely, the manual mentions one more
> > caveat: "as of now GDB is unable to record the creation of new
> > threads", but NEWS doesn't.  Should we make these two places convey
> > the same information?
> 
> Both entries do say the same information, just in different ways.
> 
> The NEWS does say that GDB can't record the creation of new threads, in 
> here:
> + Additionally, no new threads may be created during the recording.
> 
> And this entry in the NEWS:
> + First and most important, the scheduler locking must be set to "on", 
> and the user must execute each thread individually
> Is equivalent to the following information in the documentation:
> + For multi-threaded inferiors, the user must perform the scheduling by 
> hand, executing each thread manually for the desired amount of time 
> before switching to a different thread
> 
> This is what "scheduler-locking on" does, but spelled out in case a user 
> isn't familiar with the workings of the command. If you think there's a 
> clearer way to say it, I'm all ears, as I know my familiarity with the 
> code makes it hard to estimate what is simple/obvious knowledge.

I suggest to say this in the same wording in both cases.  I think the
NEWS wording is more clear in the former case and the manual's wording
is clear in the second case.

Thanks.

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

* Re: [PATCH 7/7] gdb/record: document recording multi-threaded inferiors
  2026-10-30 12:44       ` Eli Zaretskii
@ 2026-10-30 13:20         ` Guinevere Larsen
  2026-10-30 13:38           ` Eli Zaretskii
  0 siblings, 1 reply; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-30 13:20 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches


On 10/30/26 9:44 AM, Eli Zaretskii wrote:
>> Date: Fri, 30 Oct 2026 08:32:55 -0300
>> Cc: gdb-patches@sourceware.org
>> From: Guinevere Larsen <guinevere@redhat.com>
>>
>>
>> On 10/30/26 3:43 AM, Eli Zaretskii wrote:
>>>> From: Guinevere Larsen <guinevere@redhat.com>
>>>> Cc: Guinevere Larsen <guinevere@redhat.com>
>>>> Date: Thu, 29 Oct 2026 17:57:43 -0300
>>>>
>>>> This commit adds an entry to the NEWS file about the support for
>>>> recording multi-threaded programs, as well as a quick explanation of
>>>> supporting it and the limitations to the documentation.
>>>> ---
>>>>    gdb/NEWS            | 5 +++++
>>>>    gdb/doc/gdb.texinfo | 9 ++++++++-
>>>>    2 files changed, 13 insertions(+), 1 deletion(-)
>>> This is okay, but I wonder why the manual doesn't mention the caveat
>>> which NEWS spells out.  Conversely, the manual mentions one more
>>> caveat: "as of now GDB is unable to record the creation of new
>>> threads", but NEWS doesn't.  Should we make these two places convey
>>> the same information?
>> Both entries do say the same information, just in different ways.
>>
>> The NEWS does say that GDB can't record the creation of new threads, in
>> here:
>> + Additionally, no new threads may be created during the recording.
>>
>> And this entry in the NEWS:
>> + First and most important, the scheduler locking must be set to "on",
>> and the user must execute each thread individually
>> Is equivalent to the following information in the documentation:
>> + For multi-threaded inferiors, the user must perform the scheduling by
>> hand, executing each thread manually for the desired amount of time
>> before switching to a different thread
>>
>> This is what "scheduler-locking on" does, but spelled out in case a user
>> isn't familiar with the workings of the command. If you think there's a
>> clearer way to say it, I'm all ears, as I know my familiarity with the
>> code makes it hard to estimate what is simple/obvious knowledge.
> I suggest to say this in the same wording in both cases.  I think the
> NEWS wording is more clear in the former case and the manual's wording
> is clear in the second case.

That's fair. I took your suggestion and updated it locally. I'll refrain 
from sending a v2 with just this change, though.

Are you ok with me adding your approval tag to this commit now, or would 
you prefer to see the patch before doing so?

-- 
Cheers,
Guinevere Larsen
it/its
she/her (deprecated)


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

* Re: [PATCH 7/7] gdb/record: document recording multi-threaded inferiors
  2026-10-30 13:20         ` Guinevere Larsen
@ 2026-10-30 13:38           ` Eli Zaretskii
  2026-10-30 13:40             ` Guinevere Larsen
  0 siblings, 1 reply; 14+ messages in thread
From: Eli Zaretskii @ 2026-10-30 13:38 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches

> Date: Fri, 30 Oct 2026 10:20:12 -0300
> Cc: gdb-patches@sourceware.org
> From: Guinevere Larsen <guinevere@redhat.com>
> 
> 
> On 10/30/26 9:44 AM, Eli Zaretskii wrote:
> >> Date: Fri, 30 Oct 2026 08:32:55 -0300
> >> Cc: gdb-patches@sourceware.org
> >> From: Guinevere Larsen <guinevere@redhat.com>
> >>
> >>
> >> On 10/30/26 3:43 AM, Eli Zaretskii wrote:
> >>>> From: Guinevere Larsen <guinevere@redhat.com>
> >>>> Cc: Guinevere Larsen <guinevere@redhat.com>
> >>>> Date: Thu, 29 Oct 2026 17:57:43 -0300
> >>>>
> >>>> This commit adds an entry to the NEWS file about the support for
> >>>> recording multi-threaded programs, as well as a quick explanation of
> >>>> supporting it and the limitations to the documentation.
> >>>> ---
> >>>>    gdb/NEWS            | 5 +++++
> >>>>    gdb/doc/gdb.texinfo | 9 ++++++++-
> >>>>    2 files changed, 13 insertions(+), 1 deletion(-)
> >>> This is okay, but I wonder why the manual doesn't mention the caveat
> >>> which NEWS spells out.  Conversely, the manual mentions one more
> >>> caveat: "as of now GDB is unable to record the creation of new
> >>> threads", but NEWS doesn't.  Should we make these two places convey
> >>> the same information?
> >> Both entries do say the same information, just in different ways.
> >>
> >> The NEWS does say that GDB can't record the creation of new threads, in
> >> here:
> >> + Additionally, no new threads may be created during the recording.
> >>
> >> And this entry in the NEWS:
> >> + First and most important, the scheduler locking must be set to "on",
> >> and the user must execute each thread individually
> >> Is equivalent to the following information in the documentation:
> >> + For multi-threaded inferiors, the user must perform the scheduling by
> >> hand, executing each thread manually for the desired amount of time
> >> before switching to a different thread
> >>
> >> This is what "scheduler-locking on" does, but spelled out in case a user
> >> isn't familiar with the workings of the command. If you think there's a
> >> clearer way to say it, I'm all ears, as I know my familiarity with the
> >> code makes it hard to estimate what is simple/obvious knowledge.
> > I suggest to say this in the same wording in both cases.  I think the
> > NEWS wording is more clear in the former case and the manual's wording
> > is clear in the second case.
> 
> That's fair. I took your suggestion and updated it locally. I'll refrain 
> from sending a v2 with just this change, though.
> 
> Are you ok with me adding your approval tag to this commit now, or would 
> you prefer to see the patch before doing so?

No, you can consider it approved, thanks.

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

* Re: [PATCH 7/7] gdb/record: document recording multi-threaded inferiors
  2026-10-30 13:38           ` Eli Zaretskii
@ 2026-10-30 13:40             ` Guinevere Larsen
  0 siblings, 0 replies; 14+ messages in thread
From: Guinevere Larsen @ 2026-10-30 13:40 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches


On 10/30/26 10:38 AM, Eli Zaretskii wrote:
>> Date: Fri, 30 Oct 2026 10:20:12 -0300
>> Cc: gdb-patches@sourceware.org
>> From: Guinevere Larsen <guinevere@redhat.com>
>>
>>
>> On 10/30/26 9:44 AM, Eli Zaretskii wrote:
>>>> Date: Fri, 30 Oct 2026 08:32:55 -0300
>>>> Cc: gdb-patches@sourceware.org
>>>> From: Guinevere Larsen <guinevere@redhat.com>
>>>>
>>>>
>>>> On 10/30/26 3:43 AM, Eli Zaretskii wrote:
>>>>>> From: Guinevere Larsen <guinevere@redhat.com>
>>>>>> Cc: Guinevere Larsen <guinevere@redhat.com>
>>>>>> Date: Thu, 29 Oct 2026 17:57:43 -0300
>>>>>>
>>>>>> This commit adds an entry to the NEWS file about the support for
>>>>>> recording multi-threaded programs, as well as a quick explanation of
>>>>>> supporting it and the limitations to the documentation.
>>>>>> ---
>>>>>>     gdb/NEWS            | 5 +++++
>>>>>>     gdb/doc/gdb.texinfo | 9 ++++++++-
>>>>>>     2 files changed, 13 insertions(+), 1 deletion(-)
>>>>> This is okay, but I wonder why the manual doesn't mention the caveat
>>>>> which NEWS spells out.  Conversely, the manual mentions one more
>>>>> caveat: "as of now GDB is unable to record the creation of new
>>>>> threads", but NEWS doesn't.  Should we make these two places convey
>>>>> the same information?
>>>> Both entries do say the same information, just in different ways.
>>>>
>>>> The NEWS does say that GDB can't record the creation of new threads, in
>>>> here:
>>>> + Additionally, no new threads may be created during the recording.
>>>>
>>>> And this entry in the NEWS:
>>>> + First and most important, the scheduler locking must be set to "on",
>>>> and the user must execute each thread individually
>>>> Is equivalent to the following information in the documentation:
>>>> + For multi-threaded inferiors, the user must perform the scheduling by
>>>> hand, executing each thread manually for the desired amount of time
>>>> before switching to a different thread
>>>>
>>>> This is what "scheduler-locking on" does, but spelled out in case a user
>>>> isn't familiar with the workings of the command. If you think there's a
>>>> clearer way to say it, I'm all ears, as I know my familiarity with the
>>>> code makes it hard to estimate what is simple/obvious knowledge.
>>> I suggest to say this in the same wording in both cases.  I think the
>>> NEWS wording is more clear in the former case and the manual's wording
>>> is clear in the second case.
>> That's fair. I took your suggestion and updated it locally. I'll refrain
>> from sending a v2 with just this change, though.
>>
>> Are you ok with me adding your approval tag to this commit now, or would
>> you prefer to see the patch before doing so?
> No, you can consider it approved, thanks.
Thank you!

-- 
Cheers,
Guinevere Larsen
it/its
she/her (deprecated)


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

end of thread, other threads:[~2026-10-30 13:40 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 3/7] gdb/record: Make GDB automatically switch threads when replaying Guinevere Larsen
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox