| #!/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. |
| ## |
| ## TROUBLESHOOTING TIPS: |
| ## |
| ## - Remember to use "fx set-device" when working with multiple devices. |
| ## - This scripts by default will mute the SSH connection stdout/stderr, so any |
| ## errors triggered by it won't appear. Use the --verbose-agent flag to see |
| ## the output. |
| ## - This scripts uses the tool "nc" for testing TCP connections. Check that it |
| ## is in $PATH and that it works. |
| ## |
| ## Usage: fx debug [(--port|-p) <PORT>] [(--verbose-agent|-va)] |
| ## |
| ## --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. |
| ## |
| ## Flags after -- are parsed by zxdb. See zxdb's documentation for more |
| ## details. |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../lib/vars.sh || exit $? |
| fx-config-read |
| source "${FUCHSIA_DIR}/buildtools/vars.sh" |
| |
| # Defaults. |
| port= |
| core= |
| unwind_flag= |
| agent_out="/dev/null" |
| |
| # Flag parsing. |
| while [[ $# -gt 0 ]]; do |
| case "$1" in |
| --help|-h) |
| fx-command-help |
| exit 0 |
| ;; |
| --port|-p) |
| shift |
| port="$1" |
| ;; |
| --verbose-agent|-va) |
| agent_out="/dev/stdout" |
| ;; |
| --core|-c) |
| arg=$1 |
| shift |
| core="$1" |
| if [[ -z "${core}" ]]; then |
| echo "${arg} takes an argument" |
| exit 1 |
| fi |
| ;; |
| --core=*) |
| core="${1:7}" |
| ;; |
| --unwind) |
| shift |
| unwind_flag="--unwind=$1" |
| ;; |
| --unwind=*) |
| unwind_flag="--unwind=${1:9}" |
| ;; |
| --) |
| shift |
| break # Remaining flags are passed to zxdb |
| ;; |
| *) |
| echo "Invalid flag $1" |
| exit 1 |
| esac |
| shift |
| done |
| |
| if [[ -n "${port}" && -n "${core}" ]]; then |
| echo "Specifying both a port and a core doesn't make sense" |
| exit 1 |
| fi |
| |
| if [[ -n "${core}" && ! -f "${core}" ]]; then |
| echo "Cannot find file ${core}" |
| exit 1 |
| fi |
| |
| if [[ -z "${port}" ]]; then |
| port=2345 |
| fi |
| |
| connect_ops= |
| |
| if [[ -z "${core}" ]]; then |
| debug_agent_pkg="fuchsia-pkg://fuchsia.com/debug_agent#meta/debug_agent.cmx" |
| |
| fx_ssh_pid="" |
| trap 'kill ${fx_ssh_pid}' EXIT |
| |
| # Get the defaulted device address. |
| target=$(get-fuchsia-device-addr) |
| if [[ -z "${target}" ]]; then |
| fx-error "Could not find fuchsia device" "$(get-device-name)" |
| exit 1 |
| 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 -e "Attempting to start the Debug Agent." |
| fx-command-run "ssh" "${target}" "run ${debug_agent_pkg} --port=${port} ${unwind_flag}" > "${agent_out}" 2>&1 & |
| 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. |
| if nc -w5 -6 -z "${target}" "${port}"; 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 |
| fx-error "Timed out trying to find the Debug Agent. Exiting." |
| exit 1 |
| fi |
| sleep 1 |
| done |
| |
| connect_ops="--connect [${target}]:${port}" |
| else |
| connect_ops="--core ${core}" |
| fi |
| |
| # We start the client with the flag that tells it to quit the agent when zxdb quits. |
| "${FUCHSIA_BUILD_DIR}/host_x64/zxdb" \ |
| ${connect_ops} \ |
| "--quit-agent-on-exit" \ |
| "-s" "${FUCHSIA_BUILD_DIR}" \ |
| "-s" "${ZIRCON_BUILDROOT}" \ |
| "-s" "${BUILDTOOLS_CLANG_DIR}/lib/debug" \ |
| "--symbol-cache" ~ \ |
| "--symbol-server" "gs://fuchsia-infra-debug-symbols" \ |
| $@ |