Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH v2 1/1] gdb, infcall, breakpoint: Handle cycles in conditional expr evaluation
@ 2026-11-20 13:21 Mohamed Bouhaouel
  2026-11-20 13:45 ` Eli Zaretskii
  0 siblings, 1 reply; 2+ messages in thread
From: Mohamed Bouhaouel @ 2026-11-20 13:21 UTC (permalink / raw)
  To: gdb-patches; +Cc: kevinb, guinevere, abdul.b.ijaz, mohamed.bouhaouel

Take the following scenario:

  (gdb) break foo if foo (1) == 2
  (gdb) run

The program stops at 'foo', then GDB tries to evaluate the condition.
The condition triggers an inferior call of 'foo', which causes a stop at
'foo' again, and GDB is stuck in this infinite cycle.

The same issue exists also for indirect cycles, e.g.,

  (gdb) break foo if bar (1)
  (gdb) break bar if foo (1)
  (gdb) run

To avoid this, check for cycles just before evaluating a breakpoint
condition.  If a breakpoint is hit again while its own condition is
being evaluated, the evaluation of that condition is aborted with an
error message.

Since this also rejects recursion that terminates, e.g.,

  (gdb) break factorial if value > 1 && factorial (value - 1) > 100

add a new setting, "set breakpoint condition-cycle-detection on|off"
(on by default), which allows turning the check off.

With the patch, the behavior becomes:

  (gdb) break foo if foo (1) == 2
  Breakpoint 2 at 0x55555555514c: file infcall-from-bp-cond-cycle.c, line 27.
  (gdb) continue
  Continuing.
  Error in testing condition for breakpoint 2:
  Aborting evaluation of the breakpoint condition:
  A cycle was detected during the evaluation.
  To change this behavior use "set breakpoint condition-cycle-detection off".

  Breakpoint 2, foo (a=1) at infcall-from-bp-cond-cycle.c:27
  27      return a + add_one (a);
  Error in testing condition for breakpoint 2:
  The program being debugged stopped while in a function called from GDB.
  Evaluation of the expression containing the function
  (foo) will be abandoned.
  When the function is done executing, GDB will silently stop.
  (gdb) backtrace
  #0  foo (a=1) at infcall-from-bp-cond-cycle.c:27
  #1  <function called from gdb>
  #2  foo (a=11) at infcall-from-bp-cond-cycle.c:27
  #3  0x00005555555551df in main () at infcall-from-bp-cond-cycle.c:55

Tested-By: Guinevere Larsen <guinevere@redhat.com>
---
Changes in v2:
- Add "set breakpoint condition-cycle-detection on|off" (on by
  default) to allow recursion through a breakpoint condition that
  terminates, as in the factorial example from the v1 review.
- Use "breakpoint" instead of "bp" in the error message, and mention
  the new setting in it.
- Add NEWS and documentation entries.
- Extend the test with the factorial case.

 gdb/NEWS                                      |  8 ++
 gdb/breakpoint.c                              | 44 ++++++++-
 gdb/doc/gdb.texinfo                           | 19 ++++
 gdb/gdbthread.h                               | 10 ++-
 gdb/infcall.c                                 |  2 +-
 gdb/infrun.c                                  |  6 +-
 .../gdb.base/infcall-from-bp-cond-cycle.c     | 60 +++++++++++++
 .../gdb.base/infcall-from-bp-cond-cycle.exp   | 89 +++++++++++++++++++
 8 files changed, 231 insertions(+), 7 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.c
 create mode 100644 gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.exp

diff --git a/gdb/NEWS b/gdb/NEWS
index db3fa820df1..b38eaee5c09 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -67,6 +67,14 @@ maintenance info source-tracking-context BPNUM
   Display the captured source context that GDB uses to track and
   adjust a breakpoint when source changes.
 
+set breakpoint condition-cycle-detection on|off
+show breakpoint condition-cycle-detection
+  When on (the default), GDB aborts the evaluation of a breakpoint
+  condition that is reentered, e.g. because the condition calls a
+  function that hits the same breakpoint again, to avoid an infinite
+  cycle.  When off, the evaluation continues, which allows recursion
+  through a breakpoint condition.
+
 * Changed commands
 
 set stop-on-solib-events on|off
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 0f173fa9fe6..87d90863915 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -902,6 +902,21 @@ static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
    condition_evaluation_mode_1.  */
 static const char *condition_evaluation_mode = condition_evaluation_auto;
 
+/* If true, abort evaluating a breakpoint condition that is already being
+   evaluated.  */
+static bool bp_cond_cycle_detection = true;
+
+/* Implement "show breakpoint condition-cycle-detection".  */
+
+static void
+show_bp_cond_cycle_detection (struct ui_file *file, int from_tty,
+			      struct cmd_list_element *c, const char *value)
+{
+  gdb_printf (file,
+	      _("Detection of cycles in breakpoint conditions is %s.\n"),
+	      value);
+}
+
 /* Translate a condition evaluation mode MODE into either "host"
    or "target".  This is used mostly to translate from "auto" to the
    real setting that is being used.  It returns the translated
@@ -6047,8 +6062,20 @@ bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
 	{
 	  try
 	    {
-	      scoped_restore reset_in_cond_eval
-		= make_scoped_restore (&thread->control.in_cond_eval, true);
+	      /* Check if we are already evaluating the same condition.  This
+		 may lead to an infinite cycle.  */
+	      std::vector<breakpoint *> &cond_stack
+		= thread->control.cond_eval_bps;
+	      if (bp_cond_cycle_detection
+		  && (std::find (cond_stack.begin (), cond_stack.end (), b)
+		      != cond_stack.end ()))
+		error (_("Aborting evaluation of the breakpoint condition:\n"
+			 "A cycle was detected during the evaluation.\n"
+			 "To change this behavior use \"set breakpoint "
+			 "condition-cycle-detection off\"."));
+
+	      cond_stack.push_back (b);
+	      SCOPE_EXIT { cond_stack.pop_back (); };
 	      condition_result = breakpoint_cond_eval (cond);
 	    }
 	  catch (const gdb_exception_error &ex)
@@ -15719,6 +15746,19 @@ be set to \"host\"."),
 			   &breakpoint_set_cmdlist,
 			   &breakpoint_show_cmdlist);
 
+  add_setshow_boolean_cmd ("condition-cycle-detection", class_breakpoint,
+			   &bp_cond_cycle_detection, _("\
+Set whether cycles in breakpoint conditions are detected."), _("\
+Show whether cycles in breakpoint conditions are detected."), _("\
+A breakpoint condition may call a function which hits the same breakpoint\n\
+again, possibly leading to an infinite cycle.  When on (the default), the\n\
+evaluation of a breakpoint condition which is reentered is aborted.  When\n\
+off, the evaluation continues, which allows recursion that terminates."),
+			   nullptr,
+			   show_bp_cond_cycle_detection,
+			   &breakpoint_set_cmdlist,
+			   &breakpoint_show_cmdlist);
+
   add_com ("break-range", class_breakpoint, break_range_command, _("\
 Set a breakpoint for an address range.\n\
 break-range START-LOCATION, END-LOCATION\n\
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 7998dfb58c8..159f0ecf965 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -6101,6 +6101,25 @@ conditions for the
 purpose of performing side effects when a breakpoint is reached
 (@pxref{Break Commands, ,Breakpoint Command Lists}).
 
+A function called from a breakpoint condition may itself hit the same
+breakpoint, so that the condition is evaluated again before the outer
+evaluation completes.  By default, @value{GDBN} considers this a cycle
+and aborts the evaluation, as it would otherwise likely never end.
+
+@table @code
+@kindex set breakpoint condition-cycle-detection
+@item set breakpoint condition-cycle-detection on
+@itemx set breakpoint condition-cycle-detection off
+When @code{on} (the default), @value{GDBN} reports an error when the
+evaluation of a breakpoint condition is reentered.  When @code{off},
+the evaluation continues, which allows a condition to recurse through
+the same breakpoint.
+
+@kindex show breakpoint condition-cycle-detection
+@item show breakpoint condition-cycle-detection
+Show whether cycles in breakpoint conditions are detected.
+@end table
+
 Breakpoint conditions can also be evaluated on the target's side if
 the target supports it.  Instead of evaluating the conditions locally,
 @value{GDBN} encodes the expression into an agent expression
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 5561ec22b42..31af968f767 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -234,8 +234,16 @@ struct thread_control_state
      step" behaves like "on" or "off".  */
   int stepping_command = 0;
 
+  /* A list of conditional breakpoints (if any) that the thread is evaluating.
+     This list is tracking breakpoint hits during expression evaluation to make
+     sure we don't run into cycles.  */
+  std::vector<breakpoint *> cond_eval_bps;
+
   /* True if the thread is evaluating a BP condition.  */
-  bool in_cond_eval = false;
+  bool in_cond_eval () const
+  {
+    return !cond_eval_bps.empty ();
+  }
 
 private:
   /* Function the thread was in as of last it started stepping.  */
diff --git a/gdb/infcall.c b/gdb/infcall.c
index 5f27f5ad259..b5441c44d89 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -865,7 +865,7 @@ run_inferior_call (std::unique_ptr<call_thread_fsm> sm,
 
       /* Setup a timer (if possible, and if the settings allow) to prevent
 	 the inferior call running forever.  */
-      bool direct_call_p = !call_thread->control.in_cond_eval;
+      bool direct_call_p = !call_thread->control.in_cond_eval ();
       infcall_timer_controller infcall_timer (call_thread, direct_call_p);
 
       /* Inferior function calls are always synchronous, even if the
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 08fac30e9e8..03731626fa0 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -2458,7 +2458,7 @@ user_visible_resume_ptid (int step)
       resume_ptid = inferior_ptid;
     }
   else if (inferior_ptid != null_ptid
-	   && inferior_thread ()->control.in_cond_eval)
+	   && inferior_thread ()->control.in_cond_eval ())
     {
       /* The inferior thread is evaluating a BP condition.  Other threads
 	 might be stopped or running and we do not want to change their
@@ -4711,7 +4711,7 @@ fetch_inferior_event ()
     /* Is the current thread performing an inferior function call as part
        of a breakpoint condition evaluation?  */
     bool in_cond_eval = (inferior_ptid != null_ptid
-			 && inferior_thread ()->control.in_cond_eval);
+			 && inferior_thread ()->control.in_cond_eval ());
 
     /* If the thread is in the middle of the condition evaluation, wait for
        an event from the current thread.  Otherwise, wait for an event from
@@ -4780,7 +4780,7 @@ fetch_inferior_event ()
 	       function call as part of a condition evaluation, then we
 	       don't want to stop all the other threads.  */
 	    if (ecs.event_thread == nullptr
-		|| !ecs.event_thread->control.in_cond_eval)
+		|| !ecs.event_thread->control.in_cond_eval ())
 	      {
 		stop_all_threads_if_all_stop_mode ();
 
diff --git a/gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.c b/gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.c
new file mode 100644
index 00000000000..c4e05c68857
--- /dev/null
+++ b/gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.c
@@ -0,0 +1,60 @@
+/* Copyright 2025-2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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/>.  */
+
+int
+add_one (int a)
+{
+  return ++a; /* cond-bp-line.  */
+}
+
+int
+foo (int a)
+{
+  return a + add_one (a);
+}
+
+int
+bar (int a)
+{
+  return a + 100;
+}
+
+int
+baz (int a)
+{
+  return --a;
+}
+
+int
+factorial (int value)
+{
+  if (value > 1)
+    value *= factorial (value - 1);
+  return value;
+}
+
+int
+main ()
+{
+  int a = 10;
+  int b = add_one (a);
+  b += foo (b);
+  b = bar (b) + baz (b);
+  b = factorial (6);
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.exp b/gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.exp
new file mode 100644
index 00000000000..7fa71a41ddf
--- /dev/null
+++ b/gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.exp
@@ -0,0 +1,89 @@
+# Copyright 2025-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/>.
+#
+# Test that GDB is able to detect a cycle when evaluating a conditional
+# breakpoint.  In such a case, GDB should report the cycle and abort the
+# evaluation, unless cycle detection is turned off.
+
+require {!target_info exists gdb,cannot_call_functions}
+
+standard_testfile .c
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
+    return -1
+}
+
+set bp_line [gdb_get_line_number "cond-bp-line"]
+set conditional_bps [dict create \
+    direct-line [list "$bp_line if foo (1) == 2"] \
+    direct-func [list "foo if foo (1) == 2"] \
+    indirect-2 [list "foo if bar (1)" \
+		     "bar if foo (1)"] \
+    indirect-3 [list "bar if baz (1)" \
+		     "baz if add_one (1)" \
+		     "add_one if bar (1)"]]
+
+foreach_with_prefix name [dict keys $conditional_bps] {
+    set bps [dict get $conditional_bps $name]
+    clean_restart $testfile
+    if {![runto_main]} {
+	return
+    }
+    foreach bp $bps {
+	gdb_breakpoint $bp
+    }
+    gdb_test "continue" "A cycle was detected during the evaluation.*"
+
+    delete_breakpoints
+    set keep_going 1
+    set itr 0
+    while {$keep_going && $itr < 10} {
+	set keep_going 0
+	gdb_test_multiple "backtrace" "check dummy frames: itr $itr" -lbl {
+	    -re "\r\n#$::decimal\\s+<function called from gdb>(?=\r\n)" {
+		set keep_going 1
+		exp_continue
+	    }
+	    -re -wrap "" {
+		pass $gdb_test_name
+	    }
+	}
+	if {$keep_going} {
+	    gdb_test "continue" "Continuing\\." "skip dummy frame: itr $itr"
+	}
+	incr itr
+    }
+    gdb_assert {!$keep_going} "no dummy frames left"
+
+    gdb_continue_to_end
+}
+
+# Breakpoint condition recursion is treated as a cycle.  With cycle
+# detection disabled, finite recursion works as expected.
+foreach_with_prefix detection {on off} {
+    clean_restart $testfile
+    if {![runto_main]} {
+	return
+    }
+    gdb_test_no_output "set breakpoint condition-cycle-detection $detection"
+    gdb_test "show breakpoint condition-cycle-detection" \
+	"Detection of cycles in breakpoint conditions is $detection\\."
+    gdb_breakpoint "factorial if value > 1 && factorial (value - 1) > 100"
+    if {$detection eq "on"} {
+	gdb_test "continue" "A cycle was detected during the evaluation.*"
+    } else {
+	gdb_test "continue" "Breakpoint $decimal, factorial \\(value=6\\).*"
+    }
+}
-- 
2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


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

* Re: [PATCH v2 1/1] gdb, infcall, breakpoint: Handle cycles in conditional expr evaluation
  2026-11-20 13:21 [PATCH v2 1/1] gdb, infcall, breakpoint: Handle cycles in conditional expr evaluation Mohamed Bouhaouel
@ 2026-11-20 13:45 ` Eli Zaretskii
  0 siblings, 0 replies; 2+ messages in thread
