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