1 # Copyright 2000-2019 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.
46 # If specified, then ADDITIONAL_TEXT must match the text that comes after
47 # the connection message in order for the procedure to succeed.
49 proc gdb_target_cmd { targetname serialport {additional_text ""} } {
52 set serialport_re [string_to_regexp $serialport]
53 for {set i 1} {$i <= 3} {incr i} {
54 send_gdb "target $targetname $serialport\n"
56 -re "A program is being debugged already.*ill it.*y or n. $" {
60 -re "unknown host.*$gdb_prompt" {
61 verbose "Couldn't look up $serialport"
63 -re "Couldn't establish connection to remote.*$gdb_prompt $" {
64 verbose "Connection failed"
66 -re "Remote MIPS debugging.*$additional_text.*$gdb_prompt" {
67 verbose "Set target to $targetname"
70 -re "Remote debugging using .*$serialport_re.*$additional_text.*$gdb_prompt $" {
71 verbose "Set target to $targetname"
74 -re "Remote debugging using stdio.*$additional_text.*$gdb_prompt $" {
75 verbose "Set target to $targetname"
78 -re "Remote target $targetname connected to.*$additional_text.*$gdb_prompt $" {
79 verbose "Set target to $targetname"
82 -re "Connected to.*$additional_text.*$gdb_prompt $" {
83 verbose "Set target to $targetname"
86 -re "Ending remote.*$gdb_prompt $" { }
87 -re "Connection refused.*$gdb_prompt $" {
88 verbose "Connection refused by remote target. Pausing, and trying again."
92 -re "Timeout reading from remote system.*$gdb_prompt $" {
93 verbose "Got timeout error from gdb."
95 -notransfer -re "Remote debugging using .*\r\n> $" {
96 # We got an unexpected prompt while creating the target.
97 # Leave it there for the test to diagnose.
113 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
115 proc find_gdbserver { } {
119 if [info exists GDBSERVER] {
123 if [target_info exists gdb_server_prog] {
124 return [target_info gdb_server_prog]
127 set gdbserver "${GDB}server"
128 if { [file isdirectory $gdbserver] } {
129 append gdbserver "/gdbserver"
132 if { [file executable $gdbserver] } {
139 # Return non-zero if we should skip gdbserver-specific tests.
141 proc skip_gdbserver_tests { } {
142 if { [find_gdbserver] == "" } {
146 # If GDB is lack of XML support, and targets, like arm, have
147 # multiple target descriptions, GDB doesn't know which target
148 # description GDBserver uses, and may fail to parse 'g' packet
150 if { [gdb_skip_xml_test]
151 && ([istarget "arm*-*-linux*"]
152 || [istarget "mips*-*-linux*"]
153 || [istarget "powerpc*-*-linux*"]
154 || [istarget "s390*-*-linux*"]
155 || [istarget "x86_64-*-linux*"]
156 || [istarget "i\[34567\]86-*-linux*"]) } {
163 # Download the currently loaded program to the target if necessary.
164 # Return the target system filename.
165 # NOTE: This was named "gdbserver_download", but that collides with the
166 # dejagnu "download" API function when using load_generic_config "gdbserver".
168 proc gdbserver_download_current_prog { } {
169 global gdbserver_host_exec
170 global gdbserver_host_mtime
171 global gdbserver_server_exec
172 global last_loaded_file
174 if { ![info exists last_loaded_file] } {
178 set host_exec $last_loaded_file
180 # If we already downloaded a file to the target, see if we can reuse it.
182 if { [info exists gdbserver_server_exec] } {
185 # If the file has changed, we can not.
186 if { $host_exec != $gdbserver_host_exec } {
190 # If the mtime has changed, we can not.
191 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
197 set gdbserver_host_exec $host_exec
198 set gdbserver_host_mtime [file mtime $host_exec]
199 set gdbserver_server_exec [gdb_remote_download target $host_exec]
202 return $gdbserver_server_exec
205 # Default routine to compute the argument to "target remote".
207 proc gdbserver_default_get_remote_address { host port } {
208 # Historically HOST included the trailing ":".
209 # To avoid breaking any board files out there we leave things alone.
213 # Default routine to compute the "comm" argument for gdbserver.
215 proc gdbserver_default_get_comm_port { port } {
219 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
220 # The port will be filled in between them automatically.
222 # Returns the target protocol and socket to connect to.
224 proc gdbserver_start { options arguments } {
226 global GDB_TEST_SOCKETHOST
228 # Port id -- either specified in baseboard file, or managed here.
229 if [target_info exists gdb,socketport] {
230 set portnum [target_info gdb,socketport]
232 # Bump the port number to avoid conflicts with hung ports.
236 # Extract the local and remote host ids from the target board struct.
237 if { [info exists GDB_TEST_SOCKETHOST] } {
238 # The user is not supposed to provide a port number, just a
239 # hostname/address, therefore we add the trailing ":" here.
240 set debughost "${GDB_TEST_SOCKETHOST}:"
241 # Escape open and close square brackets.
242 set debughost_tmp [string map { [ \\[ ] \\] } $debughost]
243 # We need a "gdbserver" version of the debughost, which will
244 # have the possible connection prefix stripped. This is
245 # because gdbserver currently doesn't recognize the prefixes.
246 regsub -all "^\(tcp:|udp:|tcp4:|udp4:|tcp6:|udp6:\)" $debughost_tmp "" debughost_gdbserver
247 } elseif [target_info exists sockethost] {
248 set debughost [target_info sockethost]
249 set debughost_gdbserver $debughost
251 set debughost "localhost:"
252 set debughost_gdbserver $debughost
255 # Some boards use a different value for the port that is passed to
256 # gdbserver and the port that is passed to the "target remote" command.
257 # One example is the stdio gdbserver support.
258 if [target_info exists gdb,get_remote_address] {
259 set get_remote_address [target_info gdb,get_remote_address]
261 set get_remote_address gdbserver_default_get_remote_address
263 if [target_info exists gdbserver,get_comm_port] {
264 set get_comm_port [target_info gdbserver,get_comm_port]
266 set get_comm_port gdbserver_default_get_comm_port
269 # Extract the protocol
270 if [target_info exists gdb_protocol] {
271 set protocol [target_info gdb_protocol]
273 set protocol "remote"
276 set gdbserver [find_gdbserver]
278 # Loop till we find a free port.
280 # Fire off the debug agent.
281 set gdbserver_command "$gdbserver"
283 # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be
284 # set to true already during gdbserver_start.
285 global gdbserver_reconnect_p
288 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
289 # GDB client could accidentally connect to a stale server.
290 append gdbserver_command " --once"
293 # Enable debug if set.
294 if [gdbserver_debug_enabled] {
295 global gdbserverdebug
296 set debugfile [standard_output_file gdbserver.debug]
297 if { $gdbserverdebug == "debug" } {
298 append gdbserver_command " --debug --debug-file=$debugfile"
299 } elseif { $gdbserverdebug == "remote" } {
300 append gdbserver_command " --remote-debug --debug-file=$debugfile"
301 } elseif { $gdbserverdebug == "all" } {
302 append gdbserver_command " --debug --remote-debug --debug-file=$debugfile"
306 if { $options != "" } {
307 append gdbserver_command " $options"
309 if { $debughost_gdbserver != "" } {
310 append gdbserver_command " $debughost_gdbserver"
312 if { $portnum != "" } {
313 if { $debughost_gdbserver == "" } {
314 append gdbserver_command " "
316 append gdbserver_command "[$get_comm_port $portnum]"
318 if { $arguments != "" } {
319 append gdbserver_command " $arguments"
322 global server_spawn_id
323 set server_spawn_id [remote_spawn target $gdbserver_command]
325 # GDBserver doesn't do inferior I/O through GDB. But we can
326 # talk to the program using GDBserver's tty instead.
327 global inferior_spawn_id
328 set inferior_spawn_id $server_spawn_id
330 # Wait for the server to open its TCP socket, so that GDB can connect.
335 -re "Listening on" { }
336 -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" {
337 verbose -log "Port $portnum is already in use."
338 if ![target_info exists gdb,socketport] {
339 # Bump the port number to avoid the conflict.
340 wait -i $expect_out(spawn_id)
345 -re ".*: cannot resolve name: .*\r\n" {
346 error "gdbserver cannot resolve name."
349 error "Timeout waiting for gdbserver response."
355 return [list $protocol [$get_remote_address $debughost $portnum]]
358 # Start a gdbserver process running SERVER_EXEC, and connect GDB
359 # to it. CHILD_ARGS are passed to the inferior.
361 # Returns the target protocol and socket to connect to.
363 proc gdbserver_spawn { child_args } {
364 set target_exec [gdbserver_download_current_prog]
366 # Fire off the debug agent. This flavour of gdbserver takes as
367 # arguments the port information, the name of the executable file to
368 # be debugged, and any arguments.
369 set arguments "$target_exec"
370 if { $child_args != "" } {
371 append arguments " $child_args"
373 return [gdbserver_start "" $arguments]
376 # Close the GDBserver connection.
378 proc close_gdbserver {} {
379 global server_spawn_id
381 # We can't just call close, because if gdbserver is local then that means
382 # that it will get a SIGHUP. Doing it this way could also allow us to
383 # get at the inferior's input or output if necessary, and means that we
384 # don't need to redirect output.
386 if {![info exists server_spawn_id]} {
390 verbose "Quitting GDBserver"
392 catch "close -i $server_spawn_id"
393 catch "wait -i $server_spawn_id"
394 unset server_spawn_id
397 # Hook into GDB exit, and close GDBserver. We must load this
398 # explicitly here, and rename the procedures we want to override.
399 load_lib mi-support.exp
401 if { [info procs gdbserver_orig_gdb_exit] == "" } {
402 rename gdb_exit gdbserver_orig_gdb_exit
403 rename mi_gdb_exit gdbserver_orig_mi_gdb_exit
406 proc gdbserver_gdb_exit { is_mi } {
407 global gdb_spawn_id server_spawn_id
409 global gdbserver_reconnect_p
411 # Leave GDBserver running if we're exiting GDB in order to
412 # reconnect to the same instance of GDBserver again.
413 if {[info exists gdbserver_reconnect_p] && $gdbserver_reconnect_p} {
415 gdbserver_orig_mi_gdb_exit
417 gdbserver_orig_gdb_exit
422 if {[info exists gdb_spawn_id] && [info exists server_spawn_id]} {
423 # GDB may be terminated in an expected way or an unexpected way,
424 # but DejaGNU doesn't know that, so gdb_spawn_id isn't unset.
425 # Catch the exceptions.
428 set monitor_exit "-interpreter-exec console \"monitor exit\""
430 set monitor_exit "monitor exit"
432 send_gdb "$monitor_exit\n";
433 # We use expect rather than gdb_expect because
434 # we want to suppress printing exception messages, otherwise,
435 # remote_expect, invoked by gdb_expect, prints the exceptions.
437 -i "$gdb_spawn_id" -re "$gdb_prompt $" {
440 -i "$server_spawn_id" eof {
441 wait -i $expect_out(spawn_id)
442 unset server_spawn_id
450 gdbserver_orig_mi_gdb_exit
452 gdbserver_orig_gdb_exit
460 proc mi_gdb_exit {} {
464 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
465 # to it. Return 0 on success, or non-zero on failure: 2 if gdbserver
466 # failed to start or 1 if we couldn't connect to it.
468 proc gdbserver_run { child_args } {
469 global gdbserver_protocol
470 global gdbserver_gdbport
472 # Kill anything running before we try to start gdbserver, in case
473 # we are sharing a serial connection.
477 -re "Kill the program being debugged. .y or n. $" {
479 verbose "\t\tKilling previous program being debugged"
482 -re "$gdb_prompt $" {
487 if { [catch { gdbserver_spawn $child_args } res] == 1 } {
491 set gdbserver_protocol [lindex $res 0]
492 set gdbserver_gdbport [lindex $res 1]
494 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
497 # Reconnect to the previous gdbserver session.
499 proc gdbserver_reconnect { } {
500 global gdbserver_protocol
501 global gdbserver_gdbport
503 global gdbserver_reconnect_p
504 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
505 error "gdbserver_reconnect_p is not set before gdbserver_reconnect"
509 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
512 # Start gdbserver in extended mode with OPTIONS and connect to it. Note
513 # this frobs $gdbserver_protocol, so should be used only from a board
514 # that usually connects in target remote mode.
515 proc gdbserver_start_extended { {options ""} } {
516 global gdbserver_protocol
517 global gdbserver_gdbport
520 set gdbserver_options "--multi"
522 if { $options != "" } {
523 append gdbserver_options " $options"
526 if { [catch { gdbserver_start $gdbserver_options "" } res] == 1 } {
530 set gdbserver_protocol [lindex $res 0]
531 if { [string first "extended-" $gdbserver_protocol] != 0} {
532 set gdbserver_protocol "extended-$gdbserver_protocol"
534 set gdbserver_gdbport [lindex $res 1]
536 # Even if the board file is testing with target remote, our caller
537 # wants to test against gdbserver in extended-remote mode. Make sure to
538 # disable stub-like techniques.
541 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
544 # Start and connect to a gdbserver in extended/multi mode. Unlike
545 # gdbserver_start_extended, this does not frob $gdbserver_protocol.
547 proc gdbserver_start_multi { } {
548 global gdbserver_protocol
549 global gdbserver_gdbport
551 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
555 set gdbserver_protocol [lindex $res 0]
556 set gdbserver_gdbport [lindex $res 1]
558 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
561 # Start a gdbserver process in multi/extended mode, and have GDB
562 # connect to it (MI version). Return 0 on success, or non-zero on
565 proc mi_gdbserver_start_multi { } {
566 global gdbserver_protocol
567 global gdbserver_gdbport
569 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
573 set gdbserver_protocol [lindex $res 0]
574 set gdbserver_gdbport [lindex $res 1]
576 return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
579 # Check if debugging is enabled for gdbserver.
581 proc gdbserver_debug_enabled { } {
582 global gdbserverdebug
584 # If not already read, get the debug setting from environment or board setting.
585 if ![info exists gdbserverdebug] {
587 if [info exists env(GDBSERVER_DEBUG)] {
588 set gdbserverdebug $env(GDBSERVER_DEBUG)
589 } elseif [target_info exists gdbserver,debug] {
590 set gdbserverdebug [target_info gdbserver,debug]
596 # Only return success on valid values.
597 return [expr { $gdbserverdebug == "debug" || $gdbserverdebug == "remote"
598 || $gdbserverdebug == "all" }]