Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public final class DebugSettings {
public ClassFilters exceptionFilters = new ClassFilters();
public boolean exceptionFiltersUpdated = false;
public int limitOfVariablesPerJdwpRequest = 100;
public int jdwpRequestTimeout = 3000;

public static DebugSettings getCurrent() {
return current;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright (c) 2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package com.microsoft.java.debug.core.adapter;

public interface IVirtualMachineManager extends com.sun.jdi.VirtualMachineManager {
boolean connectVirtualMachine(com.sun.jdi.VirtualMachine vm);

boolean disconnectVirtualMachine(com.sun.jdi.VirtualMachine vm);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation and others.
* Copyright (c) 2017-2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -42,6 +42,7 @@

public class AttachRequestHandler implements IDebugRequestHandler {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private VMHandler vmHandler = new VMHandler();

@Override
public List<Command> getTargetCommands() {
Expand All @@ -57,12 +58,14 @@ public CompletableFuture<Response> handle(Command command, Arguments arguments,
context.setStepFilters(attachArguments.stepFilters);

IVirtualMachineManagerProvider vmProvider = context.getProvider(IVirtualMachineManagerProvider.class);
vmHandler.setVmProvider(vmProvider);

try {
logger.info(String.format("Trying to attach to remote debuggee VM %s:%d .", attachArguments.hostName, attachArguments.port));
IDebugSession debugSession = DebugUtility.attach(vmProvider.getVirtualMachineManager(), attachArguments.hostName, attachArguments.port,
attachArguments.timeout);
context.setDebugSession(debugSession);
vmHandler.connectVirtualMachine(debugSession.getVM());
logger.info("Attaching to debuggee VM succeeded.");

// If the debugger and debuggee run at the different JVM platforms, show a warning message.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation and others.
* Copyright (c) 2017-2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -27,6 +27,7 @@
import com.microsoft.java.debug.core.adapter.IDebugAdapterContext;
import com.microsoft.java.debug.core.adapter.IDebugRequestHandler;
import com.microsoft.java.debug.core.adapter.IEvaluationProvider;
import com.microsoft.java.debug.core.adapter.IVirtualMachineManagerProvider;
import com.microsoft.java.debug.core.protocol.Events;
import com.microsoft.java.debug.core.protocol.Messages.Response;
import com.microsoft.java.debug.core.protocol.Requests.Arguments;
Expand All @@ -43,6 +44,7 @@

public class ConfigurationDoneRequestHandler implements IDebugRequestHandler {
protected static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private VMHandler vmHandler = new VMHandler();

@Override
public List<Command> getTargetCommands() {
Expand All @@ -52,6 +54,7 @@ public List<Command> getTargetCommands() {
@Override
public CompletableFuture<Response> handle(Command command, Arguments arguments, Response response, IDebugAdapterContext context) {
IDebugSession debugSession = context.getDebugSession();
vmHandler.setVmProvider(context.getProvider(IVirtualMachineManagerProvider.class));
if (debugSession != null) {
// This is a global event handler to handle the JDI Event from Virtual Machine.
debugSession.getEventHub().events().subscribe(debugEvent -> {
Expand All @@ -76,9 +79,11 @@ private void handleDebugEvent(DebugEvent debugEvent, IDebugSession debugSession,
});
}
} else if (event instanceof VMDeathEvent) {
vmHandler.disconnectVirtualMachine(event.virtualMachine());
context.setVmTerminated();
context.getProtocolServer().sendEvent(new Events.ExitedEvent(0));
} else if (event instanceof VMDisconnectEvent) {
vmHandler.disconnectVirtualMachine(event.virtualMachine());
if (context.isAttached()) {
context.setVmTerminated();
context.getProtocolServer().sendEvent(new Events.TerminatedEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ public class LaunchWithDebuggingDelegate implements ILaunchDelegate {
private static final int ATTACH_TERMINAL_TIMEOUT = 20 * 1000;
private static final String TERMINAL_TITLE = "Java Debug Console";
protected static final long RUNINTERMINAL_TIMEOUT = 10 * 1000;
private VMHandler vmHandler = new VMHandler();

@Override
public CompletableFuture<Response> launchInTerminal(LaunchArguments launchArguments, Response response, IDebugAdapterContext context) {
CompletableFuture<Response> resultFuture = new CompletableFuture<>();

IVirtualMachineManagerProvider vmProvider = context.getProvider(IVirtualMachineManagerProvider.class);
vmHandler.setVmProvider(vmProvider);
final String launchInTerminalErrorFormat = "Failed to launch debuggee in terminal. Reason: %s";

try {
Expand Down Expand Up @@ -101,6 +103,7 @@ public CompletableFuture<Response> launchInTerminal(LaunchArguments launchArgume
if (runResponse.success) {
try {
VirtualMachine vm = listenConnector.accept(args);
vmHandler.connectVirtualMachine(vm);
context.setDebugSession(new DebugSession(vm));
logger.info("Launching debuggee in terminal console succeeded.");
resultFuture.complete(response);
Expand Down Expand Up @@ -172,6 +175,7 @@ public CompletableFuture<Response> launchInTerminal(LaunchArguments launchArgume
public Process launch(LaunchArguments launchArguments, IDebugAdapterContext context)
throws IOException, IllegalConnectorArgumentsException, VMStartException {
IVirtualMachineManagerProvider vmProvider = context.getProvider(IVirtualMachineManagerProvider.class);
vmHandler.setVmProvider(vmProvider);

IDebugSession debugSession = DebugUtility.launch(
vmProvider.getVirtualMachineManager(),
Expand All @@ -184,6 +188,7 @@ public Process launch(LaunchArguments launchArguments, IDebugAdapterContext cont
LaunchRequestHandler.constructEnvironmentVariables(launchArguments),
launchArguments.javaExec);
context.setDebugSession(debugSession);
vmHandler.connectVirtualMachine(debugSession.getVM());

logger.info("Launching debuggee VM succeeded.");
return debugSession.process();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package com.microsoft.java.debug.core.adapter.handler;

import com.microsoft.java.debug.core.adapter.IVirtualMachineManager;
import com.microsoft.java.debug.core.adapter.IVirtualMachineManagerProvider;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.VirtualMachineManager;

public class VMHandler {
private IVirtualMachineManagerProvider vmProvider = null;

public VMHandler() {
}

public VMHandler(IVirtualMachineManagerProvider vmProvider) {
this.vmProvider = vmProvider;
}

public IVirtualMachineManagerProvider getVmProvider() {
return vmProvider;
}

public void setVmProvider(IVirtualMachineManagerProvider vmProvider) {
this.vmProvider = vmProvider;
}

public void connectVirtualMachine(VirtualMachine vm) {
if (vm != null && vmProvider != null) {
VirtualMachineManager vmManager = vmProvider.getVirtualMachineManager();
if (vmManager instanceof IVirtualMachineManager) {
((IVirtualMachineManager) vmManager).connectVirtualMachine(vm);
}
}
}

public void disconnectVirtualMachine(VirtualMachine vm) {
if (vm != null && vmProvider != null) {
VirtualMachineManager vmManager = vmProvider.getVirtualMachineManager();
if (vmManager instanceof IVirtualMachineManager) {
((IVirtualMachineManager) vmManager).disconnectVirtualMachine(vm);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation and others.
* Copyright (c) 2017-2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -12,14 +12,31 @@
package com.microsoft.java.debug.plugin.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jdi.internal.VirtualMachineManagerImpl;

import com.microsoft.java.debug.core.DebugSettings;
import com.microsoft.java.debug.core.DebugSettings.IDebugSettingChangeListener;
import com.microsoft.java.debug.core.adapter.IVirtualMachineManager;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.VirtualMachineManager;
import com.sun.jdi.connect.LaunchingConnector;

public class AdvancedVirtualMachineManager extends VirtualMachineManagerImpl implements VirtualMachineManager {
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdi.internal.VirtualMachineManagerImpl;
import org.eclipse.jdt.debug.core.JDIDebugModel;
import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;

public class AdvancedVirtualMachineManager extends VirtualMachineManagerImpl
implements VirtualMachineManager, IDebugSettingChangeListener, IVirtualMachineManager {
List<VirtualMachine> connectedVMs = Collections.synchronizedList(new ArrayList());

public AdvancedVirtualMachineManager() {
super();
update(DebugSettings.getCurrent(), DebugSettings.getCurrent());
DebugSettings.addDebugSettingChangeListener(this);
}

@Override
public List<LaunchingConnector> launchingConnectors() {
Expand All @@ -29,4 +46,41 @@ public List<LaunchingConnector> launchingConnectors() {
return connectors;
}

@Override
public void update(DebugSettings oldSettings, DebugSettings newSettings) {
int currentTimeout = getGlobalRequestTimeout();
int newTimeout = newSettings.jdwpRequestTimeout;
if (newTimeout != currentTimeout) {
setRequestTimeout(newTimeout);
}
}

private void setRequestTimeout(int timeout) {
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(JDIDebugPlugin.getUniqueIdentifier());
if (prefs != null) {
prefs.putInt(JDIDebugModel.PREF_REQUEST_TIMEOUT, timeout);
}

if (!connectedVMs.isEmpty()) {
connectedVMs.forEach(vm -> {
if (vm instanceof org.eclipse.jdi.VirtualMachine) {
try {
((org.eclipse.jdi.VirtualMachine) vm).setRequestTimeout(timeout);
} catch (Exception e) {
// do nothing.
}
}
});
}
}

@Override
public boolean connectVirtualMachine(VirtualMachine vm) {
return connectedVMs.add(vm);
}

@Override
public boolean disconnectVirtualMachine(VirtualMachine vm) {
return connectedVMs.remove(vm);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft Corporation and others.
* Copyright (c) 2017-2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,11 +11,12 @@

package com.microsoft.java.debug.plugin.internal;

import com.microsoft.java.debug.core.adapter.IVirtualMachineManager;
import com.microsoft.java.debug.core.adapter.IVirtualMachineManagerProvider;
import com.sun.jdi.VirtualMachineManager;

public class JdtVirtualMachineManagerProvider implements IVirtualMachineManagerProvider {
private static VirtualMachineManager vmManager = null;
private static IVirtualMachineManager vmManager = null;

@Override
public synchronized VirtualMachineManager getVirtualMachineManager() {
Expand Down