1 # Copyright 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
2 # Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # This file is based on config/gdbserver.exp, which was written by
18 # Michael Snyder (msnyder@redhat.com).
21 # To be addressed or set in your baseboard config file:
23 # set_board_info gdb_protocol "remote"
24 # Unles you have a gdbserver that uses a different protocol...
26 # set_board_info gdb_server_prog
27 # This will be the path to the gdbserver program you want to test.
28 # Defaults to "gdbserver".
30 # set_board_info sockethost
31 # The name of the host computer whose socket is being used.
32 # Defaults to "localhost". Note: old gdbserver requires
33 # that you define this, but libremote/gdbserver does not.
35 # set_board_info gdb,socketport
36 # Port id to use for socket connection. If not set explicitly,
37 # it will start at "2345" and increment for each use.
42 # Send gdb the "target" command
44 proc gdb_target_cmd { targetname serialport } {
47 set serialport_re [string_to_regexp $serialport]
48 for {set i 1} {$i <= 3} {incr i} {
49 send_gdb "target $targetname $serialport\n"
51 -re "A program is being debugged already.*ill it.*y or n. $" {
55 -re "unknown host.*$gdb_prompt" {
56 verbose "Couldn't look up $serialport"
58 -re "Couldn't establish connection to remote.*$gdb_prompt $" {
59 verbose "Connection failed"
61 -re "Remote MIPS debugging.*$gdb_prompt" {
62 verbose "Set target to $targetname"
65 -re "Remote debugging using .*$serialport_re.*$gdb_prompt $" {
66 verbose "Set target to $targetname"
69 -re "Remote target $targetname connected to.*$gdb_prompt $" {
70 verbose "Set target to $targetname"
73 -re "Connected to.*$gdb_prompt $" {
74 verbose "Set target to $targetname"
77 -re "Ending remote.*$gdb_prompt $" { }
78 -re "Connection refused.*$gdb_prompt $" {
79 verbose "Connection refused by remote target. Pausing, and trying again."
83 -re "Timeout reading from remote system.*$gdb_prompt $" {
84 verbose "Got timeout error from gdb."
86 -notransfer -re "Remote debugging using .*\r\n> $" {
87 # We got an unexpected prompt while creating the target.
88 # Leave it there for the test to diagnose.
104 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
106 proc find_gdbserver { } {
110 if [info exists GDBSERVER] {
114 if [target_info exists gdb_server_prog] {
115 return [target_info gdb_server_prog]
118 set gdbserver "${GDB}server"
119 if { [file isdirectory $gdbserver] } {
120 append gdbserver "/gdbserver"
123 if { [file executable $gdbserver] } {
130 # Return non-zero if we should skip gdbserver-specific tests.
132 proc skip_gdbserver_tests { } {
133 if { [find_gdbserver] == "" } {
140 # Download the currently loaded program to the target if necessary.
141 # Return the target system filename.
143 proc gdbserver_download { } {
144 global gdbserver_host_exec
145 global gdbserver_host_mtime
146 global gdbserver_server_exec
147 global last_loaded_file
149 set host_exec $last_loaded_file
151 # If we already downloaded a file to the target, see if we can reuse it.
153 if { [info exists gdbserver_server_exec] } {
156 # If the file has changed, we can not.
157 if { $host_exec != $gdbserver_host_exec } {
161 # If the mtime has changed, we can not.
162 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
168 set gdbserver_host_exec $host_exec
169 set gdbserver_host_mtime [file mtime $host_exec]
170 if [is_remote target] {
171 set gdbserver_server_exec [gdb_download $host_exec]
173 set gdbserver_server_exec $host_exec
177 return $gdbserver_server_exec
180 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
181 # The port will be filled in between them automatically.
183 # Returns the target protocol and socket to connect to.
185 proc gdbserver_start { options arguments } {
188 # Port id -- either specified in baseboard file, or managed here.
189 if [target_info exists gdb,socketport] {
190 set portnum [target_info gdb,socketport]
192 # Bump the port number to avoid conflicts with hung ports.
196 # Extract the local and remote host ids from the target board struct.
197 if [target_info exists sockethost] {
198 set debughost [target_info sockethost]
200 set debughost "localhost:"
203 # Extract the protocol
204 if [target_info exists gdb_protocol] {
205 set protocol [target_info gdb_protocol]
207 set protocol "remote"
210 set gdbserver [find_gdbserver]
212 # Loop till we find a free port.
214 # Export the host:port pair.
215 set gdbport $debughost$portnum
217 # Fire off the debug agent.
218 set gdbserver_command "$gdbserver"
219 if { $options != "" } {
220 append gdbserver_command " $options"
222 append gdbserver_command " :$portnum"
223 if { $arguments != "" } {
224 append gdbserver_command " $arguments"
227 set server_spawn_id [remote_spawn target $gdbserver_command]
229 # Wait for the server to open its TCP socket, so that GDB can connect.
233 -re "Listening on" { }
234 -re "Can't bind address: Address already in use\\.\r\n" {
235 verbose -log "Port $portnum is already in use."
236 if ![target_info exists gdb,socketport] {
237 # Bump the port number to avoid the conflict.
238 wait -i $expect_out(spawn_id)
247 # We can't just call close, because if gdbserver is local then that means
248 # that it will get a SIGHUP. Doing it this way could also allow us to
249 # get at the inferior's input or output if necessary, and means that we
250 # don't need to redirect output.
255 # The spawn ID is already closed now (but not yet waited for).
256 wait -i $expect_out(spawn_id)
260 return [list $protocol $gdbport]
263 # Start a gdbserver process running SERVER_EXEC, and connect GDB
264 # to it. CHILD_ARGS are passed to the inferior.
266 # Returns the target protocol and socket to connect to.
268 proc gdbserver_spawn { child_args } {
269 set target_exec [gdbserver_download]
271 # Fire off the debug agent. This flavour of gdbserver takes as
272 # arguments the port information, the name of the executable file to
273 # be debugged, and any arguments.
274 set arguments "$target_exec"
275 if { $child_args != "" } {
276 append arguments " $child_args"
278 return [gdbserver_start "" $arguments]
281 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
282 # to it. Return 0 on success, or non-zero on failure.
284 proc gdbserver_run { child_args } {
285 global gdbserver_protocol
286 global gdbserver_gdbport
288 # Kill anything running before we try to start gdbserver, in case
289 # we are sharing a serial connection.
293 -re "Kill the program being debugged. .y or n. $" {
295 verbose "\t\tKilling previous program being debugged"
298 -re "$gdb_prompt $" {
303 set res [gdbserver_spawn $child_args]
304 set gdbserver_protocol [lindex $res 0]
305 set gdbserver_gdbport [lindex $res 1]
307 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
310 # Reconnect to the previous gdbserver session.
312 proc gdbserver_reconnect { } {
313 global gdbserver_protocol
314 global gdbserver_gdbport
316 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
319 # Start and connect to a gdbserver in extended mode.
320 proc gdbserver_start_extended { } {
321 set res [gdbserver_start "--multi" ""]
322 set gdbserver_protocol "extended-[lindex $res 0]"
323 set gdbserver_gdbport [lindex $res 1]
325 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]