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