aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/detach-step-over.exp
blob: ed9dc1aab88dcc99e21066941ba31e31a404c76a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Copyright 2021-2023 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test detaching from a process that is running and has threads
# constantly hitting a breakpoint and stepping over it, in all
# combinations of:
#
#  - maint target non-stop off/on
#  - set non-stop on/off
#  - displaced stepping on/off
#
# This stresses the edge cases of detaching while a displaced step or
# an in-line step over are in progress.
#
# A fail mode is that the inferior process dies after being detached.
# This can happen because e.g.:
#
# - GDB leaves a breakpoint installed behind, or
#
# - GDB leaves a thread running in the displaced step scratch buffer.
#   With no debugger around to run the finish step, the thread runs
#   off of the scratch buffer, with undefined results.
#
# To exercise this, the testcase reattaches to the process shortly
# after detaching, ensuring the process is still alive and well.
#
# In addition, since GDB may pause threads of all processes for
# stepping over a breakpoint, it needs to re-resume all threads if it
# detaches from the process that was just stepping over the
# breakpoint.  To ensure that, the testcase actually runs a second
# process at the same time as the one that is used to test detaching.
# After the first process is detached, the testcase sends a SIGUSR1 to
# the second process.  If threads failed to be resumed, then the
# SIGUSR1 is never reported to the user, resulting in timeout.  The
# threads of this second process will also be constantly stepping over
# a breakpoint, which has helped with exposing further corner case
# bugs.

require can_spawn_for_attach

standard_testfile

set bp_lineno [gdb_get_line_number "Set breakpoint here"]

# Number of threads started by the program.
set n_threads 10

# Start GDB, configuring various settings according to the arguments.
proc start_gdb_for_test {condition_eval target_non_stop non_stop displaced} {
    save_vars { ::GDBFLAGS } {
	append ::GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
	append ::GDBFLAGS " -ex \"set non-stop $non_stop\""
	append ::GDBFLAGS " -ex \"set displaced $displaced\""
	append ::GDBFLAGS " -ex \"set schedule-multiple on\""
	clean_restart $::binfile
    }

    gdb_test_no_output "set breakpoint condition-evaluation $condition_eval"
}

# Use the 'attach' command to attach to process with pid TESTPID.  Return true
# if we believe GDB has attached and we are back at the GDB prompt, otherwise,
# return false.
proc attach_to {testpid} {
    with_timeout_factor 2 {
	set attached 0
	set saw_attaching 0
	gdb_test_multiple "attach $testpid" "attach" {
	    -re "Attaching to program.*process $testpid\r\n" {
		set saw_attaching 1
		exp_continue
	    }
	    -re "new threads in iteration" {
		# Seen when "set debug libthread_db" is on.
		exp_continue
	    }
	    -re "Reading symbols from|Expanding full symbols from" {
		# Prevent -readnow timeout.
		exp_continue
	    }
	    -re "is a zombie - the process has already terminated.*$::gdb_prompt " {
		fail $gdb_test_name
	    }
	    -re "Unable to attach: .*$::gdb_prompt " {
		fail $gdb_test_name
	    }
	    -re "\r\n$::gdb_prompt " {
		if { $saw_attaching } {
		    set attached 1
		    pass $gdb_test_name
		} else {
		    fail $gdb_test_name
		}
	    }
	}
    }

    return $attached
}

# After attaching to a multi-threaded inferior in non-stop mode, we expect to
# see a stop message from each thread.  This proc waits for all of these stop
# messages.  TID_RE is a regexp used to match the thread-id of the stopped
# thread.
#
# Return true if we saw a stop from each of the expected threads (based on the
# global N_THREADS value), otherwise, return false.
proc check_stops_after_non_stop_attach {tid_re} {
    set any "\[^\r\n\]*"

    # In non-stop, we will see one stop per thread after the prompt.
    set stops 0
    set test "seen all stops"
    for {set thread 1} { $thread <= $::n_threads } { incr thread } {
	if {[gdb_test_multiple "" $test {
	    -re "Thread ${tid_re} ${any} stopped" {
		incr stops
	    }
	}] != 0} {
	    break
	}
    }

    # If we haven't seen all stops, then the
    # gdb_test_multiple in the loop above will have
    # already issued a FAIL.
    if {$stops != $::n_threads} {
	return false
    }
    pass $test
    return true
}

