Update copyright year in most headers.
[binutils-gdb.git] / gdb / testsuite / gdb.threads / pthreads.exp
1 # Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008,
2 # 2009, 2010 Free Software 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 Fred Fish. (fnf@cygnus.com)
18
19 if $tracelevel then {
20 strace $tracelevel
21 }
22
23 set prms_id 0
24 set bug_id 0
25
26 # This only works with native configurations
27 if ![isnative] then {
28 return
29 }
30
31 set testfile "pthreads"
32 set srcfile ${testfile}.c
33 set binfile ${objdir}/${subdir}/${testfile}
34
35 # regexp for "horizontal" text (i.e. doesn't include newline or
36 # carriage return)
37 set horiz "\[^\n\r\]*"
38
39 if [istarget "*-*-linux"] then {
40 set target_cflags "-D_MIT_POSIX_THREADS"
41 } else {
42 set target_cflags ""
43 }
44
45 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
46 return -1
47 }
48
49
50 # Start with a fresh gdb.
51
52 gdb_exit
53 gdb_start
54 gdb_reinitialize_dir $srcdir/$subdir
55 gdb_load ${binfile}
56
57 gdb_test "set print sevenbit-strings" ""
58 #gdb_test "set print address off" ""
59 gdb_test "set width 0" ""
60
61 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
62 # run the program and gdb starts saving and restoring tty states.
63 # On Ultrix, we don't need it and it is really slow (because shell_escape
64 # doesn't use vfork).
65 if ![istarget "*-*-ultrix*"] then {
66 gdb_test "shell stty intr '^C'" ""
67 }
68
69 proc all_threads_running {} {
70 global gdb_prompt
71 global srcfile
72
73 # Reset all the counters to zero.
74 gdb_test "set var common_routine::hits=0" ""
75 gdb_test "set var common_routine::from_thread1=0" ""
76 gdb_test "set var common_routine::from_thread2=0" ""
77 gdb_test "set var common_routine::from_main=0" ""
78 gdb_test "set var common_routine::full_coverage=0" ""
79
80 # Disable all breakpoints.
81 gdb_test "disable" ""
82
83 # Set up a breakpoint that will cause us to stop when we have
84 # been called 15 times. This should be plenty of time to allow
85 # every thread to run at least once, since each thread sleeps for
86 # one second between calls to common_routine.
87 gdb_test "tbreak common_routine if hits >= 15" ""
88
89 # Start all the threads running again and wait for the inferior
90 # to stop. Since no other breakpoints are set at this time
91 # we should stop only when we have been previously called 15 times.
92
93 send_gdb "continue\n"
94 gdb_expect {
95 -re "Continuing.*common_routine.*at.*$srcfile.*$gdb_prompt $" {}
96 default {
97 fail "continue until common routine run 15 times"
98 return 0
99 }
100 timeout {
101 fail "continue until common routine run 15 times (timeout)"
102 return 0
103 }
104 }
105
106 # Check that we stopped when we actually expected to stop, by
107 # verifying that there have been 15 previous hits.
108
109 # NOTE: Because of synchronization behavior, it is possible for
110 # more than one thread to increment "hits" between one breakpoint
111 # trap and the next. So stopping after 16 or 17 hits should be
112 # considered acceptable.
113
114 send_gdb "p common_routine::hits\n"
115 gdb_expect {
116 -re ".*= 15\r\n$gdb_prompt $" {
117 pass "stopped before calling common_routine 15 times"
118 }
119 -re ".*= 16\r\n$gdb_prompt $" {
120 pass "stopped before calling common_routine 15 times (16 times)"
121 }
122 -re ".*= 17\r\n$gdb_prompt $" {
123 pass "stopped before calling common_routine 15 times (17 times)"
124 }
125 default {
126 fail "stopped before calling common_routine 15 times"
127 return 0
128 }
129 -re ".*$gdb_prompt $" {
130 fail "stopped before calling common_routine 15 times"
131 return 0
132 }
133 timeout {
134 fail "stopped before calling common_routine 15 times (timeout)"
135 return 0
136 }
137 }
138
139 # Also check that all of the threads have run, which will only be true
140 # if the full_coverage variable is set.
141
142 send_gdb "p common_routine::full_coverage\n"
143 gdb_expect {
144 -re ".* = 1.*$gdb_prompt $" {}
145 -re ".* = 0.*$gdb_prompt $" {
146 fail "some threads didn't run"
147 return 0
148 }
149 default {
150 fail "some threads didn't run"
151 return 0
152 }
153 timeout {
154 fail "some threads didn't run (timeout)"
155 return 0
156 }
157 }
158
159 # Looks fine, return success.
160 return 1
161 }
162
163 proc test_startup {} {
164 global srcdir srcfile gdb_prompt expect_out
165 global horiz
166 global main_id thread1_id thread2_id
167
168 # We should be able to do an info threads before starting any others.
169 send_gdb "info threads\n"
170 gdb_expect {
171 -re ".*Thread.*main.*$gdb_prompt $" {
172 pass "info threads"
173 }
174 -re "\r\n$gdb_prompt $" {
175 unsupported "gdb does not support pthreads for this machine"
176 return 0
177 }
178 }
179
180 # Extract the thread id number of main thread from "info threads" output.
181 send_gdb "info threads\n"
182 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}main.*)($gdb_prompt $)"
183 set main_id $expect_out(1,string)
184
185 # Check that we can continue and create the first thread.
186 gdb_test "break thread1" "Breakpoint .* file .*$srcfile.*"
187 gdb_test "continue" \
188 "Continuing.*Breakpoint .*, thread1 \\(arg=0xfeedface\\).*at.*$srcfile.*" \
189 "Continue to creation of first thread"
190 gdb_test "disable" ""
191
192 # Extract the thread id number of thread 1 from "info threads" output.
193 send_gdb "info threads\n"
194 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}thread1.*)($gdb_prompt $)"
195 set thread1_id $expect_out(1,string)
196
197 # Check that we can continue and create the second thread,
198 # ignoring the first thread for the moment.
199 gdb_test "break thread2" "Breakpoint .* file .*$srcfile.*"
200 gdb_test "continue" \
201 "Continuing.*Breakpoint .*, thread2 \\(arg=0xdeadbeef\\).*at.*$srcfile.*" \
202 "Continue to creation of second thread"
203
204 # Extract the thread id number of thread 2 from "info threads" output.
205 send_gdb "info threads\n"
206 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}thread2.*)($gdb_prompt $)"
207 set thread2_id $expect_out(1,string)
208
209 return 1
210 }
211
212 proc check_control_c {} {
213 global gdb_prompt
214
215 # Verify that all threads are running.
216 if [all_threads_running] then {
217 pass "All threads running after startup"
218 }
219
220 # Send a continue followed by ^C to the process to stop it.
221 send_gdb "continue\n"
222 gdb_expect {
223 -re "Continuing." {
224 pass "Continue with all threads running"
225 }
226 timeout {
227 fail "Continue with all threads running (timeout)"
228 }
229 }
230 after 2000
231 send_gdb "\003"
232 set description "Stopped with a ^C"
233 gdb_expect {
234 -re "Program received signal SIGINT.*$gdb_prompt $" {
235 pass $description
236 }
237 -re "Quit.*$gdb_prompt $" {
238 pass $description
239 }
240 timeout {
241 fail "$description (timeout)"
242 return 1;
243 }
244 }
245 gdb_test "bt" ""
246
247 # Verify that all threads can be run again after a ^C stop.
248 if [all_threads_running] then {
249 pass "All threads running after continuing from ^C stop"
250 }
251 return 0;
252 }
253
254 proc check_backtraces {} {
255 global gdb_prompt main_id thread1_id thread2_id
256
257 # Check that the "thread apply N backtrace" command works
258
259 gdb_test "thread apply $main_id backtrace" \
260 ".* in main \\(argc=.*, argv=.*\\).*" \
261 "check backtrace from main thread"
262 gdb_test "thread apply $thread1_id backtrace" \
263 ".* in thread1 \\(arg=0xfeedface\\).*" \
264 "check backtrace from thread 1"
265 gdb_test "thread apply $thread2_id backtrace" \
266 ".* in thread2 \\(arg=0xdeadbeef\\).*" \
267 "check backtrace from thread 2"
268
269 # Check that we can apply the backtrace command to all
270 # three threads with a single gdb command
271
272 gdb_test "thread apply $main_id $thread1_id $thread2_id bt" \
273 ".* in main .* in thread1 .* in thread2.*" \
274 "apply backtrace command to all three threads"
275
276 # Check that we can do thread specific backtraces
277 # This also tests that we can do thread specific breakpoints.
278
279 gdb_test "break common_routine thread $thread2_id" \
280 "Breakpoint .* at 0x.* file .* line .*" \
281 "set break at common_routine in thread 2"
282
283 send_gdb "continue\n"
284 gdb_expect {
285 -re "Breakpoint .* common_routine \\(arg=2\\).*" {
286 pass "continue to bkpt at common_routine in thread 2"
287 send_gdb "backtrace\n"
288 gdb_expect {
289 -re "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" {
290 pass "backtrace from thread 2 bkpt in common_routine"
291 }
292 default {
293 fail "backtrace from thread 2 bkpt in common_routine"
294 }
295 timeout {
296 fail "backtrace from thread 2 bkpt in common_routine (timeout)"
297 }
298 }
299 }
300 -re "Breakpoint .* common_routine \\(arg=0\\).*" {
301 fail "continue to bkpt at common_routine in thread 2 (arg=0)"
302 }
303 -re "Breakpoint .* common_routine \\(arg=1\\).*" {
304 fail "continue to bkpt at common_routine in thread 2 (arg=1)"
305 }
306 -re ".*$gdb_prompt" {
307 fail "continue to bkpt at common_routine in thread 2"
308 }
309 default {
310 fail "continue to bkpt at common_routine in thread 2 (default)"
311 }
312 timeout {
313 fail "continue to bkpt at common_routine in thread 2 (timeout)"
314 }
315 }
316 }
317
318 setup_xfail "alpha-*-osf*"
319 if [runto_main] then {
320 clear_xfail "alpha-*-osf*"
321 if [test_startup] then {
322 if [check_control_c] then {
323 warning "Could not stop child with ^C; skipping rest of tests.\n"
324 return;
325 }
326 check_backtraces
327 }
328 }
329 clear_xfail "alpha-*-osf*"