2005-07-21 16:11:52 +04:00
#!/bin/sh
2006-10-06 17:23:42 +04:00
BASENAME=`basename $0`
2005-09-27 11:11:33 +04:00
if [ -n "$VALGRIND" -o -n "$SMBD_VALGRIND" ]; then
2006-10-06 17:23:42 +04:00
echo "${BASENAME}: Not running debugger under valgrind"
exit 1
2005-09-27 11:11:33 +04:00
fi
2005-07-21 16:11:52 +04:00
# we want everything on stderr, so the program is not disturbed
exec 1>&2
2006-10-06 17:23:42 +04:00
BASENAME=`basename $0`
UNAME=`uname`
2005-07-21 16:11:52 +04:00
PID=$1
2006-10-06 17:23:42 +04:00
BINARY=$2
test x"${PID}" = x"" && {
echo "Usage: ${BASENAME} <pid> [<binary>]"
exit 1
}
DB_LIST="gdb"
case "${UNAME}" in
#
# on Tru64 we need to try ladebug first
# because gdb crashes itself...
#
OSF1)
DB_LIST="ladebug ${DB_LIST}"
;;
esac
for DB in ${DB_LIST}; do
2007-05-18 13:47:53 +04:00
DB_BIN=`which ${DB} 2>/dev/null | grep '^/'`
2006-10-06 17:23:42 +04:00
test x"${DB_BIN}" != x"" && {
break
}
done
test x"${DB_BIN}" = x"" && {
echo "${BASENAME}: ERROR: No debugger found."
exit 1
}
#
# we first try to use /proc/${PID}/exe
# then fallback to the binary from the commandline
# then we search for the commandline argument with
# 'which'
#
test -f "/proc/${PID}/exe" && BINARY="/proc/${PID}/exe"
test x"${BINARY}" = x"" && BINARY="/proc/${PID}/exe"
test -f "${BINARY}" || BINARY=`which ${BINARY}`
test -f "${BINARY}" || {
echo "${BASENAME}: ERROR: Cannot find binary '${BINARY}'."
exit 1
}
2005-07-21 16:11:52 +04:00
2006-10-06 17:23:42 +04:00
echo "${BASENAME}: Trying to use ${DB_BIN} on ${BINARY} on PID ${PID}"
2006-10-06 21:55:17 +04:00
BATCHFILE_PRE=/tmp/gdb_backtrace_pre.$$
BATCHFILE_MAIN=/tmp/gdb_backtrace_main.$$
2006-10-06 17:23:42 +04:00
case "${DB}" in
ladebug)
2006-10-06 21:55:17 +04:00
cat << EOF > ${BATCHFILE_PRE}
set \$stoponattach
EOF
cat << EOF > ${BATCHFILE_MAIN}
2006-10-06 17:23:42 +04:00
where
quit
EOF
2006-10-06 21:55:17 +04:00
${DB_BIN} -c "${BATCHFILE_MAIN}" -i "${BATCHFILE_PRE}" -pid "${PID}" "${BINARY}"
2006-10-06 17:23:42 +04:00
;;
gdb)
2006-10-06 21:55:17 +04:00
cat << EOF > ${BATCHFILE_MAIN}
2005-07-21 16:11:52 +04:00
set height 1000
bt full
quit
EOF
2006-10-06 21:55:17 +04:00
${DB_BIN} -x "${BATCHFILE_MAIN}" "${BINARY}" "${PID}"
2006-10-06 17:23:42 +04:00
;;
esac
2006-10-06 21:55:17 +04:00
/bin/rm -f ${BATCHFILE_PRE} ${BATCHFILE_MAIN}