1 # Copyright (C) 1996-2014 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 # On Ultrix, we don't need it and it is really slow (because shell_escape
152 # doesn't use vfork).
153 if ![istarget "*-*-ultrix*"] then {
154 gdb_test "shell stty intr '^C'" ".*"
159 gdb_test_no_output "set print sevenbit-strings"
160 gdb_test_no_output "set width 0"
164 # See if scheduler locking is available on this target.
166 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
167 -re "Target .* cannot support this command" {
168 unsupported "target does not support scheduler locking"
171 -re "$gdb_prompt $" {
172 pass "scheduler locking set to none"
175 unsupported "target does not support scheduler locking (timeout)"
180 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
181 gdb_continue_to_breakpoint "all threads started"
183 set start_args [get_args "before initial"]
185 # First make sure that all threads are alive.
186 my_continue "initial"
188 set cont_args [get_args "after initial"]
191 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
192 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
197 pass "all threads alive"
199 fail "all threads alive ($bad/$NUM did not run)"
202 # Compare the previous thread and args with the current thread and
203 # args. Check that we didn't switch threads, and that the threads
204 # incremented their args counter the amounts expected. CMD is the
205 # command being tested. BEFORE_THREAD is the thread that was selected
206 # before the command was run. BEFORE_ARGS is the value of the
207 # thread's args before the command was run. LOCKED indicates whether
208 # we expect threads other than the selected thread remained locked.
210 proc check_result { cmd before_thread before_args locked } {
213 # Make sure we're still in the same thread.
214 set newthread [get_current_thread "after"]
216 set test "$cmd does not change thread"
217 if {$before_thread == $newthread} {
220 fail "$test (switched to thread $newthread)"
223 set after_args [get_args "after"]
225 set test "current thread advanced"
227 set test "$test - locked"
229 set test "$test - unlocked"
232 set num_other_threads 0
233 for {set i 0} {$i < $NUM} {incr i} {
234 if {[lindex $before_args $i] == [lindex $after_args $i]} {
235 if {$i == $before_thread} {
236 fail "$test (didn't run)"
239 if {$i == $before_thread} {
240 if {$cmd == "continue"
241 || [lindex $before_args $i] == [expr [lindex $after_args $i] - 10]} {
244 fail "$test (wrong amount)"
247 incr num_other_threads
253 gdb_assert {$num_other_threads == 0} "other threads didn't run - locked"
255 gdb_assert {$num_other_threads > 0} "other threads ran - unlocked"
259 with_test_prefix "schedlock=on: cmd=continue" {
260 # Use whichever we stopped in.
261 set curthread [get_current_thread "before"]
263 # Test continue with scheduler locking.
264 gdb_test "set scheduler-locking on" ""
266 my_continue "with lock"
268 check_result "continue" $curthread $cont_args 1
271 # Test stepping/nexting with different modes of scheduler locking.
272 proc test_step { schedlock cmd call_function } {
275 gdb_test_no_output "set scheduler-locking off"
278 set curthread [get_current_thread "before"]
280 # No need to set to off again. This avoids a duplicate message.
281 if {$schedlock != "off"} {
282 gdb_test_no_output "set scheduler-locking $schedlock"
285 gdb_test "print call_function = $call_function" \
288 set before_args [get_args "before"]
292 # "next" lets other threads run while stepping over functions.
293 if { $schedlock == "on" || ($schedlock == "step" && !$call_function) } {
299 check_result $cmd $curthread $before_args $locked
302 # Test stepping/nexting with different modes of scheduler locking.
303 foreach schedlock {"off" "step" "on"} {
304 with_test_prefix "schedlock=$schedlock" {
305 with_test_prefix "cmd=step" {
306 test_step $schedlock "step" 0
308 with_test_prefix "cmd=next" {
309 # With "next", and schedlock "step", threads run unlocked
310 # when stepping over a function call. This exercises both
311 # with and without a function call. Without a function
312 # call "next" should behave just like "step".
313 foreach call_function {0 1} {
314 with_test_prefix "call_function=$call_function" {
315 test_step $schedlock "next" $call_function