From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id tPH8DwhKAGtVPyQAWB0awg (envelope-from ) for ; Fri, 20 Nov 2026 08:24:24 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=RqTT3J2d; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 2E0CD1E091; Fri, 20 Nov 2026 08:24:24 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 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_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham 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 D6E0A1E091 for ; Fri, 20 Nov 2026 08:24:20 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 69AB5471326B for ; Fri, 20 Nov 2026 13:24:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69AB5471326B Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=RqTT3J2d Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by sourceware.org (Postfix) with ESMTPS id D815443ADCEF for ; Fri, 20 Nov 2026 13:22:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D815443ADCEF Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D815443ADCEF Authentication-Results: sourceware.org; arc=none smtp.remote-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795180965; cv=none; b=qmavw2M26TZfUXvKI+ZMosifOTWSzfEiGE9GCdVsw3IOocFXtJy5hnaGIDdOibmtmLeTYbgateR9OXB2S/I5ma1mGqZ7GtZLtgCgitcpQ0hUMzBowQgXfy9Ng0HXN/yHGZu4FAIaJ38j3ZPjGtoDF8VIMAUq4LYGWu/KLf3jVtk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795180965; c=relaxed/simple; bh=0KWdHgILMtYa5TE6dG6pz5h2Yk595rnl7jAoDDDVFq4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=r5EHnf23jLnNyBxmJHQtJSa/8dol52c/UYaer6N9qaxj1oC6otfLhycWcz3mOz69/VyiwYLfUxVSNY1KZwy8AAHfjxWtn/sb+5UkGCVR3ULxDsuPeokP4ASqMQmWUPVrgnDytmRdwPClvKC9S78LP6MitVQIYYuxG47ddKCvikU= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=RqTT3J2d DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D815443ADCEF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1795180959; x=1826716959; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=0KWdHgILMtYa5TE6dG6pz5h2Yk595rnl7jAoDDDVFq4=; b=RqTT3J2db0KtRU/dFYeIHwSwiLx/wxknVaRUjdeNt9r+Y+m7Mn14iU5t FYmtQtMRX9GpgZAHj/VpSxLAcvGIKmISjUFydEGUlam8MipfhuWT/hwHI BggL0UuMdkaiLxe/2GGFEN4C2uns4d5YOktWCMjhO9I2E1sZZn02C5fQx +HWu5rK20YWbpTCYaIvL5oQGoh0ho+t/K+kC7hvUdm7xc8cOcDSPPnTRW 51Qsg0nsjeu/dmW0+9N+bZ0fgTrbAWnMwHEDO+BLQq/EkZzreNGECANm3 tsBWCEBakWPaXQOP5gEQLKEUGnNlb6+wgN+z+EhdBqi2cCUhuCvpFfWfm g==; X-CSE-ConnectionGUID: nQ/OdbhFRQCACS12ygR2FA== X-CSE-MsgGUID: 3cBY5bU/R5G2AYvck6hBew== X-IronPort-AV: E=McAfee;i="6800,10657,11971"; a="3974087" X-IronPort-AV: E=Sophos;i="6.27,223,1787036400"; d="scan'208";a="3974087" Received: from fmviesa011.fm.intel.com ([10.60.135.151]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Nov 2026 05:22:38 -0800 X-CSE-ConnectionGUID: WoYX/7HzR/6KjY1ntjWyhg== X-CSE-MsgGUID: jjU6x6MQTR6XcHfWh2YxPw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,223,1787036400"; d="scan'208";a="12537365" Received: from rtrejoca-mobl.ger.corp.intel.com (HELO localhost) ([10.245.114.229]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Nov 2026 05:22:36 -0800 From: Mohamed Bouhaouel To: gdb-patches@sourceware.org Cc: kevinb@redhat.com, guinevere@redhat.com, abdul.b.ijaz@intel.com, mohamed.bouhaouel@intel.com Subject: [PATCH v2 1/1] gdb, infcall, breakpoint: Handle cycles in conditional expr evaluation Date: Fri, 20 Nov 2026 14:21:55 +0100 Message-ID: <20261120132155.430400-1-mohamed.bouhaouel@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 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 #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 --- 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 &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 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 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 . */ + +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 . +# +# 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+(?=\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.