]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/schedlock.exp
2010-05-26 Michael Snyder <msnyder@msnyder-server.eng.vmware.com>
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / schedlock.exp
CommitLineData
4c38e0a4 1# Copyright (C) 1996, 1997, 2002, 2003, 2007, 2008, 2009, 2010
9b254dd1 2# Free Software Foundation, Inc.
0312286c
DJ
3
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
e22f8b7c 6# the Free Software Foundation; either version 3 of the License, or
0312286c 7# (at your option) any later version.
e22f8b7c 8#
0312286c
DJ
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.
e22f8b7c 13#
0312286c 14# You should have received a copy of the GNU General Public License
e22f8b7c 15# along with this program. If not, see <http://www.gnu.org/licenses/>.
0312286c 16
0312286c
DJ
17# This file was written by Daniel Jacobowitz <drow@mvista.com>
18# (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
19#
20# This test covers the various forms of "set scheduler-locking".
21
22if $tracelevel then {
23 strace $tracelevel
24}
25
0312286c
DJ
26
27set testfile "schedlock"
28set srcfile ${testfile}.c
29set binfile ${objdir}/${subdir}/${testfile}
30
18ecae38
DJ
31# The number of threads, including the main thread.
32set NUM 2
33
b5ab8ff3 34if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
0312286c
DJ
35 return -1
36}
37
38# Now we can proceed with the real testing.
39
40proc get_args { } {
41 global list_count
42 global gdb_prompt
e09490f1
DJ
43 global NUM
44
45 set pattern "(\[0-9\]+)"
46 for {set i 1} {[expr $i < $NUM]} {incr i} {
47 append pattern ", (\[0-9\]+)"
48 }
0312286c
DJ
49
50 send_gdb "print args\n"
51 gdb_expect {
e09490f1 52 -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt"
0312286c
DJ
53 {
54 set list_count [expr $list_count + 1]
55 pass "listed args ($list_count)"
e09490f1
DJ
56
57 set result ""
58 for {set i 1} {[expr $i <= $NUM]} {incr i} {
59 lappend result $expect_out($i,string)
60 }
61 return $result
0312286c
DJ
62 }
63 -re "$gdb_prompt"
64 {
65 fail "listed args ($list_count) (unknown output)"
66 }
67 timeout
68 {
69 fail "listed args ($list_count) (timeout)"
70 }
71 }
72}
73
74proc stop_process { description } {
75 global gdb_prompt
76
77 # For this to work we must be sure to consume the "Continuing."
78 # message first, or GDB's signal handler may not be in place.
79 after 1000 {send_gdb "\003"}
80 gdb_expect {
81 -re "Program received signal SIGINT.*$gdb_prompt $"
82 {
83 pass $description
84 }
85 timeout
86 {
87 fail "$description (timeout)"
88 }
89 }
90}
91
92proc get_current_thread { description } {
93 global gdb_prompt
94
95 send_gdb "bt\n"
96 gdb_expect {
97 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $"
98 {
99 pass $description
100 return $expect_out(1,string)
101 }
102 -re "$gdb_prompt $"
103 {
104 fail "$description (unknown output)"
105 }
106 timeout
107 {
108 fail "$description (timeout)"
109 }
110 }
111}
112
113proc my_continue { msg } {
114 send_gdb "continue\n"
115 gdb_expect {
116 -re "Continuing"
117 { pass "continue ($msg)" }
118 timeout
119 { fail "continue ($msg) (timeout)" }
120 }
121
122 stop_process "stop all threads ($msg)"
123
a25fbfec 124 # Make sure we're in one of the non-main looping threads.
18ecae38 125 gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
0312286c
DJ
126 gdb_continue_to_breakpoint "return to loop ($msg)"
127 delete_breakpoints
128}
129
130proc step_ten_loops { msg } {
131 global gdb_prompt
132
133 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
134 send_gdb "step\n"
135 set other_step 0
136 gdb_expect {
137 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
138 pass "step to increment ($msg $i)"
139 }
140 -re "$gdb_prompt $" {
141 if {$other_step == 0} {
142 set other_step 1
143 send_gdb "step\n"
144 exp_continue
145 } else {
146 fail "step to increment ($msg $i)"
147 # FIXME cascade?
148 }
149 }
150 timeout {
151 fail "step to increment ($msg $i) (timeout)"
152 }
153 }
154 }
155}
156
157# Start with a fresh gdb.
158
159gdb_exit
160gdb_start
161gdb_reinitialize_dir $srcdir/$subdir
162
163# We'll need this when we send_gdb a ^C to GDB. Need to do it before we
164# run the program and gdb starts saving and restoring tty states.
165# On Ultrix, we don't need it and it is really slow (because shell_escape
166# doesn't use vfork).
167if ![istarget "*-*-ultrix*"] then {
168 gdb_test "shell stty intr '^C'" ""
169}
170
171gdb_load ${binfile}
172
173gdb_test "set print sevenbit-strings" ""
174gdb_test "set width 0" ""
175
176runto_main
177
178# See if scheduler locking is available on this target.
179send_gdb "set scheduler-locking off\n"
180global gdb_prompt
181gdb_expect {
182 -re "Target .* cannot support this command"
183 {
184 unsupported "target does not support scheduler locking"
185 return
186 }
187 -re "$gdb_prompt $"
188 {
189 pass "scheduler locking set to none"
190 }
191 timeout
192 {
193 unsupported "target does not support scheduler locking (timeout)"
194 return
195 }
196}
197
198gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
199gdb_continue_to_breakpoint "all threads started"
200
201global list_count
202set list_count 0
203
204set start_args [get_args]
205
206# First make sure that all threads are alive.
207my_continue "initial"
208
209set cont_args [get_args]
210
e09490f1 211set bad 0
18ecae38 212for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
0312286c 213 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
e09490f1 214 incr bad
0312286c
DJ
215 }
216}
e09490f1 217if { $bad == 0 } {
18ecae38
DJ
218 pass "all threads alive"
219} else {
e09490f1 220 fail "all threads alive ($bad/$NUM did not run)"
18ecae38 221}
0312286c
DJ
222
223# We can't change threads, unfortunately, in current GDB. Use
224# whichever we stopped in.
225set curthread [get_current_thread "find current thread (1)"]
226
227
228
229
230# Test stepping without scheduler locking.
231gdb_test "set scheduler-locking off" ""
232
233step_ten_loops "unlocked"
234
235# Make sure we're still in the same thread.
236set newthread [get_current_thread "find current thread (2)"]
237if {$curthread == $newthread} {
238 pass "step without lock does not change thread"
239} else {
240 fail "step without lock does not change thread (switched to thread $newthread)"
241}
242
243set start_args $cont_args
244set cont_args [get_args]
245
a25fbfec 246set num_other_threads 0
18ecae38 247for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
0312286c
DJ
248 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
249 if {$i == $curthread} {
250 fail "current thread stepped (didn't run)"
0312286c
DJ
251 }
252 } else {
253 if {$i == $curthread} {
254 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
255 pass "current thread stepped"
256 } else {
257 fail "current thread stepped (wrong amount)"
258 }
259 } else {
a25fbfec 260 set num_other_threads [expr $num_other_threads + 1]
0312286c
DJ
261 }
262 }
263}
a25fbfec 264if {$num_other_threads > 0} {
18ecae38 265 pass "other threads ran - unlocked"
a25fbfec 266} else {
18ecae38 267 fail "other threads ran - unlocked"
a25fbfec 268}
0312286c
DJ
269
270# Test continue with scheduler locking
271gdb_test "set scheduler-locking on" ""
272
273my_continue "with lock"
274
275# Make sure we're still in the same thread.
276set newthread [get_current_thread "find current thread (3)"]
277if {$curthread == $newthread} {
278 pass "continue with lock does not change thread"
279} else {
280 fail "continue with lock does not change thread (switched to thread $newthread)"
281}
282
283set start_args $cont_args
284set cont_args [get_args]
285
18ecae38
DJ
286set num_other_threads 0
287for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
0312286c
DJ
288 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
289 if {$i == $curthread} {
290 fail "current thread ran (didn't run)"
0312286c
DJ
291 }
292 } else {
293 if {$i == $curthread} {
294 pass "current thread ran"
295 } else {
18ecae38 296 incr num_other_threads
0312286c
DJ
297 }
298 }
299}
18ecae38
DJ
300if {$num_other_threads > 0} {
301 fail "other threads didn't run - locked"
302} else {
303 pass "other threads didn't run - locked"
304}
0312286c
DJ
305
306# Test stepping with scheduler locking
307step_ten_loops "locked"
308
309# Make sure we're still in the same thread.
310set newthread [get_current_thread "find current thread (2)"]
311if {$curthread == $newthread} {
312 pass "step with lock does not change thread"
313} else {
314 fail "step with lock does not change thread (switched to thread $newthread)"
315}
316
317set start_args $cont_args
318set cont_args [get_args]
319
18ecae38
DJ
320set num_other_threads 0
321for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
0312286c
DJ
322 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
323 if {$i == $curthread} {
324 fail "current thread stepped locked (didn't run)"
0312286c
DJ
325 }
326 } else {
327 if {$i == $curthread} {
328 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
329 pass "current thread stepped locked"
330 } else {
331 fail "current thread stepped locked (wrong amount)"
332 }
333 } else {
18ecae38 334 incr num_other_threads
0312286c
DJ
335 }
336 }
337}
18ecae38
DJ
338if {$num_other_threads > 0} {
339 fail "other threads didn't run - step locked"
340} else {
341 pass "other threads didn't run - step locked"
342}
0312286c
DJ
343
344return 0