use of com.android.tools.idea.run.tasks.ConnectJavaDebuggerTask in project intellij by bazelbuild.
the class ConnectBlazeTestDebuggerTask method launchDebugger.
/**
* Nearly a clone of {@link ConnectJavaDebuggerTask#launchDebugger}. There are a few changes to
* account for null variables that could occur in our implementation.
*/
@Override
public ProcessHandler launchDebugger(@NotNull LaunchInfo currentLaunchInfo, @NotNull Client client, @NotNull ProcessHandlerLaunchStatus launchStatus, @NotNull ProcessHandlerConsolePrinter printer) {
String debugPort = Integer.toString(client.getDebuggerListenPort());
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));
// create a new process handler
RemoteConnection connection = new RemoteConnection(true, "localhost", debugPort, false);
RemoteDebugProcessHandler debugProcessHandler = new RemoteDebugProcessHandler(project);
// 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
ProcessHandler oldProcessHandler = launchStatus.getProcessHandler();
launchStatus.setProcessHandler(debugProcessHandler);
printer.setProcessHandler(debugProcessHandler);
// Detach old process handler after the launch status
// has been updated to point to the new process handler.
oldProcessHandler.detachProcess();
AndroidDebugState debugState = new AndroidDebugState(project, debugProcessHandler, connection, currentLaunchInfo.consoleProvider);
RunContentDescriptor oldDescriptor;
AndroidSessionInfo oldSession = oldProcessHandler.getUserData(AndroidSessionInfo.KEY);
if (oldSession != null) {
oldDescriptor = oldSession.getDescriptor();
} else {
// This is the first time we are attaching the debugger; get it from the environment instead.
oldDescriptor = currentLaunchInfo.env.getContentToReuse();
}
RunContentDescriptor debugDescriptor;
try {
// @formatter:off
ExecutionEnvironment debugEnv = new ExecutionEnvironmentBuilder(currentLaunchInfo.env).executor(currentLaunchInfo.executor).runner(currentLaunchInfo.runner).contentToReuse(oldDescriptor).build();
debugDescriptor = DebuggerPanelsManager.getInstance(project).attachVirtualMachine(debugEnv, debugState, connection, false);
// @formatter:on
} catch (ExecutionException e) {
printer.stderr("ExecutionException: " + e.getMessage() + '.');
return null;
}
// Based on the above try block we shouldn't get here unless we have assigned to debugDescriptor
assert debugDescriptor != 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(oldProcessHandler);
if (oldText != null) {
oldText.printTo(debugProcessHandler);
}
RunProfile runProfile = currentLaunchInfo.env.getRunProfile();
int uniqueId = runProfile instanceof AndroidRunConfigurationBase ? ((AndroidRunConfigurationBase) runProfile).getUniqueID() : -1;
AndroidSessionInfo value = new AndroidSessionInfo(debugProcessHandler, debugDescriptor, uniqueId, currentLaunchInfo.executor.getId(), currentLaunchInfo.executor.getActionName(), false);
debugProcessHandler.putUserData(AndroidSessionInfo.KEY, value);
debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEBUG_CLIENT, client);
debugProcessHandler.putUserData(AndroidSessionInfo.ANDROID_DEVICE_API_LEVEL, client.getDevice().getVersion());
return debugProcessHandler;
}
Aggregations