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 threads" ".*"
143 # Make "continue" resume all inferiors.
144 if {${non-stop} == "off"} {
145 gdb_test_no_output "set schedule-multiple on"
151 # Test "continue" to breakpoints in different targets. In non-stop
152 # mode, also tests "interrupt -a".
153 proc test_continue {non-stop} {
154 if {![setup ${non-stop}]} {
155 untested "setup failed"
159 proc set_break {inf} {
160 gdb_test "break function${inf} thread ${inf}.1" \
161 "Breakpoint .* function${inf}\\..*"
164 # Select inferior INF, and then run to a breakpoint on inferior
166 proc test_continue_inf {inf} {
167 upvar 1 non-stop non-stop
172 set next_inf [next_live_inferior $inf]
174 gdb_test "inferior $inf" "Switching to inferior $inf.*"
177 if {${non-stop} == "off"} {
178 gdb_test "continue" "hit Breakpoint .* function${next_inf}.*"
181 gdb_test_multiple "continue -a&" $msg {
182 -re "Continuing.*$gdb_prompt " {
188 gdb_test_multiple "" $msg {
189 -re "hit Breakpoint .* function${next_inf}" {
194 set msg "stop all threads"
195 gdb_test_multiple "interrupt -a" $msg {
197 for {set i 0} {$i < 7} {incr i} {
199 gdb_test_multiple "" $msg {
200 -re "Thread\[^\r\n\]*stopped\\." {
214 for {set i 1} {$i <= 5} {incr i} {
216 # This is a core inferior.
220 with_test_prefix "inf$i" {
226 # Test interrupting multiple targets with Ctrl-C.
229 if {![setup "off"]} {
230 untested "setup failed"
236 # Select inferior INF, continue all inferiors, and then Ctrl-C.
237 proc test_ctrlc_inf {inf} {
240 gdb_test "inferior $inf" "Switching to inferior $inf.*"
243 gdb_test_multiple "continue" $msg {
249 after 200 { send_gdb "\003" }
251 set msg "send_gdb control C"
252 gdb_test_multiple "" $msg {
253 -re "received signal SIGINT.*$gdb_prompt $" {
258 set msg "all threads stopped"
259 gdb_test_multiple "info threads" "$msg" {
260 -re "\\\(running\\\).*$gdb_prompt $" {
263 -re "$gdb_prompt $" {
269 for {set i 1} {$i <= 5} {incr i} {
271 # This is a core inferior.
275 with_test_prefix "inf$i" {
281 # Test "next" bouncing between two breakpoints in two threads running
282 # in different targets.
283 proc test_ping_pong_next {} {
286 if {![setup "off"]} {
287 untested "setup failed"
291 # block/unblock inferiors 1 and 2 according to INF1 and INF2.
292 proc block {inf1 inf2} {
293 gdb_test "thread apply 1.1 p wait_for_gdb = $inf1" " = $inf1"
294 gdb_test "thread apply 2.1 p wait_for_gdb = $inf2" " = $inf2"
297 # We're use inferiors 1 and 2. Make sure they're really connected
298 # to different targets.
299 gdb_test "thread apply 1.1 maint print target-stack" \
301 gdb_test "thread apply 2.1 maint print target-stack" \
302 "- extended-remote.*"
304 # Set two breakpoints, one for each of inferior 1 and 2. Inferior
305 # 1 is running on the native target, and inferior 2 is running on
306 # extended-gdbserver. Run to breakpoint 1 to gets things started.
307 set line1 [gdb_get_line_number "set break 1 here"]
308 set line2 [gdb_get_line_number "set break 2 here"]
310 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
312 gdb_test "break $srcfile:$line1 thread 1.1" \
313 "Breakpoint .*$srcfile:$line1\\..*"
315 gdb_test "continue" "hit Breakpoint .*"
317 gdb_test "break $srcfile:$line2 thread 2.1" \
318 "Breakpoint .*$srcfile:$line2\\..*"
320 # Now block inferior 1 and issue "next". We should stop at the
321 # breakpoint for inferior 2, given schedlock off.
322 with_test_prefix "next inf 1" {
324 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
327 # Now unblock inferior 2 and block inferior 1. "next" should run
328 # into the breakpoint in inferior 1.
329 with_test_prefix "next inf 2" {
331 gdb_test "next" "Thread 1.1 .*hit Breakpoint .*$srcfile:$line1.*"
334 # Try nexting inferior 1 again.
335 with_test_prefix "next inf 1 again" {
337 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
341 # Test "info inferiors" and "info connections". MULTI_PROCESS
342 # indicates whether the multi-process feature of remote targets is
344 proc test_info_inferiors {multi_process} {
348 "set remote multiprocess-feature-packet $multi_process"
350 # Get the description for inferior INF for when the current
351 # inferior id is CURRENT.
352 proc inf_desc {inf current} {
355 upvar multi_process multi_process
357 if {($multi_process == "off") && ($inf == 2 || $inf == 5)} {
358 set desc "Remote target"
360 set desc "process ${decimal}"
363 set desc "${inf}${ws}${desc}${ws}"
364 if {$inf == $current} {
371 # Get the "Num" column for CONNECTION for when the current
372 # inferior id is CURRENT_INF.
373 proc connection_num {connection current_inf} {
374 switch $current_inf {
375 "4" { set current_connection "1"}
376 "5" { set current_connection "4"}
377 "6" { set current_connection "5"}
378 default { set current_connection $current_inf}
380 if {$connection == $current_connection} {
381 return "\\* $connection"
383 return " $connection"
388 global decimal binfile
390 # Test "info connections" and "info inferior" by switching to each
391 # inferior one by one.
392 for {set inf 1} {$inf <= 6} {incr inf} {
393 with_test_prefix "inferior $inf" {
394 gdb_test "inferior $inf" "Switching to inferior $inf.*"
396 gdb_test "info connections" \
398 "Num${ws}What${ws}Description${ws}" \
399 "[connection_num 1 $inf]${ws}native${ws}Native process${ws}" \
400 "[connection_num 2 $inf]${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
401 "[connection_num 3 $inf]${ws}core${ws}Local core dump file${ws}" \
402 "[connection_num 4 $inf]${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
403 "[connection_num 5 $inf]${ws}core${ws}Local core dump file${ws}" \
406 gdb_test "info inferiors" \
408 "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
409 "[inf_desc 1 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
410 "[inf_desc 2 $inf]2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
411 "[inf_desc 3 $inf]3 \\(core\\)${ws}${binfile}${ws}" \
412 "[inf_desc 4 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
413 "[inf_desc 5 $inf]4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
414 "[inf_desc 6 $inf]5 \\(core\\)${ws}${binfile}${ws}" \
420 # Make a core file with two threads upfront. Several tests load the
424 # Some basic "continue" + breakpoints tests.
425 with_test_prefix "continue" {
426 foreach_with_prefix non-stop {"off" "on"} {
427 test_continue ${non-stop}
431 # Some basic all-stop Ctrl-C tests.
432 with_test_prefix "interrupt" {
436 # Test ping-ponging between two targets with "next".
437 with_test_prefix "ping-pong" {
441 # Test "info inferiors" and "info connections" commands.
442 with_test_prefix "info-inferiors" {
443 foreach_with_prefix multi_process {"on" "off"} {
444 test_info_inferiors $multi_process