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