1 # Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2007
2 # Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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\]*"
42 if [istarget "*-*-linux"] then {
43 set target_cflags "-D_MIT_POSIX_THREADS"
48 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
53 # Start with a fresh gdb.
57 gdb_reinitialize_dir $srcdir/$subdir
60 gdb_test "set print sevenbit-strings" ""
61 #gdb_test "set print address off" ""
62 gdb_test "set width 0" ""
64 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
65 # run the program and gdb starts saving and restoring tty states.
66 # On Ultrix, we don't need it and it is really slow (because shell_escape
68 if ![istarget "*-*-ultrix*"] then {
69 gdb_test "shell stty intr '^C'" ""
72 proc all_threads_running {} {
76 # Reset all the counters to zero.
77 gdb_test "set var common_routine::hits=0" ""
78 gdb_test "set var common_routine::from_thread1=0" ""
79 gdb_test "set var common_routine::from_thread2=0" ""
80 gdb_test "set var common_routine::from_main=0" ""
81 gdb_test "set var common_routine::full_coverage=0" ""
83 # Disable all breakpoints.
86 # Set up a breakpoint that will cause us to stop when we have
87 # been called 15 times. This should be plenty of time to allow
88 # every thread to run at least once, since each thread sleeps for
89 # one second between calls to common_routine.
90 gdb_test "tbreak common_routine if hits >= 15" ""
92 # Start all the threads running again and wait for the inferior
93 # to stop. Since no other breakpoints are set at this time
94 # we should stop only when we have been previously called 15 times.
98 -re "Continuing.*common_routine.*at.*$srcfile.*$gdb_prompt $" {}
100 fail "continue until common routine run 15 times"
104 fail "continue until common routine run 15 times (timeout)"
109 # Check that we stopped when we actually expected to stop, by
110 # verifying that there have been 15 previous hits.
112 # NOTE: Because of synchronization behavior, it is possible for
113 # more than one thread to increment "hits" between one breakpoint
114 # trap and the next. So stopping after 16 or 17 hits should be
115 # considered acceptable.
117 send_gdb "p common_routine::hits\n"
119 -re ".*= 15\r\n$gdb_prompt $" {
120 pass "stopped before calling common_routine 15 times"
122 -re ".*= 16\r\n$gdb_prompt $" {
123 pass "stopped before calling common_routine 15 times (16 times)"
125 -re ".*= 17\r\n$gdb_prompt $" {
126 pass "stopped before calling common_routine 15 times (17 times)"
129 fail "stopped before calling common_routine 15 times"
132 -re ".*$gdb_prompt $" {
133 fail "stopped before calling common_routine 15 times"
137 fail "stopped before calling common_routine 15 times (timeout)"
142 # Also check that all of the threads have run, which will only be true
143 # if the full_coverage variable is set.
145 send_gdb "p common_routine::full_coverage\n"
147 -re ".* = 1.*$gdb_prompt $" {}
148 -re ".* = 0.*$gdb_prompt $" {
149 fail "some threads didn't run"
153 fail "some threads didn't run"
157 fail "some threads didn't run (timeout)"
162 # Looks fine, return success.
166 proc test_startup {} {
167 global srcdir srcfile gdb_prompt expect_out
169 global main_id thread1_id thread2_id
171 # We should be able to do an info threads before starting any others.
172 send_gdb "info threads\n"
174 -re ".*Thread.*main.*$gdb_prompt $" {
177 -re "\r\n$gdb_prompt $" {
178 unsupported "gdb does not support pthreads for this machine"
183 # Extract the thread id number of main thread from "info threads" output.
184 send_gdb "info threads\n"
185 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}main.*)($gdb_prompt $)"
186 set main_id $expect_out(1,string)
188 # Check that we can continue and create the first thread.
189 gdb_test "break thread1" "Breakpoint .* file .*$srcfile.*"
190 gdb_test "continue" \
191 "Continuing.*Breakpoint .*, thread1 \\(arg=0xfeedface\\).*at.*$srcfile.*" \
192 "Continue to creation of first thread"
193 gdb_test "disable" ""
195 # Extract the thread id number of thread 1 from "info threads" output.
196 send_gdb "info threads\n"
197 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}thread1.*)($gdb_prompt $)"
198 set thread1_id $expect_out(1,string)
200 # Check that we can continue and create the second thread,
201 # ignoring the first thread for the moment.
202 gdb_test "break thread2" "Breakpoint .* file .*$srcfile.*"
203 gdb_test "continue" \
204 "Continuing.*Breakpoint .*, thread2 \\(arg=0xdeadbeef\\).*at.*$srcfile.*" \
205 "Continue to creation of second thread"
207 # Extract the thread id number of thread 2 from "info threads" output.
208 send_gdb "info threads\n"
209 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}thread2.*)($gdb_prompt $)"
210 set thread2_id $expect_out(1,string)
215 proc check_control_c {} {
218 # Verify that all threads are running.
219 if [all_threads_running] then {
220 pass "All threads running after startup"
223 # Send a continue followed by ^C to the process to stop it.
224 send_gdb "continue\n"
227 pass "Continue with all threads running"
230 fail "Continue with all threads running (timeout)"
235 set description "Stopped with a ^C"
237 -re "Program received signal SIGINT.*$gdb_prompt $" {
240 -re "Quit.*$gdb_prompt $" {
244 fail "$description (timeout)"
250 # Verify that all threads can be run again after a ^C stop.
251 if [all_threads_running] then {
252 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 pass "continue to bkpt at common_routine in thread 2"
290 send_gdb "backtrace\n"
292 -re "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" {
293 pass "backtrace from thread 2 bkpt in common_routine"
296 fail "backtrace from thread 2 bkpt in common_routine"
299 fail "backtrace from thread 2 bkpt in common_routine (timeout)"
303 -re "Breakpoint .* common_routine \\(arg=0\\).*" {
304 fail "continue to bkpt at common_routine in thread 2 (arg=0)"
306 -re "Breakpoint .* common_routine \\(arg=1\\).*" {
307 fail "continue to bkpt at common_routine in thread 2 (arg=1)"
309 -re ".*$gdb_prompt" {
310 fail "continue to bkpt at common_routine in thread 2"
313 fail "continue to bkpt at common_routine in thread 2 (default)"
316 fail "continue to bkpt at common_routine in thread 2 (timeout)"
321 setup_xfail "alpha-*-osf*"
322 if [runto_main] then {
323 clear_xfail "alpha-*-osf*"
324 if [test_startup] then {
325 if [check_control_c] then {
326 warning "Could not stop child with ^C; skipping rest of tests.\n"
332 clear_xfail "alpha-*-osf*"