selftest: Ensure the DC has started and and got a RID set before we proceed
[nivanova/samba-autobuild/.git] / selftest / 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         #
34         # On solaris dbx is working way more better than gdb
35         # let's try it first
36         #
37         SunOS)
38                 DB_LIST="dbx ${DB_LIST}"
39         ;;
40         #
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 !
43         #
44         FreeBSD)
45                 DB_LIST="gdb66 ${DB_LIST}"
46         ;;
47 esac
48
49 for DB in ${DB_LIST}; do
50         DB_BIN=`which ${DB} 2>/dev/null | grep '^/'`
51         test x"${DB_BIN}" != x"" && {
52                 break
53         }
54 done
55
56 test x"${DB_BIN}" = x"" && {
57         echo "${BASENAME}: ERROR: No debugger found."
58         exit 1
59 }
60
61 need_binary="no"
62 case "${DB}" in
63 # These debuggers need the process binary specified:
64         ladebug)
65         need_binary="yes"
66         ;;
67         gdb66)
68         need_binary="yes"
69         ;;
70         dbx)
71         need_binary="yes"
72         ;;
73 esac
74
75 test x"${need_binary}" = x"yes" && {
76
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'
81 #
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}`
86         
87         test -f "${BINARY}" || {
88             echo "${BASENAME}: ERROR: Cannot find binary '${BINARY}'."
89             exit 1
90         }
91 }
92
93 BATCHFILE_PRE=/tmp/gdb_backtrace_pre.$$
94 BATCHFILE_MAIN=/tmp/gdb_backtrace_main.$$
95 case "${DB}" in
96         ladebug)
97 cat << EOF  > ${BATCHFILE_PRE}
98 set \$stoponattach
99 EOF
100
101 cat << EOF  > ${BATCHFILE_MAIN}
102 where
103 quit
104 EOF
105         ${DB_BIN} -c "${BATCHFILE_MAIN}" -i "${BATCHFILE_PRE}" -pid "${PID}" "${BINARY}"
106         ;;
107         gdb66)
108 cat << EOF  > ${BATCHFILE_MAIN}
109 set height 1000
110 bt full
111 info locals
112 kill
113 quit
114 EOF
115         ${DB_BIN} -x "${BATCHFILE_MAIN}" "${BINARY}" "${PID}"
116         ;;
117         gdb)
118 cat << EOF  > ${BATCHFILE_MAIN}
119 set height 0
120 bt full
121 thread apply all bt full
122 info locals
123 quit
124 EOF
125         ${DB_BIN} -batch -x "${BATCHFILE_MAIN}" --pid "${PID}" < /dev/null
126         ;;
127 dbx)
128         ${DB_BIN} "where;dump;kill;quit" "${BINARY}" "${PID}"
129         ;;
130 esac
131 /bin/rm -f ${BATCHFILE_PRE} ${BATCHFILE_MAIN}