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