From 041b35b8d90643b7606bd6421cc21e419421b353 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Mon, 6 Feb 2023 14:44:58 -0800 Subject: [PATCH] Add '-k' "always kill" option to am monitor Test: atest CtsShortFgsTestCases Bug: 266521938 Change-Id: I025d12104a14d5f7a950f4b793bf3d466ba5e988 --- .../am/ActivityManagerShellCommand.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java index db283c7d09dff..95ba72e3670d1 100644 --- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java +++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java @@ -1424,6 +1424,7 @@ final class ActivityManagerShellCommand extends ShellCommand { final boolean mSimpleMode; final String mTarget; final boolean mAlwaysContinue; + final boolean mAlwaysKill; static final int STATE_NORMAL = 0; static final int STATE_CRASHED = 1; @@ -1452,7 +1453,7 @@ final class ActivityManagerShellCommand extends ShellCommand { MyActivityController(IActivityManager iam, PrintWriter pw, InputStream input, String gdbPort, boolean monkey, boolean simpleMode, String target, - boolean alwaysContinue) { + boolean alwaysContinue, boolean alwaysKill) { mInterface = iam; mPw = pw; mInput = input; @@ -1461,6 +1462,7 @@ final class ActivityManagerShellCommand extends ShellCommand { mSimpleMode = simpleMode; mTarget = target; mAlwaysContinue = alwaysContinue; + mAlwaysKill = alwaysKill; } private boolean shouldHandlePackageOrProcess(String packageOrProcess) { @@ -1519,6 +1521,9 @@ final class ActivityManagerShellCommand extends ShellCommand { if (mAlwaysContinue) { return true; } + if (mAlwaysKill) { + return false; + } int result = waitControllerLocked(pid, STATE_CRASHED); return result == RESULT_CRASH_KILL ? false : true; } @@ -1543,6 +1548,9 @@ final class ActivityManagerShellCommand extends ShellCommand { if (mAlwaysContinue) { return 0; } + if (mAlwaysKill) { + return -1; + } int result = waitControllerLocked(pid, STATE_EARLY_ANR); if (result == RESULT_EARLY_ANR_KILL) return -1; return 0; @@ -1570,6 +1578,9 @@ final class ActivityManagerShellCommand extends ShellCommand { if (mAlwaysContinue) { return 0; } + if (mAlwaysKill) { + return -1; + } int result = waitControllerLocked(pid, STATE_ANR); if (result == RESULT_ANR_KILL) return -1; if (result == RESULT_ANR_WAIT) return 1; @@ -1694,7 +1705,7 @@ final class ActivityManagerShellCommand extends ShellCommand { } void printMessageForState() { - if (mAlwaysContinue && mSimpleMode) { + if ((mAlwaysContinue || mAlwaysKill) && mSimpleMode) { return; // In the simplest mode, we don't need to show anything. } switch (mState) { @@ -1794,6 +1805,7 @@ final class ActivityManagerShellCommand extends ShellCommand { boolean monkey = false; boolean simpleMode = false; boolean alwaysContinue = false; + boolean alwaysKill = false; String target = null; while ((opt=getNextOption()) != null) { @@ -1807,14 +1819,21 @@ final class ActivityManagerShellCommand extends ShellCommand { simpleMode = true; } else if (opt.equals("-c")) { alwaysContinue = true; + } else if (opt.equals("-k")) { + alwaysKill = true; } else { getErrPrintWriter().println("Error: Unknown option: " + opt); return -1; } } + if (alwaysContinue && alwaysKill) { + getErrPrintWriter().println("Error: -k and -c options can't be used together."); + return -1; + } MyActivityController controller = new MyActivityController(mInterface, pw, - getRawInputStream(), gdbPort, monkey, simpleMode, target, alwaysContinue); + getRawInputStream(), gdbPort, monkey, simpleMode, target, alwaysContinue, + alwaysKill); controller.run(); return 0; } @@ -4093,12 +4112,14 @@ final class ActivityManagerShellCommand extends ShellCommand { pw.println(" make-uid-idle [--user | all | current] "); pw.println(" If the given application's uid is in the background and waiting to"); pw.println(" become idle (not allowing background services), do that now."); - pw.println(" monitor [--gdb ] [-p ] [-s] [-c]"); + pw.println(" monitor [--gdb ] [-p ] [-s] [-c] [-k]"); pw.println(" Start monitoring for crashes or ANRs."); pw.println(" --gdb: start gdbserv on the given port at crash/ANR"); pw.println(" -p: only show events related to a specific process / package"); pw.println(" -s: simple mode, only show a summary line for each event"); pw.println(" -c: assume the input is always [c]ontinue"); + pw.println(" -k: assume the input is always [k]ill"); + pw.println(" -c and -k are mutually exclusive."); pw.println(" watch-uids [--oom ] [--mask ]"); pw.println(" Start watching for and reporting uid state changes."); pw.println(" --oom: specify a uid for which to report detailed change messages.");