1 # Copyright (C) 1996-2023 Free Software Foundation, Inc.
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
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
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.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This file was written by Daniel Jacobowitz <drow@mvista.com>
17 # (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
19 # This test covers the various forms of "set scheduler-locking".
21 # This test requires sending ^C to interrupt the running target.
23 if [target_info exists gdb,nointerrupts] {
24 verbose "Skipping schedlock.exp because of nointerrupts."
30 # The number of threads, including the main thread.
33 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
37 # Now we can proceed with the real testing.
39 # Get the current contents of the `args` array in the test program.
40 # Description is appended to the test message.
42 proc get_args { description } {
46 set pattern "(\[0-9\]+)"
47 for {set i 1} {[expr $i < $NUM]} {incr i} {
48 append pattern ", (\[0-9\]+)"
51 set test "listed args ($description)"
52 gdb_test_multiple "print args" $test {
53 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
57 for {set i 1} {[expr $i <= $NUM]} {incr i} {
58 lappend result $expect_out($i,string)
65 proc stop_process { description } {
68 # For this to work we must be sure to consume the "Continuing."
69 # message first, or GDB's signal handler may not be in place.
70 after 1000 {send_gdb "\003"}
72 -re "Thread .* received signal SIGINT.*$gdb_prompt $"
78 fail "$description (timeout)"
83 proc get_current_thread { description } {
86 set test "find current thread ($description)"
88 gdb_test_multiple "bt" $test {
89 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
91 return $expect_out(1,string)
97 # Make sure we're stopped in the loop, in one of the non-main threads.
99 proc goto_loop { msg } {
100 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
102 set test "return to loop"
104 set test "$test ($msg)"
106 gdb_continue_to_breakpoint $test
110 proc my_continue { msg } {
111 set test "continue ($msg)"
112 gdb_test_multiple "continue" $test {
118 stop_process "stop all threads ($msg)"
123 # Use CMD to step the loop 10 times. CMD may be "step" or "next".
125 proc step_ten_loops { cmd } {
128 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
130 set test "$cmd to increment ($i)"
131 gdb_test_multiple $cmd $test {
132 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
135 -re "$gdb_prompt $" {
136 if {$other_step == 0} {
149 # Start with a fresh gdb.
153 gdb_reinitialize_dir $srcdir/$subdir
155 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
156 # run the program and gdb starts saving and restoring tty states.
157 gdb_test "shell stty intr '^C'" ".*"
161 gdb_test_no_output "set print sevenbit-strings"
162 gdb_test_no_output "set width 0"
166 # See if scheduler locking is available on this target.
168 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
169 -re "Target .* cannot support this command" {
170 unsupported "target does not support scheduler locking"
173 -re "$gdb_prompt $" {
174 pass "scheduler locking set to none"
177 unsupported "target does not support scheduler locking (timeout)"
182 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
183 gdb_continue_to_breakpoint "all threads started"
185 set start_args [get_args "before initial"]
187 # First make sure that all threads are alive.
188 my_continue "initial"
190 set cont_args [get_args "after initial"]
193 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
194 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
199 pass "all threads alive"
201 fail "all threads alive ($bad/$NUM did not run)"
204 # Compare the previous thread and args with the current thread and
205 # args. Check that we didn't switch threads, and that the threads
206 # incremented their args counter the amounts expected. CMD is the
207 # command being tested. BEFORE_THREAD is the thread that was selected
208 # before the command was run. BEFORE_ARGS is the value of the
209 # thread's args before the command was run. LOCKED indicates whether
210 # we expect threads other than the selected thread remained locked.
212 proc check_result { cmd before_thread before_args locked } {
215 # Make sure we're still in the same thread.
216 set newthread [get_current_thread "after"]
218 set test "$cmd does not change thread"
219 if {$before_thread == $newthread} {
222 fail "$test (switched to thread $newthread)"
225 set after_args [get_args "after"]
227 set test "current thread advanced"
229 set test "$test - locked"
231 set test "$test - unlocked"
234 set num_other_threads 0
235 for {set i 0} {$i < $NUM} {incr i} {
236 if {[lindex $before_args $i] == [lindex $after_args $i]} {
237 if {$i == $before_thread} {
238 fail "$test (didn't run)"
241 if {$i == $before_thread} {
242 if {$cmd == "continue"
243 || [lindex $before_args $i] == [expr [lindex $after_args $i] - 10]} {
246 fail "$test (wrong amount)"
249 incr num_other_threads
255 gdb_assert {$num_other_threads == 0} "other threads didn't run - locked"
257 gdb_assert {$num_other_threads > 0} "other threads ran - unlocked"
261 with_test_prefix "schedlock=on: cmd=continue" {
262 # Use whichever we stopped in.
263 set curthread [get_current_thread "before"]
265 # Test continue with scheduler locking.
266 gdb_test "set scheduler-locking on" ""
268 my_continue "with lock"
270 check_result "continue" $curthread $cont_args 1
273 # Test stepping/nexting with different modes of scheduler locking.
274 proc test_step { schedlock cmd call_function } {
277 gdb_test_no_output "set scheduler-locking off"
280 set curthread [get_current_thread "before"]
282 # No need to set to off again. This avoids a duplicate message.
283 if {$schedlock != "off"} {
284 gdb_test_no_output "set scheduler-locking $schedlock"
287 gdb_test "print call_function = $call_function" \
290 set before_args [get_args "before"]
294 if { $schedlock == "on" || $schedlock == "step" } {
300 check_result $cmd $curthread $before_args $locked
303 # Test stepping/nexting with different modes of scheduler locking.
304 foreach schedlock {"off" "step" "on"} {
305 with_test_prefix "schedlock=$schedlock" {
306 with_test_prefix "cmd=step" {
307 test_step $schedlock "step" 0
309 with_test_prefix "cmd=next" {
310 # In GDB <= 7.9, with schedlock "step", "next" would
311 # unlock threads when stepping over a function call. This
312 # exercises "next" with and without a function call. WRT
313 # "schedlock step", "next" should behave just like "step".
314 foreach call_function {0 1} {
315 with_test_prefix "call_function=$call_function" {
316 test_step $schedlock "next" $call_function