Commit | Line | Data |
---|---|---|
ecd75fc8 | 1 | # Copyright 1999-2014 Free Software Foundation, Inc. |
917317f4 JM |
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 | |
e22f8b7c | 5 | # the Free Software Foundation; either version 3 of the License, or |
917317f4 | 6 | # (at your option) any later version. |
e22f8b7c | 7 | # |
917317f4 JM |
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. | |
e22f8b7c | 12 | # |
917317f4 | 13 | # You should have received a copy of the GNU General Public License |
e22f8b7c | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
917317f4 JM |
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 | ||
917317f4 JM |
36 | |
37 | # This only works with Linux configurations. | |
c3b69c04 | 38 | if ![istarget *-*-linux-gnu*] then { |
917317f4 JM |
39 | return |
40 | } | |
41 | ||
0efbbabc | 42 | standard_testfile |
b111b805 | 43 | if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != ""} { |
b5ab8ff3 | 44 | return -1 |
917317f4 JM |
45 | } |
46 | ||
0efbbabc | 47 | clean_restart ${binfile} |
9db70545 | 48 | gdb_test_no_output "set print sevenbit-strings" |
917317f4 JM |
49 | runto_main |
50 | ||
51 | # There should be no threads initially. | |
f6978de9 | 52 | gdb_test "info threads" ".*" "info threads 1" |
917317f4 JM |
53 | |
54 | # Try stepping over the thread creation function. | |
55 | gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: create philosopher"] | |
85e85163 | 56 | set expect_manager -1 |
917317f4 JM |
57 | for {set i 0} {$i < 5} {incr i} { |
58 | gdb_continue_to_breakpoint "about to create philosopher: $i" | |
fcfcc2b5 | 59 | set threads_before {} |
9db70545 | 60 | gdb_test_multiple "info threads" "info threads before: $i" { |
fcfcc2b5 NS |
61 | -re "info threads\r\n" { |
62 | exp_continue | |
63 | } | |
a7658b96 TT |
64 | -re "^ *Id.*Frame *\[\r\n\]+" { |
65 | exp_continue | |
66 | } | |
67 | -re "^. +(\[0-9\]+ *Thread \[-0-9a-fx\]+) \[^\n\]*\n" { | |
fcfcc2b5 NS |
68 | verbose -log "found thread $expect_out(1,string)" 2 |
69 | lappend threads_before $expect_out(1,string) | |
70 | exp_continue | |
71 | } | |
ac6c2fba NS |
72 | -re "^\[^\n\]*\n" { |
73 | verbose -log "skipping line" 2 | |
100906b0 | 74 | exp_continue -continue_timer |
ac6c2fba | 75 | } |
fcfcc2b5 NS |
76 | -re "^$gdb_prompt $" { |
77 | } | |
fcfcc2b5 | 78 | } |
fcfcc2b5 | 79 | set threads_created 0 |
9db70545 | 80 | gdb_test_multiple "next" "create philosopher: $i" { |
fcfcc2b5 NS |
81 | -re "^next\r\n" { |
82 | exp_continue | |
85e85163 | 83 | } |
2fa63963 DJ |
84 | -re "^ *\[_!\] \[0-9\]* \[_!\]\r\n" { |
85 | # Ignore program output. | |
86 | exp_continue -continue_timer | |
87 | } | |
fcfcc2b5 NS |
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 $" { | |
c2d11a7d | 96 | } |
754cbe1c | 97 | -re "Program received signal.*(Unknown signal|SIGUSR|Real-time event).*$gdb_prompt $" { |
c2d11a7d JM |
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 $" { | |
c2d11a7d | 105 | } |
c2d11a7d | 106 | } |
052ca370 | 107 | if { $threads_created == 1 } { |
fcfcc2b5 NS |
108 | if { $expect_manager < 0 } { |
109 | set expect_manager 0 | |
110 | } | |
111 | pass "create philosopher: $i" | |
112 | } elseif { !$i && $threads_created == 2 } { | |
113 | # Two threads are created the first time in LinuxThreads, | |
114 | # where the second is the manager thread. In NPTL, there is none. | |
115 | set expect_manager 1 | |
116 | pass "create philosopher: $i" | |
117 | } else { | |
118 | fail "create philosopher: $i" | |
119 | } | |
120 | ||
fcfcc2b5 | 121 | set threads_after {} |
9db70545 | 122 | gdb_test_multiple "info threads" "info threads after: $i" { |
fcfcc2b5 NS |
123 | -re "info threads\r\n" { |
124 | exp_continue | |
125 | } | |
a7658b96 TT |
126 | -re "^ *Id.*Frame *\[\r\n\]+" { |
127 | exp_continue | |
128 | } | |
129 | -re "^. +(\[0-9\]+ *Thread \[-0-9a-fx\]+) \[^\n\]*\n" { | |
fcfcc2b5 NS |
130 | set name $expect_out(1,string) |
131 | for {set j 0} {$j != [llength $threads_before] } {incr j} { | |
132 | if {$name == [lindex $threads_before $j]} { | |
133 | set threads_before [lreplace $threads_before $j $j] | |
134 | set name "" | |
135 | break | |
136 | } | |
137 | } | |
138 | if { $name != "" } { | |
139 | lappend threads_after $name | |
140 | } | |
141 | exp_continue | |
142 | } | |
ac6c2fba NS |
143 | -re "^\[^\n\]*\n" { |
144 | verbose -log "skipping line" 2 | |
100906b0 | 145 | exp_continue -continue_timer |
ac6c2fba | 146 | } |
fcfcc2b5 NS |
147 | -re "^$gdb_prompt $" { |
148 | if { [llength $threads_before] != 0 } { | |
ac6c2fba | 149 | fail "info threads after: $i" |
fcfcc2b5 NS |
150 | } elseif { !$i && [llength $threads_after] == 2 } { |
151 | set expect_manager 1 | |
ac6c2fba | 152 | pass "info threads after: $i" |
fcfcc2b5 NS |
153 | } elseif { [llength $threads_after] == 1 } { |
154 | if { $expect_manager < 0 } { | |
155 | set expect_manager 0 | |
156 | } | |
ac6c2fba | 157 | pass "info threads after: $i" |
fcfcc2b5 | 158 | } else { |
ac6c2fba | 159 | fail "info threads after: $i" |
fcfcc2b5 NS |
160 | } |
161 | } | |
fcfcc2b5 | 162 | } |
917317f4 JM |
163 | } |
164 | ||
f10b16d4 | 165 | set nthreads 6 |
85e85163 | 166 | |
917317f4 JM |
167 | # Run until there are some threads. |
168 | gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"] | |
169 | gdb_continue_to_breakpoint "main thread's sleep" | |
a7658b96 | 170 | set info_threads_ptn ".*" |
85e85163 | 171 | for {set i $nthreads} {$i > 0} {incr i -1} { |
a7658b96 | 172 | append info_threads_ptn "$i *Thread .*" |
85e85163 | 173 | } |
f10b16d4 | 174 | append info_threads_ptn "\[\r\n\]+$gdb_prompt $" |
a7658b96 | 175 | set info_threads_manager_ptn "[expr $nthreads + 1] *Thread .*$info_threads_ptn" |
f10b16d4 JB |
176 | |
177 | gdb_test_multiple "info threads" "info threads 2" { | |
178 | -re "$info_threads_manager_ptn" { | |
179 | # We did see a manager thread. Check that against what we expected. | |
180 | switch -exact -- $expect_manager { | |
181 | -1 { | |
182 | # We weren't sure whether to expect a manager thread. | |
183 | pass "info threads 2" | |
184 | } | |
185 | 1 { | |
186 | # We were expecting a manager thread. | |
187 | pass "info threads 2" | |
188 | } | |
189 | 0 { | |
190 | # We were not expecting to see the manager thread. | |
191 | fail "info threads 2" | |
192 | } | |
193 | } | |
194 | set expect_manager 1 | |
195 | incr nthreads | |
196 | } | |
197 | -re "$info_threads_ptn" { | |
198 | # We did not see a manager thread. Check that against what we | |
199 | # expected. | |
200 | switch -exact -- $expect_manager { | |
201 | -1 { | |
202 | # We weren't sure whether to expect a manager thread. | |
203 | # Don't expect it from here on out. | |
204 | pass "info threads 2" | |
205 | } | |
206 | 1 { | |
207 | # We were expecting a manager thread, but we didn't see one. | |
208 | fail "info threads 2" | |
209 | } | |
210 | 0 { | |
211 | # We were not expecting to see the manager thread. | |
212 | pass "info threads 2" | |
213 | } | |
214 | } | |
215 | set expect_manager 0 | |
216 | } | |
217 | } | |
218 | ||
917317f4 JM |
219 | |
220 | # Try setting a thread-specific breakpoint. | |
221 | gdb_breakpoint "print_philosopher thread 5" | |
222 | gdb_continue_to_breakpoint "thread 5's print" | |
f10b16d4 JB |
223 | # When there is no debugging info available for the thread library, |
224 | # the backtrace entry for philosopher's caller looks like: | |
b111b805 | 225 | # #2 0x4001c548 in pthread_create () from /lib/libpthread.so.0 |
f10b16d4 JB |
226 | # If you do have debug info, the output obviously depends more on the |
227 | # exact library in use; under NPTL, you get: | |
228 | # #2 0x0012b7fc in start_thread (arg=0x21) at pthread_create.c:264 | |
b111b805 | 229 | gdb_test "where" "print_philosopher.*philosopher.* \(from .*libpthread\|at pthread_create\|in pthread_create\).*" \ |
917317f4 JM |
230 | "first thread-specific breakpoint hit" |
231 | ||
232 | # Make sure it's catching the right thread. Try hitting the | |
233 | # breakpoint ten times, and make sure we don't get anyone else. | |
234 | set only_five 1 | |
235 | for {set i 0} {$only_five > 0 && $i < 10} {incr i} { | |
236 | gdb_continue_to_breakpoint "thread 5's print, pass: $i" | |
9db70545 | 237 | gdb_test_multiple "info threads" "" { |
a7658b96 | 238 | -re "\[*\] 5 *Thread .* +print_philosopher .*\r\n$gdb_prompt $" { |
917317f4 JM |
239 | # Okay this time. |
240 | } | |
241 | -re ".*$gdb_prompt $" { | |
242 | set only_five 0 | |
243 | } | |
244 | timeout { | |
245 | set only_five -1 | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
250 | set name "thread-specific breakpoint is thread-specific" | |
251 | if {$only_five == 1} { pass $name } | |
252 | if {$only_five == 0} { fail $name } | |
253 | if {$only_five == -1} { fail "$name (timeout)" } | |
254 | ||
255 | ||
256 | ### Select a particular thread. | |
257 | proc select_thread {thread} { | |
258 | global gdb_prompt | |
259 | ||
9db70545 MS |
260 | gdb_test "thread $thread" \ |
261 | "\\\[Switching to thread .*\\\].*" \ | |
262 | "selected thread: $thread" | |
917317f4 JM |
263 | } |
264 | ||
265 | ### Select THREAD, check for a plausible backtrace, and make sure | |
266 | ### we're actually selecting a different philosopher each time. | |
267 | ### Return true if the thread had a stack which was not only | |
268 | ### acceptable, but interesting. SEEN should be an array in which | |
269 | ### SEEN(N) exists iff we have found philosopher number N before. | |
ac55707e MS |
270 | |
271 | set main_seen 0 | |
272 | set manager_seen 0 | |
273 | ||
917317f4 JM |
274 | proc check_philosopher_stack {thread seen_name} { |
275 | global gdb_prompt | |
276 | upvar $seen_name seen | |
ac55707e | 277 | global main_seen |
85e85163 | 278 | global expect_manager manager_seen |
917317f4 JM |
279 | |
280 | set name "philosopher is distinct: $thread" | |
281 | set interesting 0 | |
282 | ||
283 | select_thread $thread | |
9db70545 | 284 | gdb_test_multiple "where" "$name" { |
917317f4 JM |
285 | -re ".* in philosopher \\(data=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" { |
286 | set data $expect_out(1,string) | |
287 | if {[info exists seen($data)]} { | |
288 | fail $name | |
289 | } else { | |
290 | pass $name | |
291 | set seen($data) yep | |
292 | } | |
293 | set interesting 1 | |
294 | } | |
396cc255 DJ |
295 | -re ".* in __pthread_manager \\(.*$gdb_prompt $" { |
296 | if {$manager_seen == 1} { | |
297 | fail "manager thread is distinct: $thread" | |
298 | } else { | |
299 | set manager_seen 1 | |
300 | pass "manager thread is distinct: $thread" | |
301 | } | |
302 | set interesting 1 | |
303 | } | |
917317f4 JM |
304 | -re "pthread_start_thread.*\r\n$gdb_prompt $" { |
305 | ## Maybe the thread hasn't started yet. | |
306 | pass $name | |
307 | } | |
ac55707e MS |
308 | -re ".* in main \\(.*$gdb_prompt $" { |
309 | if {$main_seen == 1} { | |
310 | fail "main is distinct: $thread" | |
311 | } else { | |
312 | set main_seen 1 | |
313 | pass "main is distinct: $thread" | |
314 | } | |
315 | set interesting 1 | |
316 | } | |
917317f4 JM |
317 | -re " in \\?\\?.*\r\n$gdb_prompt $" { |
318 | ## Sometimes we can't get a backtrace. I'm going to call | |
319 | ## this a pass, since we do verify that at least one | |
320 | ## thread was interesting, so we can get more consistent | |
321 | ## test suite totals. But in my heart, I think it should | |
322 | ## be an xfail. | |
323 | pass $name | |
324 | } | |
917317f4 JM |
325 | } |
326 | ||
327 | return $interesting | |
328 | } | |
329 | ||
330 | set any_interesting 0 | |
e65f9ffb | 331 | catch {unset seen} |
6ff9af88 | 332 | array set seen {} |
85e85163 | 333 | for {set i 1} {$i <= $nthreads} {incr i} { |
917317f4 JM |
334 | if [check_philosopher_stack $i seen] { |
335 | set any_interesting 1 | |
336 | } | |
337 | } | |
11315ae0 | 338 | unset seen |
917317f4 JM |
339 | |
340 | if {$any_interesting} { | |
341 | pass "found an interesting thread" | |
342 | } else { | |
343 | fail "found an interesting thread" | |
344 | } | |
85e85163 JJ |
345 | |
346 | if {$manager_seen == $expect_manager} { | |
347 | pass "manager thread found (not found) when expected" | |
348 | } else { | |
349 | fail "manager thread found (not found) when expected" | |
350 | } |