+# Copyright 2010 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 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, see <http://www.gnu.org/licenses/>.
+
+# Override its functions
+load_config gdbserver.exp
+
+proc port_open { port } {
+ set fi [open "/proc/net/tcp" "r"]
+ fconfigure $fi -translation binary
+ set data [read $fi]
+ close $fi
+
+ # 0: 00000000:07D0 00000000:0000 0A 00000000:00000000 00:00000000 00000000 500 ...
+
+ set port_hex4 [format "%04X" $port]
+ return [regexp -line "^ *\[0-9\]+: 0100007F:${port_hex4} 00000000:0000 0A " $data]
+}
+
+# OPTIONS and ARGUMENTS are ignored now.
+proc gdbserver_start { options arguments } {
+ set port 2000
+ set delay 0.1
+ set timeout 100
+
+ verbose -log "gdbserver_start options=$options arguments=$arguments"
+
+ if [port_open $port] {
+ error "Port $port is already allocated"
+ }
+
+ # FIXME: IPv6 wrt /proc/net/tcp.
+ set cmd "nc -l 127.0.0.1 $port <>/proc/ugdb >&0"
+ set cmd "exec $cmd"
+ regsub -all { } $cmd {\ } cmd
+ set cmd "bash -c $cmd"
+ set server_spawn_id [remote_spawn target $cmd]
+
+ for {set i 0} {$i < $timeout} {incr i} {
+ if [port_open $port] {
+ break
+ }
+ sleep $delay
+ }
+ if {$i == $timeout} {
+ error "Port $port is still closed"
+ }
+
+ # It should be "extended-remote" but gdbserver_start_extended prepends
+ # another "extended-" string.
+ return [list "remote" "localhost:$port"]
+}
+
+load_lib mi-support.exp
+
+proc mi_gdb_start { args } {
+ set retval [default_mi_gdb_start $args]
+
+ if {$retval == 0} {
+ global mi_gdb_prompt
+
+ set test "-gdb-set debug remote 1"
+ gdb_test_multiple $test $test {
+ -re "\\\^done\r\n$mi_gdb_prompt$" {
+ pass $test
+ }
+ }
+ }
+
+ return $retval
+}
+
+load_lib gdb.exp
+
+proc gdb_start { } {
+ set retval [default_gdb_start]
+
+ if {$retval == 0} {
+ gdb_test "set debug remote 1"
+ }
+
+ return $retval
+}