1 # Copyright (C) 1996, 1997, 2002, 2003, 2007, 2008
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 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
20 # This file was written by Daniel Jacobowitz <drow@mvista.com>
21 # (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
23 # This test covers the various forms of "set scheduler-locking".
32 set testfile "schedlock"
33 set srcfile ${testfile}.c
34 set binfile ${objdir}/${subdir}/${testfile}
36 # The number of threads, including the main thread.
39 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
43 # Now we can proceed with the real testing.
50 set pattern "(\[0-9\]+)"
51 for {set i 1} {[expr $i < $NUM]} {incr i} {
52 append pattern ", (\[0-9\]+)"
55 send_gdb "print args\n"
57 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt"
59 set list_count [expr $list_count + 1]
60 pass "listed args ($list_count)"
63 for {set i 1} {[expr $i <= $NUM]} {incr i} {
64 lappend result $expect_out($i,string)
70 fail "listed args ($list_count) (unknown output)"
74 fail "listed args ($list_count) (timeout)"
79 proc stop_process { description } {
82 # For this to work we must be sure to consume the "Continuing."
83 # message first, or GDB's signal handler may not be in place.
84 after 1000 {send_gdb "\003"}
86 -re "Program received signal SIGINT.*$gdb_prompt $"
92 fail "$description (timeout)"
97 proc get_current_thread { description } {
102 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $"
105 return $expect_out(1,string)
109 fail "$description (unknown output)"
113 fail "$description (timeout)"
118 proc my_continue { msg } {
119 send_gdb "continue\n"
122 { pass "continue ($msg)" }
124 { fail "continue ($msg) (timeout)" }
127 stop_process "stop all threads ($msg)"
129 # Make sure we're in one of the non-main looping threads.
130 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
131 gdb_continue_to_breakpoint "return to loop ($msg)"
135 proc step_ten_loops { msg } {
138 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
142 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
143 pass "step to increment ($msg $i)"
145 -re "$gdb_prompt $" {
146 if {$other_step == 0} {
151 fail "step to increment ($msg $i)"
156 fail "step to increment ($msg $i) (timeout)"
162 # Start with a fresh gdb.
166 gdb_reinitialize_dir $srcdir/$subdir
168 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
169 # run the program and gdb starts saving and restoring tty states.
170 # On Ultrix, we don't need it and it is really slow (because shell_escape
171 # doesn't use vfork).
172 if ![istarget "*-*-ultrix*"] then {
173 gdb_test "shell stty intr '^C'" ""
178 gdb_test "set print sevenbit-strings" ""
179 gdb_test "set width 0" ""
183 # See if scheduler locking is available on this target.
184 send_gdb "set scheduler-locking off\n"
187 -re "Target .* cannot support this command"
189 unsupported "target does not support scheduler locking"
194 pass "scheduler locking set to none"
198 unsupported "target does not support scheduler locking (timeout)"
203 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
204 gdb_continue_to_breakpoint "all threads started"
209 set start_args [get_args]
211 # First make sure that all threads are alive.
212 my_continue "initial"
214 set cont_args [get_args]
217 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
218 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
223 pass "all threads alive"
225 fail "all threads alive ($bad/$NUM did not run)"
228 # We can't change threads, unfortunately, in current GDB. Use
229 # whichever we stopped in.
230 set curthread [get_current_thread "find current thread (1)"]
235 # Test stepping without scheduler locking.
236 gdb_test "set scheduler-locking off" ""
238 step_ten_loops "unlocked"
240 # Make sure we're still in the same thread.
241 set newthread [get_current_thread "find current thread (2)"]
242 if {$curthread == $newthread} {
243 pass "step without lock does not change thread"
245 fail "step without lock does not change thread (switched to thread $newthread)"
248 set start_args $cont_args
249 set cont_args [get_args]
251 set num_other_threads 0
252 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
253 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
254 if {$i == $curthread} {
255 fail "current thread stepped (didn't run)"
258 if {$i == $curthread} {
259 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
260 pass "current thread stepped"
262 fail "current thread stepped (wrong amount)"
265 set num_other_threads [expr $num_other_threads + 1]
269 if {$num_other_threads > 0} {
270 pass "other threads ran - unlocked"
272 fail "other threads ran - unlocked"
275 # Test continue with scheduler locking
276 gdb_test "set scheduler-locking on" ""
278 my_continue "with lock"
280 # Make sure we're still in the same thread.
281 set newthread [get_current_thread "find current thread (3)"]
282 if {$curthread == $newthread} {
283 pass "continue with lock does not change thread"
285 fail "continue with lock does not change thread (switched to thread $newthread)"
288 set start_args $cont_args
289 set cont_args [get_args]
291 set num_other_threads 0
292 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
293 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
294 if {$i == $curthread} {
295 fail "current thread ran (didn't run)"
298 if {$i == $curthread} {
299 pass "current thread ran"
301 incr num_other_threads
305 if {$num_other_threads > 0} {
306 fail "other threads didn't run - locked"
308 pass "other threads didn't run - locked"
311 # Test stepping with scheduler locking
312 step_ten_loops "locked"
314 # Make sure we're still in the same thread.
315 set newthread [get_current_thread "find current thread (2)"]
316 if {$curthread == $newthread} {
317 pass "step with lock does not change thread"
319 fail "step with lock does not change thread (switched to thread $newthread)"
322 set start_args $cont_args
323 set cont_args [get_args]
325 set num_other_threads 0
326 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
327 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
328 if {$i == $curthread} {
329 fail "current thread stepped locked (didn't run)"
332 if {$i == $curthread} {
333 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
334 pass "current thread stepped locked"
336 fail "current thread stepped locked (wrong amount)"
339 incr num_other_threads
343 if {$num_other_threads > 0} {
344 fail "other threads didn't run - step locked"
346 pass "other threads didn't run - step locked"