1 # Copyright 2000-2018 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
47 proc gdb_target_cmd { targetname serialport } {
50 set serialport_re [string_to_regexp $serialport]
51 for {set i 1} {$i <= 3} {incr i} {
52 send_gdb "target $targetname $serialport\n"
54 -re "A program is being debugged already.*ill it.*y or n. $" {
58 -re "unknown host.*$gdb_prompt" {
59 verbose "Couldn't look up $serialport"
61 -re "Couldn't establish connection to remote.*$gdb_prompt $" {
62 verbose "Connection failed"
64 -re "Remote MIPS debugging.*$gdb_prompt" {
65 verbose "Set target to $targetname"
68 -re "Remote debugging using .*$serialport_re.*$gdb_prompt $" {
69 verbose "Set target to $targetname"
72 -re "Remote debugging using stdio.*$gdb_prompt $" {
73 verbose "Set target to $targetname"
76 -re "Remote target $targetname connected to.*$gdb_prompt $" {
77 verbose "Set target to $targetname"
80 -re "Connected to.*$gdb_prompt $" {
81 verbose "Set target to $targetname"
84 -re "Ending remote.*$gdb_prompt $" { }
85 -re "Connection refused.*$gdb_prompt $" {
86 verbose "Connection refused by remote target. Pausing, and trying again."
90 -re "Timeout reading from remote system.*$gdb_prompt $" {
91 verbose "Got timeout error from gdb."
93 -notransfer -re "Remote debugging using .*\r\n> $" {
94 # We got an unexpected prompt while creating the target.
95 # Leave it there for the test to diagnose.
111 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
113 proc find_gdbserver { } {
117 if [info exists GDBSERVER] {
121 if [target_info exists gdb_server_prog] {
122 return [target_info gdb_server_prog]
125 set gdbserver "${GDB}server"
126 if { [file isdirectory $gdbserver] } {
127 append gdbserver "/gdbserver"
130 if { [file executable $gdbserver] } {
137 # Return non-zero if we should skip gdbserver-specific tests.
139 proc skip_gdbserver_tests { } {
140 if { [find_gdbserver] == "" } {
144 # If GDB is lack of XML support, and targets, like arm, have
145 # multiple target descriptions, GDB doesn't know which target
146 # description GDBserver uses, and may fail to parse 'g' packet
148 if { [gdb_skip_xml_test]
149 && ([istarget "arm*-*-linux*"]
150 || [istarget "mips*-*-linux*"]
151 || [istarget "powerpc*-*-linux*"]
152 || [istarget "s390*-*-linux*"]
153 || [istarget "x86_64-*-linux*"]
154 || [istarget "i\[34567\]86-*-linux*"]) } {
161 # Download the currently loaded program to the target if necessary.
162 # Return the target system filename.
163 # NOTE: This was named "gdbserver_download", but that collides with the
164 # dejagnu "download" API function when using load_generic_config "gdbserver".
166 proc gdbserver_download_current_prog { } {
167 global gdbserver_host_exec
168 global gdbserver_host_mtime
169 global gdbserver_server_exec
170 global last_loaded_file
172 if { ![info exists last_loaded_file] } {
176 set host_exec $last_loaded_file
178 # If we already downloaded a file to the target, see if we can reuse it.
180 if { [info exists gdbserver_server_exec] } {
183 # If the file has changed, we can not.
184 if { $host_exec != $gdbserver_host_exec } {
188 # If the mtime has changed, we can not.
189 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
195 set gdbserver_host_exec $host_exec
196 set gdbserver_host_mtime [file mtime $host_exec]
197 set gdbserver_server_exec [gdb_remote_download target $host_exec]
200 return $gdbserver_server_exec
203 # Default routine to compute the argument to "target remote".
205 proc gdbserver_default_get_remote_address { host port } {
206 # Historically HOST included the trailing ":".
207 # To avoid breaking any board files out there we leave things alone.
211 # Default routine to compute the "comm" argument for gdbserver.
213 proc gdbserver_default_get_comm_port { port } {
217 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
218 # The port will be filled in between them automatically.
220 # Returns the target protocol and socket to connect to.
222 proc gdbserver_start { options arguments } {
224 global GDB_TEST_SOCKETHOST
226 # Port id -- either specified in baseboard file, or managed here.
227 if [target_info exists gdb,socketport] {
228 set portnum [target_info gdb,socketport]
230 # Bump the port number to avoid conflicts with hung ports.
234 # Extract the local and remote host ids from the target board struct.
235 if { [info exists GDB_TEST_SOCKETHOST] } {
236 # The user is not supposed to provide a port number, just a
237 # hostname/address, therefore we add the trailing ":" here.
238 set debughost "${GDB_TEST_SOCKETHOST}:"
239 # Escape open and close square brackets.
240 set debughost_tmp [string map { [ \\[ ] \\] } $debughost]
241 # We need a "gdbserver" version of the debughost, which will
242 # have the possible connection prefix stripped. This is
243 # because gdbserver currently doesn't recognize the prefixes.
244 regsub -all "^\(tcp:|udp:|tcp4:|udp4:|tcp6:|udp6:\)" $debughost_tmp "" debughost_gdbserver
245 } elseif [target_info exists sockethost] {
246 set debughost [target_info sockethost]
247 set debughost_gdbserver $debughost
249 set debughost "localhost:"
250 set debughost_gdbserver $debughost
253 # Some boards use a different value for the port that is passed to
254 # gdbserver and the port that is passed to the "target remote" command.
255 # One example is the stdio gdbserver support.
256 if [target_info exists gdb,get_remote_address] {
257 set get_remote_address [target_info gdb,get_remote_address]
259 set get_remote_address gdbserver_default_get_remote_address
261 if [target_info exists gdbserver,get_comm_port] {
262 set get_comm_port [target_info gdbserver,get_comm_port]
264 set get_comm_port gdbserver_default_get_comm_port
267 # Extract the protocol
268 if [target_info exists gdb_protocol] {
269 set protocol [target_info gdb_protocol]
271 set protocol "remote"
274 set gdbserver [find_gdbserver]
276 # Loop till we find a free port.
278 # Fire off the debug agent.
279 set gdbserver_command "$gdbserver"
281 # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be
282 # set to true already during gdbserver_start.
283 global gdbserver_reconnect_p
284 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
285 # GDB client could accidentally connect to a stale server.
286 # append gdbserver_command " --debug --once"
287 append gdbserver_command " --once"
290 if { $options != "" } {
291 append gdbserver_command " $options"
293 if { $debughost_gdbserver != "" } {
294 append gdbserver_command " $debughost_gdbserver"
296 if { $portnum != "" } {
297 if { $debughost_gdbserver == "" } {
298 append gdbserver_command " "
300 append gdbserver_command "[$get_comm_port $portnum]"
302 if { $arguments != "" } {
303 append gdbserver_command " $arguments"
306 global server_spawn_id
307 set server_spawn_id [remote_spawn target $gdbserver_command]
309 # GDBserver doesn't do inferior I/O through GDB. But we can
310 # talk to the program using GDBserver's tty instead.
311 global inferior_spawn_id
312 set inferior_spawn_id $server_spawn_id
314 # Wait for the server to open its TCP socket, so that GDB can connect.
319 -re "Listening on" { }
320 -re "Can't bind address: Address already in use\\.\r\n" {
321 verbose -log "Port $portnum is already in use."
322 if ![target_info exists gdb,socketport] {
323 # Bump the port number to avoid the conflict.
324 wait -i $expect_out(spawn_id)
329 -re ".*: cannot resolve name: \(No address associated with hostname|Name or service not known\)\r\n" {
330 error "gdbserver cannot resolve name."
333 error "Timeout waiting for gdbserver response."
339 return [list $protocol [$get_remote_address $debughost $portnum]]
342 # Start a gdbserver process running SERVER_EXEC, and connect GDB
343 # to it. CHILD_ARGS are passed to the inferior.
345 # Returns the target protocol and socket to connect to.
347 proc gdbserver_spawn { child_args } {
348 set target_exec [gdbserver_download_current_prog]
350 # Fire off the debug agent. This flavour of gdbserver takes as
351 # arguments the port information, the name of the executable file to
352 # be debugged, and any arguments.
353 set arguments "$target_exec"
354 if { $child_args != "" } {
355 append arguments " $child_args"
357 return [gdbserver_start "" $arguments]
360 # Close the GDBserver connection.
362 proc close_gdbserver {} {
363 global server_spawn_id
365 # We can't just call close, because if gdbserver is local then that means
366 # that it will get a SIGHUP. Doing it this way could also allow us to
367 # get at the inferior's input or output if necessary, and means that we
368 # don't need to redirect output.
370 if {![info exists server_spawn_id]} {
374 verbose "Quitting GDBserver"
376 catch "close -i $server_spawn_id"
377 catch "wait -i $server_spawn_id"
378 unset server_spawn_id
381 # Hook into GDB exit, and close GDBserver. We must load this
382 # explicitly here, and rename the procedures we want to override.
383 load_lib mi-support.exp
385 if { [info procs gdbserver_orig_gdb_exit] == "" } {
386 rename gdb_exit gdbserver_orig_gdb_exit
387 rename mi_gdb_exit gdbserver_orig_mi_gdb_exit
390 proc gdbserver_gdb_exit { is_mi } {
391 global gdb_spawn_id server_spawn_id
393 global gdbserver_reconnect_p
395 # Leave GDBserver running if we're exiting GDB in order to
396 # reconnect to the same instance of GDBserver again.
397 if {[info exists gdbserver_reconnect_p] && $gdbserver_reconnect_p} {
399 gdbserver_orig_mi_gdb_exit
401 gdbserver_orig_gdb_exit
406 if {[info exists gdb_spawn_id] && [info exists server_spawn_id]} {
407 # GDB may be terminated in an expected way or an unexpected way,
408 # but DejaGNU doesn't know that, so gdb_spawn_id isn't unset.
409 # Catch the exceptions.
412 set monitor_exit "-interpreter-exec console \"monitor exit\""
414 set monitor_exit "monitor exit"
416 send_gdb "$monitor_exit\n";
417 # We use expect rather than gdb_expect because
418 # we want to suppress printing exception messages, otherwise,
419 # remote_expect, invoked by gdb_expect, prints the exceptions.
421 -i "$gdb_spawn_id" -re "$gdb_prompt $" {
424 -i "$server_spawn_id" eof {
425 wait -i $expect_out(spawn_id)
426 unset server_spawn_id
434 gdbserver_orig_mi_gdb_exit
436 gdbserver_orig_gdb_exit
444 proc mi_gdb_exit {} {
448 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
449 # to it. Return 0 on success, or non-zero on failure: 2 if gdbserver
450 # failed to start or 1 if we couldn't connect to it.
452 proc gdbserver_run { child_args } {
453 global gdbserver_protocol
454 global gdbserver_gdbport
456 # Kill anything running before we try to start gdbserver, in case
457 # we are sharing a serial connection.
461 -re "Kill the program being debugged. .y or n. $" {
463 verbose "\t\tKilling previous program being debugged"
466 -re "$gdb_prompt $" {
471 if { [catch { gdbserver_spawn $child_args } res] == 1 } {
475 set gdbserver_protocol [lindex $res 0]
476 set gdbserver_gdbport [lindex $res 1]
478 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
481 # Reconnect to the previous gdbserver session.
483 proc gdbserver_reconnect { } {
484 global gdbserver_protocol
485 global gdbserver_gdbport
487 global gdbserver_reconnect_p
488 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
489 error "gdbserver_reconnect_p is not set before gdbserver_reconnect"
493 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
496 # Start gdbserver in extended mode with OPTIONS and connect to it. Note
497 # this frobs $gdbserver_protocol, so should be used only from a board
498 # that usually connects in target remote mode.
499 proc gdbserver_start_extended { {options ""} } {
500 global gdbserver_protocol
501 global gdbserver_gdbport
504 set gdbserver_options "--multi"
506 if { $options != "" } {
507 append gdbserver_options " $options"
510 if { [catch { gdbserver_start $gdbserver_options "" } res] == 1 } {
514 set gdbserver_protocol [lindex $res 0]
515 if { [string first "extended-" $gdbserver_protocol] != 0} {
516 set gdbserver_protocol "extended-$gdbserver_protocol"
518 set gdbserver_gdbport [lindex $res 1]
520 # Even if the board file is testing with target remote, our caller
521 # wants to test against gdbserver in extended-remote mode. Make sure to
522 # disable stub-like techniques.
525 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
528 # Start and connect to a gdbserver in extended/multi mode. Unlike
529 # gdbserver_start_extended, this does not frob $gdbserver_protocol.
531 proc gdbserver_start_multi { } {
532 global gdbserver_protocol
533 global gdbserver_gdbport
535 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
539 set gdbserver_protocol [lindex $res 0]
540 set gdbserver_gdbport [lindex $res 1]
542 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
545 # Start a gdbserver process in multi/extended mode, and have GDB
546 # connect to it (MI version). Return 0 on success, or non-zero on
549 proc mi_gdbserver_start_multi { } {
550 global gdbserver_protocol
551 global gdbserver_gdbport
553 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
557 set gdbserver_protocol [lindex $res 0]
558 set gdbserver_gdbport [lindex $res 1]
560 return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]