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
20 if { [skip_gdbserver_tests] } {
26 # The plain remote target can't do multiple inferiors.
27 if {[target_info gdb_protocol] != ""} {
31 if { [prepare_for_testing "failed to prepare" ${binfile} "${srcfile}" \
36 # Keep a list of (inferior ID, spawn ID).
37 set server_spawn_ids [list]
39 proc connect_target_extended_remote {binfile num} {
40 set res [gdbserver_start "--multi" ""]
41 global server_spawn_ids server_spawn_id
42 lappend server_spawn_ids $num $server_spawn_id
43 set gdbserver_gdbport [lindex $res 1]
44 return [gdb_target_cmd "extended-remote" $gdbserver_gdbport]
47 # Add and start inferior number NUM. Returns true on success, false
49 proc add_inferior {num target binfile {gcorefile ""}} {
50 # Start another inferior.
51 gdb_test "add-inferior -no-connection" "Added inferior $num" \
52 "add empty inferior $num"
53 gdb_test "inferior $num" "Switching to inferior $num.*" \
54 "switch to inferior $num"
55 gdb_test "file ${binfile}" ".*" "load file in inferior $num"
56 gdb_test_no_output "set remote exec-file ${binfile}" \
57 "set remote-exec file in inferior $num"
59 if {$target == "core"} {
60 gdb_test "core $gcorefile" "Core was generated by.*" \
61 "core [file tail $gcorefile], inf $num"
65 if {$target == "extended-remote"} {
66 if {[connect_target_extended_remote $binfile $num]} {
70 if ![runto "all_started"] then {
78 proc prepare_core {} {
79 global gcorefile gcore_created
82 clean_restart ${binfile}
84 if ![runto all_started] then {
89 set gcorefile [standard_output_file $testfile.gcore]
90 set gcore_created [gdb_gcore_cmd $gcorefile "save a core file"]
93 proc next_live_inferior {inf} {
107 # Clean up the server_spawn_ids.
108 proc cleanup_gdbservers { } {
109 global server_spawn_id
110 global server_spawn_ids
111 foreach { inferior_id spawn_id } $server_spawn_ids {
112 set server_spawn_id $spawn_id
113 gdb_test "inferior $inferior_id"
116 set server_spawn_ids [list]
119 # Return true on success, false otherwise.
121 proc setup {non-stop} {
122 global gcorefile gcore_created
126 clean_restart ${binfile}
128 # multi-target depends on target running in non-stop mode. Force
129 # it on for remote targets, until this is the default.
130 gdb_test_no_output "maint set target-non-stop on"
132 gdb_test_no_output "set non-stop ${non-stop}"
134 if ![runto all_started] then {
140 # inferior 1 -> native
141 # inferior 2 -> extended-remote
143 # inferior 4 -> native
144 # inferior 5 -> extended-remote
146 if {![add_inferior 2 "extended-remote" $binfile]} {
149 if {![add_inferior 3 "core" $binfile $gcorefile]} {
152 if {![add_inferior 4 "native" $binfile]} {
155 if {![add_inferior 5 "extended-remote" $binfile]} {
158 if {![add_inferior 6 "core" $binfile $gcorefile]} {
163 gdb_test "info threads" ".*"
165 # Make "continue" resume all inferiors.
166 if {${non-stop} == "off"} {
167 gdb_test_no_output "set schedule-multiple on"
173 # Test "continue" to breakpoints in different targets. In non-stop
174 # mode, also tests "interrupt -a".
175 proc test_continue {non-stop} {
176 if {![setup ${non-stop}]} {
177 untested "setup failed"
181 proc set_break {inf} {
182 gdb_test "break function${inf} thread ${inf}.1" \
183 "Breakpoint .* function${inf}\\..*"
186 # Select inferior INF, and then run to a breakpoint on inferior
188 proc test_continue_inf {inf} {
189 upvar 1 non-stop non-stop
194 set next_inf [next_live_inferior $inf]
196 gdb_test "inferior $inf" "Switching to inferior $inf.*"
199 if {${non-stop} == "off"} {
200 gdb_test "continue" "hit Breakpoint .* function${next_inf}.*"
203 gdb_test_multiple "continue -a&" $msg {
204 -re "Continuing.*$gdb_prompt " {
210 gdb_test_multiple "" $msg {
211 -re "hit Breakpoint .* function${next_inf}" {
216 set msg "stop all threads"
217 gdb_test_multiple "interrupt -a" $msg {
219 for {set i 0} {$i < 7} {incr i} {
221 gdb_test_multiple "" $msg {
222 -re "Thread\[^\r\n\]*stopped\\." {
236 for {set i 1} {$i <= 5} {incr i} {
238 # This is a core inferior.
242 with_test_prefix "inf$i" {
248 # Test interrupting multiple targets with Ctrl-C.
251 if {![setup "off"]} {
252 untested "setup failed"
258 # Select inferior INF, continue all inferiors, and then Ctrl-C.
259 proc test_ctrlc_inf {inf} {
262 gdb_test "inferior $inf" "Switching to inferior $inf.*"
265 gdb_test_multiple "continue" $msg {
271 after 200 { send_gdb "\003" }
273 set msg "send_gdb control C"
274 gdb_test_multiple "" $msg {
275 -re "received signal SIGINT.*$gdb_prompt $" {
280 set msg "all threads stopped"
281 gdb_test_multiple "info threads" "$msg" {
282 -re "\\\(running\\\).*$gdb_prompt $" {
285 -re "$gdb_prompt $" {
291 for {set i 1} {$i <= 5} {incr i} {
293 # This is a core inferior.
297 with_test_prefix "inf$i" {
303 # Test "next" bouncing between two breakpoints in two threads running
304 # in different targets.
305 proc test_ping_pong_next {} {
308 if {![setup "off"]} {
309 untested "setup failed"
313 # block/unblock inferiors 1 and 2 according to INF1 and INF2.
314 proc block {inf1 inf2} {
315 gdb_test "thread apply 1.1 p wait_for_gdb = $inf1" " = $inf1"
316 gdb_test "thread apply 2.1 p wait_for_gdb = $inf2" " = $inf2"
319 # We're use inferiors 1 and 2. Make sure they're really connected
320 # to different targets.
321 gdb_test "thread apply 1.1 maint print target-stack" \
323 gdb_test "thread apply 2.1 maint print target-stack" \
324 "- extended-remote.*"
326 # Set two breakpoints, one for each of inferior 1 and 2. Inferior
327 # 1 is running on the native target, and inferior 2 is running on
328 # extended-gdbserver. Run to breakpoint 1 to gets things started.
329 set line1 [gdb_get_line_number "set break 1 here"]
330 set line2 [gdb_get_line_number "set break 2 here"]
332 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
334 gdb_test "break $srcfile:$line1 thread 1.1" \
335 "Breakpoint .*$srcfile:$line1\\..*"
337 gdb_test "continue" "hit Breakpoint .*"
339 gdb_test "break $srcfile:$line2 thread 2.1" \
340 "Breakpoint .*$srcfile:$line2\\..*"
342 # Now block inferior 1 and issue "next". We should stop at the
343 # breakpoint for inferior 2, given schedlock off.
344 with_test_prefix "next inf 1" {
346 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
349 # Now unblock inferior 2 and block inferior 1. "next" should run
350 # into the breakpoint in inferior 1.
351 with_test_prefix "next inf 2" {
353 gdb_test "next" "Thread 1.1 .*hit Breakpoint .*$srcfile:$line1.*"
356 # Try nexting inferior 1 again.
357 with_test_prefix "next inf 1 again" {
359 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
363 # Test "info inferiors" and "info connections". MULTI_PROCESS
364 # indicates whether the multi-process feature of remote targets is
366 proc test_info_inferiors {multi_process} {
370 "set remote multiprocess-feature-packet $multi_process"
372 # Get the description for inferior INF for when the current
373 # inferior id is CURRENT.
374 proc inf_desc {inf current} {
377 upvar multi_process multi_process
379 if {($multi_process == "off") && ($inf == 2 || $inf == 5)} {
380 set desc "Remote target"
382 set desc "process ${decimal}"
385 set desc "${inf}${ws}${desc}${ws}"
386 if {$inf == $current} {
393 # Get the "Num" column for CONNECTION for when the current
394 # inferior id is CURRENT_INF.
395 proc connection_num {connection current_inf} {
396 switch $current_inf {
397 "4" { set current_connection "1"}
398 "5" { set current_connection "4"}
399 "6" { set current_connection "5"}
400 default { set current_connection $current_inf}
402 if {$connection == $current_connection} {
403 return "\\* $connection"
405 return " $connection"
410 global decimal binfile
412 # Test "info connections" and "info inferior" by switching to each
413 # inferior one by one.
414 for {set inf 1} {$inf <= 6} {incr inf} {
415 with_test_prefix "inferior $inf" {
416 gdb_test "inferior $inf" "Switching to inferior $inf.*"
418 gdb_test "info connections" \
420 "Num${ws}What${ws}Description${ws}" \
421 "[connection_num 1 $inf]${ws}native${ws}Native process${ws}" \
422 "[connection_num 2 $inf]${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
423 "[connection_num 3 $inf]${ws}core${ws}Local core dump file${ws}" \
424 "[connection_num 4 $inf]${ws}extended-remote localhost:$decimal${ws}Extended remote serial target in gdb-specific protocol${ws}" \
425 "[connection_num 5 $inf]${ws}core${ws}Local core dump file${ws}" \
428 gdb_test "info inferiors" \
430 "Num${ws}Description${ws}Connection${ws}Executable${ws}" \
431 "[inf_desc 1 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
432 "[inf_desc 2 $inf]2 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
433 "[inf_desc 3 $inf]3 \\(core\\)${ws}${binfile}${ws}" \
434 "[inf_desc 4 $inf]1 \\(native\\)${ws}${binfile}${ws}" \
435 "[inf_desc 5 $inf]4 \\(extended-remote localhost:$decimal\\)${ws}${binfile}${ws}" \
436 "[inf_desc 6 $inf]5 \\(core\\)${ws}${binfile}${ws}" \
442 # Make a core file with two threads upfront. Several tests load the
446 # Some basic "continue" + breakpoints tests.
447 with_test_prefix "continue" {
448 foreach_with_prefix non-stop {"off" "on"} {
449 test_continue ${non-stop}
453 # Some basic all-stop Ctrl-C tests.
454 with_test_prefix "interrupt" {
458 # Test ping-ponging between two targets with "next".
459 with_test_prefix "ping-pong" {
463 # Test "info inferiors" and "info connections" commands.
464 with_test_prefix "info-inferiors" {
465 foreach_with_prefix multi_process {"on" "off"} {
466 test_info_inferiors $multi_process