1 # Copyright 2020-2021 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 # Test that a multi-threaded program doing a vfork doesn't miss breakpoints.
18 # When a program vforks, its address space is shared with the parent. When we
19 # detach a vfork child, we must therefore keep breakpoints out of that address
20 # space until the child either exits or execs. For this reason, threads from
21 # the parent must be held stopped, otherwise they could miss breakpoints.
23 # The thread that did the vfork is suspended by the kernel, so it's not a
24 # concern, the other threads need to be manually stopped by GDB, and restarted
25 # once the vfork critical region is done.
27 # This test spawns one thread that calls vfork. Meanwhile, the main thread
28 # crosses a breakpoint. A buggy GDB would let the main thread run while
29 # breakpoints are removed, so the main thread would miss the breakpoint and run
34 if { [build_executable "failed to prepare" ${testfile} ${srcfile} {debug pthreads}] } {
40 # A bunch of util procedures to continue an inferior to an expected point.
42 proc continue_to_parent_breakpoint {} {
44 "hit Breakpoint .* should_break_here .*" \
45 "continue parent to breakpoint"
48 proc continue_to_parent_end {} {
49 gdb_test "continue" "Inferior 1.*exited normally.*" \
50 "continue parent to end"
53 proc continue_to_child_end {} {
54 gdb_test "continue" "Inferior 2.*exited normally.*" \
55 "continue child to end"
58 proc continue_to_child_end_ns {} {
59 # Since we're in non-stop, the vfork child is always resumed and executes
60 # until exit, while the parent is blocked. When the child exits, the
61 # prompt is shown, then the parent resumes and hits its breakpoint, which
62 # prints additional text to the console. Consume up to the prompt.
63 gdb_test_multiple "continue" "continue to end of inferior 2" {
64 -re "\\\[Inferior 2${::any}exited normally\\\]\r\n${::gdb_prompt} " {
70 proc consume_parent_breakpoint_hit_ns {} {
71 # After the child exits, the parent gets resumed and hits its breakpoint.
72 # Consume part of this output.
73 gdb_test_multiple "" "stop at should_break_here" {
74 -re "Thread 1.1 ${::any} hit Breakpoint ${::decimal}, should_break_here " {
80 # Switch to the parent inferior.
82 proc switch_to_parent { {msg "switch to parent"} } {
83 gdb_test "inferior 1" "Switching to inferior 1 .*" \
87 # Test with detach-on-fork on/off.
89 # Other axes values can be checked using global variables defined by the
90 # iteration below (e.g. ${::non-stop}).
93 if { ${::follow-fork-mode} == "parent" } {
94 continue_to_parent_breakpoint
95 continue_to_parent_end
101 proc test_dof_off {} {
102 if { ${::non-stop} == "on" } {
103 set seen_inferior_1_stop 0
104 set seen_inferior_2_exit 0
106 gdb_test_multiple "continue" "" {
107 -re -wrap "Inferior 2${::any}exited normally.*" {
108 incr seen_inferior_2_exit
111 -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
112 incr seen_inferior_1_stop
116 gdb_test_multiple "" "consume second event" {
117 -re "Inferior 2${::any}exited normally" {
118 incr seen_inferior_2_exit
121 -re "Thread 1.1${::any}hit Breakpoint" {
122 incr seen_inferior_1_stop
126 gdb_assert { $seen_inferior_1_stop == 1 }
127 gdb_assert { $seen_inferior_2_exit == 1 }
130 continue_to_parent_end
131 } elseif { ${::follow-fork-mode} == "parent" && ${::schedule-multiple} == "off" } {
132 gdb_test "continue" \
133 "Can not resume the parent process over vfork .*" \
135 gdb_test "continue" \
136 "Can not resume the parent process over vfork .*" \
139 gdb_test_no_output "set detach-on-fork on"
142 continue_to_child_end
144 continue_to_parent_breakpoint
145 continue_to_parent_end
146 # set seen_inferior_1_stop 0
147 # set seen_inferior_2_exit 0
149 # gdb_test_multiple "continue" "continue 1" {
150 # -re -wrap "Inferior 2${::any}exited normally.*" {
151 # incr seen_inferior_2_exit
154 # -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
155 # incr seen_inferior_1_stop
159 # switch_to_parent "switch to parent 1"
161 # gdb_test_multiple "continue" "continue 2" {
162 # -re -wrap "Inferior 2${::any}exited normally.*" {
163 # incr seen_inferior_2_exit
166 # -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
167 # incr seen_inferior_1_stop
171 # gdb_assert { $seen_inferior_1_stop == 1 }
172 # gdb_assert { $seen_inferior_2_exit == 1 }
174 # switch_to_parent "switch to parent 2"
175 # continue_to_parent_end
179 proc do_test { target-non-stop non-stop follow-fork-mode detach-on-fork schedule-multiple } {
180 save_vars { ::GDBFLAGS } {
181 append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\" -ex \"set non-stop ${non-stop}\""
182 clean_restart ${::binfile}
185 gdb_test_no_output "set follow-fork-mode ${follow-fork-mode}"
186 gdb_test_no_output "set detach-on-fork ${detach-on-fork}"
187 gdb_test_no_output "set schedule-multiple ${schedule-multiple}"
189 if { ![runto_main] } {
193 # The main thread is expected to hit this breakpoint.
194 gdb_test "break should_break_here" "Breakpoint $::decimal at .*"
196 test_dof_${detach-on-fork}
199 foreach_with_prefix target-non-stop {auto on off} {
200 foreach_with_prefix non-stop {off on} {
201 foreach_with_prefix follow-fork-mode {parent child} {
202 foreach_with_prefix detach-on-fork {on off} {
203 foreach_with_prefix schedule-multiple {off on} {
204 do_test ${target-non-stop} ${non-stop} ${follow-fork-mode} ${detach-on-fork} ${schedule-multiple}