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 # Detach a running program that constantly forks, verify that we correctly
17 # detach all fork children, for which events are pending.
21 # - Resume a program in background (continue &) with many threads that
22 # constantly fork and wait for their fork children to exit.
23 # - Detach the program. If testing against GDBserver, hope that the detach
24 # CLI command is processed while there is a stop reply pending in the
26 # - Signal the parent program to exit, by sending it a SIGUSR1 signal.
27 # - Have the parent write a flag file to the filesystem just before exiting.
28 # - If a pending fork child is mistakenly still attached, it will prevent its
29 # parent thread from waitpid'ing it, preventing the main thread from joining
30 # it, prevent it from writing the flag file, failing the test.
34 if { [is_remote target] } {
35 # If the target is remote, write the file in whatever the current working
36 # directory is, with a somewhat unique name.
37 set touch_file_path ${testfile}-flag
39 set touch_file_path [standard_output_file flag]
42 if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
43 executable [list debug "additional_flags=-DTOUCH_FILE_PATH=\"$touch_file_path\""]] != "" } {
48 remote_file target delete $::touch_file_path
49 gdb_assert { ![remote_file target exists $::touch_file_path] } "file does not exist before test"
51 save_vars { ::GDBFLAGS } {
52 append ::GDBFLAGS " -ex \"set non-stop on\""
53 clean_restart $::binfile
56 if { ![runto break_here_first] } {
60 set pid [get_integer_valueof "my_pid" -1]
62 error "could not get inferior pid"
65 gdb_test_no_output "set print inferior-events off"
67 gdb_test_multiple "continue &" "" {
68 -re "Continuing.\r\n$::gdb_prompt " {
75 if { [info exists ::server_spawn_id] } {
76 # Let the program run for 2 seconds, during which it will fork many times.
77 # When running against GDBserver, this makes server print a ton of
78 # "Detaching from process X" message, to the point where its output buffer
79 # gets full and it hangs in a write to stdout. During these 2 seconds,
80 # drain the messages from GDBserver to keep that from happening.
81 gdb_test_multiple "" "flush server output" {
85 exp_continue -continue_timer
93 # Not using GDBserver, just sleep 2 seconds.
97 gdb_test "detach" "Detaching from program: .*"
99 if { [info exists ::server_spawn_id] } {
100 # Drain GDBserver's output buffer, in the (unlikely) event that enough
101 # messages were output to fill the buffer between the moment we stopped
102 # consuming it and the moment GDBserver detached the process.
103 gdb_test_multiple "" "" {
104 -i $::server_spawn_id
112 remote_exec target "kill -USR1 ${pid}"
113 gdb_assert { [target_file_exists_with_timeout $::touch_file_path] } "file exists after detach"
115 # Don't leave random files on the target system.
116 if { [is_remote target] } {
117 remote_file target delete $::touch_file_path