From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 2DDB23858D1E for ; Sun, 25 Sep 2022 05:35:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2DDB23858D1E Received: by mail-wm1-x32f.google.com with SMTP id n35-20020a05600c502300b003b4924c6868so6667821wmr.1 for ; Sat, 24 Sep 2022 22:35:02 -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:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=72xJz60vWCBMd27aS9jS/OXW3mmcNoTK1T9fN2+Rvpw=; b=UYu7zTV9RDg0j1coiQM1hXxzyagZHECjeSKVFyERYxJIFy4nbf9pJCL6z812zJKx5d 5D7QSFBlSnd8vVfXiHrG+gR9El48n1ZebJQ+RFhvKxTdhG4um3rFuwmJC3p21d/m1PHf oTZM7xrddxgZXsaojLpYu3au5RBfTmLYWxVMIbgfOLtDtJRT8kF87HrV8SXRwut+Numi fL8X8YfI6w1CGMsHEp+xy+v1NGPVwIvJVMO9rLW8f2BPQvocoNZcizyqAmBqHq9J+RCr EkcFLLWDMu4cGbgn7QTsQYbOTs7EK7WGyTLF/UX5e8z95ygdvI1Wb22ztrDEhKYkggPp DZOg== X-Gm-Message-State: ACrzQf0QeoFb1hUynUxNyIS8p9VKfc5J1d9t12kEGiCrNFbzkP46mA8M Bq8rlzr05n0AjRb/taerPLHn21iPL2th/acg X-Google-Smtp-Source: AMsMyM6LpDY6QZqlL5WtNgtEfurGV107p3zDpZbpSwkVxpXeIGrIENU5622DSHyTmVNKSSDw3ATLDQ== X-Received: by 2002:a05:600c:3555:b0:3b4:c0fd:918e with SMTP id i21-20020a05600c355500b003b4c0fd918emr10507645wmq.61.1664084100723; Sat, 24 Sep 2022 22:35:00 -0700 (PDT) Received: from ubuntu-22.. (intel10.cs.nthu.edu.tw. [140.114.89.60]) by smtp.gmail.com with ESMTPSA id t126-20020a1c4684000000b003b505d26776sm6938160wma.5.2022.09.24.22.34.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Sep 2022 22:35:00 -0700 (PDT) From: Johnson Sun To: gdb-patches@sourceware.org Cc: j3.soon777@gmail.com Subject: [PATCH] [PR python/29603] Fix deletion of Watchpoints Date: Sun, 25 Sep 2022 13:33:50 +0800 Message-Id: <20220925053349.918439-1-j3.soon777@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.6 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: Sun, 25 Sep 2022 05:35:04 -0000 Currently, during normal stop (i.e., stop and waiting for the next command), GDB automatically deletes local Watchpoints that are out of scope. However, local Watchpoints are not deleted if a Python script decides not to normal stop upon hit, causing them to be hit many more times than they 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. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29603 --- gdb/breakpoint.c | 1 + gdb/testsuite/gdb.python/py-watchpoint.c | 27 +++++++++++++ gdb/testsuite/gdb.python/py-watchpoint.exp | 44 ++++++++++++++++++++++ gdb/testsuite/gdb.python/py-watchpoint.py | 31 +++++++++++++++ 4 files changed, 103 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..b78ae9c4993 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -1832,6 +1832,7 @@ watchpoint_del_at_next_stop (struct watchpoint *w) w->related_breakpoint = w; } w->disposition = disp_del_at_next_stop; + disable_breakpoint(w); } /* Extract a bitfield value from value VAL using the bit parameters contained in 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..863f3eff66a --- /dev/null +++ b/gdb/testsuite/gdb.python/py-watchpoint.exp @@ -0,0 +1,44 @@ +# 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 "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 "continue" ".*Watchpoint Hit: 4.*" "run until program stops" +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..855c820b245 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-watchpoint.py @@ -0,0 +1,31 @@ +# 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) + gdb.execute('print i') + return False + +MyBreakpoint('i', gdb.BP_WATCHPOINT) + +print("Python script imported") -- 2.34.1 base-commit: c99b2113a478a075e8d8f7c3848a92f1ce73f847