]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/pending-fork-event-detach.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.exp
CommitLineData
1d506c26 1# Copyright (C) 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# 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
37standard_testfile .c -touch-file.c
38
39set touch_file_bin $binfile-touch-file
40
41if { [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
2562954e
TV
45
46 # Now get the remote name, by creating the file on build and copying it to
47 # target.
48 remote_exec build touch $touch_file_path
49 set target_touch_file_path [remote_download target $touch_file_path]
50
51 # Clean up.
52 remote_file build delete $touch_file_path
53 remote_file target delete $target_touch_file_path
54
55 set touch_file_path $target_touch_file_path
df5ad102
SM
56} else {
57 set touch_file_path [standard_output_file flag]
58}
59
60set opts [list debug "additional_flags=-DTOUCH_FILE_PATH=\"$touch_file_path\""]
61if { [gdb_compile "$srcdir/$subdir/$srcfile2" $touch_file_bin executable $opts] != "" } {
62 return
63}
64
2562954e
TV
65set target_touch_file_bin [gdb_remote_download target $touch_file_bin]
66
df5ad102
SM
67proc do_test { target-non-stop who_forks fork_function stop_mode } {
68 set opts [list \
69 debug \
70 "additional_flags=-DFORK_FUNCTION=$fork_function" \
2562954e 71 "additional_flags=-DTOUCH_FILE_BIN=\"$::target_touch_file_bin\""]
df5ad102
SM
72
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}
81 } else {
82 error "invalid who_forks value: $who_forks"
83 }
84
85 if { [gdb_compile_pthreads "$::srcdir/$::subdir/$::srcfile" $this_binfile executable $opts] != "" } {
86 return
87 }
88
89 remote_file target delete $::touch_file_path
90 gdb_assert { ![remote_file target exists $::touch_file_path] } "file does not exist before test"
91
92 save_vars { ::GDBFLAGS } {
93 append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\""
94 clean_restart $this_binfile
95 }
96
97 if {![runto_main]} {
98 fail "could not run to main"
99 return
100 }
101
102 # Run until breakpoint in the second thread.
103 gdb_test "break break_here" "Breakpoint $::decimal.*"
104 gdb_continue_to_breakpoint "thread started"
105
106 # Delete the breakpoint so the thread doesn't do a step-over.
107 delete_breakpoints
108
109 # Let the forking thread make progress during the step.
110 gdb_test "p release_forking_thread = 1" " = 1"
111
112 # There are two "pending fork child" modes we can test here:
113 #
114 # - catch: set up a "catch fork" / "catch vfork" and run to it.
115 # - stepi: stepi the non-forking thread while the forking thread,
116 # well, forks.
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.
123 gdb_test "stepi"
124 } else {
125 error "invalid stop_mode value: $stop_mode"
126 }
127
128 # Make sure there's still a single inferior.
129 gdb_test "info inferior" {\* 1 [^\r\n]+}
130
131 gdb_test "detach"
132
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
135 # successfully.
136 gdb_assert { [target_file_exists_with_timeout $::touch_file_path] } "file exists after detach"
137
138 # Don't leave random files on the target system.
139 if { [is_remote target] } {
140 remote_file target delete $::touch_file_path
141 }
142}
143
144foreach_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
149 }
150 }
151 }
152}