public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "vries at gcc dot gnu.org" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug breakpoints/29480] New: [gdb] watchpoint condition on specific value change
Date: Fri, 12 Aug 2022 11:42:59 +0000	[thread overview]
Message-ID: <bug-29480-4717@http.sourceware.org/bugzilla/> (raw)

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

            Bug ID: 29480
           Summary: [gdb] watchpoint condition on specific value change
           Product: gdb
           Version: HEAD
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: breakpoints
          Assignee: unassigned at sourceware dot org
          Reporter: vries at gcc dot gnu.org
  Target Milestone: ---

Somebody asked on IRC:
...
is it possible to setup a watchpoint that fires off when the value of a memory
location changes from one specific value to another?
...

I wrote this test example:
...
$ cat test.c
int v = 0;

int
main (void)
{
  v = 1;
  v = 2;

  v = 3;
  v = 2;

  return 0;
}
$ gcc -g test.c
...
and decided I wanted to set a watchpoint on the change from 3 to 2.

I suppose it's possible to do this in python somehow, but I decided to try
something more simple.

I wrote this patch that introduces two new convenience variables:
...
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 37f70a77721..90ec605aeeb 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -5062,7 +5062,10 @@ watchpoint_check (bpstat *bs)
                                                       new_val)))
        {
          bs->old_val = b->val;
+         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);
...
and managed to do:
...
$ gdb -q -batch a.out -ex "watch v" -ex "cond 1 \$_wp_old_val == 3 && \$_wp_val
== 2" -ex run
Hardware watchpoint 1: v

Hardware watchpoint 1: v

Old value = 3
New value = 2
main () at test.c:12
12        return 0;
...

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2022-08-12 11:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-12 11:42 vries at gcc dot gnu.org [this message]
2022-08-12 17:02 ` [Bug breakpoints/29480] " tromey at sourceware dot org
2022-08-12 17:35 ` vries at gcc dot gnu.org
2022-09-30  9:16 ` vries at gcc dot gnu.org

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=bug-29480-4717@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@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).