1 # Copyright 2021-2024 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 require can_spawn_for_attach
55 set bp_lineno [gdb_get_line_number "Set breakpoint here"]
57 # Number of threads started by the program.
60 # Start GDB, configuring various settings according to the arguments.
61 proc start_gdb_for_test {condition_eval target_non_stop non_stop displaced} {
62 save_vars { ::GDBFLAGS } {
63 append ::GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
64 append ::GDBFLAGS " -ex \"set non-stop $non_stop\""
65 append ::GDBFLAGS " -ex \"set displaced $displaced\""
66 append ::GDBFLAGS " -ex \"set schedule-multiple on\""
67 clean_restart $::binfile
70 gdb_test_no_output "set breakpoint condition-evaluation $condition_eval"
73 # Use the 'attach' command to attach to process with pid TESTPID. Return true
74 # if we believe GDB has attached and we are back at the GDB prompt, otherwise,
76 proc attach_to {testpid} {
77 with_timeout_factor 2 {
80 gdb_test_multiple "attach $testpid" "attach" {
81 -re "Attaching to program.*process $testpid\r\n" {
85 -re "new threads in iteration" {
86 # Seen when "set debug libthread_db" is on.
89 -re "Reading symbols from|Expanding full symbols from" {
90 # Prevent -readnow timeout.
93 -re "is a zombie - the process has already terminated.*$::gdb_prompt " {
96 -re "Unable to attach: .*$::gdb_prompt " {
99 -re "\r\n$::gdb_prompt " {
100 if { $saw_attaching } {
113 # After attaching to a multi-threaded inferior in non-stop mode, we expect to
114 # see a stop message from each thread. This proc waits for all of these stop
115 # messages. TID_RE is a regexp used to match the thread-id of the stopped
118 # Return true if we saw a stop from each of the expected threads (based on the
119 # global N_THREADS value), otherwise, return false.
120 proc check_stops_after_non_stop_attach {tid_re} {
123 # In non-stop, we will see one stop per thread after the prompt.
125 set test "seen all stops"
126 for {set thread 1} { $thread <= $::n_threads } { incr thread } {
127 if {[gdb_test_multiple "" $test {
128 -re "Thread ${tid_re} ${any} stopped" {
136 # If we haven't seen all stops, then the
137 # gdb_test_multiple in the loop above will have
138 # already issued a FAIL.
139 if {$stops != $::n_threads} {
146 # Prepare for a single test iteration. TESTPID is the pid of the process GDB
147 # will be attached too. NON_STOP indicates if GDB is configured in non-stop
148 # mode or not. ATTEMPT is the current attempt number, and ATTEMPTS is the
149 # maximum number of attempts we plan to run. TID_RE is a string used to match
150 # against a thread-id in GDB's stop messages.
152 # Return true if everything is prepared correctly, otherwise return false.
153 proc prepare_test_iter {testpid non_stop attempt attempts tid_re} {
154 if {![attach_to $testpid]} {
159 if {![check_stops_after_non_stop_attach $tid_re]} {
164 gdb_test "break ${::srcfile}:${::bp_lineno} if 0" "Breakpoint.*" \
167 if {$attempt < $attempts} {
168 # Kick the time out timer for another round.
169 gdb_test "print again = 1" " = 1" "reset timer in the inferior"
170 # Show the time we had left in the logs, in case
171 # something goes wrong.
172 gdb_test "print seconds_left" " = .*"
176 set cont_cmd "continue -a &"
178 set cont_cmd "continue &"
181 set cont_cmd_re [string_to_regexp $cont_cmd]
182 gdb_test_multiple $cont_cmd "" {
183 -re "^$cont_cmd_re\r\nContinuing\.\r\n$::gdb_prompt " {
191 # The test proper. See the description at the top of the file.
192 proc_with_prefix test_detach_command {condition_eval target_non_stop non_stop displaced} {
193 set test_spawn_id [spawn_wait_for_attach $::binfile]
194 set testpid [spawn_id_get_pid $test_spawn_id]
196 start_gdb_for_test $condition_eval $target_non_stop $non_stop $displaced
198 gdb_test "add-inferior" "Added inferior 2.*"
199 gdb_test "inferior 2" "Switching to .*"
202 if {![runto setup_done]} {
203 fail "can't run to setup_done"
204 kill_wait_spawned_process $test_spawn_id
208 # Get the PID of the test process.
210 gdb_test_multiple "p mypid" "get pid of inferior 2" {
211 -re " = ($::decimal)\r\n$::gdb_prompt $" {
212 set pid_inf2 $expect_out(1,string)
218 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
219 with_test_prefix "iter $attempt" {
220 gdb_test "inferior 1" "Switching to .*"
222 if {![prepare_test_iter $testpid $non_stop \
223 $attempt $attempts "$::decimal\.$::decimal"]} {
224 kill_wait_spawned_process $test_spawn_id
230 set running_expected [expr ($::n_threads + 1) * 2]
231 gdb_test_multiple "info threads" "threads running" {
232 -re "\\(running\\)" {
236 -re "Cannot execute this command while the target is running.*$::gdb_prompt $" {
237 # Testing against a remote server that doesn't do
238 # non-stop mode. Explicitly interrupt. This
239 # doesn't test the same code paths in GDB, but
240 # it's still something.
242 gdb_test_multiple "interrupt" "" {
243 -re "$::gdb_prompt " {
244 gdb_test_multiple "" $gdb_test_name {
245 -re "received signal SIGINT, Interrupt" {
252 -re "$::gdb_prompt " {
256 if { !$interrupted } {
258 set max_iterations 10
259 while { $running_count < $running_expected } {
262 gdb_test_multiple "info threads" "threads running" {
263 -re "\\(running\\)" {
267 -re "$::gdb_prompt " {
271 if { $iterations == $max_iterations } {
275 gdb_assert {$running_count == $running_expected} \
276 "all threads running"
279 gdb_test "detach" "Detaching from.*"
282 # Now test whether inferior 2's thread were really left
283 # running. Currently an inline step-over stops all
284 # threads of all processes. If detach aborts such a step
285 # over, then threads of other inferiors should be
286 # re-resumed. Test for that by sending a signal to
288 remote_exec target "kill -USR1 ${pid_inf2}"
290 gdb_test_multiple "" "stop with SIGUSR1" {
291 -re "received signal SIGUSR1" {
300 kill_wait_spawned_process $test_spawn_id
303 # Similar to the proc above, but this time, instead of detaching using
304 # the 'detach' command, we quit GDB, this will also trigger a detach, but
305 # through a slightly different path, which can expose different bugs.
306 proc_with_prefix test_detach_quit {condition_eval target_non_stop \
307 non_stop displaced} {
308 # If debugging with target remote, check whether the all-stop variant
309 # of the RSP is being used. If so, we can't run the background tests.
311 && [target_info exists gdb_protocol]
312 && ([target_info gdb_protocol] == "remote"
313 || [target_info gdb_protocol] == "extended-remote")} {
314 start_gdb_for_test $condition_eval $target_non_stop \
317 if {![is_target_non_stop]} {
322 set test_spawn_id [spawn_wait_for_attach $::binfile]
323 set testpid [spawn_id_get_pid $test_spawn_id]
326 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
327 with_test_prefix "iter $attempt" {
329 start_gdb_for_test $condition_eval $target_non_stop \
332 if {![prepare_test_iter $testpid $non_stop \
333 $attempt $attempts "$::decimal"]} {
334 kill_wait_spawned_process $test_spawn_id
338 gdb_test_multiple "with confirm off -- quit" "" {
346 kill_wait_spawned_process $test_spawn_id
349 # The test program exits after a while, in case GDB crashes. Make it
350 # wait at least as long as we may wait before declaring a time out
352 set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }
354 if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
362 # Probe support for "set breakpoint condition-evaluation target".
363 # This setting influences who steps over the breakpoint, the (remote)
364 # target (e.g. gdbserver) or gdb, thus exposing issues on either the
366 set supports_condition_eval_target 1
367 set cmd "set breakpoint condition-evaluation target"
368 gdb_test_multiple $cmd "probe condition-evaluation target support" {
369 -re "warning: Target does not support breakpoint condition evaluation.\r\nUsing host evaluation mode instead.\r\n$gdb_prompt $" {
370 # Target doesn't support breakpoint condition evaluation on
372 set supports_condition_eval_target 0
375 -re "^$cmd\r\n$gdb_prompt $" {
380 foreach_with_prefix breakpoint-condition-evaluation {"host" "target"} {
381 if {!$supports_condition_eval_target && ${breakpoint-condition-evaluation} == "target"} {
385 foreach_with_prefix target-non-stop {"off" "on"} {
386 foreach_with_prefix non-stop {"off" "on"} {
387 if {${non-stop} && !${target-non-stop}} {
388 # "set non-stop" overrides "maint set
389 # target-non-stop", no use testing this combination.
393 foreach_with_prefix displaced {"off" "auto"} {
394 test_detach_command ${breakpoint-condition-evaluation} \
395 ${target-non-stop} ${non-stop} ${displaced}
396 test_detach_quit ${breakpoint-condition-evaluation} \
397 ${target-non-stop} ${non-stop} ${displaced}