Description: Backport btraceback from 11.0.1 to improve btraceback output
Author: Eric Bollengier <eric@baculasystems.com>
Last-Update: 2021-01-06
Bug-Debian: 954971
Bug: https://bugs.bacula.org/view.php?id=2587

    Fix org#2587 Improve btraceback output
    
    Reported by Carsten
    
    At Debian we had bug reports because of the emails that btraceback sends.
    Apparently the users were confused because of a lack of explanation in the email.

--- a/scripts/btraceback.in
+++ b/scripts/btraceback.in
@@ -2,7 +2,7 @@
 #
 #   Bacula(R) - The Network Backup Solution
 #
-#   Copyright (C) 2000-2016 Kern Sibbald
+#   Copyright (C) 2000-2020 Kern Sibbald
 #   Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
 #
 #   The original author of Bacula is Kern Sibbald, with contributions
@@ -29,6 +29,17 @@
 #
 PNAME=`basename $1`
 WD="$3"
+# When parsing the configuration, the working directory is not
+# yet initialized
+if [ "$WD" = "" ]; then
+    WD=/tmp
+fi
+
+# Start constructing message. Point to log files for better user
+# experience, append backtrace or message about missing debugger later
+echo "Check the log files for more information." > ${WD}/bacula.$2.traceback
+echo "" >> ${WD}/bacula.$2.traceback
+
 case `uname -s` in
 SunOS)
    #
@@ -46,20 +57,23 @@
    which mdb > /dev/null 2>&1 && MDB=`which mdb` || MDB=''
    gcore -o ${WD}/${PNAME} $2
    if [ ! -z "${DBX}" ]; then
-      ${DBX} $1 $2 < @scriptdir@/btraceback.dbx > ${WD}/bacula.$2.traceback 2>&1
+      ${DBX} $1 $2 < @scriptdir@/btraceback.dbx >> ${WD}/bacula.$2.traceback 2>&1
    elif [ ! -z "${GDB}" ]; then
-      ${GDB} -quiet -batch -x @scriptdir@/btraceback.gdb $1 $2 > ${WD}/bacula.$2.traceback 2>&1
+      ${GDB} -quiet -batch -x @scriptdir@/btraceback.gdb $1 $2 >> ${WD}/bacula.$2.traceback 2>&1
    elif [ ! -z "${MDB}" ]; then
-      ${MDB} -u -p $2 < @scriptdir@/btraceback.mdb > ${WD}/bacula.$2.traceback 2>&1
+      ${MDB} -u -p $2 < @scriptdir@/btraceback.mdb >> ${WD}/bacula.$2.traceback 2>&1
    fi
-   PNAME="${PNAME} on `hostname`"
-   cat ${WD}/bacula.$2.traceback \
-    | @sbindir@/bsmtp -h @smtp_host@ -f @dump_email@ -s "Bacula DBX traceback of ${PNAME}" @dump_email@
    ;;
 *)
-   gdb -quiet -batch -x @scriptdir@/btraceback.gdb $1 $2 >${WD}/bacula.$2.traceback 2>&1
-   PNAME="${PNAME} on `hostname`"
-   cat ${WD}/bacula.$2.traceback \
-    | @sbindir@/bsmtp -h @smtp_host@ -f @dump_email@ -s "Bacula GDB traceback of ${PNAME}" @dump_email@
+    which gdb > /dev/null 2>&1 && GDB=`which gdb` || GDB=''
+    if  [ ! -z "${GDB}" ]; then
+        gdb -quiet -batch -x @scriptdir@/btraceback.gdb $1 $2 >> ${WD}/bacula.$2.traceback 2>&1
+    else
+        echo "Please install a debugger (gdb) to receive a traceback." >> ${WD}/bacula.$2.traceback 2>&1
+    fi
    ;;
 esac
+
+PNAME="${PNAME} on `hostname`"
+cat ${WD}/bacula.$2.traceback \
+    | @sbindir@/bsmtp -h @smtp_host@ -f @dump_email@ -s "Bacula traceback. Process crashed ${PNAME}" @dump_email@
