1#! /bin/sh
2#
3# Author: Andrew Tridgell <tridge at samba dot org>
4
5# we want everything on stderr, so the program is not disturbed
6exec 1>&2
7
8BASENAME=$( basename $0)
9
10test -z ${GDB_BIN} && GDB_BIN=$( type -p gdb)
11if [ -z ${GDB_BIN} ]; then
12	echo "ERROR: ${BASENAME} needs an installed gdb. "
13	exit 1
14fi
15
16if [ -z $1 ]; then
17	echo "ERROR: ${BASENAME} needs a PID. "
18	exit 1
19fi
20PID=$1
21
22# use /dev/shm as default temp directory
23test -d /dev/shm && \
24	TMP_BASE_DIR=/dev/shm || \
25	TMP_BASE_DIR=/var/tmp
26TMPFILE=$( mktemp -p ${TMP_BASE_DIR} backtrace.XXXXXX)
27if [ $? -ne 0 ]; then
28	echo "ERROR: ${basename} can't create temp file in ${TMP_BASE_DIR}. "
29	exit 1
30fi
31
32cat << EOF  > "${TMPFILE}"
33set height 0
34up 8
35bt full
36quit
37EOF
38
39${GDB_BIN} -x "${TMPFILE}" "/proc/${PID}/exe" "${PID}"
40
41/bin/rm -f "${TMPFILE}"
42