gdb: fix handling of vfork by multi-threaded program (follow-fork-mode=parent, detach...
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / vfork-multi-thread.exp
CommitLineData
81d92403
SM
1# Copyright 2022 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# Test that a multi-threaded program doing a vfork doesn't miss breakpoints.
17#
18# When a program vforks, its address space is shared with the parent. When we
19# detach a vfork child, we must keep breakpoints out of that shared address space
20# until the child either exits or execs, so that the child does not hit a
21# breakpoint while out of GDB's control. During that time, threads from
22# the parent must be held stopped, otherwise they could miss breakpoints.
23#
24# The thread that did the vfork is suspended by the kernel, so it's not a
25# concern. The other threads need to be manually stopped by GDB and resumed
26# once the vfork critical region is done.
27#
28# This test spawns one thread that calls vfork. Meanwhile, the main thread
29# crosses a breakpoint. A buggy GDB would let the main thread run while
30# breakpoints are removed, so the main thread would miss the breakpoint and run
31# until exit.
32
33standard_testfile
34
35if { [build_executable "failed to prepare" ${testfile} ${srcfile} {debug pthreads}] } {
36 return
37}
38
39set any "\[^\r\n\]*"
40
41# A bunch of util procedures to continue an inferior to an expected point.
42
43proc continue_to_parent_breakpoint {} {
44 gdb_test "continue" \
45 "hit Breakpoint .* should_break_here .*" \
46 "continue parent to breakpoint"
47}
48
49proc continue_to_parent_end {} {
50 gdb_test "continue" "Inferior 1.*exited with code 06.*" \
51 "continue parent to end"
52}
53
54# Run the test with the given GDB settings.
55
56proc do_test { target-non-stop non-stop follow-fork-mode detach-on-fork schedule-multiple } {
57 save_vars { ::GDBFLAGS } {
58 append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\""
59 append ::GDBFLAGS " -ex \"set non-stop ${non-stop}\""
60 clean_restart ${::binfile}
61 }
62
63 gdb_test_no_output "set follow-fork-mode ${follow-fork-mode}"
64 gdb_test_no_output "set detach-on-fork ${detach-on-fork}"
65 gdb_test_no_output "set schedule-multiple ${schedule-multiple}"
66
67 # The message about thread 2 of inferior 1 exiting happens at a somewhat
68 # unpredictable moment, it's simpler to silence it than to try to match it.
69 gdb_test_no_output "set print thread-events off"
70
71 if { ![runto_main] } {
72 return
73 }
74
75 # The main thread is expected to hit this breakpoint.
76 gdb_test "break should_break_here" "Breakpoint $::decimal at .*"
77
78 continue_to_parent_breakpoint
79 continue_to_parent_end
80}
81
82# We only test with follow-fork-mode=parent and detach-on-fork=on at the
83# moment, but the loops below are written to make it easy to add other values
84# on these axes in the future.
85
86foreach_with_prefix target-non-stop {auto on off} {
87 foreach_with_prefix non-stop {off on} {
88 foreach_with_prefix follow-fork-mode {parent} {
89 foreach_with_prefix detach-on-fork {on} {
90 foreach_with_prefix schedule-multiple {off on} {
91 do_test ${target-non-stop} ${non-stop} ${follow-fork-mode} ${detach-on-fork} ${schedule-multiple}
92 }
93 }
94 }
95 }
96}
This page took 0.028274 seconds and 4 git commands to generate.