OSDN Git Service

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