This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / testsuite / gdb.threads / pthreads.exp
1 # Copyright (C) 1996 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 set built_binfile 0
39 if [istarget "*-*-linux"] then {
40     set target_cflags "-D_MIT_POSIX_THREADS"
41 } else {
42     set target_cflags ""
43 }
44 set why_msg "unrecognized error"
45 foreach lib {-lpthreads -lpthread} {
46     set options "debug"
47     lappend options "incdir=${objdir}/${subdir}"
48     lappend options "libs=$lib"
49     set ccout [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $options]
50     switch -regexp -- $ccout {
51         ".*no posix threads support.*" {
52             set why_msg "missing threads include file"
53             break
54         }
55         ".*cannot open -lpthread.*" {
56             set why_msg "missing runtime threads library"
57         }
58         ".*Can't find library for -lpthread.*" {
59             set why_msg "missing runtime threads library"
60         }
61         {^$} {
62             pass "successfully compiled posix threads test case"
63             set built_binfile 1
64             break
65         }
66     }
67 }
68 if {$built_binfile == "0"} {
69     unsupported "Couldn't compile ${srcfile}, ${why_msg}"
70     return -1
71 }
72
73 # Now we can proceed with the real testing.
74
75 # Start with a fresh gdb.
76
77 gdb_exit
78 gdb_start
79 gdb_reinitialize_dir $srcdir/$subdir
80 gdb_load ${binfile}
81
82 gdb_test "set print sevenbit-strings" ""
83 #gdb_test "set print address off" ""
84 gdb_test "set width 0" ""
85
86 # We'll need this when we send_gdb a ^C to GDB.  Need to do it before we
87 # run the program and gdb starts saving and restoring tty states.
88 # On Ultrix, we don't need it and it is really slow (because shell_escape
89 # doesn't use vfork).
90 if ![istarget "*-*-ultrix*"] then {
91     gdb_test "shell stty intr '^C'" ""
92 }
93
94 proc all_threads_running {} {
95     global prompt
96     global srcfile
97
98     # Reset all the counters to zero.
99     gdb_test "set var common_routine::hits=0" ""
100     gdb_test "set var common_routine::from_thread1=0" ""
101     gdb_test "set var common_routine::from_thread2=0" ""
102     gdb_test "set var common_routine::from_main=0" ""
103     gdb_test "set var common_routine::full_coverage=0" ""
104
105     # Disable all breakpoints.
106     gdb_test "disable" ""
107
108     # Set up a breakpoint that will cause us to stop when we have
109     # been called 15 times.  This should be plenty of time to allow
110     # every thread to run at least once, since each thread sleeps for
111     # one second between calls to common_routine.
112     gdb_test "tbreak common_routine if hits == 15" ""
113
114     # Start all the threads running again and wait for the inferior
115     # to stop.  Since no other breakpoints are set at this time
116     # we should stop only when we have been previously called 15 times.
117
118     send_gdb "continue\n"
119     expect {
120         -re "Continuing.*common_routine.*at.*$srcfile.*$prompt $" {}
121         default {
122             fail "continue until common routine run 15 times"
123             return 0
124         }
125         timeout {
126             fail "continue until common routine run 15 times (timeout)"
127             return 0
128         }
129     }
130
131     # Check that we stopped when we actually expected to stop, by
132     # verifying that there have been 15 previous hits.
133
134     send_gdb "p common_routine::hits\n"
135     expect {
136         -re ".*= 15\r\n$prompt $" {}
137         default {
138             fail "stopped before calling common_routine 15 times"
139             return 0
140         }
141         -re ".*$prompt $" {
142             fail "stopped before clling common_routine 15 times"
143             return 0
144         }
145         timeout {
146             fail "stopped before calling common_routine 15 times (timeout)"
147             return 0
148         }
149     }
150
151     # Also check that all of the threads have run, which will only be true
152     # if the full_coverage variable is set.
153
154     send_gdb "p common_routine::full_coverage\n"
155     expect {
156         -re ".*= 1\r\n$prompt $" {}
157         default {
158             fail "some threads didn't run"
159             return 0
160         }
161         timeout {
162             fail "some threads didn't run (timeout)"
163             return 0
164         }
165     }
166
167     # Looks fine, return success.
168     return 1
169 }
170
171 proc test_startup {} {
172     global srcdir srcfile prompt expect_out
173     global main_id thread1_id thread2_id
174
175     # We should be able to do an info threads before starting any others.
176     send_gdb "info threads\n"
177     expect {
178         -re ".*Thread.*LWP.*main.*" {
179             pass "info threads"
180         }
181         -re "\r\n$prompt $" {
182             pass "info threads"
183             setup_xfail "*-*-*"
184             fail "gdb does not support pthreads for this machine"
185             return 0
186         }
187     }
188
189     # Extract the thread id number of main thread from "info threads" output.
190     send_gdb "info threads\n" ; expect -re "(\[0-9\]+)(.*Thread.*main.*)($prompt $)"
191     set main_id $expect_out(1,string)
192
193     # Check that we can continue and create the first thread.
194     gdb_test "break thread1" "Breakpoint .* file .*$srcdir.*"
195     gdb_test "continue" \
196             "Continuing.*Breakpoint .*, thread1 \\(arg=0xfeedface\\).*at.*$srcfile.*" \
197             "Continue to creation of first thread"
198     gdb_test "disable" ""
199
200     # Extract the thread id number of thread 1 from "info threads" output.
201     send_gdb "info threads\n" ; expect -re "(\[0-9\]+)(.*Thread.*thread1.*)($prompt $)"
202     set thread1_id $expect_out(1,string)
203
204     # Check that we can continue and create the second thread,
205     # ignoring the first thread for the moment.
206     gdb_test "break thread2" "Breakpoint .* file .*$srcdir.*"
207     gdb_test "continue" \
208             "Continuing.*Breakpoint .*, thread2 \\(arg=0xdeadbeef\\).*at.*$srcfile.*" \
209             "Continue to creation of second thread"
210
211     # Extract the thread id number of thread 2 from "info threads" output.
212     send_gdb "info threads\n" ; expect -re "(\[0-9\]+)(.*Thread.*thread2.*)($prompt $)"
213     set thread2_id $expect_out(1,string)
214
215     return 1
216 }
217
218 proc check_control_c {} {
219     global prompt
220
221     # Verify that all threads are running.
222     if [all_threads_running] then {
223         pass "All threads running after startup"
224     }
225
226     # Send a continue followed by ^C to the process to stop it.
227     send_gdb "continue\n"
228     set description "Stopped with a ^C"
229     after 1000 [send_gdb "\003"]
230     expect {
231         -re "Program received signal SIGINT.*$prompt $" {
232             pass $description
233         }
234         -re "Quit.*$prompt $" {
235             pass $description
236         }
237         timeout {
238             fail "$description (timeout)"
239         }
240     }
241     gdb_test "bt" ""
242
243     # Verify that all threads can be run again after a ^C stop.
244     if [all_threads_running] then {
245         pass "All threads running after continuing from ^C stop"
246     }
247 }
248
249 proc check_backtraces {} {
250     global prompt main_id thread1_id thread2_id
251
252     # Check that the "thread apply N backtrace" command works
253
254     gdb_test "thread apply $main_id backtrace" \
255             ".* in main \\(argc=.*, argv=.*\\).*" \
256             "check backtrace from main thread"
257     gdb_test "thread apply $thread1_id backtrace" \
258             ".* in thread1 \\(arg=0xfeedface\\).*" \
259             "check backtrace from thread 1"
260     gdb_test "thread apply $thread2_id backtrace" \
261             ".* in thread2 \\(arg=0xdeadbeef\\).*" \
262             "check backtrace from thread 2"
263
264     # Check that we can apply the backtrace command to all
265     # three threads with a single gdb command
266
267     gdb_test "thread apply $main_id $thread1_id $thread2_id bt" \
268             ".* in main .* in thread1 .* in thread2.*" \
269             "apply backtrace command to all three threads"
270
271     # Check that we can do thread specific backtraces
272     # This also tests that we can do thread specific breakpoints.
273
274     gdb_test "break common_routine thread $thread2_id" \
275             "Breakpoint .* at 0x.* file .* line .*" \
276             "set break at common_routine in thread 2"
277
278     send_gdb "continue\n"
279     expect {
280         -re "Breakpoint .* common_routine \\(arg=2\\).*" {
281             send_gdb "backtrace\n"
282             expect {
283                 -re "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" {
284                     pass "backtrace from thread 2 bkpt in common_routine"
285                 }
286                 default {
287                     fail "backtrace from thread 2 bkpt in common_routine"
288                 }
289                 timeout {
290                     fail "backtrace from thread 2 bkpt in common_routine (timeout)"
291                 }
292             }
293         }
294         -re "Breakpoint .* common_routine \\(arg=0\\).*" {
295             fail "stopped in main thread at breakpoint for thread 2"
296         }
297         -re "Breakpoint .* common_routine \\(arg=1\\).*" {
298             fail "stopped in main thread at breakpoint for thread 1"
299         }
300         -re ".*$prompt" {
301             fail "continue to bkpt at common_routine in thread 2"
302         }
303         default {
304             fail "continue to bkpt at common_routine in thread 2"
305         }
306         timeout {
307             fail "continue to bkpt at common_routine in thread 2 (timeout)"
308         }
309     }
310 }
311
312 setup_xfail "alpha-*-osf*"
313 if [runto_main] then {
314     clear_xfail "alpha-*-osf*"
315     if [test_startup] then {
316         check_control_c
317         check_backtraces
318     }
319 }
320 clear_xfail "alpha-*-osf*"