Add multi-target tests
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdbserver-support.exp
1 # Copyright 2000-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 # This file is based on config/gdbserver.exp, which was written by
17 # Michael Snyder (msnyder@redhat.com).
18
19 #
20 # To be addressed or set in your baseboard config file:
21 #
22 # set_board_info gdb_protocol "remote"
23 # Unles you have a gdbserver that uses a different protocol...
24 # After GDB starts you should check global $gdbserver_protocol instead as
25 # the testfile may force a specific different target protocol itself.
26 #
27 # set_board_info gdb_server_prog
28 # This will be the path to the gdbserver program you want to test.
29 # Defaults to "gdbserver".
30 #
31 # set_board_info sockethost
32 # The name of the host computer whose socket is being used.
33 # Defaults to "localhost". Note: old gdbserver requires
34 # that you define this, but libremote/gdbserver does not.
35 #
36 # set_board_info gdb,socketport
37 # Port id to use for socket connection. If not set explicitly,
38 # it will start at "2345" and increment for each use.
39 # After GDB starts you should check global $gdbserver_gdbport for the
40 # real port used. It is not useful if $gdbserver_reconnect_p was not set.
41 #
42
43 #
44 # gdb_target_cmd_ext
45 # Send gdb the "target" command. Returns 0 on success, 1 on failure, 2 on
46 # unsupported.
47 # If specified, then ADDITIONAL_TEXT must match the text that comes after
48 # the connection message in order for the procedure to succeed.
49 #
50 proc gdb_target_cmd_ext { targetname serialport {additional_text ""} } {
51 global gdb_prompt
52
53 set serialport_re [string_to_regexp $serialport]
54 for {set i 1} {$i <= 3} {incr i} {
55 send_gdb "target $targetname $serialport\n"
56 gdb_expect 60 {
57 -re "A program is being debugged already.*ill it.*y or n. $" {
58 send_gdb "y\n"
59 exp_continue
60 }
61 -re "unknown host.*$gdb_prompt" {
62 verbose "Couldn't look up $serialport"
63 }
64 -re "Couldn't establish connection to remote.*$gdb_prompt $" {
65 verbose "Connection failed"
66 }
67 -re "Non-stop mode requested, but remote does not support non-stop.*$gdb_prompt $" {
68 verbose "remote does not support non-stop"
69 return 1
70 }
71 -re "Remote MIPS debugging.*$additional_text.*$gdb_prompt" {
72 verbose "Set target to $targetname"
73 return 0
74 }
75 -re "Remote debugging using .*$serialport_re.*$additional_text.*$gdb_prompt $" {
76 verbose "Set target to $targetname"
77 return 0
78 }
79 -re "Remote debugging using stdio.*$additional_text.*$gdb_prompt $" {
80 verbose "Set target to $targetname"
81 return 0
82 }
83 -re "Remote target $targetname connected to.*$additional_text.*$gdb_prompt $" {
84 verbose "Set target to $targetname"
85 return 0
86 }
87 -re "Connected to.*$additional_text.*$gdb_prompt $" {
88 verbose "Set target to $targetname"
89 return 0
90 }
91 -re "Ending remote.*$gdb_prompt $" { }
92 -re "Connection refused.*$gdb_prompt $" {
93 verbose "Connection refused by remote target. Pausing, and trying again."
94 sleep 30
95 continue
96 }
97 -re "Timeout reading from remote system.*$gdb_prompt $" {
98 verbose "Got timeout error from gdb."
99 }
100 -notransfer -re "Remote debugging using .*\r\n> $" {
101 # We got an unexpected prompt while creating the target.
102 # Leave it there for the test to diagnose.
103 return 1
104 }
105 -re ": Network is unreachable.\r\n.*$gdb_prompt $" {
106 return 2
107 }
108 timeout {
109 send_gdb "\ 3"
110 break
111 }
112 }
113 }
114 return 1
115 }
116
117 # Like gdb_target_cmd_ext, but returns 0 on success, 1 on failure.
118
119 proc gdb_target_cmd { args } {
120 set res [eval gdb_target_cmd_ext $args]
121 return [expr $res == 0 ? 0 : 1]
122 }
123
124 global portnum
125 set portnum "2345"
126
127 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
128
129 proc find_gdbserver { } {
130 global GDB
131 global GDBSERVER
132
133 if [info exists GDBSERVER] {
134 return ${GDBSERVER}
135 }
136
137 if [target_info exists gdb_server_prog] {
138 return [target_info gdb_server_prog]
139 }
140
141 set gdbserver "${GDB}server"
142 if { [file isdirectory $gdbserver] } {
143 append gdbserver "/gdbserver"
144 }
145
146 if { [file executable $gdbserver] } {
147 return $gdbserver
148 }
149
150 return ""
151 }
152
153 # Return non-zero if we should skip gdbserver-specific tests.
154
155 proc skip_gdbserver_tests { } {
156 if { [find_gdbserver] == "" } {
157 return 1
158 }
159
160 # If GDB is lack of XML support, and targets, like arm, have
161 # multiple target descriptions, GDB doesn't know which target
162 # description GDBserver uses, and may fail to parse 'g' packet
163 # after connection.
164 if { [gdb_skip_xml_test]
165 && ([istarget "arm*-*-linux*"]
166 || [istarget "mips*-*-linux*"]
167 || [istarget "powerpc*-*-linux*"]
168 || [istarget "s390*-*-linux*"]
169 || [istarget "x86_64-*-linux*"]
170 || [istarget "i\[34567\]86-*-linux*"]) } {
171 return 1
172 }
173
174 return 0
175 }
176
177 # Download the currently loaded program to the target if necessary.
178 # Return the target system filename.
179 # NOTE: This was named "gdbserver_download", but that collides with the
180 # dejagnu "download" API function when using load_generic_config "gdbserver".
181
182 proc gdbserver_download_current_prog { } {
183 global gdbserver_host_exec
184 global gdbserver_host_mtime
185 global gdbserver_server_exec
186 global last_loaded_file
187
188 if { ![info exists last_loaded_file] } {
189 return ""
190 }
191
192 set host_exec $last_loaded_file
193
194 # If we already downloaded a file to the target, see if we can reuse it.
195 set reuse 0
196 if { [info exists gdbserver_server_exec] } {
197 set reuse 1
198
199 # If the file has changed, we can not.
200 if { $host_exec != $gdbserver_host_exec } {
201 set reuse 0
202 }
203
204 # If the mtime has changed, we can not.
205 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
206 set reuse 0
207 }
208 }
209
210 if { $reuse == 0 } {
211 set gdbserver_host_exec $host_exec
212 set gdbserver_host_mtime [file mtime $host_exec]
213 set gdbserver_server_exec [gdb_remote_download target $host_exec]
214 }
215
216 return $gdbserver_server_exec
217 }
218
219 # Default routine to compute the argument to "target remote".
220
221 proc gdbserver_default_get_remote_address { host port } {
222 # Historically HOST included the trailing ":".
223 # To avoid breaking any board files out there we leave things alone.
224 return "$host$port"
225 }
226
227 # Default routine to compute the "comm" argument for gdbserver.
228
229 proc gdbserver_default_get_comm_port { port } {
230 return "$port"
231 }
232
233 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
234 # The port will be filled in between them automatically.
235 #
236 # Returns the target protocol and socket to connect to.
237
238 proc gdbserver_start { options arguments } {
239 global portnum
240 global GDB_TEST_SOCKETHOST
241
242 # Port id -- either specified in baseboard file, or managed here.
243 if [target_info exists gdb,socketport] {
244 set portnum [target_info gdb,socketport]
245 } else {
246 # Bump the port number to avoid conflicts with hung ports.
247 incr portnum
248 }
249
250 # Extract the local and remote host ids from the target board struct.
251 if { [info exists GDB_TEST_SOCKETHOST] } {
252 # The user is not supposed to provide a port number, just a
253 # hostname/address, therefore we add the trailing ":" here.
254 set debughost "${GDB_TEST_SOCKETHOST}:"
255 # Escape open and close square brackets.
256 set debughost_tmp [string map { [ \\[ ] \\] } $debughost]
257 # We need a "gdbserver" version of the debughost, which will
258 # have the possible connection prefix stripped. This is
259 # because gdbserver currently doesn't recognize the prefixes.
260 regsub -all "^\(tcp:|udp:|tcp4:|udp4:|tcp6:|udp6:\)" $debughost_tmp "" debughost_gdbserver
261 } elseif [target_info exists sockethost] {
262 set debughost [target_info sockethost]
263 set debughost_gdbserver $debughost
264 } else {
265 set debughost "localhost:"
266 set debughost_gdbserver $debughost
267 }
268
269 # Some boards use a different value for the port that is passed to
270 # gdbserver and the port that is passed to the "target remote" command.
271 # One example is the stdio gdbserver support.
272 if [target_info exists gdb,get_remote_address] {
273 set get_remote_address [target_info gdb,get_remote_address]
274 } else {
275 set get_remote_address gdbserver_default_get_remote_address
276 }
277 if [target_info exists gdbserver,get_comm_port] {
278 set get_comm_port [target_info gdbserver,get_comm_port]
279 } else {
280 set get_comm_port gdbserver_default_get_comm_port
281 }
282
283 # Extract the protocol
284 if [target_info exists gdb_protocol] {
285 set protocol [target_info gdb_protocol]
286 } else {
287 set protocol "remote"
288 }
289
290 set gdbserver [find_gdbserver]
291
292 # Loop till we find a free port.
293 while 1 {
294 # Fire off the debug agent.
295 set gdbserver_command "$gdbserver"
296
297 # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be
298 # set to true already during gdbserver_start.
299 global gdbserver_reconnect_p
300 global srcdir
301 global subdir
302 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
303 # GDB client could accidentally connect to a stale server.
304 append gdbserver_command " --once"
305 }
306
307 # Enable debug if set.
308 if [gdbserver_debug_enabled] {
309 global gdbserverdebug
310 set enabled 0
311 foreach entry [split $gdbserverdebug ,] {
312 switch -- $entry {
313 "debug" {
314 append gdbserver_command " --debug"
315 set enabled 1
316 }
317 "remote" {
318 append gdbserver_command " --remote-debug"
319 set enabled 1
320 }
321 }
322 }
323 # Ensure debugfile is only added if something has been enabled
324 if { $enabled } {
325 set debugfile [standard_output_file gdbserver.debug]
326 append gdbserver_command " --debug-file=$debugfile"
327 }
328 }
329
330 if { $options != "" } {
331 append gdbserver_command " $options"
332 }
333 if { $debughost_gdbserver != "" } {
334 append gdbserver_command " $debughost_gdbserver"
335 }
336 if { $portnum != "" } {
337 if { $debughost_gdbserver == "" } {
338 append gdbserver_command " "
339 }
340 append gdbserver_command "[$get_comm_port $portnum]"
341 }
342 if { $arguments != "" } {
343 append gdbserver_command " $arguments"
344 }
345
346 gdbserver_write_cmd_file $gdbserver_command
347
348 global server_spawn_id
349 set server_spawn_id [remote_spawn target $gdbserver_command]
350
351 # GDBserver doesn't do inferior I/O through GDB. But we can
352 # talk to the program using GDBserver's tty instead.
353 global inferior_spawn_id
354 set inferior_spawn_id $server_spawn_id
355
356 # Wait for the server to open its TCP socket, so that GDB can connect.
357 expect {
358 -i $server_spawn_id
359 -timeout 120
360 -notransfer
361 -re "Listening on" { }
362 -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" {
363 verbose -log "Port $portnum is already in use."
364 if ![target_info exists gdb,socketport] {
365 # Bump the port number to avoid the conflict.
366 wait -i $expect_out(spawn_id)
367 incr portnum
368 continue
369 }
370 }
371 -re ".*: cannot resolve name: .*\r\n" {
372 error "gdbserver cannot resolve name."
373 }
374 timeout {
375 error "Timeout waiting for gdbserver response."
376 }
377 }
378 break
379 }
380
381 return [list $protocol [$get_remote_address $debughost $portnum]]
382 }
383
384 # Start a gdbserver process running SERVER_EXEC, and connect GDB
385 # to it. CHILD_ARGS are passed to the inferior.
386 #
387 # Returns the target protocol and socket to connect to.
388
389 proc gdbserver_spawn { child_args } {
390 set target_exec [gdbserver_download_current_prog]
391
392 # Fire off the debug agent. This flavour of gdbserver takes as
393 # arguments the port information, the name of the executable file to
394 # be debugged, and any arguments.
395 set arguments "$target_exec"
396 if { $child_args != "" } {
397 append arguments " $child_args"
398 }
399 return [gdbserver_start "" $arguments]
400 }
401
402 # Close the GDBserver connection.
403
404 proc close_gdbserver {} {
405 global server_spawn_id
406
407 # We can't just call close, because if gdbserver is local then that means
408 # that it will get a SIGHUP. Doing it this way could also allow us to
409 # get at the inferior's input or output if necessary, and means that we
410 # don't need to redirect output.
411
412 if {![info exists server_spawn_id]} {
413 return
414 }
415
416 verbose "Quitting GDBserver"
417
418 catch "close -i $server_spawn_id"
419 catch "wait -i $server_spawn_id"
420 unset server_spawn_id
421 }
422
423 # Hook into GDB exit, and close GDBserver. We must load this
424 # explicitly here, and rename the procedures we want to override.
425 load_lib mi-support.exp
426
427 if { [info procs gdbserver_orig_gdb_exit] == "" } {
428 rename gdb_exit gdbserver_orig_gdb_exit
429 rename mi_gdb_exit gdbserver_orig_mi_gdb_exit
430 }
431
432 proc gdbserver_gdb_exit { is_mi } {
433 global gdb_spawn_id server_spawn_id
434 global gdb_prompt
435 global gdbserver_reconnect_p
436
437 # Leave GDBserver running if we're exiting GDB in order to
438 # reconnect to the same instance of GDBserver again.
439 if {[info exists gdbserver_reconnect_p] && $gdbserver_reconnect_p} {
440 if { $is_mi } {
441 gdbserver_orig_mi_gdb_exit
442 } else {
443 gdbserver_orig_gdb_exit
444 }
445 return
446 }
447
448 if {[info exists gdb_spawn_id] && [info exists server_spawn_id]} {
449 # GDB may be terminated in an expected way or an unexpected way,
450 # but DejaGNU doesn't know that, so gdb_spawn_id isn't unset.
451 # Catch the exceptions.
452 catch {
453 if { $is_mi } {
454 set monitor_exit "-interpreter-exec console \"monitor exit\""
455 } else {
456 set monitor_exit "monitor exit"
457 }
458 send_gdb "$monitor_exit\n";
459 # We use expect rather than gdb_expect because
460 # we want to suppress printing exception messages, otherwise,
461 # remote_expect, invoked by gdb_expect, prints the exceptions.
462 expect {
463 -i "$gdb_spawn_id" -re "$gdb_prompt $" {
464 exp_continue
465 }
466 -i "$server_spawn_id" eof {
467 wait -i $expect_out(spawn_id)
468 unset server_spawn_id
469 }
470 }
471 }
472 }
473 close_gdbserver
474
475 if { $is_mi } {
476 gdbserver_orig_mi_gdb_exit
477 } else {
478 gdbserver_orig_gdb_exit
479 }
480 }
481
482 proc gdb_exit {} {
483 gdbserver_gdb_exit 0
484 }
485
486 proc mi_gdb_exit {} {
487 gdbserver_gdb_exit 1
488 }
489
490 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
491 # to it. Return 0 on success, or non-zero on failure: 2 if gdbserver
492 # failed to start or 1 if we couldn't connect to it.
493
494 proc gdbserver_run { child_args } {
495 global gdbserver_protocol
496 global gdbserver_gdbport
497
498 # Kill anything running before we try to start gdbserver, in case
499 # we are sharing a serial connection.
500 global gdb_prompt
501 send_gdb "kill\n" optional
502 gdb_expect 120 {
503 -re "Kill the program being debugged. .y or n. $" {
504 send_gdb "y\n"
505 verbose "\t\tKilling previous program being debugged"
506 exp_continue
507 }
508 -re "$gdb_prompt $" {
509 # OK.
510 }
511 }
512
513 if { [catch { gdbserver_spawn $child_args } res] == 1 } {
514 perror $res
515 return 2
516 }
517 set gdbserver_protocol [lindex $res 0]
518 set gdbserver_gdbport [lindex $res 1]
519
520 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
521 }
522
523 # Reconnect to the previous gdbserver session.
524
525 proc gdbserver_reconnect { } {
526 global gdbserver_protocol
527 global gdbserver_gdbport
528
529 global gdbserver_reconnect_p
530 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
531 error "gdbserver_reconnect_p is not set before gdbserver_reconnect"
532 return 0
533 }
534
535 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
536 }
537
538 # Start gdbserver in extended mode with OPTIONS and connect to it. Note
539 # this frobs $gdbserver_protocol, so should be used only from a board
540 # that usually connects in target remote mode.
541 proc gdbserver_start_extended { {options ""} } {
542 global gdbserver_protocol
543 global gdbserver_gdbport
544 global use_gdb_stub
545
546 set gdbserver_options "--multi"
547
548 if { $options != "" } {
549 append gdbserver_options " $options"
550 }
551
552 if { [catch { gdbserver_start $gdbserver_options "" } res] == 1 } {
553 perror $res
554 return 2
555 }
556 set gdbserver_protocol [lindex $res 0]
557 if { [string first "extended-" $gdbserver_protocol] != 0} {
558 set gdbserver_protocol "extended-$gdbserver_protocol"
559 }
560 set gdbserver_gdbport [lindex $res 1]
561
562 # Even if the board file is testing with target remote, our caller
563 # wants to test against gdbserver in extended-remote mode. Make sure to
564 # disable stub-like techniques.
565 set use_gdb_stub 0
566
567 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
568 }
569
570 # Start and connect to a gdbserver in extended/multi mode. Unlike
571 # gdbserver_start_extended, this does not frob $gdbserver_protocol.
572
573 proc gdbserver_start_multi { } {
574 global gdbserver_protocol
575 global gdbserver_gdbport
576
577 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
578 perror $res
579 return 2
580 }
581 set gdbserver_protocol [lindex $res 0]
582 set gdbserver_gdbport [lindex $res 1]
583
584 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
585 }
586
587 # Start a gdbserver process in multi/extended mode, and have GDB
588 # connect to it (MI version). Return 0 on success, or non-zero on
589 # failure.
590
591 proc mi_gdbserver_start_multi { } {
592 global gdbserver_protocol
593 global gdbserver_gdbport
594
595 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
596 perror $res
597 return 2
598 }
599 set gdbserver_protocol [lindex $res 0]
600 set gdbserver_gdbport [lindex $res 1]
601
602 return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
603 }
604
605 # Check if debugging is enabled for gdbserver.
606
607 proc gdbserver_debug_enabled { } {
608 global gdbserverdebug
609
610 # If not already read, get the debug setting from environment or board setting.
611 if ![info exists gdbserverdebug] {
612 global env
613 if [info exists env(GDBSERVER_DEBUG)] {
614 set gdbserverdebug $env(GDBSERVER_DEBUG)
615 } elseif [target_info exists gdbserver,debug] {
616 set gdbserverdebug [target_info gdbserver,debug]
617 } else {
618 return 0
619 }
620 }
621
622 # Expand the all option
623 if { $gdbserverdebug == "all" } {
624 set gdbserverdebug "debug,remote,replay"
625 }
626
627 # Ensure it is not empty.
628 return [expr { $gdbserverdebug != "" }]
629 }
630
631 # Write the command line used to invocate gdbserver to the cmd file.
632
633 proc gdbserver_write_cmd_file { cmdline } {
634 set logfile [standard_output_file_with_gdb_instance gdbserver.cmd]
635 set cmd_file [open $logfile w]
636 puts $cmd_file $cmdline
637 catch "close $cmd_file"
638 }
639
640 # Override gdb_debug_init so that we can set replay logging in GDB if required.
641 # Backup the original function so we can call it afterwards
642
643 rename gdb_debug_init _gdb_debug_init
644
645 proc gdb_debug_init { } {
646 global gdbserverdebug
647 global gdb_prompt
648
649 if [gdbserver_debug_enabled] {
650 foreach entry [split $gdbserverdebug ,] {
651 if { $entry == "replay" } {
652 set replayfile [standard_output_file_with_gdb_instance gdbserver.replay]
653 send_gdb "set remotelogfile $replayfile\n" optional
654 gdb_expect 10 {
655 -re "$gdb_prompt $" {}
656 }
657 }
658 }
659 }
660
661 # Now call the standard debug init function
662 _gdb_debug_init
663 }
This page took 0.054024 seconds and 4 git commands to generate.