21ec418236cf0eade2669f5ba09d3ed13cda3015
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / gdbserver-support.exp
1 # Copyright 2000-2020 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_ext
45 # Send gdb the "target" command. Returns 0 on success, 1 on failure, 2 on
46 # unsupported.
47 # If specified, then ADDITIONAL_TEXT must match the text that comes after
48 # the connection message in order for the procedure to succeed.
49 #
50 proc gdb_target_cmd_ext { targetname serialport {additional_text ""} } {
51 global gdb_prompt
52
53 set serialport_re [string_to_regexp $serialport]
54 for {set i 1} {$i <= 3} {incr i} {
55 send_gdb "target $targetname $serialport\n"
56 gdb_expect 60 {
57 -re "A program is being debugged already.*ill it.*y or n. $" {
58 send_gdb "y\n"
59 exp_continue
60 }
61 -re "unknown host.*$gdb_prompt" {
62 verbose "Couldn't look up $serialport"
63 }
64 -re "Couldn't establish connection to remote.*$gdb_prompt $" {
65 verbose "Connection failed"
66 }
67 -re "Remote MIPS debugging.*$additional_text.*$gdb_prompt" {
68 verbose "Set target to $targetname"
69 return 0
70 }
71 -re "Remote debugging using .*$serialport_re.*$additional_text.*$gdb_prompt $" {
72 verbose "Set target to $targetname"
73 return 0
74 }
75 -re "Remote debugging using stdio.*$additional_text.*$gdb_prompt $" {
76 verbose "Set target to $targetname"
77 return 0
78 }
79 -re "Remote target $targetname connected to.*$additional_text.*$gdb_prompt $" {
80 verbose "Set target to $targetname"
81 return 0
82 }
83 -re "Connected to.*$additional_text.*$gdb_prompt $" {
84 verbose "Set target to $targetname"
85 return 0
86 }
87 -re "Ending remote.*$gdb_prompt $" { }
88 -re "Connection refused.*$gdb_prompt $" {
89 verbose "Connection refused by remote target. Pausing, and trying again."
90 sleep 30
91 continue
92 }
93 -re "Timeout reading from remote system.*$gdb_prompt $" {
94 verbose "Got timeout error from gdb."
95 }
96 -notransfer -re "Remote debugging using .*\r\n> $" {
97 # We got an unexpected prompt while creating the target.
98 # Leave it there for the test to diagnose.
99 return 1
100 }
101 -re ": Network is unreachable.\r\n.*$gdb_prompt $" {
102 return 2
103 }
104 timeout {
105 send_gdb "\ 3"
106 break
107 }
108 }
109 }
110 return 1
111 }
112
113 # Like gdb_target_cmd_ext, but returns 0 on success, 1 on failure.
114
115 proc gdb_target_cmd { args } {
116 set res [eval gdb_target_cmd_ext $args]
117 return [expr $res == 0 ? 0 : 1]
118 }
119
120 global portnum
121 set portnum "2345"
122
123 # Locate the gdbserver binary. Returns "" if gdbserver could not be found.
124
125 proc find_gdbserver { } {
126 global GDB
127 global GDBSERVER
128
129 if [info exists GDBSERVER] {
130 return ${GDBSERVER}
131 }
132
133 if [target_info exists gdb_server_prog] {
134 return [target_info gdb_server_prog]
135 }
136
137 set gdbserver "${GDB}server"
138 if { [file isdirectory $gdbserver] } {
139 append gdbserver "/gdbserver"
140 }
141
142 if { [file executable $gdbserver] } {
143 return $gdbserver
144 }
145
146 return ""
147 }
148
149 # Return non-zero if we should skip gdbserver-specific tests.
150
151 proc skip_gdbserver_tests { } {
152 if { [find_gdbserver] == "" } {
153 return 1
154 }
155
156 # If GDB is lack of XML support, and targets, like arm, have
157 # multiple target descriptions, GDB doesn't know which target
158 # description GDBserver uses, and may fail to parse 'g' packet
159 # after connection.
160 if { [gdb_skip_xml_test]
161 && ([istarget "arm*-*-linux*"]
162 || [istarget "mips*-*-linux*"]
163 || [istarget "powerpc*-*-linux*"]
164 || [istarget "s390*-*-linux*"]
165 || [istarget "x86_64-*-linux*"]
166 || [istarget "i\[34567\]86-*-linux*"]) } {
167 return 1
168 }
169
170 return 0
171 }
172
173 # Download the currently loaded program to the target if necessary.
174 # Return the target system filename.
175 # NOTE: This was named "gdbserver_download", but that collides with the
176 # dejagnu "download" API function when using load_generic_config "gdbserver".
177
178 proc gdbserver_download_current_prog { } {
179 global gdbserver_host_exec
180 global gdbserver_host_mtime
181 global gdbserver_server_exec
182 global last_loaded_file
183
184 if { ![info exists last_loaded_file] } {
185 return ""
186 }
187
188 set host_exec $last_loaded_file
189
190 # If we already downloaded a file to the target, see if we can reuse it.
191 set reuse 0
192 if { [info exists gdbserver_server_exec] } {
193 set reuse 1
194
195 # If the file has changed, we can not.
196 if { $host_exec != $gdbserver_host_exec } {
197 set reuse 0
198 }
199
200 # If the mtime has changed, we can not.
201 if { [file mtime $host_exec] != $gdbserver_host_mtime } {
202 set reuse 0
203 }
204 }
205
206 if { $reuse == 0 } {
207 set gdbserver_host_exec $host_exec
208 set gdbserver_host_mtime [file mtime $host_exec]
209 set gdbserver_server_exec [gdb_remote_download target $host_exec]
210 }
211
212 return $gdbserver_server_exec
213 }
214
215 # Default routine to compute the argument to "target remote".
216
217 proc gdbserver_default_get_remote_address { host port } {
218 # Historically HOST included the trailing ":".
219 # To avoid breaking any board files out there we leave things alone.
220 return "$host$port"
221 }
222
223 # Default routine to compute the "comm" argument for gdbserver.
224
225 proc gdbserver_default_get_comm_port { port } {
226 return "$port"
227 }
228
229 # Start a gdbserver process with initial OPTIONS and trailing ARGUMENTS.
230 # The port will be filled in between them automatically.
231 #
232 # Returns the target protocol and socket to connect to.
233
234 proc gdbserver_start { options arguments } {
235 global portnum
236 global GDB_TEST_SOCKETHOST
237
238 # Port id -- either specified in baseboard file, or managed here.
239 if [target_info exists gdb,socketport] {
240 set portnum [target_info gdb,socketport]
241 } else {
242 # Bump the port number to avoid conflicts with hung ports.
243 incr portnum
244 }
245
246 # Extract the local and remote host ids from the target board struct.
247 if { [info exists GDB_TEST_SOCKETHOST] } {
248 # The user is not supposed to provide a port number, just a
249 # hostname/address, therefore we add the trailing ":" here.
250 set debughost "${GDB_TEST_SOCKETHOST}:"
251 # Escape open and close square brackets.
252 set debughost_tmp [string map { [ \\[ ] \\] } $debughost]
253 # We need a "gdbserver" version of the debughost, which will
254 # have the possible connection prefix stripped. This is
255 # because gdbserver currently doesn't recognize the prefixes.
256 regsub -all "^\(tcp:|udp:|tcp4:|udp4:|tcp6:|udp6:\)" $debughost_tmp "" debughost_gdbserver
257 } elseif [target_info exists sockethost] {
258 set debughost [target_info sockethost]
259 set debughost_gdbserver $debughost
260 } else {
261 set debughost "localhost:"
262 set debughost_gdbserver $debughost
263 }
264
265 # Some boards use a different value for the port that is passed to
266 # gdbserver and the port that is passed to the "target remote" command.
267 # One example is the stdio gdbserver support.
268 if [target_info exists gdb,get_remote_address] {
269 set get_remote_address [target_info gdb,get_remote_address]
270 } else {
271 set get_remote_address gdbserver_default_get_remote_address
272 }
273 if [target_info exists gdbserver,get_comm_port] {
274 set get_comm_port [target_info gdbserver,get_comm_port]
275 } else {
276 set get_comm_port gdbserver_default_get_comm_port
277 }
278
279 # Extract the protocol
280 if [target_info exists gdb_protocol] {
281 set protocol [target_info gdb_protocol]
282 } else {
283 set protocol "remote"
284 }
285
286 set gdbserver [find_gdbserver]
287
288 # Loop till we find a free port.
289 while 1 {
290 # Fire off the debug agent.
291 set gdbserver_command "$gdbserver"
292
293 # If gdbserver_reconnect will be called $gdbserver_reconnect_p must be
294 # set to true already during gdbserver_start.
295 global gdbserver_reconnect_p
296 global srcdir
297 global subdir
298 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
299 # GDB client could accidentally connect to a stale server.
300 append gdbserver_command " --once"
301 }
302
303 # Enable debug if set.
304 if [gdbserver_debug_enabled] {
305 global gdbserverdebug
306 set enabled 0
307 foreach entry [split $gdbserverdebug ,] {
308 switch -- $entry {
309 "debug" {
310 append gdbserver_command " --debug"
311 set enabled 1
312 }
313 "remote" {
314 append gdbserver_command " --remote-debug"
315 set enabled 1
316 }
317 }
318 }
319 # Ensure debugfile is only added if something has been enabled
320 if { $enabled } {
321 set debugfile [standard_output_file gdbserver.debug]
322 append gdbserver_command " --debug-file=$debugfile"
323 }
324 }
325
326 if { $options != "" } {
327 append gdbserver_command " $options"
328 }
329 if { $debughost_gdbserver != "" } {
330 append gdbserver_command " $debughost_gdbserver"
331 }
332 if { $portnum != "" } {
333 if { $debughost_gdbserver == "" } {
334 append gdbserver_command " "
335 }
336 append gdbserver_command "[$get_comm_port $portnum]"
337 }
338 if { $arguments != "" } {
339 append gdbserver_command " $arguments"
340 }
341
342 gdbserver_write_cmd_file $gdbserver_command
343
344 global server_spawn_id
345 set server_spawn_id [remote_spawn target $gdbserver_command]
346
347 # GDBserver doesn't do inferior I/O through GDB. But we can
348 # talk to the program using GDBserver's tty instead.
349 global inferior_spawn_id
350 set inferior_spawn_id $server_spawn_id
351
352 # Wait for the server to open its TCP socket, so that GDB can connect.
353 expect {
354 -i $server_spawn_id
355 -timeout 120
356 -notransfer
357 -re "Listening on" { }
358 -re "Can't (bind address|listen on socket): Address already in use\\.\r\n" {
359 verbose -log "Port $portnum is already in use."
360 if ![target_info exists gdb,socketport] {
361 # Bump the port number to avoid the conflict.
362 wait -i $expect_out(spawn_id)
363 incr portnum
364 continue
365 }
366 }
367 -re ".*: cannot resolve name: .*\r\n" {
368 error "gdbserver cannot resolve name."
369 }
370 timeout {
371 error "Timeout waiting for gdbserver response."
372 }
373 }
374 break
375 }
376
377 return [list $protocol [$get_remote_address $debughost $portnum]]
378 }
379
380 # Start a gdbserver process running SERVER_EXEC, and connect GDB
381 # to it. CHILD_ARGS are passed to the inferior.
382 #
383 # Returns the target protocol and socket to connect to.
384
385 proc gdbserver_spawn { child_args } {
386 set target_exec [gdbserver_download_current_prog]
387
388 # Fire off the debug agent. This flavour of gdbserver takes as
389 # arguments the port information, the name of the executable file to
390 # be debugged, and any arguments.
391 set arguments "$target_exec"
392 if { $child_args != "" } {
393 append arguments " $child_args"
394 }
395 return [gdbserver_start "" $arguments]
396 }
397
398 # Close the GDBserver connection.
399
400 proc close_gdbserver {} {
401 global server_spawn_id
402
403 # We can't just call close, because if gdbserver is local then that means
404 # that it will get a SIGHUP. Doing it this way could also allow us to
405 # get at the inferior's input or output if necessary, and means that we
406 # don't need to redirect output.
407
408 if {![info exists server_spawn_id]} {
409 return
410 }
411
412 verbose "Quitting GDBserver"
413
414 catch "close -i $server_spawn_id"
415 catch "wait -i $server_spawn_id"
416 unset server_spawn_id
417 }
418
419 # Hook into GDB exit, and close GDBserver. We must load this
420 # explicitly here, and rename the procedures we want to override.
421 load_lib mi-support.exp
422
423 if { [info procs gdbserver_orig_gdb_exit] == "" } {
424 rename gdb_exit gdbserver_orig_gdb_exit
425 rename mi_gdb_exit gdbserver_orig_mi_gdb_exit
426 }
427
428 proc gdbserver_gdb_exit { is_mi } {
429 global gdb_spawn_id server_spawn_id
430 global gdb_prompt
431 global gdbserver_reconnect_p
432
433 # Leave GDBserver running if we're exiting GDB in order to
434 # reconnect to the same instance of GDBserver again.
435 if {[info exists gdbserver_reconnect_p] && $gdbserver_reconnect_p} {
436 if { $is_mi } {
437 gdbserver_orig_mi_gdb_exit
438 } else {
439 gdbserver_orig_gdb_exit
440 }
441 return
442 }
443
444 if {[info exists gdb_spawn_id] && [info exists server_spawn_id]} {
445 # GDB may be terminated in an expected way or an unexpected way,
446 # but DejaGNU doesn't know that, so gdb_spawn_id isn't unset.
447 # Catch the exceptions.
448 catch {
449 if { $is_mi } {
450 set monitor_exit "-interpreter-exec console \"monitor exit\""
451 } else {
452 set monitor_exit "monitor exit"
453 }
454 send_gdb "$monitor_exit\n";
455 # We use expect rather than gdb_expect because
456 # we want to suppress printing exception messages, otherwise,
457 # remote_expect, invoked by gdb_expect, prints the exceptions.
458 expect {
459 -i "$gdb_spawn_id" -re "$gdb_prompt $" {
460 exp_continue
461 }
462 -i "$server_spawn_id" eof {
463 wait -i $expect_out(spawn_id)
464 unset server_spawn_id
465 }
466 }
467 }
468 }
469 close_gdbserver
470
471 if { $is_mi } {
472 gdbserver_orig_mi_gdb_exit
473 } else {
474 gdbserver_orig_gdb_exit
475 }
476 }
477
478 proc gdb_exit {} {
479 gdbserver_gdb_exit 0
480 }
481
482 proc mi_gdb_exit {} {
483 gdbserver_gdb_exit 1
484 }
485
486 # Start a gdbserver process running HOST_EXEC and pass CHILD_ARGS
487 # to it. Return 0 on success, or non-zero on failure: 2 if gdbserver
488 # failed to start or 1 if we couldn't connect to it.
489
490 proc gdbserver_run { child_args } {
491 global gdbserver_protocol
492 global gdbserver_gdbport
493
494 # Kill anything running before we try to start gdbserver, in case
495 # we are sharing a serial connection.
496 global gdb_prompt
497 send_gdb "kill\n" optional
498 gdb_expect 120 {
499 -re "Kill the program being debugged. .y or n. $" {
500 send_gdb "y\n"
501 verbose "\t\tKilling previous program being debugged"
502 exp_continue
503 }
504 -re "$gdb_prompt $" {
505 # OK.
506 }
507 }
508
509 if { [catch { gdbserver_spawn $child_args } res] == 1 } {
510 perror $res
511 return 2
512 }
513 set gdbserver_protocol [lindex $res 0]
514 set gdbserver_gdbport [lindex $res 1]
515
516 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
517 }
518
519 # Reconnect to the previous gdbserver session.
520
521 proc gdbserver_reconnect { } {
522 global gdbserver_protocol
523 global gdbserver_gdbport
524
525 global gdbserver_reconnect_p
526 if {![info exists gdbserver_reconnect_p] || !$gdbserver_reconnect_p} {
527 error "gdbserver_reconnect_p is not set before gdbserver_reconnect"
528 return 0
529 }
530
531 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
532 }
533
534 # Start gdbserver in extended mode with OPTIONS and connect to it. Note
535 # this frobs $gdbserver_protocol, so should be used only from a board
536 # that usually connects in target remote mode.
537 proc gdbserver_start_extended { {options ""} } {
538 global gdbserver_protocol
539 global gdbserver_gdbport
540 global use_gdb_stub
541
542 set gdbserver_options "--multi"
543
544 if { $options != "" } {
545 append gdbserver_options " $options"
546 }
547
548 if { [catch { gdbserver_start $gdbserver_options "" } res] == 1 } {
549 perror $res
550 return 2
551 }
552 set gdbserver_protocol [lindex $res 0]
553 if { [string first "extended-" $gdbserver_protocol] != 0} {
554 set gdbserver_protocol "extended-$gdbserver_protocol"
555 }
556 set gdbserver_gdbport [lindex $res 1]
557
558 # Even if the board file is testing with target remote, our caller
559 # wants to test against gdbserver in extended-remote mode. Make sure to
560 # disable stub-like techniques.
561 set use_gdb_stub 0
562
563 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
564 }
565
566 # Start and connect to a gdbserver in extended/multi mode. Unlike
567 # gdbserver_start_extended, this does not frob $gdbserver_protocol.
568
569 proc gdbserver_start_multi { } {
570 global gdbserver_protocol
571 global gdbserver_gdbport
572
573 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
574 perror $res
575 return 2
576 }
577 set gdbserver_protocol [lindex $res 0]
578 set gdbserver_gdbport [lindex $res 1]
579
580 return [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
581 }
582
583 # Start a gdbserver process in multi/extended mode, and have GDB
584 # connect to it (MI version). Return 0 on success, or non-zero on
585 # failure.
586
587 proc mi_gdbserver_start_multi { } {
588 global gdbserver_protocol
589 global gdbserver_gdbport
590
591 if { [catch { gdbserver_start "--multi" "" } res] == 1 } {
592 perror $res
593 return 2
594 }
595 set gdbserver_protocol [lindex $res 0]
596 set gdbserver_gdbport [lindex $res 1]
597
598 return [mi_gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
599 }
600
601 # Check if debugging is enabled for gdbserver.
602
603 proc gdbserver_debug_enabled { } {
604 global gdbserverdebug
605
606 # If not already read, get the debug setting from environment or board setting.
607 if ![info exists gdbserverdebug] {
608 global env
609 if [info exists env(GDBSERVER_DEBUG)] {
610 set gdbserverdebug $env(GDBSERVER_DEBUG)
611 } elseif [target_info exists gdbserver,debug] {
612 set gdbserverdebug [target_info gdbserver,debug]
613 } else {
614 return 0
615 }
616 }
617
618 # Expand the all option
619 if { $gdbserverdebug == "all" } {
620 set gdbserverdebug "debug,remote,replay"
621 }
622
623 # Ensure it is not empty.
624 return [expr { $gdbserverdebug != "" }]
625 }
626
627 # Write the command line used to invocate gdbserver to the cmd file.
628
629 proc gdbserver_write_cmd_file { cmdline } {
630 set logfile [standard_output_file_with_gdb_instance gdbserver.cmd]
631 set cmd_file [open $logfile w]
632 puts $cmd_file $cmdline
633 catch "close $cmd_file"
634 }
635
636 # Override gdb_debug_init so that we can set replay logging in GDB if required.
637 # Backup the original function so we can call it afterwards
638
639 rename gdb_debug_init _gdb_debug_init
640
641 proc gdb_debug_init { } {
642 global gdbserverdebug
643 global gdb_prompt
644
645 if [gdbserver_debug_enabled] {
646 foreach entry [split $gdbserverdebug ,] {
647 if { $entry == "replay" } {
648 set replayfile [standard_output_file_with_gdb_instance gdbserver.replay]
649 send_gdb "set remotelogfile $replayfile\n" optional
650 gdb_expect 10 {
651 -re "$gdb_prompt $" {}
652 }
653 }
654 }
655 }
656
657 # Now call the standard debug init function
658 _gdb_debug_init
659 }
This page took 0.042342 seconds and 3 git commands to generate.