[1/7] gdb/record-full: Add thread pointer to recorded instructions

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

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed

Commit Message

Guinevere Larsen Oct. 29, 2026, 8:57 p.m. UTC
  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
  

Patch

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
+}