* infrun.c (inferior_ignoring_startup_exec_events): Delete.
[external/binutils.git] / gdb / testsuite / lib / gdbserver-support.exp
1 # Copyright 2000, 2002, 2003, 2004, 2005, 2006, 2007
2 # 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 # This file is based on config/gdbserver.exp, which was written by
19 # Michael Snyder (msnyder@redhat.com).
20
21 #
22 # To be addressed or set in your baseboard config file:
23 #
24 #   set_board_info gdb_protocol "remote"
25 #       Unles you have a gdbserver that uses a different protocol...
26 #
27 #   set_board_info gdb_server_prog
28 #       This will be the path to the gdbserver program you want to test.
29 #       Defaults to "gdbserver".
30 #
31 #   set_board_info sockethost
32 #       The name of the host computer whose socket is being used.
33 #       Defaults to "localhost".  Note: old gdbserver requires 
34 #       that you define this, but libremote/gdbserver does not.
35 #
36 #   set_board_info gdb,socketport
37 #       Port id to use for socket connection.  If not set explicitly,
38 #       it will start at "2345" and increment for each use.
39 #
40
41 #
42 # gdb_target_cmd
43 # Send gdb the "target" command
44 #
45 proc gdb_target_cmd { targetname serialport } {
46     global gdb_prompt
47
48     set serialport_re [string_to_regexp $serialport]
49     for {set i 1} {$i <= 3} {incr i} {
50         send_gdb "target $targetname $serialport\n"
51         gdb_expect 60 {
52             -re "A program is being debugged already.*ill it.*y or n. $" {
53                 send_gdb "y\n"
54                 exp_continue
55             }
56             -re "Couldn't establish connection to remote.*$gdb_prompt $" {
57                 verbose "Connection failed"
58             }
59             -re "Remote MIPS debugging.*$gdb_prompt" {
60                 verbose "Set target to $targetname"
61                 return 0
62             }
63             -re "Remote debugging using .*$serialport_re.*$gdb_prompt $" {
64                 verbose "Set target to $targetname"
65                 return 0
66             }
67             -re "Remote target $targetname connected to.*$gdb_prompt $" {
68                 verbose "Set target to $targetname"
69                 return 0
70             }
71             -re "Connected to.*$gdb_prompt $" { 
72                 verbose "Set target to $targetname"
73                 return 0
74             }
75             -re "Ending remote.*$gdb_prompt $" { }
76             -re "Connection refused.*$gdb_prompt $" {
77                 verbose "Connection refused by remote target.  Pausing, and trying again."
78                 sleep 30
79                 continue
80             }
81             -re "Timeout reading from remote system.*$gdb_prompt $" {
82                 verbose "Got timeout error from gdb."
83             }
84             -notransfer -re "Remote debugging using .*\r\n> $" {
85                 # We got an unexpected prompt while creating the target.
86                 # Leave it there for the test to diagnose.
87                 return 1
88             }
89             timeout {
90                 send_gdb "\ 3"
91                 break
92             }
93         }
94     }
95     return 1
96 }
97
98
99 global portnum
100 set portnum "2345"
101
102 # Locate the gdbserver binary.  Returns "" if gdbserver could not be found.
103
104 proc find_gdbserver { } {
105   global GDB
106
107   if [target_info exists gdb_server_prog] {
108     return [target_info gdb_server_prog]
109   }
110
111   set gdbserver "${GDB}server"
112   if { [file isdirectory $gdbserver] } {
113     append gdbserver "/gdbserver"
114   }
115
116   if { [file executable $gdbserver] } {
117     return $gdbserver
118   }
119
120   return ""
121 }
122
123 # Return non-zero if we should skip gdbserver-specific tests.
124
125 proc skip_gdbserver_tests { } {
126   if { [find_gdbserver] == "" } {
127     return 1
128   }
129
130   return 0
131 }
132
133 # Start a gdbserver process running SERVER_EXEC, and connect GDB
134 # to it.  CHILD_ARGS are passed to the inferior.
135 #
136 # Returns the target protocol and socket to connect to.
137
138 proc gdbserver_spawn { child_args } {
139     global portnum
140     global gdbserver_host_exec
141     global gdbserver_host_mtime
142     global gdbserver_server_exec
143     global last_loaded_file
144
145     set host_exec $last_loaded_file
146
147     # If we already downloaded a file to the target, see if we can reuse it.
148     set reuse 0
149     if { [info exists gdbserver_server_exec] } {
150         set reuse 1
151
152         # If the file has changed, we can not.
153         if { $host_exec != $gdbserver_host_exec } {
154             set reuse 0
155         }
156
157         # If the mtime has changed, we can not.
158         if { [file mtime $host_exec] != $gdbserver_host_mtime } {
159             set reuse 0
160         }
161     }
162
163     if { $reuse == 0 } {
164         set gdbserver_host_exec $host_exec
165         set gdbserver_host_mtime [file mtime $host_exec]
166         if [is_remote target] {
167             set gdbserver_server_exec [gdb_download $host_exec]
168         } else {
169             set gdbserver_server_exec $host_exec
170         }
171     }
172
173     # Port id -- either specified in baseboard file, or managed here.
174     if [target_info exists gdb,socketport] {
175         set portnum [target_info gdb,socketport]
176     } else {
177         # Bump the port number to avoid conflicts with hung ports.
178         incr portnum
179     }
180
181     # Extract the local and remote host ids from the target board struct.
182     if [target_info exists sockethost] {
183         set debughost  [target_info sockethost]
184     } else {
185         set debughost "localhost:"
186     }
187
188     # Extract the protocol
189     if [target_info exists gdb_protocol] {
190         set protocol [target_info gdb_protocol]
191     } else {
192         set protocol "remote"
193     }
194
195     set gdbserver [find_gdbserver]
196
197     # Export the host:port pair.
198     set gdbport $debughost$portnum
199
200     # Fire off the debug agent.  This flavour of gdbserver takes as
201     # arguments the port information, the name of the executable file to
202     # be debugged, and any arguments.
203     set gdbserver_command "$gdbserver :$portnum $gdbserver_server_exec"
204     if { $child_args != "" } {
205         append gdbserver_command " $child_args"
206     }
207
208     set server_spawn_id [remote_spawn target $gdbserver_command]
209
210     # Wait for the server to produce at least one line and an additional
211     # character of output.  This will wait until any TCP socket has been
212     # created, so that GDB can connect.
213     expect {
214         -i $server_spawn_id
215         -notransfer
216         -re ".*\n." { }
217     }
218
219     # We can't just call close, because if gdbserver is local then that means
220     # that it will get a SIGHUP.  Doing it this way could also allow us to
221     # get at the inferior's input or output if necessary, and means that we
222     # don't need to redirect output.
223     expect_background {
224         -i $server_spawn_id
225         full_buffer { }
226         eof {
227             # The spawn ID is already closed now (but not yet waited for).
228             wait -i $expect_out(spawn_id)
229         }
230     }
231
232     return [list $protocol $gdbport]
233 }
234
235 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
236 # to it.  Return 0 on success, or non-zero on failure.
237
238 proc gdbserver_run { child_args } {
239     global gdbserver_protocol
240     global gdbserver_gdbport
241
242     # Kill anything running before we try to start gdbserver, in case
243     # we are sharing a serial connection.
244     global gdb_prompt
245     send_gdb "kill\n"
246     gdb_expect 120 {
247         -re "Kill the program being debugged. .y or n. $" {
248             send_gdb "y\n"
249             verbose "\t\tKilling previous program being debugged"
250             exp_continue
251         }
252         -re "$gdb_prompt $" {
253             # OK.
254         }
255     }
256
257     set res [gdbserver_spawn $child_args]
258     set gdbserver_protocol [lindex $res 0]
259     set gdbserver_gdbport [lindex $res 1]
260
261     return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
262 }
263
264 # Reconnect to the previous gdbserver session.
265
266 proc gdbserver_reconnect { } {
267     global gdbserver_protocol
268     global gdbserver_gdbport
269
270     return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
271 }