]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - 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
1 # Copyright (C) 1996-2013 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 3 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, see <http://www.gnu.org/licenses/>.
15
16 # This file was written by Fred Fish. (fnf@cygnus.com)
17
18
19 standard_testfile
20
21 # regexp for "horizontal" text (i.e. doesn't include newline or
22 # carriage return)
23 set horiz "\[^\n\r\]*"
24
25 if [istarget "*-*-linux"] then {
26 set target_cflags "-D_MIT_POSIX_THREADS"
27 } else {
28 set target_cflags ""
29 }
30
31 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
32 return -1
33 }
34
35 clean_restart ${binfile}
36
37 gdb_test_no_output "set print sevenbit-strings"
38 #gdb_test_no_output "set print address off"
39 gdb_test_no_output "set width 0"
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).
45 if ![istarget "*-*-ultrix*"] then {
46 gdb_test "shell stty intr '^C'" ".*"
47 }
48
49 proc all_threads_running {} {
50 global gdb_prompt
51 global srcfile
52
53 # Reset all the counters to zero.
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"
59
60 # Disable all breakpoints.
61 gdb_test_no_output "disable"
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.
67 gdb_test "tbreak common_routine if hits >= 15" ".*"
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
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
78 }
79 }
80
81 if { $return_me == 1 } then {
82 return 0;
83 }
84
85 # Check that we stopped when we actually expected to stop, by
86 # verifying that there have been 15 previous hits.
87
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
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 }
104 }
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
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 }
118 }
119
120 # Looks fine, return success.
121 return $return_me
122 }
123
124 proc 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.
130 set return_me 1
131 gdb_test_multiple "info threads" "info threads" {
132 -re ".*Thread.*main.*$gdb_prompt $" {
133 pass "info threads"
134 set return_me 0
135 }
136 -re "\r\n$gdb_prompt $" {
137 unsupported "gdb does not support pthreads for this machine"
138 }
139 }
140
141 if { $return_me == 1 } then {
142 return 0;
143 }
144
145 # Extract the thread id number of main thread from "info threads" output.
146 gdb_test_multiple "info threads" "get main thread id" {
147 -re "(\[0-9\]+)(${horiz}Thread${horiz}main.*)($gdb_prompt $)" {
148 }
149 }
150
151 set main_id $expect_out(1,string)
152
153 # Check that we can continue and create the first thread.
154 gdb_test "break thread1" "Breakpoint .* file .*$srcfile.*"
155 gdb_test "continue" \
156 "Continuing.*Breakpoint .*, thread1 \\(arg=0xfeedface\\).*at.*$srcfile.*" \
157 "Continue to creation of first thread"
158 gdb_test_no_output "disable"
159
160 # Extract the thread id number of thread 1 from "info threads" output.
161 gdb_test_multiple "info threads" "get thread 1 id" {
162 -re "(\[0-9\]+)(${horiz}Thread${horiz}thread1.*)($gdb_prompt $)" {
163 }
164 }
165
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.
170 gdb_test "break thread2" "Breakpoint .* file .*$srcfile.*"
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.
176 gdb_test_multiple "info threads" "get thread 2 id" {
177 -re "(\[0-9\]+)(${horiz}Thread${horiz}thread2.*)($gdb_prompt $)" {
178 }
179 }
180
181 set thread2_id $expect_out(1,string)
182
183 return 1
184 }
185
186 proc 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.
195 gdb_test_multiple "continue" "continue with all threads running" {
196 -re "Continuing." {
197 pass "Continue with all threads running"
198 }
199 }
200 after 2000
201 send_gdb "\003"
202 set description "Stopped with a ^C"
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)"
212 return 1;
213 }
214 }
215 gdb_test "bt" ".*"
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 }
221 return 0;
222 }
223
224 proc 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
253 gdb_test_multiple "continue" "continue to bkpt at common_routine in thread 2" {
254 -re "Breakpoint .* common_routine \\(arg=2\\).*$gdb_prompt $" {
255 pass "continue to bkpt at common_routine in thread 2"
256 gdb_test "backtrace" \
257 "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" \
258 "backtrace from thread 2 bkpt in common_routine"
259 }
260 -re "Breakpoint .* common_routine \\(arg=0\\).*$gdb_prompt $" {
261 fail "continue to bkpt at common_routine in thread 2 (arg=0)"
262 }
263 -re "Breakpoint .* common_routine \\(arg=1\\).*$gdb_prompt $" {
264 fail "continue to bkpt at common_routine in thread 2 (arg=1)"
265 }
266 }
267 }
268
269 setup_xfail "alpha-*-osf*"
270 if [runto_main] then {
271 clear_xfail "alpha-*-osf*"
272 if [test_startup] then {
273 if [check_control_c] then {
274 warning "Could not stop child with ^C; skipping rest of tests.\n"
275 return;
276 }
277 check_backtraces
278 }
279 }
280 clear_xfail "alpha-*-osf*"