1 # Copyright (C) 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 # Then, test that if we detach an inferior with a pending fork child, that
17 # child is correctly detached and resumes execution normally. There are two
18 # kinds of "pending fork child" we test:
20 # - resulting of a fork catchpoint: we stop at a fork catchpoint and detach.
21 # - resulting of an all-stop stop on top of a non-stop target, where a fork
22 # event is saved as a pending wait status. To test this, we stepi a thread
23 # while another one forks. The stepi generally completes at least as fast
24 # as the fork, so we have a chance that the stop due to the stepi being
25 # complete is shown to the user while the fork event is saved for later.
27 # To verify that the child process is detached and resumes execution, we have
28 # it write a file on the filesystem. If we don't see the file after a certain
29 # delay, it means the child was likely not detached, and the test fails.
31 # At the same time, this tests that having this pending fork event does not
32 # cause other problems in general. For example, a buggy GDB / GDBserver combo
33 # would notice the thread of the child process of the (still unprocessed) fork
34 # event, and erroneously create a new inferior for it. Once fixed, the child
35 # process' thread is hidden by whoever holds the pending fork event.
37 standard_testfile .c -touch-file.c
39 set touch_file_bin $binfile-touch-file
41 if { [is_remote target] } {
42 # If the target is remote, write the file in whatever the current working
43 # directory is, with a somewhat unique name.
44 set touch_file_path ${testfile}-flag
46 # Now get the remote name, by creating the file on build and copying it to
48 remote_exec build touch $touch_file_path
49 set target_touch_file_path [remote_download target $touch_file_path]
52 remote_file build delete $touch_file_path
53 remote_file target delete $target_touch_file_path
55 set touch_file_path $target_touch_file_path
57 set touch_file_path [standard_output_file flag]
60 set opts [list debug "additional_flags=-DTOUCH_FILE_PATH=\"$touch_file_path\""]
61 if { [gdb_compile "$srcdir/$subdir/$srcfile2" $touch_file_bin executable $opts] != "" } {
65 set target_touch_file_bin [gdb_remote_download target $touch_file_bin]
67 proc do_test { target-non-stop who_forks fork_function stop_mode } {
70 "additional_flags=-DFORK_FUNCTION=$fork_function" \
71 "additional_flags=-DTOUCH_FILE_BIN=\"$::target_touch_file_bin\""]
73 # WHO_FORKS says which of the main or other thread calls (v)fork. The
74 # thread that does not call (v)fork is the one who tries to step.
75 if { $who_forks == "main" } {
76 lappend opts "additional_flags=-DMAIN_THREAD_FORKS"
77 set this_binfile ${::binfile}-main-${fork_function}
78 } elseif { $who_forks == "other" } {
79 lappend opts "additional_flags=-DOTHER_THREAD_FORKS"
80 set this_binfile ${::binfile}-other-${fork_function}
82 error "invalid who_forks value: $who_forks"
85 if { [gdb_compile_pthreads "$::srcdir/$::subdir/$::srcfile" $this_binfile executable $opts] != "" } {
89 remote_file target delete $::touch_file_path
90 gdb_assert { ![remote_file target exists $::touch_file_path] } "file does not exist before test"
92 save_vars { ::GDBFLAGS } {
93 append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\""
94 clean_restart $this_binfile
98 fail "could not run to main"
102 # Run until breakpoint in the second thread.
103 gdb_test "break break_here" "Breakpoint $::decimal.*"
104 gdb_continue_to_breakpoint "thread started"
106 # Delete the breakpoint so the thread doesn't do a step-over.
109 # Let the forking thread make progress during the step.
110 gdb_test "p release_forking_thread = 1" " = 1"
112 # There are two "pending fork child" modes we can test here:
114 # - catch: set up a "catch fork" / "catch vfork" and run to it.
115 # - stepi: stepi the non-forking thread while the forking thread,
117 if { $stop_mode == "catch" } {
118 gdb_test "catch fork"
119 gdb_test "catch vfork"
120 gdb_test "continue" "hit Catchpoint $::decimal.*fork.*"
121 } elseif { $stop_mode == "stepi" } {
122 # stepi the non-forking thread.
125 error "invalid stop_mode value: $stop_mode"
128 # Make sure there's still a single inferior.
129 gdb_test "info inferior" {\* 1 [^\r\n]+}
133 # After being detached, the fork child creates file ::TOUCH_FILE_PATH.
134 # Seeing this file tells us the fork child was detached and executed
136 gdb_assert { [target_file_exists_with_timeout $::touch_file_path] } "file exists after detach"
138 # Don't leave random files on the target system.
139 if { [is_remote target] } {
140 remote_file target delete $::touch_file_path
144 foreach_with_prefix target-non-stop { auto on off } {
145 foreach_with_prefix who_forks { main other } {
146 foreach_with_prefix fork_function { fork vfork } {
147 foreach_with_prefix stop_mode { stepi catch } {
148 do_test ${target-non-stop} $who_forks $fork_function $stop_mode