# Prepare for a single test iteration.  TESTPID is the pid of the process GDB
# will be attached too.  NON_STOP indicates if GDB is configured in non-stop
# mode or not.  ATTEMPT is the current attempt number, and ATTEMPTS is the
# maximum number of attempts we plan to run.  TID_RE is a string used to match
# against a thread-id in GDB's stop messages.
#
# Return true if everything is prepared correctly, otherwise return false.
proc prepare_test_iter {testpid non_stop attempt attempts tid_re} {
    if {![attach_to $testpid]} {
	return false
    }

    if {$non_stop} {
	if {![check_stops_after_non_stop_attach $tid_re]} {
	    return false
	}
    }

    gdb_test "break ${::srcfile}:${::bp_lineno} if 0" "Breakpoint.*" \
	"break LOC if 0"

    if {$attempt < $attempts} {
	# Kick the time out timer for another round.
	gdb_test "print again = 1" " = 1" "reset timer in the inferior"
	# Show the time we had left in the logs, in case
	# something goes wrong.
	gdb_test "print seconds_left" " = .*"
    }

    if {$non_stop} {
	set cont_cmd "continue -a &"
    } else {
	set cont_cmd "continue &"
    }

    set cont_cmd_re [string_to_regexp $cont_cmd]
    gdb_test_multiple $cont_cmd "" {
	-re "^$cont_cmd_re\r\nContinuing\.\r\n$::gdb_prompt " {
	    pass $gdb_test_name
	}
    }

    return true
}

# The test proper.  See the description at the top of the file.
proc_with_prefix test_detach_command {condition_eval target_non_stop non_stop displaced} {
    set test_spawn_id [spawn_wait_for_attach $::binfile]
    set testpid [spawn_id_get_pid $test_spawn_id]

    start_gdb_for_test $condition_eval $target_non_stop $non_stop $displaced

    gdb_test "add-inferior" "Added inferior 2.*"
    gdb_test "inferior 2" "Switching to .*"

    gdb_load $::binfile
    if {![runto setup_done]} {
	fail "can't run to setup_done"
	kill_wait_spawned_process $test_spawn_id
	return
    }

    # Get the PID of the test process.
    set pid_inf2 ""
    gdb_test_multiple "p mypid" "get pid of inferior 2" {
	-re " = ($::decimal)\r\n$::gdb_prompt $" {
	    set pid_inf2 $expect_out(1,string)
	    pass $gdb_test_name
	}
    }

    set attempts 3
    for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
	with_test_prefix "iter $attempt" {
	    gdb_test "inferior 1" "Switching to .*"

	    if {![prepare_test_iter $testpid $non_stop \
		      $attempt $attempts "$::decimal\.$::decimal"]} {
		kill_wait_spawned_process $test_spawn_id
		return
	    }

	    set running_count 0
	    set interrupted 0
	    set running_expected [expr ($::n_threads + 1) * 2]
	    gdb_test_multiple "info threads" "threads running" {
		-re "\\(running\\)" {
		    incr running_count
		    exp_continue
		}
		-re "Cannot execute this command while the target is running.*$::gdb_prompt $" {
		    # Testing against a remote server that doesn't do
		    # non-stop mode.  Explicitly interrupt.  This
		    # doesn't test the same code paths in GDB, but
		    # it's still something.
		    set interrupted 1
		    gdb_test_multiple "interrupt" "" {
			-re "$::gdb_prompt " {
			    gdb_test_multiple "" $gdb_test_name {
				-re "received signal SIGINT, Interrupt" {
				    pass $gdb_test_name
				}
			    }
			}
		    }
		}
		-re "$::gdb_prompt " {
		}
	    }

	    if { !$interrupted } {
		set iterations 0
		set max_iterations 10
		while { $running_count < $running_expected } {
		    sleep 1
		    set running_count 0
		    gdb_test_multiple "info threads" "threads running" {
			-re "\\(running\\)" {
			    incr running_count
			    exp_continue
			}
			-re "$::gdb_prompt " {
			}
		    }
		    incr iterations
		    if { $iterations == $max_iterations } {
			break
		    }
		}
		gdb_assert {$running_count == $running_expected} \
		    "all threads running"
	    }

	    gdb_test "detach" "Detaching from.*"

	    if {!$interrupted} {
		# Now test whether inferior 2's thread were really left
		# running.  Currently an inline step-over stops all
		# threads of all processes.  If detach aborts such a step
		# over, then threads of other inferiors should be
		# re-resumed.  Test for that by sending a signal to
		# inferior 2.
		remote_exec target "kill -SIGUSR1 ${pid_inf2}"

		gdb_test_multiple "" "stop with SIGUSR1" {
		    -re "received signal SIGUSR1" {
			pass $gdb_test_name
		    }
		}
	    }

	    delete_breakpoints
	}
    }
    kill_wait_spawned_process $test_spawn_id
}

