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