Skip to content

Commit 13eb9c8

Browse files
Allow to update the jdwp request timeout setting (#348)
1 parent 079a9c9 commit 13eb9c8

File tree

8 files changed

+149
-8
lines changed

8 files changed

+149
-8
lines changed

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugSettings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public final class DebugSettings {
4242
public ClassFilters exceptionFilters = new ClassFilters();
4343
public boolean exceptionFiltersUpdated = false;
4444
public int limitOfVariablesPerJdwpRequest = 100;
45+
public int jdwpRequestTimeout = 3000;
4546

4647
public static DebugSettings getCurrent() {
4748
return current;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2020 Microsoft Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Microsoft Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.microsoft.java.debug.core.adapter;
13+
14+
public interface IVirtualMachineManager extends com.sun.jdi.VirtualMachineManager {
15+
boolean connectVirtualMachine(com.sun.jdi.VirtualMachine vm);
16+
17+
boolean disconnectVirtualMachine(com.sun.jdi.VirtualMachine vm);
18+
}

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/AttachRequestHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017 Microsoft Corporation and others.
2+
* Copyright (c) 2017-2020 Microsoft Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -42,6 +42,7 @@
4242

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

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

5960
IVirtualMachineManagerProvider vmProvider = context.getProvider(IVirtualMachineManagerProvider.class);
61+
vmHandler.setVmProvider(vmProvider);
6062

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

6871
// If the debugger and debuggee run at the different JVM platforms, show a warning message.

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/ConfigurationDoneRequestHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017 Microsoft Corporation and others.
2+
* Copyright (c) 2017-2020 Microsoft Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -27,6 +27,7 @@
2727
import com.microsoft.java.debug.core.adapter.IDebugAdapterContext;
2828
import com.microsoft.java.debug.core.adapter.IDebugRequestHandler;
2929
import com.microsoft.java.debug.core.adapter.IEvaluationProvider;
30+
import com.microsoft.java.debug.core.adapter.IVirtualMachineManagerProvider;
3031
import com.microsoft.java.debug.core.protocol.Events;
3132
import com.microsoft.java.debug.core.protocol.Messages.Response;
3233
import com.microsoft.java.debug.core.protocol.Requests.Arguments;
@@ -43,6 +44,7 @@
4344

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

4749
@Override
4850
public List<Command> getTargetCommands() {
@@ -52,6 +54,7 @@ public List<Command> getTargetCommands() {
5254
@Override
5355
public CompletableFuture<Response> handle(Command command, Arguments arguments, Response response, IDebugAdapterContext context) {
5456
IDebugSession debugSession = context.getDebugSession();
57+
vmHandler.setVmProvider(context.getProvider(IVirtualMachineManagerProvider.class));
5558
if (debugSession != null) {
5659
// This is a global event handler to handle the JDI Event from Virtual Machine.
5760
debugSession.getEventHub().events().subscribe(debugEvent -> {
@@ -76,9 +79,11 @@ private void handleDebugEvent(DebugEvent debugEvent, IDebugSession debugSession,
7679
});
7780
}
7881
} else if (event instanceof VMDeathEvent) {
82+
vmHandler.disconnectVirtualMachine(event.virtualMachine());
7983
context.setVmTerminated();
8084
context.getProtocolServer().sendEvent(new Events.ExitedEvent(0));
8185
} else if (event instanceof VMDisconnectEvent) {
86+
vmHandler.disconnectVirtualMachine(event.virtualMachine());
8287
if (context.isAttached()) {
8388
context.setVmTerminated();
8489
context.getProtocolServer().sendEvent(new Events.TerminatedEvent());

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchWithDebuggingDelegate.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ public class LaunchWithDebuggingDelegate implements ILaunchDelegate {
5858
private static final int ATTACH_TERMINAL_TIMEOUT = 20 * 1000;
5959
private static final String TERMINAL_TITLE = "Java Debug Console";
6060
protected static final long RUNINTERMINAL_TIMEOUT = 10 * 1000;
61+
private VMHandler vmHandler = new VMHandler();
6162

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

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

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

176180
IDebugSession debugSession = DebugUtility.launch(
177181
vmProvider.getVirtualMachineManager(),
@@ -184,6 +188,7 @@ public Process launch(LaunchArguments launchArguments, IDebugAdapterContext cont
184188
LaunchRequestHandler.constructEnvironmentVariables(launchArguments),
185189
launchArguments.javaExec);
186190
context.setDebugSession(debugSession);
191+
vmHandler.connectVirtualMachine(debugSession.getVM());
187192

188193
logger.info("Launching debuggee VM succeeded.");
189194
return debugSession.process();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2020 Microsoft Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Microsoft Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.microsoft.java.debug.core.adapter.handler;
13+
14+
import com.microsoft.java.debug.core.adapter.IVirtualMachineManager;
15+
import com.microsoft.java.debug.core.adapter.IVirtualMachineManagerProvider;
16+
import com.sun.jdi.VirtualMachine;
17+
import com.sun.jdi.VirtualMachineManager;
18+
19+
public class VMHandler {
20+
private IVirtualMachineManagerProvider vmProvider = null;
21+
22+
public VMHandler() {
23+
}
24+
25+
public VMHandler(IVirtualMachineManagerProvider vmProvider) {
26+
this.vmProvider = vmProvider;
27+
}
28+
29+
public IVirtualMachineManagerProvider getVmProvider() {
30+
return vmProvider;
31+
}
32+
33+
public void setVmProvider(IVirtualMachineManagerProvider vmProvider) {
34+
this.vmProvider = vmProvider;
35+
}
36+
37+
public void connectVirtualMachine(VirtualMachine vm) {
38+
if (vm != null && vmProvider != null) {
39+
VirtualMachineManager vmManager = vmProvider.getVirtualMachineManager();
40+
if (vmManager instanceof IVirtualMachineManager) {
41+
((IVirtualMachineManager) vmManager).connectVirtualMachine(vm);
42+
}
43+
}
44+
}
45+
46+
public void disconnectVirtualMachine(VirtualMachine vm) {
47+
if (vm != null && vmProvider != null) {
48+
VirtualMachineManager vmManager = vmProvider.getVirtualMachineManager();
49+
if (vmManager instanceof IVirtualMachineManager) {
50+
((IVirtualMachineManager) vmManager).disconnectVirtualMachine(vm);
51+
}
52+
}
53+
}
54+
}

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/AdvancedVirtualMachineManager.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017 Microsoft Corporation and others.
2+
* Copyright (c) 2017-2020 Microsoft Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -12,14 +12,31 @@
1212
package com.microsoft.java.debug.plugin.internal;
1313

1414
import java.util.ArrayList;
15+
import java.util.Collections;
1516
import java.util.List;
1617

17-
import org.eclipse.jdi.internal.VirtualMachineManagerImpl;
18-
18+
import com.microsoft.java.debug.core.DebugSettings;
19+
import com.microsoft.java.debug.core.DebugSettings.IDebugSettingChangeListener;
20+
import com.microsoft.java.debug.core.adapter.IVirtualMachineManager;
21+
import com.sun.jdi.VirtualMachine;
1922
import com.sun.jdi.VirtualMachineManager;
2023
import com.sun.jdi.connect.LaunchingConnector;
2124

22-
public class AdvancedVirtualMachineManager extends VirtualMachineManagerImpl implements VirtualMachineManager {
25+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
26+
import org.eclipse.core.runtime.preferences.InstanceScope;
27+
import org.eclipse.jdi.internal.VirtualMachineManagerImpl;
28+
import org.eclipse.jdt.debug.core.JDIDebugModel;
29+
import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;
30+
31+
public class AdvancedVirtualMachineManager extends VirtualMachineManagerImpl
32+
implements VirtualMachineManager, IDebugSettingChangeListener, IVirtualMachineManager {
33+
List<VirtualMachine> connectedVMs = Collections.synchronizedList(new ArrayList());
34+
35+
public AdvancedVirtualMachineManager() {
36+
super();
37+
update(DebugSettings.getCurrent(), DebugSettings.getCurrent());
38+
DebugSettings.addDebugSettingChangeListener(this);
39+
}
2340

2441
@Override
2542
public List<LaunchingConnector> launchingConnectors() {
@@ -29,4 +46,41 @@ public List<LaunchingConnector> launchingConnectors() {
2946
return connectors;
3047
}
3148

49+
@Override
50+
public void update(DebugSettings oldSettings, DebugSettings newSettings) {
51+
int currentTimeout = getGlobalRequestTimeout();
52+
int newTimeout = newSettings.jdwpRequestTimeout;
53+
if (newTimeout != currentTimeout) {
54+
setRequestTimeout(newTimeout);
55+
}
56+
}
57+
58+
private void setRequestTimeout(int timeout) {
59+
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(JDIDebugPlugin.getUniqueIdentifier());
60+
if (prefs != null) {
61+
prefs.putInt(JDIDebugModel.PREF_REQUEST_TIMEOUT, timeout);
62+
}
63+
64+
if (!connectedVMs.isEmpty()) {
65+
connectedVMs.forEach(vm -> {
66+
if (vm instanceof org.eclipse.jdi.VirtualMachine) {
67+
try {
68+
((org.eclipse.jdi.VirtualMachine) vm).setRequestTimeout(timeout);
69+
} catch (Exception e) {
70+
// do nothing.
71+
}
72+
}
73+
});
74+
}
75+
}
76+
77+
@Override
78+
public boolean connectVirtualMachine(VirtualMachine vm) {
79+
return connectedVMs.add(vm);
80+
}
81+
82+
@Override
83+
public boolean disconnectVirtualMachine(VirtualMachine vm) {
84+
return connectedVMs.remove(vm);
85+
}
3286
}

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtVirtualMachineManagerProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017 Microsoft Corporation and others.
2+
* Copyright (c) 2017-2020 Microsoft Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,11 +11,12 @@
1111

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

14+
import com.microsoft.java.debug.core.adapter.IVirtualMachineManager;
1415
import com.microsoft.java.debug.core.adapter.IVirtualMachineManagerProvider;
1516
import com.sun.jdi.VirtualMachineManager;
1617

1718
public class JdtVirtualMachineManagerProvider implements IVirtualMachineManagerProvider {
18-
private static VirtualMachineManager vmManager = null;
19+
private static IVirtualMachineManager vmManager = null;
1920

2021
@Override
2122
public synchronized VirtualMachineManager getVirtualMachineManager() {

0 commit comments

Comments
 (0)