1# Copyright 2014-2020 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# Test "set auto-connect-native-target off" and "target native" on
17# native targets.
18
19standard_testfile
20
21if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
22    return -1
23}
24
25# Whether this GDB is configured with a "native" target.
26set have_native 0
27
28set test "help target native"
29gdb_test_multiple $test $test {
30    -re "Undefined target command.*$gdb_prompt $" {
31	set have_native 0
32    }
33    -re "Native process.*$gdb_prompt $" {
34	set have_native 1
35    }
36}
37
38if { !$have_native } {
39    unsupported "no \"target native\" support."
40    return
41}
42
43# Returns the topmost target pushed on the target stack.  TEST is used
44# as test message.
45
46proc get_topmost_target {test} {
47    global gdb_prompt
48
49    set topmost "unknown"
50
51    gdb_test_multiple "maint print target-stack" $test {
52	-re "The current target stack is:\r\n  - (\[^ \]+) .*$gdb_prompt $" {
53	    set topmost $expect_out(1,string)
54	    pass $test
55	}
56    }
57
58    return $topmost
59}
60
61set topmost [get_topmost_target "check whether a target is already connected"]
62
63# Testing against the extended-remote board, for example?
64if { $topmost != "exec" } {
65    unsupported "already connected to target $topmost."
66    return
67}
68
69# Check which target this board connects to.  If testing with a native
70# target board, this should cause the native target to auto connect.
71if ![runto_main] then {
72    fail "can't run to main"
73    return 0
74}
75
76# Returns true if the native target is pushed on the target stack.
77# TEST is used as test message.
78
79proc check_native_target {test} {
80    global gdb_prompt
81
82    gdb_test_multiple "maint print target-stack" $test {
83	-re " native .*$gdb_prompt $" {
84	    pass $test
85	    return 1
86	}
87	-re "$gdb_prompt $" {
88	    pass $test
89	}
90    }
91
92    return 0
93}
94
95# Testing against a remote board, for example?
96if { ![check_native_target "check whether board tests the native target"] } {
97    unsupported "not testing the native target."
98    return
99}
100
101# Kill program.  TEST is used as test message.
102
103proc kill_program {test} {
104    global gdb_prompt
105
106    gdb_test_multiple "kill" $test {
107	-re "Kill the program being debugged\\? .y or n. $" {
108	    send_gdb "y\n"
109	    exp_continue
110	}
111	-re "$gdb_prompt $" {
112	    pass $test
113	}
114    }
115}
116
117# Kill the program.  This should pop the target.  The "start" test
118# below will fail otherwise.
119kill_program "kill"
120
121# Now prevent the native target from auto connecting.
122gdb_test_no_output "set auto-connect-native-target off"
123
124# Commands that rely on the native target auto-connecting should no longer work.
125gdb_test "start" "Don't know how to run.*" "start no longer works"
126
127# Explicitly connect to the native target.
128gdb_test "target native" \
129    "Done.  Use the \"run\" command to start a process.*" \
130    "explicitly connect to the native target"
131
132proc test_native_target_remains_pushed {} {
133    gdb_test "maint print target-stack"  \
134	"The current target stack is:\r\n  .* native .* exec .*" \
135	"native target remains pushed"
136}
137
138# Test a set of "inferior gone" scenarios, making sure the target
139# remains pushed.
140
141with_test_prefix "kill" {
142    gdb_test "start" "main.*"
143
144    kill_program "kill"
145
146    test_native_target_remains_pushed
147}
148
149with_test_prefix "detach" {
150    gdb_test "start" "main.*"
151
152    set test "detach"
153    gdb_test_multiple $test $test {
154	-re "Detach the program being debugged\\? .y or n. $" {
155	    send_gdb "y\n"
156	    exp_continue
157	}
158	-re "$gdb_prompt $" {
159	    pass $test
160	}
161    }
162
163    test_native_target_remains_pushed
164}
165
166with_test_prefix "run to exit" {
167    gdb_test "start" "Temporary breakpoint .* main .*"
168
169    gdb_test "c" "$inferior_exited_re normally.*"
170
171    test_native_target_remains_pushed
172}
173
174# Now test disconnecting.  Commands that rely on the native target
175# auto-connecting should no longer work (again) after this.
176
177with_test_prefix "disconnect" {
178    gdb_test "start" "Temporary breakpoint .* main .*"
179
180    set test "disconnect"
181    gdb_test_multiple $test $test {
182	-re "A program is being debugged already.* .y or n. $" {
183	    send_gdb "y\n"
184	    exp_continue
185	}
186	-re "$gdb_prompt $" {
187	    pass $test
188	}
189    }
190
191    set topmost \
192	[get_topmost_target "check whether the target is no longer connected"]
193
194    set test "no longer connected to a target"
195    if { $topmost == "exec" } {
196	pass $test
197    } else {
198	fail $test
199    }
200
201    gdb_test "start" "Don't know how to run.*" "start no longer works"
202}
203
204# Reenable auto-connecting to the native target.  Plain "start" should
205# start working again.
206gdb_test_no_output "set auto-connect-native-target on"
207
208gdb_test "start" "Temporary breakpoint .* main .*" \
209    "start auto-connects to the native target after reenabling auto-connect"
210