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