1 # Copyright (C) 1996, 1997, 2002, 2003, 2007, 2008, 2009
2 # Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # This file was written by Daniel Jacobowitz <drow@mvista.com>
18 # (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
20 # This test covers the various forms of "set scheduler-locking".
29 set testfile "schedlock"
30 set srcfile ${testfile}.c
31 set binfile ${objdir}/${subdir}/${testfile}
33 # The number of threads, including the main thread.
36 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
40 # Now we can proceed with the real testing.
47 set pattern "(\[0-9\]+)"
48 for {set i 1} {[expr $i < $NUM]} {incr i} {
49 append pattern ", (\[0-9\]+)"
52 send_gdb "print args\n"
54 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt"
56 set list_count [expr $list_count + 1]
57 pass "listed args ($list_count)"
60 for {set i 1} {[expr $i <= $NUM]} {incr i} {
61 lappend result $expect_out($i,string)
67 fail "listed args ($list_count) (unknown output)"
71 fail "listed args ($list_count) (timeout)"
76 proc stop_process { description } {
79 # For this to work we must be sure to consume the "Continuing."
80 # message first, or GDB's signal handler may not be in place.
81 after 1000 {send_gdb "\003"}
83 -re "Program received signal SIGINT.*$gdb_prompt $"
89 fail "$description (timeout)"
94 proc get_current_thread { description } {
99 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $"
102 return $expect_out(1,string)
106 fail "$description (unknown output)"
110 fail "$description (timeout)"
115 proc my_continue { msg } {
116 send_gdb "continue\n"
119 { pass "continue ($msg)" }
121 { fail "continue ($msg) (timeout)" }
124 stop_process "stop all threads ($msg)"
126 # Make sure we're in one of the non-main looping threads.
127 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
128 gdb_continue_to_breakpoint "return to loop ($msg)"
132 proc step_ten_loops { msg } {
135 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
139 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
140 pass "step to increment ($msg $i)"
142 -re "$gdb_prompt $" {
143 if {$other_step == 0} {
148 fail "step to increment ($msg $i)"
153 fail "step to increment ($msg $i) (timeout)"
159 # Start with a fresh gdb.
163 gdb_reinitialize_dir $srcdir/$subdir
165 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
166 # run the program and gdb starts saving and restoring tty states.
167 # On Ultrix, we don't need it and it is really slow (because shell_escape
168 # doesn't use vfork).
169 if ![istarget "*-*-ultrix*"] then {
170 gdb_test "shell stty intr '^C'" ""
175 gdb_test "set print sevenbit-strings" ""
176 gdb_test "set width 0" ""
180 # See if scheduler locking is available on this target.
181 send_gdb "set scheduler-locking off\n"
184 -re "Target .* cannot support this command"
186 unsupported "target does not support scheduler locking"
191 pass "scheduler locking set to none"
195 unsupported "target does not support scheduler locking (timeout)"
200 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
201 gdb_continue_to_breakpoint "all threads started"
206 set start_args [get_args]
208 # First make sure that all threads are alive.
209 my_continue "initial"
211 set cont_args [get_args]
214 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
215 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
220 pass "all threads alive"
222 fail "all threads alive ($bad/$NUM did not run)"
225 # We can't change threads, unfortunately, in current GDB. Use
226 # whichever we stopped in.
227 set curthread [get_current_thread "find current thread (1)"]
232 # Test stepping without scheduler locking.
233 gdb_test "set scheduler-locking off" ""
235 step_ten_loops "unlocked"
237 # Make sure we're still in the same thread.
238 set newthread [get_current_thread "find current thread (2)"]
239 if {$curthread == $newthread} {
240 pass "step without lock does not change thread"
242 fail "step without lock does not change thread (switched to thread $newthread)"
245 set start_args $cont_args
246 set cont_args [get_args]
248 set num_other_threads 0
249 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
250 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
251 if {$i == $curthread} {
252 fail "current thread stepped (didn't run)"
255 if {$i == $curthread} {
256 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
257 pass "current thread stepped"
259 fail "current thread stepped (wrong amount)"
262 set num_other_threads [expr $num_other_threads + 1]
266 if {$num_other_threads > 0} {
267 pass "other threads ran - unlocked"
269 fail "other threads ran - unlocked"
272 # Test continue with scheduler locking
273 gdb_test "set scheduler-locking on" ""
275 my_continue "with lock"
277 # Make sure we're still in the same thread.
278 set newthread [get_current_thread "find current thread (3)"]
279 if {$curthread == $newthread} {
280 pass "continue with lock does not change thread"
282 fail "continue with lock does not change thread (switched to thread $newthread)"
285 set start_args $cont_args
286 set cont_args [get_args]
288 set num_other_threads 0
289 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
290 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
291 if {$i == $curthread} {
292 fail "current thread ran (didn't run)"
295 if {$i == $curthread} {
296 pass "current thread ran"
298 incr num_other_threads
302 if {$num_other_threads > 0} {
303 fail "other threads didn't run - locked"
305 pass "other threads didn't run - locked"
308 # Test stepping with scheduler locking
309 step_ten_loops "locked"
311 # Make sure we're still in the same thread.
312 set newthread [get_current_thread "find current thread (2)"]
313 if {$curthread == $newthread} {
314 pass "step with lock does not change thread"
316 fail "step with lock does not change thread (switched to thread $newthread)"
319 set start_args $cont_args
320 set cont_args [get_args]
322 set num_other_threads 0
323 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
324 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
325 if {$i == $curthread} {
326 fail "current thread stepped locked (didn't run)"
329 if {$i == $curthread} {
330 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
331 pass "current thread stepped locked"
333 fail "current thread stepped locked (wrong amount)"
336 incr num_other_threads
340 if {$num_other_threads > 0} {
341 fail "other threads didn't run - step locked"
343 pass "other threads didn't run - step locked"