#!/bin/sh
#
# Andrew Tridgell
# junkcode@tridgell.net
# http://www.samba.org/ftp/unpacked/junkcode/segv_handler/
#
# we want everything on stderr, so the program is not disturbed
exec 1>&2

if test -z "`which gdb`"; then
	echo "Please install gdb (GNU Debugger) so that we can generate a comprehensive backtrace"
fi

PID=$1
TMPFILE=/tmp/gdb.$$
cat << EOF  > $TMPFILE
set height 0
set pagination off
print "Backtrace"
bt
print "Backtrace Full"
bt full
print "Thread Apply all & Backtrace Full"
thread apply all bt full
print "Generating Core File"
run
generate-core-file
quit
EOF
gdb -batch -x $TMPFILE --pid $PID < /dev/null 2>&1
/bin/rm -f $TMPFILE
