Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
evaluation handler and stackframe proxy for handling InvalidStackFrameException #131
Conversation
| context.getProtocolServer().sendEvent(new Events.StoppedEvent("breakpoint", bpThread.uniqueID())); | ||
| debugEvent.shouldResume = false; | ||
| IEvaluationProvider engine = context.getProvider(IEvaluationProvider.class); | ||
| if (!engine.isInEvaluation(bpThread)) { |
akaroml
Dec 13, 2017
Member
Use early return to avoid introduce unnecessary change to the code history.
if (engine.isInEvaluation(bpThread)) {
return;
}
Use early return to avoid introduce unnecessary change to the code history.
if (engine.isInEvaluation(bpThread)) {
return;
}| @@ -68,116 +59,37 @@ | |||
| String expression = evalArguments.expression; | |||
|
|
|||
| if (StringUtils.isBlank(expression)) { | |||
akaroml
Dec 13, 2017
Member
Why do we check the expression again?
Why do we check the expression again?
| "Failed to evaluate. Reason: Empty expression cannot be evaluated."); | ||
| } | ||
|
|
||
| if (!simpleExprPattern.matcher(expression).matches()) { |
akaroml
Dec 13, 2017
Member
Why delete this check?
Why delete this check?
| CompletableFuture<Value> evaluateResult = engine.evaluate(expression, stackFrameProxy); | ||
| return evaluateResult.thenApply(value -> { | ||
| if (value instanceof VoidValue) { | ||
| response.body = new Responses.EvaluateResponseBody(value.toString(), 0, "<void>", 0); |
akaroml
Dec 13, 2017
Member
Use early return.
Use early return.
| VariableProxy varProxy = new VariableProxy(threadId, "eval", value); | ||
| int referenceId = VariableUtils.hasChildren(value, showStaticVariables) ? context.getRecyclableIdPool().addObject(threadId, varProxy) : 0; | ||
| int indexedVariableId = value instanceof ArrayReference ? ((ArrayReference) value).length() : 0; | ||
| response.body = new Responses.EvaluateResponseBody(variableFormatter.valueToString(value, options), referenceId, |
akaroml
Dec 13, 2017
Member
Return here.
Return here.
| int referenceId = VariableUtils.hasChildren(value, showStaticVariables) ? context.getRecyclableIdPool().addObject(threadId, varProxy) : 0; | ||
| int indexedVariableId = value instanceof ArrayReference ? ((ArrayReference) value).length() : 0; | ||
| response.body = new Responses.EvaluateResponseBody(variableFormatter.valueToString(value, options), referenceId, | ||
| variableFormatter.typeToString(value == null ? null : value.type(), options), indexedVariableId); | ||
| } else { |
akaroml
Dec 13, 2017
Member
This branch won't be needed.
This branch won't be needed.
| @@ -340,7 +343,7 @@ private static String parseMainClassWithoutModuleName(String mainClass) { | |||
| * @return the arguments array. | |||
| */ | |||
| private static List<String> parseArguments(String cmdStr) { | |||
| List<String> list = new ArrayList<String>(); | |||
| List<String> list = new ArrayList<>(); | |||
akaroml
Dec 13, 2017
Member
Why change this?
Why change this?
| @@ -60,23 +61,39 @@ | |||
| ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), stacktraceArgs.threadId); | |||
| int totalFrames = 0; | |||
| if (thread != null) { | |||
|
|
|||
akaroml
Dec 13, 2017
Member
Unnecessary line break.
Unnecessary line break.
| import com.sun.jdi.VirtualMachine; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public class StackFrameProxy implements StackFrame { |
akaroml
Dec 13, 2017
Member
As discussed, we can try to invalidate stackframe cache instead of making a StackFrameProxy.
As discussed, we can try to invalidate stackframe cache instead of making a StackFrameProxy.
2. redefine the StackFrameProxy to only have thread and depth since the stackframe instance may be out of date 3. add stackframe provider and add logic to update stackframe during evaluation.
| public Map<Long, StackFrame[]> getThreadStackFrameMap() { | ||
| return threadStackFrameMap; | ||
| } | ||
|
|
testforstephen
Dec 15, 2017
Contributor
Refactor IStackFrameProvider -> StackFrameCache. And let DebugAdapterContext to expose getStackFrameCache().
Refactor IStackFrameProvider -> StackFrameCache. And let DebugAdapterContext to expose getStackFrameCache().
|
|
||
| import com.sun.jdi.ThreadReference; | ||
|
|
||
| public class StackFrameProxy { |
akaroml
Dec 15, 2017
Member
StackFrameRef
StackFrameRef
|
|
||
| import com.microsoft.java.debug.core.adapter.IDisposable; | ||
|
|
||
| class ReentrantLockDisposable implements IDisposable { |
akaroml
Dec 15, 2017
Member
This implementation is anti-pattern. Please save the lock inside but not create the lock. Lock instance should be passed in by caller.
This implementation is anti-pattern. Please save the lock inside but not create the lock. Lock instance should be passed in by caller.
|
|
||
| import java.util.concurrent.locks.ReentrantLock; | ||
|
|
||
| public class DisposableReentrantLock<T> implements IDisposable { |
akaroml
Dec 15, 2017
Member
DisposableReentrantLock -> LockedObject
DisposableReentrantLock -> LockedObject
| } | ||
| } | ||
|
|
||
| public T getUnderlyingObject() { |
akaroml
Dec 15, 2017
Member
getUnderlyingObject -> getObject
getUnderlyingObject -> getObject
| import java.util.concurrent.locks.ReentrantLock; | ||
|
|
||
| public class DisposableReentrantLock<T> implements IDisposable { | ||
| private final T underlyingObject; |
akaroml
Dec 15, 2017
Member
underlyingObject -> object
underlyingObject -> object
|
|
||
| @Override | ||
| public StackFrame[] refreshStackFrames(ThreadReference thread) { | ||
| synchronized (threadStackFrameMap) { |
akaroml
Dec 15, 2017
Member
No need to synchronize a thread-safe map.
No need to synchronize a thread-safe map.
| try { | ||
| return thread.frames().toArray(new StackFrame[0]); | ||
| } catch (IncompatibleThreadStateException e) { | ||
| return new StackFrame[0]; |
akaroml
Dec 15, 2017
Member
We should let the caller handle this one instead of faking success.
We should let the caller handle this one instead of faking success.
| // ignore | ||
| CompletableFuture<Response> completableFuture = new CompletableFuture<>(); | ||
| // new thread to unsure that the logic to acquire locker and release locker will be executed in this thread | ||
| new Thread(() -> { |
akaroml
Dec 15, 2017
Member
Try not start a new thread this way. We need to let the executor run the thread. Here we can use the Async version of future methods to schedule all actions using the default executor.
Try not start a new thread this way. We need to let the executor run the thread. Here we can use the Async version of future methods to schedule all actions using the default executor.
|
|
||
| } catch (AbsentInformationException e) { | ||
| // ignore | ||
| CompletableFuture<Response> completableFuture = new CompletableFuture<>(); |
akaroml
Dec 15, 2017
Member
return CompletableFuture.supplyAsync(() -> {
try (LockedObject<StackFrame> sf = context.getStackFrameManager().getLockedStackFrame(stackFrameReference)){
return engine.evaluate(expression, stackFrameReference.getThread(), stackFrameReference.getDepth());
}
}).thenComposeAsync((CompletableFuture<Value> result) -> {
// format the value and return the value string
});
return CompletableFuture.supplyAsync(() -> {
try (LockedObject<StackFrame> sf = context.getStackFrameManager().getLockedStackFrame(stackFrameReference)){
return engine.evaluate(expression, stackFrameReference.getThread(), stackFrameReference.getDepth());
}
}).thenComposeAsync((CompletableFuture<Value> result) -> {
// format the value and return the value string
});| StackFrame stackFrame = stackFrameProxy.getProxiedObject(); | ||
| VariableProxy localScope = new VariableProxy(stackFrame.thread().uniqueID(), "Local", stackFrame); | ||
| int localScopeId = context.getRecyclableIdPool().addObject(stackFrame.thread().uniqueID(), localScope); | ||
| ThreadReference thread = stackFrameReference.getThread(); |
akaroml
Dec 15, 2017
Member
Here's the problem. If someone has the thread instance, then he is able to access the stack frames directly. The stack frame manager is bypassed this way.
Here's the problem. If someone has the thread instance, then he is able to access the stack frames directly. The stack frame manager is bypassed this way.
akaroml
Dec 15, 2017
Member
I'm thinking whether putting a lock on the stack frame is enough. Maybe the lock should be on a thread.
I'm thinking whether putting a lock on the stack frame is enough. Maybe the lock should be on a thread.
2. stackframes/varaible/setVariable/evaluate will first gain the thread lock on stack frame manager.
|
|
||
| int count = stacktraceArgs.levels == 0 | ||
| ? totalFrames - stacktraceArgs.startFrame | ||
| : Math.min(totalFrames - stacktraceArgs.startFrame, stacktraceArgs.levels); |
akaroml
Dec 19, 2017
Member
Wrong indentation.
Wrong indentation.
| StackFrame[] frames = context.getStackFrameManager().reloadStackFrames(thread); | ||
|
|
||
| int count = stacktraceArgs.levels == 0 | ||
| ? totalFrames - stacktraceArgs.startFrame |
akaroml
Dec 19, 2017
Member
Wrong indentation.
Wrong indentation.
| indexedVariables); | ||
| return CompletableFuture.completedFuture(response); | ||
| }); | ||
| return completableFuture; |
akaroml
Dec 19, 2017
Member
Simply return when the future is completed. No need to allocate a variable and do late return.
Simply return when the future is completed. No need to allocate a variable and do late return.
| for (int i = stacktraceArgs.startFrame; i < frames.length && count-- > 0; i++) { | ||
| StackFrameReference stackframe = new StackFrameReference(thread, i); | ||
| int frameId = context.getRecyclableIdPool().addObject(thread.uniqueID(), | ||
| stackframe); |
akaroml
Dec 19, 2017
Member
Wrong indentation.
Wrong indentation.
| @@ -116,6 +117,8 @@ | |||
| */ | |||
| public static void checkThreadRunningAndRecycleIds(ThreadReference thread, IDebugAdapterContext context) { | |||
| try { | |||
| IEvaluationProvider engine = context.getProvider(IEvaluationProvider.class); | |||
| engine.clearState(thread); | |||
akaroml
Dec 19, 2017
Member
Why?
Why?
No description provided.