]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/detach-step-over.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / detach-step-over.exp
1 # Copyright 2021-2024 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 require can_spawn_for_attach
52
53 standard_testfile
54
55 set bp_lineno [gdb_get_line_number "Set breakpoint here"]
56
57 # Number of threads started by the program.
58 set n_threads 10
59
60 # Start GDB, configuring various settings according to the arguments.
61 proc start_gdb_for_test {condition_eval target_non_stop non_stop displaced} {
62 save_vars { ::GDBFLAGS } {
63 append ::GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
64 append ::GDBFLAGS " -ex \"set non-stop $non_stop\""
65 append ::GDBFLAGS " -ex \"set displaced $displaced\""
66 append ::GDBFLAGS " -ex \"set schedule-multiple on\""
67 clean_restart $::binfile
68 }
69
70 gdb_test_no_output "set breakpoint condition-evaluation $condition_eval"
71 }
72
73 # Use the 'attach' command to attach to process with pid TESTPID. Return true
74 # if we believe GDB has attached and we are back at the GDB prompt, otherwise,
75 # return false.
76 proc attach_to {testpid} {
77 with_timeout_factor 2 {
78 set attached 0
79 set saw_attaching 0
80 gdb_test_multiple "attach $testpid" "attach" {
81 -re "Attaching to program.*process $testpid\r\n" {
82 set saw_attaching 1
83 exp_continue
84 }
85 -re "new threads in iteration" {
86 # Seen when "set debug libthread_db" is on.
87 exp_continue
88 }
89 -re "Reading symbols from|Expanding full symbols from" {
90 # Prevent -readnow timeout.
91 exp_continue
92 }
93 -re "is a zombie - the process has already terminated.*$::gdb_prompt " {
94 fail $gdb_test_name
95 }
96 -re "Unable to attach: .*$::gdb_prompt " {
97 fail $gdb_test_name
98 }
99 -re "\r\n$::gdb_prompt " {
100 if { $saw_attaching } {
101 set attached 1
102 pass $gdb_test_name
103 } else {
104 fail $gdb_test_name
105 }
106 }
107 }
108 }
109
110 return $attached
111 }
112
113 # After attaching to a multi-threaded inferior in non-stop mode, we expect to
114 # see a stop message from each thread. This proc waits for all of these stop
115 # messages. TID_RE is a regexp used to match the thread-id of the stopped
116 # thread.
117 #
118 # Return true if we saw a stop from each of the expected threads (based on the
119 # global N_THREADS value), otherwise, return false.
120 proc check_stops_after_non_stop_attach {tid_re} {
121 set any "\[^\r\n\]*"
122
123 # In non-stop, we will see one stop per thread after the prompt.
124 set stops 0
125 set test "seen all stops"
126 for {set thread 1} { $thread <= $::n_threads } { incr thread } {
127 if {[gdb_test_multiple "" $test {
128 -re "Thread ${tid_re} ${any} stopped" {
129 incr stops
130 }
131 }] != 0} {
132 break
133 }
134 }
135
136 # If we haven't seen all stops, then the
137 # gdb_test_multiple in the loop above will have
138 # already issued a FAIL.
139 if {$stops != $::n_threads} {
140 return false
141 }
142 pass $test
143 return true
144 }
145
146 # Prepare for a single test iteration. TESTPID is the pid of the process GDB
147 # will be attached too. NON_STOP indicates if GDB is configured in non-stop
148 # mode or not. ATTEMPT is the current attempt number, and ATTEMPTS is the
149 # maximum number of attempts we plan to run. TID_RE is a string used to match
150 # against a thread-id in GDB's stop messages.
151 #
152 # Return true if everything is prepared correctly, otherwise return false.
153 proc prepare_test_iter {testpid non_stop attempt attempts tid_re} {
154 if {![attach_to $testpid]} {
155 return false
156 }
157
158 if {$non_stop} {
159 if {![check_stops_after_non_stop_attach $tid_re]} {
160 return false
161 }
162 }
163
164 gdb_test "break ${::srcfile}:${::bp_lineno} if 0" "Breakpoint.*" \
165 "break LOC if 0"
166
167 if {$attempt < $attempts} {
168 # Kick the time out timer for another round.
169 gdb_test "print again = 1" " = 1" "reset timer in the inferior"
170 # Show the time we had left in the logs, in case
171 # something goes wrong.
172 gdb_test "print seconds_left" " = .*"
173 }
174
175 if {$non_stop} {
176 set cont_cmd "continue -a &"
177 } else {
178 set cont_cmd "continue &"
179 }
180
181 set cont_cmd_re [string_to_regexp $cont_cmd]
182 gdb_test_multiple $cont_cmd "" {
183 -re "^$cont_cmd_re\r\nContinuing\.\r\n$::gdb_prompt " {
184 pass $gdb_test_name
185 }
186 }
187
188 return true
189 }
190
191 # The test proper. See the description at the top of the file.
192 proc_with_prefix test_detach_command {condition_eval target_non_stop non_stop displaced} {
193 set test_spawn_id [spawn_wait_for_attach $::binfile]
194 set testpid [spawn_id_get_pid $test_spawn_id]
195
196 start_gdb_for_test $condition_eval $target_non_stop $non_stop $displaced
197
198 gdb_test "add-inferior" "Added inferior 2.*"
199 gdb_test "inferior 2" "Switching to .*"
200
201 gdb_load $::binfile
202 if {![runto setup_done]} {
203 fail "can't run to setup_done"
204 kill_wait_spawned_process $test_spawn_id
205 return
206 }
207
208 # Get the PID of the test process.
209 set pid_inf2 ""
210 gdb_test_multiple "p mypid" "get pid of inferior 2" {
211 -re " = ($::decimal)\r\n$::gdb_prompt $" {
212 set pid_inf2 $expect_out(1,string)
213 pass $gdb_test_name
214 }
215 }
216
217 set attempts 3
218 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
219 with_test_prefix "iter $attempt" {
220 gdb_test "inferior 1" "Switching to .*"
221
222 if {![prepare_test_iter $testpid $non_stop \
223 $attempt $attempts "$::decimal\.$::decimal"]} {
224 kill_wait_spawned_process $test_spawn_id
225 return
226 }
227
228 set running_count 0
229 set interrupted 0
230 set running_expected [expr ($::n_threads + 1) * 2]
231 gdb_test_multiple "info threads" "threads running" {
232 -re "\\(running\\)" {
233 incr running_count
234 exp_continue
235 }
236 -re "Cannot execute this command while the target is running.*$::gdb_prompt $" {
237 # Testing against a remote server that doesn't do
238 # non-stop mode. Explicitly interrupt. This
239 # doesn't test the same code paths in GDB, but
240 # it's still something.
241 set interrupted 1
242 gdb_test_multiple "interrupt" "" {
243 -re "$::gdb_prompt " {
244 gdb_test_multiple "" $gdb_test_name {
245 -re "received signal SIGINT, Interrupt" {
246 pass $gdb_test_name
247 }
248 }
249 }
250 }
251 }
252 -re "$::gdb_prompt " {
253 }
254 }
255
256 if { !$interrupted } {
257 set iterations 0
258 set max_iterations 10
259 while { $running_count < $running_expected } {
260 sleep 1
261 set running_count 0
262 gdb_test_multiple "info threads" "threads running" {
263 -re "\\(running\\)" {
264 incr running_count
265 exp_continue
266 }
267 -re "$::gdb_prompt " {
268 }
269 }
270 incr iterations
271 if { $iterations == $max_iterations } {
272 break
273 }
274 }
275 gdb_assert {$running_count == $running_expected} \
276 "all threads running"
277 }
278
279 gdb_test "detach" "Detaching from.*"
280
281 if {!$interrupted} {
282 # Now test whether inferior 2's thread were really left
283 # running. Currently an inline step-over stops all
284 # threads of all processes. If detach aborts such a step
285 # over, then threads of other inferiors should be
286 # re-resumed. Test for that by sending a signal to
287 # inferior 2.
288 remote_exec target "kill -USR1 ${pid_inf2}"
289
290 gdb_test_multiple "" "stop with SIGUSR1" {
291 -re "received signal SIGUSR1" {
292 pass $gdb_test_name
293 }
294 }
295 }
296
297 delete_breakpoints
298 }
299 }
300 kill_wait_spawned_process $test_spawn_id
301 }
302
303 # Similar to the proc above, but this time, instead of detaching using
304 # the 'detach' command, we quit GDB, this will also trigger a detach, but
305 # through a slightly different path, which can expose different bugs.
306 proc_with_prefix test_detach_quit {condition_eval target_non_stop \
307 non_stop displaced} {
308 # If debugging with target remote, check whether the all-stop variant
309 # of the RSP is being used. If so, we can't run the background tests.
310 if {!$non_stop
311 && [target_info exists gdb_protocol]
312 && ([target_info gdb_protocol] == "remote"
313 || [target_info gdb_protocol] == "extended-remote")} {
314 start_gdb_for_test $condition_eval $target_non_stop \
315 $non_stop $displaced
316
317 if {![is_target_non_stop]} {
318 return
319 }
320 }
321
322 set test_spawn_id [spawn_wait_for_attach $::binfile]
323 set testpid [spawn_id_get_pid $test_spawn_id]
324
325 set attempts 3
326 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
327 with_test_prefix "iter $attempt" {
328
329 start_gdb_for_test $condition_eval $target_non_stop \
330 $non_stop $displaced
331
332 if {![prepare_test_iter $testpid $non_stop \
333 $attempt $attempts "$::decimal"]} {
334 kill_wait_spawned_process $test_spawn_id
335 return
336 }
337
338 gdb_test_multiple "with confirm off -- quit" "" {
339 eof {
340 pass $gdb_test_name
341 }
342 }
343 }
344 }
345
346 kill_wait_spawned_process $test_spawn_id
347 }
348
349 # The test program exits after a while, in case GDB crashes. Make it
350 # wait at least as long as we may wait before declaring a time out
351 # failure.
352 set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }
353
354 if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
355 return -1
356 }
357
358 if ![runto_main] {
359 return -1
360 }
361
362 # Probe support for "set breakpoint condition-evaluation target".
363 # This setting influences who steps over the breakpoint, the (remote)
364 # target (e.g. gdbserver) or gdb, thus exposing issues on either the
365 # target or gdb.
366 set supports_condition_eval_target 1
367 set cmd "set breakpoint condition-evaluation target"
368 gdb_test_multiple $cmd "probe condition-evaluation target support" {
369 -re "warning: Target does not support breakpoint condition evaluation.\r\nUsing host evaluation mode instead.\r\n$gdb_prompt $" {
370 # Target doesn't support breakpoint condition evaluation on
371 # its side.
372 set supports_condition_eval_target 0
373 pass $gdb_test_name
374 }
375 -re "^$cmd\r\n$gdb_prompt $" {
376 pass $gdb_test_name
377 }
378 }
379
380 foreach_with_prefix breakpoint-condition-evaluation {"host" "target"} {
381 if {!$supports_condition_eval_target && ${breakpoint-condition-evaluation} == "target"} {
382 continue
383 }
384
385 foreach_with_prefix target-non-stop {"off" "on"} {
386 foreach_with_prefix non-stop {"off" "on"} {
387 if {${non-stop} && !${target-non-stop}} {
388 # "set non-stop" overrides "maint set
389 # target-non-stop", no use testing this combination.
390 continue
391 }
392
393 foreach_with_prefix displaced {"off" "auto"} {
394 test_detach_command ${breakpoint-condition-evaluation} \
395 ${target-non-stop} ${non-stop} ${displaced}
396 test_detach_quit ${breakpoint-condition-evaluation} \
397 ${target-non-stop} ${non-stop} ${displaced}
398 }
399 }
400 }
401 }