replace:wscript - reintroduce the size check for "bool" due to a request of metze
[gd/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 esac
40
41 for DB in ${DB_LIST}; do
42         DB_BIN=`which ${DB} 2>/dev/null | grep '^/'`
43         test x"${DB_BIN}" != x"" && {
44                 break
45         }
46 done
47
48 test x"${DB_BIN}" = x"" && {
49         echo "${BASENAME}: ERROR: No debugger found."
50         exit 1
51 }
52
53 #
54 # we first try to use /proc/${PID}/exe or /proc/{$PID}/path for solaris
55 # then fallback to the binary from the commandline
56 # then we search for the commandline argument with
57 # 'which'
58 #
59 test -f "/proc/${PID}/exe" && BINARY="/proc/${PID}/exe"
60 test -f "/proc/${PID}/path/a.out" && BINARY=`ls -l /proc/${PID}/path/a.out |sed 's/.*-> //'`
61 test x"${BINARY}" = x"" && BINARY="/proc/${PID}/exe"
62 test -f "${BINARY}" || BINARY=`which ${BINARY}`
63
64 test -f "${BINARY}" || {
65         echo "${BASENAME}: ERROR: Cannot find binary '${BINARY}'."
66         exit 1
67 }
68
69 echo "${BASENAME}: Trying to use ${DB_BIN} on ${BINARY} on PID ${PID}"
70
71 BATCHFILE_PRE=/tmp/gdb_backtrace_pre.$$
72 BATCHFILE_MAIN=/tmp/gdb_backtrace_main.$$
73 case "${DB}" in
74         ladebug)
75 cat << EOF  > ${BATCHFILE_PRE}
76 set \$stoponattach
77 EOF
78
79 cat << EOF  > ${BATCHFILE_MAIN}
80 where
81 quit
82 EOF
83         ${DB_BIN} -c "${BATCHFILE_MAIN}" -i "${BATCHFILE_PRE}" -pid "${PID}" "${BINARY}"
84         ;;
85         gdb)
86 cat << EOF  > ${BATCHFILE_MAIN}
87 set height 1000
88 bt full
89 info locals
90 quit
91 EOF
92         ${DB_BIN} -x "${BATCHFILE_MAIN}" "${BINARY}" "${PID}"
93         ;;
94 dbx)
95         ${DB_BIN} "where;dump;kill;quit" "${BINARY}" "${PID}"
96         ;;
97 esac
98 /bin/rm -f ${BATCHFILE_PRE} ${BATCHFILE_MAIN}