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