1# Test Framework Driver for GDB using the extended gdb remote protocol
2#   Copyright 1995 Free Software Foundation, Inc.
3#
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 2 of the License, or
7# (at your option) any later version.
8#
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.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17#
18# For this to function correctly, you need to set a number of variables
19# in your gdb/site.exp file
20#
21#	set noargs 1		-- we can't pass arguments (yet)
22#	set noinferiorio 1	-- we can't get io to/from the inferior
23#	set targethost <host>	-- name of the remote system (runs gdbserver)
24#	set debughost <host>	-- name of the system running gdb
25#	set port <number>	-- starting port number for communication
26#	set gdbserver <path>	-- path (on the remote side) to find
27#				   gdbserver
28#       set rsh <path>		-- path (on debughost side) to rsh
29#	set rcp <path>		-- path (on debughost side) to rcp
30#
31# You will need to be able to spawn processes from gdbhost to run on
32# targethost via rsh (this is how we start gdbserver); similarly
33# you need to be able to rcp files from gdbhost to targethost.
34#
35# We don't do much error checking, if something goes wrong, you'll probably
36# just get a tcl error and everything will die.  FIXME
37#
38
39# Load the basic gdb testing library
40load_lib gdb.exp
41
42#
43# gdb_version -- extract and print the version number of gdb
44#
45proc gdb_version {} {
46    default_gdb_version
47}
48
49#
50# gdb_target_monitor
51# Set gdb to target the monitor
52#
53proc gdb_target_monitor { } {
54    global prompt
55    global exit_status
56    global targetname
57    global serialport
58    global baud
59
60    set timeout 60
61    verbose "Timeout is now $timeout seconds" 2
62    for {set i 1} {$i <= 3} {incr i} {
63	send "target $targetname $serialport\n"
64	expect {
65		-re "Remote debugging using $serialport.*$prompt $"	{
66		     verbose "Set target to $targetname"
67		     return
68		}
69		-re "Connection refused" {
70		    verbose "Connection refused by remote target.  Pausing, and trying again."
71		    sleep 30
72		    continue
73		}
74		timeout {
75		    break
76		}
77	}
78    }
79
80    perror "Couldn't set target for $targetname."
81    cleanup
82    exit $exit_status
83}
84
85#
86# gdb_load -- load a file into the debugger.
87#             return a -1 if anything goes wrong.
88#
89# Loading a file in the gdbsrever framework is a little strange in that
90# we also create the inferior (which is stopped at the first instruction
91# in the program when we get control).
92#
93proc gdb_load { arg } {
94    global verbose
95    global loadpath
96    global loadfile
97    global GDB
98    global prompt
99    global serialport
100    global targethost
101    global debughost
102    global port
103    global gdbserver
104    global rsh
105    global rcp
106
107    # first load the file into gdb
108    if [gdb_file_cmd $arg] then { return -1 }
109
110    # bump the port number to avoid conflicts with hung ports
111    incr port
112    set serialport $targethost:$port
113
114
115    # Copy the file down to the remote host.
116    exec $rcp [lindex $arg 0] $targethost:/tmp
117
118    # now start gdbserver on the remote side
119    exec $rsh $targethost $gdbserver $debughost:$port /tmp/[file tail [lindex $arg 0]] > /dev/null >& /dev/null < /dev/null &
120
121    # give it plenty of time to get going (lynx)
122    sleep 30
123
124    # tell gdb we are remote debugging
125    gdb_target_monitor
126
127    return 1
128}
129
130#
131# gdb_start -- start GDB running.
132#
133proc gdb_start { } {
134    global prompt
135
136    # do the usual stuff
137    catch default_gdb_start
138
139    # FIXME: This shouldn't be necessary, but lots of PA tests fail
140    # without it.
141    send "set remotecache 0\n"
142    expect {
143	-re "set remotecache 0\[\r\n\]+.*$prompt $" {}
144	default { fail "gdb_start"}
145    }
146}
147
148#
149# gdb_exit -- exit gdb
150#
151proc gdb_exit { } {
152    catch default_gdb_exit
153}
154
155gdb_start
156
157