Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH v3 0/4] gdb, btrace: infrun fixes
@ 2022-07-04 11:54 Markus Metzger via Gdb-patches
  2022-07-04 11:54 ` [PATCH v3 1/4] gdb, infrun, btrace: fix reverse/replay stepping at end of execution history Markus Metzger via Gdb-patches
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Markus Metzger via Gdb-patches @ 2022-07-04 11:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: pedro

Changes in v3:
  - fix an assertion in clean_up_just_stopped_threads_fsms after
    finish_step_over changed the current thread.

Changes in v2:
  - address feedback from Pedro Alves

There's still an open question raised here:
https://sourceware.org/pipermail/gdb-patches/2021-November/183700.html.

    I'm wondering if prepare_one_step() should reset tp->control.step_stop
    to zero.  It does re-initialize tp->control.step_range_start/end and
    we do start another step.

Markus Metzger (4):
  gdb, infrun, btrace: fix reverse/replay stepping at end of execution
    history
  gdb, infrun, record: fix hang when step-over fails with no-history
  gdb, infrun, record: move no-history notification into normal_stop
  gdb, infrun: fix multi-threaded reverse stepping

 gdb/gdbthread.h                               |  13 +++
 gdb/infrun.c                                  |  88 +++++++++++----
 gdb/infrun.h                                  |   7 --
 gdb/record-btrace.c                           |  19 ++--
 gdb/testsuite/gdb.btrace/cont-hang.exp        |  47 ++++++++
 .../gdb.btrace/implicit-stop-replaying.exp    | 105 ++++++++++++++++++
 .../gdb.btrace/multi-thread-break-hang.exp    |  88 +++++++++++++++
 gdb/testsuite/gdb.btrace/step-hang.exp        |  46 ++++++++
 gdb/testsuite/gdb.btrace/stepn.exp            |  54 +++++++++
 9 files changed, 431 insertions(+), 36 deletions(-)
 create mode 100644 gdb/testsuite/gdb.btrace/cont-hang.exp
 create mode 100644 gdb/testsuite/gdb.btrace/implicit-stop-replaying.exp
 create mode 100644 gdb/testsuite/gdb.btrace/multi-thread-break-hang.exp
 create mode 100644 gdb/testsuite/gdb.btrace/step-hang.exp
 create mode 100644 gdb/testsuite/gdb.btrace/stepn.exp

-- 
2.35.3

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


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

* [PATCH v3 1/4] gdb, infrun, btrace: fix reverse/replay stepping at end of execution history
  2022-07-04 11:54 [PATCH v3 0/4] gdb, btrace: infrun fixes Markus Metzger via Gdb-patches
@ 2022-07-04 11:54 ` Markus Metzger via Gdb-patches
  2022-07-04 11:54 ` [PATCH v3 2/4] gdb, infrun, record: fix hang when step-over fails with no-history Markus Metzger via Gdb-patches
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Markus Metzger via Gdb-patches @ 2022-07-04 11:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: pedro

When trying to step over a breakpoint at the end of the trace, the
step-over will fail with no-history.  This does not clear step_over_info
so a subsequent resume will cause GDB to not resume the thread and expect
a SIGTRAP to complete the step-over.  This will never come causing GDB to
hang in the wait-for-event poll.

That step-over failed after actually completing the step.  This is wrong.
The step-over itself should have failed and the step should not have
completed.  Fix it by moving the end of execution history check to before
we are stepping.

This exposes another issue, however.  When completing a step-over at the
end of the execution history, we implicitly stop replaying that thread.  A
continue command would resume after the step-over and, since we're no
longer replaying, would continue recording.

Fix that by recording the replay state in the thread's control state and
failing with no-history in keep_going if we're switching from replay to
recording.
---
 gdb/gdbthread.h                        |  3 ++
 gdb/infrun.c                           | 25 ++++++++++++
 gdb/record-btrace.c                    | 19 ++++-----
 gdb/testsuite/gdb.btrace/cont-hang.exp | 47 ++++++++++++++++++++++
 gdb/testsuite/gdb.btrace/step-hang.exp | 46 ++++++++++++++++++++++
 gdb/testsuite/gdb.btrace/stepn.exp     | 54 ++++++++++++++++++++++++++
 6 files changed, 185 insertions(+), 9 deletions(-)
 create mode 100644 gdb/testsuite/gdb.btrace/cont-hang.exp
 create mode 100644 gdb/testsuite/gdb.btrace/step-hang.exp
 create mode 100644 gdb/testsuite/gdb.btrace/stepn.exp

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 1a33eb61221..7799511f918 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -171,6 +171,9 @@ struct thread_control_state
      command.  This is used to decide whether "set scheduler-locking
      step" behaves like "on" or "off".  */
   int stepping_command = 0;
+
+  /* Whether the thread was replaying when the command was issued.  */
+  bool is_replaying = false;
 };
 
 /* Inferior thread specific part of `struct infcall_suspend_state'.  */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 02c98b50c8c..a821ba06b22 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -2781,6 +2781,8 @@ clear_proceed_status_thread (struct thread_info *tp)
 
   /* Discard any remaining commands or status from previous stop.  */
   bpstat_clear (&tp->control.stop_bpstat);
