1 # Copyright (C) 1996, 1997, 2002, 2003, 2007 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 # Please email any bugs, comments, and/or additions to this file to:
17 # bug-gdb@prep.ai.mit.edu
19 # This file was written by Daniel Jacobowitz <drow@mvista.com>
20 # (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
22 # This test covers the various forms of "set scheduler-locking".
31 set testfile "schedlock"
32 set srcfile ${testfile}.c
33 set binfile ${objdir}/${subdir}/${testfile}
35 # The number of threads, including the main thread.
38 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
42 # Now we can proceed with the real testing.
48 send_gdb "print args\n"
50 -re "\\\$\[0-9\]+ = {(\[0-9\]+), (\[0-9\]+)}.*$gdb_prompt"
52 set list_count [expr $list_count + 1]
53 pass "listed args ($list_count)"
54 return [list $expect_out(1,string) $expect_out(2,string)]
58 fail "listed args ($list_count) (unknown output)"
62 fail "listed args ($list_count) (timeout)"
67 proc stop_process { description } {
70 # For this to work we must be sure to consume the "Continuing."
71 # message first, or GDB's signal handler may not be in place.
72 after 1000 {send_gdb "\003"}
74 -re "Program received signal SIGINT.*$gdb_prompt $"
80 fail "$description (timeout)"
85 proc get_current_thread { description } {
90 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $"
93 return $expect_out(1,string)
97 fail "$description (unknown output)"
101 fail "$description (timeout)"
106 proc my_continue { msg } {
107 send_gdb "continue\n"
110 { pass "continue ($msg)" }
112 { fail "continue ($msg) (timeout)" }
115 stop_process "stop all threads ($msg)"
117 # Make sure we're in one of the non-main looping threads.
118 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
119 gdb_continue_to_breakpoint "return to loop ($msg)"
123 proc step_ten_loops { msg } {
126 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
130 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
131 pass "step to increment ($msg $i)"
133 -re "$gdb_prompt $" {
134 if {$other_step == 0} {
139 fail "step to increment ($msg $i)"
144 fail "step to increment ($msg $i) (timeout)"
150 # Start with a fresh gdb.
154 gdb_reinitialize_dir $srcdir/$subdir
156 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
157 # run the program and gdb starts saving and restoring tty states.
158 # On Ultrix, we don't need it and it is really slow (because shell_escape
159 # doesn't use vfork).
160 if ![istarget "*-*-ultrix*"] then {
161 gdb_test "shell stty intr '^C'" ""
166 gdb_test "set print sevenbit-strings" ""
167 gdb_test "set width 0" ""
171 # See if scheduler locking is available on this target.
172 send_gdb "set scheduler-locking off\n"
175 -re "Target .* cannot support this command"
177 unsupported "target does not support scheduler locking"
182 pass "scheduler locking set to none"
186 unsupported "target does not support scheduler locking (timeout)"
191 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
192 gdb_continue_to_breakpoint "all threads started"
197 set start_args [get_args]
199 # First make sure that all threads are alive.
200 my_continue "initial"
202 set cont_args [get_args]
205 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
206 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
211 pass "all threads alive"
213 fail "all threads alive"
216 # We can't change threads, unfortunately, in current GDB. Use
217 # whichever we stopped in.
218 set curthread [get_current_thread "find current thread (1)"]
223 # Test stepping without scheduler locking.
224 gdb_test "set scheduler-locking off" ""
226 step_ten_loops "unlocked"
228 # Make sure we're still in the same thread.
229 set newthread [get_current_thread "find current thread (2)"]
230 if {$curthread == $newthread} {
231 pass "step without lock does not change thread"
233 fail "step without lock does not change thread (switched to thread $newthread)"
236 set start_args $cont_args
237 set cont_args [get_args]
239 set num_other_threads 0
240 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
241 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
242 if {$i == $curthread} {
243 fail "current thread stepped (didn't run)"
246 if {$i == $curthread} {
247 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
248 pass "current thread stepped"
250 fail "current thread stepped (wrong amount)"
253 set num_other_threads [expr $num_other_threads + 1]
257 if {$num_other_threads > 0} {
258 pass "other threads ran - unlocked"
260 fail "other threads ran - unlocked"
263 # Test continue with scheduler locking
264 gdb_test "set scheduler-locking on" ""
266 my_continue "with lock"
268 # Make sure we're still in the same thread.
269 set newthread [get_current_thread "find current thread (3)"]
270 if {$curthread == $newthread} {
271 pass "continue with lock does not change thread"
273 fail "continue with lock does not change thread (switched to thread $newthread)"
276 set start_args $cont_args
277 set cont_args [get_args]
279 set num_other_threads 0
280 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
281 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
282 if {$i == $curthread} {
283 fail "current thread ran (didn't run)"
286 if {$i == $curthread} {
287 pass "current thread ran"
289 incr num_other_threads
293 if {$num_other_threads > 0} {
294 fail "other threads didn't run - locked"
296 pass "other threads didn't run - locked"
299 # Test stepping with scheduler locking
300 step_ten_loops "locked"
302 # Make sure we're still in the same thread.
303 set newthread [get_current_thread "find current thread (2)"]
304 if {$curthread == $newthread} {
305 pass "step with lock does not change thread"
307 fail "step with lock does not change thread (switched to thread $newthread)"
310 set start_args $cont_args
311 set cont_args [get_args]
313 set num_other_threads 0
314 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
315 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
316 if {$i == $curthread} {
317 fail "current thread stepped locked (didn't run)"
320 if {$i == $curthread} {
321 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
322 pass "current thread stepped locked"
324 fail "current thread stepped locked (wrong amount)"
327 incr num_other_threads
331 if {$num_other_threads > 0} {
332 fail "other threads didn't run - step locked"
334 pass "other threads didn't run - step locked"