Use GDB to dump threads in test timeouts, if available.

GDB is able to dump threads which may not be responding to the SIGQUIT
thread dump.

Bug: 23485091
Change-Id: Ib80db4e63258887f3b7c313ed5e6eb9e6300ac4c
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 49451ad..431ba95 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -20,6 +20,7 @@
 #include <sys/mount.h>
 #ifdef __linux__
 #include <linux/fs.h>
+#include <sys/prctl.h>
 #endif
 
 #define ATRACE_TAG ATRACE_TAG_DALVIK
@@ -492,6 +493,13 @@
 
   CHECK(!no_sig_chain_) << "A started runtime should have sig chain enabled";
 
+  // If a debug host build, disable ptrace restriction for debugging and test timeout thread dump.
+#if defined(__linux__) && !defined(__ANDROID__)
+  if (kIsDebugBuild) {
+    CHECK_EQ(prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY), 0);
+  }
+#endif
+
   // Restore main thread state to kNative as expected by native code.
   Thread* self = Thread::Current();