Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa178e14 authored by Andreas Gampe's avatar Andreas Gampe
Browse files

Frameworks: Change Java stack trace dumping

When dumping stacks, e.g., for ANRs, even if the "Java" version from ART
reports success, check that output was generated, and fall back to a
"native" dump if the output is too small, currently set at one hundred
bytes.

Add more logging of failure paths.

Fix some error logging in native to go to logcat instead of stderr.

Bug: 133525168
Test: m
Test: manual (induce ANRs with various platform changes)
Exempt-From-Owner-Approval: Cherry-pick
Merged-In: I0760207229989f6f34e10edcf45fb140bc81d0e5
Change-Id: I0760207229989f6f34e10edcf45fb140bc81d0e5
parent 6210ac61
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -713,7 +713,7 @@ static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs
                                     O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND,
                                     0666));
    if (fd < 0) {
        fprintf(stderr, "Can't open %s: %s\n", fileNameChars.c_str(), strerror(errno));
        PLOG(ERROR) << "Can't open " << fileNameChars.c_str();
        return false;
    }

+23 −3
Original line number Diff line number Diff line
@@ -547,6 +547,7 @@ public class ActivityManagerService extends IActivityManager.Stub
    private static final int MAX_BUGREPORT_TITLE_SIZE = 50;
    private static final int NATIVE_DUMP_TIMEOUT_MS = 2000; // 2 seconds;
    private static final int JAVA_DUMP_MINIMUM_SIZE = 100; // 100 bytes.
    OomAdjuster mOomAdjuster;
    final LowMemDetector mLowMemDetector;
@@ -3805,9 +3806,28 @@ public class ActivityManagerService extends IActivityManager.Stub
     */
    private static long dumpJavaTracesTombstoned(int pid, String fileName, long timeoutMs) {
        final long timeStart = SystemClock.elapsedRealtime();
        if (!Debug.dumpJavaBacktraceToFileTimeout(pid, fileName, (int) (timeoutMs / 1000))) {
            Debug.dumpNativeBacktraceToFileTimeout(pid, fileName,
                    (NATIVE_DUMP_TIMEOUT_MS / 1000));
        boolean javaSuccess = Debug.dumpJavaBacktraceToFileTimeout(pid, fileName,
                (int) (timeoutMs / 1000));
        if (javaSuccess) {
            // Check that something is in the file, actually. Try-catch should not be necessary,
            // but better safe than sorry.
            try {
                long size = new File(fileName).length();
                if (size < JAVA_DUMP_MINIMUM_SIZE) {
                    Slog.w(TAG, "Successfully created Java ANR file is empty!");
                    javaSuccess = false;
                }
            } catch (Exception e) {
                Slog.w(TAG, "Unable to get ANR file size", e);
                javaSuccess = false;
            }
        }
        if (!javaSuccess) {
            Slog.w(TAG, "Dumping Java threads failed, initiating native stack dump.");
            if (!Debug.dumpNativeBacktraceToFileTimeout(pid, fileName,
                    (NATIVE_DUMP_TIMEOUT_MS / 1000))) {
                Slog.w(TAG, "Native stack dump failed!");
            }
        }
        return SystemClock.elapsedRealtime() - timeStart;