-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...core/src/main/java/com/microsoft/java/debug/core/adapter/handler/RestartFrameHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2017 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 java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import com.microsoft.java.debug.core.DebugException; | ||
import com.microsoft.java.debug.core.DebugUtility; | ||
import com.microsoft.java.debug.core.StackFrameUtility; | ||
import com.microsoft.java.debug.core.adapter.AdapterUtils; | ||
import com.microsoft.java.debug.core.adapter.ErrorCode; | ||
import com.microsoft.java.debug.core.adapter.IDebugAdapterContext; | ||
import com.microsoft.java.debug.core.adapter.IDebugRequestHandler; | ||
import com.microsoft.java.debug.core.adapter.variables.StackFrameReference; | ||
import com.microsoft.java.debug.core.protocol.Events; | ||
import com.microsoft.java.debug.core.protocol.Events.UserNotificationEvent.NotificationType; | ||
import com.microsoft.java.debug.core.protocol.Messages.Response; | ||
import com.microsoft.java.debug.core.protocol.Requests; | ||
import com.microsoft.java.debug.core.protocol.Requests.Arguments; | ||
import com.microsoft.java.debug.core.protocol.Requests.Command; | ||
import com.microsoft.java.debug.core.protocol.Requests.RestartFrameArguments; | ||
import com.sun.jdi.StackFrame; | ||
import com.sun.jdi.ThreadReference; | ||
import com.sun.jdi.request.StepRequest; | ||
|
||
/** | ||
* Support Eclipse's `Drop To Frame` action, which is restartFrame in VSCode's | ||
* debug. | ||
*/ | ||
public class RestartFrameHandler implements IDebugRequestHandler { | ||
|
||
@Override | ||
public List<Command> getTargetCommands() { | ||
return Arrays.asList(Requests.Command.RESTARTFRAME); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Response> handle(Command command, Arguments arguments, Response response, IDebugAdapterContext context) { | ||
RestartFrameArguments restartFrameArgs = (RestartFrameArguments) arguments; | ||
StackFrameReference stackFrameReference = (StackFrameReference) context.getRecyclableIdPool().getObjectById(restartFrameArgs.frameId); | ||
|
||
if (stackFrameReference == null) { | ||
return AdapterUtils.createAsyncErrorResponse(response, ErrorCode.RESTARTFRAME_FAILURE, | ||
String.format("RestartFrame: cannot find the stack frame with frameID %s", restartFrameArgs.frameId)); | ||
} | ||
|
||
if (canRestartFrame(context, stackFrameReference)) { | ||
try { | ||
ThreadReference reference = stackFrameReference.getThread(); | ||
popStackFrames(context, reference, stackFrameReference.getDepth()); | ||
stepInto(context, reference); | ||
} catch (DebugException de) { | ||
context.getProtocolServer().sendEvent(new Events.UserNotificationEvent(NotificationType.ERROR, de.getMessage())); | ||
} | ||
return CompletableFuture.completedFuture(response); | ||
} else { | ||
context.getProtocolServer().sendEvent(new Events.UserNotificationEvent(NotificationType.ERROR, "Current stack frame doesn't support restart.")); | ||
context.getProtocolServer().sendEvent(new Events.StoppedEvent("restartframe", stackFrameReference.getThread().uniqueID())); | ||
return AdapterUtils.createAsyncErrorResponse(response, ErrorCode.RESTARTFRAME_FAILURE, "Failed to restart the selected stack frame."); | ||
} | ||
} | ||
|
||
private boolean canRestartFrame(IDebugAdapterContext context, StackFrameReference frameReference) { | ||
if (!context.getDebugSession().getVM().canPopFrames()) { | ||
return false; | ||
} | ||
ThreadReference reference = frameReference.getThread(); | ||
StackFrame[] frames = context.getStackFrameManager().reloadStackFrames(reference); | ||
// The frame cannot be the first one of the stack: | ||
if (frames.length <= frameReference.getDepth() + 1) { | ||
return false; | ||
} | ||
|
||
// Cannot restart frame involved with native call stacks: | ||
for (int i = 0; i <= frameReference.getDepth() + 1; i++) { | ||
if (StackFrameUtility.isNative(frames[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private void popStackFrames(IDebugAdapterContext context, ThreadReference thread, int depth) throws DebugException { | ||
StackFrame[] frames = context.getStackFrameManager().reloadStackFrames(thread); | ||
StackFrameUtility.pop(frames[depth]); | ||
} | ||
|
||
private void stepInto(IDebugAdapterContext context, ThreadReference thread) { | ||
StepRequest request = DebugUtility.createStepIntoRequest(thread, context.getStepFilters().classNameFilters); | ||
context.getDebugSession().getEventHub().stepEvents().filter(debugEvent -> request.equals(debugEvent.event.request())).take(1).subscribe(debugEvent -> { | ||
debugEvent.shouldResume = false; | ||
// Have to send two events to keep the UI sync with the step in operations: | ||
context.getProtocolServer().sendEvent(new Events.StoppedEvent("step", thread.uniqueID())); | ||
context.getProtocolServer().sendEvent(new Events.ContinuedEvent(thread.uniqueID())); | ||
}); | ||
request.enable(); | ||
thread.resume(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters