1 # Copyright 2021-2023 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test detaching from a process that is running and has threads
17 # constantly hitting a breakpoint and stepping over it, in all
20 # - maint target non-stop off/on
21 # - set non-stop on/off
22 # - displaced stepping on/off
24 # This stresses the edge cases of detaching while a displaced step or
25 # an in-line step over are in progress.
27 # A fail mode is that the inferior process dies after being detached.
28 # This can happen because e.g.:
30 # - GDB leaves a breakpoint installed behind, or
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.
36 # To exercise this, the testcase reattaches to the process shortly
37 # after detaching, ensuring the process is still alive and well.
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
51 if {![can_spawn_for_attach]} {
57 set bp_lineno [gdb_get_line_number "Set breakpoint here"]
59 # Number of threads started by the program.
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
72 gdb_test_no_output "set breakpoint condition-evaluation $condition_eval"
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,
78 proc attach_to {testpid} {
79 with_timeout_factor 2 {
82 gdb_test_multiple "attach $testpid" "attach" {
83 -re "Attaching to program.*process $testpid\r\n" {
87 -re "new threads in iteration" {
88 # Seen when "set debug libthread_db" is on.
91 -re "Reading symbols from|Expanding full symbols from" {
92 # Prevent -readnow timeout.
95 -re "is a zombie - the process has already terminated.*$::gdb_prompt " {
98 -re "Unable to attach: .*$::gdb_prompt " {
101 -re "\r\n$::gdb_prompt " {
102 if { $saw_attaching } {
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
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} {
125 # In non-stop, we will see one stop per thread after the prompt.
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" {
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} {
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.
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]} {
161 if {![check_stops_after_non_stop_attach $tid_re]} {
166 gdb_test "break ${::srcfile}:${::bp_lineno} if 0" "Breakpoint.*" \
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" " = .*"
178 set cont_cmd "continue -a &"
180 set cont_cmd "continue &"
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 " {
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]
198 start_gdb_for_test $condition_eval $target_non_stop $non_stop $displaced
200 gdb_test "add-inferior" "Added inferior 2.*"
201 gdb_test "inferior 2" "Switching to .*"
204 if {![runto setup_done]} {
205 fail "can't run to setup_done"
206 kill_wait_spawned_process $test_spawn_id
210 # Get the PID of the test process.
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)
220 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
221 with_test_prefix "iter $attempt" {
222 gdb_test "inferior 1" "Switching to .*"
224 if {![prepare_test_iter $testpid $non_stop \
225 $attempt $attempts "$::decimal\.$::decimal"]} {
226 kill_wait_spawned_process $test_spawn_id
232 set running_expected [expr ($::n_threads + 1) * 2]
233 gdb_test_multiple "info threads" "threads running" {
234 -re "\\(running\\)" {
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.
244 gdb_test_multiple "interrupt" "" {
245 -re "$::gdb_prompt " {
246 gdb_test_multiple "" $gdb_test_name {
247 -re "received signal SIGINT, Interrupt" {
254 -re "$::gdb_prompt " {
258 if { !$interrupted } {
260 set max_iterations 10
261 while { $running_count < $running_expected } {
264 gdb_test_multiple "info threads" "threads running" {
265 -re "\\(running\\)" {
269 -re "$::gdb_prompt " {
273 if { $iterations == $max_iterations } {
277 gdb_assert {$running_count == $running_expected} \
278 "all threads running"
281 gdb_test "detach" "Detaching from.*"
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
290 remote_exec target "kill -SIGUSR1 ${pid_inf2}"
292 gdb_test_multiple "" "stop with SIGUSR1" {
293 -re "received signal SIGUSR1" {
302 kill_wait_spawned_process $test_spawn_id
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.
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 \
319 gdb_test_multiple "maint show target-non-stop" "" {
320 -wrap -re "(is|currently) on.*" {
322 -wrap -re "(is|currently) off.*" {
328 set test_spawn_id [spawn_wait_for_attach $::binfile]
329 set testpid [spawn_id_get_pid $test_spawn_id]
332 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
333 with_test_prefix "iter $attempt" {
335 start_gdb_for_test $condition_eval $target_non_stop \
338 if {![prepare_test_iter $testpid $non_stop \
339 $attempt $attempts "$::decimal"]} {
340 kill_wait_spawned_process $test_spawn_id
344 gdb_test_multiple "with confirm off -- quit" "" {
352 kill_wait_spawned_process $test_spawn_id
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
358 set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }
360 if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
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
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
378 set supports_condition_eval_target 0
381 -re "^$cmd\r\n$gdb_prompt $" {
386 foreach_with_prefix breakpoint-condition-evaluation {"host" "target"} {
387 if {!$supports_condition_eval_target && ${breakpoint-condition-evaluation} == "target"} {
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.
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}