Imported Upstream version 7.8
[platform/upstream/gdb.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 proc get_args { } {
34     global list_count
35     global gdb_prompt
36     global NUM
37
38     set pattern "(\[0-9\]+)"
39     for {set i 1} {[expr $i < $NUM]} {incr i} {
40         append pattern ", (\[0-9\]+)"
41     }
42
43     gdb_test_multiple "print args" "listed args ($list_count)" {
44         -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt $" {
45             set list_count [expr $list_count + 1]
46             pass "listed args ($list_count)"
47
48             set result ""
49             for {set i 1} {[expr $i <= $NUM]} {incr i} {
50                 lappend result $expect_out($i,string)
51             }
52             return $result
53         }
54     }
55 }
56
57 proc stop_process { description } {
58   global gdb_prompt
59
60   # For this to work we must be sure to consume the "Continuing."
61   # message first, or GDB's signal handler may not be in place.
62   after 1000 {send_gdb "\003"}
63   gdb_expect {
64     -re "Program received signal SIGINT.*$gdb_prompt $"
65       {
66         pass $description
67       }
68     timeout
69       {
70         fail "$description (timeout)"
71       }
72   }
73 }
74
75 proc get_current_thread { description } {
76     global gdb_prompt
77
78     gdb_test_multiple "bt" "$description" {
79         -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $" {
80             pass $description
81             return $expect_out(1,string)
82         }
83     }
84     return ""
85 }
86
87 proc my_continue { msg } {
88     gdb_test_multiple "continue" "continuing ($msg)" {
89         -re "Continuing" {
90             pass "continue ($msg)"
91         }
92     }
93
94     stop_process "stop all threads ($msg)"
95
96     # Make sure we're in one of the non-main looping threads.
97     gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
98     gdb_continue_to_breakpoint "return to loop ($msg)"
99     delete_breakpoints
100 }
101
102 proc step_ten_loops { msg } {
103     global gdb_prompt
104
105     for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
106         set other_step 0
107         gdb_test_multiple "step" "step to increment ($msg $i)" {
108             -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
109                 pass "step to increment ($msg $i)"
110             }
111             -re "$gdb_prompt $" {
112                 if {$other_step == 0} {
113                     set other_step 1
114                     send_gdb "step\n"
115                     exp_continue
116                 } else {
117                     fail "step to increment ($msg $i)"
118                     # FIXME cascade?
119                 }
120             }
121         }
122     }
123 }
124
125 # Start with a fresh gdb.
126
127 gdb_exit
128 gdb_start
129 gdb_reinitialize_dir $srcdir/$subdir
130
131 # We'll need this when we send_gdb a ^C to GDB.  Need to do it before we
132 # run the program and gdb starts saving and restoring tty states.
133 # On Ultrix, we don't need it and it is really slow (because shell_escape
134 # doesn't use vfork).
135 if ![istarget "*-*-ultrix*"] then {
136     gdb_test "shell stty intr '^C'" ".*"
137 }
138
139 gdb_load ${binfile}
140
141 gdb_test_no_output "set print sevenbit-strings"
142 gdb_test_no_output "set width 0"
143
144 runto_main
145
146 # See if scheduler locking is available on this target.
147 global gdb_prompt
148 gdb_test_multiple "set scheduler-locking off" "scheduler locking set to none" {
149     -re "Target .* cannot support this command" {
150         unsupported "target does not support scheduler locking"
151         return
152     }
153     -re "$gdb_prompt $" {
154         pass "scheduler locking set to none"
155     }
156     timeout {
157         unsupported "target does not support scheduler locking (timeout)"
158         return
159     }
160 }
161
162 gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
163 gdb_continue_to_breakpoint "all threads started"
164
165 global list_count
166 set list_count 0
167
168 set start_args [get_args]
169
170 # First make sure that all threads are alive.
171 my_continue "initial"
172
173 set cont_args [get_args]
174
175 set bad 0
176 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
177   if {[lindex $start_args $i] == [lindex $cont_args $i]} {
178     incr bad
179   }
180 }
181 if { $bad == 0 } {
182   pass "all threads alive"
183 } else {
184   fail "all threads alive ($bad/$NUM did not run)"
185 }
186
187 # We can't change threads, unfortunately, in current GDB.  Use
188 # whichever we stopped in.
189 set curthread [get_current_thread "find current thread (1)"]
190
191
192
193
194 # Test stepping without scheduler locking.
195 gdb_test_no_output "set scheduler-locking off"
196
197 step_ten_loops "unlocked"
198
199 # Make sure we're still in the same thread.
200 set newthread [get_current_thread "find current thread (2)"]
201 if {$curthread == $newthread} {
202     pass "step without lock does not change thread"
203 } else {
204     fail "step without lock does not change thread (switched to thread $newthread)"
205 }
206
207 set start_args $cont_args
208 set cont_args [get_args]
209
210 set num_other_threads 0
211 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
212   if {[lindex $start_args $i] == [lindex $cont_args $i]} {
213     if {$i == $curthread} {
214       fail "current thread stepped (didn't run)"
215     }
216   } else {
217     if {$i == $curthread} {
218         if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
219             pass "current thread stepped"
220         } else {
221             fail "current thread stepped (wrong amount)"
222         }
223     } else {
224       set num_other_threads [expr $num_other_threads + 1]
225     }
226   }
227 }
228 if {$num_other_threads > 0} {
229   pass "other threads ran - unlocked"
230 } else {
231   fail "other threads ran - unlocked"
232 }
233
234 # Test continue with scheduler locking
235 gdb_test "set scheduler-locking on" ""
236
237 my_continue "with lock"
238
239 # Make sure we're still in the same thread.
240 set newthread [get_current_thread "find current thread (3)"]
241 if {$curthread == $newthread} {
242     pass "continue with lock does not change thread"
243 } else {
244     fail "continue with lock does not change thread (switched to thread $newthread)"
245 }
246
247 set start_args $cont_args
248 set cont_args [get_args]
249
250 set num_other_threads 0
251 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
252   if {[lindex $start_args $i] == [lindex $cont_args $i]} {
253     if {$i == $curthread} {
254       fail "current thread ran (didn't run)"
255     }
256   } else {
257     if {$i == $curthread} {
258       pass "current thread ran"
259     } else {
260       incr num_other_threads
261     }
262   }
263 }
264 if {$num_other_threads > 0} {
265   fail "other threads didn't run - locked"
266 } else {
267   pass "other threads didn't run - locked"
268 }
269
270 # Test stepping with scheduler locking
271 step_ten_loops "locked"
272
273 # Make sure we're still in the same thread.
274 set newthread [get_current_thread "find current thread (2)"]
275 if {$curthread == $newthread} {
276     pass "step with lock does not change thread"
277 } else {
278     fail "step with lock does not change thread (switched to thread $newthread)"
279 }
280
281 set start_args $cont_args
282 set cont_args [get_args]
283
284 set num_other_threads 0
285 for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
286   if {[lindex $start_args $i] == [lindex $cont_args $i]} {
287     if {$i == $curthread} {
288       fail "current thread stepped locked (didn't run)"
289     }
290   } else {
291     if {$i == $curthread} {
292         if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
293             pass "current thread stepped locked"
294         } else {
295             fail "current thread stepped locked (wrong amount)"
296         }
297     } else {
298       incr num_other_threads
299     }
300   }
301 }
302 if {$num_other_threads > 0} {
303   fail "other threads didn't run - step locked"
304 } else {
305   pass "other threads didn't run - step locked"
306 }
307
308 return 0