From e02e3ea7f97fe9554a9ff1464a2875b7d07a19bf Mon Sep 17 00:00:00 2001 From: songjinshi Date: Fri, 16 Dec 2016 17:48:21 +0800 Subject: [PATCH] [Debug]: Add timeout for dumpNativeBacktraceToFile. If the debuggerd be blocked, the watchdog and activitymanager thread will be blocked when the ANR or watchdog occurs,so we needed to add timeout for dumpNativeBacktraceToFile. Change-Id: Iab1a64328e70257025d860638d93a4fb8eaaeea2 Signed-off-by: songjinshi --- core/java/android/os/Debug.java | 8 +++++--- core/jni/android_os_Debug.cpp | 11 +++++------ .../com/android/server/am/ActivityManagerService.java | 3 ++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java index 210ddb6cd3970..e05bd89079afb 100644 --- a/core/java/android/os/Debug.java +++ b/core/java/android/os/Debug.java @@ -2219,11 +2219,13 @@ public final class Debug } /** - * Have the stack traces of the given native process dumped to the - * specified file. Will be appended to the file. + * Append the stack traces of a given native process to a specified file. + * @param pid pid to dump. + * @param file path of file to append dump to. + * @param timeoutSecs time to wait in seconds, or 0 to wait forever. * @hide */ - public static native void dumpNativeBacktraceToFile(int pid, String file); + public static native void dumpNativeBacktraceToFileTimeout(int pid, String file, int timeoutSecs); /** * Get description of unreachable native memory. diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp index cbe2bbae80daa..3a2df75c90f91 100644 --- a/core/jni/android_os_Debug.cpp +++ b/core/jni/android_os_Debug.cpp @@ -1012,9 +1012,8 @@ static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz, ALOGD("Native heap dump complete.\n"); } - -static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz, - jint pid, jstring fileName) +static void android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz, + jint pid, jstring fileName, jint timeoutSecs) { if (fileName == NULL) { jniThrowNullPointerException(env, "file == null"); @@ -1037,7 +1036,7 @@ static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject claz if (lseek(fd, 0, SEEK_END) < 0) { fprintf(stderr, "lseek: %s\n", strerror(errno)); } else { - dump_backtrace_to_file(pid, fd); + dump_backtrace_to_file_timeout(pid, fd, timeoutSecs); } close(fd); @@ -1083,8 +1082,8 @@ static const JNINativeMethod gMethods[] = { (void*)android_os_Debug_getProxyObjectCount }, { "getBinderDeathObjectCount", "()I", (void*)android_os_Debug_getDeathObjectCount }, - { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V", - (void*)android_os_Debug_dumpNativeBacktraceToFile }, + { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)V", + (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout }, { "getUnreachableMemory", "(IZ)Ljava/lang/String;", (void*)android_os_Debug_getUnreachableMemory }, }; diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 40617c8bc04d1..00fe3379c423b 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -5357,7 +5357,8 @@ public final class ActivityManagerService extends ActivityManagerNative for (int pid : pids) { if (DEBUG_ANR) Slog.d(TAG, "Collecting stacks for native pid " + pid); final long sime = SystemClock.elapsedRealtime(); - Debug.dumpNativeBacktraceToFile(pid, tracesPath); + + Debug.dumpNativeBacktraceToFileTimeout(pid, tracesPath, 10); if (DEBUG_ANR) Slog.d(TAG, "Done with native pid " + pid + " in " + (SystemClock.elapsedRealtime()-sime) + "ms"); }