diff --git a/etc/gdb-backtrace-script b/etc/gdb-backtrace-script
index 0ccd9b79b9cbb366e1edabfd007e030c045c78bd..4e963e11f89da1445c8225bc1de08f91c68a450c 100644
--- a/etc/gdb-backtrace-script
+++ b/etc/gdb-backtrace-script
@@ -1,3 +1,5 @@
+# This script is used by TUnixSystem::StackTrace() on Mac OS X.
+
 echo \n
 echo ========== STACKS OF ALL THREADS ==========\n
 thread apply all bt
diff --git a/etc/gdb-backtrace.sh b/etc/gdb-backtrace.sh
new file mode 100755
index 0000000000000000000000000000000000000000..0d1077a44ed3324b1f038b8f01662434c15ae20a
--- /dev/null
+++ b/etc/gdb-backtrace.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+# This script is almost identical to /usr/bin/gstack.
+# It is used by TUnixSystem::StackTrace() on Linux.
+
+if test $# -ne 1; then
+    echo "Usage: `basename $0 .sh` <process-id>" 1>&2
+    exit 1
+fi
+
+if test ! -r /proc/$1; then
+    echo "Process $1 not found." 1>&2
+    exit 1
+fi
+
+# GDB doesn't allow "thread apply all bt" when the process isn't
+# threaded; need to peek at the process to determine if that or the
+# simpler "bt" should be used.
+
+backtrace="bt"
+if test -d /proc/$1/task ; then
+    # Newer kernel; has a task/ directory.
+    if test `ls /proc/$1/task | wc -l` -gt 1 2>/dev/null ; then
+	backtrace="thread apply all bt"
+    fi
+elif test -f /proc/$1/maps ; then
+    # Older kernel; go by it loading libpthread.
+    if grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
+	backtrace="thread apply all bt"
+    fi
+fi
+
+GDB=${GDB:-gdb}
+
+if $GDB -nx --quiet --batch --readnever > /dev/null 2>&1; then
+    readnever=--readnever
+else
+    readnever=
+fi
+
+# Run GDB, strip out unwanted noise.
+$GDB --quiet $readnever -nx /proc/$1/exe $1 <<EOF 2>&1 | 
+$backtrace
+EOF
+sed -n \
+    -e 's/^(gdb) //' \
+    -e '/^#/p' \
+    -e '/^Thread/p'