]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/schedlock.exp
Update year range in copyright notice of all files owned by the GDB project.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / schedlock.exp
1 # Copyright (C) 1996-2015 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 gdb_test "shell stty intr '^C'" ".*"
152
153 gdb_load ${binfile}
154
155 gdb_test_no_output "set print sevenbit-strings"
156 gdb_test_no_output "set width 0"
157
158 runto_main
159
160 # See if scheduler locking is available on this target.
161 global gdb_prompt
162 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
163 -re "Target .* cannot support this command" {
164 unsupported "target does not support scheduler locking"
165 return
166 }
167 -re "$gdb_prompt $" {
168 pass "scheduler locking set to none"
169 }
170 timeout {
171 unsupported "target does not support scheduler locking (timeout)"
172 return
173 }
174 }
175
176 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
177 gdb_continue_to_breakpoint "all threads started"
178
179 set start_args [get_args "before initial"]
180
181 # First make sure that all threads are alive.
182 my_continue "initial"
183
184 set cont_args [get_args "after initial"]
185
186 set bad 0
187 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
188 if {[lindex $start_args $i] == [lindex $cont_args $i]} {
189 incr bad
190 }
191 }
192 if { $bad == 0 } {
193 pass "all threads alive"
194 } else {
195 fail "all threads alive ($bad/$NUM did not run)"
196 }
197
198 # Compare the previous thread and args with the current thread and
199 # args. Check that we didn't switch threads, and that the threads
200 # incremented their args counter the amounts expected. CMD is the
201 # command being tested. BEFORE_THREAD is the thread that was selected
202 # before the command was run. BEFORE_ARGS is the value of the
203 # thread's args before the command was run. LOCKED indicates whether
204 # we expect threads other than the selected thread remained locked.
205
206 proc check_result { cmd before_thread before_args locked } {
207 global NUM
208
209 # Make sure we're still in the same thread.
210 set newthread [get_current_thread "after"]
211
212 set test "$cmd does not change thread"
213 if {$before_thread == $newthread} {
214 pass "$test"
215 } else {
216 fail "$test (switched to thread $newthread)"
217 }
218
219 set after_args [get_args "after"]
220
221 set test "current thread advanced"
222 if { $locked } {
223 set test "$test - locked"
224 } else {
225 set test "$test - unlocked"
226 }
227
228 set num_other_threads 0
229 for {set i 0} {$i < $NUM} {incr i} {
230 if {[lindex $before_args $i] == [lindex $after_args $i]} {
231 if {$i == $before_thread} {
232 fail "$test (didn't run)"
233 }
234 } else {
235 if {$i == $before_thread} {
236 if {$cmd == "continue"
237 || [lindex $before_args $i] == [expr [lindex $after_args $i] - 10]} {
238 pass "$test"
239 } else {
240 fail "$test (wrong amount)"
241 }
242 } else {
243 incr num_other_threads
244 }
245 }
246 }
247
248 if { $locked } {
249 gdb_assert {$num_other_threads == 0} "other threads didn't run - locked"
250 } else {
251 gdb_assert {$num_other_threads > 0} "other threads ran - unlocked"
252 }
253 }
254
255 with_test_prefix "schedlock=on: cmd=continue" {
256 # Use whichever we stopped in.
257 set curthread [get_current_thread "before"]
258
259 # Test continue with scheduler locking.
260 gdb_test "set scheduler-locking on" ""
261
262 my_continue "with lock"
263
264 check_result "continue" $curthread $cont_args 1
265 }
266
267 # Test stepping/nexting with different modes of scheduler locking.
268 proc test_step { schedlock cmd call_function } {
269 global NUM
270
271 gdb_test_no_output "set scheduler-locking off"
272 goto_loop ""
273
274 set curthread [get_current_thread "before"]
275
276 # No need to set to off again. This avoids a duplicate message.
277 if {$schedlock != "off"} {
278 gdb_test_no_output "set scheduler-locking $schedlock"
279 }
280
281 gdb_test "print call_function = $call_function" \
282 " = $call_function"
283
284 set before_args [get_args "before"]
285
286 step_ten_loops $cmd
287
288 # "next" lets other threads run while stepping over functions.
289 if { $schedlock == "on" || ($schedlock == "step" && !$call_function) } {
290 set locked 1
291 } else {
292 set locked 0
293 }
294
295 check_result $cmd $curthread $before_args $locked
296 }
297
298 # Test stepping/nexting with different modes of scheduler locking.
299 foreach schedlock {"off" "step" "on"} {
300 with_test_prefix "schedlock=$schedlock" {
301 with_test_prefix "cmd=step" {
302 test_step $schedlock "step" 0
303 }
304 with_test_prefix "cmd=next" {
305 # With "next", and schedlock "step", threads run unlocked
306 # when stepping over a function call. This exercises both
307 # with and without a function call. Without a function
308 # call "next" should behave just like "step".
309 foreach call_function {0 1} {
310 with_test_prefix "call_function=$call_function" {
311 test_step $schedlock "next" $call_function
312 }
313 }
314 }
315 }
316 }