OSDN Git Service

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