1 # Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008
2 # Free Software Foundation, Inc.
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.
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.
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/>.
17 # This file was written by Fred Fish. (fnf@cygnus.com)
26 # This only works with native configurations
31 set testfile "pthreads"
32 set srcfile ${testfile}.c
33 set binfile ${objdir}/${subdir}/${testfile}
35 # regexp for "horizontal" text (i.e. doesn't include newline or
37 set horiz "\[^\n\r\]*"
39 if [istarget "*-*-linux"] then {
40 set target_cflags "-D_MIT_POSIX_THREADS"
45 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
50 # Start with a fresh gdb.
54 gdb_reinitialize_dir $srcdir/$subdir
57 gdb_test "set print sevenbit-strings" ""
58 #gdb_test "set print address off" ""
59 gdb_test "set width 0" ""
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
65 if ![istarget "*-*-ultrix*"] then {
66 gdb_test "shell stty intr '^C'" ""
69 proc all_threads_running {} {
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" ""
80 # Disable all breakpoints.
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" ""
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.
95 -re "Continuing.*common_routine.*at.*$srcfile.*$gdb_prompt $" {}
97 fail "continue until common routine run 15 times"
101 fail "continue until common routine run 15 times (timeout)"
106 # Check that we stopped when we actually expected to stop, by
107 # verifying that there have been 15 previous hits.
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.
114 send_gdb "p common_routine::hits\n"
116 -re ".*= 15\r\n$gdb_prompt $" {
117 pass "stopped before calling common_routine 15 times"
119 -re ".*= 16\r\n$gdb_prompt $" {
120 pass "stopped before calling common_routine 15 times (16 times)"
122 -re ".*= 17\r\n$gdb_prompt $" {
123 pass "stopped before calling common_routine 15 times (17 times)"
126 fail "stopped before calling common_routine 15 times"
129 -re ".*$gdb_prompt $" {
130 fail "stopped before calling common_routine 15 times"
134 fail "stopped before calling common_routine 15 times (timeout)"
139 # Also check that all of the threads have run, which will only be true
140 # if the full_coverage variable is set.
142 send_gdb "p common_routine::full_coverage\n"
144 -re ".* = 1.*$gdb_prompt $" {}
145 -re ".* = 0.*$gdb_prompt $" {
146 fail "some threads didn't run"
150 fail "some threads didn't run"
154 fail "some threads didn't run (timeout)"
159 # Looks fine, return success.
163 proc test_startup {} {
164 global srcdir srcfile gdb_prompt expect_out
166 global main_id thread1_id thread2_id
168 # We should be able to do an info threads before starting any others.
169 send_gdb "info threads\n"
171 -re ".*Thread.*main.*$gdb_prompt $" {
174 -re "\r\n$gdb_prompt $" {
175 unsupported "gdb does not support pthreads for this machine"
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)
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" ""
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)
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"
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)
212 proc check_control_c {} {
215 # Verify that all threads are running.
216 if [all_threads_running] then {
217 pass "All threads running after startup"
220 # Send a continue followed by ^C to the process to stop it.
221 send_gdb "continue\n"
224 pass "Continue with all threads running"
227 fail "Continue with all threads running (timeout)"
232 set description "Stopped with a ^C"
234 -re "Program received signal SIGINT.*$gdb_prompt $" {
237 -re "Quit.*$gdb_prompt $" {
241 fail "$description (timeout)"
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"
254 proc check_backtraces {} {
255 global gdb_prompt main_id thread1_id thread2_id
257 # Check that the "thread apply N backtrace" command works
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"
269 # Check that we can apply the backtrace command to all
270 # three threads with a single gdb command
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"
276 # Check that we can do thread specific backtraces
277 # This also tests that we can do thread specific breakpoints.
279 gdb_test "break common_routine thread $thread2_id" \
280 "Breakpoint .* at 0x.* file .* line .*" \
281 "set break at common_routine in thread 2"
283 send_gdb "continue\n"
285 -re "Breakpoint .* common_routine \\(arg=2\\).*" {
286 pass "continue to bkpt at common_routine in thread 2"
287 send_gdb "backtrace\n"
289 -re "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" {
290 pass "backtrace from thread 2 bkpt in common_routine"
293 fail "backtrace from thread 2 bkpt in common_routine"
296 fail "backtrace from thread 2 bkpt in common_routine (timeout)"
300 -re "Breakpoint .* common_routine \\(arg=0\\).*" {
301 fail "continue to bkpt at common_routine in thread 2 (arg=0)"
303 -re "Breakpoint .* common_routine \\(arg=1\\).*" {
304 fail "continue to bkpt at common_routine in thread 2 (arg=1)"
306 -re ".*$gdb_prompt" {
307 fail "continue to bkpt at common_routine in thread 2"
310 fail "continue to bkpt at common_routine in thread 2 (default)"
313 fail "continue to bkpt at common_routine in thread 2 (timeout)"
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"
329 clear_xfail "alpha-*-osf*"