Watching expressions that don't involve memory (e.g., watch $regfoo)

Pedro Alves pedro@codesourcery.com
Thu Mar 4 15:39:00 GMT 2010


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"



More information about the Gdb-patches mailing list