277185511883984ca5bfa722f57fc2decc8d452f
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / detach-step-over.exp
1 # Copyright 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 detaching from a process that is running and has threads
17 # constantly hitting a breakpoint and stepping over it, in all
18 # combinations of:
19 #
20 # - maint target non-stop off/on
21 # - set non-stop on/off
22 # - displaced stepping on/off
23 #
24 # This stresses the edge cases of detaching while a displaced step or
25 # an in-line step over are in progress.
26 #
27 # A fail mode is that the inferior process dies after being detached.
28 # This can happen because e.g.:
29 #
30 # - GDB leaves a breakpoint installed behind, or
31 #
32 # - GDB leaves a thread running in the displaced step scratch buffer.
33 # With no debugger around to run the finish step, the thread runs
34 # off of the scratch buffer, with undefined results.
35 #
36 # To exercise this, the testcase reattaches to the process shortly
37 # after detaching, ensuring the process is still alive and well.
38 #
39 # In addition, since GDB may pause threads of all processes for
40 # stepping over a breakpoint, it needs to re-resume all threads if it
41 # detaches from the process that was just stepping over the
42 # breakpoint. To ensure that, the testcase actually runs a second
43 # process at the same time as the one that is used to test detaching.
44 # After the first process is detached, the testcase sends a SIGUSR1 to
45 # the second process. If threads failed to be resumed, then the
46 # SIGUSR1 is never reported to the user, resulting in timeout. The
47 # threads of this second process will also be constantly stepping over
48 # a breakpoint, which has helped with exposing further corner case
49 # bugs.
50
51 if {![can_spawn_for_attach]} {
52 return 0
53 }
54
55 standard_testfile
56
57 set bp_lineno [gdb_get_line_number "Set breakpoint here"]
58
59 # The test proper. See description above.
60 proc test {condition_eval target_non_stop non_stop displaced} {
61 global binfile srcfile
62 global gdb_prompt
63 global decimal
64 global bp_lineno
65 global GDBFLAGS
66
67 # Number of threads started by the program.
68 set n_threads 10
69
70 save_vars { GDBFLAGS } {
71 append GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
72 append GDBFLAGS " -ex \"set non-stop $non_stop\""
73 append GDBFLAGS " -ex \"set displaced $displaced\""
74 append GDBFLAGS " -ex \"set schedule-multiple on\""
75 clean_restart $binfile
76 }
77
78 set test_spawn_id [spawn_wait_for_attach $binfile]
79 set testpid [spawn_id_get_pid $test_spawn_id]
80
81 set any "\[^\r\n\]*"
82
83 gdb_test "add-inferior" "Added inferior 2.*"
84 gdb_test "inferior 2" "Switching to .*"
85
86 gdb_load $binfile
87 if ![runto setup_done] then {
88 fail "can't run to setup_done"
89 kill_wait_spawned_process $test_spawn_id
90 return
91 }
92
93 gdb_test_no_output "set breakpoint condition-evaluation $condition_eval"
94
95 # Get the PID of the test process.
96 set pid_inf2 ""
97 gdb_test_multiple "p mypid" "get pid of inferior 2" {
98 -re " = ($decimal)\r\n$gdb_prompt $" {
99 set pid_inf2 $expect_out(1,string)
100 pass $gdb_test_name
101 }
102 }
103
104 set attempts 3
105 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
106 with_test_prefix "iter $attempt" {
107 gdb_test "inferior 1" "Switching to .*"
108
109 with_timeout_factor 2 {
110 set attached 0
111 set saw_attaching 0
112 set eperm 0
113 set test "attach"
114 gdb_test_multiple "attach $testpid" $test {
115 -re "Attaching to program.*process $testpid\r\n" {
116 set saw_attaching 1
117 exp_continue
118 }
119 -re "new threads in iteration" {
120 # Seen when "set debug libthread_db" is on.
121 exp_continue
122 }
123 -re "Reading symbols from|Expanding full symbols from" {
124 # Prevent -readnow timeout.
125 exp_continue
126 }
127 -re "is a zombie - the process has already terminated.*$gdb_prompt " {
128 fail $gdb_test_name
129 }
130 -re "Unable to attach: .*$gdb_prompt " {
131 fail $gdb_test_name
132 }
133 -re "\r\n$gdb_prompt " {
134 if { $saw_attaching } {
135 set attached 1
136 pass $test
137 } else {
138 fail $test
139 }
140 }
141 }
142 }
143
144 if {!$attached} {
145 kill_wait_spawned_process $test_spawn_id
146 return
147 }
148
149 if {$non_stop} {
150 # In non-stop, we will see one stop per thread after
151 # the prompt.
152 set stops 0
153 set tid_re "$::decimal\.$::decimal"
154 set test "seen all stops"
155 for {set thread 1} { $thread <= $n_threads } { incr thread } {
156 if {[gdb_test_multiple "" $test {
157 -re "Thread ${tid_re} ${any} stopped" {
158 incr stops
159 }
160 }] != 0} {
161 break
162 }
163 }
164
165 # If we haven't seen all stops, then the
166 # gdb_test_multiple in the loop above will have
167 # already issued a FAIL.
168 if {$stops != $n_threads} {
169 kill_wait_spawned_process $test_spawn_id
170 return
171 }
172 pass $test
173 }
174
175 # Set threads stepping over a breakpoint continuously.
176 gdb_test "break $srcfile:$bp_lineno if 0" "Breakpoint.*" \
177 "break LOC if 0"
178
179 if {$attempt < $attempts} {
180 # Kick the time out timer for another round.
181 gdb_test "print again = 1" " = 1" "reset timer in the inferior"
182 # Show the time we had left in the logs, in case
183 # something goes wrong.
184 gdb_test "print seconds_left" " = .*"
185 }
186
187 if {$non_stop} {
188 set cont_cmd "continue -a &"
189 } else {
190 set cont_cmd "continue &"
191 }
192
193 set cont_cmd_re [string_to_regexp $cont_cmd]
194 gdb_test_multiple $cont_cmd "" {
195 -re "^$cont_cmd_re\r\nContinuing\.\r\n$gdb_prompt " {
196 pass $gdb_test_name
197 }
198 }
199
200 # Wait a bit, to give time for the threads to hit the
201 # breakpoint.
202 sleep 1
203
204 set running_count 0
205 set interrupted 0
206 gdb_test_multiple "info threads" "all threads running" {
207 -re "\\(running\\)" {
208 incr running_count
209 exp_continue
210 }
211 -re "Cannot execute this command while the target is running.*$gdb_prompt $" {
212 # Testing against a remote server that doesn't do
213 # non-stop mode. Explicitly interrupt. This
214 # doesn't test the same code paths in GDB, but
215 # it's still something.
216 set interrupted 1
217 gdb_test_multiple "interrupt" "" {
218 -re "$gdb_prompt " {
219 gdb_test_multiple "" $gdb_test_name {
220 -re "received signal SIGINT, Interrupt" {
221 pass $gdb_test_name
222 }
223 }
224 }
225 }
226 }
227 -re "$gdb_prompt $" {
228 gdb_assert {$running_count == ($n_threads + 1) * 2} $gdb_test_name
229 }
230 }
231
232 gdb_test "detach" "Detaching from.*"
233
234 if {!$interrupted} {
235 # Now test whether inferior 2's thread were really left
236 # running. Currently an inline step-over stops all
237 # threads of all processes. If detach aborts such a step
238 # over, then threads of other inferiors should be
239 # re-resumed. Test for that by sending a signal to
240 # inferior 2.
241 remote_exec target "kill -SIGUSR1 ${pid_inf2}"
242
243 gdb_test_multiple "" "stop with SIGUSR1" {
244 -re "received signal SIGUSR1" {
245 pass $gdb_test_name
246 }
247 }
248 }
249
250 delete_breakpoints
251 }
252 }
253 kill_wait_spawned_process $test_spawn_id
254 }
255
256 # The test program exits after a while, in case GDB crashes. Make it
257 # wait at least as long as we may wait before declaring a time out
258 # failure.
259 set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }
260
261 if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
262 return -1
263 }
264
265 if ![runto_main] {
266 return -1
267 }
268
269 # Probe support for "set breakpoint condition-evaluation target".
270 # This setting influences who steps over the breakpoint, the (remote)
271 # target (e.g. gdbserver) or gdb, thus exposing issues on either the
272 # target or gdb.
273 set supports_condition_eval_target 1
274 set cmd "set breakpoint condition-evaluation target"
275 gdb_test_multiple $cmd "probe condition-evaluation target support" {
276 -re "warning: Target does not support breakpoint condition evaluation.\r\nUsing host evaluation mode instead.\r\n$gdb_prompt $" {
277 # Target doesn't support breakpoint condition evaluation on
278 # its side.
279 set supports_condition_eval_target 0
280 pass $gdb_test_name
281 }
282 -re "^$cmd\r\n$gdb_prompt $" {
283 pass $gdb_test_name
284 }
285 }
286
287 foreach_with_prefix breakpoint-condition-evaluation {"host" "target"} {
288 if {!$supports_condition_eval_target && ${breakpoint-condition-evaluation} == "target"} {
289 continue
290 }
291
292 foreach_with_prefix target-non-stop {"off" "on"} {
293 foreach_with_prefix non-stop {"off" "on"} {
294 if {${non-stop} && !${target-non-stop}} {
295 # "set non-stop" overrides "maint set
296 # target-non-stop", no use testing this combination.
297 continue
298 }
299
300 foreach_with_prefix displaced {"off" "auto"} {
301 test ${breakpoint-condition-evaluation} ${target-non-stop} ${non-stop} ${displaced}
302 }
303 }
304 }
305 }
This page took 0.045183 seconds and 3 git commands to generate.