Skip to content
Open
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 @@ -81,4 +81,11 @@ public String getExitReason() {
public List<ErlangTraceElement> getStack() {
return myStack;
}

public boolean isSameBreakpoint(@NotNull ErlangProcessSnapshot snapshot) {
return myPid.equals(snapshot.getPid())
&& myBreakModule != null
&& myBreakModule.equals(snapshot.getBreakModule())
&& myBreakLine == snapshot.getBreakLine();
}
}
58 changes: 51 additions & 7 deletions src/org/intellij/erlang/debugger/xdebug/ErlangXDebugProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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!
Copy link
Owner

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

Copy link
Contributor Author

@wk8 wk8 Oct 5, 2017

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

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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit tricky assignments, do you need that because of the syncronization?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Expand Down