]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb/testsuite] Improve gdb.base/watchpoint-unaligned.exp
authorTom de Vries <tdevries@suse.de>
Tue, 12 Aug 2025 14:35:05 +0000 (16:35 +0200)
committerTom de Vries <tdevries@suse.de>
Tue, 12 Aug 2025 14:35:05 +0000 (16:35 +0200)
Improve the part of gdb.base/watchpoint-unaligned.exp handling
write_size8twice:
- move the code into a proc
- use -wrap
- use $decimal more often
- don't use gdb_test_multiple $test $test
- add comments
- start test when entering write_size8twice function, instead of main
- finish test when leaving write_size8twice function, instead of main
- add insn in between:
  - insn triggering watchpoint, and
  - insn triggering breakpoint
  to ensure that the watchpoint triggers before the breakpoint, and
  add a comment explaining this.

Tested on x86_64-linux.

gdb/testsuite/gdb.base/watchpoint-unaligned.c
gdb/testsuite/gdb.base/watchpoint-unaligned.exp

index d3c1349805b036216e1542afc3a8545ac84fc645..a3b6b74c3ff9df27cc5e0039cccd6adff2abf5fd 100644 (file)
@@ -18,6 +18,8 @@
 #include <stdint.h>
 #include <assert.h>
 
+static volatile int volatile_dummy;
+
 static int again;
 
 static volatile struct
@@ -53,6 +55,14 @@ write_size8twice (void)
   data.u.size8twice[offset] = first;
   data.u.size8twice[offset + 1] = second;
 #endif
+
+  /* Setting a breakpoint on an instruction after an instruction triggering a
+     watchpoint makes it ambiguous which one will be reported.
+     Insert a dummy instruction in between to make sure the watchpoint gets
+     reported.  */
+  volatile_dummy = 1;
+
+  return; /* write_size8twice_return */
 }
 
 int
index 9220402236d069fec733948d3e4c28d6216cb8b2..ffa574b9dcf757b49c433bbe5b5b6323db986435 100644 (file)
@@ -151,6 +151,63 @@ foreach_with_prefix wpcount {4 7} {
     gdb_assert $got_hit $test
 }
 
+proc size8twice { function offset index } {
+    clean_restart $::testfile
+
+    if { ![runto $function] } {
+       return -1
+    }
+
+    # Set offset in the inferior.
+    gdb_test_no_output "set var offset = $offset"
+
+    # Set a breakpoint.
+    set bp_src_string "${function}_return"
+    set bp_loc [gdb_get_line_number $bp_src_string]
+    gdb_breakpoint $bp_loc \
+       "Breakpoint $::decimal at $::hex" "$bp_src_string"
+
+    # Set a hardware watchpoint.
+    set watch_index [expr $offset + $index]
+    set test "watch data.u.size8twice\[$watch_index\]"
+    set wpnum 0
+    gdb_test_multiple $test "" {
+       -re -wrap "Hardware watchpoint ($::decimal): .*" {
+           set wpnum $expect_out(1,string)
+           pass $gdb_test_name
+       }
+       -re -wrap "Watchpoint ($::decimal): .*" {
+           if {[istarget "arm*-*-*"]} {
+               untested $gdb_test_name
+           } else {
+               fail $gdb_test_name
+           }
+       }
+    }
+
+    if { ! $wpnum } {
+       # No hardware watchpoint, we're done.
+       return 0
+    }
+
+    # Try to trigger the hardware watchpoint.
+    set got_hit 0
+    gdb_test_multiple "continue" "" {
+       -re -wrap "\r\nCould not insert hardware watchpoint .*" {
+       }
+       -re -wrap "Hardware watchpoint $wpnum:.*New value = .*" {
+           set got_hit 1
+           send_gdb "continue\n"
+           exp_continue
+       }
+       -re -wrap " $bp_src_string .*" {
+       }
+    }
+    gdb_assert $got_hit "size8twice write"
+
+    return $got_hit
+}
+
 # We've got an array with 3 8-byte elements.  Do a store of 16 bytes,
 # to:
 # - elements 0 and 1 (offset == 0), and
@@ -160,47 +217,8 @@ foreach_with_prefix wpcount {4 7} {
 # - the second element (index == 1).
 foreach_with_prefix offset { 0 1 } {
     foreach_with_prefix index { 0 1 } {
-
-       clean_restart $binfile
-
-       if ![runto_main] {
-           return -1
-       }
-
-       gdb_test_no_output "set var offset = $offset"
-       gdb_breakpoint [gdb_get_line_number "final_return"] \
-           "Breakpoint $decimal at $hex" "final_return"
-       set watch_index [expr $offset + $index]
-       set test "watch data.u.size8twice\[$watch_index\]"
-       set wpnum 0
-       gdb_test_multiple $test $test {
-           -re "Hardware watchpoint (\[0-9\]+): .*\r\n$gdb_prompt $" {
-               set wpnum $expect_out(1,string)
-               pass $gdb_test_name
-           }
-           -re "Watchpoint (\[0-9\]+): .*\r\n$gdb_prompt $" {
-               if {[istarget "arm*-*-*"]} {
-                   untested $gdb_test_name
-               } else {
-                   fail $gdb_test_name
-               }
-           }
-       }
-       if {$wpnum} {
-           set test "continue"
-           set got_hit 0
-           gdb_test_multiple $test $test {
-               -re "\r\nCould not insert hardware watchpoint .*\r\n$gdb_prompt $" {
-               }
-               -re "Hardware watchpoint $wpnum:.*New value = .*\r\n$gdb_prompt $" {
-                   set got_hit 1
-                   send_gdb "continue\n"
-                   exp_continue
-               }
-               -re " final_return .*\r\n$gdb_prompt $" {
-               }
-           }
-           gdb_assert $got_hit "size8twice write"
+       if { [size8twice write_size8twice $offset $index] != 1 } {
+           return
        }
     }
 }