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