xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/fork-running-state.exp (revision 8e33eff89e26cf71871ead62f0d5063e1313c33a)
1# Copyright (C) 2016-2023 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# 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.
19
20standard_testfile
21
22# The test proper.
23
24proc do_test { detach_on_fork follow_fork non_stop schedule_multiple } {
25    global GDBFLAGS
26    global srcfile testfile
27    global gdb_prompt
28
29    save_vars { GDBFLAGS } {
30	append GDBFLAGS " -ex \"set non-stop $non_stop\""
31
32	if {[prepare_for_testing "failed to prepare" \
33		 $testfile $srcfile {debug}] == -1} {
34	    return -1
35	}
36    }
37
38    if {![runto_main]} {
39	return 0
40    }
41
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
44    # all-stop tests.
45    if { [target_info exists gdb_protocol]
46	 && ([target_info gdb_protocol] == "remote"
47	     || [target_info gdb_protocol] == "extended-remote")} {
48
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 $" {
52	    }
53	    -re "(is|currently) off.*$gdb_prompt $" {
54		unsupported "can't issue info threads while target is running"
55		return 0
56	    }
57	}
58    }
59
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"
63    }
64
65    gdb_test_no_output "set detach-on-fork $detach_on_fork"
66
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"
70    }
71
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"
77    }
78
79    set test "continue &"
80    gdb_test_multiple $test $test {
81	-re "$gdb_prompt " {
82	    pass $test
83	}
84    }
85
86    if {$detach_on_fork == "off" || $follow_fork == "child"} {
87	set test "fork child appears"
88	gdb_test_multiple "" $test {
89	    -re "\\\[New inferior " {
90		pass $test
91	    }
92	}
93    } else {
94	# All we can do is wait a little bit for the parent to fork.
95	sleep 1
96    }
97
98    set not_nl "\[^\r\n\]*"
99
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" \
110	    [multi_line \
111		 "\\\* 1.1 ${not_nl} \\\(running\\\)${not_nl}" \
112		 "  2.1 ${not_nl} \\\(running\\\)"]
113    } else {
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 $" {
117		fail $test
118	    }
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
121	    }
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
124	    }
125	}
126    }
127
128    # We don't want to see "Inferior exited" in reaction to the kills.
129    gdb_test_no_output "set print inferior-events off"
130
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"
134    }
135    if {$detach_on_fork == "off" || $follow_fork == "child"} {
136	gdb_test_no_output "kill inferior 2" "kill child"
137    }
138}
139
140# Exercise all permutations of:
141#
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
146
147foreach_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" "-"
151	}
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}
155	    }
156	}
157    }
158}
159