+# Copyright 2020-2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test that a multi-threaded program doing a vfork doesn't miss breakpoints.
+#
+# When a program vforks, its address space is shared with the parent. When we
+# detach a vfork child, we must therefore keep breakpoints out of that address
+# space until the child either exits or execs. For this reason, threads from
+# the parent must be held stopped, otherwise they could miss breakpoints.
+#
+# The thread that did the vfork is suspended by the kernel, so it's not a
+# concern, the other threads need to be manually stopped by GDB, and restarted
+# once the vfork critical region is done.
+#
+# This test spawns one thread that calls vfork. Meanwhile, the main thread
+# crosses a breakpoint. A buggy GDB would let the main thread run while
+# breakpoints are removed, so the main thread would miss the breakpoint and run
+# until exit.
+
+standard_testfile
+
+if { [build_executable "failed to prepare" ${testfile} ${srcfile} {debug pthreads}] } {
+ return
+}
+
+set any "\[^\r\n\]*"
+
+# A bunch of util procedures to continue an inferior to an expected point.
+
+proc continue_to_parent_breakpoint {} {
+ gdb_test "continue" \
+ "hit Breakpoint .* should_break_here .*" \
+ "continue parent to breakpoint"
+}
+
+proc continue_to_parent_end {} {
+ gdb_test "continue" "Inferior 1.*exited normally.*" \
+ "continue parent to end"
+}
+
+proc continue_to_child_end {} {
+ gdb_test "continue" "Inferior 2.*exited normally.*" \
+ "continue child to end"
+}
+
+proc continue_to_child_end_ns {} {
+ # Since we're in non-stop, the vfork child is always resumed and executes
+ # until exit, while the parent is blocked. When the child exits, the
+ # prompt is shown, then the parent resumes and hits its breakpoint, which
+ # prints additional text to the console. Consume up to the prompt.
+ gdb_test_multiple "continue" "continue to end of inferior 2" {
+ -re "\\\[Inferior 2${::any}exited normally\\\]\r\n${::gdb_prompt} " {
+ pass $gdb_test_name
+ }
+ }
+}
+
+proc consume_parent_breakpoint_hit_ns {} {
+ # After the child exits, the parent gets resumed and hits its breakpoint.
+ # Consume part of this output.
+ gdb_test_multiple "" "stop at should_break_here" {
+ -re "Thread 1.1 ${::any} hit Breakpoint ${::decimal}, should_break_here " {
+ pass $gdb_test_name
+ }
+ }
+}
+
+# Switch to the parent inferior.
+
+proc switch_to_parent { {msg "switch to parent"} } {
+ gdb_test "inferior 1" "Switching to inferior 1 .*" \
+ $msg
+}
+
+# Test with detach-on-fork on/off.
+#
+# Other axes values can be checked using global variables defined by the
+# iteration below (e.g. ${::non-stop}).
+
+proc test_dof_on {} {
+ if { ${::follow-fork-mode} == "parent" } {
+ continue_to_parent_breakpoint
+ continue_to_parent_end
+ } else {
+ continue_to_child_end
+ }
+}
+
+proc test_dof_off {} {
+ if { ${::non-stop} == "on" } {
+ set seen_inferior_1_stop 0
+ set seen_inferior_2_exit 0
+
+ gdb_test_multiple "continue" "" {
+ -re -wrap "Inferior 2${::any}exited normally.*" {
+ incr seen_inferior_2_exit
+ }
+
+ -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
+ incr seen_inferior_1_stop
+ }
+ }
+
+ gdb_test_multiple "" "consume second event" {
+ -re "Inferior 2${::any}exited normally" {
+ incr seen_inferior_2_exit
+ }
+
+ -re "Thread 1.1${::any}hit Breakpoint" {
+ incr seen_inferior_1_stop
+ }
+ }
+
+ gdb_assert { $seen_inferior_1_stop == 1 }
+ gdb_assert { $seen_inferior_2_exit == 1 }
+
+ switch_to_parent
+ continue_to_parent_end
+ } elseif { ${::follow-fork-mode} == "parent" && ${::schedule-multiple} == "off" } {
+ gdb_test "continue" \
+ "Can not resume the parent process over vfork .*" \
+ "continue 1"
+ gdb_test "continue" \
+ "Can not resume the parent process over vfork .*" \
+ "continue 2"
+
+ gdb_test_no_output "set detach-on-fork on"
+ test_dof_on
+ } else {
+ continue_to_child_end
+ switch_to_parent
+ continue_to_parent_breakpoint
+ continue_to_parent_end
+# set seen_inferior_1_stop 0
+# set seen_inferior_2_exit 0
+#
+# gdb_test_multiple "continue" "continue 1" {
+# -re -wrap "Inferior 2${::any}exited normally.*" {
+# incr seen_inferior_2_exit
+# }
+#
+# -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
+# incr seen_inferior_1_stop
+# }
+# }
+#
+# switch_to_parent "switch to parent 1"
+#
+# gdb_test_multiple "continue" "continue 2" {
+# -re -wrap "Inferior 2${::any}exited normally.*" {
+# incr seen_inferior_2_exit
+# }
+#
+# -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
+# incr seen_inferior_1_stop
+# }
+# }
+#
+# gdb_assert { $seen_inferior_1_stop == 1 }
+# gdb_assert { $seen_inferior_2_exit == 1 }
+#
+# switch_to_parent "switch to parent 2"
+# continue_to_parent_end
+ }
+}
+
+proc do_test { target-non-stop non-stop follow-fork-mode detach-on-fork schedule-multiple } {
+ save_vars { ::GDBFLAGS } {
+ append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\" -ex \"set non-stop ${non-stop}\""
+ clean_restart ${::binfile}
+ }
+
+ gdb_test_no_output "set follow-fork-mode ${follow-fork-mode}"
+ gdb_test_no_output "set detach-on-fork ${detach-on-fork}"
+ gdb_test_no_output "set schedule-multiple ${schedule-multiple}"
+
+ if { ![runto_main] } {
+ return
+ }
+
+ # The main thread is expected to hit this breakpoint.
+ gdb_test "break should_break_here" "Breakpoint $::decimal at .*"
+
+ test_dof_${detach-on-fork}
+}
+
+foreach_with_prefix target-non-stop {auto on off} {
+ foreach_with_prefix non-stop {off on} {
+ foreach_with_prefix follow-fork-mode {parent child} {
+ foreach_with_prefix detach-on-fork {on off} {
+ foreach_with_prefix schedule-multiple {off on} {
+ do_test ${target-non-stop} ${non-stop} ${follow-fork-mode} ${detach-on-fork} ${schedule-multiple}
+ }
+ }
+ }
+ }
+}