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

Commit c926d459 authored by Fabien Sanglard's avatar Fabien Sanglard
Browse files

Enabled suspended app debug via am flag "--suspend"

Test: NA
Bug: 265460189

Change-Id: I6a44f06363a1f6a954af64c769044cc4d4a4e140
parent 718b40dd
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -403,6 +403,13 @@ public class ActivityManager {
     */
    public static final int START_FLAG_NATIVE_DEBUGGING = 1<<3;

    /**
     * Flag for IActivityManaqer.startActivity: launch the app for
     * debugging and suspend threads.
     * @hide
     */
    public static final int START_FLAG_DEBUG_SUSPEND = 1 << 4;

    /**
     * Result for IActivityManaqer.broadcastIntent: success!
     * @hide
+47 −17
Original line number Diff line number Diff line
@@ -6872,26 +6872,13 @@ public final class ActivityThread extends ClientTransactionHandler
        final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
        final StrictMode.ThreadPolicy writesAllowedPolicy = StrictMode.getThreadPolicy();

        // Wait for debugger after we have notified the system to finish attach application
        if (data.debugMode != ApplicationThreadConstants.DEBUG_OFF) {
            if (data.debugMode == ApplicationThreadConstants.DEBUG_WAIT) {
                Slog.w(TAG, "Application " + data.info.getPackageName()
                        + " is waiting for the debugger ...");

                try {
                    mgr.showWaitingForDebugger(mAppThread, true);
                } catch (RemoteException ex) {
                    throw ex.rethrowFromSystemServer();
                }

                Debug.waitForDebugger();

                try {
                    mgr.showWaitingForDebugger(mAppThread, false);
                } catch (RemoteException ex) {
                    throw ex.rethrowFromSystemServer();
                }
                waitForDebugger(data);
            } else if (data.debugMode == ApplicationThreadConstants.DEBUG_SUSPEND) {
                suspendAllAndSendVmStart(data);
            }
            // Nothing special to do in case of DEBUG_ON.
        }

        try {
@@ -6975,6 +6962,49 @@ public final class ActivityThread extends ClientTransactionHandler
        }
    }

    @UnsupportedAppUsage
    private void waitForDebugger(AppBindData data) {
        final IActivityManager mgr = ActivityManager.getService();
        Slog.w(TAG, "Application " + data.info.getPackageName()
                         + " is waiting for the debugger ...");

        try {
            mgr.showWaitingForDebugger(mAppThread, true);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }

        Debug.waitForDebugger();

        try {
            mgr.showWaitingForDebugger(mAppThread, false);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

    @UnsupportedAppUsage
    private void suspendAllAndSendVmStart(AppBindData data) {
        final IActivityManager mgr = ActivityManager.getService();
        Slog.w(TAG, "Application " + data.info.getPackageName()
                         + " is suspending. Debugger needs to resume to continue.");

        try {
            mgr.showWaitingForDebugger(mAppThread, true);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }

        Debug.suspendAllAndSendVmStart();

        try {
            mgr.showWaitingForDebugger(mAppThread, false);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }


    private void handleSetContentCaptureOptionsCallback(String packageName) {
        if (mContentCaptureOptionsCallback != null) {
            return;
+2 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ public final class ApplicationThreadConstants {
    public static final int DEBUG_OFF = 0;
    public static final int DEBUG_ON = 1;
    public static final int DEBUG_WAIT = 2;
    public static final int DEBUG_SUSPEND = 3;

    // the package has been removed, clean up internal references
    public static final int PACKAGE_REMOVED = 0;
+22 −3
Original line number Diff line number Diff line
@@ -1439,6 +1439,9 @@ public class ActivityManagerService extends IActivityManager.Stub
    @GuardedBy("this")
    private boolean mWaitForDebugger = false;
    @GuardedBy("this")
    private boolean mSuspendUponWait = false;
    @GuardedBy("this")
    private boolean mDebugTransient = false;
@@ -5009,9 +5012,15 @@ public class ActivityManagerService extends IActivityManager.Stub
        try {
            int testMode = ApplicationThreadConstants.DEBUG_OFF;
            if (mDebugApp != null && mDebugApp.equals(processName)) {
                testMode = mWaitForDebugger
                    ? ApplicationThreadConstants.DEBUG_WAIT
                    : ApplicationThreadConstants.DEBUG_ON;
                if (mWaitForDebugger) {
                    if (mSuspendUponWait) {
                        testMode = ApplicationThreadConstants.DEBUG_SUSPEND;
                    } else {
                        testMode = ApplicationThreadConstants.DEBUG_WAIT;
                    }
                } else {
                    testMode = ApplicationThreadConstants.DEBUG_ON;
                }
                app.setDebugging(true);
                if (mDebugTransient) {
                    mDebugApp = mOrigDebugApp;
@@ -7158,6 +7167,11 @@ public class ActivityManagerService extends IActivityManager.Stub
    public void setDebugApp(String packageName, boolean waitForDebugger,
            boolean persistent) {
        setDebugApp(packageName, waitForDebugger, persistent, false);
    }
    private void setDebugApp(String packageName, boolean waitForDebugger,
            boolean persistent, boolean suspendUponWait) {
        enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
                "setDebugApp()");
@@ -7183,6 +7197,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                }
                mDebugApp = packageName;
                mWaitForDebugger = waitForDebugger;
                mSuspendUponWait = suspendUponWait;
                mDebugTransient = !persistent;
                if (packageName != null) {
                    forceStopPackageLocked(packageName, -1, false, false, true, true,
@@ -17785,6 +17800,10 @@ public class ActivityManagerService extends IActivityManager.Stub
                        setDebugApp(aInfo.processName, true, false);
                    }
                    if ((startFlags & ActivityManager.START_FLAG_DEBUG_SUSPEND) != 0) {
                        setDebugApp(aInfo.processName, true, false, true);
                    }
                    if ((startFlags & ActivityManager.START_FLAG_NATIVE_DEBUGGING) != 0) {
                        setNativeDebuggingAppLocked(aInfo.applicationInfo, aInfo.processName);
                    }
+2 −0
Original line number Diff line number Diff line
@@ -411,6 +411,8 @@ final class ActivityManagerShellCommand extends ShellCommand {
            public boolean handleOption(String opt, ShellCommand cmd) {
                if (opt.equals("-D")) {
                    mStartFlags |= ActivityManager.START_FLAG_DEBUG;
                } else if (opt.equals("--suspend")) {
                    mStartFlags |= ActivityManager.START_FLAG_DEBUG_SUSPEND;
                } else if (opt.equals("-N")) {
                    mStartFlags |= ActivityManager.START_FLAG_NATIVE_DEBUGGING;
                } else if (opt.equals("-W")) {