2005-07-21 16:11:52 +04:00
#!/bin/sh
2022-02-21 16:06:36 +03:00
BASENAME=$(basename $0)
2006-10-06 17:23:42 +04:00
2021-02-09 18:22:37 +03:00
unset LD_PRELOAD
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
2021-02-11 07:39:18 +03:00
if [ "x$PLEASE_NO_GDB_BACKTRACE" != "x" ]; then
echo "${BASENAME}: Not running debugger because PLEASE_NO_GDB_BACKTRACE is set"
2022-02-21 16:06:36 +03:00
exit 0
2021-02-11 07:39:18 +03:00
fi
2005-07-21 16:11:52 +04:00
# we want everything on stderr, so the program is not disturbed
exec 1>&2
2022-02-21 16:06:36 +03:00
UNAME=$(uname)
2006-10-06 17:23:42 +04:00
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
2022-02-21 16:06:36 +03:00
#
# on Tru64 we need to try ladebug first
# because gdb crashes itself...
#
OSF1)
DB_LIST="ladebug ${DB_LIST}"
2006-10-06 17:23:42 +04:00
;;
2022-02-21 16:06:36 +03:00
#
# On solaris dbx is working way more better than gdb
# let's try it first
#
SunOS)
DB_LIST="dbx ${DB_LIST}"
2010-11-28 17:28:57 +03:00
;;
2022-02-21 16:06:36 +03:00
#
# FreeBSD comes with a flavor that works gdb66 and one that don't gdb
# (gdb 6.1) let's try it first the one that works !
#
FreeBSD)
DB_LIST="gdb66 ${DB_LIST}"
2010-11-28 17:32:06 +03:00
;;
2006-10-06 17:23:42 +04:00
esac
for DB in ${DB_LIST}; do
2022-02-21 16:06:36 +03: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
}
2011-06-24 07:17:19 +04:00
need_binary="no"
case "${DB}" in
# These debuggers need the process binary specified:
2022-02-21 16:06:36 +03:00
ladebug)
2011-06-24 07:17:19 +04:00
need_binary="yes"
;;
2022-02-21 16:06:36 +03:00
gdb66)
2011-06-24 07:17:19 +04:00
need_binary="yes"
;;
2022-02-21 16:06:36 +03:00
dbx)
2011-06-24 07:17:19 +04:00
need_binary="yes"
;;
esac
test x"${need_binary}" = x"yes" && {
2022-02-21 16:06:36 +03:00
# we first try to use /proc/${PID}/exe or /proc/{$PID}/path for solaris
# then fallback to the binary from the commandline
# then we search for the commandline argument with
# 'which'
#
2011-06-24 07:17:19 +04:00
test -f "/proc/${PID}/exe" && BINARY="/proc/${PID}/exe"
2022-02-21 16:06:36 +03:00
test -f "/proc/${PID}/path/a.out" && BINARY=$(ls -l /proc/${PID}/path/a.out | sed 's/.*-> //')
2011-06-24 07:17:19 +04:00
test x"${BINARY}" = x"" && BINARY="/proc/${PID}/exe"
2022-02-21 16:06:36 +03:00
test -f "${BINARY}" || BINARY=$(which ${BINARY})
2011-06-24 07:17:19 +04:00
test -f "${BINARY}" || {
2022-02-21 16:06:36 +03:00
echo "${BASENAME}: ERROR: Cannot find binary '${BINARY}'."
exit 1
2011-06-24 07:17:19 +04:00
}
2006-10-06 17:23:42 +04:00
}
2005-07-21 16:11:52 +04:00
2022-02-21 16:06:36 +03:00
BATCHFILE_PRE=$(mktemp --tmpdir gdb_backtrace_pre.XXXXXXXXXX)
2016-11-23 14:26:18 +03:00
test -n "${BATCHFILE_PRE}" || {
echo "mktemp doesn't work" 1>&2
exit 1
}
2022-02-21 16:06:36 +03:00
BATCHFILE_MAIN=$(mktemp --tmpdir gdb_backtrace_main.XXXXXXXXXX)
2016-11-23 14:26:18 +03:00
test -n "${BATCHFILE_MAIN}" || {
echo "mktemp doesn't work" 1>&2
exit 1
}
2006-10-06 17:23:42 +04:00
case "${DB}" in
2022-02-21 16:06:36 +03:00
ladebug)
cat <<EOF >${BATCHFILE_PRE}
2006-10-06 21:55:17 +04:00
set \$stoponattach
EOF
2022-02-21 16:06:36 +03:00
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
;;
2022-02-21 16:06:36 +03:00
gdb66)
cat <<EOF >${BATCHFILE_MAIN}
2010-11-28 17:32:06 +03:00
set height 1000
bt full
info locals
kill
quit
EOF
${DB_BIN} -x "${BATCHFILE_MAIN}" "${BINARY}" "${PID}"
;;
2022-02-21 16:06:36 +03:00
gdb)
cat <<EOF >${BATCHFILE_MAIN}
2011-02-22 04:28:33 +03:00
set height 0
2005-07-21 16:11:52 +04:00
bt full
2011-02-22 04:28:33 +03:00
thread apply all bt full
2010-03-01 14:22:23 +03:00
info locals
2005-07-21 16:11:52 +04:00
quit
EOF
2022-02-21 16:06:36 +03:00
${DB_BIN} -batch -x "${BATCHFILE_MAIN}" --pid "${PID}" </dev/null
2006-10-06 17:23:42 +04:00
;;
2010-11-28 13:53:40 +03:00
dbx)
${DB_BIN} "where;dump;kill;quit" "${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}