* NEWS: Mention tracepoint additions.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdbserver-support.exp
1 # Copyright 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 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 # This file is based on config/gdbserver.exp, which was written by
18 # Michael Snyder (msnyder@redhat.com).
19
20 #
21 # To be addressed or set in your baseboard config file:
22 #
23 # set_board_info gdb_protocol "remote"
24 # Unles you have a gdbserver that uses a different protocol...
25 #
26 # set_board_info gdb_server_prog
27 # This will be the path to the gdbserver program you want to test.
28 # Defaults to "gdbserver".
29 #
30 # set_board_info sockethost
31 # The name of the host computer whose socket is being used.
32 # Defaults to "localhost". Note: old gdbserver requires
33 # that you define this, but libremote/gdbserver does not.
34 #
35 # set_board_info gdb,socketport
36 # Port id to use for socket connection. If not set explicitly,
37 # it will start at "2345" and increment for each use.
38 #
39
40 #
41 # gdb_target_cmd
42 # Send gdb the "target" command
43 #
44 proc gdb_target_cmd { targetname serialport } {
45 global gdb_prompt
46
47 set serialport_re [string_to_regexp $serialport]
48 for {set i 1} {$i <= 3} {incr i} {
49 send_gdb "target $targetname $serialport\n"
50 gdb_expect 60 {
51 -re "A program is being debugged already.*ill it.*y or n. $" {
52 send_gdb "y\n"
53 exp_continue
54 }
55 -re "unknown host.*$gdb_prompt" {
56 verbose "Couldn't look up $serialport"
57 }
58 -re "Couldn't establish connection to remote.*$gdb_prompt $" {
59 verbose "Connection failed"
60 }
61 -re "Remote MIPS debugging.*$gdb_prompt" {
62 verbose "Set target to $targetname"
63 return 0
64 }
65 -re "Remote debugging using .*$serialport_re.*$gdb_prompt $" {
66 verbose "Set target to $targetname"
67 return 0
68 }
69 -re "Remote target $targetname connected to.*$gdb_prompt $" {
70 verbose "Set target to $targetname"
71 return 0
72 }
73 -re "Connected to.*$gdb_prompt $" {
74 verbose "Set target to $targetname"
75 return 0
76 }
77 -re "Ending remote.*$gdb_prompt $" { }
78 -re "Connection refused.*$gdb_prompt $" {
79 verbose "Connection refused by remote target. Pausing, and trying again."
80 sleep 30
81 continue
82 }
83 -re "Timeout reading from remote system.*$gdb_prompt $" {
84 verbose "Got timeout error from gdb."
85 }
86 -notransfer -re "Remote debugging using .*\r\n> $" {
87 # We got an unexpected prompt while creating the target.
88 # Leave it there for the test to diagnose.
89 return 1
90 }
91 timeout {
92 send_gdb "\ 3"
93 break
94 }
95 }
96 }
97 return 1
98 }
99
100
101 global portnum
102 set portnum "2345"
103
104 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
105
106 proc find_gdbserver { } {
107 global GDB
108 global GDBSERVER
109
110 if [info exists GDBSERVER] {
111 return ${GDBSERVER}
112 }
113
114 if [target_info exists gdb_server_prog] {
115 return [target_info gdb_server_prog]
116 }
117
118 set gdbserver "${GDB}server"
119 if { [file isdirectory $gdbserver] } {
120 append gdbserver "/gdbserver"
121 }
122
123 if { [file executable $gdbserver] } {
124 return $gdbserver
125 }
126
127 return ""
128 }
129
130 # Return non-zero if we should skip gdbserver-specific tests.
131
132 proc skip_gdbserver_tests { } {
133 if { [find_gdbserver] == "" } {
134 return 1
135 }
136
137 return 0
138 }
139
140 # Download the currently loaded program to the target if necessary.
141 # Return the target system filename.
142 # NOTE: This was named "gdbserver_download", but that collides with the
143 # dejagnu "download" API function when using load_generic_config "gdbserver".
144
145 proc gdbserver_download_current_prog { } {
146 global gdbserver_host_exec
147 global gdbserver_host_mtime
148 global gdbserver_server_exec
149 global last_loaded_file
150
151 set host_exec $last_loaded_file
152
153 # If we already downloaded a file to the target, see if we can reuse it.
154 set reuse 0
155 if { [info exists gdbserver_server_exec] } {
156 set reuse 1
157
158 # If the file has changed, we can not.
159 if { $host_exec != $gdbserver_host_exec } {
160 set reuse 0
161 }
162
163 # If the mtime has changed, we can not.
164 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
165 set reuse 0
166 }
167 }
168
169 if { $reuse == 0 } {
170 set gdbserver_host_exec $host_exec
171 set gdbserver_host_mtime [file mtime $host_exec]
172 if [is_remote target] {
173 set gdbserver_server_exec [gdb_download $host_exec]
174 } else {
175 set gdbserver_server_exec $host_exec
176 }
177 }
178
179 return $gdbserver_server_exec
180 }
181
182 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
183 # The port will be filled in between them automatically.
184 #
185 # Returns the target protocol and socket to connect to.
186
187 proc gdbserver_start { options arguments } {
188 global portnum
189
190 # Port id -- either specified in baseboard file, or managed here.
191 if [target_info exists gdb,socketport] {
192 set portnum [target_info gdb,socketport]
193 } else {
194 # Bump the port number to avoid conflicts with hung ports.
195 incr portnum
196 }
197
198 # Extract the local and remote host ids from the target board struct.
199 if [target_info exists sockethost] {
200 set debughost [target_info sockethost]
201 } else {
202 set debughost "localhost:"
203 }
204
205 # Extract the protocol
206 if [target_info exists gdb_protocol] {
207 set protocol [target_info gdb_protocol]
208 } else {
209 set protocol "remote"
210 }
211
212 set gdbserver [find_gdbserver]
213
214 # Loop till we find a free port.
215 while 1 {
216 # Export the host:port pair.
217 set gdbport $debughost$portnum
218
219 # Fire off the debug agent.
220 set gdbserver_command "$gdbserver"
221
222 # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be
223 # set to true already during gdbserver_start.
224 global gdbserver_reconnect_p
225 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
226 # GDB client could accidentally connect to a stale server.
227 # append gdbserver_command " --debug --once"
228 append gdbserver_command " --once"
229 }
230
231 if { $options != "" } {
232 append gdbserver_command " $options"
233 }
234
235 append gdbserver_command " :$portnum"
236
237 if { $arguments != "" } {
238 append gdbserver_command " $arguments"
239 }
240
241 set server_spawn_id [remote_spawn target $gdbserver_command]
242
243 # Wait for the server to open its TCP socket, so that GDB can connect.
244 expect {
245 -i $server_spawn_id
246 -notransfer
247 -re "Listening on" { }
248 -re "Can't bind address: Address already in use\\.\r\n" {
249 verbose -log "Port $portnum is already in use."
250 if ![target_info exists gdb,socketport] {
251 # Bump the port number to avoid the conflict.
252 wait -i $expect_out(spawn_id)
253 incr portnum
254 continue
255 }
256 }
257 }
258 break
259 }
260
261 # We can't just call close, because if gdbserver is local then that means
262 # that it will get a SIGHUP. Doing it this way could also allow us to
263 # get at the inferior's input or output if necessary, and means that we
264 # don't need to redirect output.
265 expect_background {
266 -i $server_spawn_id
267 full_buffer { }
268 eof {
269 # The spawn ID is already closed now (but not yet waited for).
270 wait -i $expect_out(spawn_id)
271 }
272 }
273
274 return [list $protocol $gdbport]
275 }
276
277 # Start a gdbserver process running SERVER_EXEC, and connect GDB
278 # to it. CHILD_ARGS are passed to the inferior.
279 #
280 # Returns the target protocol and socket to connect to.
281
282 proc gdbserver_spawn { child_args } {
283 set target_exec [gdbserver_download_current_prog]
284
285 # Fire off the debug agent. This flavour of gdbserver takes as
286 # arguments the port information, the name of the executable file to
287 # be debugged, and any arguments.
288 set arguments "$target_exec"
289 if { $child_args != "" } {
290 append arguments " $child_args"
291 }
292 return [gdbserver_start "" $arguments]
293 }
294
295 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
296 # to it. Return 0 on success, or non-zero on failure.
297
298 proc gdbserver_run { child_args } {
299 global gdbserver_protocol
300 global gdbserver_gdbport
301
302 # Kill anything running before we try to start gdbserver, in case
303 # we are sharing a serial connection.
304 global gdb_prompt
305 send_gdb "kill\n"
306 gdb_expect 120 {
307 -re "Kill the program being debugged. .y or n. $" {
308 send_gdb "y\n"
309 verbose "\t\tKilling previous program being debugged"
310 exp_continue
311 }
312 -re "$gdb_prompt $" {
313 # OK.
314 }
315 }
316
317 set res [gdbserver_spawn $child_args]
318 set gdbserver_protocol [lindex $res 0]
319 set gdbserver_gdbport [lindex $res 1]
320
321 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
322 }
323
324 # Reconnect to the previous gdbserver session.
325
326 proc gdbserver_reconnect { } {
327 global gdbserver_protocol
328 global gdbserver_gdbport
329
330 global gdbserver_reconnect_p;
331 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
332 error "gdbserver_reconnect_p is not set before gdbserver_reconnect"
333 return 0
334 }
335
336 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
337 }
338
339 # Start and connect to a gdbserver in extended mode.
340 proc gdbserver_start_extended { } {
341 set res [gdbserver_start "--multi" ""]
342 set gdbserver_protocol "extended-[lindex $res 0]"
343 set gdbserver_gdbport [lindex $res 1]
344
345 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
346 }
This page took 0.040625 seconds and 5 git commands to generate.