1# Copyright 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
2
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.
7#
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.
12#
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.
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-gdb@gnu.org
19
20#### Dining Philosophers, on LinuxThreads - Jim Blandy <jimb@cygnus.com>
21####
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.
26####
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.
31
32### Other things we ought to test:
33### stepping a thread while others are running
34### killing and restarting
35### quitting gracefully
36
37if $tracelevel then {
38	strace $tracelevel
39}
40
41set prms_id 0
42set bug_id 0
43
44# This only works with Linux configurations.
45if ![istarget *-*-linux-gnu] then {
46    return
47}
48
49set testfile "linux-dp"
50set srcfile ${testfile}.c
51set binfile ${objdir}/${subdir}/${testfile}
52if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug libs=-lpthread}] != ""} {
53    return -1
54}
55
56gdb_start
57gdb_reinitialize_dir $srcdir/$subdir
58gdb_load ${binfile}
59send_gdb "set print sevenbit-strings\n" ; gdb_expect -re "$gdb_prompt $"
60runto_main
61
62# There should be no threads initially.
63gdb_test "info threads" "" "info threads 1"
64
65# Try stepping over the thread creation function.
66gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: create philosopher"]
67set expect_manager -1
68for {set i 0} {$i < 5} {incr i} {
69    gdb_continue_to_breakpoint "about to create philosopher: $i"
70    send_gdb "next\n"
71    gdb_expect {
72	-re "\\\[New .*\\\].*\\\[New .*\\\].*$gdb_prompt $" {
73	    # Two threads are created the first time in LinuxThreads,
74	    # where the second is the manager thread.  In NPTL, there is none.
75	    if {$i == 0} {
76		set expect_manager 1
77	    }
78	    pass "create philosopher: $i"
79	}
80	-re "\\\[New .*\\\].*$gdb_prompt $" {
81	    if {$i == 0} {
82		set expect_manager 0
83	    }
84	    pass "create philosopher: $i"
85	}
86	-re "Program received signal.*(Unknown signal|SIGUSR|Real-time event).*$gdb_prompt $" {
87	    # It would be nice if we could catch the message that GDB prints
88	    # when it first notices that the thread library doesn't support
89	    # debugging, or if we could explicitly ask GDB somehow.
90	    unsupported "This GDB does not support threads on this system."
91	    return -1
92	}
93	-re "$gdb_prompt $" {
94	    fail "create philosopher: $i"
95	}
96	timeout {
97	    fail "(timeout) create philosopher: $i"
98	}
99    }
100}
101
102if {$expect_manager} {
103    set nthreads 7
104} else {
105    set nthreads 6
106}
107
108# Run until there are some threads.
109gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"]
110gdb_continue_to_breakpoint "main thread's sleep"
111set info_threads_ptn ""
112for {set i $nthreads} {$i > 0} {incr i -1} {
113    append info_threads_ptn "$i Thread .*"
114}
115gdb_test "info threads" $info_threads_ptn "info threads 2"
116
117# Try setting a thread-specific breakpoint.
118gdb_breakpoint "print_philosopher thread 5"
119gdb_continue_to_breakpoint "thread 5's print"
120gdb_test "where" "print_philosopher.*philosopher.* from .*libpthread.*" \
121	"first thread-specific breakpoint hit"
122
123# Make sure it's catching the right thread.  Try hitting the
124# breakpoint ten times, and make sure we don't get anyone else.
125set only_five 1
126for {set i 0} {$only_five > 0 && $i < 10} {incr i} {
127    gdb_continue_to_breakpoint "thread 5's print, pass: $i"
128    send_gdb "info threads\n"
129    gdb_expect {
130	-re "\\* 5 Thread .*  print_philosopher .*\r\n$gdb_prompt $" {
131	    # Okay this time.
132	}
133	-re ".*$gdb_prompt $" {
134	    set only_five 0
135	}
136	timeout {
137	    set only_five -1
138	}
139    }
140}
141
142set name "thread-specific breakpoint is thread-specific"
143if {$only_five ==  1} { pass $name }
144if {$only_five ==  0} { fail $name }
145if {$only_five == -1} { fail "$name (timeout)" }
146
147
148### Select a particular thread.
149proc select_thread {thread} {
150    global gdb_prompt
151
152    send_gdb "thread $thread\n"
153    gdb_expect {
154	-re "\\\[Switching to thread .*\\\].*\r\n$gdb_prompt $" {
155	    pass "selected thread: $thread"
156	}
157	-re "$gdb_prompt $" {
158	    fail "selected thread: $thread"
159	}
160	timeout {
161	    fail "selected thread: $thread (timeout)"
162	}
163    }
164}
165
166### Select THREAD, check for a plausible backtrace, and make sure
167### we're actually selecting a different philosopher each time.
168### Return true if the thread had a stack which was not only
169### acceptable, but interesting.  SEEN should be an array in which
170### SEEN(N) exists iff we have found philosopher number N before.
171
172set main_seen 0
173set manager_seen 0
174
175proc check_philosopher_stack {thread seen_name} {
176    global gdb_prompt
177    upvar $seen_name seen
178    global main_seen
179    global expect_manager manager_seen
180
181    set name "philosopher is distinct: $thread"
182    set interesting 0
183
184    select_thread $thread
185    send_gdb "where\n"
186    gdb_expect {
187	-re ".* in philosopher \\(data=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
188	    set data $expect_out(1,string)
189	    if {[info exists seen($data)]} {
190		fail $name
191	    } else {
192		pass $name
193		set seen($data) yep
194	    }
195	    set interesting 1
196	}
197	-re ".* in __pthread_manager \\(.*$gdb_prompt $" {
198	    if {$manager_seen == 1} {
199		fail "manager thread is distinct: $thread"
200	    } else {
201		set manager_seen 1
202		pass "manager thread is distinct: $thread"
203	    }
204	    set interesting 1
205	}
206	-re "pthread_start_thread.*\r\n$gdb_prompt $" {
207	    ## Maybe the thread hasn't started yet.
208	    pass $name
209	}
210	-re ".* in main \\(.*$gdb_prompt $" {
211	    if {$main_seen == 1} {
212		fail "main is distinct: $thread"
213	    } else {
214		set main_seen 1
215		pass "main is distinct: $thread"
216	    }
217	    set interesting 1
218	}
219	-re " in \\?\\?.*\r\n$gdb_prompt $" {
220	    ## Sometimes we can't get a backtrace.  I'm going to call
221	    ## this a pass, since we do verify that at least one
222	    ## thread was interesting, so we can get more consistent
223	    ## test suite totals.  But in my heart, I think it should
224	    ## be an xfail.
225	    pass $name
226	}
227	-re "$gdb_prompt $" {
228	    fail $name
229	}
230	timeout {
231	    fail "$name (timeout)"
232	}
233    }
234
235    return $interesting
236}
237
238set any_interesting 0
239array set seen {}
240unset seen
241for {set i 1} {$i <= $nthreads} {incr i} {
242    if [check_philosopher_stack $i seen] {
243	set any_interesting 1
244    }
245}
246
247if {$any_interesting} {
248    pass "found an interesting thread"
249} else {
250    fail "found an interesting thread"
251}
252
253if {$manager_seen == $expect_manager} {
254    pass "manager thread found (not found) when expected"
255} else {
256    fail "manager thread found (not found) when expected"
257}
258