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