gdb
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdbserver-support.exp
index 0a8b1117bb5c095ae71321fab7ad6fd6571c8c4c..861406cc1f47f42fc4f9c78266956ce0d4e1e004 100644 (file)
@@ -1,19 +1,18 @@
-# Copyright 2000, 2002, 2003, 2004, 2005, 2006, 2007
+# Copyright 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
 # Free Software Foundation, Inc.
 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
+# the Free Software Foundation; either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # This file is based on config/gdbserver.exp, which was written by
 # Michael Snyder (msnyder@redhat.com).
@@ -53,6 +52,9 @@ proc gdb_target_cmd { targetname serialport } {
                send_gdb "y\n"
                exp_continue
            }
+           -re "unknown host.*$gdb_prompt" {
+               verbose "Couldn't look up $serialport"
+           }
            -re "Couldn't establish connection to remote.*$gdb_prompt $" {
                verbose "Connection failed"
            }
@@ -103,6 +105,11 @@ set portnum "2345"
 
 proc find_gdbserver { } {
   global GDB
+  global GDBSERVER
+
+  if [info exists GDBSERVER] {
+    return ${GDBSERVER}
+  }
 
   if [target_info exists gdb_server_prog] {
     return [target_info gdb_server_prog]
@@ -130,13 +137,10 @@ proc skip_gdbserver_tests { } {
   return 0
 }
 
-# Start a gdbserver process running SERVER_EXEC, and connect GDB
-# to it.  CHILD_ARGS are passed to the inferior.
-#
-# Returns the target protocol and socket to connect to.
+# Download the currently loaded program to the target if necessary.
+# Return the target system filename.
 
-proc gdbserver_spawn { child_args } {
-    global portnum
+proc gdbserver_download { } {
     global gdbserver_host_exec
     global gdbserver_host_mtime
     global gdbserver_server_exec
@@ -170,6 +174,17 @@ proc gdbserver_spawn { child_args } {
        }
     }
 
+    return $gdbserver_server_exec
+}
+
+# Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
+# The port will be filled in between them automatically.
+#
+# Returns the target protocol and socket to connect to.
+
+proc gdbserver_start { options arguments } {
+    global portnum
+
     # Port id -- either specified in baseboard file, or managed here.
     if [target_info exists gdb,socketport] {
        set portnum [target_info gdb,socketport]
@@ -180,7 +195,7 @@ proc gdbserver_spawn { child_args } {
 
     # Extract the local and remote host ids from the target board struct.
     if [target_info exists sockethost] {
-       set debughost  [target_info sockethost]
+       set debughost [target_info sockethost]
     } else {
        set debughost "localhost:"
     }
@@ -194,26 +209,39 @@ proc gdbserver_spawn { child_args } {
 
     set gdbserver [find_gdbserver]
 
-    # Export the host:port pair.
-    set gdbport $debughost$portnum
-
-    # Fire off the debug agent.  This flavour of gdbserver takes as
-    # arguments the port information, the name of the executable file to
-    # be debugged, and any arguments.
-    set gdbserver_command "$gdbserver :$portnum $gdbserver_server_exec"
-    if { $child_args != "" } {
-       append gdbserver_command " $child_args"
-    }
+    # Loop till we find a free port.
+    while 1 {
+       # Export the host:port pair.
+       set gdbport $debughost$portnum
 
-    set server_spawn_id [remote_spawn target $gdbserver_command]
+       # Fire off the debug agent.
+       set gdbserver_command "$gdbserver"
+       if { $options != "" } {
+           append gdbserver_command " $options"
+       }
+       append gdbserver_command " :$portnum"
+       if { $arguments != "" } {
+           append gdbserver_command " $arguments"
+       }
 
-    # Wait for the server to produce at least one line and an additional
-    # character of output.  This will wait until any TCP socket has been
-    # created, so that GDB can connect.
-    expect {
-       -i $server_spawn_id
-       -notransfer
-       -re ".*\n." { }
+       set server_spawn_id [remote_spawn target $gdbserver_command]
+
+       # Wait for the server to open its TCP socket, so that GDB can connect.
+       expect {
+           -i $server_spawn_id
+           -notransfer
+           -re "Listening on" { }
+           -re "Can't bind address: Address already in use\\.\r\n" {
+               verbose -log "Port $portnum is already in use."
+               if ![target_info exists gdb,socketport] {
+                   # Bump the port number to avoid the conflict.
+                   wait -i $expect_out(spawn_id)
+                   incr portnum
+                   continue
+               }
+           }
+       }
+       break
     }
 
     # We can't just call close, because if gdbserver is local then that means
@@ -222,7 +250,7 @@ proc gdbserver_spawn { child_args } {
     # don't need to redirect output.
     expect_background {
        -i $server_spawn_id
-       -re "." { }
+       full_buffer { }
        eof {
            # The spawn ID is already closed now (but not yet waited for).
            wait -i $expect_out(spawn_id)
@@ -232,13 +260,67 @@ proc gdbserver_spawn { child_args } {
     return [list $protocol $gdbport]
 }
 
+# Start a gdbserver process running SERVER_EXEC, and connect GDB
+# to it.  CHILD_ARGS are passed to the inferior.
+#
+# Returns the target protocol and socket to connect to.
+
+proc gdbserver_spawn { child_args } {
+    set target_exec [gdbserver_download]
+
+    # Fire off the debug agent.  This flavour of gdbserver takes as
+    # arguments the port information, the name of the executable file to
+    # be debugged, and any arguments.
+    set arguments "$target_exec"
+    if { $child_args != "" } {
+       append arguments " $child_args"
+    }
+    return [gdbserver_start "" $arguments]
+}
+
 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
 # to it.  Return 0 on success, or non-zero on failure.
 
 proc gdbserver_run { child_args } {
+    global gdbserver_protocol
+    global gdbserver_gdbport
+
+    # Kill anything running before we try to start gdbserver, in case
+    # we are sharing a serial connection.
+    global gdb_prompt
+    send_gdb "kill\n"
+    gdb_expect 120 {
+       -re "Kill the program being debugged. .y or n. $" {
+           send_gdb "y\n"
+           verbose "\t\tKilling previous program being debugged"
+           exp_continue
+       }
+       -re "$gdb_prompt $" {
+           # OK.
+       }
+    }
+
     set res [gdbserver_spawn $child_args]
-    set protocol [lindex $res 0]
-    set gdbport [lindex $res 1]
+    set gdbserver_protocol [lindex $res 0]
+    set gdbserver_gdbport [lindex $res 1]
+
+    return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
+}
+
+# Reconnect to the previous gdbserver session.
+
+proc gdbserver_reconnect { } {
+    global gdbserver_protocol
+    global gdbserver_gdbport
+
+    return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
+}
+
+# Start and connect to a gdbserver in extended mode.
+proc gdbserver_start_extended { } {
+    set res [gdbserver_start "--multi" ""]
+    set gdbserver_protocol "extended-[lindex $res 0]"
+    set gdbserver_gdbport [lindex $res 1]
 
-    return [gdb_target_cmd $protocol $gdbport]
+    return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
 }
This page took 0.056997 seconds and 4 git commands to generate.