Search in sources :

Example 21 with RunProfile

use of com.intellij.execution.configurations.RunProfile in project android by JetBrains.

the class ConnectJavaDebuggerTask method launchDebugger.

@Override
public ProcessHandler launchDebugger(@NotNull LaunchInfo currentLaunchInfo, @NotNull final Client client, @NotNull ProcessHandlerLaunchStatus launchStatus, @NotNull ProcessHandlerConsolePrinter printer) {
    String debugPort = Integer.toString(client.getDebuggerListenPort());
    final int pid = client.getClientData().getPid();
    Logger.getInstance(ConnectJavaDebuggerTask.class).info(String.format(Locale.US, "Attempting to connect debugger to port %1$s [client %2$d]", debugPort, pid));
    // detach old process handler
    RunContentDescriptor descriptor = currentLaunchInfo.env.getContentToReuse();
    // reach here before the EDT gets around to creating the descriptor?
    assert descriptor != null : "ConnectJavaDebuggerTask expects an existing descriptor that will be reused";
    final ProcessHandler processHandler = descriptor.getProcessHandler();
    assert processHandler != null;
    // create a new process handler
    RemoteConnection connection = new RemoteConnection(true, "localhost", debugPort, false);
    final AndroidRemoteDebugProcessHandler debugProcessHandler = new AndroidRemoteDebugProcessHandler(myProject, myMonitorRemoteProcess);
    // switch the launch status and console printers to point to the new process handler
    // this is required, esp. for AndroidTestListener which holds a reference to the launch status and printers, and those should
    // be updated to point to the new process handlers, otherwise test results will not be forwarded appropriately
    launchStatus.setProcessHandler(debugProcessHandler);
    printer.setProcessHandler(debugProcessHandler);
    // detach after the launch status has been updated to point to the new process handler
    processHandler.detachProcess();
    AndroidDebugState debugState = new AndroidDebugState(myProject, debugProcessHandler, connection, currentLaunchInfo.consoleProvider);
    RunContentDescriptor debugDescriptor;
    try {
        // @formatter:off
        ExecutionEnvironment debugEnv = new ExecutionEnvironmentBuilder(currentLaunchInfo.env).executor(currentLaunchInfo.executor).runner(currentLaunchInfo.runner).contentToReuse(descriptor).build();
        debugDescriptor = DebuggerPanelsManager.getInstance(myProject).attachVirtualMachine(debugEnv, debugState, connection, false);
    // @formatter:on
    } catch (ExecutionException e) {
        processHandler.notifyTextAvailable("ExecutionException: " + e.getMessage() + '.', STDERR);
        return null;
    }
    if (debugDescriptor == null) {
        processHandler.notifyTextAvailable("Unable to connect debugger to Android application", STDERR);
        return null;
    }
    // re-run the collected text from the old process handler to the new
    // TODO: is there a race between messages received once the debugger has been connected, and these messages that are printed out?
    final AndroidProcessText oldText = AndroidProcessText.get(processHandler);
    if (oldText != null) {
        oldText.printTo(debugProcessHandler);
    }
    RunProfile runProfile = currentLaunchInfo.env.getRunProfile();
    int uniqueId = runProfile instanceof RunConfigurationBase ? ((RunConfigurationBase) runProfile).getUniqueID() : -1;
    AndroidSessionInfo value = new AndroidSessionInfo(debugProcessHandler, debugDescriptor, uniqueId, currentLaunchInfo.executor.getId(), InstantRunUtils.isInstantRunEnabled(currentLaunchInfo.env));
    debugProcessHandler.putUserData(AndroidSessionInfo.KEY, value);
    debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEBUG_CLIENT, client);
    debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEVICE_API_LEVEL, client.getDevice().getVersion());
    final String pkgName = client.getClientData().getClientDescription();
    final IDevice device = client.getDevice();
    // kill the process when the debugger is stopped
    debugProcessHandler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(ProcessEvent event) {
            debugProcessHandler.removeProcessListener(this);
            Client currentClient = device.getClient(pkgName);
            if (currentClient != null && currentClient.getClientData().getPid() != pid) {
                // a new process has been launched for the same package name, we aren't interested in killing this
                return;
            }
            Logger.getInstance(ConnectJavaDebuggerTask.class).info("Debugger terminating, so terminating process: " + pkgName);
            // Note: client.kill() doesn't work when the debugger is attached, we explicitly stop by package id..
            try {
                device.executeShellCommand("am force-stop " + pkgName, new NullOutputReceiver());
            } catch (Exception e) {
            // don't care..
            }
        }
    });
    return debugProcessHandler;
}
Also used : ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) RunContentDescriptor(com.intellij.execution.ui.RunContentDescriptor) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) IDevice(com.android.ddmlib.IDevice) RunProfile(com.intellij.execution.configurations.RunProfile) ExecutionException(com.intellij.execution.ExecutionException) RunConfigurationBase(com.intellij.execution.configurations.RunConfigurationBase) ProcessHandler(com.intellij.execution.process.ProcessHandler) RemoteConnection(com.intellij.execution.configurations.RemoteConnection) ExecutionEnvironmentBuilder(com.intellij.execution.runners.ExecutionEnvironmentBuilder) ExecutionException(com.intellij.execution.ExecutionException) Client(com.android.ddmlib.Client) NullOutputReceiver(com.android.ddmlib.NullOutputReceiver)

Aggregations

RunProfile (com.intellij.execution.configurations.RunProfile)21 NotNull (org.jetbrains.annotations.NotNull)7 ProcessHandler (com.intellij.execution.process.ProcessHandler)6 Nullable (org.jetbrains.annotations.Nullable)6 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)5 Project (com.intellij.openapi.project.Project)5 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)4 RunConfigurationBase (com.intellij.execution.configurations.RunConfigurationBase)4 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)4 ExecutionException (com.intellij.execution.ExecutionException)3 ExecutionResult (com.intellij.execution.ExecutionResult)3 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)3 ProcessEvent (com.intellij.execution.process.ProcessEvent)3 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)3 Module (com.intellij.openapi.module.Module)3 DefaultExecutionResult (com.intellij.execution.DefaultExecutionResult)2 CompatibilityAwareRunProfile (com.intellij.execution.configuration.CompatibilityAwareRunProfile)2 RunProfileState (com.intellij.execution.configurations.RunProfileState)2 RuntimeConfigurationError (com.intellij.execution.configurations.RuntimeConfigurationError)2 ProgramRunner (com.intellij.execution.runners.ProgramRunner)2