Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* Watching expressions that don't involve memory (e.g., watch $regfoo)
@ 2010-03-04  1:57 Pedro Alves
  2010-03-04  2:05 ` Michael Snyder
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Pedro Alves @ 2010-03-04  1:57 UTC (permalink / raw)
  To: gdb-patches

Anyone else things this is useful?  Here's a 5 minute hack at it.

It allows for e.g.:

(top-gdb) watch $rax
Watchpoint 4: $rax
(top-gdb) c
Continuing.
Watchpoint 4: $rax

Old value = 11802104
New value = 140737488347216
0x0000000000456647 in main (argc=1, argv=0x7fffffffe158) at ../../src/gdb/gdb.c:28
28        memset (&args, 0, sizeof args);
(top-gdb)

Current GDB will successfuly create the watchpoint in the
breakpoint list, but it never triggers, because since the
watchpoint isn't watching any memory, it ends up with
no bp_location associated.


For extra fun, even watching $_siginfo works.

-- 
Pedro Alves

2010-03-04  Pedro Alves  <pedro@codesourcery.com>

	* breakpoint.c (update_watchpoint): Create a sentinel location if
	the software watchpoint isn't watching any memory.
	(breakpoint_address_bits): Skip dummy software watchpoint locations.

	* NEWS: Mention support for watching expressions that don't
	involve memory.

---
 gdb/NEWS         |    6 ++++++
 gdb/breakpoint.c |   20 +++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

Index: src/gdb/breakpoint.c
===================================================================
--- src.orig/gdb/breakpoint.c	2010-03-04 01:17:24.000000000 +0000
+++ src/gdb/breakpoint.c	2010-03-04 01:50:42.000000000 +0000
@@ -1237,6 +1237,19 @@ update_watchpoint (struct breakpoint *b,
 	    value_free (v);
 	}
 
+      /* If a software watchpoint is not watching any memory, then it
+	 will not have any location set up yet.  But,
+	 bpstat_stop_status requires a location to be able to report
+	 stops, so add a sentinel one now.  */
+      if (b->type == bp_watchpoint && b->loc == NULL)
+	{
+	  b->loc = allocate_bp_location (b);
+	  b->loc->pspace = frame_pspace;
+	  b->loc->address = -1;
+	  b->loc->length = -1;
+	  b->loc->watchpoint_type = -1;
+	}
+
       /* We just regenerated the list of breakpoint locations.
          The new location does not have its condition field set to anything
          and therefore, we must always reparse the cond_string, independently
@@ -4516,7 +4529,12 @@ breakpoint_address_bits (struct breakpoi
 
   for (loc = b->loc; loc; loc = loc->next)
     {
-      int addr_bit = gdbarch_addr_bit (loc->gdbarch);
+      int addr_bit;
+
+      if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
+	continue;
+
+      addr_bit = gdbarch_addr_bit (loc->gdbarch);
       if (addr_bit > print_address_bits)
 	print_address_bits = addr_bit;
     }
Index: src/gdb/NEWS
===================================================================
--- src.orig/gdb/NEWS	2010-03-04 01:44:06.000000000 +0000
+++ src/gdb/NEWS	2010-03-04 01:45:37.000000000 +0000
@@ -3,6 +3,12 @@
 
 *** Changes since GDB 7.1
 
+* Watchpoints on expressions not involving memory
+
+  GDB now supports watching expressions that don't involve memory.
+  This allows, for example, watching for register changes.
+  E.g. "watch $pc" will do the right thing.
+
 * X86 general purpose registers
 
   GDB now supports reading/writing byte, word and double-word x86


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Watching expressions that don't involve memory (e.g., watch $regfoo)
  2010-03-04  1:57 Watching expressions that don't involve memory (e.g., watch $regfoo) Pedro Alves
@ 2010-03-04  2:05 ` Michael Snyder
  2010-03-04  2:13   ` Pedro Alves
  2010-03-04  4:12 ` Eli Zaretskii
  2010-03-04  5:30 ` Joel Brobecker
  2 siblings, 1 reply; 9+ messages in thread
From: Michael Snyder @ 2010-03-04  2:05 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

Pedro Alves wrote:
> Anyone else things this is useful?  Here's a 5 minute hack at it.
> 
> It allows for e.g.:
> 
> (top-gdb) watch $rax
> Watchpoint 4: $rax
> (top-gdb) c
> Continuing.
> Watchpoint 4: $rax
> 
> Old value = 11802104
> New value = 140737488347216
> 0x0000000000456647 in main (argc=1, argv=0x7fffffffe158) at ../../src/gdb/gdb.c:28
> 28        memset (&args, 0, sizeof args);
> (top-gdb)
> 
> Current GDB will successfuly create the watchpoint in the
> breakpoint list, but it never triggers, because since the
> watchpoint isn't watching any memory, it ends up with
> no bp_location associated.
> 
> 
> For extra fun, even watching $_siginfo works.

I think watching registers, or expressions involving registers
(such as $sp > 0x10000000) could be incredibly useful.

Expressions on a varobj sound useful to watch too.

One question; what will happen if I watch a constant?



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Watching expressions that don't involve memory (e.g., watch $regfoo)
  2010-03-04  2:05 ` Michael Snyder
@ 2010-03-04  2:13   ` Pedro Alves
  0 siblings, 0 replies; 9+ messages in thread
From: Pedro Alves @ 2010-03-04  2:13 UTC (permalink / raw)
  To: Michael Snyder; +Cc: gdb-patches

On Thursday 04 March 2010 02:05:18, Michael Snyder wrote:

> I think watching registers, or expressions involving registers
> (such as $sp > 0x10000000) could be incredibly useful.

Yeah, that works.

(top-gdb) p $rsp
$1 = (void *) 0x7fffffffe040
(top-gdb) watch $rsp < 0x7fffffffe040
Watchpoint 4: $rsp < 0x7fffffffe040
(top-gdb) c
Continuing.
Watchpoint 4: $rsp < 0x7fffffffe040

Old value = 0
New value = 1
0x0000000000454fd8 in memset@plt ()
(top-gdb) bt
#0  0x0000000000454fd8 in memset@plt ()
#1  0x0000000000456659 in main (argc=1, argv=0x7fffffffe158) at ../../src/gdb/gdb.c:28
(top-gdb)

> Expressions on a varobj sound useful to watch too.

What do you mean by varobj here?  If that's something
that you can "(gdb) print", it should work too.  It sounds
like it opens pathway for some crazy watching :-)

> One question; what will happen if I watch a constant?

The watchpoint never triggers, as is today.

-- 
Pedro Alves


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Watching expressions that don't involve memory (e.g., watch $regfoo)
  2010-03-04  1:57 Watching expressions that don't involve memory (e.g., watch $regfoo) Pedro Alves
  2010-03-04  2:05 ` Michael Snyder
@ 2010-03-04  4:12 ` Eli Zaretskii
  2010-03-04 13:16   ` Pedro Alves
  2010-03-04  5:30 ` Joel Brobecker
  2 siblings, 1 reply; 9+ messages in thread
From: Eli Zaretskii @ 2010-03-04  4:12 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

> From: Pedro Alves <pedro@codesourcery.com>
> Date: Thu, 4 Mar 2010 01:56:44 +0000
> 
> Anyone else things this is useful?

I do.  (Somehow, I thought it actually did work at some point in the
past, but maybe I was dreaming.)

Thanks.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Watching expressions that don't involve memory (e.g., watch  $regfoo)
  2010-03-04  1:57 Watching expressions that don't involve memory (e.g., watch $regfoo) Pedro Alves
  2010-03-04  2:05 ` Michael Snyder
  2010-03-04  4:12 ` Eli Zaretskii
@ 2010-03-04  5:30 ` Joel Brobecker
  2010-03-04  7:40   ` Eli Zaretskii
  2 siblings, 1 reply; 9+ messages in thread
From: Joel Brobecker @ 2010-03-04  5:30 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

> Anyone else things this is useful?  Here's a 5 minute hack at it.
> +* Watchpoints on expressions not involving memory
> +
> +  GDB now supports watching expressions that don't involve memory.
> +  This allows, for example, watching for register changes.
> +  E.g. "watch $pc" will do the right thing.

I think that this might be very useful, indeed. For instance, if
a variable is stored in a register instead of memory.

-- 
Joel


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Watching expressions that don't involve memory (e.g., watch  $regfoo)
  2010-03-04  5:30 ` Joel Brobecker
@ 2010-03-04  7:40   ` Eli Zaretskii
  0 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2010-03-04  7:40 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: pedro, gdb-patches

> Date: Thu, 4 Mar 2010 09:30:17 +0400
> From: Joel Brobecker <brobecker@adacore.com>
> Cc: gdb-patches@sourceware.org
> 
> > Anyone else things this is useful?  Here's a 5 minute hack at it.
> > +* Watchpoints on expressions not involving memory
> > +
> > +  GDB now supports watching expressions that don't involve memory.
> > +  This allows, for example, watching for register changes.
> > +  E.g. "watch $pc" will do the right thing.
> 
> I think that this might be very useful, indeed. For instance, if
> a variable is stored in a register instead of memory.

What would be _really_ nice is if GDB would automatically put a
watchpoint on the register that hold the variable, when the debug info
tells that.  In optimized programs, watching the memory alone will
frequently miss value changes.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Watching expressions that don't involve memory (e.g., watch $regfoo)
  2010-03-04  4:12 ` Eli Zaretskii
@ 2010-03-04 13:16   ` Pedro Alves
  2010-03-04 14:11     ` Eli Zaretskii
  0 siblings, 1 reply; 9+ messages in thread
From: Pedro Alves @ 2010-03-04 13:16 UTC (permalink / raw)
  To: gdb-patches, Eli Zaretskii

On Thursday 04 March 2010 04:12:19, Eli Zaretskii wrote:
> > From: Pedro Alves <pedro@codesourcery.com>
> > Date: Thu, 4 Mar 2010 01:56:44 +0000
> > 
> > Anyone else things this is useful?
> 
> I do.  (Somehow, I thought it actually did work at some point in the
> past, but maybe I was dreaming.)

Indeed, I tried it on 5.3 and 6.0 gdbs, and it worked.  Looking
at the code, I guess it broke with the support for
multi-location breakpoints, in 6.8.  I guess I shall drop the
NEWS entry, as it's a regression fix afterall?
I think I'll add a simple test that watches for "watch $pc",
so that we don't regress again.  That expression should be
portable.

-- 
Pedro Alves


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Watching expressions that don't involve memory (e.g., watch $regfoo)
  2010-03-04 13:16   ` Pedro Alves
@ 2010-03-04 14:11     ` Eli Zaretskii
  2010-03-04 15:39       ` Pedro Alves
  0 siblings, 1 reply; 9+ messages in thread
From: Eli Zaretskii @ 2010-03-04 14:11 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

> From: Pedro Alves <pedro@codesourcery.com>
> Date: Thu, 4 Mar 2010 13:16:29 +0000
> 
> On Thursday 04 March 2010 04:12:19, Eli Zaretskii wrote:
> > > From: Pedro Alves <pedro@codesourcery.com>
> > > Date: Thu, 4 Mar 2010 01:56:44 +0000
> > > 
> > > Anyone else things this is useful?
> > 
> > I do.  (Somehow, I thought it actually did work at some point in the
> > past, but maybe I was dreaming.)
> 
> Indeed, I tried it on 5.3 and 6.0 gdbs, and it worked.  Looking
> at the code, I guess it broke with the support for
> multi-location breakpoints, in 6.8.  I guess I shall drop the
> NEWS entry, as it's a regression fix afterall?

There's nothing wrong with saying "works again", if it was broken for
a long time.  But if it became broken in 6.8, maybe it's not enough
time.

> I think I'll add a simple test that watches for "watch $pc",
> so that we don't regress again.

Yes, thanks.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Watching expressions that don't involve memory (e.g., watch $regfoo)
  2010-03-04 14:11     ` Eli Zaretskii
