* configure.in (configdirs): Add gdb.threads.
[binutils-gdb.git] / gdb / testsuite / gdb.threads / pthreads.exp
1 # Copyright (C) 1996 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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. */
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
19
20 # This file was written by Fred Fish. (fnf@cygnus.com)
21
22 if $tracelevel then {
23 strace $tracelevel
24 }
25
26 set prms_id 0
27 set bug_id 0
28
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 }
37 }
38
39 # Now we can proceed with the real testing.
40
41 # Start with a fresh gdb.
42
43 gdb_exit
44 gdb_start
45 gdb_reinitialize_dir $srcdir/$subdir
46 gdb_load ${binfile}
47
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 $"
51
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
55 # doesn't use vfork).
56 if ![istarget "*-*-ultrix*"] then {
57 send "shell stty intr '^C'\n" ; expect -re "$prompt $"
58 }
59
60 proc all_threads_running {} {
61 global prompt
62 global srcfile
63
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 $"
70
71 # Disable all breakpoints.
72 send "disable\n" ; expect -re "$prompt $"
73
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 $"
79
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.
83
84 send "continue\n"
85 expect {
86 -re "Continuing.*common_routine.*at.*$srcfile.*$prompt $" {}
87 default {
88 fail "continue until common routine run 15 times"
89 return 0
90 }
91 timeout {
92 fail "continue until common routine run 15 times (timeout)"
93 return 0
94 }
95 }
96
97 # Check that we stopped when we actually expected to stop, by
98 # verifying that there have been 15 previous hits.
99
100 send "p common_routine::hits\n"
101 expect {
102 -re ".*= 15\r\n$prompt $" {}
103 default {
104 fail "stopped before calling common_routine 15 times"
105 return 0
106 }
107 timeout {
108 fail "stopped before calling common_routine 15 times (timeout)"
109 return 0
110 }
111 }
112
113 # Also check that all of the threads have run, which will only be true
114 # if the full_coverage variable is set.
115
116 send "p common_routine::full_coverage\n"
117 expect {
118 -re ".*= 1\r\n$prompt $" {}
119 default {
120 fail "some threads didn't run"
121 return 0
122 }
123 timeout {
124 fail "some threads didn't run (timeout)"
125 return 0
126 }
127 }
128
129 # Looks fine, return success.
130 return 1
131 }
132
133 proc test_startup {} {
134 global srcdir srcfile prompt expect_out
135 global main_id thread1_id thread2_id
136
137 # We should be able to do an info threads before starting any others.
138 gdb_test "info threads" ".*Thread.*LWP.*main.*"
139
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)
143
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 $"
150
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)
154
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"
161
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)
165 }
166
167 proc check_control_c {} {
168 global prompt
169
170 # Verify that all threads are running.
171 if [all_threads_running] then {
172 pass "All threads running after startup"
173 }
174
175 # Send a continue followed by ^C to the process to stop it.
176 send "continue\n"
177 set description "Stopped with a ^C"
178 after 1000 [send "\003"]
179 expect {
180 -re "Program received signal SIGINT.*$prompt $" {
181 pass $description
182 }
183 -re "Quit.*$prompt $" {
184 pass $description
185 }
186 timeout {
187 fail "$description (timeout)"
188 }
189 }
190 send "bt\n" ; expect -re "$prompt $"
191
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"
195 }
196 }
197
198 proc check_backtraces {} {
199 global prompt main_id thread1_id thread2_id
200
201 # Check that the "thread apply N backtrace" command works
202
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"
212
213 # Check that we can apply the backtrace command to all
214 # three threads with a single gdb command
215
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"
219
220 # Check that we can do thread specific backtraces
221 # This also tests that we can do thread specific breakpoints.
222
223 gdb_test "break common_routine thread $thread2_id" \
224 "Breakpoint .* at 0x.* file .* line .*" \
225 "set break at common_routine in thread 2"
226
227 send "continue\n"
228 expect {
229 -re "Breakpoint .* common_routine \\(arg=2\\).*" {
230 send "backtrace\n"
231 expect {
232 -re "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" {
233 pass "backtrace from thread 2 bkpt in common_routine"
234 }
235 default {
236 fail "backtrace from thread 2 bkpt in common_routine"
237 }
238 timeout {
239 fail "backtrace from thread 2 bkpt in common_routine (timeout)"
240 }
241 }
242 }
243 -re "Breakpoint .* common_routine \\(arg=0\\).*" {
244 fail "stopped in main thread at breakpoint for thread 2"
245 }
246 -re "Breakpoint .* common_routine \\(arg=1\\).*" {
247 fail "stopped in main thread at breakpoint for thread 1"
248 }
249 default {
250 fail "continue to bkpt at common_routine in thread 2"
251 }
252 timeout {
253 fail "continue to bkpt at common_routine in thread 2 (timeout)"
254 }
255
256 }
257 }
258
259 if [runto_main] then {
260 test_startup
261 check_control_c
262 check_backtraces
263 }