gdb: fix vfork with multiple threads
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / vfork-multi-thread.exp
1 # Copyright 2020-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 # 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 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.
22 #
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.
26 #
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
30 # until exit.
31
32 standard_testfile
33
34 if { [build_executable "failed to prepare" ${testfile} ${srcfile} {debug pthreads}] } {
35 return
36 }
37
38 set any "\[^\r\n\]*"
39
40 # A bunch of util procedures to continue an inferior to an expected point.
41
42 proc continue_to_parent_breakpoint {} {
43 gdb_test "continue" \
44 "hit Breakpoint .* should_break_here .*" \
45 "continue parent to breakpoint"
46 }
47
48 proc continue_to_parent_end {} {
49 gdb_test "continue" "Inferior 1.*exited normally.*" \
50 "continue parent to end"
51 }
52
53 proc continue_to_child_end {} {
54 gdb_test "continue" "Inferior 2.*exited normally.*" \
55 "continue child to end"
56 }
57
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} " {
65 pass $gdb_test_name
66 }
67 }
68 }
69
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 " {
75 pass $gdb_test_name
76 }
77 }
78 }
79
80 # Switch to the parent inferior.
81
82 proc switch_to_parent { {msg "switch to parent"} } {
83 gdb_test "inferior 1" "Switching to inferior 1 .*" \
84 $msg
85 }
86
87 # Test with detach-on-fork on/off.
88 #
89 # Other axes values can be checked using global variables defined by the
90 # iteration below (e.g. ${::non-stop}).
91
92 proc test_dof_on {} {
93 if { ${::follow-fork-mode} == "parent" } {
94 continue_to_parent_breakpoint
95 continue_to_parent_end
96 } else {
97 continue_to_child_end
98 }
99 }
100
101 proc test_dof_off {} {
102 if { ${::non-stop} == "on" } {
103 set seen_inferior_1_stop 0
104 set seen_inferior_2_exit 0
105
106 gdb_test_multiple "continue" "" {
107 -re -wrap "Inferior 2${::any}exited normally.*" {
108 incr seen_inferior_2_exit
109 }
110
111 -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
112 incr seen_inferior_1_stop
113 }
114 }
115
116 gdb_test_multiple "" "consume second event" {
117 -re "Inferior 2${::any}exited normally" {
118 incr seen_inferior_2_exit
119 }
120
121 -re "Thread 1.1${::any}hit Breakpoint" {
122 incr seen_inferior_1_stop
123 }
124 }
125
126 gdb_assert { $seen_inferior_1_stop == 1 }
127 gdb_assert { $seen_inferior_2_exit == 1 }
128
129 switch_to_parent
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 .*" \
134 "continue 1"
135 gdb_test "continue" \
136 "Can not resume the parent process over vfork .*" \
137 "continue 2"
138
139 gdb_test_no_output "set detach-on-fork on"
140 test_dof_on
141 } else {
142 continue_to_child_end
143 switch_to_parent
144 continue_to_parent_breakpoint
145 continue_to_parent_end
146 # set seen_inferior_1_stop 0
147 # set seen_inferior_2_exit 0
148 #
149 # gdb_test_multiple "continue" "continue 1" {
150 # -re -wrap "Inferior 2${::any}exited normally.*" {
151 # incr seen_inferior_2_exit
152 # }
153 #
154 # -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
155 # incr seen_inferior_1_stop
156 # }
157 # }
158 #
159 # switch_to_parent "switch to parent 1"
160 #
161 # gdb_test_multiple "continue" "continue 2" {
162 # -re -wrap "Inferior 2${::any}exited normally.*" {
163 # incr seen_inferior_2_exit
164 # }
165 #
166 # -re -wrap "Thread 1.1${::any}hit Breakpoint.*" {
167 # incr seen_inferior_1_stop
168 # }
169 # }
170 #
171 # gdb_assert { $seen_inferior_1_stop == 1 }
172 # gdb_assert { $seen_inferior_2_exit == 1 }
173 #
174 # switch_to_parent "switch to parent 2"
175 # continue_to_parent_end
176 }
177 }
178
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}
183 }
184
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}"
188
189 if { ![runto_main] } {
190 return
191 }
192
193 # The main thread is expected to hit this breakpoint.
194 gdb_test "break should_break_here" "Breakpoint $::decimal at .*"
195
196 test_dof_${detach-on-fork}
197 }
198
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}
205 }
206 }
207 }
208 }
209 }
This page took 0.041028 seconds and 4 git commands to generate.