@@ -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++)
new file mode 100644
@@ -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"