1# Test Framework Driver for GDB driving an external simulator
2#   Copyright 1999, 2001, 2007 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 3 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, see <http://www.gnu.org/licenses/>.
16
17load_lib gdb.exp
18
19proc sid_start {} {
20    global verbose
21
22    set port [lindex [split [target_info netport] ":"] 1]
23
24    # Set a default endianness
25    case [target_info multilib_flags] in {
26	{ *big-endian* *-EB* *-meb* } { set sidendian "-EB" }
27	{ *little-endian* *-EL* *-mel* } { set sidendian "-EL" }
28	default {
29	    if {[target_info exists sim,defaultendian]} then {
30		set sidendian [target_info sim,defaultendian]
31	    } else {
32                # rely on endianness settings in sid configuration defaults
33                set sidendian ""
34	    }
35	}
36    }
37    case $sidendian in {
38	{ -EB } { set sidendian2 {-e "set cpu endian big"} }
39	{ -EL } { set sidendian2 {-e "set cpu endian little"} }
40	default { set sidendian2 {} }
41    }
42
43    # test to see whether to use use sid in build or install tree
44    set use_build_tree [file exists ../../sid]
45
46    if {$use_build_tree} then {
47	set pre_spawn {
48	    global env
49	    set env(SID_LIBRARY_PATH) [join [glob "../../sid/component/*"] ":"]
50	    set env(SID) "../../sid/main/dynamic/sid"
51	    if {! [file exists $env(SID)]} then { error "Cannot find sid in build tree" }
52	}
53	if { [board_info target sim,protocol] == "sid" } {
54	    set spawncmd "[target_info sim] [target_info sim,options] $sidendian2 -e \"set cpu-gdb-socket sockaddr-local 0.0.0.0:$port\""
55	} elseif { [board_info target sim,protocol] == "rawsid" } {
56	    set spawncmd "[target_info sim] [target_info sim,options] -$sidendian --gdb=$port"
57	} else {
58	    set spawncmd "../../sid/bsp/[target_info sim] $sidendian --gdb=$port [target_info sim,options]"
59	}
60	set post_spawn {
61	    global env
62	    unset env(SID_LIBRARY_PATH)
63	    unset env(SID)
64	}
65    } else {
66	set pre_spawn {}
67	if { [board_info target sim,protocol] == "sid" } {
68	    # FIXME: sim,options may be from the build tree, should find
69	    # it in the install tree.
70	    set spawncmd "sid [target_info sim,options] $sidendian2 -e \"set cpu-gdb-socket sockaddr-local 0.0.0.0:$port\""
71	} elseif { [board_info target sim,protocol] == "rawsid" } {
72	    set spawncmd "[target_info sim] [target_info sim,options] -$sidendian --gdb=$port"
73	} else {
74	    set spawncmd "[target_info sim] $sidendian --gdb=$port [target_info sim,options]"
75	}
76	set post_spawn {}
77    }
78
79    eval $pre_spawn
80    if {[catch [list remote_spawn host $spawncmd] msg]} {
81	perror $msg
82	exit 1
83    }
84    eval $post_spawn
85
86    # Don't do the following any more; it breaks with "runtest ... < /dev/null"
87#    expect_background {
88#	-re \[^\n\]*\n {
89#	    regsub "\n" $expect_out(buffer) {} msg
90#	    verbose "SID: $msg" 2
91#	}
92#    }
93
94    # There should be no need to sleep to give SID time to start;
95    # GDB would wait for a fair while for the stub to respond.
96    sleep 4
97
98    if ![target_info exists gdb,no_push_conn] {
99        remote_push_conn host;
100    }
101}
102
103#
104# Handle GDB talking to SID
105#
106
107proc gdb_start {} {
108    sid_start
109    return [default_gdb_start]
110}
111
112proc sid_exit {} {
113    if ![target_info exists gdb,no_push_conn] {
114	remote_close host;
115	remote_pop_conn host;
116    }
117}
118
119proc gdb_exit {} {
120    set result [default_gdb_exit]
121    sid_exit
122    return $result
123}
124
125#
126# gdb_target_sid
127# Set gdb to target the simulator
128#
129proc send_target_sid { } {
130    # wait a little while, giving sid time to shut down & restart its
131    # gdb socket
132    sleep 4
133    send_gdb "target [target_info gdb_protocol] [target_info netport]\n"
134}
135
136proc gdb_target_sid { } {
137    global gdb_prompt
138    global exit_status
139
140    send_target_sid
141
142    global timeout
143    set prev_timeout $timeout
144    set timeout 60
145    verbose "Timeout is now $timeout seconds" 2
146    gdb_expect {
147	-re ".*\[Ee\]rror.*$gdb_prompt $" {
148	    perror "Couldn't set target for remote simulator."
149	    cleanup
150	    gdb_exit
151	}
152	-re "Remote debugging using.*$gdb_prompt"	{
153	    verbose "Set target to sid"
154	}
155	timeout {
156	    perror "Couldn't set target for remote simulator."
157	    cleanup
158	    gdb_exit
159	}
160    }
161    set timeout $prev_timeout
162    verbose "Timeout is now $timeout seconds" 2
163}
164
165#
166# gdb_load -- load a file into the debugger.
167#             return a -1 if anything goes wrong.
168#
169proc gdb_load { arg } {
170    global verbose
171    global loadpath
172    global loadfile
173    global GDB
174    global gdb_prompt
175    global retval
176
177    if { $arg != "" } {
178	if [gdb_file_cmd $arg] then { return -1 }
179    }
180
181    gdb_target_sid
182
183    send_gdb "load\n"
184    global timeout
185    set prev_timeout $timeout
186    set timeout 2400
187    verbose "Timeout is now $timeout seconds" 2
188    gdb_expect {
189	-re ".*\[Ee\]rror.*$gdb_prompt $" {
190	    if $verbose>1 then {
191		perror "Error during download."
192	    }
193	    set retval -1;
194	}
195	-re ".*$gdb_prompt $" {
196	    if $verbose>1 then {
197		send_user "Loaded $arg into $GDB\n"
198	    }
199	    set retval 0;
200	}
201	-re "$gdb_prompt $"     {
202	    if $verbose>1 then {
203		perror "GDB couldn't load."
204	    }
205	    set retval -1;
206	}
207	timeout {
208	    if $verbose>1 then {
209		perror "Timed out trying to load $arg."
210	    }
211	    set retval -1;
212	}
213    }
214    set timeout $prev_timeout
215    verbose "Timeout is now $timeout seconds" 2
216    return $retval;
217}
218