+
+  tp->control.is_replaying = target_record_is_replaying (tp->ptid);
 }
 
 void
@@ -8154,6 +8156,29 @@ keep_going_pass_signal (struct execution_control_state *ecs)
   gdb_assert (ecs->event_thread->ptid == inferior_ptid);
   gdb_assert (!ecs->event_thread->resumed ());
 
+  /* When a thread reaches the end of its execution history, it automatically
+     stops replaying.  This is so the user doesn't need to explicitly stop it
+     with a separate command.
+
+     We do not want a single command (e.g. continue) to transition from
+     replaying to recording, though, e.g. when starting from a breakpoint we
+     needed to step over at the end of the trace.  When we reach the end of the
+     execution history during stepping, stop with no-history.
+
+     The other direction is fine.  When we're at the end of the execution
+     history, we may reverse-continue to start replaying.  */
+  if (ecs->event_thread->control.is_replaying
+      && !target_record_is_replaying (ecs->event_thread->ptid))
+    {
+      gdb::observers::no_history.notify ();
+      ecs->ws.set_no_history ();
+      set_last_target_status (ecs->target, ecs->ptid, ecs->ws);
+      stop_print_frame = true;
+      stop_waiting (ecs);
+      normal_stop ();
+      return;
+    }
+
   /* Save the pc before execution, to compare with pc after stop.  */
   ecs->event_thread->prev_pc
     = regcache_read_pc_protected (get_thread_regcache (ecs->event_thread));
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 3f8a69dd04f..ba8debff031 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -2335,6 +2335,16 @@ record_btrace_single_step_forward (struct thread_info *tp)
   if (replay == NULL)
     return btrace_step_no_history ();
 
+  /* The execution trace contains (and ends with) the current instruction.
+     This instruction has not been executed, yet, so the trace really ends
+     one instruction earlier.
+
+     We'd fail later on in btrace_insn_next () but we must not trigger
+     breakpoints as we're not really able to step.  */
+  btrace_insn_end (&end, btinfo);
+  if (btrace_insn_cmp (replay, &end) == 0)
+    return btrace_step_no_history ();
+
   /* Check if we're stepping a breakpoint.  */
   if (record_btrace_replay_at_breakpoint (tp))
     return btrace_step_stopped ();
@@ -2357,15 +2367,6 @@ record_btrace_single_step_forward (struct thread_info *tp)
     }
   while (btrace_insn_get (replay) == NULL);
 
-  /* Determine the end of the instruction trace.  */
-  btrace_insn_end (&end, btinfo);
-
-  /* The execution trace contains (and ends with) the current instruction.
-     This instruction has not been executed, yet, so the trace really ends
-     one instruction earlier.  */
-  if (btrace_insn_cmp (replay, &end) == 0)
-    return btrace_step_no_history ();
-
   return btrace_step_spurious ();
 }
 
diff --git a/gdb/testsuite/gdb.btrace/cont-hang.exp b/gdb/testsuite/gdb.btrace/cont-hang.exp
new file mode 100644
index 00000000000..21e15c1ed69
--- /dev/null
+++ b/gdb/testsuite/gdb.btrace/cont-hang.exp
@@ -0,0 +1,47 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2021-2022 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/>.
+
+# Test that we do not hang when trying to continue over a breakpoint at
+# the end of the trace.
+
+if { [skip_btrace_tests] } {
+    unsupported "target does not support record-btrace"
+    return -1
+}
+
+standard_testfile record_goto.c
+if [prepare_for_testing "failed to prepare" $testfile $srcfile] {
+    return -1
+}
+
+if ![runto_main] {
+    untested "failed to run to main"
+    return -1
+}
+
+# Trace the call to the test function.
+gdb_test_no_output "record btrace"
+gdb_test "next" "main\.3.*"
+
+# We need to be replaying, otherwise, we'd just continue recording.
+gdb_test "reverse-stepi"
+gdb_test "break"
+
+# Continuing will step over the breakpoint and then run into the end of
+# the execution history.  This ends replay, so we can continue recording.
+gdb_test "continue" "No more reverse-execution history.*"
+gdb_continue_to_end
diff --git a/gdb/testsuite/gdb.btrace/step-hang.exp b/gdb/testsuite/gdb.btrace/step-hang.exp
new file mode 100644
index 00000000000..94a598729f1
--- /dev/null
+++ b/gdb/testsuite/gdb.btrace/step-hang.exp
@@ -0,0 +1,46 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2021-2022 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/>.
+
+# Test that we do not hang when trying to step over a breakpoint at the
+# end of the trace.
+
+if { [skip_btrace_tests] } {
+    unsupported "target does not support record-btrace"
+    return -1
+}
+
+standard_testfile record_goto.c
+if [prepare_for_testing "failed to prepare" $testfile $srcfile] {
+    return -1
+}
+
+if ![runto_main] {
+    untested "failed to run to main"
+    return -1
+}
+
+# Trace the call to the test function.
+gdb_test_no_output "record btrace"
+gdb_test "next" "main\.3.*"
+
+# We need to be replaying, otherwise, we'd just continue recording.
+gdb_test "reverse-stepi"
+gdb_test "break"
+
+# Stepping over the breakpoint ends replaying and we can continue recording.
+gdb_test "step"  "main\.3.*"
+gdb_continue_to_end
diff --git a/gdb/testsuite/gdb.btrace/stepn.exp b/gdb/testsuite/gdb.btrace/stepn.exp
new file mode 100644
index 00000000000..821278ba534
--- /dev/null
+++ b/gdb/testsuite/gdb.btrace/stepn.exp
@@ -0,0 +1,54 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2021-2022 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/>.
+
+# Test that step n does not start recording when issued while replaying.
+
+if { [skip_btrace_tests] } {
+    unsupported "target does not support record-btrace"
+    return -1
+}
+
+standard_testfile record_goto.c
+if [prepare_for_testing "failed to prepare" $testfile $srcfile] {
+    return -1
+}
+
+if ![runto_main] {
+    untested "failed to run to main"
+    return -1
+}
+
+# Trace the call to the test function.
+gdb_test_no_output "record btrace"
+gdb_test "next" "main\.3.*"
+
+# Stepping should bring us to the end of the execution history, but should
+# not resume recording.
+with_test_prefix "stepi" {
+    gdb_test "reverse-stepi"
+    gdb_test "stepi 5" "No more reverse-execution history.*main\.3.*"
+}
+
+with_test_prefix "step" {
+    gdb_test "reverse-step"
+    gdb_test "step 5" "No more reverse-execution history.*main\.3.*"
+}
+
+with_test_prefix "next" {
+    gdb_test "reverse-step"
+    gdb_test "next 5" "No more reverse-execution history.*main\.3.*"
+}
-- 
2.35.3

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


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

