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