Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Mohamed Bouhaouel <mohamed.bouhaouel@intel.com>
Cc: gdb-patches@sourceware.org, kevinb@redhat.com,
	guinevere@redhat.com, abdul.b.ijaz@intel.com
Subject: Re: [PATCH v2 1/1] gdb, infcall, breakpoint: Handle cycles in conditional expr evaluation
Date: Fri, 20 Nov 2026 15:45:39 +0200	[thread overview]
Message-ID: <86a4n3k458.fsf@gnu.org> (raw)
In-Reply-To: <20261120132155.430400-1-mohamed.bouhaouel@intel.com> (message from Mohamed Bouhaouel on Fri, 20 Nov 2026 14:21:55 +0100)

> 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>

      reply	other threads:[~2026-11-20 13:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 13:21 Mohamed Bouhaouel
2026-11-20 13:45 ` Eli Zaretskii [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86a4n3k458.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=abdul.b.ijaz@intel.com \
    --cc=gdb-patches@sourceware.org \
    --cc=guinevere@redhat.com \
    --cc=kevinb@redhat.com \
    --cc=mohamed.bouhaouel@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox