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