Imported Upstream version 7.5
[platform/upstream/gdb.git] / gdb / testsuite / gdb.threads / schedlock.exp
1 # Copyright (C) 1996-1997, 2002-2003, 2007-2012 Free Software
2 # Foundation, Inc.
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
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
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.
13 #
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/>.
16
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
22
23 standard_testfile
24
25 # The number of threads, including the main thread.
26 set NUM 2
27
28 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
29     return -1
30 }
31
32 # Now we can proceed with the real testing.
33
34 proc get_args { } {
35     global list_count
36     global gdb_prompt
37     global NUM
38
39     set pattern "(\[0-9\]+)"
40     for {set i 1} {[expr $i < $NUM]} {incr i} {
41         append pattern ", (\[0-9\]+)"
42     }
43
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)"
48
49             set result ""
50             for {set i 1} {[expr $i <= $NUM]} {incr i} {
51                 lappend result $expect_out($i,string)
52             }
53             return $result
54         }
55     }
56 }
57
58 proc stop_process { description } {
59   global gdb_prompt
60
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"}
64   gdb_expect {
65     -re "Program received signal SIGINT.*$gdb_prompt $"
66       {
67         pass $description
68       }
69     timeout
70       {
71         fail "$description (timeout)"
72       }
73   }
74 }
75
76 proc get_current_thread { description } {
77     global gdb_prompt
78
79     gdb_test_multiple "bt" "$description" {
80         -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
81             pass $description
82             return $expect_out(1,string)
83         }
84     }
85     return ""
86 }
87
88 proc my_continue { msg } {
89     gdb_test_multiple "continue" "continuing ($msg)" {
90         -re "Continuing" {
91             pass "continue ($msg)"
92         }
93     }
94
95     stop_process "stop all threads ($msg)"
96
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)"
100     delete_breakpoints
101 }
102
103 proc step_ten_loops { msg } {
104     global gdb_prompt
105
106     for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
107         set other_step 0
108         gdb_test_multiple "step" "step to increment ($msg $i)" {
109             -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
110                 pass "step to increment ($msg $i)"
111             }
112             -re "$gdb_prompt $" {
113                 if {$other_step == 0} {
114                     set other_step 1
115                     send_gdb "step\n"
116                     exp_continue
117                 } else {
118                     fail "step to increment ($msg $i)"
119                     # FIXME cascade?
120                 }
121             }
122         }
123     }
124 }
125
126 # Start with a fresh gdb.
127
128 gdb_exit
129 gdb_start
130 gdb_reinitialize_dir $srcdir/$subdir
131
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'" ".*"
138 }
139
140 gdb_load ${binfile}
141
142 gdb_test_no_output "set print sevenbit-strings"
143 gdb_test_no_output "set width 0"
144
145 runto_main
146
147 # See if scheduler locking is available on this target.
148 global gdb_prompt
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"
152         return
153     }
154     -re "$gdb_prompt $" {
155         pass "scheduler locking set to none"
156     }
157     timeout {
158         unsupported "target does not support scheduler locking (timeout)"
159         return
160     }
161 }
162
163 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
164 gdb_continue_to_breakpoint "all threads started"
165
166 global list_count
167 set list_count 0
168
169 set start_args [get_args]
170
171 # First make sure that all threads are alive.
172 my_continue "initial"
173
174 set cont_args [get_args]
175
176 set bad 0
177 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
178   if {[lindex $start_args $i] == [lindex $cont_args $i]} {
179     incr bad
180   }
181 }
182 if { $bad == 0 } {
183   pass "all threads alive"
184 } else {
185   fail "all threads alive ($bad/$NUM did not run)"
186 }
187
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)"]
191
192
193
194
195 # Test stepping without scheduler locking.
196 gdb_test_no_output "set scheduler-locking off"
197
198 step_ten_loops "unlocked"
199
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"
204 } else {
205     fail "step without lock does not change thread (switched to thread $newthread)"
206 }
207
208 set start_args $cont_args
209 set cont_args [get_args]
210
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)"
216     }
217   } else {
218     if {$i == $curthread} {
219         if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
220             pass "current thread stepped"
221         } else {
222             fail "current thread stepped (wrong amount)"
223         }
224     } else {
225       set num_other_threads [expr $num_other_threads + 1]
226     }
227   }
228 }
229 if {$num_other_threads > 0} {
230   pass "other threads ran - unlocked"
231 } else {
232   fail "other threads ran - unlocked"
233 }
234
235 # Test continue with scheduler locking
236 gdb_test "set scheduler-locking on" ""
237
238 my_continue "with lock"
239
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"
244 } else {
245     fail "continue with lock does not change thread (switched to thread $newthread)"
246 }
247
248 set start_args $cont_args
249 set cont_args [get_args]
250
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)"
256     }
257   } else {
258     if {$i == $curthread} {
259       pass "current thread ran"
260     } else {
261       incr num_other_threads
262     }
263   }
264 }
265 if {$num_other_threads > 0} {
266   fail "other threads didn't run - locked"
267 } else {
268   pass "other threads didn't run - locked"
269 }
270
271 # Test stepping with scheduler locking
272 step_ten_loops "locked"
273
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"
278 } else {
279     fail "step with lock does not change thread (switched to thread $newthread)"
280 }
281
282 set start_args $cont_args
283 set cont_args [get_args]
284
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)"
290     }
291   } else {
292     if {$i == $curthread} {
293         if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
294             pass "current thread stepped locked"
295         } else {
296             fail "current thread stepped locked (wrong amount)"
297         }
298     } else {
299       incr num_other_threads
300     }
301   }
302 }
303 if {$num_other_threads > 0} {
304   fail "other threads didn't run - step locked"
305 } else {
306   pass "other threads didn't run - step locked"
307 }
308
309 return 0