1 # Copyright (C) 1996-1997, 2002-2003, 2007-2012 Free Software
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".
23 set testfile "schedlock"
24 set srcfile ${testfile}.c
25 set binfile ${objdir}/${subdir}/${testfile}
27 # The number of threads, including the main thread.
30 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
34 # Now we can proceed with the real testing.
41 set pattern "(\[0-9\]+)"
42 for {set i 1} {[expr $i < $NUM]} {incr i} {
43 append pattern ", (\[0-9\]+)"
46 gdb_test_multiple "print args" "listed args ($list_count)" {
47 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
48 set list_count [expr $list_count + 1]
49 pass "listed args ($list_count)"
52 for {set i 1} {[expr $i <= $NUM]} {incr i} {
53 lappend result $expect_out($i,string)
60 proc stop_process { description } {
63 # For this to work we must be sure to consume the "Continuing."
64 # message first, or GDB's signal handler may not be in place.
65 after 1000 {send_gdb "\003"}
67 -re "Program received signal SIGINT.*$gdb_prompt $"
73 fail "$description (timeout)"
78 proc get_current_thread { description } {
81 gdb_test_multiple "bt" "$description" {
82 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
84 return $expect_out(1,string)
90 proc my_continue { msg } {
91 gdb_test_multiple "continue" "continuing ($msg)" {
93 pass "continue ($msg)"
97 stop_process "stop all threads ($msg)"
99 # Make sure we're in one of the non-main looping threads.
100 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
101 gdb_continue_to_breakpoint "return to loop ($msg)"
105 proc step_ten_loops { msg } {
108 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
110 gdb_test_multiple "step" "step to increment ($msg $i)" {
111 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
112 pass "step to increment ($msg $i)"
114 -re "$gdb_prompt $" {
115 if {$other_step == 0} {
120 fail "step to increment ($msg $i)"
128 # Start with a fresh gdb.
132 gdb_reinitialize_dir $srcdir/$subdir
134 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
135 # run the program and gdb starts saving and restoring tty states.
136 # On Ultrix, we don't need it and it is really slow (because shell_escape
137 # doesn't use vfork).
138 if ![istarget "*-*-ultrix*"] then {
139 gdb_test "shell stty intr '^C'" ".*"
144 gdb_test_no_output "set print sevenbit-strings"
145 gdb_test_no_output "set width 0"
149 # See if scheduler locking is available on this target.
151 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
152 -re "Target .* cannot support this command" {
153 unsupported "target does not support scheduler locking"
156 -re "$gdb_prompt $" {
157 pass "scheduler locking set to none"
160 unsupported "target does not support scheduler locking (timeout)"
165 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
166 gdb_continue_to_breakpoint "all threads started"
171 set start_args [get_args]
173 # First make sure that all threads are alive.
174 my_continue "initial"
176 set cont_args [get_args]
179 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
180 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
185 pass "all threads alive"
187 fail "all threads alive ($bad/$NUM did not run)"
190 # We can't change threads, unfortunately, in current GDB. Use
191 # whichever we stopped in.
192 set curthread [get_current_thread "find current thread (1)"]
197 # Test stepping without scheduler locking.
198 gdb_test_no_output "set scheduler-locking off"
200 step_ten_loops "unlocked"
202 # Make sure we're still in the same thread.
203 set newthread [get_current_thread "find current thread (2)"]
204 if {$curthread == $newthread} {
205 pass "step without lock does not change thread"
207 fail "step without lock does not change thread (switched to thread $newthread)"
210 set start_args $cont_args
211 set cont_args [get_args]
213 set num_other_threads 0
214 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
215 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
216 if {$i == $curthread} {
217 fail "current thread stepped (didn't run)"
220 if {$i == $curthread} {
221 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
222 pass "current thread stepped"
224 fail "current thread stepped (wrong amount)"
227 set num_other_threads [expr $num_other_threads + 1]
231 if {$num_other_threads > 0} {
232 pass "other threads ran - unlocked"
234 fail "other threads ran - unlocked"
237 # Test continue with scheduler locking
238 gdb_test "set scheduler-locking on" ""
240 my_continue "with lock"
242 # Make sure we're still in the same thread.
243 set newthread [get_current_thread "find current thread (3)"]
244 if {$curthread == $newthread} {
245 pass "continue with lock does not change thread"
247 fail "continue with lock does not change thread (switched to thread $newthread)"
250 set start_args $cont_args
251 set cont_args [get_args]
253 set num_other_threads 0
254 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
255 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
256 if {$i == $curthread} {
257 fail "current thread ran (didn't run)"
260 if {$i == $curthread} {
261 pass "current thread ran"
263 incr num_other_threads
267 if {$num_other_threads > 0} {
268 fail "other threads didn't run - locked"
270 pass "other threads didn't run - locked"
273 # Test stepping with scheduler locking
274 step_ten_loops "locked"
276 # Make sure we're still in the same thread.
277 set newthread [get_current_thread "find current thread (2)"]
278 if {$curthread == $newthread} {
279 pass "step with lock does not change thread"
281 fail "step with lock does not change thread (switched to thread $newthread)"
284 set start_args $cont_args
285 set cont_args [get_args]
287 set num_other_threads 0
288 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
289 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
290 if {$i == $curthread} {
291 fail "current thread stepped locked (didn't run)"
294 if {$i == $curthread} {
295 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
296 pass "current thread stepped locked"
298 fail "current thread stepped locked (wrong amount)"
301 incr num_other_threads
305 if {$num_other_threads > 0} {
306 fail "other threads didn't run - step locked"
308 pass "other threads didn't run - step locked"