PR 17408 - assertion failure in switch_back_to_stepped_thread
[external/binutils.git] / gdb / testsuite / gdb.threads / schedlock.exp
1 # Copyright (C) 1996-2014 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 # This file was written by Daniel Jacobowitz <drow@mvista.com>
17 # (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
18 #
19 # This test covers the various forms of "set scheduler-locking".
20
21
22 standard_testfile
23
24 # The number of threads, including the main thread.
25 set NUM 2
26
27 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
28     return -1
29 }
30
31 # Now we can proceed with the real testing.
32
33 # Get the current contents of the `args` array in the test program.
34 # Description is appended to the test message.
35
36 proc get_args { description } {
37     global gdb_prompt
38     global NUM
39
40     set pattern "(\[0-9\]+)"
41     for {set i 1} {[expr $i < $NUM]} {incr i} {
42         append pattern ", (\[0-9\]+)"
43     }
44
45     set test "listed args ($description)"
46     gdb_test_multiple "print args" $test {
47         -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
48             pass $test
49
50             set result ""
51             for {set i 1} {[expr $i <= $NUM]} {incr i} {
52                 lappend result $expect_out($i,string)
53             }
54             return $result
55         }
56     }
57 }
58
59 proc stop_process { description } {
60   global gdb_prompt
61
62   # For this to work we must be sure to consume the "Continuing."
63   # message first, or GDB's signal handler may not be in place.
64   after 1000 {send_gdb "\003"}
65   gdb_expect {
66     -re "Program received signal SIGINT.*$gdb_prompt $"
67       {
68         pass $description
69       }
70     timeout
71       {
72         fail "$description (timeout)"
73       }
74   }
75 }
76
77 proc get_current_thread { description } {
78     global gdb_prompt
79
80     set test "find current thread ($description)"
81
82     gdb_test_multiple "bt" $test {
83         -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
84             pass $test
85             return $expect_out(1,string)
86         }
87     }
88     return ""
89 }
90
91 # Make sure we're stopped in the loop, in one of the non-main threads.
92
93 proc goto_loop { msg } {
94     gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
95
96     set test "return to loop"
97     if {$msg != ""} {
98         set test "$test ($msg)"
99     }
100     gdb_continue_to_breakpoint $test
101     delete_breakpoints
102 }
103
104 proc my_continue { msg } {
105     set test "continue ($msg)"
106     gdb_test_multiple "continue" $test {
107         -re "Continuing" {
108             pass $test
109         }
110     }
111
112     stop_process "stop all threads ($msg)"
113
114     goto_loop $msg
115 }
116
117 # Use CMD to step the loop 10 times.  CMD may be "step" or "next".
118
119 proc step_ten_loops { cmd } {
120     global gdb_prompt
121
122     for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
123         set other_step 0
124         set test "$cmd to increment ($i)"
125         gdb_test_multiple $cmd $test {
126             -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
127                 pass $test
128             }
129             -re "$gdb_prompt $" {
130                 if {$other_step == 0} {
131                     set other_step 1
132                     send_gdb "$cmd\n"
133                     exp_continue
134                 } else {
135                     fail $test
136                     # FIXME cascade?
137                 }
138             }
139         }
140     }
141 }
142
143 # Start with a fresh gdb.
144
145 gdb_exit
146 gdb_start
147 gdb_reinitialize_dir $srcdir/$subdir
148
149 # We'll need this when we send_gdb a ^C to GDB.  Need to do it before we
150 # run the program and gdb starts saving and restoring tty states.
151 # On Ultrix, we don't need it and it is really slow (because shell_escape
152 # doesn't use vfork).
153 if ![istarget "*-*-ultrix*"] then {
154     gdb_test "shell stty intr '^C'" ".*"
155 }
156
157 gdb_load ${binfile}
158
159 gdb_test_no_output "set print sevenbit-strings"
160 gdb_test_no_output "set width 0"
161
162 runto_main
163
164 # See if scheduler locking is available on this target.
165 global gdb_prompt
166 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
167     -re "Target .* cannot support this command" {
168         unsupported "target does not support scheduler locking"
169         return
170     }
171     -re "$gdb_prompt $" {
172         pass "scheduler locking set to none"
173     }
174     timeout {
175         unsupported "target does not support scheduler locking (timeout)"
176         return
177     }
178 }
179
180 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
181 gdb_continue_to_breakpoint "all threads started"
182
183 set start_args [get_args "before initial"]
184
185 # First make sure that all threads are alive.
186 my_continue "initial"
187
188 set cont_args [get_args "after initial"]
189
190 set bad 0
191 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
192   if {[lindex $start_args $i] == [lindex $cont_args $i]} {
193     incr bad
194   }
195 }
196 if { $bad == 0 } {
197   pass "all threads alive"
198 } else {
199   fail "all threads alive ($bad/$NUM did not run)"
200 }
201
202 # Compare the previous thread and args with the current thread and
203 # args.  Check that we didn't switch threads, and that the threads
204 # incremented their args counter the amounts expected.  CMD is the
205 # command being tested.  BEFORE_THREAD is the thread that was selected
206 # before the command was run.  BEFORE_ARGS is the value of the
207 # thread's args before the command was run.  LOCKED indicates whether
208 # we expect threads other than the selected thread remained locked.
209
210 proc check_result { cmd before_thread before_args locked } {
211     global NUM
212
213     # Make sure we're still in the same thread.
214     set newthread [get_current_thread "after"]
215
216     set test "$cmd does not change thread"
217     if {$before_thread == $newthread} {
218         pass "$test"
219     } else {
220         fail "$test (switched to thread $newthread)"
221     }
222
223     set after_args [get_args "after"]
224
225     set test "current thread advanced"
226     if { $locked } {
227         set test "$test - locked"
228     } else {
229         set test "$test - unlocked"
230     }
231
232     set num_other_threads 0
233     for {set i 0} {$i < $NUM} {incr i} {
234         if {[lindex $before_args $i] == [lindex $after_args $i]} {
235             if {$i == $before_thread} {
236                 fail "$test (didn't run)"
237             }
238         } else {
239             if {$i == $before_thread} {
240                 if {$cmd == "continue"
241                     || [lindex $before_args $i] == [expr [lindex $after_args $i] - 10]} {
242                     pass "$test"
243                 } else {
244                     fail "$test (wrong amount)"
245                 }
246             } else {
247                 incr num_other_threads
248             }
249         }
250     }
251
252     if { $locked } {
253         gdb_assert {$num_other_threads == 0} "other threads didn't run - locked"
254     } else {
255         gdb_assert {$num_other_threads > 0} "other threads ran - unlocked"
256     }
257 }
258
259 with_test_prefix "schedlock=on: cmd=continue" {
260     # Use whichever we stopped in.
261     set curthread [get_current_thread "before"]
262
263     # Test continue with scheduler locking.
264     gdb_test "set scheduler-locking on" ""
265
266     my_continue "with lock"
267
268     check_result "continue" $curthread $cont_args 1
269 }
270
271 # Test stepping/nexting with different modes of scheduler locking.
272 proc test_step { schedlock cmd call_function } {
273     global NUM
274
275     gdb_test_no_output "set scheduler-locking off"
276     goto_loop ""
277
278     set curthread [get_current_thread "before"]
279
280     # No need to set to off again.  This avoids a duplicate message.
281     if {$schedlock != "off"} {
282         gdb_test_no_output "set scheduler-locking $schedlock"
283     }
284
285     gdb_test "print call_function = $call_function" \
286         " = $call_function"
287
288     set before_args [get_args "before"]
289
290     step_ten_loops $cmd
291
292     # "next" lets other threads run while stepping over functions.
293     if { $schedlock == "on" || ($schedlock == "step" && !$call_function) } {
294         set locked 1
295     } else {
296         set locked 0
297     }
298
299     check_result $cmd $curthread $before_args $locked
300 }
301
302 # Test stepping/nexting with different modes of scheduler locking.
303 foreach schedlock {"off" "step" "on"} {
304     with_test_prefix "schedlock=$schedlock" {
305         with_test_prefix "cmd=step" {
306             test_step $schedlock "step" 0
307         }
308         with_test_prefix "cmd=next" {
309             # With "next", and schedlock "step", threads run unlocked
310             # when stepping over a function call.  This exercises both
311             # with and without a function call.  Without a function
312             # call "next" should behave just like "step".
313             foreach call_function {0 1} {
314                 with_test_prefix "call_function=$call_function" {
315                     test_step $schedlock "next" $call_function
316                 }
317             }
318         }
319     }
320 }