[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[sfrench/samba-autobuild/.git] / source3 / script / tests / gdb_backtrace
1 #!/bin/sh
2
3 BASENAME=`basename $0`
4
5 if [ -n "$VALGRIND" -o -n "$SMBD_VALGRIND" ]; then
6         echo "${BASENAME}: Not running debugger under valgrind"
7         exit 1
8 fi
9
10 # we want everything on stderr, so the program is not disturbed
11 exec 1>&2
12
13 BASENAME=`basename $0`
14 UNAME=`uname`
15
16 PID=$1
17 BINARY=$2
18
19 test x"${PID}" = x"" && {
20         echo "Usage: ${BASENAME} <pid> [<binary>]"
21         exit 1
22 }
23
24 DB_LIST="gdb"
25 case "${UNAME}" in
26         #
27         # on Tru64 we need to try ladebug first
28         # because gdb crashes itself...
29         #
30         OSF1)
31                 DB_LIST="ladebug ${DB_LIST}"
32         ;;
33 esac
34
35 for DB in ${DB_LIST}; do
36         DB_BIN=`which ${DB} 2>/dev/null | grep '^/'`
37         test x"${DB_BIN}" != x"" && {
38                 break
39         }
40 done
41
42 test x"${DB_BIN}" = x"" && {
43         echo "${BASENAME}: ERROR: No debugger found."
44         exit 1
45 }
46
47 #
48 # we first try to use /proc/${PID}/exe
49 # then fallback to the binary from the commandline
50 # then we search for the commandline argument with
51 # 'which'
52 #
53 test -f "/proc/${PID}/exe" && BINARY="/proc/${PID}/exe"
54 test x"${BINARY}" = x"" && BINARY="/proc/${PID}/exe"
55 test -f "${BINARY}" || BINARY=`which ${BINARY}`
56
57 test -f "${BINARY}" || {
58         echo "${BASENAME}: ERROR: Cannot find binary '${BINARY}'."
59         exit 1
60 }
61
62 echo "${BASENAME}: Trying to use ${DB_BIN} on ${BINARY} on PID ${PID}"
63
64 BATCHFILE_PRE=/tmp/gdb_backtrace_pre.$$
65 BATCHFILE_MAIN=/tmp/gdb_backtrace_main.$$
66 case "${DB}" in
67         ladebug)
68 cat << EOF  > ${BATCHFILE_PRE}
69 set \$stoponattach
70 EOF
71
72 cat << EOF  > ${BATCHFILE_MAIN}
73 where
74 quit
75 EOF
76         ${DB_BIN} -c "${BATCHFILE_MAIN}" -i "${BATCHFILE_PRE}" -pid "${PID}" "${BINARY}"
77         ;;
78         gdb)
79 cat << EOF  > ${BATCHFILE_MAIN}
80 set height 1000
81 bt full
82 quit
83 EOF
84         ${DB_BIN} -x "${BATCHFILE_MAIN}" "${BINARY}" "${PID}"
85         ;;
86 esac
87 /bin/rm -f ${BATCHFILE_PRE} ${BATCHFILE_MAIN}