1 # Copyright 2000-2024 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 ""} } {
51 global gdb_prompt gdb_target_remote_cmd_msg
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"
76 set gdb_target_remote_cmd_msg $expect_out(buffer)
79 -re "Remote debugging using .*$serialport_re.*$additional_text.*$gdb_prompt $" {
80 verbose "Set target to $targetname"
81 set gdb_target_remote_cmd_msg $expect_out(buffer)
84 -re "Remote debugging using stdio.*$additional_text.*$gdb_prompt $" {
85 verbose "Set target to $targetname"
86 set gdb_target_remote_cmd_msg $expect_out(buffer)
89 -re "Remote target $targetname connected to.*$additional_text.*$gdb_prompt $" {
90 verbose "Set target to $targetname"
91 set gdb_target_remote_cmd_msg $expect_out(buffer)
94 -re "Connected to.*$additional_text.*$gdb_prompt $" {
95 verbose "Set target to $targetname"
96 set gdb_target_remote_cmd_msg $expect_out(buffer)
99 -re "Ending remote.*$gdb_prompt $" { }
100 -re "Connection refused.*$gdb_prompt $" {
101 verbose "Connection refused by remote target. Pausing, and trying again."
105 -re "Timeout reading from remote system.*$gdb_prompt $" {
106 verbose "Got timeout error from gdb."
108 -notransfer -re "Remote debugging using .*\r\n> $" {
109 # We got an unexpected prompt while creating the target.
110 # Leave it there for the test to diagnose.
113 -re ": Network is unreachable.\r\n.*$gdb_prompt $" {
125 # Like gdb_target_cmd_ext, but returns 0 on success, 1 on failure.
127 proc gdb_target_cmd { args } {
128 set res [eval gdb_target_cmd_ext $args]
129 return [expr $res == 0 ? 0 : 1]
135 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
137 proc find_gdbserver { } {
141 if [info exists GDBSERVER] {
145 if [target_info exists gdb_server_prog] {
146 return [target_info gdb_server_prog]
149 set toplevel [file join [file dirname $GDB] .. gdbserver]
150 foreach gdbserver [list "${GDB}server" $toplevel] {
151 if { [file isdirectory $gdbserver] } {
152 append gdbserver "/gdbserver"
155 if { [file executable $gdbserver] } {
163 # Return non-zero if we should run gdbserver-specific tests.
165 proc allow_gdbserver_tests { } {
166 if { [find_gdbserver] == "" } {
170 # If GDB is lack of XML support, and targets, like arm, have
171 # multiple target descriptions, GDB doesn't know which target
172 # description GDBserver uses, and may fail to parse 'g' packet
174 if { ![allow_xml_test]
175 && ([istarget "arm*-*-linux*"]
176 || [istarget "aarch64*-*-linux*"]
177 || [istarget "mips*-*-linux*"]
178 || [istarget "powerpc*-*-linux*"]
179 || [istarget "s390*-*-linux*"]
180 || [istarget "x86_64-*-linux*"]
181 || [istarget "i\[34567\]86-*-linux*"]) } {
188 # Download the currently loaded program to the target if necessary.
189 # Return the target system filename.
190 # NOTE: This was named "gdbserver_download", but that collides with the
191 # dejagnu "download" API function when using load_generic_config "gdbserver".
193 proc gdbserver_download_current_prog { } {
194 global gdbserver_host_exec
195 global gdbserver_host_mtime
196 global gdbserver_server_exec
197 global last_loaded_file
199 if { ![info exists last_loaded_file] } {
203 set host_exec $last_loaded_file
205 # If we already downloaded a file to the target, see if we can reuse it.
207 if { [info exists gdbserver_server_exec] } {
210 # If the file has changed, we can not.
211 if { $host_exec != $gdbserver_host_exec } {
215 # If the mtime has changed, we can not.
216 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
222 set gdbserver_host_exec $host_exec
223 set gdbserver_host_mtime [file mtime $host_exec]
224 set gdbserver_server_exec [gdb_remote_download target $host_exec]
227 return $gdbserver_server_exec
230 # Default routine to compute the argument to "target remote".
232 proc gdbserver_default_get_remote_address { host port } {
233 # Historically HOST included the trailing ":".
234 # To avoid breaking any board files out there we leave things alone.
238 # Default routine to compute the "comm" argument for gdbserver.
240 proc gdbserver_default_get_comm_port { port } {
244 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
245 # The port will be filled in between them automatically.
247 # Returns the target protocol and socket to connect to.
249 proc gdbserver_start { options arguments } {
251 global GDB_TEST_SOCKETHOST
253 # Port id -- either specified in baseboard file, or managed here.
254 if [target_info exists gdb,socketport] {
255 set portnum [target_info gdb,socketport]
257 # Bump the port number to avoid conflicts with hung ports.
261 # Extract the local and remote host ids from the target board struct.
262 if { [info exists GDB_TEST_SOCKETHOST] } {
263 # The user is not supposed to provide a port number, just a
264 # hostname/address, therefore we add the trailing ":" here.
265 set debughost "${GDB_TEST_SOCKETHOST}:"
266 # Escape open and close square brackets.
267 set debughost_tmp [string map { [ \\[ ] \\] } $debughost]
268 # We need a "gdbserver" version of the debughost, which will
269 # have the possible connection prefix stripped. This is
270 # because gdbserver currently doesn't recognize the prefixes.
271 regsub -all "^\(tcp:|udp:|tcp4:|udp4:|tcp6:|udp6:\)" $debughost_tmp "" debughost_gdbserver
272 } elseif [target_info exists sockethost] {
273 set debughost [target_info sockethost]
274 set debughost_gdbserver $debughost
276 set debughost "localhost:"
277 set debughost_gdbserver $debughost
280 # Some boards use a different value for the port that is passed to
281 # gdbserver and the port that is passed to the "target remote" command.
282 # One example is the stdio gdbserver support.
283 if [target_info exists gdb,get_remote_address] {
284 set get_remote_address [target_info gdb,get_remote_address]
286 set get_remote_address gdbserver_default_get_remote_address
288 if [target_info exists gdbserver,get_comm_port] {
289 set get_comm_port [target_info gdbserver,get_comm_port]
291 set get_comm_port gdbserver_default_get_comm_port
294 # Extract the protocol
295 if [target_info exists gdb_protocol] {
296 set protocol [target_info gdb_protocol]
298 set protocol "remote"
301 set gdbserver [find_gdbserver]
303 # Loop till we find a free port.
305 # Fire off the debug agent.
306 set gdbserver_command "$gdbserver"
308 # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be
309 # set to true already during gdbserver_start.
310 global gdbserver_reconnect_p
313 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
314 # GDB client could accidentally connect to a stale server.
315 append gdbserver_command " --once"
318 # Enable debug if set.
319 if [gdbserver_debug_enabled] {
320 global gdbserverdebug
322 foreach entry [split $gdbserverdebug ,] {
325 append gdbserver_command " --debug"
329 append gdbserver_command " --remote-debug"
334 # Ensure debugfile is only added if something has been enabled
336 set debugfile [standard_output_file gdbserver.debug]
337 append gdbserver_command " --debug-file=$debugfile"
341 if { $options != "" } {
342 append gdbserver_command " $options"
344 if { $debughost_gdbserver != "" } {
345 append gdbserver_command " $debughost_gdbserver"
347 if { $portnum != "" } {
348 if { $debughost_gdbserver == "" } {
349 append gdbserver_command " "
351 append gdbserver_command "[$get_comm_port $portnum]"
353 if { $arguments != "" } {
354 append gdbserver_command " $arguments"
357 gdbserver_write_cmd_file $gdbserver_command
359 global server_spawn_id
360 set server_spawn_id [remote_spawn target $gdbserver_command]
362 # GDBserver doesn't do inferior I/O through GDB. But we can
363 # talk to the program using GDBserver's tty instead.
364 global inferior_spawn_id
365 set inferior_spawn_id $server_spawn_id
367 # Wait for the server to open its TCP socket, so that GDB can connect.
372 -re "Listening on" { }
373 -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" {
374 verbose -log "Port $portnum is already in use."
375 if ![target_info exists gdb,socketport] {
376 # Bump the port number to avoid the conflict.
377 wait -i $expect_out(spawn_id)
382 -re ".*: cannot resolve name: .*\r\n" {
383 error "gdbserver cannot resolve name."
386 error "Timeout waiting for gdbserver response."
392 return [list $protocol [$get_remote_address $debughost $portnum]]
395 # Start a gdbserver process running SERVER_EXEC, and connect GDB
396 # to it. CHILD_ARGS are passed to the inferior.
398 # Returns the target protocol and socket to connect to.
400 proc gdbserver_spawn { child_args } {
401 set target_exec [gdbserver_download_current_prog]
403 # Fire off the debug agent. This flavour of gdbserver takes as
404 # arguments the port information, the name of the executable file to
405 # be debugged, and any arguments.
406 set arguments "$target_exec"
407 if { $child_args != "" } {
408 append arguments " $child_args"
410 return [gdbserver_start "" $arguments]
413 # Close the GDBserver connection.
415 proc close_gdbserver {} {
416 global server_spawn_id
418 # We can't just call close, because if gdbserver is local then that means
419 # that it will get a SIGHUP. Doing it this way could also allow us to
420 # get at the inferior's input or output if necessary, and means that we
421 # don't need to redirect output.
423 if {![info exists server_spawn_id]} {
427 verbose "Quitting GDBserver"
429 catch "close -i $server_spawn_id"
431 # If gdbserver misbehaves, and ignores the close, waiting for it
432 # without the -nowait flag will cause testing to hang. Passing
433 # -nowait makes expect tell Tcl to wait for the process in the
435 catch "wait -nowait -i $server_spawn_id"
436 unset server_spawn_id
439 # Hook into GDB exit, and close GDBserver. We must load this
440 # explicitly here, and rename the procedures we want to override.
441 load_lib mi-support.exp
443 if { [info procs gdbserver_orig_gdb_exit] == "" } {
444 rename gdb_exit gdbserver_orig_gdb_exit
445 rename mi_gdb_exit gdbserver_orig_mi_gdb_exit
448 # Cleanup gdbserver $server_spawn_id
450 proc gdbserver_exit { is_mi } {
451 global gdb_spawn_id server_spawn_id
454 if {[info exists gdb_spawn_id] && [info exists server_spawn_id]} {
455 # GDB may be terminated in an expected way or an unexpected way,
456 # but DejaGNU doesn't know that, so gdb_spawn_id isn't unset.
457 # Catch the exceptions.
460 set monitor_exit "-interpreter-exec console \"monitor exit\""
462 set monitor_exit "monitor exit"
464 send_gdb "$monitor_exit\n";
465 # We use expect rather than gdb_expect because
466 # we want to suppress printing exception messages, otherwise,
467 # remote_expect, invoked by gdb_expect, prints the exceptions.
470 -i "$gdb_spawn_id" -re "$gdb_prompt $" {
472 if { [info exists server_spawn_id] } {
476 -i "$server_spawn_id" eof {
477 wait -i $expect_out(spawn_id)
478 unset server_spawn_id
479 if { ! $have_prompt } {
484 warning "Timed out waiting for EOF in server after $monitor_exit"
492 # Local version of gdb_exit that also cleans up gdbserver $server_spawn_id.
494 proc gdbserver_gdb_exit { is_mi } {
495 global gdb_spawn_id server_spawn_id
497 global gdbserver_reconnect_p
499 # Leave GDBserver running if we're exiting GDB in order to
500 # reconnect to the same instance of GDBserver again.
501 if {[info exists gdbserver_reconnect_p] && $gdbserver_reconnect_p} {
503 gdbserver_orig_mi_gdb_exit
505 gdbserver_orig_gdb_exit
510 gdbserver_exit $is_mi
513 gdbserver_orig_mi_gdb_exit
515 gdbserver_orig_gdb_exit
523 proc mi_gdb_exit {} {
527 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
528 # to it. Return 0 on success, or non-zero on failure: 2 if gdbserver
529 # failed to start or 1 if we couldn't connect to it.
531 proc gdbserver_run { child_args } {
532 global gdbserver_protocol
533 global gdbserver_gdbport
535 # Kill anything running before we try to start gdbserver, in case
536 # we are sharing a serial connection.
538 send_gdb "kill\n" optional
540 -re "Kill the program being debugged. .y or n. $" {
542 verbose "\t\tKilling previous program being debugged"
545 -re "$gdb_prompt $" {
550 if { [catch { gdbserver_spawn $child_args } res] == 1 } {
554 set gdbserver_protocol [lindex $res 0]
555 set gdbserver_gdbport [lindex $res 1]
557 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
560 # Reconnect to the previous gdbserver session.
562 proc gdbserver_reconnect { } {
563 global gdbserver_protocol
564 global gdbserver_gdbport
566 global gdbserver_reconnect_p
567 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
568 error "gdbserver_reconnect_p is not set before gdbserver_reconnect"
572 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
575 # Start gdbserver in extended mode with OPTIONS and connect to it. Note
576 # this frobs $gdbserver_protocol, so should be used only from a board
577 # that usually connects in target remote mode.
578 proc gdbserver_start_extended { {options ""} } {
579 global gdbserver_protocol
580 global gdbserver_gdbport
583 set gdbserver_options "--multi"
585 if { $options != "" } {
586 append gdbserver_options " $options"
589 if { [catch { gdbserver_start $gdbserver_options "" } res] == 1 } {
593 set gdbserver_protocol [lindex $res 0]
594 if { [string first "extended-" $gdbserver_protocol] != 0} {
595 set gdbserver_protocol "extended-$gdbserver_protocol"
597 set gdbserver_gdbport [lindex $res 1]
599 # Even if the board file is testing with target remote, our caller
600 # wants to test against gdbserver in extended-remote mode. Make sure to
601 # disable stub-like techniques.
604 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
607 # Start and connect to a gdbserver in extended/multi mode. Unlike
608 # gdbserver_start_extended, this does not frob $gdbserver_protocol.
610 proc gdbserver_start_multi { } {
611 global gdbserver_protocol
612 global gdbserver_gdbport
614 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
618 set gdbserver_protocol [lindex $res 0]
619 set gdbserver_gdbport [lindex $res 1]
621 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
624 # Start a gdbserver process in multi/extended mode, and have GDB
625 # connect to it (MI version). Return 0 on success, or non-zero on
628 proc mi_gdbserver_start_multi { } {
629 global gdbserver_protocol
630 global gdbserver_gdbport
632 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
636 set gdbserver_protocol [lindex $res 0]
637 set gdbserver_gdbport [lindex $res 1]
639 return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
642 # Check if debugging is enabled for gdbserver.
644 proc gdbserver_debug_enabled { } {
645 global gdbserverdebug
647 # If not already read, get the debug setting from environment or board setting.
648 if ![info exists gdbserverdebug] {
650 if [info exists env(GDBSERVER_DEBUG)] {
651 set gdbserverdebug $env(GDBSERVER_DEBUG)
652 } elseif [target_info exists gdbserver,debug] {
653 set gdbserverdebug [target_info gdbserver,debug]
659 # Expand the all option
660 if { $gdbserverdebug == "all" } {
661 set gdbserverdebug "debug,remote,replay"
664 # Ensure it is not empty.
665 return [expr { $gdbserverdebug != "" }]
668 # Write the command line used to invocate gdbserver to the cmd file.
670 proc gdbserver_write_cmd_file { cmdline } {
671 set logfile [standard_output_file_with_gdb_instance gdbserver.cmd]
672 set cmd_file [open $logfile w]
673 puts $cmd_file $cmdline
674 catch "close $cmd_file"
677 # Override gdb_debug_init so that we can set replay logging in GDB if required.
678 # Backup the original function so we can call it afterwards
680 rename gdb_debug_init _gdb_debug_init
682 proc gdb_debug_init { } {
683 global gdbserverdebug
686 if [gdbserver_debug_enabled] {
687 foreach entry [split $gdbserverdebug ,] {
688 if { $entry == "replay" } {
689 set replayfile [standard_output_file_with_gdb_instance gdbserver.replay]
690 send_gdb "set remotelogfile $replayfile\n" optional
692 -re "$gdb_prompt $" {}
698 # Now call the standard debug init function