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