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 2 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, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-gdb@prep.ai.mit.edu
21 # This file was written by Fred Fish. (fnf@cygnus.com)
30 # This only works with native configurations
35 set testfile "pthreads"
36 set srcfile ${testfile}.c
37 set binfile ${objdir}/${subdir}/${testfile}
39 # regexp for "horizontal" text (i.e. doesn't include newline or
41 set horiz "\[^\n\r\]*"
43 if [istarget "*-*-linux"] then {
44 set target_cflags "-D_MIT_POSIX_THREADS"
49 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
54 # Start with a fresh gdb.
58 gdb_reinitialize_dir $srcdir/$subdir
61 gdb_test "set print sevenbit-strings" ""
62 #gdb_test "set print address off" ""
63 gdb_test "set width 0" ""
65 # We'll need this when we send_gdb a ^C to GDB. Need to do it before we
66 # run the program and gdb starts saving and restoring tty states.
67 # On Ultrix, we don't need it and it is really slow (because shell_escape
69 if ![istarget "*-*-ultrix*"] then {
70 gdb_test "shell stty intr '^C'" ""
73 proc all_threads_running {} {
77 # Reset all the counters to zero.
78 gdb_test "set var common_routine::hits=0" ""
79 gdb_test "set var common_routine::from_thread1=0" ""
80 gdb_test "set var common_routine::from_thread2=0" ""
81 gdb_test "set var common_routine::from_main=0" ""
82 gdb_test "set var common_routine::full_coverage=0" ""
84 # Disable all breakpoints.
87 # Set up a breakpoint that will cause us to stop when we have
88 # been called 15 times. This should be plenty of time to allow
89 # every thread to run at least once, since each thread sleeps for
90 # one second between calls to common_routine.
91 gdb_test "tbreak common_routine if hits >= 15" ""
93 # Start all the threads running again and wait for the inferior
94 # to stop. Since no other breakpoints are set at this time
95 # we should stop only when we have been previously called 15 times.
99 -re "Continuing.*common_routine.*at.*$srcfile.*$gdb_prompt $" {}
101 fail "continue until common routine run 15 times"
105 fail "continue until common routine run 15 times (timeout)"
110 # Check that we stopped when we actually expected to stop, by
111 # verifying that there have been 15 previous hits.
113 # NOTE: Because of synchronization behavior, it is possible for
114 # more than one thread to increment "hits" between one breakpoint
115 # trap and the next. So stopping after 16 or 17 hits should be
116 # considered acceptable.
118 send_gdb "p common_routine::hits\n"
120 -re ".*= 15\r\n$gdb_prompt $" {
121 pass "stopped before calling common_routine 15 times"
123 -re ".*= 16\r\n$gdb_prompt $" {
124 pass "stopped before calling common_routine 15 times (16 times)"
126 -re ".*= 17\r\n$gdb_prompt $" {
127 pass "stopped before calling common_routine 15 times (17 times)"
130 fail "stopped before calling common_routine 15 times"
133 -re ".*$gdb_prompt $" {
134 fail "stopped before calling common_routine 15 times"
138 fail "stopped before calling common_routine 15 times (timeout)"
143 # Also check that all of the threads have run, which will only be true
144 # if the full_coverage variable is set.
146 send_gdb "p common_routine::full_coverage\n"
148 -re ".* = 1.*$gdb_prompt $" {}
149 -re ".* = 0.*$gdb_prompt $" {
150 fail "some threads didn't run"
154 fail "some threads didn't run"
158 fail "some threads didn't run (timeout)"
163 # Looks fine, return success.
167 proc test_startup {} {
168 global srcdir srcfile gdb_prompt expect_out
170 global main_id thread1_id thread2_id
172 # We should be able to do an info threads before starting any others.
173 send_gdb "info threads\n"
175 -re ".*Thread.*main.*$gdb_prompt $" {
178 -re "\r\n$gdb_prompt $" {
179 unsupported "gdb does not support pthreads for this machine"
184 # Extract the thread id number of main thread from "info threads" output.
185 send_gdb "info threads\n"
186 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}main.*)($gdb_prompt $)"
187 set main_id $expect_out(1,string)
189 # Check that we can continue and create the first thread.
190 gdb_test "break thread1" "Breakpoint .* file .*$srcfile.*"
191 gdb_test "continue" \
192 "Continuing.*Breakpoint .*, thread1 \\(arg=0xfeedface\\).*at.*$srcfile.*" \
193 "Continue to creation of first thread"
194 gdb_test "disable" ""
196 # Extract the thread id number of thread 1 from "info threads" output.
197 send_gdb "info threads\n"
198 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}thread1.*)($gdb_prompt $)"
199 set thread1_id $expect_out(1,string)
201 # Check that we can continue and create the second thread,
202 # ignoring the first thread for the moment.
203 gdb_test "break thread2" "Breakpoint .* file .*$srcfile.*"
204 gdb_test "continue" \
205 "Continuing.*Breakpoint .*, thread2 \\(arg=0xdeadbeef\\).*at.*$srcfile.*" \
206 "Continue to creation of second thread"
208 # Extract the thread id number of thread 2 from "info threads" output.
209 send_gdb "info threads\n"
210 gdb_expect -re "(\[0-9\]+)(${horiz}Thread${horiz}thread2.*)($gdb_prompt $)"
211 set thread2_id $expect_out(1,string)
216 proc check_control_c {} {
219 # Verify that all threads are running.
220 if [all_threads_running] then {
221 pass "All threads running after startup"
224 # Send a continue followed by ^C to the process to stop it.
225 send_gdb "continue\n"
228 pass "Continue with all threads running"
231 fail "Continue with all threads running (timeout)"
236 set description "Stopped with a ^C"
238 -re "Program received signal SIGINT.*$gdb_prompt $" {
241 -re "Quit.*$gdb_prompt $" {
245 fail "$description (timeout)"
251 # Verify that all threads can be run again after a ^C stop.
252 if [all_threads_running] then {
253 pass "All threads running after continuing from ^C stop"
258 proc check_backtraces {} {
259 global gdb_prompt main_id thread1_id thread2_id
261 # Check that the "thread apply N backtrace" command works
263 gdb_test "thread apply $main_id backtrace" \
264 ".* in main \\(argc=.*, argv=.*\\).*" \
265 "check backtrace from main thread"
266 gdb_test "thread apply $thread1_id backtrace" \
267 ".* in thread1 \\(arg=0xfeedface\\).*" \
268 "check backtrace from thread 1"
269 gdb_test "thread apply $thread2_id backtrace" \
270 ".* in thread2 \\(arg=0xdeadbeef\\).*" \
271 "check backtrace from thread 2"
273 # Check that we can apply the backtrace command to all
274 # three threads with a single gdb command
276 gdb_test "thread apply $main_id $thread1_id $thread2_id bt" \
277 ".* in main .* in thread1 .* in thread2.*" \
278 "apply backtrace command to all three threads"
280 # Check that we can do thread specific backtraces
281 # This also tests that we can do thread specific breakpoints.
283 gdb_test "break common_routine thread $thread2_id" \
284 "Breakpoint .* at 0x.* file .* line .*" \
285 "set break at common_routine in thread 2"
287 send_gdb "continue\n"
289 -re "Breakpoint .* common_routine \\(arg=2\\).*" {
290 pass "continue to bkpt at common_routine in thread 2"
291 send_gdb "backtrace\n"
293 -re "#0.*common_routine \\(arg=2\\).*#1.*thread2.*" {
294 pass "backtrace from thread 2 bkpt in common_routine"
297 fail "backtrace from thread 2 bkpt in common_routine"
300 fail "backtrace from thread 2 bkpt in common_routine (timeout)"
304 -re "Breakpoint .* common_routine \\(arg=0\\).*" {
305 fail "continue to bkpt at common_routine in thread 2 (arg=0)"
307 -re "Breakpoint .* common_routine \\(arg=1\\).*" {
308 fail "continue to bkpt at common_routine in thread 2 (arg=1)"
310 -re ".*$gdb_prompt" {
311 fail "continue to bkpt at common_routine in thread 2"
314 fail "continue to bkpt at common_routine in thread 2 (default)"
317 fail "continue to bkpt at common_routine in thread 2 (timeout)"
322 setup_xfail "alpha-*-osf*"
323 if [runto_main] then {
324 clear_xfail "alpha-*-osf*"
325 if [test_startup] then {
326 if [check_control_c] then {
327 warning "Could not stop child with ^C; skipping rest of tests.\n"
333 clear_xfail "alpha-*-osf*"