1 # Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008,
2 # 2009, 2010, 2011 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 # This file was written by Fred Fish. (fnf@cygnus.com)
24 # This only works with native configurations
29 set testfile "pthreads"
30 set srcfile ${testfile}.c
31 set binfile ${objdir}/${subdir}/${testfile}
33 # regexp for "horizontal" text (i.e. doesn't include newline or
35 set horiz "\[^\n\r\]*"
37 if [istarget "*-*-linux"] then {
38 set target_cflags "-D_MIT_POSIX_THREADS"
43 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
48 # Start with a fresh gdb.
52 gdb_reinitialize_dir $srcdir/$subdir
55 gdb_test_no_output "set print sevenbit-strings"
56 #gdb_test_no_output "set print address off"
57 gdb_test_no_output "set width 0"
59 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
60 # run the program and gdb starts saving and restoring tty states.
61 # On Ultrix, we don't need it and it is really slow (because shell_escape
63 if ![istarget "*-*-ultrix*"] then {
64 gdb_test "shell stty intr '^C'" ".*"
67 proc all_threads_running {} {
71 # Reset all the counters to zero.
72 gdb_test_no_output "set var common_routine::hits=0"
73 gdb_test_no_output "set var common_routine::from_thread1=0"
74 gdb_test_no_output "set var common_routine::from_thread2=0"
75 gdb_test_no_output "set var common_routine::from_main=0"
76 gdb_test_no_output "set var common_routine::full_coverage=0"
78 # Disable all breakpoints.
79 gdb_test_no_output "disable"
81 # Set up a breakpoint that will cause us to stop when we have
82 # been called 15 times. This should be plenty of time to allow
83 # every thread to run at least once, since each thread sleeps for
84 # one second between calls to common_routine.
85 gdb_test "tbreak common_routine if hits >= 15" ".*"
87 # Start all the threads running again and wait for the inferior
88 # to stop. Since no other breakpoints are set at this time
89 # we should stop only when we have been previously called 15 times.
93 gdb_test_multiple "continue" "continue until common routine run 15 times" {
94 -re "Continuing.*common_routine.*at.*$srcfile.*$gdb_prompt $" {
99 if { $return_me == 1 } then {
103 # Check that we stopped when we actually expected to stop, by
104 # verifying that there have been 15 previous hits.
106 # NOTE: Because of synchronization behavior, it is possible for
107 # more than one thread to increment "hits" between one breakpoint
108 # trap and the next. So stopping after 16 or 17 hits should be
109 # considered acceptable.
111 gdb_test_multiple "p common_routine::hits" \
112 "stopped before calling common_routine 15 times" {
113 -re ".*= 15\r\n$gdb_prompt $" {
114 pass "stopped before calling common_routine 15 times"
116 -re ".*= 16\r\n$gdb_prompt $" {
117 pass "stopped before calling common_routine 15 times (16 times)"
119 -re ".*= 17\r\n$gdb_prompt $" {
120 pass "stopped before calling common_routine 15 times (17 times)"
124 # Also check that all of the threads have run, which will only be true
125 # if the full_coverage variable is set.
128 gdb_test_multiple "p common_routine::full_coverage" \
129 "some threads didn't run" {
130 -re ".* = 1.*$gdb_prompt $" {
132 -re ".* = 0.*$gdb_prompt $" {
133 fail "some threads didn't run"
138 # Looks fine, return success.
142 proc test_startup {} {
143 global srcdir srcfile gdb_prompt expect_out
145 global main_id thread1_id thread2_id
147 # We should be able to do an info threads before starting any others.
149 gdb_test_multiple "info threads" "info threads" {
150 -re ".*Thread.*main.*$gdb_prompt $" {
154 -re "\r\n$gdb_prompt $" {
155 unsupported "gdb does not support pthreads for this machine"
159 if { $return_me == 1 } then {
163 # Extract the thread id number of main thread from "info threads" output.
164 gdb_test_multiple "info threads" "get main thread id" {
165 -re "(\[0-9\]+)(${horiz}Thread${horiz}main.*)($gdb_prompt $)" {
169 set main_id $expect_out(1,string)
171 # Check that we can continue and create the first thread.
172 gdb_test "break thread1" "Breakpoint .* file .*$srcfile.*"
173 gdb_test "continue" \
174 "Continuing.*Breakpoint .*, thread1 \\(arg=0xfeedface\\).*at.*$srcfile.*" \
175 "Continue to creation of first thread"
176 gdb_test_no_output "disable"
178 # Extract the thread id number of thread 1 from "info threads" output.
179 gdb_test_multiple "info threads" "get thread 1 id" {
180 -re "(\[0-9\]+)(${horiz}Thread${horiz}thread1.*)($gdb_prompt $)" {
184 set thread1_id $expect_out(1,string)
186 # Check that we can continue and create the second thread,
187 # ignoring the first thread for the moment.
188 gdb_test "break thread2" "Breakpoint .* file .*$srcfile.*"
189 gdb_test "continue" \
190 "Continuing.*Breakpoint .*, thread2 \\(arg=0xdeadbeef\\).*at.*$srcfile.*" \
191 "Continue to creation of second thread"
193 # Extract the thread id number of thread 2 from "info threads" output.
194 gdb_test_multiple "info threads" "get thread 2 id" {
195 -re "(\[0-9\]+)(${horiz}Thread${horiz}thread2.*)($gdb_prompt $)" {
199 set thread2_id $expect_out(1,string)
204 proc check_control_c {} {
207 # Verify that all threads are running.
208 if [all_threads_running] then {
209 pass "All threads running after startup"
212 # Send a continue followed by ^C to the process to stop it.
213 gdb_test_multiple "continue" "continue with all threads running" {
215 pass "Continue with all threads running"
220 set description "Stopped with a ^C"
222 -re "Program received signal SIGINT.*$gdb_prompt $" {
225 -re "Quit.*$gdb_prompt $" {
229 fail "$description (timeout)"
235 # Verify that all threads can be run again after a ^C stop.
236 if [all_threads_running] then {
237 pass "All threads running after continuing from ^C stop"
242 proc check_backtraces {} {
243 global gdb_prompt main_id thread1_id thread2_id
245 # Check that the "thread apply N backtrace" command works
247 gdb_test "thread apply $main_id backtrace" \
248 ".* in main \\(argc=.*, argv=.*\\).*" \
249 "check backtrace from main thread"
250 gdb_test "thread apply $thread1_id backtrace" \
251 ".* in thread1 \\(arg=0xfeedface\\).*" \
252 "check backtrace from thread 1"
253 gdb_test "thread apply $thread2_id backtrace" \
254 ".* in thread2 \\(arg=0xdeadbeef\\).*" \
255 "check backtrace from thread 2"
257 # Check that we can apply the backtrace command to all
258 # three threads with a single gdb command
260 gdb_test "thread apply $main_id $thread1_id $thread2_id bt" \
261 ".* in main .* in thread1 .* in thread2.*" \
262 "apply backtrace command to all three threads"
264 # Check that we can do thread specific backtraces
265 # This also tests that we can do thread specific breakpoints.
267 gdb_test "break common_routine thread $thread2_id" \
268 "Breakpoint .* at 0x.* file .* line .*" \
269 "set break at common_routine in thread 2"
271 gdb_test_multiple "continue" "continue to bkpt at common_routine in thread 2" {
272 -re "Breakpoint .* common_routine \\(arg=2\\).*$gdb_prompt $" {
273 pass "continue to bkpt at common_routine in thread 2"
274 gdb_test "backtrace" \
275 "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" \
276 "backtrace from thread 2 bkpt in common_routine"
278 -re "Breakpoint .* common_routine \\(arg=0\\).*$gdb_prompt $" {
279 fail "continue to bkpt at common_routine in thread 2 (arg=0)"
281 -re "Breakpoint .* common_routine \\(arg=1\\).*$gdb_prompt $" {
282 fail "continue to bkpt at common_routine in thread 2 (arg=1)"
287 setup_xfail "alpha-*-osf*"
288 if [runto_main] then {
289 clear_xfail "alpha-*-osf*"
290 if [test_startup] then {
291 if [check_control_c] then {
292 warning "Could not stop child with ^C; skipping rest of tests.\n"
298 clear_xfail "alpha-*-osf*"