blob: 1eb4493c8d5b70c57b11a316a3a88f564b6087ac [file] [edit]
#!/bin/bash
# Copyright 2010 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Script to generate stackdumps from a machine or dmp files.
SCRIPT_ROOT="$(dirname "$(readlink -f "$0")")"
# shellcheck source=common.sh
. "${SCRIPT_ROOT}/common.sh" || exit 1
assert_inside_chroot
MINIDUMP_DUMP=minidump_dump
MINIDUMP_STACKWALK=minidump_stackwalk
DEFINE_string board "${DEFAULT_BOARD}" \
"The board for which you are building autotest"
DEFINE_string breakpad_root "" \
"Path to root of breakpad symbols if pre-existing symbols should be used"
DEFINE_boolean clean ${FLAGS_FALSE} \
"Remove crash reports from remote system after showing stacks"
DEFINE_string remote "" "remote hostname/IP of device"
DEFINE_integer ssh_port 0 "SSH port of the remote device"
usage() {
echo "usage: $(basename $0) [--remote=<IP>] [dump...]"
echo "Specify either a remote IP of a ChromeOS device to gather "
echo "all crash reports from, or list crash reports"
exit 1
}
# Clean up remote access and temp files.
cleanup() {
rm -rf "${TMP}"
}
# Removes single quotes around parameter
# Arguments:
# $1 - string which optionally has surrounding quotes
# Returns:
# None, but prints the string without quotes.
remove_quotes() {
echo "$1" | sed -e "s/^'//; s/'$//"
}
# Echoes kind of crash (minidump or kcrash).
get_kind() {
local kind="${1##*.}"
if [ "${kind}" = "dmp" ]; then
kind="minidump"
fi
echo ${kind}
}
# Generate symbols for the given module list.
# Args:
# $1 - file with a "module" per line. A module is the full target's
# path to a DSO or executable that was loaded during a crash.
generate_symbols() {
local modules_file="$1"
local modules=""
local any_missing=0
local module_count=0
for module in $(sort -u "${modules_file}"); do
local text_file="/build/${FLAGS_board}/${module}"
local debug_file="/build/${FLAGS_board}/usr/lib/debug/${module}.debug"
if [ -f "${text_file}" ] && [ -f "${debug_file}" ]; then
modules="${modules} ${text_file}"
module_count=$((module_count + 1))
else
if [ ${any_missing} -eq 0 ]; then
warn "Some modules are missing debug information:"
any_missing=1
fi
warn "* ${text_file}"
fi
done
if [ ${module_count} -gt 0 ]; then
info "Generating breakpad symbols for ${module_count} modules"
"${CHROMITE_BIN}/cros_generate_breakpad_symbols" \
--board=${FLAGS_board} ${modules}
fi
}
cros_cp() {
local remote="${FLAGS_remote}"
local port=()
if [[ "${FLAGS_ssh_port}" != "0" ]]; then
port+=( --port="${FLAGS_ssh_port}" )
elif [[ "${FLAGS_remote}" == *:* ]]; then
port+=( --port="${FLAGS_remote##*:}" )
remote=${FLAGS_remote%:*}
fi
cros cp "${port[@]}" "${remote}:$1" "$2"
}
learn_board() {
FLAGS_board=$(
cros_cp "/etc/lsb-release" - | \
awk -F= '$1 == "CHROMEOS_RELEASE_BOARD" {print $2}'
)
info "Target reports board is ${FLAGS_board}"
}
main() {
FLAGS "$@" || usage
eval set -- "${FLAGS_ARGV}"
local basename=$(basename "$0")
TMP=$(mktemp -d /tmp/${basename}.XXXX)
trap cleanup EXIT INT TERM
if [ -n "${FLAGS_remote}" ]; then
learn_board
local crashes=""
# File spec of all interesting crashes. /home/chronos... is
# listed separately from /mnt/stateful_partition/home/chronos/...
# because the former may be a mount point for the cryptohome.
# This allows us to get crashes from the currently logged in
# user as well as from non-logged in users at once. We remove
# duplicate crashes (in case cryptohome is not mounted) below.
local remote_crash_dirs=(
"/var/spool/crash"
"/home/chronos/crash"
"/home/user/*/crash"
"/run/daemon-store/crash/*"
"/mnt/stateful_partition/home/user/*/crash"
)
local remote_crash_patterns=()
for remote_crash_dir in "${remote_crash_dirs[@]}"; do
remote_crash_patterns+=( "${remote_crash_dir}/*.{dmp,kcrash}" )
done
local crashes
crashes=$(
cros shell "${FLAGS_remote}" \
ls -1 "${remote_crash_patterns[@]}" "2>/dev/null"
)
# Remove duplicates.
local unique_crashes=""
local crash_count=0
for crash in ${crashes}; do
local crash_short=$(basename ${crash})
if echo "${unique_crashes}" | grep -v -q "${crash_short}"; then
unique_crashes="${unique_crashes} ${crash}"
crash_count=$((crash_count + 1))
fi
done
if [ ${crash_count} -eq 0 ]; then
info "No crashes found on device."
exit 0
fi
info "Copying back ${crash_count} crashes."
crashes="${unique_crashes}"
declare -a dumps
local remote_crashes=()
for crash in ${crashes}; do
cros_cp "${crash}" "${TMP}"
dumps+=( "${TMP}/$(basename "${crash}")" )
done
set -- "$@" "${dumps[@]}"
if [ ${FLAGS_clean} -eq ${FLAGS_TRUE} ]; then
cros shell "${FLAGS_remote}" rm -rf "${remote_crash_dirs[@]}"
fi
else
[[ $# -gt 0 ]] || usage
[ -n "${FLAGS_board}" ] || die_notrace "--board is required."
fi
local modules_file="${TMP}/modules"
for dump in "$@"; do
dump=$(remove_quotes "${dump}")
if [ $(get_kind "${dump}") == "minidump" ]; then
# Find all DSOs and executables listed in lines like:
# (code_file) = "/usr/lib/mylib.so"
${MINIDUMP_DUMP} "${dump}" 2>/dev/null \
| grep code_file \
| sed 's/.*= "\(.*\)"/\1/' \
>> "${modules_file}"
fi
done
if [ -z "${FLAGS_breakpad_root}" ]; then
generate_symbols "${modules_file}"
FLAGS_breakpad_root=/build/${FLAGS_board}/usr/lib/debug/breakpad
fi
for dump in "$@"; do
dump=$(remove_quotes "${dump}")
if [ $(get_kind "${dump}") = "minidump" ]; then
info "Dumping stack for $(basename "${dump}")" \
"with ${FLAGS_breakpad_root}:"
${MINIDUMP_STACKWALK} "${dump}" "${FLAGS_breakpad_root}" 2> /dev/null
else
info "Dumping kcrash $(basename "${dump}"):"
cat "${dump}"
fi
echo ""
done
}
main "$@"