1 # Copyright 2000-2021 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 # This file is based on config/gdbserver.exp, which was written by
17 # Michael Snyder (msnyder@redhat.com).
20 # To be addressed or set in your baseboard config file:
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.
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".
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.
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.
45 # Send gdb the "target" command. Returns 0 on success, 1 on failure, 2 on
47 # If specified, then ADDITIONAL_TEXT must match the text that comes after
48 # the connection message in order for the procedure to succeed.
50 proc gdb_target_cmd_ext { targetname serialport {additional_text ""} } {
53 set serialport_re [string_to_regexp $serialport]
54 for {set i 1} {$i <= 3} {incr i} {
55 send_gdb "target $targetname $serialport\n"
57 -re "A program is being debugged already.*ill it.*y or n. $" {
61 -re "unknown host.*$gdb_prompt" {
62 verbose "Couldn't look up $serialport"
64 -re "Couldn't establish connection to remote.*$gdb_prompt $" {
65 verbose "Connection failed"
67 -re "Cannot assign requested address.*$gdb_prompt $" {
68 verbose "Could not assign requested address"
70 -re "Non-stop mode requested, but remote does not support non-stop.*$gdb_prompt $" {
71 verbose "remote does not support non-stop"
74 -re "Remote MIPS debugging.*$additional_text.*$gdb_prompt" {
75 verbose "Set target to $targetname"
78 -re "Remote debugging using .*$serialport_re.*$additional_text.*$gdb_prompt $" {
79 verbose "Set target to $targetname"
82 -re "Remote debugging using stdio.*$additional_text.*$gdb_prompt $" {
83 verbose "Set target to $targetname"
86 -re "Remote target $targetname connected to.*$additional_text.*$gdb_prompt $" {
87 verbose "Set target to $targetname"
90 -re "Connected to.*$additional_text.*$gdb_prompt $" {
91 verbose "Set target to $targetname"
94 -re "Ending remote.*$gdb_prompt $" { }
95 -re "Connection refused.*$gdb_prompt $" {
96 verbose "Connection refused by remote target. Pausing, and trying again."
100 -re "Timeout reading from remote system.*$gdb_prompt $" {
101 verbose "Got timeout error from gdb."
103 -notransfer -re "Remote debugging using .*\r\n> $" {
104 # We got an unexpected prompt while creating the target.
105 # Leave it there for the test to diagnose.
108 -re ": Network is unreachable.\r\n.*$gdb_prompt $" {
120 # Like gdb_target_cmd_ext, but returns 0 on success, 1 on failure.
122 proc gdb_target_cmd { args } {
123 set res [eval gdb_target_cmd_ext $args]
124 return [expr $res == 0 ? 0 : 1]
130 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
132 proc find_gdbserver { } {
136 if [info exists GDBSERVER] {
140 if [target_info exists gdb_server_prog] {
141 return [target_info gdb_server_prog]
144 set toplevel [file join [file dirname $GDB] .. gdbserver]
145 foreach gdbserver [list "${GDB}server" $toplevel] {
146 if { [file isdirectory $gdbserver] } {
147 append gdbserver "/gdbserver"
150 if { [file executable $gdbserver] } {
158 # Return non-zero if we should skip gdbserver-specific tests.
160 proc skip_gdbserver_tests { } {
161 if { [find_gdbserver] == "" } {
165 # If GDB is lack of XML support, and targets, like arm, have
166 # multiple target descriptions, GDB doesn't know which target
167 # description GDBserver uses, and may fail to parse 'g' packet
169 if { [gdb_skip_xml_test]
170 && ([istarget "arm*-*-linux*"]
171 || [istarget "mips*-*-linux*"]
172 || [istarget "powerpc*-*-linux*"]
173 || [istarget "s390*-*-linux*"]
174 || [istarget "x86_64-*-linux*"]
175 || [istarget "i\[34567\]86-*-linux*"]) } {
182 # Download the currently loaded program to the target if necessary.
183 # Return the target system filename.
184 # NOTE: This was named "gdbserver_download", but that collides with the
185 # dejagnu "download" API function when using load_generic_config "gdbserver".
187 proc gdbserver_download_current_prog { } {
188 global gdbserver_host_exec
189 global gdbserver_host_mtime
190 global gdbserver_server_exec
191 global last_loaded_file
193 if { ![info exists last_loaded_file] } {
197 set host_exec $last_loaded_file
199 # If we already downloaded a file to the target, see if we can reuse it.
201 if { [info exists gdbserver_server_exec] } {
204 # If the file has changed, we can not.
205 if { $host_exec != $gdbserver_host_exec } {
209 # If the mtime has changed, we can not.
210 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
216 set gdbserver_host_exec $host_exec
217 set gdbserver_host_mtime [file mtime $host_exec]
218 set gdbserver_server_exec [gdb_remote_download target $host_exec]
221 return $gdbserver_server_exec
224 # Default routine to compute the argument to "target remote".
226 proc gdbserver_default_get_remote_address { host port } {
227 # Historically HOST included the trailing ":".
228 # To avoid breaking any board files out there we leave things alone.
232 # Default routine to compute the "comm" argument for gdbserver.
234 proc gdbserver_default_get_comm_port { port } {
238 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
239 # The port will be filled in between them automatically.
241 # Returns the target protocol and socket to connect to.
243 proc gdbserver_start { options arguments } {
245 global GDB_TEST_SOCKETHOST
247 # Port id -- either specified in baseboard file, or managed here.
248 if [target_info exists gdb,socketport] {
249 set portnum [target_info gdb,socketport]
251 # Bump the port number to avoid conflicts with hung ports.
255 # Extract the local and remote host ids from the target board struct.
256 if { [info exists GDB_TEST_SOCKETHOST] } {
257 # The user is not supposed to provide a port number, just a
258 # hostname/address, therefore we add the trailing ":" here.
259 set debughost "${GDB_TEST_SOCKETHOST}:"
260 # Escape open and close square brackets.
261 set debughost_tmp [string map { [ \\[ ] \\] } $debughost]
262 # We need a "gdbserver" version of the debughost, which will
263 # have the possible connection prefix stripped. This is
264 # because gdbserver currently doesn't recognize the prefixes.
265 regsub -all "^\(tcp:|udp:|tcp4:|udp4:|tcp6:|udp6:\)" $debughost_tmp "" debughost_gdbserver
266 } elseif [target_info exists sockethost] {
267 set debughost [target_info sockethost]
268 set debughost_gdbserver $debughost
270 set debughost "localhost:"
271 set debughost_gdbserver $debughost
274 # Some boards use a different value for the port that is passed to
275 # gdbserver and the port that is passed to the "target remote" command.
276 # One example is the stdio gdbserver support.
277 if [target_info exists gdb,get_remote_address] {
278 set get_remote_address [target_info gdb,get_remote_address]
280 set get_remote_address gdbserver_default_get_remote_address
282 if [target_info exists gdbserver,get_comm_port] {
283 set get_comm_port [target_info gdbserver,get_comm_port]
285 set get_comm_port gdbserver_default_get_comm_port
288 # Extract the protocol
289 if [target_info exists gdb_protocol] {
290 set protocol [target_info gdb_protocol]
292 set protocol "remote"
295 set gdbserver [find_gdbserver]
297 # Loop till we find a free port.
299 # Fire off the debug agent.
300 set gdbserver_command "$gdbserver"
302 # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be
303 # set to true already during gdbserver_start.
304 global gdbserver_reconnect_p
307 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
308 # GDB client could accidentally connect to a stale server.
309 append gdbserver_command " --once"
312 # Enable debug if set.
313 if [gdbserver_debug_enabled] {
314 global gdbserverdebug
316 foreach entry [split $gdbserverdebug ,] {
319 append gdbserver_command " --debug"
323 append gdbserver_command " --remote-debug"
328 # Ensure debugfile is only added if something has been enabled
330 set debugfile [standard_output_file gdbserver.debug]
331 append gdbserver_command " --debug-file=$debugfile"
335 if { $options != "" } {
336 append gdbserver_command " $options"
338 if { $debughost_gdbserver != "" } {
339 append gdbserver_command " $debughost_gdbserver"
341 if { $portnum != "" } {
342 if { $debughost_gdbserver == "" } {
343 append gdbserver_command " "
345 append gdbserver_command "[$get_comm_port $portnum]"
347 if { $arguments != "" } {
348 append gdbserver_command " $arguments"
351 gdbserver_write_cmd_file $gdbserver_command
353 global server_spawn_id
354 set server_spawn_id [remote_spawn target $gdbserver_command]
356 # GDBserver doesn't do inferior I/O through GDB. But we can
357 # talk to the program using GDBserver's tty instead.
358 global inferior_spawn_id
359 set inferior_spawn_id $server_spawn_id
361 # Wait for the server to open its TCP socket, so that GDB can connect.
366 -re "Listening on" { }
367 -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" {
368 verbose -log "Port $portnum is already in use."
369 if ![target_info exists gdb,socketport] {
370 # Bump the port number to avoid the conflict.
371 wait -i $expect_out(spawn_id)
376 -re ".*: cannot resolve name: .*\r\n" {
377 error "gdbserver cannot resolve name."
380 error "Timeout waiting for gdbserver response."
386 return [list $protocol [$get_remote_address $debughost $portnum]]
389 # Start a gdbserver process running SERVER_EXEC, and connect GDB
390 # to it. CHILD_ARGS are passed to the inferior.
392 # Returns the target protocol and socket to connect to.
394 proc gdbserver_spawn { child_args } {
395 set target_exec [gdbserver_download_current_prog]
397 # Fire off the debug agent. This flavour of gdbserver takes as
398 # arguments the port information, the name of the executable file to
399 # be debugged, and any arguments.
400 set arguments "$target_exec"
401 if { $child_args != "" } {
402 append arguments " $child_args"
404 return [gdbserver_start "" $arguments]
407 # Close the GDBserver connection.
409 proc close_gdbserver {} {
410 global server_spawn_id
412 # We can't just call close, because if gdbserver is local then that means
413 # that it will get a SIGHUP. Doing it this way could also allow us to
414 # get at the inferior's input or output if necessary, and means that we
415 # don't need to redirect output.
417 if {![info exists server_spawn_id]} {
421 verbose "Quitting GDBserver"
423 catch "close -i $server_spawn_id"
425 # If gdbserver misbehaves, and ignores the close, waiting for it
426 # without the -nowait flag will cause testing to hang. Passing
427 # -nowait makes expect tell Tcl to wait for the process in the
429 catch "wait -nowait -i $server_spawn_id"
430 unset server_spawn_id
433 # Hook into GDB exit, and close GDBserver. We must load this
434 # explicitly here, and rename the procedures we want to override.
435 load_lib mi-support.exp
437 if { [info procs gdbserver_orig_gdb_exit] == "" } {
438 rename gdb_exit gdbserver_orig_gdb_exit
439 rename mi_gdb_exit gdbserver_orig_mi_gdb_exit
442 # Cleanup gdbserver $server_spawn_id
444 proc gdbserver_exit { is_mi } {
445 global gdb_spawn_id server_spawn_id
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.
454 set monitor_exit "-interpreter-exec console \"monitor exit\""
456 set monitor_exit "monitor exit"
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.
464 -i "$gdb_spawn_id" -re "$gdb_prompt $" {
466 if { [info exists server_spawn_id] } {
470 -i "$server_spawn_id" eof {
471 wait -i $expect_out(spawn_id)
472 unset server_spawn_id
473 if { ! $have_prompt } {
478 warning "Timed out waiting for EOF in server after $monitor_exit"
486 # Local version of gdb_exit that also cleans up gdbserver $server_spawn_id.
488 proc gdbserver_gdb_exit { is_mi } {
489 global gdb_spawn_id server_spawn_id
491 global gdbserver_reconnect_p
493 # Leave GDBserver running if we're exiting GDB in order to
494 # reconnect to the same instance of GDBserver again.
495 if {[info exists gdbserver_reconnect_p] && $gdbserver_reconnect_p} {
497 gdbserver_orig_mi_gdb_exit
499 gdbserver_orig_gdb_exit
504 gdbserver_exit $is_mi
507 gdbserver_orig_mi_gdb_exit
509 gdbserver_orig_gdb_exit
517 proc mi_gdb_exit {} {
521 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
522 # to it. Return 0 on success, or non-zero on failure: 2 if gdbserver
523 # failed to start or 1 if we couldn't connect to it.
525 proc gdbserver_run { child_args } {
526 global gdbserver_protocol
527 global gdbserver_gdbport
529 # Kill anything running before we try to start gdbserver, in case
530 # we are sharing a serial connection.
532 send_gdb "kill\n" optional
534 -re "Kill the program being debugged. .y or n. $" {
536 verbose "\t\tKilling previous program being debugged"
539 -re "$gdb_prompt $" {
544 if { [catch { gdbserver_spawn $child_args } res] == 1 } {
548 set gdbserver_protocol [lindex $res 0]
549 set gdbserver_gdbport [lindex $res 1]
551 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
554 # Reconnect to the previous gdbserver session.
556 proc gdbserver_reconnect { } {
557 global gdbserver_protocol
558 global gdbserver_gdbport
560 global gdbserver_reconnect_p
561 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
562 error "gdbserver_reconnect_p is not set before gdbserver_reconnect"
566 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
569 # Start gdbserver in extended mode with OPTIONS and connect to it. Note
570 # this frobs $gdbserver_protocol, so should be used only from a board
571 # that usually connects in target remote mode.
572 proc gdbserver_start_extended { {options ""} } {
573 global gdbserver_protocol
574 global gdbserver_gdbport
577 set gdbserver_options "--multi"
579 if { $options != "" } {
580 append gdbserver_options " $options"
583 if { [catch { gdbserver_start $gdbserver_options "" } res] == 1 } {
587 set gdbserver_protocol [lindex $res 0]
588 if { [string first "extended-" $gdbserver_protocol] != 0} {
589 set gdbserver_protocol "extended-$gdbserver_protocol"
591 set gdbserver_gdbport [lindex $res 1]
593 # Even if the board file is testing with target remote, our caller
594 # wants to test against gdbserver in extended-remote mode. Make sure to
595 # disable stub-like techniques.
598 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
601 # Start and connect to a gdbserver in extended/multi mode. Unlike
602 # gdbserver_start_extended, this does not frob $gdbserver_protocol.
604 proc gdbserver_start_multi { } {
605 global gdbserver_protocol
606 global gdbserver_gdbport
608 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
612 set gdbserver_protocol [lindex $res 0]
613 set gdbserver_gdbport [lindex $res 1]
615 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
618 # Start a gdbserver process in multi/extended mode, and have GDB
619 # connect to it (MI version). Return 0 on success, or non-zero on
622 proc mi_gdbserver_start_multi { } {
623 global gdbserver_protocol
624 global gdbserver_gdbport
626 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
630 set gdbserver_protocol [lindex $res 0]
631 set gdbserver_gdbport [lindex $res 1]
633 return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
636 # Check if debugging is enabled for gdbserver.
638 proc gdbserver_debug_enabled { } {
639 global gdbserverdebug
641 # If not already read, get the debug setting from environment or board setting.
642 if ![info exists gdbserverdebug] {
644 if [info exists env(GDBSERVER_DEBUG)] {
645 set gdbserverdebug $env(GDBSERVER_DEBUG)
646 } elseif [target_info exists gdbserver,debug] {
647 set gdbserverdebug [target_info gdbserver,debug]
653 # Expand the all option
654 if { $gdbserverdebug == "all" } {
655 set gdbserverdebug "debug,remote,replay"
658 # Ensure it is not empty.
659 return [expr { $gdbserverdebug != "" }]
662 # Write the command line used to invocate gdbserver to the cmd file.
664 proc gdbserver_write_cmd_file { cmdline } {
665 set logfile [standard_output_file_with_gdb_instance gdbserver.cmd]
666 set cmd_file [open $logfile w]
667 puts $cmd_file $cmdline
668 catch "close $cmd_file"
671 # Override gdb_debug_init so that we can set replay logging in GDB if required.
672 # Backup the original function so we can call it afterwards
674 rename gdb_debug_init _gdb_debug_init
676 proc gdb_debug_init { } {
677 global gdbserverdebug
680 if [gdbserver_debug_enabled] {
681 foreach entry [split $gdbserverdebug ,] {
682 if { $entry == "replay" } {
683 set replayfile [standard_output_file_with_gdb_instance gdbserver.replay]
684 send_gdb "set remotelogfile $replayfile\n" optional
686 -re "$gdb_prompt $" {}
692 # Now call the standard debug init function