Include important native processes in watchdog stacks.

Helps us track down deadlocks involving native service processes.

Bug: 6615693
Change-Id: I580047550772e29586195a8cf440141574e3f40c
diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp
index 9586717..6724f36 100644
--- a/core/jni/android_os_Debug.cpp
+++ b/core/jni/android_os_Debug.cpp
@@ -17,8 +17,11 @@
 #define LOG_TAG "android.os.Debug"
 #include "JNIHelp.h"
 #include "jni.h"
+#include <utils/String8.h>
 #include "utils/misc.h"
+#include "cutils/debugger.h"
 
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -538,6 +541,35 @@
 }
 
 
+static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
+    jint pid, jstring fileName)
+{
+    if (fileName == NULL) {
+        jniThrowNullPointerException(env, NULL);
+        return;
+    }
+    const jchar* str = env->GetStringCritical(fileName, 0);
+    String8 fileName8;
+    if (str) {
+        fileName8 = String8(str, env->GetStringLength(fileName));
+        env->ReleaseStringCritical(fileName, str);
+    }
+
+    int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666);  /* -rw-rw-rw- */
+    if (fd < 0) {
+        fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
+        return;
+    }
+
+    if (lseek(fd, 0, SEEK_END) < 0) {
+        fprintf(stderr, "lseek: %s\n", strerror(errno));
+    } else {
+        dump_backtrace_to_file(pid, fd);
+    }
+
+    close(fd);
+}
+
 /*
  * JNI registration.
  */
@@ -569,6 +601,8 @@
             (void*)android_os_Debug_getProxyObjectCount },
     { "getBinderDeathObjectCount", "()I",
             (void*)android_os_Debug_getDeathObjectCount },
+    { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
+            (void*)android_os_Debug_dumpNativeBacktraceToFile },
 };
 
 int register_android_os_Debug(JNIEnv *env)