From 92dc62fe6e1f60ca9085ed19876dd4f990cb824f Mon Sep 17 00:00:00 2001 From: Markus Metzger Date: Wed, 13 Aug 2025 14:25:27 +0000 Subject: [PATCH] btrace: do not stop replaying or recording while a thread is running With asynchronous stepping commands, one may start replaying a thread in the background and then stop recording its inferior in the foreground. This causes the execution history to be cleared and the record target to be unpushed while the thread is using said execution history. I fail to see a use-case for this, so rather than trying to make this work, I prevent such a scenario by not allowing replaying or recording to be stopped while a thread is running. We could do this only when a thread is running in replay mode and preserve the existing behavior of being able to stop recording while threads are running in recording mode. It seems more consistent to not allow this for both replaying and recording threads, though. --- gdb/record-btrace.c | 24 ++++++++++++++++++--- gdb/testsuite/gdb.btrace/enable-running.exp | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c index 1af9f49ec60..1534b76de77 100644 --- a/gdb/record-btrace.c +++ b/gdb/record-btrace.c @@ -413,6 +413,11 @@ record_btrace_target::stop_recording () { DEBUG ("stop recording"); + /* Check that before so stop recording is atomic. */ + for (thread_info &tp : current_inferior ()->non_exited_threads ()) + if (tp.state == THREAD_RUNNING) + error (_("You cannot stop recording while threads are running.")); + bool is_replaying = record_is_replaying (inferior_ptid); record_stop_replaying (); record_btrace_auto_disable (); @@ -2140,12 +2145,25 @@ record_btrace_start_replaying (struct thread_info *tp) static void record_btrace_stop_replaying (struct thread_info *tp) { - struct btrace_thread_info *btinfo; + struct btrace_thread_info *btinfo = &tp->btrace; - btinfo = &tp->btrace; + if (btinfo->replay == nullptr) + return; + + switch (tp->state) + { + case THREAD_STOPPED: + break; + + case THREAD_RUNNING: + error (_("Cannot stop replaying a running thread.")); + + case THREAD_EXITED: + gdb_assert_not_reached ("unexpected thread state"); + } xfree (btinfo->replay); - btinfo->replay = NULL; + btinfo->replay = nullptr; /* Make sure we're not leaving any stale registers. */ registers_changed_thread (tp); diff --git a/gdb/testsuite/gdb.btrace/enable-running.exp b/gdb/testsuite/gdb.btrace/enable-running.exp index 92ae5663cc4..c2e64864b28 100644 --- a/gdb/testsuite/gdb.btrace/enable-running.exp +++ b/gdb/testsuite/gdb.btrace/enable-running.exp @@ -93,4 +93,4 @@ foreach thread {1 2 3 4} { } # Stop recording while all threads are running. -gdb_test "record stop" "Process record is stopped \[^\\\r\\\n\]*" +gdb_test "record stop" "You cannot stop recording while threads are running\." -- 2.47.3