]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/schedlock.exp
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / schedlock.exp
CommitLineData
1d506c26 1# Copyright (C) 1996-2024 Free Software Foundation, Inc.
0312286c
DJ
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
0312286c 6# (at your option) any later version.
e22f8b7c 7#
0312286c
DJ
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
e22f8b7c 12#
0312286c 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
0312286c 15
0312286c
DJ
16# This file was written by Daniel Jacobowitz <drow@mvista.com>
17# (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
18#
19# This test covers the various forms of "set scheduler-locking".
20
87a3a92c
SL
21# This test requires sending ^C to interrupt the running target.
22
450d26c8 23require {!target_info exists gdb,nointerrupts}
0312286c 24
0efbbabc 25standard_testfile
0312286c 26
18ecae38
DJ
27# The number of threads, including the main thread.
28set NUM 2
29
0efbbabc 30if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
0312286c
DJ
31 return -1
32}
33
34# Now we can proceed with the real testing.
35
35420406
PA
36# Get the current contents of the `args` array in the test program.
37# Description is appended to the test message.
38
39proc get_args { description } {
9db70545
MS
40 global gdb_prompt
41 global NUM
e09490f1 42
9db70545
MS
43 set pattern "(\[0-9\]+)"
44 for {set i 1} {[expr $i < $NUM]} {incr i} {
45 append pattern ", (\[0-9\]+)"
46 }
0312286c 47
35420406
PA
48 set test "listed args ($description)"
49 gdb_test_multiple "print args" $test {
9db70545 50 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
35420406 51 pass $test
e09490f1 52
9db70545
MS
53 set result ""
54 for {set i 1} {[expr $i <= $NUM]} {incr i} {
55 lappend result $expect_out($i,string)
56 }
57 return $result
e09490f1 58 }
9db70545 59 }
0312286c
DJ
60}
61
62proc stop_process { description } {
63 global gdb_prompt
64
65 # For this to work we must be sure to consume the "Continuing."
66 # message first, or GDB's signal handler may not be in place.
67 after 1000 {send_gdb "\003"}
68 gdb_expect {
f303dbd6 69 -re "Thread .* received signal SIGINT.*$gdb_prompt $"
0312286c
DJ
70 {
71 pass $description
72 }
73 timeout
74 {
75 fail "$description (timeout)"
76 }
77 }
78}
79
80proc get_current_thread { description } {
9db70545 81 global gdb_prompt
0312286c 82
35420406
PA
83 set test "find current thread ($description)"
84
85 gdb_test_multiple "bt" $test {
9db70545 86 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
35420406 87 pass $test
9db70545
MS
88 return $expect_out(1,string)
89 }
90 }
91 return ""
0312286c
DJ
92}
93
35420406
PA
94# Make sure we're stopped in the loop, in one of the non-main threads.
95
96proc goto_loop { msg } {
97 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
98
99 set test "return to loop"
100 if {$msg != ""} {
101 set test "$test ($msg)"
102 }
103 gdb_continue_to_breakpoint $test
104 delete_breakpoints
105}
106
0312286c 107proc my_continue { msg } {
35420406
PA
108 set test "continue ($msg)"
109 gdb_test_multiple "continue" $test {
9db70545 110 -re "Continuing" {
35420406 111 pass $test
9db70545
MS
112 }
113 }
0312286c 114
9db70545 115 stop_process "stop all threads ($msg)"
0312286c 116
35420406 117 goto_loop $msg
0312286c
DJ
118}
119
35420406
PA
120# Use CMD to step the loop 10 times. CMD may be "step" or "next".
121
122proc step_ten_loops { cmd } {
0312286c
DJ
123 global gdb_prompt
124
125 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
0312286c 126 set other_step 0
35420406
PA
127 set test "$cmd to increment ($i)"
128 gdb_test_multiple $cmd $test {
0312286c 129 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
35420406 130 pass $test
0312286c
DJ
131 }
132 -re "$gdb_prompt $" {
133 if {$other_step == 0} {
134 set other_step 1
35420406 135 send_gdb "$cmd\n"
0312286c
DJ
136 exp_continue
137 } else {
35420406 138 fail $test
0312286c
DJ
139 # FIXME cascade?
140 }
141 }
0312286c
DJ
142 }
143 }
144}
145
a30f2b7b 146clean_restart
0312286c
DJ
147
148# We'll need this when we send_gdb a ^C to GDB. Need to do it before we
149# run the program and gdb starts saving and restoring tty states.
09dd9a69 150gdb_test "shell stty intr '^C'" ".*"
0312286c
DJ
151
152gdb_load ${binfile}
153
12b5d08a
MS
154gdb_test_no_output "set print sevenbit-strings"
155gdb_test_no_output "set width 0"
0312286c
DJ
156
157runto_main
158
159# See if scheduler locking is available on this target.
0312286c 160global gdb_prompt
9db70545
MS
161gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
162 -re "Target .* cannot support this command" {
163 unsupported "target does not support scheduler locking"
164 return
0312286c 165 }
9db70545
MS
166 -re "$gdb_prompt $" {
167 pass "scheduler locking set to none"
0312286c 168 }
9db70545
MS
169 timeout {
170 unsupported "target does not support scheduler locking (timeout)"
171 return
0312286c
DJ
172 }
173}
174
175gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
176gdb_continue_to_breakpoint "all threads started"
177
35420406 178set start_args [get_args "before initial"]
0312286c
DJ
179
180# First make sure that all threads are alive.
181my_continue "initial"
182
35420406 183set cont_args [get_args "after initial"]
0312286c 184
e09490f1 185set bad 0
18ecae38 186for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
0312286c 187 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
e09490f1 188 incr bad
0312286c
DJ
189 }
190}
e09490f1 191if { $bad == 0 } {
18ecae38
DJ
192 pass "all threads alive"
193} else {
e09490f1 194 fail "all threads alive ($bad/$NUM did not run)"
18ecae38 195}
0312286c 196
35420406
PA
197# Compare the previous thread and args with the current thread and
198# args. Check that we didn't switch threads, and that the threads
199# incremented their args counter the amounts expected. CMD is the
200# command being tested. BEFORE_THREAD is the thread that was selected
201# before the command was run. BEFORE_ARGS is the value of the
202# thread's args before the command was run. LOCKED indicates whether
203# we expect threads other than the selected thread remained locked.
0312286c 204
35420406
PA
205proc check_result { cmd before_thread before_args locked } {
206 global NUM
0312286c 207
35420406
PA
208 # Make sure we're still in the same thread.
209 set newthread [get_current_thread "after"]
0312286c 210
35420406
PA
211 set test "$cmd does not change thread"
212 if {$before_thread == $newthread} {
213 pass "$test"
214 } else {
215 fail "$test (switched to thread $newthread)"
216 }
0312286c 217
35420406 218 set after_args [get_args "after"]
0312286c 219
35420406
PA
220 set test "current thread advanced"
221 if { $locked } {
222 set test "$test - locked"
223 } else {
224 set test "$test - unlocked"
0312286c 225 }
35420406
PA
226
227 set num_other_threads 0
228 for {set i 0} {$i < $NUM} {incr i} {
229 if {[lindex $before_args $i] == [lindex $after_args $i]} {
230 if {$i == $before_thread} {
231 fail "$test (didn't run)"
232 }
0312286c 233 } else {
35420406
PA
234 if {$i == $before_thread} {
235 if {$cmd == "continue"
236 || [lindex $before_args $i] == [expr [lindex $after_args $i] - 10]} {
237 pass "$test"
238 } else {
239 fail "$test (wrong amount)"
240 }
241 } else {
242 incr num_other_threads
243 }
0312286c 244 }
35420406
PA
245 }
246
247 if { $locked } {
248 gdb_assert {$num_other_threads == 0} "other threads didn't run - locked"
0312286c 249 } else {
35420406 250 gdb_assert {$num_other_threads > 0} "other threads ran - unlocked"
0312286c 251 }
a25fbfec 252}
0312286c 253
35420406
PA
254with_test_prefix "schedlock=on: cmd=continue" {
255 # Use whichever we stopped in.
256 set curthread [get_current_thread "before"]
0312286c 257
35420406
PA
258 # Test continue with scheduler locking.
259 gdb_test "set scheduler-locking on" ""
0312286c 260
35420406
PA
261 my_continue "with lock"
262
263 check_result "continue" $curthread $cont_args 1
0312286c
DJ
264}
265
35420406
PA
266# Test stepping/nexting with different modes of scheduler locking.
267proc test_step { schedlock cmd call_function } {
268 global NUM
0312286c 269
35420406
PA
270 gdb_test_no_output "set scheduler-locking off"
271 goto_loop ""
272
273 set curthread [get_current_thread "before"]
274
275 # No need to set to off again. This avoids a duplicate message.
276 if {$schedlock != "off"} {
277 gdb_test_no_output "set scheduler-locking $schedlock"
0312286c 278 }
0312286c 279
35420406
PA
280 gdb_test "print call_function = $call_function" \
281 " = $call_function"
0312286c 282
35420406 283 set before_args [get_args "before"]
0312286c 284
35420406 285 step_ten_loops $cmd
0312286c 286
856e7dd6 287 if { $schedlock == "on" || $schedlock == "step" } {
35420406 288 set locked 1
0312286c 289 } else {
35420406 290 set locked 0
0312286c 291 }
35420406
PA
292
293 check_result $cmd $curthread $before_args $locked
18ecae38 294}
0312286c 295
35420406
PA
296# Test stepping/nexting with different modes of scheduler locking.
297foreach schedlock {"off" "step" "on"} {
298 with_test_prefix "schedlock=$schedlock" {
299 with_test_prefix "cmd=step" {
300 test_step $schedlock "step" 0
301 }
302 with_test_prefix "cmd=next" {
856e7dd6
PA
303 # In GDB <= 7.9, with schedlock "step", "next" would
304 # unlock threads when stepping over a function call. This
305 # exercises "next" with and without a function call. WRT
306 # "schedlock step", "next" should behave just like "step".
35420406
PA
307 foreach call_function {0 1} {
308 with_test_prefix "call_function=$call_function" {
309 test_step $schedlock "next" $call_function
310 }
311 }
312 }
313 }
314}