Updated copyright notices for most files.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdbserver-support.exp
index 71535d0085787bd615a857ed6d039eedb368c14a..406a3ccc4bceb1dc8a85413baea6d81d90f990ca 100644 (file)
@@ -1,18 +1,18 @@
-# Copyright 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+# Copyright 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+# 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).
@@ -44,6 +44,7 @@
 proc gdb_target_cmd { targetname serialport } {
     global gdb_prompt
 
+    set serialport_re [string_to_regexp $serialport]
     for {set i 1} {$i <= 3} {incr i} {
        send_gdb "target $targetname $serialport\n"
        gdb_expect 60 {
@@ -51,34 +52,39 @@ proc gdb_target_cmd { targetname serialport } {
                send_gdb "y\n"
                exp_continue
            }
-           -re "Couldn't establish connection to remote.*$gdb_prompt" {
+           -re "Couldn't establish connection to remote.*$gdb_prompt $" {
                verbose "Connection failed"
            }
            -re "Remote MIPS debugging.*$gdb_prompt" {
                verbose "Set target to $targetname"
                return 0
            }
-           -re "Remote debugging using .*$serialport.*$gdb_prompt" {
+           -re "Remote debugging using .*$serialport_re.*$gdb_prompt $" {
                verbose "Set target to $targetname"
                return 0
            }
-           -re "Remote target $targetname connected to.*$gdb_prompt" {
+           -re "Remote target $targetname connected to.*$gdb_prompt $" {
                verbose "Set target to $targetname"
                return 0
            }
-           -re "Connected to.*$gdb_prompt" { 
+           -re "Connected to.*$gdb_prompt $" { 
                verbose "Set target to $targetname"
                return 0
            }
-           -re "Ending remote.*$gdb_prompt" { }
-           -re "Connection refused.*$gdb_prompt" {
+           -re "Ending remote.*$gdb_prompt $" { }
+           -re "Connection refused.*$gdb_prompt $" {
                verbose "Connection refused by remote target.  Pausing, and trying again."
                sleep 30
                continue
            }
-           -re "Timeout reading from remote system.*$gdb_prompt" {
+           -re "Timeout reading from remote system.*$gdb_prompt $" {
                verbose "Got timeout error from gdb."
            }
+           -notransfer -re "Remote debugging using .*\r\n> $" {
+               # We got an unexpected prompt while creating the target.
+               # Leave it there for the test to diagnose.
+               return 1
+           }
            timeout {
                send_gdb "\ 3"
                break
@@ -128,8 +134,40 @@ proc skip_gdbserver_tests { } {
 #
 # Returns the target protocol and socket to connect to.
 
-proc gdbserver_spawn { server_exec child_args } {
+proc gdbserver_spawn { child_args } {
     global portnum
+    global gdbserver_host_exec
+    global gdbserver_host_mtime
+    global gdbserver_server_exec
+    global last_loaded_file
+
+    set host_exec $last_loaded_file
+
+    # If we already downloaded a file to the target, see if we can reuse it.
+    set reuse 0
+    if { [info exists gdbserver_server_exec] } {
+       set reuse 1
+
+       # If the file has changed, we can not.
+       if { $host_exec != $gdbserver_host_exec } {
+           set reuse 0
+       }
+
+       # If the mtime has changed, we can not.
+       if { [file mtime $host_exec] != $gdbserver_host_mtime } {
+           set reuse 0
+       }
+    }
+
+    if { $reuse == 0 } {
+       set gdbserver_host_exec $host_exec
+       set gdbserver_host_mtime [file mtime $host_exec]
+       if [is_remote target] {
+           set gdbserver_server_exec [gdb_download $host_exec]
+       } else {
+           set gdbserver_server_exec $host_exec
+       }
+    }
 
     # Port id -- either specified in baseboard file, or managed here.
     if [target_info exists gdb,socketport] {
@@ -161,7 +199,7 @@ proc gdbserver_spawn { server_exec child_args } {
     # 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 $server_exec"
+    set gdbserver_command "$gdbserver :$portnum $gdbserver_server_exec"
     if { $child_args != "" } {
        append gdbserver_command " $child_args"
     }
@@ -183,7 +221,7 @@ proc gdbserver_spawn { server_exec 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)
@@ -193,58 +231,40 @@ proc gdbserver_spawn { server_exec child_args } {
     return [list $protocol $gdbport]
 }
 
-proc infer_host_exec { } {
-    set host_exec ""
+# 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
 
-    send_gdb "info files\n"
-    gdb_expect 30 {
-       -re "Symbols from \"(\[^\"\]+)\"" {
-           set host_exec $expect_out(1,string)
+    # 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 "Local exec file:\[\r\n\]+\[ \t\]*`(\[^'\]+)'," {
-           set host_exec $expect_out(1,string)
-           exp_continue
+       -re "$gdb_prompt $" {
+           # OK.
        }
-       -re "$gdb_prompt $" { }
     }
 
-    return $host_exec
-}
-
-# Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
-# to it.  Return 0 on success, or non-zero on failure.
-
-proc gdbserver_load { host_exec child_args } {
-    global gdbserver_host_exec
-    global gdbserver_server_exec
-
-    # If we weren't passed an explicit binary, try to reuse the current
-    # one.  If we were, be sure to redownload it.
+    set res [gdbserver_spawn $child_args]
+    set gdbserver_protocol [lindex $res 0]
+    set gdbserver_gdbport [lindex $res 1]
 
-    if { $host_exec == "" && $gdbserver_host_exec == "" } {
-       set gdbserver_host_exec [infer_host_exec]
-    } elseif { $host_exec != "" } {
-       set gdbserver_host_exec $host_exec
-       if [info exists gdbserver_server_exec] { unset gdbserver_server_exec }
-    }
+    return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
+}
 
-    if { ! [info exists gdbserver_server_exec] } {
-       if [is_remote target] {
-           set gdbserver_server_exec [remote_download target $host_exec /tmp/[file tail $gdbserver_host_exec].[pid]]
-       } else {
-           set gdbserver_server_exec $gdbserver_host_exec
-       }
-    }
+# Reconnect to the previous gdbserver session.
 
-    set res [gdbserver_spawn $gdbserver_server_exec $child_args]
-    set protocol [lindex $res 0]
-    set gdbport [lindex $res 1]
+proc gdbserver_reconnect { } {
+    global gdbserver_protocol
+    global gdbserver_gdbport
 
-    if { $host_exec != "" } {
-       if [gdb_file_cmd $host_exec] {
-           return -1
-       }
-    }
-    gdb_target_cmd $protocol $gdbport
+    return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
 }
This page took 0.045833 seconds and 4 git commands to generate.