1 # Copyright (C) 1996-2013 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.
38 set pattern "(\[0-9\]+)"
39 for {set i 1} {[expr $i < $NUM]} {incr i} {
40 append pattern ", (\[0-9\]+)"
43 gdb_test_multiple "print args" "listed args ($list_count)" {
44 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
45 set list_count [expr $list_count + 1]
46 pass "listed args ($list_count)"
49 for {set i 1} {[expr $i <= $NUM]} {incr i} {
50 lappend result $expect_out($i,string)
57 proc stop_process { description } {
60 # For this to work we must be sure to consume the "Continuing."
61 # message first, or GDB's signal handler may not be in place.
62 after 1000 {send_gdb "\003"}
64 -re "Program received signal SIGINT.*$gdb_prompt $"
70 fail "$description (timeout)"
75 proc get_current_thread { description } {
78 gdb_test_multiple "bt" "$description" {
79 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
81 return $expect_out(1,string)
87 proc my_continue { msg } {
88 gdb_test_multiple "continue" "continuing ($msg)" {
90 pass "continue ($msg)"
94 stop_process "stop all threads ($msg)"
96 # Make sure we're in one of the non-main looping threads.
97 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
98 gdb_continue_to_breakpoint "return to loop ($msg)"
102 proc step_ten_loops { msg } {
105 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
107 gdb_test_multiple "step" "step to increment ($msg $i)" {
108 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
109 pass "step to increment ($msg $i)"
111 -re "$gdb_prompt $" {
112 if {$other_step == 0} {
117 fail "step to increment ($msg $i)"
125 # Start with a fresh gdb.
129 gdb_reinitialize_dir $srcdir/$subdir
131 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
132 # run the program and gdb starts saving and restoring tty states.
133 # On Ultrix, we don't need it and it is really slow (because shell_escape
134 # doesn't use vfork).
135 if ![istarget "*-*-ultrix*"] then {
136 gdb_test "shell stty intr '^C'" ".*"
141 gdb_test_no_output "set print sevenbit-strings"
142 gdb_test_no_output "set width 0"
146 # See if scheduler locking is available on this target.
148 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
149 -re "Target .* cannot support this command" {
150 unsupported "target does not support scheduler locking"
153 -re "$gdb_prompt $" {
154 pass "scheduler locking set to none"
157 unsupported "target does not support scheduler locking (timeout)"
162 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
163 gdb_continue_to_breakpoint "all threads started"
168 set start_args [get_args]
170 # First make sure that all threads are alive.
171 my_continue "initial"
173 set cont_args [get_args]
176 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
177 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
182 pass "all threads alive"
184 fail "all threads alive ($bad/$NUM did not run)"
187 # We can't change threads, unfortunately, in current GDB. Use
188 # whichever we stopped in.
189 set curthread [get_current_thread "find current thread (1)"]
194 # Test stepping without scheduler locking.
195 gdb_test_no_output "set scheduler-locking off"
197 step_ten_loops "unlocked"
199 # Make sure we're still in the same thread.
200 set newthread [get_current_thread "find current thread (2)"]
201 if {$curthread == $newthread} {
202 pass "step without lock does not change thread"
204 fail "step without lock does not change thread (switched to thread $newthread)"
207 set start_args $cont_args
208 set cont_args [get_args]
210 set num_other_threads 0
211 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
212 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
213 if {$i == $curthread} {
214 fail "current thread stepped (didn't run)"
217 if {$i == $curthread} {
218 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
219 pass "current thread stepped"
221 fail "current thread stepped (wrong amount)"
224 set num_other_threads [expr $num_other_threads + 1]
228 if {$num_other_threads > 0} {
229 pass "other threads ran - unlocked"
231 fail "other threads ran - unlocked"
234 # Test continue with scheduler locking
235 gdb_test "set scheduler-locking on" ""
237 my_continue "with lock"
239 # Make sure we're still in the same thread.
240 set newthread [get_current_thread "find current thread (3)"]
241 if {$curthread == $newthread} {
242 pass "continue with lock does not change thread"
244 fail "continue with lock does not change thread (switched to thread $newthread)"
247 set start_args $cont_args
248 set cont_args [get_args]
250 set num_other_threads 0
251 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
252 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
253 if {$i == $curthread} {
254 fail "current thread ran (didn't run)"
257 if {$i == $curthread} {
258 pass "current thread ran"
260 incr num_other_threads
264 if {$num_other_threads > 0} {
265 fail "other threads didn't run - locked"
267 pass "other threads didn't run - locked"
270 # Test stepping with scheduler locking
271 step_ten_loops "locked"
273 # Make sure we're still in the same thread.
274 set newthread [get_current_thread "find current thread (2)"]
275 if {$curthread == $newthread} {
276 pass "step with lock does not change thread"
278 fail "step with lock does not change thread (switched to thread $newthread)"
281 set start_args $cont_args
282 set cont_args [get_args]
284 set num_other_threads 0
285 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
286 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
287 if {$i == $curthread} {
288 fail "current thread stepped locked (didn't run)"
291 if {$i == $curthread} {
292 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
293 pass "current thread stepped locked"
295 fail "current thread stepped locked (wrong amount)"
298 incr num_other_threads
302 if {$num_other_threads > 0} {
303 fail "other threads didn't run - step locked"
305 pass "other threads didn't run - step locked"