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".
25 # The number of threads, including the main thread.
28 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
32 # Now we can proceed with the real testing.
39 set pattern "(\[0-9\]+)"
40 for {set i 1} {[expr $i < $NUM]} {incr i} {
41 append pattern ", (\[0-9\]+)"
44 gdb_test_multiple "print args" "listed args ($list_count)" {
45 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
46 set list_count [expr $list_count + 1]
47 pass "listed args ($list_count)"
50 for {set i 1} {[expr $i <= $NUM]} {incr i} {
51 lappend result $expect_out($i,string)
58 proc stop_process { description } {
61 # For this to work we must be sure to consume the "Continuing."
62 # message first, or GDB's signal handler may not be in place.
63 after 1000 {send_gdb "\003"}
65 -re "Program received signal SIGINT.*$gdb_prompt $"
71 fail "$description (timeout)"
76 proc get_current_thread { description } {
79 gdb_test_multiple "bt" "$description" {
80 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
82 return $expect_out(1,string)
88 proc my_continue { msg } {
89 gdb_test_multiple "continue" "continuing ($msg)" {
91 pass "continue ($msg)"
95 stop_process "stop all threads ($msg)"
97 # Make sure we're in one of the non-main looping threads.
98 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
99 gdb_continue_to_breakpoint "return to loop ($msg)"
103 proc step_ten_loops { msg } {
106 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
108 gdb_test_multiple "step" "step to increment ($msg $i)" {
109 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
110 pass "step to increment ($msg $i)"
112 -re "$gdb_prompt $" {
113 if {$other_step == 0} {
118 fail "step to increment ($msg $i)"
126 # Start with a fresh gdb.
130 gdb_reinitialize_dir $srcdir/$subdir
132 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
133 # run the program and gdb starts saving and restoring tty states.
134 # On Ultrix, we don't need it and it is really slow (because shell_escape
135 # doesn't use vfork).
136 if ![istarget "*-*-ultrix*"] then {
137 gdb_test "shell stty intr '^C'" ".*"
142 gdb_test_no_output "set print sevenbit-strings"
143 gdb_test_no_output "set width 0"
147 # See if scheduler locking is available on this target.
149 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
150 -re "Target .* cannot support this command" {
151 unsupported "target does not support scheduler locking"
154 -re "$gdb_prompt $" {
155 pass "scheduler locking set to none"
158 unsupported "target does not support scheduler locking (timeout)"
163 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
164 gdb_continue_to_breakpoint "all threads started"
169 set start_args [get_args]
171 # First make sure that all threads are alive.
172 my_continue "initial"
174 set cont_args [get_args]
177 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
178 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
183 pass "all threads alive"
185 fail "all threads alive ($bad/$NUM did not run)"
188 # We can't change threads, unfortunately, in current GDB. Use
189 # whichever we stopped in.
190 set curthread [get_current_thread "find current thread (1)"]
195 # Test stepping without scheduler locking.
196 gdb_test_no_output "set scheduler-locking off"
198 step_ten_loops "unlocked"
200 # Make sure we're still in the same thread.
201 set newthread [get_current_thread "find current thread (2)"]
202 if {$curthread == $newthread} {
203 pass "step without lock does not change thread"
205 fail "step without lock does not change thread (switched to thread $newthread)"
208 set start_args $cont_args
209 set cont_args [get_args]
211 set num_other_threads 0
212 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
213 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
214 if {$i == $curthread} {
215 fail "current thread stepped (didn't run)"
218 if {$i == $curthread} {
219 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
220 pass "current thread stepped"
222 fail "current thread stepped (wrong amount)"
225 set num_other_threads [expr $num_other_threads + 1]
229 if {$num_other_threads > 0} {
230 pass "other threads ran - unlocked"
232 fail "other threads ran - unlocked"
235 # Test continue with scheduler locking
236 gdb_test "set scheduler-locking on" ""
238 my_continue "with lock"
240 # Make sure we're still in the same thread.
241 set newthread [get_current_thread "find current thread (3)"]
242 if {$curthread == $newthread} {
243 pass "continue with lock does not change thread"
245 fail "continue with 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 ran (didn't run)"
258 if {$i == $curthread} {
259 pass "current thread ran"
261 incr num_other_threads
265 if {$num_other_threads > 0} {
266 fail "other threads didn't run - locked"
268 pass "other threads didn't run - locked"
271 # Test stepping with scheduler locking
272 step_ten_loops "locked"
274 # Make sure we're still in the same thread.
275 set newthread [get_current_thread "find current thread (2)"]
276 if {$curthread == $newthread} {
277 pass "step with lock does not change thread"
279 fail "step with lock does not change thread (switched to thread $newthread)"
282 set start_args $cont_args
283 set cont_args [get_args]
285 set num_other_threads 0
286 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
287 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
288 if {$i == $curthread} {
289 fail "current thread stepped locked (didn't run)"
292 if {$i == $curthread} {
293 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
294 pass "current thread stepped locked"
296 fail "current thread stepped locked (wrong amount)"
299 incr num_other_threads
303 if {$num_other_threads > 0} {
304 fail "other threads didn't run - step locked"
306 pass "other threads didn't run - step locked"