1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2020-2021 Free Software Foundation, Inc.
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18# Test receiving TARGET_WAITKIND_SIGNALLED events from multiple
19# inferiors.  In all stop-mode, upon receiving the exit event from one
20# of the inferiors, GDB will try to stop the other inferior, too.  So,
21# a stop request will be sent.  Receiving a TARGET_WAITKIND_SIGNALLED
22# status kind as a response to that stop request instead of a
23# TARGET_WAITKIND_STOPPED should be handled by GDB without problems.
24
25standard_testfile
26
27if {[use_gdb_stub]} {
28    return 0
29}
30
31if {[build_executable "failed to prepare" $testfile $srcfile {debug}]} {
32    return -1
33}
34
35# We are testing GDB's ability to stop all threads.
36# Hence, go with the all-stop-on-top-of-non-stop mode.
37save_vars { GDBFLAGS } {
38    append GDBFLAGS " -ex \"maint set target-non-stop on\""
39    clean_restart ${binfile}
40}
41
42# Wrap the entire test in a namespace to avoid contaminating other tests.
43namespace eval $testfile {
44
45# Start inferior NUM and record its PID in the TESTPID array.
46
47proc start_inferior {num} {
48    with_test_prefix "start_inferior $num" {
49	variable testpid
50	global binfile srcfile
51
52	if {$num != 1} {
53	    gdb_test "add-inferior" "Added inferior .*" \
54		"add empty inferior"
55	    gdb_test "inferior $num" "Switching to inferior .*" \
56		"switch to inferior"
57	}
58
59	gdb_load $binfile
60
61	gdb_breakpoint "initialized" {temporary}
62	gdb_run_cmd
63	gdb_test "" ".*reakpoint .*, initialized .*${srcfile}.*" "run"
64
65	set testpid($num) [get_integer_valueof "pid" -1]
66	if {$testpid($num) == -1} {
67	    return -1
68	}
69
70	return 0
71    }
72}
73
74# Sufficient inferiors to make sure that at least some other inferior
75# is killed while we're handling a killed event.
76set NUM_INFS 10
77
78# The array holding each inferior's PID, indexed by inferior number.
79variable testpid
80array set testpid {}
81
82for {set i 1} {$i <= $NUM_INFS} {incr i} {
83    if {[start_inferior $i] < 0} {
84	return -1
85    }
86}
87
88# We want to continue all processes.
89gdb_test_no_output "set schedule-multiple on"
90
91# Resume, but then kill all from outside.
92gdb_test_multiple "continue" "continue processes" {
93    -re "Continuing.\[\r\n\]+" {
94	# Kill all processes at once.
95
96	set kill_cmd "kill -9"
97	for {set i 1} {$i <= $NUM_INFS} {incr i} {
98	    append kill_cmd " $testpid($i)"
99	}
100
101	remote_exec target $kill_cmd
102	exp_continue
103    }
104    -re "Program terminated with signal.*$gdb_prompt $" {
105	pass $gdb_test_name
106    }
107}
108
109# Check that "continue" collects the process kill event, as many times
110# as we have inferiors left.
111
112for {set i 2} {$i <= $NUM_INFS} {incr i} {
113    with_test_prefix "inf $i" {
114	set live_inferior ""
115
116	# Pick any live inferior.
117	gdb_test_multiple "info inferiors" "" {
118	    -re "($decimal) *process.*$gdb_prompt $" {
119		set live_inferior $expect_out(1,string)
120	    }
121	}
122
123	if {$live_inferior == ""} {
124	    return -1
125	}
126
127	gdb_test "inferior $live_inferior" \
128	    ".*Switching to inferior $live_inferior.*" \
129	    "switch to inferior"
130
131	gdb_test "continue" \
132	    "Program terminated with signal SIGKILL, .*" \
133	    "continue to SIGKILL"
134    }
135}
136
137}
138