blob: 44c5c94a099523557ace043946ee08a2f60c3ebb [file] [log] [blame]
#!/bin/bash
# Copyright 2019 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.
### Obtain and parse a bugreport from a connected target.
## Connects to a running target and obtains a bugreport that contains useful
## information for debugging a target.
##
## Information supplied includes:
## - Build information and annotations.
## - Kernel and System logs.
## - Inspect Data.
##
## Each element will be separated in its own report file within a supplied
## directory. If no directory is supplied, a unique one will be generated.
##
## Usage: fx bugreport [(--output-directory|-o)] <DIRECTORY>] -- <TARGET ARGS>
##
## --output-directory (Optional) Directory where the files will be stored.
## Must be an existing directory.
## --serial (Optional) Use serial channel instead of ssh.
##
## <TARGET ARGS> Arguments to be passed to the bugreport client within
## the target.
##
## NOTE: For the serial report to work correctly, it requires a clean channel
## (no other output). While this is often true, it is not assured. This
## means that the serial report is best effort only. If it fails, try
## running it again or use minimal.
# Load environment helpers.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../lib/vars.sh || exit $?
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../contrib/lib/serial.sh || exit $?
fx-config-read
# Flag Parsing.
output_dir=
is_output_dir_provided=false
use_serial=
while [[ $# -gt 0 ]]; do
case "$1" in
--help|-h)
fx-command-help
exit 0
;;
--output-directory|-o)
shift
output_dir="$1"
is_output_dir_provided=true
;;
--serial)
use_serial=true
;;
--)
shift
break # Remaining flags are passed on to the target.
;;
*)
fx-error "Invalid flag $1"
exit 1
esac
shift
done
# Check the validity of the output file.
if [[ "${is_output_dir_provided}" == "true" ]]; then
if [[ -z "${output_dir}" ]]; then
fx-error "No output directory provided."
exit 1
fi
if [[ ! -d "${output_dir}" ]]; then
fx-error "Path provided is not a directory."
exit 1
fi
fi
# Verify that we can obtain the target (ssh only).
if [[ -z "${use_serial}" ]]; then
target=$(get-fuchsia-device-addr)
if [[ -z "${target}" ]]; then
fx-error "Could not obtain target name."
exit 1
fi
fi
# If no output directory is supplied, generate a unique one according to time.
if [[ -z "${output_dir}" ]]; then
# Generate an output directory.
# Format is /tmp/bugreports/report_<YearMonthDay_HourMinuteSecond>
uuid=`date +"%Y%m%d_%H%M%S"`
output_dir="/tmp/bugreports/report_${uuid}"
mkdir -p "${output_dir}"
if [[ "$?" -ne 0 ]]; then
rm -rf "${output_dir}" # Just in case.
fx-error "Failed to create output directory ${output_dir}."
fx-error "Check errors or provide your own with --output-directory. See --help for more details."
exit 1
fi
fi
# At this point, we know that we have a valid output directory.
error_bringing_report=
tempfile="/tmp/__bugreport_temp"
if [[ -z "${use_serial}" ]]; then
# USING SSH.
# Obtain the bug report and store it in a temporary location.
fx-command-run "ssh" "${target}" "bugreport ${@}" > "${tempfile}"
if [ "$?" -ne 0 ]; then
fx-error "Could not obtain bugreport from target."
error_bringing_report=true
fi
else
# USING SERIAL.
get_serial_device
if [[ -z "${DEVICE}" ]]; then
fx-error "Could not obtain a serial device."
error_bringing_report=true
elif serial_port_unavailable "${DEVICE}"; then
fx-error "${DEVICE} is unavailable."
error_bringing_report=true
else
echo "Transfering data to host..."
send_serial_command "${DEVICE}" "${tempfile}" "run bugreport.cmx ${@}"
if [[ "$?" -ne 0 ]]; then
fx-error "Could not obtain bugreport from target."
error_bringing_report=true
else
# Do the adhoc serial cleanup.
# This is needed because the report file will have all the serial output,
# including the prompt and the actual "run bugreport.cmx" command.
# In order to have our process tool work, we need to clear it to be just the
# json content.
sed -e "1,2d" -e "$ s/.*/}/" -i "${tempfile}"
fi
fi
fi
if [[ ! -z "${error_bringing_report}" ]]; then
# Do error cleanup.
rm -f "${tempfile}"
if [[ "${is_output_dir_provided}" == "false" ]]; then
rm -rf "${output_dir}"
fi
exit 1
fi
# Process the bugreport.
"${FUCHSIA_BUILD_DIR}/tools/bugreport" "${output_dir}" < "${tempfile}"
if [ "$?" -ne 0 ]; then
fx-error "Error processing bugreport."
fx-error "Downloaded unprocessed report is in ${tempfile}."
# Clean up temp directory.
if [[ "${is_output_dir_provided}" == "false" ]]; then
rm -rf "${output_dir}"
fi
exit 1
else
# Clean up.
rm -f "${tempfile}"
fi