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