* schedlock.c (NUM): Change to 1.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / schedlock.exp
1 # Copyright (C) 1996, 1997, 2002, 2003, 2007 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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/>.
15
16 # Please email any bugs, comments, and/or additions to this file to:
17 # bug-gdb@prep.ai.mit.edu
18
19 # This file was written by Daniel Jacobowitz <drow@mvista.com>
20 # (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
21 #
22 # This test covers the various forms of "set scheduler-locking".
23
24 if $tracelevel then {
25 strace $tracelevel
26 }
27
28 set prms_id 0
29 set bug_id 0
30
31 set testfile "schedlock"
32 set srcfile ${testfile}.c
33 set binfile ${objdir}/${subdir}/${testfile}
34
35 # The number of threads, including the main thread.
36 set NUM 2
37
38 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
39 return -1
40 }
41
42 # Now we can proceed with the real testing.
43
44 proc get_args { } {
45 global list_count
46 global gdb_prompt
47
48 send_gdb "print args\n"
49 gdb_expect {
50 -re "\\\$\[0-9\]+ = {(\[0-9\]+), (\[0-9\]+)}.*$gdb_prompt"
51 {
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)]
55 }
56 -re "$gdb_prompt"
57 {
58 fail "listed args ($list_count) (unknown output)"
59 }
60 timeout
61 {
62 fail "listed args ($list_count) (timeout)"
63 }
64 }
65 }
66
67 proc stop_process { description } {
68 global gdb_prompt
69
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"}
73 gdb_expect {
74 -re "Program received signal SIGINT.*$gdb_prompt $"
75 {
76 pass $description
77 }
78 timeout
79 {
80 fail "$description (timeout)"
81 }
82 }
83 }
84
85 proc get_current_thread { description } {
86 global gdb_prompt
87
88 send_gdb "bt\n"
89 gdb_expect {
90 -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $"
91 {
92 pass $description
93 return $expect_out(1,string)
94 }
95 -re "$gdb_prompt $"
96 {
97 fail "$description (unknown output)"
98 }
99 timeout
100 {
101 fail "$description (timeout)"
102 }
103 }
104 }
105
106 proc my_continue { msg } {
107 send_gdb "continue\n"
108 gdb_expect {
109 -re "Continuing"
110 { pass "continue ($msg)" }
111 timeout
112 { fail "continue ($msg) (timeout)" }
113 }
114
115 stop_process "stop all threads ($msg)"
116
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)"
120 delete_breakpoints
121 }
122
123 proc step_ten_loops { msg } {
124 global gdb_prompt
125
126 for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
127 send_gdb "step\n"
128 set other_step 0
129 gdb_expect {
130 -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
131 pass "step to increment ($msg $i)"
132 }
133 -re "$gdb_prompt $" {
134 if {$other_step == 0} {
135 set other_step 1
136 send_gdb "step\n"
137 exp_continue
138 } else {
139 fail "step to increment ($msg $i)"
140 # FIXME cascade?
141 }
142 }
143 timeout {
144 fail "step to increment ($msg $i) (timeout)"
145 }
146 }
147 }
148 }
149
150 # Start with a fresh gdb.
151
152 gdb_exit
153 gdb_start
154 gdb_reinitialize_dir $srcdir/$subdir
155
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'" ""
162 }
163
164 gdb_load ${binfile}
165
166 gdb_test "set print sevenbit-strings" ""
167 gdb_test "set width 0" ""
168
169 runto_main
170
171 # See if scheduler locking is available on this target.
172 send_gdb "set scheduler-locking off\n"
173 global gdb_prompt
174 gdb_expect {
175 -re "Target .* cannot support this command"
176 {
177 unsupported "target does not support scheduler locking"
178 return
179 }
180 -re "$gdb_prompt $"
181 {
182 pass "scheduler locking set to none"
183 }
184 timeout
185 {
186 unsupported "target does not support scheduler locking (timeout)"
187 return
188 }
189 }
190
191 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
192 gdb_continue_to_breakpoint "all threads started"
193
194 global list_count
195 set list_count 0
196
197 set start_args [get_args]
198
199 # First make sure that all threads are alive.
200 my_continue "initial"
201
202 set cont_args [get_args]
203
204 set ok 1
205 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
206 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
207 set ok 0
208 }
209 }
210 if { $ok } {
211 pass "all threads alive"
212 } else {
213 fail "all threads alive"
214 }
215
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)"]
219
220
221
222
223 # Test stepping without scheduler locking.
224 gdb_test "set scheduler-locking off" ""
225
226 step_ten_loops "unlocked"
227
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"
232 } else {
233 fail "step without lock does not change thread (switched to thread $newthread)"
234 }
235
236 set start_args $cont_args
237 set cont_args [get_args]
238
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)"
244 }
245 } else {
246 if {$i == $curthread} {
247 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
248 pass "current thread stepped"
249 } else {
250 fail "current thread stepped (wrong amount)"
251 }
252 } else {
253 set num_other_threads [expr $num_other_threads + 1]
254 }
255 }
256 }
257 if {$num_other_threads > 0} {
258 pass "other threads ran - unlocked"
259 } else {
260 fail "other threads ran - unlocked"
261 }
262
263 # Test continue with scheduler locking
264 gdb_test "set scheduler-locking on" ""
265
266 my_continue "with lock"
267
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"
272 } else {
273 fail "continue with lock does not change thread (switched to thread $newthread)"
274 }
275
276 set start_args $cont_args
277 set cont_args [get_args]
278
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)"
284 }
285 } else {
286 if {$i == $curthread} {
287 pass "current thread ran"
288 } else {
289 incr num_other_threads
290 }
291 }
292 }
293 if {$num_other_threads > 0} {
294 fail "other threads didn't run - locked"
295 } else {
296 pass "other threads didn't run - locked"
297 }
298
299 # Test stepping with scheduler locking
300 step_ten_loops "locked"
301
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"
306 } else {
307 fail "step with lock does not change thread (switched to thread $newthread)"
308 }
309
310 set start_args $cont_args
311 set cont_args [get_args]
312
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)"
318 }
319 } else {
320 if {$i == $curthread} {
321 if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
322 pass "current thread stepped locked"
323 } else {
324 fail "current thread stepped locked (wrong amount)"
325 }
326 } else {
327 incr num_other_threads
328 }
329 }
330 }
331 if {$num_other_threads > 0} {
332 fail "other threads didn't run - step locked"
333 } else {
334 pass "other threads didn't run - step locked"
335 }
336
337 return 0
This page took 0.038187 seconds and 5 git commands to generate.