3c92a4fe9f536748a19badda1fdf75a30db48249
[external/binutils.git] / gdb / testsuite / config / sid.exp
1 # Test Framework Driver for GDB driving an external simulator
2 #   Copyright 1999-2019 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
17 load_lib gdb.exp
18
19 proc 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
107 proc gdb_start {} {
108     sid_start
109     return [default_gdb_start]
110 }
111
112 proc sid_exit {} {
113     if ![target_info exists gdb,no_push_conn] {
114         remote_close host
115         remote_pop_conn host
116     }
117 }
118
119 proc 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 #
129 proc 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
136 proc 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             gdb_exit
150         }
151         -re "Remote debugging using.*$gdb_prompt"       {
152             verbose "Set target to sid"
153         }
154         timeout { 
155             perror "Couldn't set target for remote simulator."
156             gdb_exit
157         }
158     }
159     set timeout $prev_timeout
160     verbose "Timeout is now $timeout seconds" 2
161 }
162
163 #
164 # gdb_load -- load a file into the debugger.
165 #             return a -1 if anything goes wrong.
166 #
167 proc gdb_load { arg } {
168     global verbose
169     global loadpath
170     global loadfile
171     global GDB
172     global gdb_prompt
173     global retval
174
175     if { $arg != "" } {
176         if [gdb_file_cmd $arg] then { return -1 }
177     }
178
179     gdb_target_sid
180
181     send_gdb "load\n"
182     global timeout
183     set prev_timeout $timeout
184     set timeout 2400
185     verbose "Timeout is now $timeout seconds" 2
186     gdb_expect {
187         -re ".*\[Ee\]rror.*$gdb_prompt $" {
188             if $verbose>1 then {
189                 perror "Error during download."
190             }
191             set retval -1
192         }
193         -re ".*$gdb_prompt $" {
194             if $verbose>1 then {
195                 send_user "Loaded $arg into $GDB\n"
196             }
197             set retval 0
198         }
199         -re "$gdb_prompt $"     {
200             if $verbose>1 then {
201                 perror "GDB couldn't load."
202             }
203             set retval -1
204         }
205         timeout {
206             if $verbose>1 then {
207                 perror "Timed out trying to load $arg."
208             }
209             set retval -1
210         }
211     }
212     set timeout $prev_timeout
213     verbose "Timeout is now $timeout seconds" 2
214     return $retval
215 }