# Similar to the proc above, but this time, instead of detaching using
# the 'detach' command, we quit GDB, this will also trigger a detach, but
# through a slightly different path, which can expose different bugs.
proc_with_prefix test_detach_quit {condition_eval target_non_stop \
	non_stop displaced} {
    # If debugging with target remote, check whether the all-stop variant
    # of the RSP is being used.  If so, we can't run the background tests.
    if {!$non_stop
	&& [target_info exists gdb_protocol]
	&& ([target_info gdb_protocol] == "remote"
	    || [target_info gdb_protocol] == "extended-remote")} {
	start_gdb_for_test $condition_eval $target_non_stop \
	    $non_stop $displaced

	gdb_test_multiple "maint show target-non-stop" "" {
	    -wrap -re "(is|currently) on.*" {
	    }
	    -wrap -re "(is|currently) off.*" {
		return
	    }
	}
    }

    set test_spawn_id [spawn_wait_for_attach $::binfile]
    set testpid [spawn_id_get_pid $test_spawn_id]

    set attempts 3
    for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
	with_test_prefix "iter $attempt" {

	    start_gdb_for_test $condition_eval $target_non_stop \
		$non_stop $displaced

	    if {![prepare_test_iter $testpid $non_stop \
		      $attempt $attempts "$::decimal"]} {
		kill_wait_spawned_process $test_spawn_id
		return
	    }

	    gdb_test_multiple "with confirm off -- quit" "" {
		eof {
		    pass $gdb_test_name
		}
	    }
	}
    }

    kill_wait_spawned_process $test_spawn_id
}

# The test program exits after a while, in case GDB crashes.  Make it
# wait at least as long as we may wait before declaring a time out
# failure.
set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }

if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
    return -1
}

if ![runto_main] {
    return -1
}

# Probe support for "set breakpoint condition-evaluation target".
# This setting influences who steps over the breakpoint, the (remote)
# target (e.g. gdbserver) or gdb, thus exposing issues on either the
# target or gdb.
set supports_condition_eval_target 1
set cmd "set breakpoint condition-evaluation target"
gdb_test_multiple $cmd "probe condition-evaluation target support" {
    -re "warning: Target does not support breakpoint condition evaluation.\r\nUsing host evaluation mode instead.\r\n$gdb_prompt $" {
	# Target doesn't support breakpoint condition evaluation on
	# its side.
	set supports_condition_eval_target 0
	pass $gdb_test_name
    }
    -re "^$cmd\r\n$gdb_prompt $" {
	pass $gdb_test_name
    }
}

foreach_with_prefix breakpoint-condition-evaluation {"host" "target"} {
    if {!$supports_condition_eval_target && ${breakpoint-condition-evaluation} == "target"} {
	continue
    }

    foreach_with_prefix target-non-stop {"off" "on"} {
	foreach_with_prefix non-stop {"off" "on"} {
	    if {${non-stop} && !${target-non-stop}} {
		# "set non-stop" overrides "maint set
		# target-non-stop", no use testing this combination.
		continue
	    }

	    foreach_with_prefix displaced {"off" "auto"} {
		test_detach_command ${breakpoint-condition-evaluation} \
		    ${target-non-stop} ${non-stop} ${displaced}
		test_detach_quit ${breakpoint-condition-evaluation} \
		    ${target-non-stop} ${non-stop} ${displaced}
	    }
	}
    }
}