From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 3BE163858D1E for ; Sat, 1 Oct 2022 05:29:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3BE163858D1E Received: by mail-wr1-x42e.google.com with SMTP id s14so9692368wro.0 for ; Fri, 30 Sep 2022 22:29:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dVIqSmDQ4Lhe2xXG+lLivAg4R6W/0AEr9QOqtOrjcVc=; b=0ORe+Vd2JYcOZ95/BrCtDsptRSp5E8P/dd9QT7O6+ic86tCAZmjFiYUsFShOqXhQf+ RF5zTN2UjdzyFyahPTZtwvlUtiheCMEsDH3znjOmcSGG0+bErs65p3H2b5Goqlzv7DY3 nhcA75XGXTTdEk9Ftu0YE30KAyOY0azLDi85BuCa0ttQB5X2LsrdOe47g52EV2R3gTvb CjG4ELtgTHtHz3wKcCO7PxLAxgmVdtWUbFdQ0tHqtkQPR0pCzcTFZSkVz254wo8g306E waBwOVJYRcZvU6G7WFZ4BXeXTCd6TMpk7Y4GYYV3sb2ziLIxfkAWzUll/xtsFr40lX16 vXIA== X-Gm-Message-State: ACrzQf1U/C8l+s/bkmzFObM0f3NlfrLFd3W9SMPUtlLp/vHEA+M+P/Za fMWXq54JzlpFQ0A874AUQnYhz2vP4gcF9isZ X-Google-Smtp-Source: AMsMyM5GZTCvW8ZulmN8CiV7zDBtq3WTX8Y8BuuwZyGk94WCtic/Uj+71AFrbhBhdZdWB2pG5LYeag== X-Received: by 2002:a5d:5c0a:0:b0:228:db53:22cc with SMTP id cc10-20020a5d5c0a000000b00228db5322ccmr7107165wrb.126.1664602175798; Fri, 30 Sep 2022 22:29:35 -0700 (PDT) Received: from ubuntu-22.. (intel10.cs.nthu.edu.tw. [140.114.89.60]) by smtp.gmail.com with ESMTPSA id z15-20020adfdf8f000000b0022cc895cc11sm4002071wrl.104.2022.09.30.22.29.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Sep 2022 22:29:35 -0700 (PDT) From: Johnson Sun To: Lancelot SIX , gdb-patches@sourceware.org Cc: j3.soon777@gmail.com Subject: [PATCH v2] [PR python/29603] Disable out-of-scope watchpoints Date: Sat, 1 Oct 2022 13:27:11 +0800 Message-Id: <20221001052708.2531176-1-j3.soon777@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221001052017.2531121-1-j3.soon777@gmail.com> References: <20221001052017.2531121-1-j3.soon777@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 01 Oct 2022 05:29:39 -0000 Currently, when a local software watchpoint goes out of scope, GDB sets the watchpoint's disposition to `delete at next stop' and then normal stops (i.e., stop and wait for the next GDB command). When GDB normal stops, it automatically deletes the breakpoints with disposition set to `delete at next stop'. Suppose a Python script decides not to normal stop when a local software watchpoint goes out of scope, the watchpoint will not be automatically deleted even when its disposition is set to `delete at next stop'. Since GDB single-steps the program and tests the watched expression after each instruction, not deleting the watchpoint causes the watchpoint to be hit many more times than it should, as reported in PR python/29603. This was happening because the watchpoint is not disabled when going out of scope. This commit fixes this issue by disabling the watchpoint when going out of scope. It also adds a test to ensure this feature isn't regressed in the future. There are two other solutions that seem to solve this issue but don't: 1. Automatically delete breakpoints on all kinds of stops (in `fetch_inferior_event'). This solution is very slow since the deletion requires O(N) time for N breakpoints. 2. Disable the watchpoint when the watchpoint's disposition is set to `delete at next stop' (in `watchpoint_del_at_next_stop'). This solution will not trigger a breakpoint stop callback when the watchpoint goes out of scope. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29603 --- gdb/breakpoint.c | 2 + gdb/testsuite/gdb.python/py-watchpoint.c | 27 ++++++++++++ gdb/testsuite/gdb.python/py-watchpoint.exp | 50 ++++++++++++++++++++++ gdb/testsuite/gdb.python/py-watchpoint.py | 30 +++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 gdb/testsuite/gdb.python/py-watchpoint.c create mode 100644 gdb/testsuite/gdb.python/py-watchpoint.exp create mode 100644 gdb/testsuite/gdb.python/py-watchpoint.py diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index bff3bac7d1a..15f4ae2131c 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -5340,6 +5340,8 @@ bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread) /* Evaluate extension language breakpoints that have a "stop" method implemented. */ bs->stop = breakpoint_ext_lang_cond_says_stop (b); + if (b->disposition == disp_del_at_next_stop) + disable_breakpoint(b); if (is_watchpoint (b)) { diff --git a/gdb/testsuite/gdb.python/py-watchpoint.c b/gdb/testsuite/gdb.python/py-watchpoint.c new file mode 100644 index 00000000000..6d02e87e571 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-watchpoint.c @@ -0,0 +1,27 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 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 . */ + +#include + +int +main () +{ + int i; + for (i = 0; i < 3; i++) + printf ("%d", i); + return 0; +} diff --git a/gdb/testsuite/gdb.python/py-watchpoint.exp b/gdb/testsuite/gdb.python/py-watchpoint.exp new file mode 100644 index 00000000000..a14ab139a3f --- /dev/null +++ b/gdb/testsuite/gdb.python/py-watchpoint.exp @@ -0,0 +1,50 @@ +# Copyright (C) 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 . + +# This file is part of the GDB testsuite. It checks Watchpoints +# are deleted after used. + +load_lib gdb-python.exp + +standard_testfile + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} { + return -1 +} + +# Skip all tests if Python scripting is not enabled. +if { [skip_python_tests] } { continue } + +if ![runto_main] then { + return 0 +} + +# +# Check Watchpoints are deleted after used +# + +gdb_test_no_output "set can-use-hw-watchpoints 0" "Don't use hardware watchpoints" +gdb_test "python print(len(gdb.breakpoints()))" "1" "check default BP count" +gdb_test "source ${srcdir}/${subdir}/${testfile}.py" ".*Python script imported.*" \ + "import python scripts" +gdb_test "python print(len(gdb.breakpoints()))" "2" "check modified BP count" +gdb_test_sequence "continue" "run until program stops" { + "Watchpoint Hit: ." + "\[\r\n\]+Watchpoint . deleted because the program has left the block in" + "\[\r\n\]+which its expression is valid\." + "Watchpoint Hit: ." + "\[\r\n\]+012\\[Inferior 1 \\(process .*\\) exited normally\\]" +} +gdb_test "python print(len(gdb.breakpoints()))" "1" "check BP count" diff --git a/gdb/testsuite/gdb.python/py-watchpoint.py b/gdb/testsuite/gdb.python/py-watchpoint.py new file mode 100644 index 00000000000..2c668aeca77 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-watchpoint.py @@ -0,0 +1,30 @@ +# Copyright (C) 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 . + +# This file is part of the GDB testsuite. It checks Watchpoints +# are deleted after used. + +class MyBreakpoint(gdb.Breakpoint): + def __init__(self, *args, **kwargs): + gdb.Breakpoint.__init__(self, *args, **kwargs) + self.i = 0 + def stop(self): + self.i += 1 + print("Watchpoint Hit:", self.i) + return False + +MyBreakpoint('i', gdb.BP_WATCHPOINT) + +print("Python script imported") -- 2.34.1 base-commit: c99b2113a478a075e8d8f7c3848a92f1ce73f847