1 # Copyright 1999, 2001-2012 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Please email any bugs, comments, and/or additions to this file to:
19 #### Dining Philosophers, on LinuxThreads - Jim Blandy <jimb@cygnus.com>
21 #### At the moment, GDB's support for LinuxThreads is pretty
22 #### idiosyncratic --- GDB's output doesn't look much like the output
23 #### it produces for other thread implementations, messages appear at
24 #### different times, etc. So these tests are specific to LinuxThreads.
26 #### However, if all goes well, Linux will soon have a libthread_db
27 #### interface, and GDB will manage it the same way it does other
28 #### libthread_db-based systems. Then, we can adjust this file to
29 #### work with any such system.
31 ### Other things we ought to test:
32 ### stepping a thread while others are running
33 ### killing and restarting
34 ### quitting gracefully
37 # This only works with Linux configurations.
38 if ![istarget *-*-linux-gnu*] then {
42 set testfile "linux-dp"
43 set srcfile ${testfile}.c
44 set binfile ${objdir}/${subdir}/${testfile}
45 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != ""} {
50 gdb_reinitialize_dir $srcdir/$subdir
52 gdb_test_no_output "set print sevenbit-strings"
55 # There should be no threads initially.
56 gdb_test "info threads" ".*" "info threads 1"
58 # Try stepping over the thread creation function.
59 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: create philosopher"]
61 for {set i 0} {$i < 5} {incr i} {
62 gdb_continue_to_breakpoint "about to create philosopher: $i"
64 gdb_test_multiple "info threads" "info threads before: $i" {
65 -re "info threads\r\n" {
68 -re "^ *Id.*Frame *\[\r\n\]+" {
71 -re "^. +(\[0-9\]+ *Thread \[-0-9a-fx\]+) \[^\n\]*\n" {
72 verbose -log "found thread $expect_out(1,string)" 2
73 lappend threads_before $expect_out(1,string)
77 verbose -log "skipping line" 2
78 exp_continue -continue_timer
80 -re "^$gdb_prompt $" {
84 gdb_test_multiple "next" "create philosopher: $i" {
88 -re "^ *\[_!\] \[0-9\]* \[_!\]\r\n" {
89 # Ignore program output.
90 exp_continue -continue_timer
92 -re "^\\\[New \[^\]\n\]+\\\]\[^\n\]+\n" {
96 -re "^189\[^\n\]+\n" {
99 -re "^$gdb_prompt $" {
101 -re "Program received signal.*(Unknown signal|SIGUSR|Real-time event).*$gdb_prompt $" {
102 # It would be nice if we could catch the message that GDB prints
103 # when it first notices that the thread library doesn't support
104 # debugging, or if we could explicitly ask GDB somehow.
105 unsupported "This GDB does not support threads on this system."
108 -re "$gdb_prompt $" {
111 if { $threads_created == 0 } {
112 # Not all targets announce new threads as they are created.
113 # For example, the GDB
114 # remote protocol target only finds out about threads when
115 # they actually report some event like a breakpoint hit,
116 # or when the user types 'info threads'.
117 unsupported "create philosopher: $i"
118 } elseif { $threads_created == 1 } {
119 if { $expect_manager < 0 } {
122 pass "create philosopher: $i"
123 } elseif { !$i && $threads_created == 2 } {
124 # Two threads are created the first time in LinuxThreads,
125 # where the second is the manager thread. In NPTL, there is none.
127 pass "create philosopher: $i"
129 fail "create philosopher: $i"
133 gdb_test_multiple "info threads" "info threads after: $i" {
134 -re "info threads\r\n" {
137 -re "^ *Id.*Frame *\[\r\n\]+" {
140 -re "^. +(\[0-9\]+ *Thread \[-0-9a-fx\]+) \[^\n\]*\n" {
141 set name $expect_out(1,string)
142 for {set j 0} {$j != [llength $threads_before] } {incr j} {
143 if {$name == [lindex $threads_before $j]} {
144 set threads_before [lreplace $threads_before $j $j]
150 lappend threads_after $name
155 verbose -log "skipping line" 2
156 exp_continue -continue_timer
158 -re "^$gdb_prompt $" {
159 if { [llength $threads_before] != 0 } {
160 fail "info threads after: $i"
161 } elseif { !$i && [llength $threads_after] == 2 } {
163 pass "info threads after: $i"
164 } elseif { [llength $threads_after] == 1 } {
165 if { $expect_manager < 0 } {
168 pass "info threads after: $i"
170 fail "info threads after: $i"
178 # Run until there are some threads.
179 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"]
180 gdb_continue_to_breakpoint "main thread's sleep"
181 set info_threads_ptn ".*"
182 for {set i $nthreads} {$i > 0} {incr i -1} {
183 append info_threads_ptn "$i *Thread .*"
185 append info_threads_ptn "\[\r\n\]+$gdb_prompt $"
186 set info_threads_manager_ptn "[expr $nthreads + 1] *Thread .*$info_threads_ptn"
188 gdb_test_multiple "info threads" "info threads 2" {
189 -re "$info_threads_manager_ptn" {
190 # We did see a manager thread. Check that against what we expected.
191 switch -exact -- $expect_manager {
193 # We weren't sure whether to expect a manager thread.
194 pass "info threads 2"
197 # We were expecting a manager thread.
198 pass "info threads 2"
201 # We were not expecting to see the manager thread.
202 fail "info threads 2"
208 -re "$info_threads_ptn" {
209 # We did not see a manager thread. Check that against what we
211 switch -exact -- $expect_manager {
213 # We weren't sure whether to expect a manager thread.
214 # Don't expect it from here on out.
215 pass "info threads 2"
218 # We were expecting a manager thread, but we didn't see one.
219 fail "info threads 2"
222 # We were not expecting to see the manager thread.
223 pass "info threads 2"
231 # Try setting a thread-specific breakpoint.
232 gdb_breakpoint "print_philosopher thread 5"
233 gdb_continue_to_breakpoint "thread 5's print"
234 # When there is no debugging info available for the thread library,
235 # the backtrace entry for philosopher's caller looks like:
236 # #2 0x4001c548 in pthread_create () from /lib/libpthread.so.0
237 # If you do have debug info, the output obviously depends more on the
238 # exact library in use; under NPTL, you get:
239 # #2 0x0012b7fc in start_thread (arg=0x21) at pthread_create.c:264
240 gdb_test "where" "print_philosopher.*philosopher.* \(from .*libpthread\|at pthread_create\|in pthread_create\).*" \
241 "first thread-specific breakpoint hit"
243 # Make sure it's catching the right thread. Try hitting the
244 # breakpoint ten times, and make sure we don't get anyone else.
246 for {set i 0} {$only_five > 0 && $i < 10} {incr i} {
247 gdb_continue_to_breakpoint "thread 5's print, pass: $i"
248 gdb_test_multiple "info threads" "" {
249 -re "\[*\] 5 *Thread .* +print_philosopher .*\r\n$gdb_prompt $" {
252 -re ".*$gdb_prompt $" {
261 set name "thread-specific breakpoint is thread-specific"
262 if {$only_five == 1} { pass $name }
263 if {$only_five == 0} { fail $name }
264 if {$only_five == -1} { fail "$name (timeout)" }
267 ### Select a particular thread.
268 proc select_thread {thread} {
271 gdb_test "thread $thread" \
272 "\\\[Switching to thread .*\\\].*" \
273 "selected thread: $thread"
276 ### Select THREAD, check for a plausible backtrace, and make sure
277 ### we're actually selecting a different philosopher each time.
278 ### Return true if the thread had a stack which was not only
279 ### acceptable, but interesting. SEEN should be an array in which
280 ### SEEN(N) exists iff we have found philosopher number N before.
285 proc check_philosopher_stack {thread seen_name} {
287 upvar $seen_name seen
289 global expect_manager manager_seen
291 set name "philosopher is distinct: $thread"
294 select_thread $thread
295 gdb_test_multiple "where" "$name" {
296 -re ".* in philosopher \\(data=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
297 set data $expect_out(1,string)
298 if {[info exists seen($data)]} {
306 -re ".* in __pthread_manager \\(.*$gdb_prompt $" {
307 if {$manager_seen == 1} {
308 fail "manager thread is distinct: $thread"
311 pass "manager thread is distinct: $thread"
315 -re "pthread_start_thread.*\r\n$gdb_prompt $" {
316 ## Maybe the thread hasn't started yet.
319 -re ".* in main \\(.*$gdb_prompt $" {
320 if {$main_seen == 1} {
321 fail "main is distinct: $thread"
324 pass "main is distinct: $thread"
328 -re " in \\?\\?.*\r\n$gdb_prompt $" {
329 ## Sometimes we can't get a backtrace. I'm going to call
330 ## this a pass, since we do verify that at least one
331 ## thread was interesting, so we can get more consistent
332 ## test suite totals. But in my heart, I think it should
341 set any_interesting 0
344 for {set i 1} {$i <= $nthreads} {incr i} {
345 if [check_philosopher_stack $i seen] {
346 set any_interesting 1
350 if {$any_interesting} {
351 pass "found an interesting thread"
353 fail "found an interesting thread"
356 if {$manager_seen == $expect_manager} {
357 pass "manager thread found (not found) when expected"
359 fail "manager thread found (not found) when expected"