1 # Copyright (C) 1996, 1997 Free Software Foundation, Inc.
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.
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.
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.
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
20 # This file was written by Fred Fish. (fnf@cygnus.com)
29 # This only works with native configurations
34 set testfile "pthreads"
35 set srcfile ${testfile}.c
36 set binfile ${objdir}/${subdir}/${testfile}
38 # regexp for "horizontal" text (i.e. doesn't include newline or
40 set horiz "\[^\n\r\]*"
43 if [istarget "*-*-linux"] then {
44 set target_cflags "-D_MIT_POSIX_THREADS"
48 set why_msg "unrecognized error"
49 foreach lib {-lpthreads -lpthread} {
51 lappend options "incdir=${objdir}/${subdir}"
52 lappend options "libs=$lib"
53 set ccout [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $options]
54 switch -regexp -- $ccout {
55 ".*no posix threads support.*" {
56 set why_msg "missing threads include file"
59 ".*cannot open -lpthread.*" {
60 set why_msg "missing runtime threads library"
62 ".*Can't find library for -lpthread.*" {
63 set why_msg "missing runtime threads library"
66 pass "successfully compiled posix threads test case"
72 if {$built_binfile == "0"} {
73 unsupported "Couldn't compile ${srcfile}, ${why_msg}"
77 # Now we can proceed with the real testing.
79 # Start with a fresh gdb.
83 gdb_reinitialize_dir $srcdir/$subdir
86 gdb_test "set print sevenbit-strings" ""
87 #gdb_test "set print address off" ""
88 gdb_test "set width 0" ""
90 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
91 # run the program and gdb starts saving and restoring tty states.
92 # On Ultrix, we don't need it and it is really slow (because shell_escape
94 if ![istarget "*-*-ultrix*"] then {
95 gdb_test "shell stty intr '^C'" ""
98 proc all_threads_running {} {
102 # Reset all the counters to zero.
103 gdb_test "set var common_routine::hits=0" ""
104 gdb_test "set var common_routine::from_thread1=0" ""
105 gdb_test "set var common_routine::from_thread2=0" ""
106 gdb_test "set var common_routine::from_main=0" ""
107 gdb_test "set var common_routine::full_coverage=0" ""
109 # Disable all breakpoints.
110 gdb_test "disable" ""
112 # Set up a breakpoint that will cause us to stop when we have
113 # been called 15 times. This should be plenty of time to allow
114 # every thread to run at least once, since each thread sleeps for
115 # one second between calls to common_routine.
116 gdb_test "tbreak common_routine if hits == 15" ""
118 # Start all the threads running again and wait for the inferior
119 # to stop. Since no other breakpoints are set at this time
120 # we should stop only when we have been previously called 15 times.
122 send_gdb "continue\n"
124 -re "Continuing.*common_routine.*at.*$srcfile.*$gdb_prompt $" {}
126 fail "continue until common routine run 15 times"
130 fail "continue until common routine run 15 times (timeout)"
135 # Check that we stopped when we actually expected to stop, by
136 # verifying that there have been 15 previous hits.
138 send_gdb "p common_routine::hits\n"
140 -re ".*= 15\r\n$gdb_prompt $" {}
142 fail "stopped before calling common_routine 15 times"
145 -re ".*$gdb_prompt $" {
146 fail "stopped before calling common_routine 15 times"
150 fail "stopped before calling common_routine 15 times (timeout)"
155 # Also check that all of the threads have run, which will only be true
156 # if the full_coverage variable is set.
158 send_gdb "p common_routine::full_coverage\n"
160 -re ".*= 1\r\n$gdb_prompt $" {}
162 fail "some threads didn't run"
166 fail "some threads didn't run (timeout)"
171 # Looks fine, return success.
175 proc test_startup {} {
176 global srcdir srcfile gdb_prompt expect_out
178 global main_id thread1_id thread2_id
180 # We should be able to do an info threads before starting any others.
181 send_gdb "info threads\n"
183 -re ".*Thread.*LWP.*main.*$gdb_prompt $" {
186 -re "\r\n$gdb_prompt $" {
189 fail "gdb does not support pthreads for this machine"
194 # Extract the thread id number of main thread from "info threads" output.
195 send_gdb "info threads\n"
196 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}main.*)($gdb_prompt $)"
197 set main_id $expect_out(1,string)
199 # Check that we can continue and create the first thread.
200 gdb_test "break thread1" "Breakpoint .* file .*$srcdir.*"
201 gdb_test "continue" \
202 "Continuing.*Breakpoint .*, thread1 \\(arg=0xfeedface\\).*at.*$srcfile.*" \
203 "Continue to creation of first thread"
204 gdb_test "disable" ""
206 # Extract the thread id number of thread 1 from "info threads" output.
207 send_gdb "info threads\n"
208 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}thread1.*)($gdb_prompt $)"
209 set thread1_id $expect_out(1,string)
211 # Check that we can continue and create the second thread,
212 # ignoring the first thread for the moment.
213 gdb_test "break thread2" "Breakpoint .* file .*$srcdir.*"
214 gdb_test "continue" \
215 "Continuing.*Breakpoint .*, thread2 \\(arg=0xdeadbeef\\).*at.*$srcfile.*" \
216 "Continue to creation of second thread"
218 # Extract the thread id number of thread 2 from "info threads" output.
219 send_gdb "info threads\n"
220 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}thread2.*)($gdb_prompt $)"
221 set thread2_id $expect_out(1,string)
226 proc check_control_c {} {
229 # Verify that all threads are running.
230 if [all_threads_running] then {
231 pass "All threads running after startup"
234 # Send a continue followed by ^C to the process to stop it.
235 send_gdb "continue\n"
236 set description "Stopped with a ^C"
237 after 1000 [send_gdb "\003"]
239 -re "Program received signal SIGINT.*$gdb_prompt $" {
242 -re "Quit.*$gdb_prompt $" {
246 fail "$description (timeout)"
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"
257 proc check_backtraces {} {
258 global gdb_prompt main_id thread1_id thread2_id
260 # Check that the "thread apply N backtrace" command works
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"
272 # Check that we can apply the backtrace command to all
273 # three threads with a single gdb command
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"
279 # Check that we can do thread specific backtraces
280 # This also tests that we can do thread specific breakpoints.
282 gdb_test "break common_routine thread $thread2_id" \
283 "Breakpoint .* at 0x.* file .* line .*" \
284 "set break at common_routine in thread 2"
286 send_gdb "continue\n"
288 -re "Breakpoint .* common_routine \\(arg=2\\).*" {
289 send_gdb "backtrace\n"
291 -re "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" {
292 pass "backtrace from thread 2 bkpt in common_routine"
295 fail "backtrace from thread 2 bkpt in common_routine"
298 fail "backtrace from thread 2 bkpt in common_routine (timeout)"
302 -re "Breakpoint .* common_routine \\(arg=0\\).*" {
303 fail "stopped in main thread at breakpoint for thread 2"
305 -re "Breakpoint .* common_routine \\(arg=1\\).*" {
306 fail "stopped in main thread at breakpoint for thread 1"
308 -re ".*$gdb_prompt" {
309 fail "continue to bkpt at common_routine in thread 2"
312 fail "continue to bkpt at common_routine in thread 2"
315 fail "continue to bkpt at common_routine in thread 2 (timeout)"
320 setup_xfail "alpha-*-osf*"
321 if [runto_main] then {
322 clear_xfail "alpha-*-osf*"
323 if [test_startup] then {
328 clear_xfail "alpha-*-osf*"