]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/detach-step-over.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / detach-step-over.exp
1 # Copyright 2021-2023 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 # Test detaching from a process that is running and has threads
17 # constantly hitting a breakpoint and stepping over it, in all
18 # combinations of:
19 #
20 # - maint target non-stop off/on
21 # - set non-stop on/off
22 # - displaced stepping on/off
23 #
24 # This stresses the edge cases of detaching while a displaced step or
25 # an in-line step over are in progress.
26 #
27 # A fail mode is that the inferior process dies after being detached.
28 # This can happen because e.g.:
29 #
30 # - GDB leaves a breakpoint installed behind, or
31 #
32 # - GDB leaves a thread running in the displaced step scratch buffer.
33 # With no debugger around to run the finish step, the thread runs
34 # off of the scratch buffer, with undefined results.
35 #
36 # To exercise this, the testcase reattaches to the process shortly
37 # after detaching, ensuring the process is still alive and well.
38 #
39 # In addition, since GDB may pause threads of all processes for
40 # stepping over a breakpoint, it needs to re-resume all threads if it
41 # detaches from the process that was just stepping over the
42 # breakpoint. To ensure that, the testcase actually runs a second
43 # process at the same time as the one that is used to test detaching.
44 # After the first process is detached, the testcase sends a SIGUSR1 to
45 # the second process. If threads failed to be resumed, then the
46 # SIGUSR1 is never reported to the user, resulting in timeout. The
47 # threads of this second process will also be constantly stepping over
48 # a breakpoint, which has helped with exposing further corner case
49 # bugs.
50
51 if {![can_spawn_for_attach]} {
52 return 0
53 }
54
55 standard_testfile
56
57 set bp_lineno [gdb_get_line_number "Set breakpoint here"]
58
59 # Number of threads started by the program.
60 set n_threads 10
61
62 # Start GDB, configuring various settings according to the arguments.
63 proc start_gdb_for_test {condition_eval target_non_stop non_stop displaced} {
64 save_vars { ::GDBFLAGS } {
65 append ::GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
66 append ::GDBFLAGS " -ex \"set non-stop $non_stop\""
67 append ::GDBFLAGS " -ex \"set displaced $displaced\""
68 append ::GDBFLAGS " -ex \"set schedule-multiple on\""
69 clean_restart $::binfile
70 }
71
72 gdb_test_no_output "set breakpoint condition-evaluation $condition_eval"
73 }
74
75 # Use the 'attach' command to attach to process with pid TESTPID. Return true
76 # if we believe GDB has attached and we are back at the GDB prompt, otherwise,
77 # return false.
78 proc attach_to {testpid} {
79 with_timeout_factor 2 {
80 set attached 0
81 set saw_attaching 0
82 gdb_test_multiple "attach $testpid" "attach" {
83 -re "Attaching to program.*process $testpid\r\n" {
84 set saw_attaching 1
85 exp_continue
86 }
87 -re "new threads in iteration" {
88 # Seen when "set debug libthread_db" is on.
89 exp_continue
90 }
91 -re "Reading symbols from|Expanding full symbols from" {
92 # Prevent -readnow timeout.
93 exp_continue
94 }
95 -re "is a zombie - the process has already terminated.*$::gdb_prompt " {
96 fail $gdb_test_name
97 }
98 -re "Unable to attach: .*$::gdb_prompt " {
99 fail $gdb_test_name
100 }
101 -re "\r\n$::gdb_prompt " {
102 if { $saw_attaching } {
103 set attached 1
104 pass $gdb_test_name
105 } else {
106 fail $gdb_test_name
107 }
108 }
109 }
110 }
111
112 return $attached
113 }
114
115 # After attaching to a multi-threaded inferior in non-stop mode, we expect to
116 # see a stop message from each thread. This proc waits for all of these stop
117 # messages. TID_RE is a regexp used to match the thread-id of the stopped
118 # thread.
119 #
120 # Return true if we saw a stop from each of the expected threads (based on the
121 # global N_THREADS value), otherwise, return false.
122 proc check_stops_after_non_stop_attach {tid_re} {
123 set any "\[^\r\n\]*"
124
125 # In non-stop, we will see one stop per thread after the prompt.
126 set stops 0
127 set test "seen all stops"
128 for {set thread 1} { $thread <= $::n_threads } { incr thread } {
129 if {[gdb_test_multiple "" $test {
130 -re "Thread ${tid_re} ${any} stopped" {
131 incr stops
132 }
133 }] != 0} {
134 break
135 }
136 }
137
138 # If we haven't seen all stops, then the
139 # gdb_test_multiple in the loop above will have
140 # already issued a FAIL.
141 if {$stops != $::n_threads} {
142 return false
143 }
144 pass $test
145 return true
146 }
147
148 # Prepare for a single test iteration. TESTPID is the pid of the process GDB
149 # will be attached too. NON_STOP indicates if GDB is configured in non-stop
150 # mode or not. ATTEMPT is the current attempt number, and ATTEMPTS is the
151 # maximum number of attempts we plan to run. TID_RE is a string used to match
152 # against a thread-id in GDB's stop messages.
153 #
154 # Return true if everything is prepared correctly, otherwise return false.
155 proc prepare_test_iter {testpid non_stop attempt attempts tid_re} {
156 if {![attach_to $testpid]} {
157 return false
158 }
159
160 if {$non_stop} {
161 if {![check_stops_after_non_stop_attach $tid_re]} {
162 return false
163 }
164 }
165
166 gdb_test "break ${::srcfile}:${::bp_lineno} if 0" "Breakpoint.*" \
167 "break LOC if 0"
168
169 if {$attempt < $attempts} {
170 # Kick the time out timer for another round.
171 gdb_test "print again = 1" " = 1" "reset timer in the inferior"
172 # Show the time we had left in the logs, in case
173 # something goes wrong.
174 gdb_test "print seconds_left" " = .*"
175 }
176
177 if {$non_stop} {
178 set cont_cmd "continue -a &"
179 } else {
180 set cont_cmd "continue &"
181 }
182
183 set cont_cmd_re [string_to_regexp $cont_cmd]
184 gdb_test_multiple $cont_cmd "" {
185 -re "^$cont_cmd_re\r\nContinuing\.\r\n$::gdb_prompt " {
186 pass $gdb_test_name
187 }
188 }
189
190 return true
191 }
192
193 # The test proper. See the description at the top of the file.
194 proc_with_prefix test_detach_command {condition_eval target_non_stop non_stop displaced} {
195 set test_spawn_id [spawn_wait_for_attach $::binfile]
196 set testpid [spawn_id_get_pid $test_spawn_id]
197
198 start_gdb_for_test $condition_eval $target_non_stop $non_stop $displaced
199
200 gdb_test "add-inferior" "Added inferior 2.*"
201 gdb_test "inferior 2" "Switching to .*"
202
203 gdb_load $::binfile
204 if {![runto setup_done]} {
205 fail "can't run to setup_done"
206 kill_wait_spawned_process $test_spawn_id
207 return
208 }
209
210 # Get the PID of the test process.
211 set pid_inf2 ""
212 gdb_test_multiple "p mypid" "get pid of inferior 2" {
213 -re " = ($::decimal)\r\n$::gdb_prompt $" {
214 set pid_inf2 $expect_out(1,string)
215 pass $gdb_test_name
216 }
217 }
218
219 set attempts 3
220 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
221 with_test_prefix "iter $attempt" {
222 gdb_test "inferior 1" "Switching to .*"
223
224 if {![prepare_test_iter $testpid $non_stop \
225 $attempt $attempts "$::decimal\.$::decimal"]} {
226 kill_wait_spawned_process $test_spawn_id
227 return
228 }
229
230 set running_count 0
231 set interrupted 0
232 set running_expected [expr ($::n_threads + 1) * 2]
233 gdb_test_multiple "info threads" "threads running" {
234 -re "\\(running\\)" {
235 incr running_count
236 exp_continue
237 }
238 -re "Cannot execute this command while the target is running.*$::gdb_prompt $" {
239 # Testing against a remote server that doesn't do
240 # non-stop mode. Explicitly interrupt. This
241 # doesn't test the same code paths in GDB, but
242 # it's still something.
243 set interrupted 1
244 gdb_test_multiple "interrupt" "" {
245 -re "$::gdb_prompt " {
246 gdb_test_multiple "" $gdb_test_name {
247 -re "received signal SIGINT, Interrupt" {
248 pass $gdb_test_name
249 }
250 }
251 }
252 }
253 }
254 -re "$::gdb_prompt " {
255 }
256 }
257
258 if { !$interrupted } {
259 set iterations 0
260 set max_iterations 10
261 while { $running_count < $running_expected } {
262 sleep 1
263 set running_count 0
264 gdb_test_multiple "info threads" "threads running" {
265 -re "\\(running\\)" {
266 incr running_count
267 exp_continue
268 }
269 -re "$::gdb_prompt " {
270 }
271 }
272 incr iterations
273 if { $iterations == $max_iterations } {
274 break
275 }
276 }
277 gdb_assert {$running_count == $running_expected} \
278 "all threads running"
279 }
280
281 gdb_test "detach" "Detaching from.*"
282
283 if {!$interrupted} {
284 # Now test whether inferior 2's thread were really left
285 # running. Currently an inline step-over stops all
286 # threads of all processes. If detach aborts such a step
287 # over, then threads of other inferiors should be
288 # re-resumed. Test for that by sending a signal to
289 # inferior 2.
290 remote_exec target "kill -SIGUSR1 ${pid_inf2}"
291
292 gdb_test_multiple "" "stop with SIGUSR1" {
293 -re "received signal SIGUSR1" {
294 pass $gdb_test_name
295 }
296 }
297 }
298
299 delete_breakpoints
300 }
301 }
302 kill_wait_spawned_process $test_spawn_id
303 }
304
305 # Similar to the proc above, but this time, instead of detaching using
306 # the 'detach' command, we quit GDB, this will also trigger a detach, but
307 # through a slightly different path, which can expose different bugs.
308 proc_with_prefix test_detach_quit {condition_eval target_non_stop \
309 non_stop displaced} {
310 # If debugging with target remote, check whether the all-stop variant
311 # of the RSP is being used. If so, we can't run the background tests.
312 if {!$non_stop
313 && [target_info exists gdb_protocol]
314 && ([target_info gdb_protocol] == "remote"
315 || [target_info gdb_protocol] == "extended-remote")} {
316 start_gdb_for_test $condition_eval $target_non_stop \
317 $non_stop $displaced
318
319 gdb_test_multiple "maint show target-non-stop" "" {
320 -wrap -re "(is|currently) on.*" {
321 }
322 -wrap -re "(is|currently) off.*" {
323 return
324 }
325 }
326 }
327
328 set test_spawn_id [spawn_wait_for_attach $::binfile]
329 set testpid [spawn_id_get_pid $test_spawn_id]
330
331 set attempts 3
332 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
333 with_test_prefix "iter $attempt" {
334
335 start_gdb_for_test $condition_eval $target_non_stop \
336 $non_stop $displaced
337
338 if {![prepare_test_iter $testpid $non_stop \
339 $attempt $attempts "$::decimal"]} {
340 kill_wait_spawned_process $test_spawn_id
341 return
342 }
343
344 gdb_test_multiple "with confirm off -- quit" "" {
345 eof {
346 pass $gdb_test_name
347 }
348 }
349 }
350 }
351
352 kill_wait_spawned_process $test_spawn_id
353 }
354
355 # The test program exits after a while, in case GDB crashes. Make it
356 # wait at least as long as we may wait before declaring a time out
357 # failure.
358 set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }
359
360 if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
361 return -1
362 }
363
364 if ![runto_main] {
365 return -1
366 }
367
368 # Probe support for "set breakpoint condition-evaluation target".
369 # This setting influences who steps over the breakpoint, the (remote)
370 # target (e.g. gdbserver) or gdb, thus exposing issues on either the
371 # target or gdb.
372 set supports_condition_eval_target 1
373 set cmd "set breakpoint condition-evaluation target"
374 gdb_test_multiple $cmd "probe condition-evaluation target support" {
375 -re "warning: Target does not support breakpoint condition evaluation.\r\nUsing host evaluation mode instead.\r\n$gdb_prompt $" {
376 # Target doesn't support breakpoint condition evaluation on
377 # its side.
378 set supports_condition_eval_target 0
379 pass $gdb_test_name
380 }
381 -re "^$cmd\r\n$gdb_prompt $" {
382 pass $gdb_test_name
383 }
384 }
385
386 foreach_with_prefix breakpoint-condition-evaluation {"host" "target"} {
387 if {!$supports_condition_eval_target && ${breakpoint-condition-evaluation} == "target"} {
388 continue
389 }
390
391 foreach_with_prefix target-non-stop {"off" "on"} {
392 foreach_with_prefix non-stop {"off" "on"} {
393 if {${non-stop} && !${target-non-stop}} {
394 # "set non-stop" overrides "maint set
395 # target-non-stop", no use testing this combination.
396 continue
397 }
398
399 foreach_with_prefix displaced {"off" "auto"} {
400 test_detach_command ${breakpoint-condition-evaluation} \
401 ${target-non-stop} ${non-stop} ${displaced}
402 test_detach_quit ${breakpoint-condition-evaluation} \
403 ${target-non-stop} ${non-stop} ${displaced}
404 }
405 }
406 }
407 }