public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v4] gdb/testsuite: Add gdb.base/memops-watchpoint.exp
@ 2024-04-26  1:46 Thiago Jung Bauermann
  2024-04-26  2:44 ` Kevin Buettner
  0 siblings, 1 reply; 3+ messages in thread
From: Thiago Jung Bauermann @ 2024-04-26  1:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Kevin Buettner, Luis Machado

Test behaviour of watchpoints triggered by libc's memset/memcpy/memmove.
These functions are frequently optimized with specialized instructions
that favor larger memory access operations, so make sure GDB behaves
correctly in their presence.

There's a separate watched variable for each function so that the testcase
can test whether GDB correctly identified the watchpoint that triggered.

Also, the watchpoint is 28 bytes away from the beginning of the buffer
being modified, so that large memory accesses (if present) are exercised.

PR testsuite/31484
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31484
---

Changes in v4:
- Also accept memmove symbol for memcpy watchpoint test (Suggested by Kevin).

Changes in v3:
- Reworked "continue until memset/memcpy/memmove watchpoint hits" tests to
  have a chance to work even without libc debug info (Suggested by Kevin).
- Dropped "require libc6_has_debug_info" call (Suggested by Kevin).

Changes in v2:
- Ensure watchpoints are aligned to 4 bytes.
- Add kfail for arm-linux.

 gdb/testsuite/gdb.base/memops-watchpoint.c   |  45 ++++++
 gdb/testsuite/gdb.base/memops-watchpoint.exp | 161 +++++++++++++++++++
 2 files changed, 206 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/memops-watchpoint.c
 create mode 100644 gdb/testsuite/gdb.base/memops-watchpoint.exp

diff --git a/gdb/testsuite/gdb.base/memops-watchpoint.c b/gdb/testsuite/gdb.base/memops-watchpoint.c
new file mode 100644
index 000000000000..0255cfb43404
--- /dev/null
+++ b/gdb/testsuite/gdb.base/memops-watchpoint.c
@@ -0,0 +1,45 @@
+/* This test program is part of GDB, the GNU debugger.
+
+   Copyright 2024 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/>.  */
+
+#include <stdio.h>
+#include <string.h>
+
+int
+main (void)
+{
+  /* Some targets need 4-byte alignment for hardware watchpoints.  */
+  char s[40] __attribute__ ((aligned (4)))
+    = "This is a relatively long string...";
+  char a[40] __attribute__ ((aligned (4)))
+    = "String to be overwritten with zeroes";
+  char b[40] __attribute__ ((aligned (4)))
+    = "Another string to be memcopied...";
+  char c[40] __attribute__ ((aligned (4)))
+    = "Another string to be memmoved...";
+
+  /* Break here.  */
+  memset (a, 0, sizeof (a));
+
+  memcpy (b, s, sizeof (b));
+
+  memmove (c, s, sizeof (c));
+
+  printf ("b = '%s'\n", b);
+  printf ("c = '%s'\n", c);
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/memops-watchpoint.exp b/gdb/testsuite/gdb.base/memops-watchpoint.exp
new file mode 100644
index 000000000000..cee2d799c67a
--- /dev/null
+++ b/gdb/testsuite/gdb.base/memops-watchpoint.exp
@@ -0,0 +1,161 @@
+# Copyright 2024 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/>.
+
+# Test a binary that uses standard libc memory operation functions.  They are
+# frequently optimized with specialized instructions, so make sure GDB behaves
+# correctly in their presence.
+
+standard_testfile
+set options "-fno-builtin-memset -fno-builtin-memcpy -fno-builtin-memmove"
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
+	  [list debug additional_flags=$options]] } {
+    return -1
+}
+
+set linespec ${srcfile}:[gdb_get_line_number "Break here"]
+if ![runto ${linespec}] {
+    return -1
+}
+
+gdb_test "watch -location a\[28\]" \
+    "(Hardware w|W)atchpoint ${decimal}: -location a\\\[28\\\]" \
+    "set watch on a"
+gdb_test "watch -location b\[28\]" \
+    "(Hardware w|W)atchpoint ${decimal}: -location b\\\[28\\\]" \
+    "set watchpoint on b"
+gdb_test "watch -location c\[28\]" \
+    "(Hardware w|W)atchpoint ${decimal}: -location c\\\[28\\\]" \
+    "set watchpoint on c"
+
+# For the tests below, split the pattern matching in two parts: one for the
+# watchpoint trigger, and another for the line showing the function name.
+# This is to allow the tests to work if there's a properly named symbol for
+# the function, even if there's no libc debug info.
+
+set saw_watch_trigger 0
+set saw_function 0
+set is_supported 1
+set message "continue until memset watchpoint hits"
+set watch_trigger \
+	[multi_line \
+	     "Continuing\\." \
+	     "" \
+	     "(Hardware w|W)atchpoint ${decimal}: -location a\\\[28\\\]" \
+	     "" \
+	     "Old value = 104 'h'" \
+	     "New value = 0 '\\\\000'"]
+gdb_test_multiple "continue" $message {
+    -re $watch_trigger {
+	set saw_watch_trigger 1
+	exp_continue
+    }
+    -re ".*memset.* \\(\\) at .*:$decimal\r\n" {
+	set saw_function 1
+	exp_continue
+    }
+    -re ".*memset.* \\(\\) from .*libc\[^\r\n\]+\r\n" {
+	set saw_function 1
+	exp_continue
+    }
+    -re "in \\?\\? \\(\\) from .*libc\[^\r\n\]+\r\n" {
+	set is_supported 0
+	unsupported "symbol for memset not found"
+	exp_continue
+    }
+    -re "$gdb_prompt $" {
+	if { $is_supported } {
+	    setup_kfail breakpoints/31665 arm*-*-linux*
+	    gdb_assert { $saw_watch_trigger && $saw_function } $message
+	}
+    }
+}
+
+# Note: Some architectures use memmove for memcpy.
+set saw_watch_trigger 0
+set saw_function 0
+set is_supported 1
+set message "continue until memcpy watchpoint hits"
+set watch_trigger \
+	[multi_line \
+	     "Continuing\\." \
+	     "" \
+	     "(Hardware w|W)atchpoint ${decimal}: -location b\\\[28\\\]" \
+	     "" \
+	     "Old value = 101 'e'" \
+	     "New value = 114 'r'"]
+gdb_test_multiple "continue" $message {
+    -re $watch_trigger {
+	set saw_watch_trigger 1
+	exp_continue
+    }
+    -re ".*(memcpy|memmove).* \\(\\) at .*:$decimal\r\n" {
+	set saw_function 1
+	exp_continue
+    }
+    -re ".*(memcpy|memmove).* \\(\\) from .*libc\[^\r\n\]+\r\n" {
+	set saw_function 1
+	exp_continue
+    }
+    -re "in \\?\\? \\(\\) from .*libc\[^\r\n\]+\r\n" {
+	set is_supported 0
+	unsupported "symbol for memcpy not found"
+	exp_continue
+    }
+    -re "$gdb_prompt $" {
+	if { $is_supported } {
+	    setup_kfail breakpoints/31665 arm*-*-linux*
+	    gdb_assert { $saw_watch_trigger && $saw_function } $message
+	}
+    }
+}
+
+# Note: Some architectures use memcpy for memmove.
+set saw_watch_trigger 0
+set saw_function 0
+set is_supported 1
+set message "continue until memmove watchpoint hits"
+set watch_trigger \
+	[multi_line \
+	     "Continuing\\." \
+	     "" \
+	     "(Hardware w|W)atchpoint ${decimal}: -location c\\\[28\\\]" \
+	     "" \
+	     "Old value = 100 'd'" \
+	     "New value = 114 'r'"]
+gdb_test_multiple "continue" $message {
+    -re $watch_trigger {
+	set saw_watch_trigger 1
+	exp_continue
+    }
+    -re ".*(memcpy|memmove).* \\(\\) at .*:$decimal\r\n" {
+	set saw_function 1
+	exp_continue
+    }
+    -re ".*(memcpy|memmove).* \\(\\) from .*libc\[^\r\n\]+\r\n" {
+	set saw_function 1
+	exp_continue
+    }
+    -re "in \\?\\? \\(\\) from .*libc\[^\r\n\]+\r\n" {
+	set is_supported 0
+	unsupported "symbol for memmove not found"
+	exp_continue
+    }
+    -re "$gdb_prompt $" {
+	if { $is_supported } {
+	    setup_kfail breakpoints/31665 arm*-*-linux*
+	    gdb_assert { $saw_watch_trigger && $saw_function } $message
+	}
+    }
+}

base-commit: d3c2603167baf9f6fea006f8b747b8186f89b177

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

* Re: [PATCH v4] gdb/testsuite: Add gdb.base/memops-watchpoint.exp
  2024-04-26  1:46 [PATCH v4] gdb/testsuite: Add gdb.base/memops-watchpoint.exp Thiago Jung Bauermann
@ 2024-04-26  2:44 ` Kevin Buettner
  2024-04-30  3:08   ` Thiago Jung Bauermann
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Buettner @ 2024-04-26  2:44 UTC (permalink / raw)
  To: Thiago Jung Bauermann; +Cc: gdb-patches, Luis Machado

On Thu, 25 Apr 2024 22:46:26 -0300
Thiago Jung Bauermann <thiago.bauermann@linaro.org> wrote:

> Test behaviour of watchpoints triggered by libc's memset/memcpy/memmove.
> These functions are frequently optimized with specialized instructions
> that favor larger memory access operations, so make sure GDB behaves
> correctly in their presence.
> 
> There's a separate watched variable for each function so that the testcase
> can test whether GDB correctly identified the watchpoint that triggered.
> 
> Also, the watchpoint is 28 bytes away from the beginning of the buffer
> being modified, so that large memory accesses (if present) are exercised.
> 
> PR testsuite/31484
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31484
> ---
> 
> Changes in v4:
> - Also accept memmove symbol for memcpy watchpoint test (Suggested by Kevin).
> 
> Changes in v3:
> - Reworked "continue until memset/memcpy/memmove watchpoint hits" tests to
>   have a chance to work even without libc debug info (Suggested by Kevin).
> - Dropped "require libc6_has_debug_info" call (Suggested by Kevin).
> 
> Changes in v2:
> - Ensure watchpoints are aligned to 4 bytes.
> - Add kfail for arm-linux.

LGTM, plus I didn't find any problems while testing on Fedora 39 x86_64,
both with and without debuginfo.

Approved-by: Kevin Buettner <kevinb@redhat.com>


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

* Re: [PATCH v4] gdb/testsuite: Add gdb.base/memops-watchpoint.exp
  2024-04-26  2:44 ` Kevin Buettner