From: Eli Zaretskii @ 2026-11-20 13:45 UTC (permalink / raw)
  To: Mohamed Bouhaouel; +Cc: gdb-patches, kevinb, guinevere, abdul.b.ijaz

> From: Mohamed Bouhaouel <mohamed.bouhaouel@intel.com>
> Cc: kevinb@redhat.com, guinevere@redhat.com, abdul.b.ijaz@intel.com,
>  mohamed.bouhaouel@intel.com
> Date: Fri, 20 Nov 2026 14:21:55 +0100
> 
> Take the following scenario:
> 
>   (gdb) break foo if foo (1) == 2
>   (gdb) run
> 
> The program stops at 'foo', then GDB tries to evaluate the condition.
> The condition triggers an inferior call of 'foo', which causes a stop at
> 'foo' again, and GDB is stuck in this infinite cycle.
> 
> The same issue exists also for indirect cycles, e.g.,
> 
>   (gdb) break foo if bar (1)
>   (gdb) break bar if foo (1)
>   (gdb) run
> 
> To avoid this, check for cycles just before evaluating a breakpoint
> condition.  If a breakpoint is hit again while its own condition is
> being evaluated, the evaluation of that condition is aborted with an
> error message.
> 
> Since this also rejects recursion that terminates, e.g.,
> 
>   (gdb) break factorial if value > 1 && factorial (value - 1) > 100
> 
> add a new setting, "set breakpoint condition-cycle-detection on|off"
> (on by default), which allows turning the check off.
> 
> With the patch, the behavior becomes:
> 
>   (gdb) break foo if foo (1) == 2
>   Breakpoint 2 at 0x55555555514c: file infcall-from-bp-cond-cycle.c, line 27.
>   (gdb) continue
>   Continuing.
>   Error in testing condition for breakpoint 2:
>   Aborting evaluation of the breakpoint condition:
>   A cycle was detected during the evaluation.
>   To change this behavior use "set breakpoint condition-cycle-detection off".
> 
>   Breakpoint 2, foo (a=1) at infcall-from-bp-cond-cycle.c:27
>   27      return a + add_one (a);
>   Error in testing condition for breakpoint 2:
>   The program being debugged stopped while in a function called from GDB.
>   Evaluation of the expression containing the function
>   (foo) will be abandoned.
>   When the function is done executing, GDB will silently stop.
>   (gdb) backtrace
>   #0  foo (a=1) at infcall-from-bp-cond-cycle.c:27
>   #1  <function called from gdb>
>   #2  foo (a=11) at infcall-from-bp-cond-cycle.c:27
>   #3  0x00005555555551df in main () at infcall-from-bp-cond-cycle.c:55
> 
> Tested-By: Guinevere Larsen <guinevere@redhat.com>
> ---
> Changes in v2:
> - Add "set breakpoint condition-cycle-detection on|off" (on by
>   default) to allow recursion through a breakpoint condition that
>   terminates, as in the factorial example from the v1 review.
> - Use "breakpoint" instead of "bp" in the error message, and mention
>   the new setting in it.
> - Add NEWS and documentation entries.
> - Extend the test with the factorial case.
> 
>  gdb/NEWS                                      |  8 ++
>  gdb/breakpoint.c                              | 44 ++++++++-
>  gdb/doc/gdb.texinfo                           | 19 ++++
>  gdb/gdbthread.h                               | 10 ++-
>  gdb/infcall.c                                 |  2 +-
>  gdb/infrun.c                                  |  6 +-
>  .../gdb.base/infcall-from-bp-cond-cycle.c     | 60 +++++++++++++
>  .../gdb.base/infcall-from-bp-cond-cycle.exp   | 89 +++++++++++++++++++
>  8 files changed, 231 insertions(+), 7 deletions(-)
>  create mode 100644 gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.c
>  create mode 100644 gdb/testsuite/gdb.base/infcall-from-bp-cond-cycle.exp

