1 # Copyright 2020-2024 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 handling a vfork while another inferior is running. The bug that
17 # prompted writing this test case was in the Linux native target. The target
18 # assumed that the vfork-done event it received was for the current inferior
19 # (an invalid assumption, the current inferior is the one randomly selected by
20 # do_target_wait (at the time of writing). This caused the target to drop the
21 # vfork-done event, because it was seen as unneeded and to restart the thread
22 # as if nothing happened. This however resulted in the thread running with
23 # breakpoints not inserted.
25 # To catch the bug, this test verifies that we can hit a breakpoint after a
26 # vfork call, while a second inferior runs in the background.
30 standard_testfile .c -sleep.c
32 set srcfile_sleep $srcfile2
33 set binfile_sleep ${binfile}-sleep
35 # The reproducibility of the bug depends on which inferior randomly selects in
36 # do_target_wait when consuming the vfork-done event. Since GDB doesn't call
37 # srand(), we are likely to always see the same sequence of inferior selected by
38 # do_target_wait, which can hide the bug if you are not "lucky". To work
39 # around that, call vfork and hit the breakpoint in a loop, it makes it
40 # somewhat likely that the wrong inferior will be selected eventually.
43 # Compile the main program that calls vfork and hits a breakpoint.
44 set opts [list debug additional_flags=-DNR_LOOPS=$nr_loops]
45 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable \
47 untested "failed to compile"
51 # Compile the secondary program, which just sleeps.
52 if { [gdb_compile "${srcdir}/${subdir}/${srcfile_sleep}" "${binfile_sleep}" executable \
54 untested "failed to compile"
58 # We exercise two methods of getting a second inferior to execute while the
59 # first one vforks. METHOD can be:
61 # - non-stop: start GDB with non-stop on and run the second inferior in
63 # - schedule-multiple: set "schedule-multiple on", this will make "continue"
64 # resume both inferiors.
65 proc do_test {method} {
66 save_vars { ::GDBFLAGS } {
67 if { $method == "non-stop" } {
68 append ::GDBFLAGS " -ex \"set non-stop on\""
73 # Start the second inferior in background.
74 gdb_test "add-inferior" "Added inferior 2.*"
75 gdb_test "inferior 2" "Switching to inferior 2 .*"
76 gdb_file_cmd ${::binfile_sleep}
77 if { $method == "non-stop" } {
78 gdb_test "run &" "Starting program: .*" "run inferior 2"
80 gdb_test "start" "Temporary breakpoint $::decimal, main .*" \
84 # Start the first inferior.
85 gdb_test "inferior 1" "Switching to inferior 1 .*"
86 gdb_file_cmd ${::binfile}
87 gdb_test "break should_break_here" "Breakpoint $::decimal at .*"
88 gdb_test "start" "Thread 1.1 .* hit Temporary breakpoint.*" \
91 # Only enable schedule-multiple this late, because of:
92 # https://sourceware.org/bugzilla/show_bug.cgi?id=28777
93 if { $method == "schedule-multiple" } {
94 gdb_test_no_output "set schedule-multiple on"
98 # Continue over vfork and until the breakpoint. The number of loops here
99 # matches the number of loops in the program. So if a breakpoint is missed
100 # at some point, a "continue" will wrongfully continue until the end of the
101 # program, which will fail the test.
102 for {set i 0} {$i < $::nr_loops} {incr i} {
103 with_test_prefix "i=$i" {
104 gdb_test "continue" \
105 "Thread 1.1 .* hit Breakpoint $::decimal, should_break_here.*"
110 foreach_with_prefix method {schedule-multiple non-stop} {