1 # Copyright (C) 1996-2015 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".
24 # The number of threads, including the main thread.
27 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
31 # Now we can proceed with the real testing.
33 # Get the current contents of the `args` array in the test program.
34 # Description is appended to the test message.
36 proc get_args { description } {
40 set pattern "(\[0-9\]+)"
41 for {set i 1} {[expr $i < $NUM]} {incr i} {
42 append pattern ", (\[0-9\]+)"
45 set test "listed args ($description)"
46 gdb_test_multiple "print args" $test {
47 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
51 for {set i 1} {[expr $i <= $NUM]} {incr i} {
52 lappend result $expect_out($i,string)
59 proc stop_process { description } {
62 # For this to work we must be sure to consume the "Continuing."
63 # message first, or GDB's signal handler may not be in place.
64 after 1000 {send_gdb "\003"}
66 -re "Program received signal SIGINT.*$gdb_prompt $"
72 fail "$description (timeout)"
77 proc get_current_thread { description } {
80 set test "find current thread ($description)"
82 gdb_test_multiple "bt" $test {
83 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
85 return $expect_out(1,string)
91 # Make sure we're stopped in the loop, in one of the non-main threads.
93 proc goto_loop { msg } {
94 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
96 set test "return to loop"
98 set test "$test ($msg)"
100 gdb_continue_to_breakpoint $test
104 proc my_continue { msg } {
105 set test "continue ($msg)"
106 gdb_test_multiple "continue" $test {
112 stop_process "stop all threads ($msg)"
117 # Use CMD to step the loop 10 times. CMD may be "step" or "next".
119 proc step_ten_loops { cmd } {
122 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
124 set test "$cmd to increment ($i)"
125 gdb_test_multiple $cmd $test {
126 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
129 -re "$gdb_prompt $" {
130 if {$other_step == 0} {
143 # Start with a fresh gdb.
147 gdb_reinitialize_dir $srcdir/$subdir
149 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
150 # run the program and gdb starts saving and restoring tty states.
151 gdb_test "shell stty intr '^C'" ".*"
155 gdb_test_no_output "set print sevenbit-strings"
156 gdb_test_no_output "set width 0"
160 # See if scheduler locking is available on this target.
162 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
163 -re "Target .* cannot support this command" {
164 unsupported "target does not support scheduler locking"
167 -re "$gdb_prompt $" {
168 pass "scheduler locking set to none"
171 unsupported "target does not support scheduler locking (timeout)"
176 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
177 gdb_continue_to_breakpoint "all threads started"
179 set start_args [get_args "before initial"]
181 # First make sure that all threads are alive.
182 my_continue "initial"
184 set cont_args [get_args "after initial"]
187 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
188 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
193 pass "all threads alive"
195 fail "all threads alive ($bad/$NUM did not run)"
198 # Compare the previous thread and args with the current thread and
199 # args. Check that we didn't switch threads, and that the threads
200 # incremented their args counter the amounts expected. CMD is the
201 # command being tested. BEFORE_THREAD is the thread that was selected
202 # before the command was run. BEFORE_ARGS is the value of the
203 # thread's args before the command was run. LOCKED indicates whether
204 # we expect threads other than the selected thread remained locked.
206 proc check_result { cmd before_thread before_args locked } {
209 # Make sure we're still in the same thread.
210 set newthread [get_current_thread "after"]
212 set test "$cmd does not change thread"
213 if {$before_thread == $newthread} {
216 fail "$test (switched to thread $newthread)"
219 set after_args [get_args "after"]
221 set test "current thread advanced"
223 set test "$test - locked"
225 set test "$test - unlocked"
228 set num_other_threads 0
229 for {set i 0} {$i < $NUM} {incr i} {
230 if {[lindex $before_args $i] == [lindex $after_args $i]} {
231 if {$i == $before_thread} {
232 fail "$test (didn't run)"
235 if {$i == $before_thread} {
236 if {$cmd == "continue"
237 || [lindex $before_args $i] == [expr [lindex $after_args $i] - 10]} {
240 fail "$test (wrong amount)"
243 incr num_other_threads
249 gdb_assert {$num_other_threads == 0} "other threads didn't run - locked"
251 gdb_assert {$num_other_threads > 0} "other threads ran - unlocked"
255 with_test_prefix "schedlock=on: cmd=continue" {
256 # Use whichever we stopped in.
257 set curthread [get_current_thread "before"]
259 # Test continue with scheduler locking.
260 gdb_test "set scheduler-locking on" ""
262 my_continue "with lock"
264 check_result "continue" $curthread $cont_args 1
267 # Test stepping/nexting with different modes of scheduler locking.
268 proc test_step { schedlock cmd call_function } {
271 gdb_test_no_output "set scheduler-locking off"
274 set curthread [get_current_thread "before"]
276 # No need to set to off again. This avoids a duplicate message.
277 if {$schedlock != "off"} {
278 gdb_test_no_output "set scheduler-locking $schedlock"
281 gdb_test "print call_function = $call_function" \
284 set before_args [get_args "before"]
288 if { $schedlock == "on" || $schedlock == "step" } {
294 check_result $cmd $curthread $before_args $locked
297 # Test stepping/nexting with different modes of scheduler locking.
298 foreach schedlock {"off" "step" "on"} {
299 with_test_prefix "schedlock=$schedlock" {
300 with_test_prefix "cmd=step" {
301 test_step $schedlock "step" 0
303 with_test_prefix "cmd=next" {
304 # In GDB <= 7.9, with schedlock "step", "next" would
305 # unlock threads when stepping over a function call. This
306 # exercises "next" with and without a function call. WRT
307 # "schedlock step", "next" should behave just like "step".
308 foreach call_function {0 1} {
309 with_test_prefix "call_function=$call_function" {
310 test_step $schedlock "next" $call_function