1 # Copyright (C) 2016-2023 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 # Regression test for PR threads/19461 (strange "info thread" behavior
17 # in non-stop). GDB used to miss updating the parent/child running
18 # states after a fork.
24 proc do_test { detach_on_fork follow_fork non_stop schedule_multiple } {
26 global srcfile testfile
29 save_vars { GDBFLAGS } {
30 append GDBFLAGS " -ex \"set non-stop $non_stop\""
32 if {[prepare_for_testing "failed to prepare" \
33 $testfile $srcfile {debug}] == -1} {
42 # If debugging with target remote, check whether the all-stop
43 # variant of the RSP is being used. If so, we can't run the
45 if { [target_info exists gdb_protocol]
46 && ([target_info gdb_protocol] == "remote"
47 || [target_info gdb_protocol] == "extended-remote")} {
49 set test "maint show target-non-stop"
50 gdb_test_multiple "maint show target-non-stop" $test {
51 -re "(is|currently) on.*$gdb_prompt $" {
53 -re "(is|currently) off.*$gdb_prompt $" {
54 unsupported "can't issue info threads while target is running"
60 # We want to catch "[New inferior ...]" below, to avoid sleeping.
61 if {$detach_on_fork == "off" || $follow_fork == "child"} {
62 gdb_test_no_output "set print inferior-events on"
65 gdb_test_no_output "set detach-on-fork $detach_on_fork"
67 gdb_test_no_output "set follow-fork $follow_fork"
68 if {$non_stop == "off"} {
69 gdb_test_no_output "set schedule-multiple $schedule_multiple"
72 # If we're detaching from the parent (or child), then tell it to
73 # exit itself when its child (or parent) exits. If we stay
74 # attached, we take care of killing it.
75 if {$detach_on_fork == "on"} {
76 gdb_test "print exit_if_relative_exits = 1" " = 1"
80 gdb_test_multiple $test $test {
86 if {$detach_on_fork == "off" || $follow_fork == "child"} {
87 set test "fork child appears"
88 gdb_test_multiple "" $test {
89 -re "\\\[New inferior " {
94 # All we can do is wait a little bit for the parent to fork.
98 set not_nl "\[^\r\n\]*"
100 if {$detach_on_fork == "on" && $follow_fork == "child"} {
101 gdb_test "info threads" \
102 " 2.1 ${not_nl}\\\(running\\\).*No selected thread.*"
103 } elseif {$detach_on_fork == "on"} {
104 gdb_test "info threads" \
105 "\\\* 1 ${not_nl}\\\(running\\\)"
106 } elseif {$non_stop == "on" || $schedule_multiple == "on"} {
107 # Both parent and child should be marked running, and the
108 # parent should be selected.
109 gdb_test "info threads" \
111 "\\\* 1.1 ${not_nl} \\\(running\\\)${not_nl}" \
112 " 2.1 ${not_nl} \\\(running\\\)"]
114 set test "only $follow_fork marked running"
115 gdb_test_multiple "info threads" $test {
116 -re "\\\(running\\\)${not_nl}\\\(running\\\)\r\n$gdb_prompt $" {
119 -re "\\\* 1.1 ${not_nl}\\\(running\\\)\r\n 2.1 ${not_nl}\r\n$gdb_prompt $" {
120 gdb_assert [string eq $follow_fork "parent"] $test
122 -re "\\\* 1.1 ${not_nl}\r\n 2.1 ${not_nl}\\\(running\\\)\r\n$gdb_prompt $" {
123 gdb_assert [string eq $follow_fork "child"] $test
128 # We don't want to see "Inferior exited" in reaction to the kills.
129 gdb_test_no_output "set print inferior-events off"
131 # Kill both parent and child.
132 if {$detach_on_fork == "off" || $follow_fork == "parent"} {
133 gdb_test_no_output "kill inferior 1" "kill parent"
135 if {$detach_on_fork == "off" || $follow_fork == "child"} {
136 gdb_test_no_output "kill inferior 2" "kill child"
140 # Exercise all permutations of:
142 # set detach-on-fork off|on
143 # set follow-fork parent|child
144 # set non-stop on|off
145 # set schedule-multiple on|off
147 foreach_with_prefix detach-on-fork {"off" "on"} {
148 foreach_with_prefix follow-fork {"parent" "child"} {
149 with_test_prefix "non-stop" {
150 do_test ${detach-on-fork} ${follow-fork} "on" "-"
152 with_test_prefix "all-stop" {
153 foreach_with_prefix schedule-multiple {"on" "off"} {
154 do_test ${detach-on-fork} ${follow-fork} "off" ${schedule-multiple}