]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/pending-fork-event-detach-ns.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / pending-fork-event-detach-ns.exp
CommitLineData
1d506c26 1# Copyright 2021-2024 Free Software Foundation, Inc.
df5ad102
SM
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# Detach a running program that constantly forks, verify that we correctly
17# detach all fork children, for which events are pending.
18#
19# The strategy is:
20#
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.
31
32standard_testfile
33
34if { [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]
40}
41
42if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
43 executable [list debug "additional_flags=-DTOUCH_FILE_PATH=\"$touch_file_path\""]] != "" } {
44 return
45}
46
47proc 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"
50
51 save_vars { ::GDBFLAGS } {
52 append ::GDBFLAGS " -ex \"set non-stop on\""
53 clean_restart $::binfile
54 }
55
56 if { ![runto break_here_first] } {
57 return
58 }
59
60 set pid [get_integer_valueof "my_pid" -1]
61 if { $pid == -1 } {
62 error "could not get inferior pid"
63 }
64
65 gdb_test_no_output "set print inferior-events off"
66
67 gdb_test_multiple "continue &" "" {
68 -re "Continuing.\r\n$::gdb_prompt " {
69 pass $gdb_test_name
70 }
71 }
72
73 set wait_time_s 2
74
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 }
87
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 }
96
97 gdb_test "detach" "Detaching from program: .*"
98
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 }
111
112 remote_exec target "kill -USR1 ${pid}"
113 gdb_assert { [target_file_exists_with_timeout $::touch_file_path] } "file exists after detach"
114
115 # Don't leave random files on the target system.
116 if { [is_remote target] } {
117 remote_file target delete $::touch_file_path
118 }
119}
120
121do_test