-
-
Notifications
You must be signed in to change notification settings - Fork 123
Improving the expression evaluator #798
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
Open
wk8
wants to merge
1
commit into
ignatov:master
Choose a base branch
from
wk8:wk8/eval_fixes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,8 +16,10 @@ | |
|
|
||
| package org.intellij.erlang.debugger.xdebug; | ||
|
|
||
| import com.ericsson.otp.erlang.OtpErlangAtom; | ||
| import com.ericsson.otp.erlang.OtpErlangObject; | ||
| import com.ericsson.otp.erlang.OtpErlangPid; | ||
| import com.ericsson.otp.erlang.OtpErlangTuple; | ||
| import com.intellij.execution.ExecutionException; | ||
| import com.intellij.execution.configurations.GeneralCommandLine; | ||
| import com.intellij.execution.process.OSProcessHandler; | ||
|
|
@@ -72,7 +74,6 @@ | |
| import static org.intellij.erlang.debugger.ErlangDebuggerLog.LOG; | ||
|
|
||
| public class ErlangXDebugProcess extends XDebugProcess implements ErlangDebuggerEventListener { | ||
| private final XDebugSession mySession; | ||
| private final ExecutionEnvironment myExecutionEnvironment; | ||
| private final ErlangRunningState myRunningState; | ||
| private final ErlangDebuggerNode myDebuggerNode; | ||
|
|
@@ -83,11 +84,17 @@ public class ErlangXDebugProcess extends XDebugProcess implements ErlangDebugger | |
| private ConcurrentHashMap<ErlangSourcePosition, XLineBreakpoint<ErlangLineBreakpointProperties>> myPositionToLineBreakpointMap = | ||
| new ConcurrentHashMap<>(); | ||
| private XDebuggerEvaluator.XEvaluationCallback myEvalCallback = null; | ||
| // right after evaluating an expression, the erlang debugger will trigger a new "breakpoint reached" event | ||
| // unfortunately, that means this plugin will try to concurrently render the new state of the bindings as | ||
| // well as the result from the evaluation, which Intellij doesn't seem to handle too well, resulting in | ||
| // the debugging session losing track of what the current frame is... so we just ignore the next "breakpoint | ||
| // reached" event after evaluating an expression if it's the same breakpoint - better ideas welcome! | ||
| private ErlangProcessSnapshot myLastBreakpointReached = null; | ||
| private boolean myIgnoreNextBreakpointIfSame = false; | ||
|
|
||
| public ErlangXDebugProcess(@NotNull XDebugSession session, ExecutionEnvironment env) throws ExecutionException { | ||
| //TODO add debug build targets and make sure the project is built using them. | ||
| super(session); | ||
| mySession = session; | ||
|
|
||
| session.setPauseActionSupported(false); | ||
|
|
||
|
|
@@ -124,18 +131,46 @@ public ErlangDebugLocationResolver getLocationResolver() { | |
| public synchronized void evaluateExpression(@NotNull String expression, | ||
| @NotNull XDebuggerEvaluator.XEvaluationCallback callback, | ||
| @NotNull ErlangTraceElement traceElement) { | ||
| // need to pause the debugging session otherwise the callback might get invalidated | ||
| mySession.pause(); | ||
| myEvalCallback = callback; | ||
| // see the comment about myIgnoreNextBreakpointIfSame | ||
| myIgnoreNextBreakpointIfSame = true; | ||
| myDebuggerNode.evaluate(expression, traceElement); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void handleEvaluationResponse(OtpErlangObject response) { | ||
| if (myEvalCallback != null) { | ||
| myEvalCallback.evaluated(ErlangXValueFactory.create(response)); | ||
| mySession.resume(); | ||
| String error = maybeExtractErrorFromEvaluationResponse(response); | ||
|
|
||
| if (error == null) { | ||
| myEvalCallback.evaluated(ErlangXValueFactory.create(response)); | ||
| } | ||
| else { | ||
| myEvalCallback.errorOccurred(error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Parses the response from an evaluation and determines whether it's an error | ||
| // response or not; if it is an error, formats it as a displayable string, if | ||
| // it's not, just returns null | ||
| private static String maybeExtractErrorFromEvaluationResponse(OtpErlangObject response) { | ||
| // is it a parsing error? | ||
| if (response instanceof OtpErlangAtom && ((OtpErlangAtom) response).atomValue().equals("Parse error")) { | ||
| return "Parse error"; | ||
| } | ||
|
|
||
| // is it an uncaught exception? | ||
| if (response instanceof OtpErlangTuple) { | ||
| OtpErlangObject[] elements = ((OtpErlangTuple) response).elements(); | ||
| if (elements.length == 2 | ||
| && elements[0] instanceof OtpErlangAtom | ||
| && ((OtpErlangAtom) elements[0]).atomValue().equals("EXIT")) { | ||
| return "Uncaught exception: " + elements[1]; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -177,9 +212,18 @@ public void breakpointIsSet(String module, int line) { | |
| } | ||
|
|
||
| @Override | ||
| public void breakpointReached(final OtpErlangPid pid, List<ErlangProcessSnapshot> snapshots) { | ||
| public synchronized void breakpointReached(final OtpErlangPid pid, List<ErlangProcessSnapshot> snapshots) { | ||
| ErlangProcessSnapshot processInBreakpoint = ContainerUtil.find(snapshots, erlangProcessSnapshot -> erlangProcessSnapshot.getPid().equals(pid)); | ||
| assert processInBreakpoint != null; | ||
|
|
||
| boolean ignoreIfSame = myIgnoreNextBreakpointIfSame; | ||
| myIgnoreNextBreakpointIfSame = false; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit tricky assignments, do you need that because of the syncronization?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above :) |
||
| if (ignoreIfSame && myLastBreakpointReached.isSameBreakpoint(processInBreakpoint)) { | ||
| // see the comment about myLastBreakpointReached | ||
| return; | ||
| } | ||
|
|
||
| myLastBreakpointReached = processInBreakpoint; | ||
| ErlangSourcePosition breakPosition = ErlangSourcePosition.create(myLocationResolver, processInBreakpoint); | ||
| XLineBreakpoint<ErlangLineBreakpointProperties> breakpoint = getLineBreakpoint(breakPosition); | ||
| ErlangSuspendContext suspendContext = new ErlangSuspendContext(this, pid, snapshots); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, a bit tricky condition :)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well that's the thing, I wasn't able to understand exactly why Intellij doesn't handle it well when the "breakpoint reached" event is fired right after an "eval result" event comes in, but it really doesn't; hence the need to ignore that "breakpoint reached" event. But at the same time, we don't want to ignore just any such event: it's very possible that, while evaluating an expression, you hit another breakpoint - and then we do want it to trigger.
Which results in this condition.
But I said,
better ideas welcome;)