1 # Copyright (C) 1999 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 2 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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # Please email any bugs, comments, and/or additions to this file to:
20 #### Dining Philosophers, on LinuxThreads - Jim Blandy <jimb@cygnus.com>
22 #### At the moment, GDB's support for LinuxThreads is pretty
23 #### idiosyncratic --- GDB's output doesn't look much like the output
24 #### it produces for other thread implementations, messages appear at
25 #### different times, etc. So these tests are specific to LinuxThreads.
27 #### However, if all goes well, Linux will soon have a libthread_db
28 #### interface, and GDB will manage it the same way it does other
29 #### libthread_db-based systems. Then, we can adjust this file to
30 #### work with any such system.
32 ### Other things we ought to test:
33 ### stepping a thread while others are running
34 ### killing and restarting
35 ### quitting gracefully
44 # This only works with Linux configurations.
45 if ![istarget *-*-linux-gnu] then {
49 set testfile "linux-dp"
50 set srcfile ${testfile}.c
51 set binfile ${objdir}/${subdir}/${testfile}
52 if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug libs=-lpthread}] != ""} {
53 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
57 gdb_reinitialize_dir $srcdir/$subdir
59 send_gdb "set print sevenbit-strings\n" ; gdb_expect -re "$gdb_prompt $"
62 # There should be no threads initially.
63 gdb_test "info threads" "" "info threads 1"
65 # Try stepping over the thread creation function.
66 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: create philosopher"]
67 for {set i 0} {$i < 5} {incr i} {
68 gdb_continue_to_breakpoint "about to create philosopher: $i"
69 gdb_test "next" "\\\[New Thread .*\\\].*" "create philosopher: $i"
72 # Run until there are some threads.
73 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"]
74 gdb_continue_to_breakpoint "main thread's sleep"
75 gdb_test "info threads" "7 Thread .*6 Thread .*5 Thread .*4 Thread .*3 Thread .*2 Thread .* \\(initial thread\\) main \\(argc=1, argv=.*\\) at .*linux-dp.c:.*1 Thread .* \\(manager thread\\).*" "info threads 2"
77 # Try setting a thread-specific breakpoint.
78 gdb_breakpoint "print_philosopher thread 5"
79 gdb_continue_to_breakpoint "thread 5's print"
80 gdb_test "where" "print_philosopher.*philosopher.*pthread_start_thread.*" \
81 "first thread-specific breakpoint hit"
83 # Make sure it's catching the right thread. Try hitting the
84 # breakpoint ten times, and make sure we don't get anyone else.
86 for {set i 0} {$only_five > 0 && $i < 10} {incr i} {
87 gdb_continue_to_breakpoint "thread 5's print, pass: $i"
88 send_gdb "info threads\n"
90 -re "\\* 5 Thread .* print_philosopher .*\r\n$gdb_prompt $" {
93 -re ".*$gdb_prompt $" {
102 set name "thread-specific breakpoint is thread-specific"
103 if {$only_five == 1} { pass $name }
104 if {$only_five == 0} { fail $name }
105 if {$only_five == -1} { fail "$name (timeout)" }
108 ### Select a particular thread.
109 proc select_thread {thread} {
112 send_gdb "thread $thread\n"
114 -re "\\\[Switching to thread .*\\\].*\r\n$gdb_prompt $" {
115 pass "selected thread: $thread"
117 -re "$gdb_prompt $" {
118 fail "selected thread: $thread"
121 fail "selected thread: $thread (timeout)"
126 ### Select THREAD, check for a plausible backtrace, and make sure
127 ### we're actually selecting a different philosopher each time.
128 ### Return true if the thread had a stack which was not only
129 ### acceptable, but interesting. SEEN should be an array in which
130 ### SEEN(N) exists iff we have found philosopher number N before.
131 proc check_philosopher_stack {thread seen_name} {
133 upvar $seen_name seen
135 set name "philosopher is distinct: $thread"
138 select_thread $thread
141 -re ".* in philosopher \\(data=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
142 set data $expect_out(1,string)
143 if {[info exists seen($data)]} {
151 -re "pthread_start_thread.*\r\n$gdb_prompt $" {
152 ## Maybe the thread hasn't started yet.
155 -re " in \\?\\?.*\r\n$gdb_prompt $" {
156 ## Sometimes we can't get a backtrace. I'm going to call
157 ## this a pass, since we do verify that at least one
158 ## thread was interesting, so we can get more consistent
159 ## test suite totals. But in my heart, I think it should
163 -re "$gdb_prompt $" {
167 fail "$name (timeout)"
174 set any_interesting 0
176 for {set i 3} {$i <= 7} {incr i} {
177 if [check_philosopher_stack $i seen] {
178 set any_interesting 1
182 if {$any_interesting} {
183 pass "found an interesting thread"
185 fail "found an interesting thread"