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]} {
143 # Test "info connections" and "info inferior"'s "Connection"
144 # column, while at it.
146 gdb_test "info connections" \
148 "Num${ws}What${ws}Description${ws}" \
149 " 1${ws}native${ws}Native process${ws}" \
150 " 2${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
151 " 3${ws}core${ws}Local core dump file${ws}" \
152 " 4${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
153 "\\* 5${ws}core${ws}Local core dump file${ws}" \
156 gdb_test "info inferiors" \
158 "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
159 " 1${ws}process ${decimal}${ws}1 \\(native\\)${ws}${binfile}${ws}" \
160 " 2${ws}process ${decimal}${ws}2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
161 " 3${ws}process ${decimal}${ws}3 \\(core\\)${ws}${binfile}${ws}" \
162 " 4${ws}process ${decimal}${ws}1 \\(native\\)${ws}${binfile}${ws}" \
163 " 5${ws}process ${decimal}${ws}4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
164 "\\* 6${ws}process ${decimal}${ws}5 \\(core\\)${ws}${binfile}${ws}" \
168 gdb_test "info threads" ".*"
170 # Make "continue" resume all inferiors.
171 if {${non-stop} == "off"} {
172 gdb_test_no_output "set schedule-multiple on"
178 # Test "continue" to breakpoints in different targets. In non-stop
179 # mode, also tests "interrupt -a".
180 proc test_continue {non-stop} {
181 if {![setup ${non-stop}]} {
182 untested "setup failed"
186 proc set_break {inf} {
187 gdb_test "break function${inf} thread ${inf}.1" \
188 "Breakpoint .* function${inf}\\..*"
191 # Select inferior INF, and then run to a breakpoint on inferior
193 proc test_continue_inf {inf} {
194 upvar 1 non-stop non-stop
199 set next_inf [next_live_inferior $inf]
201 gdb_test "inferior $inf" "Switching to inferior $inf.*"
204 if {${non-stop} == "off"} {
205 gdb_test "continue" "hit Breakpoint .* function${next_inf}.*"
208 gdb_test_multiple "continue -a&" $msg {
209 -re "Continuing.*$gdb_prompt " {
215 gdb_test_multiple "" $msg {
216 -re "hit Breakpoint .* function${next_inf}" {
221 set msg "stop all threads"
222 gdb_test_multiple "interrupt -a" $msg {
224 for {set i 0} {$i < 7} {incr i} {
226 gdb_test_multiple "" $msg {
227 -re "Thread\[^\r\n\]*stopped\\." {
241 for {set i 1} {$i <= 5} {incr i} {
243 # This is a core inferior.
247 with_test_prefix "inf$i" {
253 # Test interrupting multiple targets with Ctrl-C.
256 if {![setup "off"]} {
257 untested "setup failed"
263 # Select inferior INF, continue all inferiors, and then Ctrl-C.
264 proc test_ctrlc_inf {inf} {
267 gdb_test "inferior $inf" "Switching to inferior $inf.*"
270 gdb_test_multiple "continue" $msg {
276 after 200 { send_gdb "\003" }
278 set msg "send_gdb control C"
279 gdb_test_multiple "" $msg {
280 -re "received signal SIGINT.*$gdb_prompt $" {
285 set msg "all threads stopped"
286 gdb_test_multiple "info threads" "$msg" {
287 -re "\\\(running\\\).*$gdb_prompt $" {
290 -re "$gdb_prompt $" {
296 for {set i 1} {$i <= 5} {incr i} {
298 # This is a core inferior.
302 with_test_prefix "inf$i" {
308 # Test "next" bouncing between two breakpoints in two threads running
309 # in different targets.
310 proc test_ping_pong_next {} {
313 if {![setup "off"]} {
314 untested "setup failed"
318 # block/unblock inferiors 1 and 2 according to INF1 and INF2.
319 proc block {inf1 inf2} {
320 gdb_test "thread apply 1.1 p wait_for_gdb = $inf1" " = $inf1"
321 gdb_test "thread apply 2.1 p wait_for_gdb = $inf2" " = $inf2"
324 # We're use inferiors 1 and 2. Make sure they're really connected
325 # to different targets.
326 gdb_test "thread apply 1.1 maint print target-stack" \
328 gdb_test "thread apply 2.1 maint print target-stack" \
329 "- extended-remote.*"
331 # Set two breakpoints, one for each of inferior 1 and 2. Inferior
332 # 1 is running on the native target, and inferior 2 is running on
333 # extended-gdbserver. Run to breakpoint 1 to gets things started.
334 set line1 [gdb_get_line_number "set break 1 here"]
335 set line2 [gdb_get_line_number "set break 2 here"]
337 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
339 gdb_test "break $srcfile:$line1 thread 1.1" \
340 "Breakpoint .*$srcfile:$line1\\..*"
342 gdb_test "continue" "hit Breakpoint .*"
344 gdb_test "break $srcfile:$line2 thread 2.1" \
345 "Breakpoint .*$srcfile:$line2\\..*"
347 # Now block inferior 1 and issue "next". We should stop at the
348 # breakpoint for inferior 2, given schedlock off.
349 with_test_prefix "next inf 1" {
351 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
354 # Now unblock inferior 2 and block inferior 1. "next" should run
355 # into the breakpoint in inferior 1.
356 with_test_prefix "next inf 2" {
358 gdb_test "next" "Thread 1.1 .*hit Breakpoint .*$srcfile:$line1.*"
361 # Try nexting inferior 1 again.
362 with_test_prefix "next inf 1 again" {
364 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
368 # Make a core file with two threads upfront. Several tests load the
372 # Some basic "continue" + breakpoints tests.
373 with_test_prefix "continue" {
374 foreach_with_prefix non-stop {"off" "on"} {
375 test_continue ${non-stop}
379 # Some basic all-stop Ctrl-C tests.
380 with_test_prefix "interrupt" {
384 # Test ping-ponging between two targets with "next".
385 with_test_prefix "ping-pong" {