FFFF fix the vmdisconnectedexception error to debugger user. by andxu · Pull Request #67 · microsoft/java-debug · GitHub
[go: up one dir, main page]

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 @@ -19,6 +19,7 @@

import com.sun.jdi.Location;
import com.sun.jdi.ReferenceType;
import com.sun.jdi.VMDisconnectedException;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.event.ClassPrepareEvent;
import com.sun.jdi.request.BreakpointRequest;
Expand Down Expand Up @@ -49,8 +50,8 @@ public class Breakpoint implements IBreakpoint {
}

// IDebugResource
private List<EventRequest> requests = new ArrayList<EventRequest>();
private List<Disposable> subscriptions = new ArrayList<Disposable>();
private List<EventRequest> requests = new ArrayList<>();
private List<Disposable> subscriptions = new ArrayList<>();

@Override
public List<EventRequest> requests() {
Expand Down Expand Up @@ -124,7 +125,7 @@ public CompletableFuture<IBreakpoint> install() {
localClassPrepareRequest.enable();
requests.add(localClassPrepareRequest);

CompletableFuture<IBreakpoint> future = new CompletableFuture<IBreakpoint>();
CompletableFuture<IBreakpoint> future = new CompletableFuture<>();

Disposable subscription = eventHub.events()
.filter(debugEvent -> debugEvent.event instanceof ClassPrepareEvent
Expand Down Expand Up @@ -154,7 +155,7 @@ public CompletableFuture<IBreakpoint> install() {
}

private static List<Location> collectLocations(ReferenceType refType, int lineNumber) {
List<Location> locations = new ArrayList<Location>();
List<Location> locations = new ArrayList<>();

try {
locations.addAll(refType.locationsOfLine(lineNumber));
Expand All @@ -167,62 +168,71 @@ private static List<Location> collectLocations(ReferenceType refType, int lineNu
}

private static List<Location> collectLocations(List<ReferenceType> refTypes, int lineNumber) {
List<Location> locations = new ArrayList<Location>();
refTypes.forEach(refType -> {
locations.addAll(collectLocations(refType, lineNumber));
locations.addAll(collectLocations(refType.nestedTypes(), lineNumber));
});
List<Location> locations = new ArrayList<>();
try {
refTypes.forEach(refType -> {
locations.addAll(collectLocations(refType, lineNumber));
locations.addAll(collectLocations(refType.nestedTypes(), lineNumber));
});
} catch (VMDisconnectedException ex) {
// collect locations operation may be executing while JVM is running, thus the VMDisconnectedException may be
// possible, in case of VMDisconnectedException, this method will return an empty array which turns out a valid
// response in vscode, causing no error log in trace.
}

return locations;
}

private List<BreakpointRequest> createBreakpointRequests(ReferenceType refType,
int lineNumber, int hitCount) {
List<ReferenceType> refTypes = new ArrayList<ReferenceType>();
private List<BreakpointRequest> createBreakpointRequests(ReferenceType refType, int lineNumber, int hitCount) {
List<ReferenceType> refTypes = new ArrayList<>();
refTypes.add(refType);
return createBreakpointRequests(refTypes, lineNumber, hitCount);
}

private List<BreakpointRequest> createBreakpointRequests(List<ReferenceType> refTypes,
int lineNumber, int hitCount) {
private List<BreakpointRequest> createBreakpointRequests(List<ReferenceType> refTypes, int lineNumber, int hitCount) {
List<Location> locations = collectLocations(refTypes, lineNumber);

// find out the existing breakpoint locations
List<Location> existingLocations = new ArrayList<Location>(requests.size());
List<Location> existingLocations = new ArrayList<>(requests.size());
Observable.fromIterable(requests).filter(request -> request instanceof BreakpointRequest)
.map(request -> ((BreakpointRequest) request).location()).toList().subscribe(list -> {
existingLocations.addAll(list);
});

// remove duplicated locations
List<Location> newLocations = new ArrayList<Location>(locations.size());
Observable.fromIterable(locations).filter(location -> !existingLocations.contains(location)).toList()
.subscribe(list -> {
newLocations.addAll(list);
});
List<Location> newLocations = new ArrayList<>(locations.size());
Observable.fromIterable(locations).filter(location -> !existingLocations.contains(location)).toList().subscribe(list -> {
newLocations.addAll(list);
});

List<BreakpointRequest> newRequests = new ArrayList<BreakpointRequest>(newLocations.size());
List<BreakpointRequest> newRequests = new ArrayList<>(newLocations.size());

newLocations.forEach(location -> {
BreakpointRequest request = vm.eventRequestManager().createBreakpointRequest(location);
request.setSuspendPolicy(BreakpointRequest.SUSPEND_EVENT_THREAD);
if (hitCount > 0) {
request.addCountFilter(hitCount);
try {
BreakpointRequest request = vm.eventRequestManager().createBreakpointRequest(location);
request.setSuspendPolicy(BreakpointRequest.SUSPEND_EVENT_THREAD);
if (hitCount > 0) {
request.addCountFilter(hitCount);
}
request.enable();
newRequests.add(request);
} catch (VMDisconnectedException ex) {
// enable breakpoint operation may be executing while JVM is running, thus the VMDisconnectedException may be
// possible, in case of VMDisconnectedException, this method will return an empty array which turns out a valid
// response in vscode, causing no error log in trace.
}
request.enable();
newRequests.add(request);
});

return newRequests;
}

@Override
public void putProperty(Object key, Object value) {
this.propertyMap.put(key, value);
propertyMap.put(key, value);
}

@Override
public Object getProperty(Object key) {
return this.propertyMap.get(key);
return propertyMap.get(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void resume() {
* all threads fully.
*/
for (ThreadReference tr : DebugUtility.getAllThreadsSafely(this)) {
while (tr.suspendCount() > 1) {
while (!tr.isCollected() && tr.suspendCount() > 1) {
tr.resume();
}
}
Expand All @@ -78,18 +78,18 @@ public void terminate() {

@Override
public IBreakpoint createBreakpoint(String className, int lineNumber) {
return new Breakpoint(this.vm, this.eventHub(), className, lineNumber);
return new Breakpoint(vm, this.eventHub(), className, lineNumber);
}

@Override
public IBreakpoint createBreakpoint(String className, int lineNumber, int hitCount) {
return new Breakpoint(this.vm, this.eventHub(), className, lineNumber, hitCount);
return new Breakpoint(vm, this.eventHub(), className, lineNumber, hitCount);
}

@Override
public void setExceptionBreakpoints(boolean notifyCaught, boolean notifyUncaught) {
EventRequestManager manager = vm.eventRequestManager();
ArrayList<ExceptionRequest> legacy = new ArrayList<ExceptionRequest>(manager.exceptionRequests());
ArrayList<ExceptionRequest> legacy = new ArrayList<>(manager.exceptionRequests());
manager.deleteEventRequests(legacy);
ExceptionRequest request = manager.createExceptionRequest(null, notifyCaught, notifyUncaught);
request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.lang3.StringUtils;

import com.sun.jdi.Location;
import com.sun.jdi.ObjectCollectedException;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.VMDisconnectedException;
import com.sun.jdi.VirtualMachine;
Expand Down Expand Up @@ -161,7 +162,7 @@ public static CompletableFuture<Location> stepOut(ThreadReference thread, IEvent

private static CompletableFuture<Location> step(ThreadReference thread, IEventHub eventHub, int stepSize,
int stepDepth) {
CompletableFuture<Location> future = new CompletableFuture<Location>();
CompletableFuture<Location> future = new CompletableFuture<>();

StepRequest request = thread.virtualMachine().eventRequestManager().createStepRequest(thread, stepSize,
stepDepth);
Expand Down Expand Up @@ -191,7 +192,7 @@ private static CompletableFuture<Location> step(ThreadReference thread, IEventHu
*/
public static ThreadReference getThread(IDebugSession debugSession, long threadId) {
for (ThreadReference thread : getAllThreadsSafely(debugSession)) {
if (thread.uniqueID() == threadId) {
if (thread.uniqueID() == threadId && !thread.isCollected()) {
return thread;
}
}
Expand Down Expand Up @@ -223,16 +224,22 @@ public static List<ThreadReference> getAllThreadsSafely(IDebugSession debugSessi
* the thread reference
*/
public static void resumeThread(ThreadReference thread) {
if (thread == null) {
// if thread is not found or is garbage collected, do nothing
if (thread == null || thread.isCollected()) {
return;
}
int suspends = thread.suspendCount();
for (int i = 0; i < suspends; i++) {
/**
* Invoking this method will decrement the count of pending suspends on this thread.
* If it is decremented to 0, the thread will continue to execute.
*/
thread.resume();
try {
int suspends = thread.suspendCount();
for (int i = 0; i < suspends; i++) {
/**
* Invoking this method will decrement the count of pending suspends on this thread.
* If it is decremented to 0, the thread will continue to execute.
*/
thread.resume();
}
} catch (ObjectCollectedException ex) {
// ObjectCollectionException can be thrown if the thread has already completed (exited) in the VM when calling suspendCount,
// the resume operation to this thread is meanness.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public class DebugAdapter implements IDebugAdapter {
* Constructor.
*/
public DebugAdapter(BiConsumer<Events.DebugEvent, Boolean> consumer, IProviderContext providerContext) {
this.eventConsumer = consumer;
eventConsumer = consumer;
this.providerContext = providerContext;
this.debugContext = new DebugAdapterContext(this);
this.requestHandlers = new HashMap<>();
debugContext = new DebugAdapterContext(this);
requestHandlers = new HashMap<>();
initialize();
}

Expand All @@ -67,10 +67,15 @@ public Messages.Response dispatchRequest(Messages.Request request) {
Arguments cmdArgs = JsonUtils.fromJson(request.arguments, command.getArgumentType());

try {
if (debugContext.isVmTerminated()) {
AdapterUtils.setErrorResponse(response, ErrorCode.VM_TERMINATED,
String.format("Target VM is already termi 3B0C nated.", request.command));
return response;
}
List<IDebugRequestHandler> handlers = requestHandlers.get(command);
if (handlers != null && !handlers.isEmpty()) {
for (IDebugRequestHandler handler : handlers) {
handler.handle(command, cmdArgs, response, this.debugContext);
handler.handle(command, cmdArgs, response, debugContext);
}
} else {
AdapterUtils.setErrorResponse(response, ErrorCode.UNRECOGNIZED_REQUEST_FAILURE,
Expand All @@ -91,7 +96,7 @@ public Messages.Response dispatchRequest(Messages.Request request) {
* @see ProtocolServer#sendEvent(String, Object)
*/
public void sendEvent(Events.DebugEvent event) {
this.eventConsumer.accept(event, false);
eventConsumer.accept(event, false);
}

/**
Expand All @@ -100,7 +105,7 @@ public void sendEvent(Events.DebugEvent event) {
* @see ProtocolServer#sendEventLater(String, Object)
*/
public void sendEventLater(Events.DebugEvent event) {
this.eventConsumer.accept(event, true);
eventConsumer.accept(event, true);
}

public <T extends IProvider> T getProvider(Class<T> clazz) {
Expand Down
Loading
0