★ wanayoo — archive 1999 https://github.com/microsoft/java-debug/pull/131Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evaluation handler and stackframe proxy for handling InvalidStackFrameException #131

Merged
merged 32 commits into from Dec 19, 2017

Conversation

@andxu
Copy link
Collaborator

@andxu andxu commented Dec 13, 2017

No description provided.

andxu added 3 commits Dec 13, 2017
@andxu
Copy link
Collaborator Author

@andxu andxu commented Dec 13, 2017

#80

andxu added 2 commits Dec 13, 2017
context.getProtocolServer().sendEvent(new Events.StoppedEvent("breakpoint", bpThread.uniqueID()));
debugEvent.shouldResume = false;
IEvaluationProvider engine = context.getProvider(IEvaluationProvider.class);
if (!engine.isInEvaluation(bpThread)) {

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

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)) {

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

Why do we check the expression again?

"Failed to evaluate. Reason: Empty expression cannot be evaluated.");
}

if (!simpleExprPattern.matcher(expression).matches()) {

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

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);

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

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,

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

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 {

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

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<>();

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

Why change this?

@@ -60,23 +61,39 @@
ThreadReference thread = DebugUtility.getThread(context.getDebugSession(), stacktraceArgs.threadId);
int totalFrames = 0;
if (thread != null) {

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

Unnecessary line break.

import com.sun.jdi.VirtualMachine;

@SuppressWarnings("unchecked")
public class StackFrameProxy implements StackFrame {

This comment has been minimized.

@akaroml

akaroml Dec 13, 2017
Member

As discussed, we can try to invalidate stackframe cache instead of making a StackFrameProxy.

andxu added 7 commits Dec 15, 2017
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;
}

This comment has been minimized.

@testforstephen

testforstephen Dec 15, 2017
Contributor

Refactor IStackFrameProvider -> StackFrameCache. And let DebugAdapterContext to expose getStackFrameCache().


import com.sun.jdi.ThreadReference;

public class StackFrameProxy {

This comment has been minimized.

@akaroml

akaroml Dec 15, 2017
Member

StackFrameRef


import com.microsoft.java.debug.core.adapter.IDisposable;

class ReentrantLockDisposable implements IDisposable {

This comment has been minimized.

@akaroml

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.


import java.util.concurrent.locks.ReentrantLock;

public class DisposableReentrantLock<T> implements IDisposable {

This comment has been minimized.

@akaroml

akaroml Dec 15, 2017
Member

DisposableReentrantLock -> LockedObject

}
}

public T getUnderlyingObject() {

This comment has been minimized.

@akaroml

akaroml Dec 15, 2017
Member

getUnderlyingObject -> getObject

import java.util.concurrent.locks.ReentrantLock;

public class DisposableReentrantLock<T> implements IDisposable {
private final T underlyingObject;

This comment has been minimized.

@akaroml

akaroml Dec 15, 2017
Member

underlyingObject -> object


@Override
public StackFrame[] refreshStackFrames(ThreadReference thread) {
synchronized (threadStackFrameMap) {

This comment has been minimized.

@akaroml

akaroml Dec 15, 2017
Member

No need to synchronize a thread-safe map.

try {
return thread.frames().toArray(new StackFrame[0]);
} catch (IncompatibleThreadStateException e) {
return new StackFrame[0];

This comment has been minimized.

@akaroml

akaroml Dec 15, 2017
Member

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(() -> {

This comment has been minimized.

@akaroml

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.


} catch (AbsentInformationException e) {
// ignore
CompletableFuture<Response> completableFuture = new CompletableFuture<>();

This comment has been minimized.

@akaroml

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
});
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();

This comment has been minimized.

@akaroml

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.

This comment has been minimized.

@akaroml

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.

andxu added 5 commits Dec 18, 2017
2. stackframes/varaible/setVariable/evaluate will first gain the thread lock on stack frame manager.
andxu added 5 commits Dec 19, 2017
* Redefine HCR event. (#133)

* Sequentially process debug requests (#135)

* bump version to 0.5.0 (#136)

* Redefine HCR event.

* bump version to 0.5.0

Signed-off-by: Jinbo Wang <jinbwan@microsoft.com>

* don't use lock

* don't use lock

int count = stacktraceArgs.levels == 0
? totalFrames - stacktraceArgs.startFrame
: Math.min(totalFrames - stacktraceArgs.startFrame, stacktraceArgs.levels);

This comment has been minimized.

@akaroml

akaroml Dec 19, 2017
Member

Wrong indentation.

StackFrame[] frames = context.getStackFrameManager().reloadStackFrames(thread);

int count = stacktraceArgs.levels == 0
? totalFrames - stacktraceArgs.startFrame

This comment has been minimized.

@akaroml

akaroml Dec 19, 2017
Member

Wrong indentation.

indexedVariables);
return CompletableFuture.completedFuture(response);
});
return completableFuture;

This comment has been minimized.

@akaroml

akaroml Dec 19, 2017
Member

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);

This comment has been minimized.

@akaroml

akaroml Dec 19, 2017
Member

Wrong indentation.

@@ -116,6 +117,8 @@
*/
public static void checkThreadRunningAndRecycleIds(ThreadReference thread, IDebugAdapterContext context) {
try {
IEvaluationProvider engine = context.getProvider(IEvaluationProvider.class);
engine.clearState(thread);

This comment has been minimized.

andxu added 3 commits Dec 19, 2017
@andxu andxu merged commit f649bc6 into master Dec 19, 2017
3 checks passed
3 checks passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
license/cla All CLA requirements met.
Details
@andxu andxu deleted the andy_evaluate branch Dec 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.