* [PATCH v3 2/4] gdb, infrun, record: fix hang when step-over fails with no-history
  2022-07-04 11:54 [PATCH v3 0/4] gdb, btrace: infrun fixes Markus Metzger via Gdb-patches
  2022-07-04 11:54 ` [PATCH v3 1/4] gdb, infrun, btrace: fix reverse/replay stepping at end of execution history Markus Metzger via Gdb-patches
@ 2022-07-04 11:54 ` Markus Metzger via Gdb-patches
  2022-07-04 11:54 ` [PATCH v3 3/4] gdb, infrun, record: move no-history notification into normal_stop Markus Metzger via Gdb-patches
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Markus Metzger via Gdb-patches @ 2022-07-04 11:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: pedro

When trying to step over a breakpoint at the end of the trace while
another thread is replaying, the step-over will fail with no-history.
This does not clear step_over_info so a subsequent resume will cause GDB
to not resume the thread and expect a SIGTRAP to complete the step-over.
This will never come causing GDB to hang in the wait-for-event poll.

This is a variant of the issue fixed in the parent commit.  That commit
addressed the issue for a single-threaded process and fixed an issue with
reverse/replay stepping in general.

This commit addresses the issue for a multi-threaded process.  In this
case, the single-step does not complete.

Finish an in-flight step-over when a thread stopped with NO_HISTORY.
Since we did not move, we will simply start the step-over again.
---
 gdb/infrun.c                                  | 14 +++
 .../gdb.btrace/multi-thread-break-hang.exp    | 88 +++++++++++++++++++
 2 files changed, 102 insertions(+)
 create mode 100644 gdb/testsuite/gdb.btrace/multi-thread-break-hang.exp

diff --git a/gdb/infrun.c b/gdb/infrun.c
index a821ba06b22..3fa262efb01 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -74,6 +74,8 @@
 #include "gdbsupport/common-debug.h"
 #include "gdbsupport/buildargv.h"
 
+struct execution_control_state;
+
 /* Prototypes for local functions */
 
 static void sig_print_info (enum gdb_signal);
@@ -103,6 +105,8 @@ static bool start_step_over (void);
 
 static bool step_over_info_valid_p (void);
 
+static int finish_step_over (execution_control_state *ecs);
+
 /* Asynchronous signal handler registered as event loop source for
    when we have pending events ready to be passed to the core.  */
 static struct async_event_handler *infrun_async_inferior_event_token;
@@ -5872,6 +5876,16 @@ handle_inferior_event (struct execution_control_state *ecs)
 	return;
 
       gdb::observers::no_history.notify ();
+
+      /* Cancel an in-flight step-over.  It will not succeed since we
+	 won't be able to step at the end of the execution history.  */
+      {
+	/* finish_step_over may call restart_threads, which may change the
+	   current thread.  make sure we leave the event thread as the
+	   current thread.  */
+	scoped_restore_current_thread restore_thread;
+	finish_step_over (ecs);
+      }
       stop_waiting (ecs);
       return;
     }
diff --git a/gdb/testsuite/gdb.btrace/multi-thread-break-hang.exp b/gdb/testsuite/gdb.btrace/multi-thread-break-hang.exp
new file mode 100644
index 00000000000..df0c984cee0
--- /dev/null
+++ b/gdb/testsuite/gdb.btrace/multi-thread-break-hang.exp
@@ -0,0 +1,88 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2021-2022 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/>.
+
+# Test that we cancel an in-flight step-over at the end of the execution
+# history as long as some other thread is still replaying.
+#
+# This used to cause GDB to hang in poll ().
+
+if { [skip_btrace_tests] } {
+    unsupported "target does not support record-btrace"
+    return -1
+}
+
+standard_testfile multi-thread-step.c
+if [prepare_for_testing "failed to prepare" $testfile $srcfile {debug pthreads}] {
+    return -1
+}
+
+if ![runto_main] {
+    untested "failed to run to main"
+    return -1
+}
+
+# Set up breakpoints.
+set bp_1 [gdb_get_line_number "bp.1" $srcfile]
+set bp_2 [gdb_get_line_number "bp.2" $srcfile]
+
+# Trace the code between the two breakpoints.
+gdb_breakpoint $srcfile:$bp_1
+gdb_continue_to_breakpoint "continue to bp.1" ".*$srcfile:$bp_1\r\n.*"
+
+gdb_test_no_output "record btrace"
+
+# We have two threads at or close to bp.1 but handled only one stop event.
+# Remove the breakpoint so we do not need to deal with the 2nd event.
+delete_breakpoints
+gdb_breakpoint $srcfile:$bp_2
+gdb_continue_to_breakpoint "continue to bp.2" ".*$srcfile:$bp_2\r\n.*"
+
+# Determine the thread that reported the breakpoint.
+set thread [get_integer_valueof "\$_thread" bad]
+
+# Determine the other thread.
+set other "bad"
+if { $thread == 1 } {
+    set other 2
+} elseif { $thread == 2 } {
+    set other 1
+}
+
+# This test requires scheduler-locking 'on' or 'step'; 'replay' would
+# implicitly stop replaying, avoiding the problem; 'off' would step one
+# and resume the other.
+#
+# With the current record-btrace implementation that steps all resumed
+# threads in lock-step, 'off' might actually pass but we don't want to
+# bake that behavior into tests.
+gdb_test_no_output "set scheduler-locking step"
+
+# Start replaying the other thread.  This will prevent stepping the thread
+# that reported the event.
+gdb_test "thread apply $other record goto begin" ".*"
+gdb_test "thread apply $other info record" "Replay in progress.*"
+
+# We're at a breakpoint so this triggers step-over.  Since we're at the
+# end of the trace, the step will fail.
+gdb_test "stepi" "No more reverse-execution history.*" "stepi.1"
+
+# We used to hang at the second step since step-over insisted on polling
+# the next event.
+gdb_test "stepi" "No more reverse-execution history.*" "stepi.2"
+
+# Do one more just in case.
+gdb_test "stepi" "No more reverse-execution history.*" "stepi.3"
-- 
2.35.3

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


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

* [PATCH v3 3/4] gdb, infrun, record: move no-history notification into normal_stop
  2022-07-04 11:54 [PATCH v3 0/4] gdb, btrace: infrun fixes Markus Metzger via Gdb-patches
  2022-07-04 11:54 ` [PATCH v3 1/4] gdb, infrun, btrace: fix reverse/replay stepping at end of execution history Markus Metzger via Gdb-patches
  2022-07-04 11:54 ` [PATCH v3 2/4] gdb, infrun, record: fix hang when step-over fails with no-history Markus Metzger via Gdb-patches
@ 2022-07-04 11:54 ` Markus Metzger via Gdb-patches
  2022-07-04 11:54 ` [PATCH v3 4/4] gdb, infrun: fix multi-threaded reverse stepping Markus Metzger via Gdb-patches
  2022-08-29  4:41 ` [PATCH v3 0/4] gdb, btrace: infrun fixes Metzger, Markus T via Gdb-patches
  4 siblings, 0 replies; 6+ messages in thread
