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