Switch the inferior before outputting its id in "info inferiors"
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.multi / multi-target.exp
1 # Copyright 2017-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 multi-target features.
17
18 load_lib gdbserver-support.exp
19
20 standard_testfile
21
22 # The plain remote target can't do multiple inferiors.
23 if {[target_info gdb_protocol] != ""} {
24 return
25 }
26
27 if { [prepare_for_testing "failed to prepare" ${binfile} "${srcfile}" \
28 {debug pthreads}] } {
29 return
30 }
31
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]
36 }
37
38 # Add and start inferior number NUM. Returns true on success, false
39 # otherwise.
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"
49
50 if {$target == "core"} {
51 gdb_test "core $gcorefile" "Core was generated by.*" \
52 "core [file tail $gcorefile], inf $num"
53 return 1
54 }
55
56 if {$target == "extended-remote"} {
57 if {[connect_target_extended_remote $binfile]} {
58 return 0
59 }
60 }
61 if ![runto "all_started"] then {
62 return 0
63 }
64 delete_breakpoints
65
66 return 1
67 }
68
69 proc prepare_core {} {
70 global gcorefile gcore_created
71 global binfile
72
73 clean_restart ${binfile}
74
75 if ![runto all_started] then {
76 return -1
77 }
78
79 global testfile
80 set gcorefile [standard_output_file $testfile.gcore]
81 set gcore_created [gdb_gcore_cmd $gcorefile "save a core file"]
82 }
83
84 proc next_live_inferior {inf} {
85 incr inf
86 if {$inf == 3} {
87 # 3 is a core.
88 return 4
89 }
90 if {$inf > 5} {
91 # 6 is a core.
92 return 1
93 }
94
95 return $inf
96 }
97
98 # Return true on success, false otherwise.
99
100 proc setup {non-stop} {
101 global gcorefile gcore_created
102 global binfile
103
104 clean_restart ${binfile}
105
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"
109
110 gdb_test_no_output "set non-stop ${non-stop}"
111
112 if ![runto all_started] then {
113 return 0
114 }
115
116 delete_breakpoints
117
118 # inferior 1 -> native
119 # inferior 2 -> extended-remote
120 # inferior 3 -> core
121 # inferior 4 -> native
122 # inferior 5 -> extended-remote
123 # inferior 6 -> core
124 if {![add_inferior 2 "extended-remote" $binfile]} {
125 return 0
126 }
127 if {![add_inferior 3 "core" $binfile $gcorefile]} {
128 return 0
129 }
130 if {![add_inferior 4 "native" $binfile]} {
131 return 0
132 }
133 if {![add_inferior 5 "extended-remote" $binfile]} {
134 return 0
135 }
136 if {![add_inferior 6 "core" $binfile $gcorefile]} {
137 return 0
138 }
139
140 # For debugging.
141 gdb_test "info threads" ".*"
142
143 # Make "continue" resume all inferiors.
144 if {${non-stop} == "off"} {
145 gdb_test_no_output "set schedule-multiple on"
146 }
147
148 return 1
149 }
150
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"
156 return
157 }
158
159 proc set_break {inf} {
160 gdb_test "break function${inf} thread ${inf}.1" \
161 "Breakpoint .* function${inf}\\..*"
162 }
163
164 # Select inferior INF, and then run to a breakpoint on inferior
165 # INF+1.
166 proc test_continue_inf {inf} {
167 upvar 1 non-stop non-stop
168
169 global gdb_prompt
170 delete_breakpoints
171
172 set next_inf [next_live_inferior $inf]
173
174 gdb_test "inferior $inf" "Switching to inferior $inf.*"
175 set_break $next_inf
176
177 if {${non-stop} == "off"} {
178 gdb_test "continue" "hit Breakpoint .* function${next_inf}.*"
179 } else {
180 set msg "continue"
181 gdb_test_multiple "continue -a&" $msg {
182 -re "Continuing.*$gdb_prompt " {
183 pass $msg
184 }
185 }
186
187 set msg "hit bp"
188 gdb_test_multiple "" $msg {
189 -re "hit Breakpoint .* function${next_inf}" {
190 pass $msg
191 }
192 }
193
194 set msg "stop all threads"
195 gdb_test_multiple "interrupt -a" $msg {
196 -re "$gdb_prompt " {
197 for {set i 0} {$i < 7} {incr i} {
198 set ok 0
199 gdb_test_multiple "" $msg {
200 -re "Thread\[^\r\n\]*stopped\\." {
201 set ok 1
202 }
203 }
204 if {!$ok} {
205 break
206 }
207 }
208 gdb_assert $ok $msg
209 }
210 }
211 }
212 }
213
214 for {set i 1} {$i <= 5} {incr i} {
215 if {$i == 3} {
216 # This is a core inferior.
217 continue
218 }
219
220 with_test_prefix "inf$i" {
221 test_continue_inf $i
222 }
223 }
224 }
225
226 # Test interrupting multiple targets with Ctrl-C.
227
228 proc test_ctrlc {} {
229 if {![setup "off"]} {
230 untested "setup failed"
231 return
232 }
233
234 delete_breakpoints
235
236 # Select inferior INF, continue all inferiors, and then Ctrl-C.
237 proc test_ctrlc_inf {inf} {
238 global gdb_prompt
239
240 gdb_test "inferior $inf" "Switching to inferior $inf.*"
241
242 set msg "continue"
243 gdb_test_multiple "continue" $msg {
244 -re "Continuing" {
245 pass $msg
246 }
247 }
248
249 after 200 { send_gdb "\003" }
250
251 set msg "send_gdb control C"
252 gdb_test_multiple "" $msg {
253 -re "received signal SIGINT.*$gdb_prompt $" {
254 pass $msg
255 }
256 }
257
258 set msg "all threads stopped"
259 gdb_test_multiple "info threads" "$msg" {
260 -re "\\\(running\\\).*$gdb_prompt $" {
261 fail $msg
262 }
263 -re "$gdb_prompt $" {
264 pass $msg
265 }
266 }
267 }
268
269 for {set i 1} {$i <= 5} {incr i} {
270 if {$i == 3} {
271 # This is a core inferior.
272 continue
273 }
274
275 with_test_prefix "inf$i" {
276 test_ctrlc_inf $i
277 }
278 }
279 }
280
281 # Test "next" bouncing between two breakpoints in two threads running
282 # in different targets.
283 proc test_ping_pong_next {} {
284 global srcfile
285
286 if {![setup "off"]} {
287 untested "setup failed"
288 return
289 }
290
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"
295 }
296
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" \
300 "- native.*"
301 gdb_test "thread apply 2.1 maint print target-stack" \
302 "- extended-remote.*"
303
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"]
309
310 gdb_test "thread 1.1" "Switching to thread 1.1 .*"
311
312 gdb_test "break $srcfile:$line1 thread 1.1" \
313 "Breakpoint .*$srcfile:$line1\\..*"
314
315 gdb_test "continue" "hit Breakpoint .*"
316
317 gdb_test "break $srcfile:$line2 thread 2.1" \
318 "Breakpoint .*$srcfile:$line2\\..*"
319
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" {
323 block 1 0
324 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
325 }
326
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" {
330 block 0 1
331 gdb_test "next" "Thread 1.1 .*hit Breakpoint .*$srcfile:$line1.*"
332 }
333
334 # Try nexting inferior 1 again.
335 with_test_prefix "next inf 1 again" {
336 block 1 0
337 gdb_test "next" "Thread 2.1 .*hit Breakpoint .*$srcfile:$line2.*"
338 }
339 }
340
341 # Test "info inferiors" and "info connections". MULTI_PROCESS
342 # indicates whether the multi-process feature of remote targets is
343 # turned off or on.
344 proc test_info_inferiors {multi_process} {
345 setup "off"
346
347 gdb_test_no_output \
348 "set remote multiprocess-feature-packet $multi_process"
349
350 # Get the description for inferior INF for when the current
351 # inferior id is CURRENT.
352 proc inf_desc {inf current} {
353 set ws "\[ \t\]+"
354 global decimal
355 upvar multi_process multi_process
356
357 if {($multi_process == "off") && ($inf == 2 || $inf == 5)} {
358 set desc "Remote target"
359 } else {
360 set desc "process ${decimal}"
361 }
362
363 set desc "${inf}${ws}${desc}${ws}"
364 if {$inf == $current} {
365 return "\\* $desc"
366 } else {
367 return " $desc"
368 }
369 }
370
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}
379 }
380 if {$connection == $current_connection} {
381 return "\\* $connection"
382 } else {
383 return " $connection"
384 }
385 }
386
387 set ws "\[ \t\]+"
388 global decimal binfile
389
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.*"
395
396 gdb_test "info connections" \
397 [multi_line \
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}" \
404 ]
405
406 gdb_test "info inferiors" \
407 [multi_line \
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}" \
415 ]
416 }
417 }
418 }
419
420 # Make a core file with two threads upfront. Several tests load the
421 # same core file.
422 prepare_core
423
424 # Some basic "continue" + breakpoints tests.
425 with_test_prefix "continue" {
426 foreach_with_prefix non-stop {"off" "on"} {
427 test_continue ${non-stop}
428 }
429 }
430
431 # Some basic all-stop Ctrl-C tests.
432 with_test_prefix "interrupt" {
433 test_ctrlc
434 }
435
436 # Test ping-ponging between two targets with "next".
437 with_test_prefix "ping-pong" {
438 test_ping_pong_next
439 }
440
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
445 }
446 }
This page took 0.039413 seconds and 4 git commands to generate.