testsuite: Introduce $inferior_spawn_id
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdbserver-support.exp
1 # Copyright 2000-2015 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file is based on config/gdbserver.exp, which was written by
17 # Michael Snyder (msnyder@redhat.com).
18
19 #
20 # To be addressed or set in your baseboard config file:
21 #
22 # set_board_info gdb_protocol "remote"
23 # Unles you have a gdbserver that uses a different protocol...
24 # After GDB starts you should check global $gdbserver_protocol instead as
25 # the testfile may force a specific different target protocol itself.
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 # After GDB starts you should check global $gdbserver_gdbport for the
40 # real port used. It is not useful if $gdbserver_reconnect_p was not set.
41 #
42
43 #
44 # gdb_target_cmd
45 # Send gdb the "target" command
46 #
47 proc gdb_target_cmd { targetname serialport } {
48 global gdb_prompt
49
50 set serialport_re [string_to_regexp $serialport]
51 for {set i 1} {$i <= 3} {incr i} {
52 send_gdb "target $targetname $serialport\n"
53 gdb_expect 60 {
54 -re "A program is being debugged already.*ill it.*y or n. $" {
55 send_gdb "y\n"
56 exp_continue
57 }
58 -re "unknown host.*$gdb_prompt" {
59 verbose "Couldn't look up $serialport"
60 }
61 -re "Couldn't establish connection to remote.*$gdb_prompt $" {
62 verbose "Connection failed"
63 }
64 -re "Remote MIPS debugging.*$gdb_prompt" {
65 verbose "Set target to $targetname"
66 return 0
67 }
68 -re "Remote debugging using .*$serialport_re.*$gdb_prompt $" {
69 verbose "Set target to $targetname"
70 return 0
71 }
72 -re "Remote debugging using stdio.*$gdb_prompt $" {
73 verbose "Set target to $targetname"
74 return 0
75 }
76 -re "Remote target $targetname connected to.*$gdb_prompt $" {
77 verbose "Set target to $targetname"
78 return 0
79 }
80 -re "Connected to.*$gdb_prompt $" {
81 verbose "Set target to $targetname"
82 return 0
83 }
84 -re "Ending remote.*$gdb_prompt $" { }
85 -re "Connection refused.*$gdb_prompt $" {
86 verbose "Connection refused by remote target. Pausing, and trying again."
87 sleep 30
88 continue
89 }
90 -re "Timeout reading from remote system.*$gdb_prompt $" {
91 verbose "Got timeout error from gdb."
92 }
93 -notransfer -re "Remote debugging using .*\r\n> $" {
94 # We got an unexpected prompt while creating the target.
95 # Leave it there for the test to diagnose.
96 return 1
97 }
98 timeout {
99 send_gdb "\ 3"
100 break
101 }
102 }
103 }
104 return 1
105 }
106
107
108 global portnum
109 set portnum "2345"
110
111 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
112
113 proc find_gdbserver { } {
114 global GDB
115 global GDBSERVER
116
117 if [info exists GDBSERVER] {
118 return ${GDBSERVER}
119 }
120
121 if [target_info exists gdb_server_prog] {
122 return [target_info gdb_server_prog]
123 }
124
125 set gdbserver "${GDB}server"
126 if { [file isdirectory $gdbserver] } {
127 append gdbserver "/gdbserver"
128 }
129
130 if { [file executable $gdbserver] } {
131 return $gdbserver
132 }
133
134 return ""
135 }
136
137 # Return non-zero if we should skip gdbserver-specific tests.
138
139 proc skip_gdbserver_tests { } {
140 if { [find_gdbserver] == "" } {
141 return 1
142 }
143
144 return 0
145 }
146
147 # Download the currently loaded program to the target if necessary.
148 # Return the target system filename.
149 # NOTE: This was named "gdbserver_download", but that collides with the
150 # dejagnu "download" API function when using load_generic_config "gdbserver".
151
152 proc gdbserver_download_current_prog { } {
153 global gdbserver_host_exec
154 global gdbserver_host_mtime
155 global gdbserver_server_exec
156 global last_loaded_file
157
158 set host_exec $last_loaded_file
159
160 # If we already downloaded a file to the target, see if we can reuse it.
161 set reuse 0
162 if { [info exists gdbserver_server_exec] } {
163 set reuse 1
164
165 # If the file has changed, we can not.
166 if { $host_exec != $gdbserver_host_exec } {
167 set reuse 0
168 }
169
170 # If the mtime has changed, we can not.
171 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
172 set reuse 0
173 }
174 }
175
176 if { $reuse == 0 } {
177 set gdbserver_host_exec $host_exec
178 set gdbserver_host_mtime [file mtime $host_exec]
179 if [is_remote target] {
180 set gdbserver_server_exec [gdb_download $host_exec]
181 } else {
182 set gdbserver_server_exec $host_exec
183 }
184 }
185
186 return $gdbserver_server_exec
187 }
188
189 # Default routine to compute the argument to "target remote".
190
191 proc gdbserver_default_get_remote_address { host port } {
192 # Historically HOST included the trailing ":".
193 # To avoid breaking any board files out there we leave things alone.
194 return "$host$port"
195 }
196
197 # Default routine to compute the "comm" argument for gdbserver.
198
199 proc gdbserver_default_get_comm_port { port } {
200 return ":$port"
201 }
202
203 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
204 # The port will be filled in between them automatically.
205 #
206 # Returns the target protocol and socket to connect to.
207
208 proc gdbserver_start { options arguments } {
209 global portnum
210
211 # Port id -- either specified in baseboard file, or managed here.
212 if [target_info exists gdb,socketport] {
213 set portnum [target_info gdb,socketport]
214 } else {
215 # Bump the port number to avoid conflicts with hung ports.
216 incr portnum
217 }
218
219 # Extract the local and remote host ids from the target board struct.
220 if [target_info exists sockethost] {
221 set debughost [target_info sockethost]
222 } else {
223 set debughost "localhost:"
224 }
225
226 # Some boards use a different value for the port that is passed to
227 # gdbserver and the port that is passed to the "target remote" command.
228 # One example is the stdio gdbserver support.
229 if [target_info exists gdb,get_remote_address] {
230 set get_remote_address [target_info gdb,get_remote_address]
231 } else {
232 set get_remote_address gdbserver_default_get_remote_address
233 }
234 if [target_info exists gdbserver,get_comm_port] {
235 set get_comm_port [target_info gdbserver,get_comm_port]
236 } else {
237 set get_comm_port gdbserver_default_get_comm_port
238 }
239
240 # Extract the protocol
241 if [target_info exists gdb_protocol] {
242 set protocol [target_info gdb_protocol]
243 } else {
244 set protocol "remote"
245 }
246
247 set gdbserver [find_gdbserver]
248
249 # Loop till we find a free port.
250 while 1 {
251 # Fire off the debug agent.
252 set gdbserver_command "$gdbserver"
253
254 # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be
255 # set to true already during gdbserver_start.
256 global gdbserver_reconnect_p
257 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
258 # GDB client could accidentally connect to a stale server.
259 # append gdbserver_command " --debug --once"
260 append gdbserver_command " --once"
261 }
262
263 if { $options != "" } {
264 append gdbserver_command " $options"
265 }
266 if { $portnum != "" } {
267 append gdbserver_command " [$get_comm_port $portnum]"
268 }
269 if { $arguments != "" } {
270 append gdbserver_command " $arguments"
271 }
272
273 global server_spawn_id
274 set server_spawn_id [remote_spawn target $gdbserver_command]
275
276 # GDBserver doesn't do inferior I/O through GDB. But we can
277 # talk to the program using GDBserver's tty instead.
278 global inferior_spawn_id
279 set inferior_spawn_id $server_spawn_id
280
281 # Wait for the server to open its TCP socket, so that GDB can connect.
282 expect {
283 -i $server_spawn_id
284 -timeout 120
285 -notransfer
286 -re "Listening on" { }
287 -re "Can't bind address: Address already in use\\.\r\n" {
288 verbose -log "Port $portnum is already in use."
289 if ![target_info exists gdb,socketport] {
290 # Bump the port number to avoid the conflict.
291 wait -i $expect_out(spawn_id)
292 incr portnum
293 continue
294 }
295 }
296 timeout {
297 error "Timeout waiting for gdbserver response."
298 }
299 }
300 break
301 }
302
303 return [list $protocol [$get_remote_address $debughost $portnum]]
304 }
305
306 # Start a gdbserver process running SERVER_EXEC, and connect GDB
307 # to it. CHILD_ARGS are passed to the inferior.
308 #
309 # Returns the target protocol and socket to connect to.
310
311 proc gdbserver_spawn { child_args } {
312 set target_exec [gdbserver_download_current_prog]
313
314 # Fire off the debug agent. This flavour of gdbserver takes as
315 # arguments the port information, the name of the executable file to
316 # be debugged, and any arguments.
317 set arguments "$target_exec"
318 if { $child_args != "" } {
319 append arguments " $child_args"
320 }
321 return [gdbserver_start "" $arguments]
322 }
323
324 # Close the GDBserver connection.
325
326 proc close_gdbserver {} {
327 global server_spawn_id inferior_spawn_id
328
329 # We can't just call close, because if gdbserver is local then that means
330 # that it will get a SIGHUP. Doing it this way could also allow us to
331 # get at the inferior's input or output if necessary, and means that we
332 # don't need to redirect output.
333
334 if {![info exists server_spawn_id]} {
335 return
336 }
337
338 verbose "Quitting GDBserver"
339
340 catch "close -i $server_spawn_id"
341 catch "wait -i $server_spawn_id"
342 unset server_spawn_id
343 unset inferior_spawn_id
344 }
345
346 # Hook into GDB exit, and close GDBserver.
347
348 if { [info procs gdbserver_gdb_exit] == "" } {
349 rename gdb_exit gdbserver_orig_gdb_exit
350 }
351 proc gdb_exit {} {
352 global gdb_spawn_id server_spawn_id inferior_spawn_id
353 global gdb_prompt
354
355 if {[info exists gdb_spawn_id] && [info exists server_spawn_id]} {
356 send_gdb "monitor exit\n";
357 gdb_expect {
358 -re "$gdb_prompt $" {
359 exp_continue
360 }
361 -i "$server_spawn_id" eof {
362 wait -i $expect_out(spawn_id)
363 unset server_spawn_id
364 unset inferior_spawn_id
365 }
366 }
367 }
368 close_gdbserver
369
370 gdbserver_orig_gdb_exit
371 }
372
373 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
374 # to it. Return 0 on success, or non-zero on failure: 2 if gdbserver
375 # failed to start or 1 if we couldn't connect to it.
376
377 proc gdbserver_run { child_args } {
378 global gdbserver_protocol
379 global gdbserver_gdbport
380
381 # Kill anything running before we try to start gdbserver, in case
382 # we are sharing a serial connection.
383 global gdb_prompt
384 send_gdb "kill\n"
385 gdb_expect 120 {
386 -re "Kill the program being debugged. .y or n. $" {
387 send_gdb "y\n"
388 verbose "\t\tKilling previous program being debugged"
389 exp_continue
390 }
391 -re "$gdb_prompt $" {
392 # OK.
393 }
394 }
395
396 if { [catch { gdbserver_spawn $child_args } res] == 1 } {
397 perror $res
398 return 2
399 }
400 set gdbserver_protocol [lindex $res 0]
401 set gdbserver_gdbport [lindex $res 1]
402
403 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
404 }
405
406 # Reconnect to the previous gdbserver session.
407
408 proc gdbserver_reconnect { } {
409 global gdbserver_protocol
410 global gdbserver_gdbport
411
412 global gdbserver_reconnect_p
413 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
414 error "gdbserver_reconnect_p is not set before gdbserver_reconnect"
415 return 0
416 }
417
418 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
419 }
420
421 # Start and connect to a gdbserver in extended mode. Note this frobs
422 # $gdbserver_protocol, so should be used only from a board that
423 # usually connects in target remote mode.
424 proc gdbserver_start_extended { } {
425 global gdbserver_protocol
426 global gdbserver_gdbport
427 global use_gdb_stub
428
429 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
430 perror $res
431 return 2
432 }
433 set gdbserver_protocol [lindex $res 0]
434 if { [string first "extended-" $gdbserver_protocol] != 0} {
435 set gdbserver_protocol "extended-$gdbserver_protocol"
436 }
437 set gdbserver_gdbport [lindex $res 1]
438
439 # Even if the board file is testing with target remote, our caller
440 # wants to test against gdbserver in extended-remote mode. Make sure to
441 # disable stub-like techniques.
442 set use_gdb_stub 0
443
444 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
445 }
446
447 # Start and connect to a gdbserver in extended/multi mode. Unlike
448 # gdbserver_start_extended, this does not frob $gdbserver_protocol.
449
450 proc gdbserver_start_multi { } {
451 global gdbserver_protocol
452 global gdbserver_gdbport
453
454 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
455 perror $res
456 return 2
457 }
458 set gdbserver_protocol [lindex $res 0]
459 set gdbserver_gdbport [lindex $res 1]
460
461 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
462 }
463
464 # Start a gdbserver process in multi/extended mode, and have GDB
465 # connect to it (MI version). Return 0 on success, or non-zero on
466 # failure.
467
468 proc mi_gdbserver_start_multi { } {
469 global gdbserver_protocol
470 global gdbserver_gdbport
471
472 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
473 perror $res
474 return 2
475 }
476 set gdbserver_protocol [lindex $res 0]
477 set gdbserver_gdbport [lindex $res 1]
478
479 return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
480 }
This page took 0.050023 seconds and 5 git commands to generate.