ctdb-common: Add command line processing abstraction
[Samba.git] / selftest / gdb_backtrace
blobef02e784efc926277ab88664f588b3a2a587f97a
1 #!/bin/sh
3 BASENAME=`basename $0`
5 if [ -n "$VALGRIND" -o -n "$SMBD_VALGRIND" ]; then
6 echo "${BASENAME}: Not running debugger under valgrind"
7 exit 1
8 fi
10 # we want everything on stderr, so the program is not disturbed
11 exec 1>&2
13 BASENAME=`basename $0`
14 UNAME=`uname`
16 PID=$1
17 BINARY=$2
19 test x"${PID}" = x"" && {
20 echo "Usage: ${BASENAME} <pid> [<binary>]"
21 exit 1
24 DB_LIST="gdb"
25 case "${UNAME}" in
27 # on Tru64 we need to try ladebug first
28 # because gdb crashes itself...
30 OSF1)
31 DB_LIST="ladebug ${DB_LIST}"
34 # On solaris dbx is working way more better than gdb
35 # let's try it first
37 SunOS)
38 DB_LIST="dbx ${DB_LIST}"
41 # FreeBSD comes with a flavor that works gdb66 and one that don't gdb
42 # (gdb 6.1) let's try it first the one that works !
44 FreeBSD)
45 DB_LIST="gdb66 ${DB_LIST}"
47 esac
49 for DB in ${DB_LIST}; do
50 DB_BIN=`which ${DB} 2>/dev/null | grep '^/'`
51 test x"${DB_BIN}" != x"" && {
52 break
54 done
56 test x"${DB_BIN}" = x"" && {
57 echo "${BASENAME}: ERROR: No debugger found."
58 exit 1
61 need_binary="no"
62 case "${DB}" in
63 # These debuggers need the process binary specified:
64 ladebug)
65 need_binary="yes"
67 gdb66)
68 need_binary="yes"
70 dbx)
71 need_binary="yes"
73 esac
75 test x"${need_binary}" = x"yes" && {
77 # we first try to use /proc/${PID}/exe or /proc/{$PID}/path for solaris
78 # then fallback to the binary from the commandline
79 # then we search for the commandline argument with
80 # 'which'
82 test -f "/proc/${PID}/exe" && BINARY="/proc/${PID}/exe"
83 test -f "/proc/${PID}/path/a.out" && BINARY=`ls -l /proc/${PID}/path/a.out |sed 's/.*-> //'`
84 test x"${BINARY}" = x"" && BINARY="/proc/${PID}/exe"
85 test -f "${BINARY}" || BINARY=`which ${BINARY}`
87 test -f "${BINARY}" || {
88 echo "${BASENAME}: ERROR: Cannot find binary '${BINARY}'."
89 exit 1
93 BATCHFILE_PRE=`mktemp --tmpdir gdb_backtrace_pre.XXXXXXXXXX`
94 test -n "${BATCHFILE_PRE}" || {
95 echo "mktemp doesn't work" 1>&2
96 exit 1
98 BATCHFILE_MAIN=`mktemp --tmpdir gdb_backtrace_main.XXXXXXXXXX`
99 test -n "${BATCHFILE_MAIN}" || {
100 echo "mktemp doesn't work" 1>&2
101 exit 1
103 case "${DB}" in
104 ladebug)
105 cat << EOF > ${BATCHFILE_PRE}
106 set \$stoponattach
109 cat << EOF > ${BATCHFILE_MAIN}
110 where
111 quit
113 ${DB_BIN} -c "${BATCHFILE_MAIN}" -i "${BATCHFILE_PRE}" -pid "${PID}" "${BINARY}"
115 gdb66)
116 cat << EOF > ${BATCHFILE_MAIN}
117 set height 1000
118 bt full
119 info locals
120 kill
121 quit
123 ${DB_BIN} -x "${BATCHFILE_MAIN}" "${BINARY}" "${PID}"
125 gdb)
126 cat << EOF > ${BATCHFILE_MAIN}
127 set height 0
128 bt full
129 thread apply all bt full
130 info locals
131 quit
133 ${DB_BIN} -batch -x "${BATCHFILE_MAIN}" --pid "${PID}" < /dev/null
135 dbx)
136 ${DB_BIN} "where;dump;kill;quit" "${BINARY}" "${PID}"
138 esac
139 /bin/rm -f ${BATCHFILE_PRE} ${BATCHFILE_MAIN}