@ 2010-03-04 15:39       ` Pedro Alves
  0 siblings, 0 replies; 9+ messages in thread
From: Pedro Alves @ 2010-03-04 15:39 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

On Thursday 04 March 2010 14:11:27, Eli Zaretskii wrote:
> > From: Pedro Alves <pedro@codesourcery.com>
> > Date: Thu, 4 Mar 2010 13:16:29 +0000
> > 
> > On Thursday 04 March 2010 04:12:19, Eli Zaretskii wrote:
> > > > From: Pedro Alves <pedro@codesourcery.com>
> > > > Date: Thu, 4 Mar 2010 01:56:44 +0000
> > > > 
> > > > Anyone else things this is useful?
> > > 
> > > I do.  (Somehow, I thought it actually did work at some point in the
> > > past, but maybe I was dreaming.)
> > 
> > Indeed, I tried it on 5.3 and 6.0 gdbs, and it worked.  Looking
> > at the code, I guess it broke with the support for
> > multi-location breakpoints, in 6.8.  I guess I shall drop the
> > NEWS entry, as it's a regression fix afterall?
> 
> There's nothing wrong with saying "works again", if it was broken for
> a long time.  But if it became broken in 6.8, maybe it's not enough
> time.

I agree.

> 
> > I think I'll add a simple test that watches for "watch $pc",
> > so that we don't regress again.
> 
> Yes, thanks.

I've applied this patch below.

-- 
Pedro Alves

2010-03-04  Pedro Alves  <pedro@codesourcery.com>

	gdb/
	* breakpoint.c (update_watchpoint): Create a sentinel location if
	the software watchpoint isn't watching any memory.
	(breakpoint_address_bits): Skip dummy software watchpoint locations.

	gdb/testsuite/
	* gdb.base/watch-non-mem.c, gdb.base/watch-non-mem.exp: New.

---
 gdb/breakpoint.c                         |   22 ++++++++++++++++-
 gdb/testsuite/gdb.base/watch-non-mem.c   |   27 ++++++++++++++++++++
 gdb/testsuite/gdb.base/watch-non-mem.exp |   40 +++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 1 deletion(-)

Index: src/gdb/breakpoint.c
===================================================================
--- src.orig/gdb/breakpoint.c	2010-03-04 14:48:59.000000000 +0000
+++ src/gdb/breakpoint.c	2010-03-04 15:05:29.000000000 +0000
@@ -1237,6 +1237,19 @@ update_watchpoint (struct breakpoint *b,
 	    value_free (v);
 	}
 
+      /* If a software watchpoint is not watching any memory, then the
+	 above left it without any location set up.  But,
+	 bpstat_stop_status requires a location to be able to report
+	 stops, so make sure there's at least a dummy one.  */
+      if (b->type == bp_watchpoint && b->loc == NULL)
+	{
+	  b->loc = allocate_bp_location (b);
+	  b->loc->pspace = frame_pspace;
+	  b->loc->address = -1;
+	  b->loc->length = -1;
+	  b->loc->watchpoint_type = -1;
+	}
+
       /* We just regenerated the list of breakpoint locations.
          The new location does not have its condition field set to anything
          and therefore, we must always reparse the cond_string, independently
@@ -4516,7 +4529,14 @@ breakpoint_address_bits (struct breakpoi
 
   for (loc = b->loc; loc; loc = loc->next)
     {
-      int addr_bit = gdbarch_addr_bit (loc->gdbarch);
+      int addr_bit;
+
+      /* Software watchpoints that aren't watching memory don't have
+	 an address to print.  */
+      if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
+	continue;
+
+      addr_bit = gdbarch_addr_bit (loc->gdbarch);
       if (addr_bit > print_address_bits)
 	print_address_bits = addr_bit;
     }
Index: src/gdb/testsuite/gdb.base/watch-non-mem.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ src/gdb/testsuite/gdb.base/watch-non-mem.c	2010-03-04 15:21:34.000000000 +0000
@@ -0,0 +1,27 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2010 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 global = 0;
+
+int main()
+{
+  global++;
+  global++;
+  global++;
+
+  return 0;
+}
Index: src/gdb/testsuite/gdb.base/watch-non-mem.exp
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ src/gdb/testsuite/gdb.base/watch-non-mem.exp	2010-03-04 15:20:41.000000000 +0000
@@ -0,0 +1,40 @@
+# Copyright 2010 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/>.
+
+#
+# Tests watchpoints that watch expressions that don't involve memory.
+#
+
+set testfile "watch-non-mem"
+set srcfile ${testfile}.c
+set binfile ${objdir}/${subdir}/${testfile}
+
+if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
+    untested ${testfile}.exp
+    return -1
+}
+
+if ![runto_main] then {
+    fail "Can't run to main"
+    return
+}
+
+gdb_test "watch \$pc" \
+    "Watchpoint .*: .pc" \
+    "set write watchpoint on \$pc"
+
+gdb_test "continue" \
+    "Watchpoint 2: .pc.*Old value = .*New value = .*" \
+    "watchpoint on \$pc works"


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-03-04 15:39 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-04  1:57 Watching expressions that don't involve memory (e.g., watch $regfoo) Pedro Alves
2010-03-04  2:05 ` Michael Snyder
2010-03-04  2:13   ` Pedro Alves
2010-03-04  4:12 ` Eli Zaretskii
2010-03-04 13:16   ` Pedro Alves
2010-03-04 14:11     ` Eli Zaretskii
2010-03-04 15:39       ` Pedro Alves
2010-03-04  5:30 ` Joel Brobecker
2010-03-04  7:40   ` Eli Zaretskii

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox