xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.threads/watchthreads.exp (revision 15a984a0d95c8f96abe9717ee6241762c55dc106)
1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2004-2020 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] then {
48    fail "can't run to main"
49    return 0
50}
51
52set args_0 0
53set args_1 0
54
55# Watch values that will be modified by distinct threads.
56gdb_test "watch args\[0\]" "Hardware watchpoint 2: args\\\[0\\\]"
57gdb_test "watch args\[1\]" "Hardware watchpoint 3: args\\\[1\\\]"
58set hwwp_2_enabled 1
59set hwwp_3_enabled 1
60
61set init_line [gdb_get_line_number "Init value"]
62set inc_line [gdb_get_line_number "Loop increment"]
63set main_loc "main \\\(\\\) at .*watchthreads.c:$init_line"
64set thread0_loc "thread_function \\\(arg=0x0\\\) at .*watchthreads.c:$inc_line"
65set thread1_loc "thread_function \\\(arg=0x1\\\) at .*watchthreads.c:$inc_line"
66
67# Loop and continue to allow both watchpoints to be triggered.
68for {set i 0} {$i < 30} {incr i} {
69  set test_flag_0 0
70  set test_flag_1 0
71  set test_flag 0
72  gdb_test_multiple "continue" "threaded watch loop" {
73    -re "(.*Hardware watchpoint.*)$gdb_prompt $" {
74	# At least one hardware watchpoint was hit.  Check if both were.
75	set string $expect_out(1,string)
76
77	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] {
78	    incr args_0
79	    incr test_flag_0
80	}
81	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] {
82	    incr args_1
83	    incr test_flag_1
84	}
85
86	set expected_loc "bogus location"
87	if { $test_flag_0 == 1 && $test_flag_1 == 0 && $args_0 == 1 } {
88	    set expected_loc $main_loc
89	} elseif { $test_flag_0 == 0 && $test_flag_1 == 1 && $args_1 == 1 } {
90	    set expected_loc $main_loc
91	} elseif { $test_flag_0 == 1 && $test_flag_1 == 0 } {
92	    set expected_loc $thread0_loc
93	} elseif { $test_flag_0 == 0 && $test_flag_1 == 1 } {
94	    set expected_loc $thread1_loc
95	} elseif { $test_flag_0 + $test_flag_1 == 2 } {
96	    # On S/390, or any other system which can not report the
97	    # stopped data address, it is OK to report two watchpoints
98	    # at once in this test.  Make sure the reported location
99	    # corresponds to at least one of the watchpoints (and not,
100	    # e.g., __nptl_create_event).  On other systems, we should
101	    # report the two watchpoints serially.
102	    if { [target_no_stopped_data] } {
103		set expected_loc "($main_loc|$thread0_loc|$thread1_loc)"
104	    }
105	}
106
107	if [ regexp "$expected_loc" $string ] {
108	    set test_flag 1
109	} else {
110	    fail "threaded watch loop"
111	}
112
113	# If one of the watchpoints is disabled, we'd better not stop there.
114	if { !$hwwp_2_enabled && $test_flag_0 } {
115	    fail "disabled hw watchpoint 2 triggered"
116	}
117	if { !$hwwp_3_enabled && $test_flag_1 } {
118	    fail "disabled hw watchpoint 3 triggered"
119	}
120
121	# If we get to 10 in one of the watched locations disable it so we
122	# see some of the other watched location.
123	# 10 is chosen so we're guaranteed to come through here.
124	if { $hwwp_2_enabled && $hwwp_3_enabled } {
125	    if { $args_0 >= 10 && $hwwp_2_enabled } {
126		gdb_test_no_output "disable 2" "disable first watchpoint at 10"
127		set hwwp_2_enabled 0
128	    } elseif { $args_1 >= 10 && $hwwp_3_enabled } {
129		gdb_test_no_output "disable 3" "disable first watchpoint at 10"
130		set hwwp_3_enabled 0
131	    }
132	}
133    }
134  }
135
136  # If we fail above, don't bother continuing loop
137  if { $test_flag == 0 } {
138    set i 30
139  }
140}
141
142# Print success message if loop succeeded.
143if { $test_flag == 1 } {
144  pass "threaded watch loop"
145}
146
147# Verify that we hit first watchpoint in main thread.
148set message "first watchpoint on args\[0\] hit"
149if { $args_0 > 0 } {
150  pass $message
151} else {
152  fail $message
153}
154
155# Verify that we hit second watchpoint in main thread.
156set message "first watchpoint on args\[1\] hit"
157if { $args_1 > 0 } {
158  pass $message
159} else {
160  fail $message
161}
162
163# Verify that we hit first watchpoint in child thread.
164set message "watchpoint on args\[0\] hit in thread"
165if { $args_0 > 1 } {
166  pass $message
167} else {
168  fail $message
169}
170
171# Verify that we hit second watchpoint in child thread.
172set message "watchpoint on args\[1\] hit in thread"
173if { $args_1 > 1 } {
174  pass $message
175} else {
176  fail $message
177}
178
179# Verify that all watchpoint hits are accounted for.
180set message "combination of threaded watchpoints = 30"
181if { [target_no_stopped_data] } {
182    # See above.  If we allow two watchpoints to be hit at once, we
183    # may have more than 30 hits total.
184    set result [expr $args_0 + $args_1 >= 30]
185} else {
186    set result [expr $args_0 + $args_1 == 30]
187}
188if { $result } {
189  pass $message
190} else {
191  fail $message
192}
193