1# Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008,
2# 2009, 2010, 2011 Free Software Foundation, Inc.
3
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.
8#
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.
13#
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/>.
16
17# This file was written by Fred Fish. (fnf@cygnus.com)
18
19if $tracelevel then {
20	strace $tracelevel
21}
22
23
24# This only works with native configurations
25if ![isnative] then {
26    return
27}
28
29set testfile "pthreads"
30set srcfile ${testfile}.c
31set binfile ${objdir}/${subdir}/${testfile}
32
33# regexp for "horizontal" text (i.e. doesn't include newline or
34# carriage return)
35set horiz "\[^\n\r\]*"
36
37if [istarget "*-*-linux"] then {
38    set target_cflags "-D_MIT_POSIX_THREADS"
39} else {
40    set target_cflags ""
41}
42
43if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
44    return -1
45}
46
47
48# Start with a fresh gdb.
49
50gdb_exit
51gdb_start
52gdb_reinitialize_dir $srcdir/$subdir
53gdb_load ${binfile}
54
55gdb_test_no_output "set print sevenbit-strings"
56#gdb_test_no_output "set print address off"
57gdb_test_no_output "set width 0"
58
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
62# doesn't use vfork).
63if ![istarget "*-*-ultrix*"] then {
64    gdb_test "shell stty intr '^C'" ".*"
65}
66
67proc all_threads_running {} {
68    global gdb_prompt
69    global srcfile
70
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"
77
78    # Disable all breakpoints.
79    gdb_test_no_output "disable"
80
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" ".*"
86
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.
90
91    set return_me 1
92
93    gdb_test_multiple "continue" "continue until common routine run 15 times" {
94	-re "Continuing.*common_routine.*at.*$srcfile.*$gdb_prompt $" {
95	    set return_me 0
96	}
97    }
98
99    if { $return_me == 1 } then {
100	return 0;
101    }
102
103    # Check that we stopped when we actually expected to stop, by
104    # verifying that there have been 15 previous hits.
105
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.
110
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"
115	    }
116	    -re ".*= 16\r\n$gdb_prompt $" {
117		pass "stopped before calling common_routine 15 times (16 times)"
118	    }
119	    -re ".*= 17\r\n$gdb_prompt $" {
120		pass "stopped before calling common_routine 15 times (17 times)"
121	    }
122	}
123
124    # Also check that all of the threads have run, which will only be true
125    # if the full_coverage variable is set.
126
127    set return_me 1
128    gdb_test_multiple "p common_routine::full_coverage" \
129	"some threads didn't run" {
130	    -re ".* = 1.*$gdb_prompt $" {
131	    }
132	    -re ".* = 0.*$gdb_prompt $" {
133		fail "some threads didn't run"
134		set return_me 0
135	    }
136	}
137
138    # Looks fine, return success.
139    return $return_me
140}
141
142proc test_startup {} {
143    global srcdir srcfile gdb_prompt expect_out
144    global horiz
145    global main_id thread1_id thread2_id
146
147    # We should be able to do an info threads before starting any others.
148    set return_me 1
149    gdb_test_multiple "info threads" "info threads" {
150	-re ".*Thread.*main.*$gdb_prompt $" {
151	    pass "info threads"
152	    set return_me 0
153	}
154	-re "\r\n$gdb_prompt $" {
155	    unsupported "gdb does not support pthreads for this machine"
156	}
157    }
158
159    if { $return_me == 1 } then {
160	return 0;
161    }
162
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 $)" {
166	}
167    }
168
169    set main_id $expect_out(1,string)
170
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"
177
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 $)" {
181	}
182    }
183
184    set thread1_id $expect_out(1,string)
185
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"
192
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 $)" {
196	}
197    }
198
199    set thread2_id $expect_out(1,string)
200
201    return 1
202}
203
204proc check_control_c {} {
205    global gdb_prompt
206
207    # Verify that all threads are running.
208    if [all_threads_running] then {
209	pass "All threads running after startup"
210    }
211
212    # Send a continue followed by ^C to the process to stop it.
213    gdb_test_multiple "continue" "continue with all threads running" {
214	-re "Continuing." {
215	    pass "Continue with all threads running"
216	}
217    }
218    after 2000
219    send_gdb "\003"
220    set description "Stopped with a ^C"
221    gdb_expect {
222	-re "Program received signal SIGINT.*$gdb_prompt $" {
223	    pass $description
224	}
225	-re "Quit.*$gdb_prompt $" {
226	    pass $description
227	}
228	timeout {
229	    fail "$description (timeout)"
230	    return 1;
231	}
232    }
233    gdb_test "bt" ".*"
234
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"
238    }
239    return 0;
240}
241
242proc check_backtraces {} {
243    global gdb_prompt main_id thread1_id thread2_id
244
245    # Check that the "thread apply N backtrace" command works
246
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"
256
257    # Check that we can apply the backtrace command to all
258    # three threads with a single gdb command
259
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"
263
264    # Check that we can do thread specific backtraces
265    # This also tests that we can do thread specific breakpoints.
266
267    gdb_test "break common_routine thread $thread2_id" \
268	    "Breakpoint .* at 0x.* file .* line .*" \
269	    "set break at common_routine in thread 2"
270
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"
277	}
278	-re "Breakpoint .* common_routine \\(arg=0\\).*$gdb_prompt $" {
279	    fail "continue to bkpt at common_routine in thread 2 (arg=0)"
280	}
281	-re "Breakpoint .* common_routine \\(arg=1\\).*$gdb_prompt $" {
282	    fail "continue to bkpt at common_routine in thread 2 (arg=1)"
283	}
284    }
285}
286
287setup_xfail "alpha-*-osf*"
288if [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"
293	    return;
294	}
295	check_backtraces
296    }
297}
298clear_xfail "alpha-*-osf*"
299