Commit | Line | Data |
---|---|---|
ecd75fc8 | 1 | # Copyright (C) 1996-2014 Free Software Foundation, Inc. |
0312286c DJ |
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 | |
e22f8b7c | 5 | # the Free Software Foundation; either version 3 of the License, or |
0312286c | 6 | # (at your option) any later version. |
e22f8b7c | 7 | # |
0312286c DJ |
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. | |
e22f8b7c | 12 | # |
0312286c | 13 | # You should have received a copy of the GNU General Public License |
e22f8b7c | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0312286c | 15 | |
0312286c DJ |
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 | ||
0312286c | 21 | |
0efbbabc | 22 | standard_testfile |
0312286c | 23 | |
18ecae38 DJ |
24 | # The number of threads, including the main thread. |
25 | set NUM 2 | |
26 | ||
0efbbabc | 27 | if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } { |
0312286c DJ |
28 | return -1 |
29 | } | |
30 | ||
31 | # Now we can proceed with the real testing. | |
32 | ||
35420406 PA |
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 } { | |
9db70545 MS |
37 | global gdb_prompt |
38 | global NUM | |
e09490f1 | 39 | |
9db70545 MS |
40 | set pattern "(\[0-9\]+)" |
41 | for {set i 1} {[expr $i < $NUM]} {incr i} { | |
42 | append pattern ", (\[0-9\]+)" | |
43 | } | |
0312286c | 44 | |
35420406 PA |
45 | set test "listed args ($description)" |
46 | gdb_test_multiple "print args" $test { | |
9db70545 | 47 | -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" { |
35420406 | 48 | pass $test |
e09490f1 | 49 | |
9db70545 MS |
50 | set result "" |
51 | for {set i 1} {[expr $i <= $NUM]} {incr i} { | |
52 | lappend result $expect_out($i,string) | |
53 | } | |
54 | return $result | |
e09490f1 | 55 | } |
9db70545 | 56 | } |
0312286c DJ |
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 } { | |
9db70545 | 78 | global gdb_prompt |
0312286c | 79 | |
35420406 PA |
80 | set test "find current thread ($description)" |
81 | ||
82 | gdb_test_multiple "bt" $test { | |
9db70545 | 83 | -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" { |
35420406 | 84 | pass $test |
9db70545 MS |
85 | return $expect_out(1,string) |
86 | } | |
87 | } | |
88 | return "" | |
0312286c DJ |
89 | } |
90 | ||
35420406 PA |
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 | ||
0312286c | 104 | proc my_continue { msg } { |
35420406 PA |
105 | set test "continue ($msg)" |
106 | gdb_test_multiple "continue" $test { | |
9db70545 | 107 | -re "Continuing" { |
35420406 | 108 | pass $test |
9db70545 MS |
109 | } |
110 | } | |
0312286c | 111 | |
9db70545 | 112 | stop_process "stop all threads ($msg)" |
0312286c | 113 | |
35420406 | 114 | goto_loop $msg |
0312286c DJ |
115 | } |
116 | ||
35420406 PA |
117 | # Use CMD to step the loop 10 times. CMD may be "step" or "next". |
118 | ||
119 | proc step_ten_loops { cmd } { | |
0312286c DJ |
120 | global gdb_prompt |
121 | ||
122 | for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} { | |
0312286c | 123 | set other_step 0 |
35420406 PA |
124 | set test "$cmd to increment ($i)" |
125 | gdb_test_multiple $cmd $test { | |
0312286c | 126 | -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" { |
35420406 | 127 | pass $test |
0312286c DJ |
128 | } |
129 | -re "$gdb_prompt $" { | |
130 | if {$other_step == 0} { | |
131 | set other_step 1 | |
35420406 | 132 | send_gdb "$cmd\n" |
0312286c DJ |
133 | exp_continue |
134 | } else { | |
35420406 | 135 | fail $test |
0312286c DJ |
136 | # FIXME cascade? |
137 | } | |
138 | } | |
0312286c DJ |
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. | |
09dd9a69 | 151 | gdb_test "shell stty intr '^C'" ".*" |
0312286c DJ |
152 | |
153 | gdb_load ${binfile} | |
154 | ||
12b5d08a MS |
155 | gdb_test_no_output "set print sevenbit-strings" |
156 | gdb_test_no_output "set width 0" | |
0312286c DJ |
157 | |
158 | runto_main | |
159 | ||
160 | # See if scheduler locking is available on this target. | |
0312286c | 161 | global gdb_prompt |
9db70545 MS |
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 | |
0312286c | 166 | } |
9db70545 MS |
167 | -re "$gdb_prompt $" { |
168 | pass "scheduler locking set to none" | |
0312286c | 169 | } |
9db70545 MS |
170 | timeout { |
171 | unsupported "target does not support scheduler locking (timeout)" | |
172 | return | |
0312286c DJ |
173 | } |
174 | } | |
175 | ||
176 | gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"] | |
177 | gdb_continue_to_breakpoint "all threads started" | |
178 | ||
35420406 | 179 | set start_args [get_args "before initial"] |
0312286c DJ |
180 | |
181 | # First make sure that all threads are alive. | |
182 | my_continue "initial" | |
183 | ||
35420406 | 184 | set cont_args [get_args "after initial"] |
0312286c | 185 | |
e09490f1 | 186 | set bad 0 |
18ecae38 | 187 | for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} { |
0312286c | 188 | if {[lindex $start_args $i] == [lindex $cont_args $i]} { |
e09490f1 | 189 | incr bad |
0312286c DJ |
190 | } |
191 | } | |
e09490f1 | 192 | if { $bad == 0 } { |
18ecae38 DJ |
193 | pass "all threads alive" |
194 | } else { | |
e09490f1 | 195 | fail "all threads alive ($bad/$NUM did not run)" |
18ecae38 | 196 | } |
0312286c | 197 | |
35420406 PA |
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. | |
0312286c | 205 | |
35420406 PA |
206 | proc check_result { cmd before_thread before_args locked } { |
207 | global NUM | |
0312286c | 208 | |
35420406 PA |
209 | # Make sure we're still in the same thread. |
210 | set newthread [get_current_thread "after"] | |
0312286c | 211 | |
35420406 PA |
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 | } | |
0312286c | 218 | |
35420406 | 219 | set after_args [get_args "after"] |
0312286c | 220 | |
35420406 PA |
221 | set test "current thread advanced" |
222 | if { $locked } { | |
223 | set test "$test - locked" | |
224 | } else { | |
225 | set test "$test - unlocked" | |
0312286c | 226 | } |
35420406 PA |
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 | } | |
0312286c | 234 | } else { |
35420406 PA |
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 | } | |
0312286c | 245 | } |
35420406 PA |
246 | } |
247 | ||
248 | if { $locked } { | |
249 | gdb_assert {$num_other_threads == 0} "other threads didn't run - locked" | |
0312286c | 250 | } else { |
35420406 | 251 | gdb_assert {$num_other_threads > 0} "other threads ran - unlocked" |
0312286c | 252 | } |
a25fbfec | 253 | } |
0312286c | 254 | |
35420406 PA |
255 | with_test_prefix "schedlock=on: cmd=continue" { |
256 | # Use whichever we stopped in. | |
257 | set curthread [get_current_thread "before"] | |
0312286c | 258 | |
35420406 PA |
259 | # Test continue with scheduler locking. |
260 | gdb_test "set scheduler-locking on" "" | |
0312286c | 261 | |
35420406 PA |
262 | my_continue "with lock" |
263 | ||
264 | check_result "continue" $curthread $cont_args 1 | |
0312286c DJ |
265 | } |
266 | ||
35420406 PA |
267 | # Test stepping/nexting with different modes of scheduler locking. |
268 | proc test_step { schedlock cmd call_function } { | |
269 | global NUM | |
0312286c | 270 | |
35420406 PA |
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" | |
0312286c | 279 | } |
0312286c | 280 | |
35420406 PA |
281 | gdb_test "print call_function = $call_function" \ |
282 | " = $call_function" | |
0312286c | 283 | |
35420406 | 284 | set before_args [get_args "before"] |
0312286c | 285 | |
35420406 | 286 | step_ten_loops $cmd |
0312286c | 287 | |
35420406 PA |
288 | # "next" lets other threads run while stepping over functions. |
289 | if { $schedlock == "on" || ($schedlock == "step" && !$call_function) } { | |
290 | set locked 1 | |
0312286c | 291 | } else { |
35420406 | 292 | set locked 0 |
0312286c | 293 | } |
35420406 PA |
294 | |
295 | check_result $cmd $curthread $before_args $locked | |
18ecae38 | 296 | } |
0312286c | 297 | |
35420406 PA |
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 | } |