@ 2024-04-30  3:08   ` Thiago Jung Bauermann
  0 siblings, 0 replies; 3+ messages in thread
From: Thiago Jung Bauermann @ 2024-04-30  3:08 UTC (permalink / raw)
  To: Kevin Buettner; +Cc: gdb-patches, Luis Machado

Hello Kevin,

Kevin Buettner <kevinb@redhat.com> writes:

> On Thu, 25 Apr 2024 22:46:26 -0300
> Thiago Jung Bauermann <thiago.bauermann@linaro.org> wrote:
>
>> Test behaviour of watchpoints triggered by libc's memset/memcpy/memmove.
>> These functions are frequently optimized with specialized instructions
>> that favor larger memory access operations, so make sure GDB behaves
>> correctly in their presence.
>> 
>> There's a separate watched variable for each function so that the testcase
>> can test whether GDB correctly identified the watchpoint that triggered.
>> 
>> Also, the watchpoint is 28 bytes away from the beginning of the buffer
>> being modified, so that large memory accesses (if present) are exercised.
>> 
>> PR testsuite/31484
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31484
>> ---
>> 
>> Changes in v4:
>> - Also accept memmove symbol for memcpy watchpoint test (Suggested by Kevin).
>> 
>> Changes in v3:
>> - Reworked "continue until memset/memcpy/memmove watchpoint hits" tests to
>>   have a chance to work even without libc debug info (Suggested by Kevin).
>> - Dropped "require libc6_has_debug_info" call (Suggested by Kevin).
>> 
>> Changes in v2:
>> - Ensure watchpoints are aligned to 4 bytes.
>> - Add kfail for arm-linux.
>
> LGTM, plus I didn't find any problems while testing on Fedora 39 x86_64,
> both with and without debuginfo.

Thanks for testing.

> Approved-by: Kevin Buettner <kevinb@redhat.com>

Thank you! Pushed as commit 6b4b601b35a0 ("gdb/testsuite: Add
gdb.base/memops-watchpoint.exp").

-- 
Thiago

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

end of thread, other threads:[~2024-04-30  3:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-26  1:46 [PATCH v4] gdb/testsuite: Add gdb.base/memops-watchpoint.exp Thiago Jung Bauermann
2024-04-26  2:44 ` Kevin Buettner
2024-04-30  3:08   ` Thiago Jung Bauermann

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).