From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id ecDaNyi042oHLA8AWB0awg (envelope-from ) for ; Thu, 29 Oct 2026 17:01:28 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=C106acsN; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id DF063378002; Thu, 29 Oct 2026 17:01:28 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_SBL_CSS,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=no autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 01D991E007 for ; Thu, 29 Oct 2026 17:01:27 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C74A74BA23FA for ; Thu, 29 Oct 2026 21:01:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C74A74BA23FA Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=C106acsN Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id BC2134BAE7CB for ; Thu, 29 Oct 2026 20:58:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BC2134BAE7CB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BC2134BAE7CB Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793307511; cv=none; b=sMRkjAFhTxUsRA38IYZ8hOuBVPXa83PGvpBH2ojYGNrY15IKxwNZo4igiEwUVYLOR2X0yUUuKpZwSkHuJ/BRko2Sz4nSPBCb6edfr6Yei1xkxoh+dCsyyCM7ktAPD5zOQKtpR78izY/JnPFnh4zp3c+o3LSipEprr+cQepRHyXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793307511; c=relaxed/simple; bh=jxT4wZ92g3e74siESFabyNfheNwghAc4h52G1ihsTko=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=iR/H436nA5lWd9NAJ5Brf9Y/MJcyBjwWtJF9FPJf7fHZllSQNl4ChjYG6ZdHgYz9pjiK1P11XvwAcnJC7q8XMTaVFJlA5omP0C98q/mjd0+ld09dfRkNGXjJ0gQUwp7qHOniGzWxdvE77D7qarFRCkYlBU8Jyo/vMgnx//9aM2U= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=C106acsN DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC2134BAE7CB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793307511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cm7OWL2TSGe/Dlb0FsmMNOEbcfTQMhGF4TLSpEpKBsE=; b=C106acsNJxhXHb5E4lJPLJAuf9efS/cDZtwN5j1PzOKlwS2AlbMy7kJgEoJGf7PLeau4uh borD81aTeOF5CMbd4m9nR54wHJyXum+fxmJEIn1YtZ6Vumc0/yxCqP1YwVgAzUGoYpM54w V0ahd+GZZPmveMY1xYz/r8h25sJxc5I= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-116-ETHmhHcnMXmDg6ykCj6i9A-1; Thu, 29 Oct 2026 16:58:29 -0400 X-MC-Unique: ETHmhHcnMXmDg6ykCj6i9A-1 X-Mimecast-MFC-AGG-ID: ETHmhHcnMXmDg6ykCj6i9A_1793307509 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0F0241955F18 for ; Thu, 29 Oct 2026 20:58:29 +0000 (UTC) Received: from glarsen-thinkpadp16vgen1.rmtbr.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 14AC25AA; Thu, 29 Oct 2026 20:58:27 +0000 (UTC) From: Guinevere Larsen To: gdb-patches@sourceware.org Cc: Guinevere Larsen Subject: [PATCH 5/7] gdb/record: Record through a thread exiting Date: Thu, 29 Oct 2026 17:57:41 -0300 Message-ID: <20261029205743.1917668-6-guinevere@redhat.com> In-Reply-To: <20261029205743.1917668-1-guinevere@redhat.com> References: <20261029205743.1917668-1-guinevere@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 7UOljFCWbtNZEKyntM9VDtSkC5S4x43G54CiK1epT2k_1793307509 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 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 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 record_full_core_regbuf; static std::vector 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 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& 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 + (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& 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 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 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