Thanks.

> diff --git a/gdb/NEWS b/gdb/NEWS
> index db3fa820df1..b38eaee5c09 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -67,6 +67,14 @@ maintenance info source-tracking-context BPNUM
>    Display the captured source context that GDB uses to track and
>    adjust a breakpoint when source changes.
>  
> +set breakpoint condition-cycle-detection on|off
> +show breakpoint condition-cycle-detection
> +  When on (the default), GDB aborts the evaluation of a breakpoint
> +  condition that is reentered, e.g. because the condition calls a
> +  function that hits the same breakpoint again, to avoid an infinite
> +  cycle.  When off, the evaluation continues, which allows recursion
> +  through a breakpoint condition.

I think the name of the option could be made more meaningful: this is
not about "detection", this is about aborting evaluation of breakpoint
condition.  So I would suggest "set breakpoint allow-cycles-in-condition"
or something similar (yes, that reverts the meaning).

> +  add_setshow_boolean_cmd ("condition-cycle-detection", class_breakpoint,
> +			   &bp_cond_cycle_detection, _("\
> +Set whether cycles in breakpoint conditions are detected."), _("\
                                                   ^^^^^^^^
Not "detected", but "allowed".  Even if we keep the name of the
option, the doc string should be amended to be more accurate.

> +A function called from a breakpoint condition may itself hit the same
> +breakpoint, so that the condition is evaluated again before the outer
> +evaluation completes.  By default, @value{GDBN} considers this a cycle
> +and aborts the evaluation, as it would otherwise likely never end.

I would add to the paragraph above something like

  You can control this behavior using the following option.

It adds continuity to the manual.

The documentation parts are okay with these nits fixed.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>

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

end of thread, other threads:[~2026-11-20 13:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 13:21 [PATCH v2 1/1] gdb, infcall, breakpoint: Handle cycles in conditional expr evaluation Mohamed Bouhaouel
2026-11-20 13:45 ` Eli Zaretskii

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