1 # Copyright (C) 1996-2024 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 require {!target_info exists gdb,nointerrupts}
27 # The number of threads, including the main thread.
30 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
34 # Now we can proceed with the real testing.
36 # Get the current contents of the `args` array in the test program.
37 # Description is appended to the test message.
39 proc get_args { description } {
43 set pattern "(\[0-9\]+)"
44 for {set i 1} {[expr $i < $NUM]} {incr i} {
45 append pattern ", (\[0-9\]+)"
48 set test "listed args, $description"
49 gdb_test_multiple "print args" $test {
50 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
54 for {set i 1} {[expr $i <= $NUM]} {incr i} {
55 lappend result $expect_out($i,string)
62 proc stop_process { description } {
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"}
69 -re "Thread .* received signal SIGINT.*$gdb_prompt $"
75 fail "$description (timeout)"
80 proc get_current_thread { description } {
83 set test "find current thread, $description"
85 gdb_test_multiple "bt" $test {
86 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
88 return $expect_out(1,string)
94 # Make sure we're stopped in the loop, in one of the non-main threads.
96 proc goto_loop { msg } {
97 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
99 set test "return to loop"
101 set test "$test ($msg)"
103 gdb_continue_to_breakpoint $test
107 proc my_continue { msg } {
108 set test "continue ($msg)"
109 gdb_test_multiple "continue" $test {
115 stop_process "stop all threads ($msg)"
120 # Use CMD to step the loop 10 times. CMD may be "step" or "next".
122 proc step_ten_loops { cmd } {
125 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
127 set test "$cmd to increment, $i"
128 gdb_test_multiple $cmd $test {
129 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
132 -re "$gdb_prompt $" {
133 if {$other_step == 0} {
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.
150 gdb_test "shell stty intr '^C'" ".*"
154 gdb_test_no_output "set print sevenbit-strings"
155 gdb_test_no_output "set width 0"
159 # See if scheduler locking is available on this target.
161 gdb_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"
166 -re "$gdb_prompt $" {
167 pass "scheduler locking set to none"
170 unsupported "target does not support scheduler locking (timeout)"
175 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
176 gdb_continue_to_breakpoint "all threads started"
178 set start_args [get_args "before initial"]
180 # First make sure that all threads are alive.
181 my_continue "initial"
183 set cont_args [get_args "after initial"]
186 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
187 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
192 pass "all threads alive"
194 fail "all threads alive ($bad/$NUM did not run)"
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.
205 proc check_result { cmd before_thread before_args locked } {
208 # Make sure we're still in the same thread.
209 set newthread [get_current_thread "after"]
211 set test "$cmd does not change thread"
212 if {$before_thread == $newthread} {
215 fail "$test (switched to thread $newthread)"
218 set after_args [get_args "after"]
220 set test "current thread advanced"
222 set test "$test - locked"
224 set test "$test - unlocked"
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)"
234 if {$i == $before_thread} {
235 if {$cmd == "continue"
236 || [lindex $before_args $i] == [expr [lindex $after_args $i] - 10]} {
239 fail "$test (wrong amount)"
242 incr num_other_threads
248 gdb_assert {$num_other_threads == 0} "other threads didn't run - locked"
250 gdb_assert {$num_other_threads > 0} "other threads ran - unlocked"
254 with_test_prefix "schedlock=on: cmd=continue" {
255 # Use whichever we stopped in.
256 set curthread [get_current_thread "before"]
258 # Test continue with scheduler locking.
259 gdb_test "set scheduler-locking on" ""
261 my_continue "with lock"
263 check_result "continue" $curthread $cont_args 1
266 # Test stepping/nexting with different modes of scheduler locking.
267 proc test_step { schedlock cmd call_function } {
270 gdb_test_no_output "set scheduler-locking off"
273 set curthread [get_current_thread "before"]
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"
280 gdb_test "print call_function = $call_function" \
283 set before_args [get_args "before"]
287 if { $schedlock == "on" || $schedlock == "step" } {
293 check_result $cmd $curthread $before_args $locked
296 # Test stepping/nexting with different modes of scheduler locking.
297 foreach schedlock {"off" "step" "on"} {
298 with_test_prefix "schedlock=$schedlock" {
299 with_test_prefix "cmd=step" {
300 test_step $schedlock "step" 0
302 with_test_prefix "cmd=next" {
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".
307 foreach call_function {0 1} {
308 with_test_prefix "call_function=$call_function" {
309 test_step $schedlock "next" $call_function