From 3103fbdd74d85da9ac3e76a0d6b914eec98e79cc Mon Sep 17 00:00:00 2001 From: Yisroel Forta Date: Thu, 19 May 2022 19:07:19 +0000 Subject: Add ANR annotation to AppExitInfo reason Bug: 220354436 Test: force anr, check field "reason" user request after error: Input dispatching timed out (b2c1d00 com.example.myapplication/com.example.myapplication.MainActivity (server) is not responding. Waited 5004ms for MotionEvent(deviceId=4, eventTime=24557057000, source=TOUCHSCREEN, displayId=0, action=DOWN, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, classification=NONE, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, xCursorPosition=nan, yCursorPosition=nan, pointers=[0: (933.0, 1926.0)]), policyFlags=0x62000000) Change-Id: Ibb317ffe5b21b52d4bef9ee27f072647f147c20b --- .../android/server/am/ProcessErrorStateRecord.java | 21 +++++++++++++++++++++ .../java/com/android/server/am/ProcessRecord.java | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/services/core/java/com/android/server/am/ProcessErrorStateRecord.java b/services/core/java/com/android/server/am/ProcessErrorStateRecord.java index 7fe270084e25..653ece4a883c 100644 --- a/services/core/java/com/android/server/am/ProcessErrorStateRecord.java +++ b/services/core/java/com/android/server/am/ProcessErrorStateRecord.java @@ -125,6 +125,12 @@ class ProcessErrorStateRecord { @CompositeRWLock({"mService", "mProcLock"}) private AppNotRespondingDialog.Data mAnrData; + /** + * Annotation from process killed due to an ANR. + */ + @GuardedBy("mService") + private String mAnrAnnotation; + /** * Optional local handler to be invoked in the process crash. */ @@ -193,6 +199,16 @@ class ProcessErrorStateRecord { mCrashingReport = crashingReport; } + @GuardedBy("mService") + String getAnrAnnotation() { + return mAnrAnnotation; + } + + @GuardedBy("mService") + void setAnrAnnotation(String anrAnnotation) { + mAnrAnnotation = anrAnnotation; + } + @GuardedBy(anyOf = {"mService", "mProcLock"}) ActivityManager.ProcessErrorStateInfo getNotRespondingReport() { return mNotRespondingReport; @@ -243,6 +259,8 @@ class ProcessErrorStateRecord { mApp.getWindowProcessController().appEarlyNotResponding(annotation, () -> { synchronized (mService) { + // Store annotation here as instance below races with this killLocked. + setAnrAnnotation(annotation); mApp.killLocked("anr", ApplicationExitInfo.REASON_ANR, true); } }); @@ -256,6 +274,9 @@ class ProcessErrorStateRecord { final int pid = mApp.getPid(); final UUID errorId; synchronized (mService) { + // Store annotation here as instance above will not be hit on all paths. + setAnrAnnotation(annotation); + // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down. if (mService.mAtmInternal.isShuttingDown()) { Slog.i(TAG, "During shutdown skipping ANR: " + this + " " + annotation); diff --git a/services/core/java/com/android/server/am/ProcessRecord.java b/services/core/java/com/android/server/am/ProcessRecord.java index ac5cb2da6945..07b6fcdcb0ca 100644 --- a/services/core/java/com/android/server/am/ProcessRecord.java +++ b/services/core/java/com/android/server/am/ProcessRecord.java @@ -1071,6 +1071,10 @@ class ProcessRecord implements WindowProcessListener { @SubReason int subReason, boolean noisy) { if (!mKilledByAm) { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "kill"); + if (reasonCode == ApplicationExitInfo.REASON_ANR + && mErrorState.getAnrAnnotation() != null) { + description = description + ": " + mErrorState.getAnrAnnotation(); + } if (mService != null && (noisy || info.uid == mService.mCurOomAdjUid)) { mService.reportUidInfoMessageLocked(TAG, "Killing " + toShortString() + " (adj " + mState.getSetAdj() -- cgit v1.2.3-59-g8ed1b