public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [RFC][gdb/cli] Add convenience vars _wp_old_val and _wp_val
Date: Fri, 30 Sep 2022 11:16:16 +0200	[thread overview]
Message-ID: <20220930091614.GA30107@delia.home> (raw)

Hi,

Add convenience variables _wp_old_val and _wp_val, that match the reported
values of a stopped watchpoint:
...
Hardware watchpoint 2: v

Old value = 3
New value = 2
main () at watchpoint-convenience-vars.c:12
12        return 0;
(gdb) print $_wp_old_val
$1 = 3
(gdb) print $_wp_val
$2 = 2
...

These can be used in a watchpoint condition, for instance to only stop when
changing from one value to another value:
...
(gdb) cond 2 $_wp_old_val == 3 && $_wp_val == 2
...

RFC for now, so no docs yet.

What about naming?  Maybe _wp_old_value and _wp_new_value are more intuitive
because they match the "Old value = _/ New value = _" message?

Tested on x86_64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29480

Any comments?

Thanks,
- Tom

[gdb/cli] Add convenience vars _wp_old_val and _wp_val

---
 gdb/breakpoint.c                                   |  4 +++
 .../gdb.base/watchpoint-convenience-vars.c         | 30 ++++++++++++++++
 .../gdb.base/watchpoint-convenience-vars.exp       | 40 ++++++++++++++++++++++
 3 files changed, 74 insertions(+)

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 002f4a935b1..15b5356cb98 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -5060,7 +5060,11 @@ watchpoint_check (bpstat *bs)
 						       new_val)))
 	{
 	  bs->old_val = b->val;
+	  if (bs->old_val.get () != nullptr)
+	    set_internalvar (lookup_internalvar ("_wp_old_val"),
+			     bs->old_val.get ());
 	  b->val = release_value (new_val);
+	  set_internalvar (lookup_internalvar ("_wp_val"), b->val.get ());
 	  b->val_valid = true;
 	  if (new_val != NULL)
 	    value_free_to_mark (mark);
diff --git a/gdb/testsuite/gdb.base/watchpoint-convenience-vars.c b/gdb/testsuite/gdb.base/watchpoint-convenience-vars.c
new file mode 100644
index 00000000000..8681fedb1d9
--- /dev/null
+++ b/gdb/testsuite/gdb.base/watchpoint-convenience-vars.c
@@ -0,0 +1,30 @@
+/* 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 <http://www.gnu.org/licenses/>.  */
+
+int v = 0;
+
+int
+main (void)
+{
+  v = 1;
+  v = 2;
+
+  v = 3;
+  v = 2;
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/watchpoint-convenience-vars.exp b/gdb/testsuite/gdb.base/watchpoint-convenience-vars.exp
new file mode 100644
index 00000000000..b5d1ed619f8
--- /dev/null
+++ b/gdb/testsuite/gdb.base/watchpoint-convenience-vars.exp
@@ -0,0 +1,40 @@
+# 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 <http://www.gnu.org/licenses/>.
+
+# Check the watchpoint convenience variables _wp_old_val and _wp_val.
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+# Set a watchpoint.
+gdb_test "watch v" "\[Ww\]atchpoint 2: v"
+
+# Make the watchpoint conditional on a specific value transition, using
+# watchpoint convenience variables.
+gdb_test_no_output "cond 2 \$_wp_old_val == 3 && \$_wp_val == 2"
+
+# Verify that the watchpoint triggers on the value transition.
+gdb_test "continue" "Old value = 3\r\nNew value = 2\r\n.*"
+
+# Verify the value of the convenience variables.
+gdb_test "print \$_wp_old_val" " = 3"
+gdb_test "print \$_wp_val" " = 2"

             reply	other threads:[~2022-09-30  9:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-30  9:16 Tom de Vries [this message]
2022-09-30 15:37 ` Pedro Alves
2022-10-07 19:41   ` Tom Tromey
2022-10-08  6:11     ` Tom de Vries
2022-10-10 13:49     ` Pedro Alves

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=20220930091614.GA30107@delia.home \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).