gdb, gdbserver: detach fork child when detaching from fork parent
[binutils-gdb.git] / gdb / testsuite / gdb.threads / pending-fork-event-detach.exp
1 # Copyright (C) 2021 Free Software Foundation, Inc.
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 # 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:
19 #
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.
26 #
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.
30 #
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.
36
37 standard_testfile .c -touch-file.c
38
39 set touch_file_bin $binfile-touch-file
40
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
45 } else {
46 set touch_file_path [standard_output_file flag]
47 }
48
49 set opts [list debug "additional_flags=-DTOUCH_FILE_PATH=\"$touch_file_path\""]
50 if { [gdb_compile "$srcdir/$subdir/$srcfile2" $touch_file_bin executable $opts] != "" } {
51 return
52 }
53
54 proc do_test { target-non-stop who_forks fork_function stop_mode } {
55 set opts [list \
56 debug \
57 "additional_flags=-DFORK_FUNCTION=$fork_function" \
58 "additional_flags=-DTOUCH_FILE_BIN=\"$::touch_file_bin\""]
59
60 # WHO_FORKS says which of the main or other thread calls (v)fork. The
61 # thread that does not call (v)fork is the one who tries to step.
62 if { $who_forks == "main" } {
63 lappend opts "additional_flags=-DMAIN_THREAD_FORKS"
64 set this_binfile ${::binfile}-main-${fork_function}
65 } elseif { $who_forks == "other" } {
66 lappend opts "additional_flags=-DOTHER_THREAD_FORKS"
67 set this_binfile ${::binfile}-other-${fork_function}
68 } else {
69 error "invalid who_forks value: $who_forks"
70 }
71
72 if { [gdb_compile_pthreads "$::srcdir/$::subdir/$::srcfile" $this_binfile executable $opts] != "" } {
73 return
74 }
75
76 remote_file target delete $::touch_file_path
77 gdb_assert { ![remote_file target exists $::touch_file_path] } "file does not exist before test"
78
79 save_vars { ::GDBFLAGS } {
80 append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\""
81 clean_restart $this_binfile
82 }
83
84 if {![runto_main]} {
85 fail "could not run to main"
86 return
87 }
88
89 # Run until breakpoint in the second thread.
90 gdb_test "break break_here" "Breakpoint $::decimal.*"
91 gdb_continue_to_breakpoint "thread started"
92
93 # Delete the breakpoint so the thread doesn't do a step-over.
94 delete_breakpoints
95
96 # Let the forking thread make progress during the step.
97 gdb_test "p release_forking_thread = 1" " = 1"
98
99 # There are two "pending fork child" modes we can test here:
100 #
101 # - catch: set up a "catch fork" / "catch vfork" and run to it.
102 # - stepi: stepi the non-forking thread while the forking thread,
103 # well, forks.
104 if { $stop_mode == "catch" } {
105 gdb_test "catch fork"
106 gdb_test "catch vfork"
107 gdb_test "continue" "hit Catchpoint $::decimal.*fork.*"
108 } elseif { $stop_mode == "stepi" } {
109 # stepi the non-forking thread.
110 gdb_test "stepi"
111 } else {
112 error "invalid stop_mode value: $stop_mode"
113 }
114
115 # Make sure there's still a single inferior.
116 gdb_test "info inferior" {\* 1 [^\r\n]+}
117
118 gdb_test "detach"
119
120 # After being detached, the fork child creates file ::TOUCH_FILE_PATH.
121 # Seeing this file tells us the fork child was detached and executed
122 # successfully.
123 gdb_assert { [target_file_exists_with_timeout $::touch_file_path] } "file exists after detach"
124
125 # Don't leave random files on the target system.
126 if { [is_remote target] } {
127 remote_file target delete $::touch_file_path
128 }
129 }
130
131 foreach_with_prefix target-non-stop { auto on off } {
132 foreach_with_prefix who_forks { main other } {
133 foreach_with_prefix fork_function { fork vfork } {
134 foreach_with_prefix stop_mode { stepi catch } {
135 do_test ${target-non-stop} $who_forks $fork_function $stop_mode
136 }
137 }
138 }
139 }