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