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