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