From: Markus Metzger via Gdb-patches @ 2022-07-04 11:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: pedro

Leave calling gdb::observers::no_history.notify to normal_stop based on
the last waitstatus.
---
 gdb/infrun.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 3fa262efb01..2e42d3663a7 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -5875,8 +5875,6 @@ handle_inferior_event (struct execution_control_state *ecs)
       if (handle_stop_requested (ecs))
 	return;
 
-      gdb::observers::no_history.notify ();
-
       /* Cancel an in-flight step-over.  It will not succeed since we
 	 won't be able to step at the end of the execution history.  */
       {
@@ -8184,7 +8182,6 @@ keep_going_pass_signal (struct execution_control_state *ecs)
   if (ecs->event_thread->control.is_replaying
       && !target_record_is_replaying (ecs->event_thread->ptid))
     {
-      gdb::observers::no_history.notify ();
       ecs->ws.set_no_history ();
       set_last_target_status (ecs->target, ecs->ptid, ecs->ws);
       stop_print_frame = true;
@@ -8808,6 +8805,9 @@ normal_stop (void)
   if (saved_context.changed ())
     return 1;
 
+  if (last.kind () == TARGET_WAITKIND_NO_HISTORY)
+    gdb::observers::no_history.notify ();
+
   /* Notify observers about the stop.  This is where the interpreters
      print the stop event.  */
   if (inferior_ptid != null_ptid)
-- 
2.35.3

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


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

* [PATCH v3 4/4] gdb, infrun: fix multi-threaded reverse stepping
  2022-07-04 11:54 [PATCH v3 0/4] gdb, btrace: infrun fixes Markus Metzger via Gdb-patches
                   ` (2 preceding siblings ...)
  2022-07-04 11:54 ` [PATCH v3 3/4] gdb, infrun, record: move no-history notification into normal_stop Markus Metzger via Gdb-patches
@ 2022-07-04 11:54 ` Markus Metzger via Gdb-patches
  2022-08-29  4:41 ` [PATCH v3 0/4] gdb, btrace: infrun fixes Metzger, Markus T via Gdb-patches
  4 siblings, 0 replies; 6+ messages in thread
From: Markus Metzger via Gdb-patches @ 2022-07-04 11:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: pedro

When reverse-stepping a thread that has a pending breakpoint event, the
thread is not resumed as part of the infcmd function.  A first resume
notices the event and returns without resuming the target.

If the corresponding breakpoint has been deleted, event processing results
in a second resume that performs the intended stepping action.  That
resume happens after the infcmd function returned and the temporarily
modified execution direction was restored.  We end up resuming in the
wrong direction.

Store the direction in a thread's control state and change most of
infrun to take it from there rather than using the global variable.
---
 gdb/gdbthread.h                               |  10 ++
 gdb/infrun.c                                  |  47 ++++----
 gdb/infrun.h                                  |   7 --
 .../gdb.btrace/implicit-stop-replaying.exp    | 105 ++++++++++++++++++
 4 files changed, 143 insertions(+), 26 deletions(-)
 create mode 100644 gdb/testsuite/gdb.btrace/implicit-stop-replaying.exp

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 7799511f918..607749d6ccf 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -90,6 +90,13 @@ enum step_over_calls_kind
     STEP_OVER_UNDEBUGGABLE
   };
 
+/* Reverse execution.  */
+enum exec_direction_kind
+  {
+    EXEC_FORWARD,
+    EXEC_REVERSE
+  };
+
 /* Inferior thread specific part of `struct infcall_control_state'.
 
    Inferior process counterpart is `struct inferior_control_state'.  */
@@ -174,6 +181,9 @@ struct thread_control_state
 
   /* Whether the thread was replaying when the command was issued.  */
   bool is_replaying = false;
+
+  /* The execution direction when the command was issued.  */
+  enum exec_direction_kind execution_direction = EXEC_FORWARD;
 };
 
 /* Inferior thread specific part of `struct infcall_suspend_state'.  */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 2e42d3663a7..1c60a396a50 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -92,7 +92,8 @@ static void insert_step_resume_breakpoint_at_caller (struct frame_info *);
 
 static void insert_longjmp_resume_breakpoint (struct gdbarch *, CORE_ADDR);
 
