Skip to content

Instantly share code, notes, and snippets.

@dreamcat4
Last active April 27, 2019 07:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dreamcat4/c2bea0e889de8860b035 to your computer and use it in GitHub Desktop.
Save dreamcat4/c2bea0e889de8860b035 to your computer and use it in GitHub Desktop.
entrypoint script to take core files, and another script to take a stacktrace (while program is hung running, deadlocked / live-locked)
#!/bin/bash
# Tvheadend Debugging Guide
# https://tvheadend.org/projects/tvheadend/wiki/Debugging
_cleanup ()
{
if [ "$core_pattern_orig" ]; then
# Restore core path
echo "$core_pattern_orig" > /proc/sys/kernel/core_pattern
fi
# Restore original file ownership and permissions
chown -R ${crash_uid}:${crash_gid} /crash && chmod ${crash_rwx} /crash
exit 0
}
trap _cleanup TERM INT QUIT HUP
# Remember the folder ownership for later
crash_uid="$(stat -c %u /crash)"
crash_gid="$(stat -c %g /crash)"
crash_rwx="$(stat -c %a /crash)"
# Set folder permissions
chown -R hts:video /config /recordings
chown -R hts:video /crash && chmod u+rwx /crash
if [ "$pipework_wait" ]; then
echo "Waiting for pipework to bring up $pipework_wait..."
pipework --wait -i $pipework_wait
fi
# Enable core dumps
ulimit -c unlimited
echo "Set: ulimit -c unlimited"
# Override core path
core_pattern_orig=$(cat /proc/sys/kernel/core_pattern)
core_pattern="/crash/core.%h.%e.%t"
echo "$core_pattern" > /proc/sys/kernel/core_pattern
# Make sure our core files get saved as '/crash/core*'
if [ "$?" -ne "0" ]; then
echo "error: can't modify /proc/sys/kernel/core_pattern
Did you run this image with --privileged=true flag?"
fi
if [ "$(cat /proc/sys/kernel/core_pattern | grep -v -e '^/crash/core')" ]; then
echo "error: the save path of core files is not /crash/core*
Aborting."
exit 1
fi
echo "Set: core_pattern=/crash/core.%h.%e.%t"
# Uname
echo "uname -a:"
uname -a
# Dmesg
dmesg > /crash/dmesg
echo "Saved: dmesg --> /crash/dmesg"
# Start tvheadend
echo /usr/bin/tvheadend "$@"
/usr/bin/tvheadend "$@"
# Echo full trace to stdout
tvh_pid="$(ps -o pid= -C tvheadend | head -1 | tr -d ' ')"
[ "${tvh_pid}" ] || exit 1
tail -n 99999 -F --pid=$tvh_pid /crash/tvheadend.log
# Restore core path
echo "$core_pattern_orig" > /proc/sys/kernel/core_pattern
# Keep debug binary with symbols
cp /usr/lib/debug/usr/bin/tvheadend /crash/tvheadend
# Restore original file ownership and permissions
chown -R ${crash_uid}:${crash_gid} /crash && chmod ${crash_rwx} /crash
# Exit cleanly if there was no segfault crash
core_file=$(find /crash/core.*)
[ "$core_file" ] || rm /crash/tvheadend && exit 0
echo "
***********************************************************************
GDB Backtrace
***********************************************************************
"
echo "set logging on /crash/gdb.txt
set pagination off
bt full" | gdb /usr/bin/tvheadend "$core_file"
echo "
***********************************************************************"
echo "
Tvheadend Debugging Guide
----> https://tvheadend.org/projects/tvheadend/wiki/Debugging
Tvheadend Issue Tracker
----> https://tvheadend.org/projects/tvheadend/issues
Tvheadend New Issue
----> https://tvheadend.org/projects/tvheadend/issues/new
"
containers:
tvheadend-debug:
image: dreamcat4/tvheadend-debug
dockerfile: tvheadend/Dockerfile.debug
run:
cmd: --abort
volume:
- /home/id/tvh_debug:/crash
privileged: true
#!/bin/bash
# Tvheadend Debugging Guide
# https://tvheadend.org/projects/tvheadend/wiki/Debugging
# Override core path
core_pattern_orig=$(cat /proc/sys/kernel/core_pattern)
core_pattern="/crash/core.%h.%e.%t"
echo "$core_pattern" > /proc/sys/kernel/core_pattern
# Remember the folder ownership for later
crash_uid="$(stat -c %u /crash)"
crash_gid="$(stat -c %g /crash)"
crash_rwx="$(stat -c %a /crash)"
# Set folder permissions
chown -R hts:video /crash && chmod u+rwx /crash
echo "
***********************************************************************
uname -a"
uname -a
echo "
***********************************************************************
ps -aux"
ps -aux
echo "
***********************************************************************
GDB Backtrace
***********************************************************************"
tvh_pid="$(ps -o pid= -C tvheadend)"
echo "set logging on
set pagination off
thread apply all bt full
generate core file" | gdb /usr/bin/tvheadend $tvh_pid
# Restore core path
echo "$core_pattern_orig" > /proc/sys/kernel/core_pattern
# Keep debug binary with symbols
cp /usr/lib/debug/usr/bin/tvheadend /crash/tvheadend
# Restore original file ownership and permissions
chown -R ${crash_uid}:${crash_gid} /crash && chmod ${crash_rwx} /crash
echo "
***********************************************************************"
echo "
Tvheadend Debugging Guide
----> https://tvheadend.org/projects/tvheadend/wiki/Debugging
Tvheadend Issue Tracker
----> https://tvheadend.org/projects/tvheadend/issues
Tvheadend New Issue
----> https://tvheadend.org/projects/tvheadend/issues/new
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment