1 # Copyright 2017-2020 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 multi-target features.
18 load_lib gdbserver-support.exp
22 # The plain remote target can't do multiple inferiors.
23 if {[target_info gdb_protocol] != ""} {
27 if { [prepare_for_testing "failed to prepare" ${binfile} "${srcfile}" \
32 proc connect_target_extended_remote {binfile} {
33 set res [gdbserver_start "--multi" ""]
34 set gdbserver_gdbport [lindex $res 1]
35 return [gdb_target_cmd "extended-remote" $gdbserver_gdbport]
38 # Add and start inferior number NUM. Returns true on success, false
40 proc add_inferior {num target binfile {gcorefile ""}} {
41 # Start another inferior.
42 gdb_test "add-inferior -no-connection" "Added inferior $num" \
43 "add empty inferior $num"
44 gdb_test "inferior $num" "Switching to inferior $num.*" \
45 "switch to inferior $num"
46 gdb_test "file ${binfile}" ".*" "load file in inferior $num"
47 gdb_test_no_output "set remote exec-file ${binfile}" \
48 "set remote-exec file in inferior $num"
50 if {$target == "core"} {
51 gdb_test "core $gcorefile" "Core was generated by.*" \
52 "core [file tail $gcorefile], inf $num"
56 if {$target == "extended-remote"} {
57 if {[connect_target_extended_remote $binfile]} {
61 if ![runto "all_started"] then {
69 proc prepare_core {} {
70 global gcorefile gcore_created
73 clean_restart ${binfile}
75 if ![runto all_started] then {
80 set gcorefile [standard_output_file $testfile.gcore]
81 set gcore_created [gdb_gcore_cmd $gcorefile "save a core file"]
84 proc next_live_inferior {inf} {
98 # Return true on success, false otherwise.
100 proc setup {non-stop} {
101 global gcorefile gcore_created
104 clean_restart ${binfile}
106 # multi-target depends on target running in non-stop mode. Force
107 # it on for remote targets, until this is the default.
108 gdb_test_no_output "maint set target-non-stop on"
110 gdb_test_no_output "set non-stop ${non-stop}"
112 if ![runto all_started] then {
118 # inferior 1 -> native
119 # inferior 2 -> extended-remote
121 # inferior 4 -> native
122 # inferior 5 -> extended-remote
124 if {![add_inferior 2 "extended-remote" $binfile]} {
127 if {![add_inferior 3 "core" $binfile $gcorefile]} {
130 if {![add_inferior 4 "native" $binfile]} {
133 if {![add_inferior 5 "extended-remote" $binfile]} {
136 if {![add_inferior 6 "core" $binfile $gcorefile]} {
141 gdb_test "info inferiors" ".*"
142 gdb_test "info threads" ".*"
144 # Make "continue" resume all inferiors.
145 if {${non-stop} == "off"} {
146 gdb_test_no_output "set schedule-multiple on"
152 # Test "continue" to breakpoints in different targets. In non-stop
153 # mode, also tests "interrupt -a".
154 proc test_continue {non-stop} {
155 if {![setup ${non-stop}]} {
156 untested "setup failed"
160 proc set_break {inf} {
161 gdb_test "break function${inf} thread ${inf}.1" \
162 "Breakpoint .* function${inf}\\..*"
165 # Select inferior INF, and then run to a breakpoint on inferior
167 proc test_continue_inf {inf} {
168 upvar 1 non-stop non-stop
173 set next_inf [next_live_inferior $inf]
175 gdb_test "inferior $inf" "Switching to inferior $inf.*"
178 if {${non-stop} == "off"} {
179 gdb_test "continue" "hit Breakpoint .* function${next_inf}.*"
182 gdb_test_multiple "continue -a&" $msg {
183 -re "Continuing.*$gdb_prompt " {
189 gdb_test_multiple "" $msg {
190 -re "hit Breakpoint .* function${next_inf}" {
195 set msg "stop all threads"
196 gdb_test_multiple "interrupt -a" $msg {
198 for {set i 0} {$i < 7} {incr i} {
200 gdb_test_multiple "" $msg {
201 -re "Thread\[^\r\n\]*stopped\\." {
215 for {set i 1} {$i <= 5} {incr i} {
217 # This is a core inferior.
221 with_test_prefix "inf$i" {
227 # Test interrupting multiple targets with Ctrl-C.
230 if {![setup "off"]} {
231 untested "setup failed"
237 # Select inferior INF, continue all inferiors, and then Ctrl-C.
238 proc test_ctrlc_inf {inf} {
241 gdb_test "inferior $inf" "Switching to inferior $inf.*"
244 gdb_test_multiple "continue" $msg {
250 after 200 { send_gdb "\003" }
252 set msg "send_gdb control C"
253 gdb_test_multiple "" $msg {
254 -re "received signal SIGINT.*$gdb_prompt $" {
259 set msg "all threads stopped"
260 gdb_test_multiple "info threads" "$msg" {
261 -re "\\\(running\\\).*$gdb_prompt $" {
264 -re "$gdb_prompt $" {
270 for {set i 1} {$i <= 5} {incr i} {
272 # This is a core inferior.
276 with_test_prefix "inf$i" {
282 # Test "next" bouncing between two breakpoints in two threads running
283 # in different targets.
284 proc test_ping_pong_next {} {
287 if {![setup "off"]} {
288 untested "setup failed"
292 # block/unblock inferiors 1 and 2 according to INF1 and INF2.
293 proc block {inf1 inf2} {
294 gdb_test "thread apply 1.1 p wait_for_gdb = $inf1" " = $inf1"
295 gdb_test "thread apply 2.1 p wait_for_gdb = $inf2" " = $inf2"
298 # We're use inferiors 1 and 2. Make sure they're really connected
299 # to different targets.
300 gdb_test "thread apply 1.1 maint print target-stack" \
302 gdb_test "thread apply 2.1 maint print target-stack" \
303 "- extended-remote.*"
305 # Set two breakpoints, one for each of inferior 1 and 2. Inferior
306 # 1 is running on the native target, and inferior 2 is running on
307 # extended-gdbserver. Run to breakpoint 1 to gets things started.
308 set line1 [gdb_get_line_number "set break 1 here"]
309 set line2 [gdb_get_line_number "set break 2 here"]
311 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
313 gdb_test "break $srcfile:$line1 thread 1.1" \
314 "Breakpoint .*$srcfile:$line1\\..*"
316 gdb_test "continue" "hit Breakpoint .*"
318 gdb_test "break $srcfile:$line2 thread 2.1" \
319 "Breakpoint .*$srcfile:$line2\\..*"
321 # Now block inferior 1 and issue "next". We should stop at the
322 # breakpoint for inferior 2, given schedlock off.
323 with_test_prefix "next inf 1" {
325 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
328 # Now unblock inferior 2 and block inferior 1. "next" should run
329 # into the breakpoint in inferior 1.
330 with_test_prefix "next inf 2" {
332 gdb_test "next" "Thread 1.1 .*hit Breakpoint .*$srcfile:$line1.*"
335 # Try nexting inferior 1 again.
336 with_test_prefix "next inf 1 again" {
338 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
342 # Make a core file with two threads upfront. Several tests load the
346 # Some basic "continue" + breakpoints tests.
347 with_test_prefix "continue" {
348 foreach_with_prefix non-stop {"off" "on"} {
349 test_continue ${non-stop}
353 # Some basic all-stop Ctrl-C tests.
354 with_test_prefix "interrupt" {
358 # Test ping-ponging between two targets with "next".
359 with_test_prefix "ping-pong" {