-static bool maybe_software_singlestep (struct gdbarch *gdbarch);
+static bool maybe_software_singlestep (const thread_info *tp,
+				       gdbarch *gdbarch, CORE_ADDR pc);
 
 static void resume (gdb_signal sig);
 
@@ -2142,11 +2143,12 @@ bool sched_multi = false;
    GDBARCH the current gdbarch.  */
 
 static bool
-maybe_software_singlestep (struct gdbarch *gdbarch)
+maybe_software_singlestep (const thread_info *tp, gdbarch *gdbarch,
+			   CORE_ADDR pc)
 {
   bool hw_step = true;
 
-  if (execution_direction == EXEC_FORWARD
+  if (tp->control.execution_direction == EXEC_FORWARD
       && gdbarch_software_single_step_p (gdbarch))
     hw_step = !insert_single_step_breakpoints (gdbarch);
 
@@ -2270,6 +2272,10 @@ do_target_resume (ptid_t resume_ptid, bool step, enum gdb_signal sig)
   /* Install inferior's terminal modes.  */
   target_terminal::inferior ();
 
+  scoped_restore save_exec_dir
+    = make_scoped_restore (&execution_direction,
+			   tp->control.execution_direction);
+
   /* Avoid confusing the next resume, if the next stop/resume
      happens to apply to another thread.  */
   tp->set_stop_signal (GDB_SIGNAL_0);
@@ -2462,6 +2468,7 @@ resume_1 (enum gdb_signal sig)
 	      insert_breakpoints ();
 
 	      resume_ptid = internal_resume_ptid (user_step);
+
 	      do_target_resume (resume_ptid, false, GDB_SIGNAL_0);
 	      tp->set_resumed (true);
 	      return;
@@ -2511,7 +2518,7 @@ resume_1 (enum gdb_signal sig)
 	  set_step_over_info (regcache->aspace (),
 			      regcache_read_pc (regcache), 0, tp->global_num);
 
-	  step = maybe_software_singlestep (gdbarch);
+	  step = maybe_software_singlestep (tp, gdbarch, pc);
 
 	  insert_breakpoints ();
 	}
@@ -2530,7 +2537,7 @@ resume_1 (enum gdb_signal sig)
 
   /* Do we need to do it the hard way, w/temp breakpoints?  */
   else if (step)
-    step = maybe_software_singlestep (gdbarch);
+    step = maybe_software_singlestep (tp, gdbarch, pc);
 
   /* Currently, our software single-step implementation leads to different
      results than hardware single-stepping in one situation: when stepping
@@ -2601,7 +2608,7 @@ resume_1 (enum gdb_signal sig)
   else
     resume_ptid = internal_resume_ptid (user_step);
 
-  if (execution_direction != EXEC_REVERSE
+  if (tp->control.execution_direction != EXEC_REVERSE
       && step && breakpoint_inserted_here_p (aspace, pc))
     {
       /* There are two cases where we currently need to step a
@@ -2787,6 +2794,7 @@ clear_proceed_status_thread (struct thread_info *tp)
   bpstat_clear (&tp->control.stop_bpstat);
 
   tp->control.is_replaying = target_record_is_replaying (tp->ptid);
+  tp->control.execution_direction = ::execution_direction;
 }
 
 void
@@ -2886,7 +2894,7 @@ schedlock_applies (struct thread_info *tp)
 	      && tp->control.stepping_command)
 	  || (scheduler_mode == schedlock_replay
 	      && target_record_will_replay (minus_one_ptid,
-					    execution_direction)));
+					    tp->control.execution_direction)));
 }
 
 /* Set process_stratum_target::COMMIT_RESUMED_STATE in all target
@@ -3212,7 +3220,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
       if (cur_thr->stop_pc_p ()
 	  && pc == cur_thr->stop_pc ()
 	  && breakpoint_here_p (aspace, pc) == ordinary_breakpoint_here
-	  && execution_direction != EXEC_REVERSE)
+	  && cur_thr->control.execution_direction != EXEC_REVERSE)
 	/* There is a breakpoint at the address we will resume at,
 	   step one instruction before inserting breakpoints so that
 	   we do not stop right away (and report a second hit at this
@@ -4469,7 +4477,7 @@ adjust_pc_after_break (struct thread_info *thread,
      breakpoint at PC - 1.  We'd then report a hit on B1, although
      INSN1 hadn't been de-executed yet.  Doing nothing is the correct
      behaviour.  */
-  if (execution_direction == EXEC_REVERSE)
+  if (thread->control.execution_direction == EXEC_REVERSE)
     return;
 
   /* If the target can tell whether the thread hit a SW breakpoint,
@@ -6777,7 +6785,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 
       delete_step_resume_breakpoint (ecs->event_thread);
       if (ecs->event_thread->control.proceed_to_finish
-	  && execution_direction == EXEC_REVERSE)
+	  && ecs->event_thread->control.execution_direction == EXEC_REVERSE)
 	{
 	  struct thread_info *tp = ecs->event_thread;
 
@@ -6792,7 +6800,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 	}
       fill_in_stop_func (gdbarch, ecs);
       if (ecs->event_thread->stop_pc () == ecs->stop_func_start
-	  && execution_direction == EXEC_REVERSE)
+	  && ecs->event_thread->control.execution_direction == EXEC_REVERSE)
 	{
 	  /* We are stepping over a function call in reverse, and just
 	     hit the step-resume breakpoint at the start address of
@@ -6917,7 +6925,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 
   if (pc_in_thread_step_range (ecs->event_thread->stop_pc (),
 			       ecs->event_thread)
-      && (execution_direction != EXEC_REVERSE
+      && (ecs->event_thread->control.execution_direction != EXEC_REVERSE
 	  || frame_id_eq (get_frame_id (frame),
 			  ecs->event_thread->control.step_frame_id)))
     {
@@ -6937,7 +6945,7 @@ process_event_stop_test (struct execution_control_state *ecs)
       CORE_ADDR stop_pc = ecs->event_thread->stop_pc ();
       if (stop_pc == ecs->event_thread->control.step_range_start
 	  && stop_pc != ecs->stop_func_start
-	  && execution_direction == EXEC_REVERSE)
+	  && ecs->event_thread->control.execution_direction == EXEC_REVERSE)
 	end_stepping_range (ecs);
       else
 	keep_going (ecs);
@@ -6959,7 +6967,7 @@ process_event_stop_test (struct execution_control_state *ecs)
      backward through the trampoline code, and that's handled further
      down, so there is nothing for us to do here.  */
 
-  if (execution_direction != EXEC_REVERSE
+  if (ecs->event_thread->control.execution_direction != EXEC_REVERSE
       && ecs->event_thread->control.step_over_calls == STEP_OVER_UNDEBUGGABLE
       && in_solib_dynsym_resolve_code (ecs->event_thread->stop_pc ()))
     {
@@ -7091,7 +7099,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 
       /* Reverse stepping through solib trampolines.  */
 
-      if (execution_direction == EXEC_REVERSE
+      if (ecs->event_thread->control.execution_direction == EXEC_REVERSE
 	  && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE
 	  && (gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc)
 	      || (ecs->stop_func_start == 0
@@ -7119,7 +7127,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 	     stepped into (backwards), and continue to there.  When we
 	     get there, we'll need to single-step back to the caller.  */
 
-	  if (execution_direction == EXEC_REVERSE)
+	  if (ecs->event_thread->control.execution_direction == EXEC_REVERSE)
 	    {
 	      /* If we're already at the start of the function, we've either
 		 just stepped backward into a single instruction function,
@@ -7182,7 +7190,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 						  tmp_sal)
 	    && !inline_frame_is_marked_for_skip (true, ecs->event_thread))
 	  {
-	    if (execution_direction == EXEC_REVERSE)
+	    if (ecs->event_thread->control.execution_direction == EXEC_REVERSE)
 	      handle_step_into_function_backward (gdbarch, ecs);
 	    else
 	      handle_step_into_function (gdbarch, ecs);
@@ -7200,7 +7208,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 	  return;
 	}
 
-      if (execution_direction == EXEC_REVERSE)
+      if (ecs->event_thread->control.execution_direction == EXEC_REVERSE)
 	{
 	  /* If we're already at the start of the function, we've either just
 	     stepped backward into a single instruction function without line
@@ -7229,7 +7237,7 @@ process_event_stop_test (struct execution_control_state *ecs)
 
   /* Reverse stepping through solib trampolines.  */
 
-  if (execution_direction == EXEC_REVERSE
+  if (ecs->event_thread->control.execution_direction == EXEC_REVERSE
       && ecs->event_thread->control.step_over_calls != STEP_OVER_NONE)
     {
       CORE_ADDR stop_pc = ecs->event_thread->stop_pc ();
@@ -7753,6 +7761,7 @@ keep_going_stepped_thread (struct thread_info *tp)
 
       tp->set_resumed (true);
       resume_ptid = internal_resume_ptid (tp->control.stepping_command);
+
       do_target_resume (resume_ptid, false, GDB_SIGNAL_0);
     }
   else
diff --git a/gdb/infrun.h b/gdb/infrun.h
index 0c7c55eabec..2baa4984c12 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -105,13 +105,6 @@ extern bool disable_randomization;
    current location.  */
 extern ULONGEST get_stop_id (void);
 
-/* Reverse execution.  */
-enum exec_direction_kind
-  {
-    EXEC_FORWARD,
-    EXEC_REVERSE
-  };
-
 /* The current execution direction.  */
 extern enum exec_direction_kind execution_direction;
 
diff --git a/gdb/testsuite/gdb.btrace/implicit-stop-replaying.exp b/gdb/testsuite/gdb.btrace/implicit-stop-replaying.exp
new file mode 100644
index 00000000000..914451edebf
--- /dev/null
+++ b/gdb/testsuite/gdb.btrace/implicit-stop-replaying.exp
@@ -0,0 +1,105 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2021-2022 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/>.
+
+# Test that we stop replaying other threads when stepping a thread at the
+# end of its execution history.
+#
+# This is similar to the last test in multi-thread-step.exp, except that
+# we reverse-step instead of record goto begin to start replaying and we
+# step instead of continuing.
+#
+# This triggered a bug where GDB confused the execution direction and kept
+# stepping both threads backwards instead of forwards.
+
+if { [skip_btrace_tests] } {
+    unsupported "target does not support record-btrace"
+    return -1
+}
+
+standard_testfile multi-thread-step.c
+if [prepare_for_testing "failed to prepare" $testfile $srcfile {debug libs=-lpthread}] {
+    return -1
+}
+
+if ![runto_main] {
+    untested "failed to run to main"
+    return -1
+}
+
+# Set up breakpoints.
+set bp_1 [gdb_get_line_number "bp.1" $srcfile]
+set bp_2 [gdb_get_line_number "bp.2" $srcfile]
+
+# Trace the code between the two breakpoints.
+gdb_breakpoint $srcfile:$bp_1
+gdb_continue_to_breakpoint "continue to bp.1" ".*$srcfile:$bp_1\r\n.*"
+
+# Make sure GDB knows about the new thread.
+gdb_test "info threads" ".*"
+gdb_test_no_output "record btrace"
+
+# We have two threads at or close to bp.1 but handled only one stop event.
+# Remove the breakpoint so we do not need to deal with the 2nd event.
+delete_breakpoints
+gdb_breakpoint $srcfile:$bp_2
+gdb_continue_to_breakpoint "continue to bp.2" ".*$srcfile:$bp_2\r\n.*"
+
+# Determine the thread that reported the breakpoint.
+set thread "bad"
+gdb_test_multiple "thread" "thread" {
+    -re -wrap "Current thread is \($decimal\).*" {
+	set thread $expect_out(1,string)
+    }
+}
+
+# Determine the other thread.
+set other "bad"
+if { $thread == 1 } {
+    set other 2
+} elseif { $thread == 2 } {
+    set other 1
+}
+
+# This test only works for scheduler-locking 'replay'.
+gdb_test_no_output "set scheduler-locking replay"
+
+# Remove breakpoints or we might run into it right away.
+delete_breakpoints
+
+# Start replaying the other thread.
+gdb_test "thread apply $other reverse-stepi" ".*"
+gdb_test "thread apply $other info record" "Replay in progress.*"
+
+# Step the thread that reported the breakpoint, which is not replaying.
+gdb_test "next" "return arg;.*"
+
+proc check_not_replaying { thread } {
+    global gdb_prompt
+
+    gdb_test_multiple "thread apply $thread info record" \
+	"thread $thread not replaying" {
+        -re -wrap "Replay in progress.*" {
+            fail $gdb_test_name
+        }
+        -re "$gdb_prompt $" {
+            pass $gdb_test_name
+        }
+    }
+}
+
+check_not_replaying 1
+check_not_replaying 2
-- 
2.35.3

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


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

* RE: [PATCH v3 0/4] gdb, btrace: infrun fixes
  2022-07-04 11:54 [PATCH v3 0/4] gdb, btrace: infrun fixes Markus Metzger via Gdb-patches
                   ` (3 preceding siblings ...)
  2022-07-04 11:54 ` [PATCH v3 4/4] gdb, infrun: fix multi-threaded reverse stepping Markus Metzger via Gdb-patches
@ 2022-08-29  4:41 ` Metzger, Markus T via Gdb-patches
  4 siblings, 0 replies; 6+ messages in thread
From: Metzger, Markus T via Gdb-patches @ 2022-08-29  4:41 UTC (permalink / raw)
  To: gdb-patches; +Cc: pedro

ping,
markus.

>-----Original Message-----
>From: Metzger, Markus T <markus.t.metzger@intel.com>
>Sent: Montag, 4. Juli 2022 13:54
>To: gdb-patches@sourceware.org
>Cc: pedro@palves.net
>Subject: [PATCH v3 0/4] gdb, btrace: infrun fixes
>
>Changes in v3:
>  - fix an assertion in clean_up_just_stopped_threads_fsms after
>    finish_step_over changed the current thread.
>
>Changes in v2:
>  - address feedback from Pedro Alves
>
>There's still an open question raised here:
>https://sourceware.org/pipermail/gdb-patches/2021-November/183700.html.
>
>    I'm wondering if prepare_one_step() should reset tp->control.step_stop
>    to zero.  It does re-initialize tp->control.step_range_start/end and
>    we do start another step.
>
>Markus Metzger (4):
>  gdb, infrun, btrace: fix reverse/replay stepping at end of execution
>    history
>  gdb, infrun, record: fix hang when step-over fails with no-history
>  gdb, infrun, record: move no-history notification into normal_stop
>  gdb, infrun: fix multi-threaded reverse stepping
>
> gdb/gdbthread.h                               |  13 +++
> gdb/infrun.c                                  |  88 +++++++++++----
> gdb/infrun.h                                  |   7 --
> gdb/record-btrace.c                           |  19 ++--
> gdb/testsuite/gdb.btrace/cont-hang.exp        |  47 ++++++++
> .../gdb.btrace/implicit-stop-replaying.exp    | 105 ++++++++++++++++++
> .../gdb.btrace/multi-thread-break-hang.exp    |  88 +++++++++++++++
> gdb/testsuite/gdb.btrace/step-hang.exp        |  46 ++++++++
> gdb/testsuite/gdb.btrace/stepn.exp            |  54 +++++++++
> 9 files changed, 431 insertions(+), 36 deletions(-)
> create mode 100644 gdb/testsuite/gdb.btrace/cont-hang.exp
> create mode 100644 gdb/testsuite/gdb.btrace/implicit-stop-replaying.exp
> create mode 100644 gdb/testsuite/gdb.btrace/multi-thread-break-hang.exp
> create mode 100644 gdb/testsuite/gdb.btrace/step-hang.exp
> create mode 100644 gdb/testsuite/gdb.btrace/stepn.exp
>
>--
>2.35.3

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


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

end of thread, other threads:[~2022-08-29  4:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-04 11:54 [PATCH v3 0/4] gdb, btrace: infrun fixes Markus Metzger via Gdb-patches
2022-07-04 11:54 ` [PATCH v3 1/4] gdb, infrun, btrace: fix reverse/replay stepping at end of execution history Markus Metzger via Gdb-patches
2022-07-04 11:54 ` [PATCH v3 2/4] gdb, infrun, record: fix hang when step-over fails with no-history Markus Metzger via Gdb-patches
2022-07-04 11:54 ` [PATCH v3 3/4] gdb, infrun, record: move no-history notification into normal_stop Markus Metzger via Gdb-patches
2022-07-04 11:54 ` [PATCH v3 4/4] gdb, infrun: fix multi-threaded reverse stepping Markus Metzger via Gdb-patches
2022-08-29  4:41 ` [PATCH v3 0/4] gdb, btrace: infrun fixes Metzger, Markus T via Gdb-patches

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