xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.threads/watchthreads.exp (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2004-2023 Free Software Foundation, Inc.
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18# Check that GDB can support multiple watchpoints across threads.
19
20
21# This test verifies that a watchpoint is detected in the proper thread
22# so the test is only meaningful on a system with hardware watchpoints.
23if {[skip_hw_watchpoint_multi_tests]} {
24    return 0
25}
26
27proc target_no_stopped_data { } {
28    return [istarget s390*-*-*]
29}
30
31standard_testfile
32if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
33    return -1
34}
35
36gdb_exit
37gdb_start
38gdb_reinitialize_dir $srcdir/$subdir
39gdb_load ${binfile}
40
41gdb_test_no_output "set can-use-hw-watchpoints 1" ""
42
43#
44# Run to `main' where we begin our tests.
45#
46
47if {![runto_main]} {
48    return 0
49}
50
51set args_0 0
52set args_1 0
53
54# Watch values that will be modified by distinct threads.
55gdb_test "watch args\[0\]" "Hardware watchpoint 2: args\\\[0\\\]"
56gdb_test "watch args\[1\]" "Hardware watchpoint 3: args\\\[1\\\]"
57set hwwp_2_enabled 1
58set hwwp_3_enabled 1
59
60set init_line [gdb_get_line_number "Init value"]
61set inc_line [gdb_get_line_number "Loop increment"]
62set main_loc "main \\\(\\\) at .*watchthreads.c:$init_line"
63set thread0_loc "thread_function \\\(arg=0x0\\\) at .*watchthreads.c:$inc_line"
64set thread1_loc "thread_function \\\(arg=0x1\\\) at .*watchthreads.c:$inc_line"
65
66# Loop and continue to allow both watchpoints to be triggered.
67for {set i 0} {$i < 30} {incr i} {
68  set test_flag_0 0
69  set test_flag_1 0
70  set test_flag 0
71  gdb_test_multiple "continue" "threaded watch loop" {
72    -re "(.*Hardware watchpoint.*)$gdb_prompt $" {
73	# At least one hardware watchpoint was hit.  Check if both were.
74	set string $expect_out(1,string)
75
76	if [regexp "Hardware watchpoint 2: args\\\[0\\\]\[^\r\]*\r\[^\r\]*\r\[^\r\]*Old value = $args_0\[^\r\]*\r\[^\r\]*New value = [expr $args_0+1]\r" $string] {
77	    incr args_0
78	    incr test_flag_0
79	}
80	if [regexp "Hardware watchpoint 3: args\\\[1\\\]\[^\r\]*\r\[^\r\]*\r\[^\r\]*Old value = $args_1\[^\r\]*\r\[^\r\]*New value = [expr $args_1+1]\r" $string] {
81	    incr args_1
82	    incr test_flag_1
83	}
84
85	set expected_loc "bogus location"
86	if { $test_flag_0 == 1 && $test_flag_1 == 0 && $args_0 == 1 } {
87	    set expected_loc $main_loc
88	} elseif { $test_flag_0 == 0 && $test_flag_1 == 1 && $args_1 == 1 } {
89	    set expected_loc $main_loc
90	} elseif { $test_flag_0 == 1 && $test_flag_1 == 0 } {
91	    set expected_loc $thread0_loc
92	} elseif { $test_flag_0 == 0 && $test_flag_1 == 1 } {
93	    set expected_loc $thread1_loc
94	} elseif { $test_flag_0 + $test_flag_1 == 2 } {
95	    # On S/390, or any other system which can not report the
96	    # stopped data address, it is OK to report two watchpoints
97	    # at once in this test.  Make sure the reported location
98	    # corresponds to at least one of the watchpoints (and not,
99	    # e.g., __nptl_create_event).  On other systems, we should
100	    # report the two watchpoints serially.
101	    if { [target_no_stopped_data] } {
102		set expected_loc "($main_loc|$thread0_loc|$thread1_loc)"
103	    }
104	}
105
106	if [ regexp "$expected_loc" $string ] {
107	    set test_flag 1
108	} else {
109	    fail "threaded watch loop"
110	}
111
112	# If one of the watchpoints is disabled, we'd better not stop there.
113	if { !$hwwp_2_enabled && $test_flag_0 } {
114	    fail "disabled hw watchpoint 2 triggered"
115	}
116	if { !$hwwp_3_enabled && $test_flag_1 } {
117	    fail "disabled hw watchpoint 3 triggered"
118	}
119
120	# If we get to 10 in one of the watched locations disable it so we
121	# see some of the other watched location.
122	# 10 is chosen so we're guaranteed to come through here.
123	if { $hwwp_2_enabled && $hwwp_3_enabled } {
124	    if { $args_0 >= 10 && $hwwp_2_enabled } {
125		gdb_test_no_output "disable 2" "disable first watchpoint at 10"
126		set hwwp_2_enabled 0
127	    } elseif { $args_1 >= 10 && $hwwp_3_enabled } {
128		gdb_test_no_output "disable 3" "disable first watchpoint at 10"
129		set hwwp_3_enabled 0
130	    }
131	}
132    }
133  }
134
135  # If we fail above, don't bother continuing loop
136  if { $test_flag == 0 } {
137    set i 30
138  }
139}
140
141# Print success message if loop succeeded.
142if { $test_flag == 1 } {
143  pass "threaded watch loop"
144}
145
146# Verify that we hit first watchpoint in main thread.
147set message "first watchpoint on args\[0\] hit"
148if { $args_0 > 0 } {
149  pass $message
150} else {
151  fail $message
152}
153
154# Verify that we hit second watchpoint in main thread.
155set message "first watchpoint on args\[1\] hit"
156if { $args_1 > 0 } {
157  pass $message
158} else {
159  fail $message
160}
161
162# Verify that we hit first watchpoint in child thread.
163set message "watchpoint on args\[0\] hit in thread"
164if { $args_0 > 1 } {
165  pass $message
166} else {
167  fail $message
168}
169
170# Verify that we hit second watchpoint in child thread.
171set message "watchpoint on args\[1\] hit in thread"
172if { $args_1 > 1 } {
173  pass $message
174} else {
175  fail $message
176}
177
178# Verify that all watchpoint hits are accounted for.
179set message "combination of threaded watchpoints = 30"
180if { [target_no_stopped_data] } {
181    # See above.  If we allow two watchpoints to be hit at once, we
182    # may have more than 30 hits total.
183    set result [expr $args_0 + $args_1 >= 30]
184} else {
185    set result [expr $args_0 + $args_1 == 30]
186}
187if { $result } {
188  pass $message
189} else {
190  fail $message
191}
192