blob: e7d9208955a3bae99f3f6334e95a475b42b5e5e9 [file] [log] [blame]
#!/bin/bash
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# This opens various servo consoles using cu, from outside the chroot,
# automatically getting the correct console from dut-control. This
# also tees the output to a secondary logfile.
#
# The recommended way to use this is to setup an alias, e.g. add this in
# your .bashrc (outside the chroot):
# alias dut-console="~/chromiumos/src/platform/dev/contrib/dut-console"
# And then simply run:
# dut-console ec
#
# The script also allows to connect to different servod port, e.g.
# dut-control cpu 9999
set -e
# Script is stored in $CROS_SDK/src/platform/dev/contrib
CROS_SDK="$(readlink -f "$(dirname "$0")/../../../..")"
PYTHONPATH="${CROS_SDK}/chroot/usr/lib64/python2.7/site-packages"
kdmx_pid=
cleanup() {
trap - INT TERM ERR EXIT
if [[ -n "${kdmx_pid}" ]]; then
kill "${kdmx_pid}" 2>/dev/null
fi
}
get_pty() {
local pty_name="$1"
local port="$2"
local args=()
if [[ -n "${port}" ]]; then
args+=( --port "${port}" )
fi
PYTHONPATH="${PYTHONPATH}" \
python "${PYTHONPATH}/servo/dut_control.py" "${pty_name}" "${args[@]}" | \
cut -d : -f 2
}
find_cu() {
if ! hash cu; then
echo "Please install cu first:" >&2
echo "sudo apt-get install cu" >&2
exit 1
fi
}
usage() {
cat <<EOF
dut-control type [port]
Opens CPU/EC servo console using "cu", outputs to both terminal, and an output
file named "log-\${type}[-\${port}]" in the current directory.
type One of cpu, cpudbg, ec, cr50, fpmcu, servo, servo_v4
[port] servod port (optional, uses default if not specified)
EOF
exit 2
}
main() {
local type="$1"
local port="$2"
local kdmx
local pty
local pty_name
local outlog
find_cu
trap cleanup INT TERM ERR EXIT
case "${type}" in
cpu)
pty_name=cpu_uart_pty
;;
cpudbg)
pty_name=cpu_uart_pty
kdmx='y'
;;
ec)
pty_name=ec_uart_pty
;;
cr50)
pty_name=cr50_uart_pty
;;
fpmcu)
pty_name=fpmcu_uart_pty
;;
servo)
pty_name=servo_console_pty
;;
servo_v4)
pty_name=servo_v4_console_pty
;;
*)
usage
;;
esac
pty="$(get_pty "${pty_name}" "${port}")"
if [[ ! -e "${pty}" ]]; then
echo "Cannot find pty ${pty_name}." >&2
exit 1
fi
outlog="log-${type}"
if [[ -n "${port}" ]]; then
outlog="${outlog}-${port}"
fi
if [[ "${kdmx}" == 'y' ]]; then
if ! hash kdmx; then
echo "Please build and put kdmx in your PATH first." >&2
echo "See https://www.chromium.org/chromium-os/how-tos-and-troubleshooting/kernel-faq" >&2
exit 1
fi
# Note that we hardcode kdmx pty file location in the /tmp directory of the
# chroot. This should be ok as there is normally only one user, and only
# a single instance of dut-console/kdmx can run per console/servod instance.
local spath_in="/tmp/kdmx_${type}"
if [[ -n "${port}" ]]; then
spath_in="${spath_in}_${port}"
fi
local spath="${CROS_SDK}/chroot${spath_in}"
local spath_pty="${spath}_trm"
local spath_log="${spath}_log"
# Make sure we do not use a stale pty file
rm -f "${spath_pty}"
echo "Muxing ${pty_name}:${pty} with kdmx..."
kdmx -n -p "${pty}" -s "${spath}" > "${spath_log}" 2>&1 &
kdmx_pid="$!"
# Give kdmx enough time to start and create pty file
sleep 0.5
pty="$(cat "${spath_pty}" 2>/dev/null || true)"
if [[ -z "${pty}" ]]; then
echo "kdmx failed to start:" >&2
cat "${spath_log}" >&2
exit 1
fi
echo "You can now use kdbg inside the chroot with a command like:"
echo '${CROSS_ARCH}-gdb \
/build/${BOARD}/usr/lib/debug/boot/vmlinux \
-ex "target remote $(cat '"${spath_in}"'_gdb)'
fi
echo "Opening ${pty_name}:${pty}..."
echo "Log in ${outlog}, type ~.<enter> to exit."
stdbuf -oL cu --nostop -l "${pty}" | tee "${outlog}"
}
main "$@"