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