| #!/bin/bash |
| # Copyright 2018 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| ### Run the debug agent on target and connect to it with zxdb. |
| |
| ## Starts the debug agent on the proposed target and automatically connect zxdb |
| ## to it. Will close the debug agent on exit. |
| ## |
| ## Usage: fx debug [(--port|-p) <PORT>] [(--verbose-agent|-va)] [--] [TARGET] |
| ## |
| ## --port Port the debug agent will be listening on. Will use 2345 |
| ## by default. |
| ## --verbose-agent Whether the debug agent's stdout/stderr should be shown. |
| ## Useful for debugging the debugger. Yo' dawg. |
| ## |
| ## TARGET The hostname of the target to connect to. Initially, it |
| ## will attempt to pass it to netaddr to check if it is a |
| ## fuchsia IP word encoding. If not, it will attempt to use |
| ## it directly as a hostname. |
| ## Can be ommited if there is only one target. |
| |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/vars.sh |
| fx-config-read |
| |
| # Defaults. |
| port=2345 |
| agent_out="/dev/null" |
| |
| # Flag parsing. |
| while [[ "$1" =~ ^- ]]; do |
| case "$1" in |
| --help|-h) |
| fx-command-help |
| exit 0 |
| ;; |
| --port|-p) |
| shift |
| port="$1" |
| ;; |
| --verbose-agent|-va) |
| shift |
| agent_out="" |
| break |
| ;; |
| --) |
| break |
| ;; |
| *) |
| break |
| esac |
| shift |
| done |
| |
| # The target is the first non-flag argument. |
| target="$1" |
| |
| # Initially, We attempt to see if this is a netaddr target name. If not, we're |
| # going to try it directly as a hostname. |
| netaddr_target=$(fx-command-run "netaddr" "--fuchsia" "${target}") |
| if [[ -z ${netaddr_target} ]]; then |
| # No valid netaddr and no explicit target. |
| if [[ -z ${target} ]]; then |
| echo -e "Could not get a valid target. Exiting." |
| exit 1 |
| fi |
| echo "Target is not a Fuchsia target name. Attempting to use it as hostname." |
| else |
| # netaddr generated a valid target. Using that one. |
| target="${netaddr_target}" |
| fi |
| |
| # Leave the SSH connection open. Will be closed on script end. |
| # We branch out on whether the user used the verbose-agent flag. If so, we |
| # redirect the debug agent output to /dev/null. |
| echo "Attempting to start the Debug Agent." |
| if [[ -z "${agent_out}" ]]; then |
| (fx-command-run "ssh" "${target}" "debug_agent --port=${port}" &) & |
| else |
| (fx-command-run "ssh" "${target}" "debug_agent --port=${port}" &) > "${agent_out}" 2>&1 & |
| fi |
| fx_ssh_pid="$!" |
| |
| # We wait until the debug agent is listening on the given port. We use NC to |
| # attemp a tcp connection. This will actually go all the way with the handshake, |
| # so the debug agent will think initially that NC is a client. But then it will |
| # close the connection and receive the actual client's connection and work fine. |
| try_count=0 |
| max_tries=10 |
| echo "Waiting for the Debug Agent to start." |
| while true; do |
| # Use NC to test if the port is open and the debug agent is listening. |
| nc -w5 -6 -z ${target} ${port} |
| # If successful, we proceed to connect. |
| if [[ "$?" -eq 0 ]]; then |
| break |
| fi |
| |
| # Otherwise, we count and check if we need to exit. |
| try_count=$(expr "${try_count}" + 1) |
| if [[ "${try_count}" -gt "${max_tries}" ]]; then |
| echo -e "Timed out trying to find the Debug Agent. Exiting." |
| echo -e "Attempting to kill SSH connection." |
| kill "${fx_ssh_pid}" |
| exit 1 |
| fi |
| sleep 1 |
| done |
| |
| # We start the client with the flag that tells it to quit the agent when zxdb |
| # quits. |
| echo "Connection found. Starting ZXDB." |
| "${FUCHSIA_BUILD_DIR}/host_x64/zxdb" "--connect" "[${target}]:${port}" "--quit-agent-on-exit" |