1 # Copyright (C) 1996 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 set testfile "pthreads"
30 set srcfile ${testfile}.c
31 set binfile ${objdir}/${subdir}/${testfile}
32 set ccout [compile "${srcdir}/${subdir}/${srcfile} -I${objdir}/${subdir} -g -o ${binfile} -lpthread"]
33 switch -regexp -- $ccout {
34 ".*no posix threads support.*" { return 0 }
35 {^$} { pass "successfully compiled posix threads test case" }
36 default { perror "Couldn't compile ${srcfile}" ; return -1 }
39 # Now we can proceed with the real testing.
41 # Start with a fresh gdb.
45 gdb_reinitialize_dir $srcdir/$subdir
48 send "set print sevenbit-strings\n" ; expect -re "$prompt $"
49 #send "set print address off\n" ; expect -re "$prompt $"
50 send "set width 0\n" ; expect -re "$prompt $"
52 # We'll need this when we send a ^C to GDB. Need to do it before we
53 # run the program and gdb starts saving and restoring tty states.
54 # On Ultrix, we don't need it and it is really slow (because shell_escape
56 if ![istarget "*-*-ultrix*"] then {
57 send "shell stty intr '^C'\n" ; expect -re "$prompt $"
60 proc all_threads_running {} {
64 # Reset all the counters to zero.
65 send "set var common_routine::hits=0\n" ; expect -re "$prompt $"
66 send "set var common_routine::from_thread1=0\n" ; expect -re "$prompt $"
67 send "set var common_routine::from_thread2=0\n" ; expect -re "$prompt $"
68 send "set var common_routine::from_main=0\n" ; expect -re "$prompt $"
69 send "set var common_routine::full_coverage=0\n" ; expect -re "$prompt $"
71 # Disable all breakpoints.
72 send "disable\n" ; expect -re "$prompt $"
74 # Set up a breakpoint that will cause us to stop when we have
75 # been called 15 times. This should be plenty of time to allow
76 # every thread to run at least once, since each thread sleeps for
77 # one second between calls to common_routine.
78 send "tbreak common_routine if hits == 15\n"; expect -re "$prompt $"
80 # Start all the threads running again and wait for the inferior
81 # to stop. Since no other breakpoints are set at this time
82 # we should stop only when we have been previously called 15 times.
86 -re "Continuing.*common_routine.*at.*$srcfile.*$prompt $" {}
88 fail "continue until common routine run 15 times"
92 fail "continue until common routine run 15 times (timeout)"
97 # Check that we stopped when we actually expected to stop, by
98 # verifying that there have been 15 previous hits.
100 send "p common_routine::hits\n"
102 -re ".*= 15\r\n$prompt $" {}
104 fail "stopped before calling common_routine 15 times"
108 fail "stopped before calling common_routine 15 times (timeout)"
113 # Also check that all of the threads have run, which will only be true
114 # if the full_coverage variable is set.
116 send "p common_routine::full_coverage\n"
118 -re ".*= 1\r\n$prompt $" {}
120 fail "some threads didn't run"
124 fail "some threads didn't run (timeout)"
129 # Looks fine, return success.
133 proc test_startup {} {
134 global srcdir srcfile prompt expect_out
135 global main_id thread1_id thread2_id
137 # We should be able to do an info threads before starting any others.
138 gdb_test "info threads" ".*Thread.*LWP.*main.*"
140 # Extract the thread id number of main thread from "info threads" output.
141 send "info threads\n" ; expect -re "(\[0-9\]+)(.*Thread.*main.*)($prompt $)"
142 set main_id $expect_out(1,string)
144 # Check that we can continue and create the first thread.
145 gdb_test "break thread1" "Breakpoint .* file .*$srcdir.*"
146 gdb_test "continue" \
147 "Continuing.*Breakpoint .*, thread1 \\(arg=0xfeedface\\).*at.*$srcfile.*" \
148 "Continue to creation of first thread"
149 send "disable\n" ; expect -re "$prompt $"
151 # Extract the thread id number of thread 1 from "info threads" output.
152 send "info threads\n" ; expect -re "(\[0-9\]+)(.*Thread.*thread1.*)($prompt $)"
153 set thread1_id $expect_out(1,string)
155 # Check that we can continue and create the second thread,
156 # ignoring the first thread for the moment.
157 gdb_test "break thread2" "Breakpoint .* file .*$srcdir.*"
158 gdb_test "continue" \
159 "Continuing.*Breakpoint .*, thread2 \\(arg=0xdeadbeef\\).*at.*$srcfile.*" \
160 "Continue to creation of second thread"
162 # Extract the thread id number of thread 2 from "info threads" output.
163 send "info threads\n" ; expect -re "(\[0-9\]+)(.*Thread.*thread2.*)($prompt $)"
164 set thread2_id $expect_out(1,string)
167 proc check_control_c {} {
170 # Verify that all threads are running.
171 if [all_threads_running] then {
172 pass "All threads running after startup"
175 # Send a continue followed by ^C to the process to stop it.
177 set description "Stopped with a ^C"
178 after 1000 [send "\003"]
180 -re "Program received signal SIGINT.*$prompt $" {
183 -re "Quit.*$prompt $" {
187 fail "$description (timeout)"
190 send "bt\n" ; expect -re "$prompt $"
192 # Verify that all threads can be run again after a ^C stop.
193 if [all_threads_running] then {
194 pass "All threads running after continuing from ^C stop"
198 proc check_backtraces {} {
199 global prompt main_id thread1_id thread2_id
201 # Check that the "thread apply N backtrace" command works
203 gdb_test "thread apply $main_id backtrace" \
204 ".* in main \\(argc=.*, argv=.*\\).*" \
205 "check backtrace from main thread"
206 gdb_test "thread apply $thread1_id backtrace" \
207 ".* in thread1 \\(arg=0xfeedface\\).*" \
208 "check backtrace from thread 1"
209 gdb_test "thread apply $thread2_id backtrace" \
210 ".* in thread2 \\(arg=0xdeadbeef\\).*" \
211 "check backtrace from thread 2"
213 # Check that we can apply the backtrace command to all
214 # three threads with a single gdb command
216 gdb_test "thread apply $main_id $thread1_id $thread2_id bt" \
217 ".* in main .* in thread1 .* in thread2.*" \
218 "apply backtrace command to all three threads"
220 # Check that we can do thread specific backtraces
221 # This also tests that we can do thread specific breakpoints.
223 gdb_test "break common_routine thread $thread2_id" \
224 "Breakpoint .* at 0x.* file .* line .*" \
225 "set break at common_routine in thread 2"
229 -re "Breakpoint .* common_routine \\(arg=2\\).*" {
232 -re "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" {
233 pass "backtrace from thread 2 bkpt in common_routine"
236 fail "backtrace from thread 2 bkpt in common_routine"
239 fail "backtrace from thread 2 bkpt in common_routine (timeout)"
243 -re "Breakpoint .* common_routine \\(arg=0\\).*" {
244 fail "stopped in main thread at breakpoint for thread 2"
246 -re "Breakpoint .* common_routine \\(arg=1\\).*" {
247 fail "stopped in main thread at breakpoint for thread 1"
250 fail "continue to bkpt at common_routine in thread 2"
253 fail "continue to bkpt at common_routine in thread 2 (timeout)"
259 if [runto_main] then {