2010-06-02 Michael Snyder <msnyder@msnyder-server.eng.vmware.com>
[binutils-gdb.git] / gdb / testsuite / gdb.threads / linux-dp.exp
1 # Copyright 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
2 # 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 # 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
37 if $tracelevel then {
38 strace $tracelevel
39 }
40
41
42 # This only works with Linux configurations.
43 if ![istarget *-*-linux-gnu*] then {
44 return
45 }
46
47 set testfile "linux-dp"
48 set srcfile ${testfile}.c
49 set binfile ${objdir}/${subdir}/${testfile}
50 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != ""} {
51 return -1
52 }
53
54 gdb_start
55 gdb_reinitialize_dir $srcdir/$subdir
56 gdb_load ${binfile}
57 gdb_test_no_output "set print sevenbit-strings"
58 runto_main
59
60 # There should be no threads initially.
61 gdb_test "info threads" "" "info threads 1"
62
63 # Try stepping over the thread creation function.
64 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: create philosopher"]
65 set expect_manager -1
66 for {set i 0} {$i < 5} {incr i} {
67 gdb_continue_to_breakpoint "about to create philosopher: $i"
68 set threads_before {}
69 gdb_test_multiple "info threads" "info threads before: $i" {
70 -re "info threads\r\n" {
71 exp_continue
72 }
73 -re "^. +(\[0-9\]+ Thread \[-0-9a-fx\]+) \[^\n\]*\n" {
74 verbose -log "found thread $expect_out(1,string)" 2
75 lappend threads_before $expect_out(1,string)
76 exp_continue
77 }
78 -re "^\[^\n\]*\n" {
79 verbose -log "skipping line" 2
80 exp_continue -continue_timer
81 }
82 -re "^$gdb_prompt $" {
83 }
84 }
85 set threads_created 0
86 gdb_test_multiple "next" "create philosopher: $i" {
87 -re "^next\r\n" {
88 exp_continue
89 }
90 -re "^ *\[_!\] \[0-9\]* \[_!\]\r\n" {
91 # Ignore program output.
92 exp_continue -continue_timer
93 }
94 -re "^\\\[New \[^\]\n\]+\\\]\[^\n\]+\n" {
95 incr threads_created
96 exp_continue
97 }
98 -re "^189\[^\n\]+\n" {
99 exp_continue
100 }
101 -re "^$gdb_prompt $" {
102 }
103 -re "Program received signal.*(Unknown signal|SIGUSR|Real-time event).*$gdb_prompt $" {
104 # It would be nice if we could catch the message that GDB prints
105 # when it first notices that the thread library doesn't support
106 # debugging, or if we could explicitly ask GDB somehow.
107 unsupported "This GDB does not support threads on this system."
108 return -1
109 }
110 -re "$gdb_prompt $" {
111 }
112 }
113 if { $threads_created == 0 } {
114 # Not all targets announce new threads as they are created.
115 # For example, the GDB
116 # remote protocol target only finds out about threads when
117 # they actually report some event like a breakpoint hit,
118 # or when the user types 'info threads'.
119 unsupported "create philosopher: $i"
120 } elseif { $threads_created == 1 } {
121 if { $expect_manager < 0 } {
122 set expect_manager 0
123 }
124 pass "create philosopher: $i"
125 } elseif { !$i && $threads_created == 2 } {
126 # Two threads are created the first time in LinuxThreads,
127 # where the second is the manager thread. In NPTL, there is none.
128 set expect_manager 1
129 pass "create philosopher: $i"
130 } else {
131 fail "create philosopher: $i"
132 }
133
134 set threads_after {}
135 gdb_test_multiple "info threads" "info threads after: $i" {
136 -re "info threads\r\n" {
137 exp_continue
138 }
139 -re "^. +(\[0-9\]+ Thread \[-0-9a-fx\]+) \[^\n\]*\n" {
140 set name $expect_out(1,string)
141 for {set j 0} {$j != [llength $threads_before] } {incr j} {
142 if {$name == [lindex $threads_before $j]} {
143 set threads_before [lreplace $threads_before $j $j]
144 set name ""
145 break
146 }
147 }
148 if { $name != "" } {
149 lappend threads_after $name
150 }
151 exp_continue
152 }
153 -re "^\[^\n\]*\n" {
154 verbose -log "skipping line" 2
155 exp_continue -continue_timer
156 }
157 -re "^$gdb_prompt $" {
158 if { [llength $threads_before] != 0 } {
159 fail "info threads after: $i"
160 } elseif { !$i && [llength $threads_after] == 2 } {
161 set expect_manager 1
162 pass "info threads after: $i"
163 } elseif { [llength $threads_after] == 1 } {
164 if { $expect_manager < 0 } {
165 set expect_manager 0
166 }
167 pass "info threads after: $i"
168 } else {
169 fail "info threads after: $i"
170 }
171 }
172 }
173 }
174
175 set nthreads 6
176
177 # Run until there are some threads.
178 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"]
179 gdb_continue_to_breakpoint "main thread's sleep"
180 set info_threads_ptn ""
181 for {set i $nthreads} {$i > 0} {incr i -1} {
182 append info_threads_ptn "$i Thread .*"
183 }
184 append info_threads_ptn "\[\r\n\]+$gdb_prompt $"
185 set info_threads_manager_ptn "[expr $nthreads + 1] Thread .*$info_threads_ptn"
186
187 gdb_test_multiple "info threads" "info threads 2" {
188 -re "$info_threads_manager_ptn" {
189 # We did see a manager thread. Check that against what we expected.
190 switch -exact -- $expect_manager {
191 -1 {
192 # We weren't sure whether to expect a manager thread.
193 pass "info threads 2"
194 }
195 1 {
196 # We were expecting a manager thread.
197 pass "info threads 2"
198 }
199 0 {
200 # We were not expecting to see the manager thread.
201 fail "info threads 2"
202 }
203 }
204 set expect_manager 1
205 incr nthreads
206 }
207 -re "$info_threads_ptn" {
208 # We did not see a manager thread. Check that against what we
209 # expected.
210 switch -exact -- $expect_manager {
211 -1 {
212 # We weren't sure whether to expect a manager thread.
213 # Don't expect it from here on out.
214 pass "info threads 2"
215 }
216 1 {
217 # We were expecting a manager thread, but we didn't see one.
218 fail "info threads 2"
219 }
220 0 {
221 # We were not expecting to see the manager thread.
222 pass "info threads 2"
223 }
224 }
225 set expect_manager 0
226 }
227 }
228
229
230 # Try setting a thread-specific breakpoint.
231 gdb_breakpoint "print_philosopher thread 5"
232 gdb_continue_to_breakpoint "thread 5's print"
233 # When there is no debugging info available for the thread library,
234 # the backtrace entry for philosopher's caller looks like:
235 # #2 0x4001c548 in pthread_create () from /lib/libpthread.so.0
236 # If you do have debug info, the output obviously depends more on the
237 # exact library in use; under NPTL, you get:
238 # #2 0x0012b7fc in start_thread (arg=0x21) at pthread_create.c:264
239 gdb_test "where" "print_philosopher.*philosopher.* \(from .*libpthread\|at pthread_create\|in pthread_create\).*" \
240 "first thread-specific breakpoint hit"
241
242 # Make sure it's catching the right thread. Try hitting the
243 # breakpoint ten times, and make sure we don't get anyone else.
244 set only_five 1
245 for {set i 0} {$only_five > 0 && $i < 10} {incr i} {
246 gdb_continue_to_breakpoint "thread 5's print, pass: $i"
247 gdb_test_multiple "info threads" "" {
248 -re "\\* 5 Thread .* print_philosopher .*\r\n$gdb_prompt $" {
249 # Okay this time.
250 }
251 -re ".*$gdb_prompt $" {
252 set only_five 0
253 }
254 timeout {
255 set only_five -1
256 }
257 }
258 }
259
260 set name "thread-specific breakpoint is thread-specific"
261 if {$only_five == 1} { pass $name }
262 if {$only_five == 0} { fail $name }
263 if {$only_five == -1} { fail "$name (timeout)" }
264
265
266 ### Select a particular thread.
267 proc select_thread {thread} {
268 global gdb_prompt
269
270 gdb_test "thread $thread" \
271 "\\\[Switching to thread .*\\\].*" \
272 "selected thread: $thread"
273 }
274
275 ### Select THREAD, check for a plausible backtrace, and make sure
276 ### we're actually selecting a different philosopher each time.
277 ### Return true if the thread had a stack which was not only
278 ### acceptable, but interesting. SEEN should be an array in which
279 ### SEEN(N) exists iff we have found philosopher number N before.
280
281 set main_seen 0
282 set manager_seen 0
283
284 proc check_philosopher_stack {thread seen_name} {
285 global gdb_prompt
286 upvar $seen_name seen
287 global main_seen
288 global expect_manager manager_seen
289
290 set name "philosopher is distinct: $thread"
291 set interesting 0
292
293 select_thread $thread
294 gdb_test_multiple "where" "$name" {
295 -re ".* in philosopher \\(data=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
296 set data $expect_out(1,string)
297 if {[info exists seen($data)]} {
298 fail $name
299 } else {
300 pass $name
301 set seen($data) yep
302 }
303 set interesting 1
304 }
305 -re ".* in __pthread_manager \\(.*$gdb_prompt $" {
306 if {$manager_seen == 1} {
307 fail "manager thread is distinct: $thread"
308 } else {
309 set manager_seen 1
310 pass "manager thread is distinct: $thread"
311 }
312 set interesting 1
313 }
314 -re "pthread_start_thread.*\r\n$gdb_prompt $" {
315 ## Maybe the thread hasn't started yet.
316 pass $name
317 }
318 -re ".* in main \\(.*$gdb_prompt $" {
319 if {$main_seen == 1} {
320 fail "main is distinct: $thread"
321 } else {
322 set main_seen 1
323 pass "main is distinct: $thread"
324 }
325 set interesting 1
326 }
327 -re " in \\?\\?.*\r\n$gdb_prompt $" {
328 ## Sometimes we can't get a backtrace. I'm going to call
329 ## this a pass, since we do verify that at least one
330 ## thread was interesting, so we can get more consistent
331 ## test suite totals. But in my heart, I think it should
332 ## be an xfail.
333 pass $name
334 }
335 }
336
337 return $interesting
338 }
339
340 set any_interesting 0
341 array set seen {}
342 unset seen
343 for {set i 1} {$i <= $nthreads} {incr i} {
344 if [check_philosopher_stack $i seen] {
345 set any_interesting 1
346 }
347 }
348
349 if {$any_interesting} {
350 pass "found an interesting thread"
351 } else {
352 fail "found an interesting thread"
353 }
354
355 if {$manager_seen == $expect_manager} {
356 pass "manager thread found (not found) when expected"
357 } else {
358 fail "manager thread found (not found) when expected"
359 }