Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.threads / pending-fork-event-detach-ns.exp
blobc7275b942312579f81b8e3db83ad81c6e2cae85e
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.
19 # The strategy is:
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
25 #     remote target.
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.
32 standard_testfile
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
38 } else {
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\""]] != "" } {
44     return
47 proc do_test { } {
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
54     }
56     if { ![runto break_here_first] } {
57         return
58     }
60     set pid [get_integer_valueof "my_pid" -1]
61     if { $pid == -1 } {
62         error "could not get inferior pid"
63     }
65     gdb_test_no_output "set print inferior-events off"
67     gdb_test_multiple "continue &" "" {
68         -re "Continuing.\r\n$::gdb_prompt " {
69             pass $gdb_test_name
70         }
71     }
73     set wait_time_s 2
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" {
82             -timeout $wait_time_s
83             -i $::server_spawn_id
84             -re ".+" {
85                 exp_continue -continue_timer
86             }
88             timeout {
89                 pass $gdb_test_name
90             }
91         }
92     } else {
93         # Not using GDBserver, just sleep 2 seconds.
94         sleep $wait_time_s
95     }
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
105             -re ".+" {
106                 exp_continue
107             }
108             -re "^$" {}
109         }
110     }
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
118     }
121 do_test