How to chain debugger actions and evaluations with com.intellij.xdebugger.XDebugSession?

Answered

I am implementing an IntelliJ plugin that provides extra features to do with debugging Java programs. it does some special things around session.resume(), i.e. before it calls session.resume(), it calls the XDebuggerEvaluator to evaluate a first expression which sets a flag in the debuggee JVM. Then after resume() is done, it calls the XDebuggerEvaluator again to evaluate a second expression that clears the flag in the JVM.  Whilst I have got the first expression evaluation to work and then call the session.resume() in its callback, the challenge I have is that it is difficult to get the second expression evaluation to execute.  Here is a synopsis of my action class. NB I based it off the source of the core ResumeAction.

MyAction extends XDebuggerActionBase implements DumbAware {

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
session.getDebugProcess().getEvaluator().evaluate(
"first_expression",
getFirstEvaluationCallback(() -> session.resume()),
null);
}

@NotNull
private XDebuggerEvaluator.XEvaluationCallback getFirstEvaluationCallback(Runnable operation) {
return new XEvaluationCallbackBase() {
@Override
public void evaluated(@NotNull final XValue result) {
ApplicationManager.getApplication().invokeLater(() -> {
operation.run();
Thread.sleep(5000);
session.getDebugProcess().getEvaluator().evaluate(
"second_expression",
getSecondEvaluationCallback(), // trivial implementation not shown
null);
});
}

@Override
public void errorOccurred(@NotNull final String errorMessage) {
System.err.println("Error occurred: " + errorMessage);
}
};
}

}

 

As you see, I tried putting a sleep() between the operation .run() and second evaluation, which is a rubbishy idea. I would prefer to find a reliable asynchronous way of handling this. However some of the time the sleep() has caused the second evaluation to happen, which is at least informative. Does the platform API have a "proper" way of doing this kind of operation chaining?  I would appreciate your advice.

Thanks.

2 comments
Comment actions Permalink

You can try adding a listener com.intellij.xdebugger.XDebugSessionListener with your evaluations in beforeSessionResume and sessionPaused.

1
Comment actions Permalink

Brilliant, thanks @Egor, that worked perfectly :)

Simon

0

Please sign in to leave a comment.