Skip to content

Commit

Permalink
Merge pull request ballerina-platform#43576 from NipunaRanasinghe/deb…
Browse files Browse the repository at this point in the history
…ugger-fix-stackframe-loading-slowness-u11

[Debugger] Fix slowness while loading stack frames and processing user breakpoints
  • Loading branch information
NipunaRanasinghe authored Nov 17, 2024
2 parents d25f02f + e8b5bc5 commit cce8df1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.sun.jdi.ThreadReference;
import com.sun.jdi.event.BreakpointEvent;
import com.sun.jdi.request.BreakpointRequest;
import com.sun.jdi.request.StepRequest;
import org.ballerinalang.debugadapter.breakpoint.BalBreakpoint;
import org.ballerinalang.debugadapter.breakpoint.LogMessage;
import org.ballerinalang.debugadapter.breakpoint.TemplateLogMessage;
Expand Down Expand Up @@ -173,8 +172,12 @@ void restoreUserBreakpoints() {
if (context.getDebuggeeVM() == null) {
return;
}

context.getEventManager().deleteAllBreakpoints();
context.getDebuggeeVM().allClasses().forEach(classRef -> activateUserBreakPoints(classRef, false));
for (Map.Entry<String, LinkedHashMap<Integer, BalBreakpoint>> entry : userBreakpoints.entrySet()) {
String qClassName = entry.getKey();
context.getDebuggeeVM().classesByName(qClassName).forEach(ref -> activateUserBreakPoints(ref, false));
}
}

/**
Expand Down Expand Up @@ -210,7 +213,7 @@ void activateUserBreakPoints(ReferenceType referenceType, boolean shouldNotify)
} catch (AbsentInformationException ignored) {
// classes with no line number information can be ignored.
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
LOGGER.error("Error while activating user breakpoints:" + e.getMessage(), e);
}
}

Expand Down Expand Up @@ -252,11 +255,7 @@ void activateDynamicBreakPoints(int threadId, DynamicBreakpointMode mode, boolea
context.setPrevLocation(validFrames.get(0).getJStackFrame().location());
}
} catch (JdiProxyException e) {
LOGGER.error(e.getMessage());
if (!jdiEventProcessor.getStepRequests().isEmpty()) {
int stepType = ((StepRequest) jdiEventProcessor.getStepRequests().get(0)).depth();
jdiEventProcessor.sendStepRequest(threadId, stepType);
}
LOGGER.error("Error while activating dynamic breakpoints:" + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.ballerinalang.debugadapter.evaluation.DebugExpressionEvaluator;
import org.ballerinalang.debugadapter.evaluation.EvaluationException;
import org.ballerinalang.debugadapter.evaluation.EvaluationExceptionKind;
import org.ballerinalang.debugadapter.jdi.JDIUtils;
import org.ballerinalang.debugadapter.jdi.JdiProxyException;
import org.ballerinalang.debugadapter.jdi.LocalVariableProxyImpl;
import org.ballerinalang.debugadapter.jdi.StackFrameProxyImpl;
Expand Down Expand Up @@ -123,7 +124,6 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.ballerinalang.debugadapter.DebugExecutionManager.LOCAL_HOST;
Expand Down Expand Up @@ -590,11 +590,11 @@ public CompletableFuture<RunInTerminalResponse> runInTerminal(RunInTerminalReque
// attach to target VM
while (context.getDebuggeeVM() == null && tryCounter < 10) {
try {
TimeUnit.SECONDS.sleep(3);
JDIUtils.sleepMillis(3000);
attachToRemoteVM("", clientConfigHolder.getDebuggePort());
} catch (IOException ignored) {
tryCounter++;
} catch (IllegalConnectorArgumentsException | ClientConfigurationException | InterruptedException e) {
} catch (IllegalConnectorArgumentsException | ClientConfigurationException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -680,10 +680,7 @@ void terminateDebugServer(boolean terminateDebuggee, boolean logsEnabled) {

// Exits from the debug server VM.
new java.lang.Thread(() -> {
try {
java.lang.Thread.sleep(500);
} catch (InterruptedException ignored) {
}
JDIUtils.sleepMillis(500);
System.exit(0);
}).start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,16 @@ private void processEvent(EventSet eventSet, Event event) {
}
}

void enableBreakpoints(String qualifiedClassName, LinkedHashMap<Integer, BalBreakpoint> breakpoints) {
breakpointProcessor.addSourceBreakpoints(qualifiedClassName, breakpoints);

if (context.getDebuggeeVM() != null) {
// Setting breakpoints to a already running debug session.
context.getEventManager().deleteAllBreakpoints();
context.getDebuggeeVM().allClasses().forEach(referenceType ->
breakpointProcessor.activateUserBreakPoints(referenceType, false));
void enableBreakpoints(String qClassName, LinkedHashMap<Integer, BalBreakpoint> breakpoints) {
breakpointProcessor.addSourceBreakpoints(qClassName, breakpoints);
if (context.getDebuggeeVM() == null) {
return;
}

// Setting breakpoints to an already running debug session.
context.getEventManager().deleteAllBreakpoints();
context.getDebuggeeVM().classesByName(qClassName)
.forEach(ref -> breakpointProcessor.activateUserBreakPoints(ref, false));
}

void sendStepRequest(int threadId, int stepType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ public static void enableJDIRequests(ExecutionContext context) {
EventRequestManager eventManager = context.getEventManager();
eventManager.breakpointRequests().forEach(EventRequest::enable);
}

/**
* Sleep for the given number of milliseconds.
*
* @param millis number of milliseconds to sleep
*/
public static void sleepMillis(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ignored) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ public StackFrame getStackFrame() throws JdiProxyException {
} catch (IncompatibleThreadStateException e) {
error = e;
clearCaches();
try {
Thread.sleep(10);
} catch (InterruptedException ignored) {
}
JDIUtils.sleepMillis(10);
} catch (IndexOutOfBoundsException e) {
throw new JdiProxyException(e.getMessage(), e);
} catch (ObjectCollectedException ignored) {
Expand Down Expand Up @@ -286,14 +283,8 @@ public Value getValue(LocalVariableProxyImpl localVariable) throws JdiProxyExcep
Exception error = null;
for (int attempt = 0; attempt < RETRY_COUNT; attempt++) {
try {
Map<LocalVariable, Value> values = getAllValues();
LocalVariable variable = localVariable.getVariable();
if (values.containsKey(variable)) {
return values.get(variable);
} else { // try direct get
return getStackFrame().getValue(variable);
}
} catch (InvalidStackFrameException e) {
return getStackFrame().getValue(localVariable.getVariable());
} catch (InvalidStackFrameException | IllegalArgumentException e) {
error = e;
clearCaches();
} catch (InconsistentDebugInfoException ignored) {
Expand All @@ -313,11 +304,13 @@ public Value getValue(LocalVariableProxyImpl localVariable) throws JdiProxyExcep
throw e;
}
}
JDIUtils.sleepMillis(1);
}
throw new JdiProxyException(error.getMessage(), error);
}

private Map<LocalVariable, Value> getAllValues() throws JdiProxyException {
@SuppressWarnings("unused")
public Map<LocalVariable, Value> getAllValues() throws JdiProxyException {
checkValid();
if (myAllValues == null) {
try {
Expand Down Expand Up @@ -345,6 +338,7 @@ private Map<LocalVariable, Value> getAllValues() throws JdiProxyException {
return myAllValues;
}

@SuppressWarnings("unused")
public void setValue(LocalVariableProxyImpl localVariable, Value value)
throws JdiProxyException, ClassNotLoadedException, InvalidTypeException {
InvalidStackFrameException error = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.sun.jdi.Value;
import org.ballerinalang.debugadapter.SuspendedContext;
import org.ballerinalang.debugadapter.evaluation.EvaluationException;
import org.ballerinalang.debugadapter.jdi.JDIUtils;
import org.ballerinalang.debugadapter.jdi.LocalVariableProxyImpl;

import java.util.AbstractMap;
Expand Down Expand Up @@ -212,10 +213,7 @@ public static Value invokeRemoteVMMethod(SuspendedContext context, Value jvmObje
return ((ObjectReference) jvmObject).invokeMethod(context.getOwningThread()
.getThreadReference(), method, arguments, ObjectReference.INVOKE_SINGLE_THREADED);
} catch (Exception e) {
try {
Thread.sleep(10);
} catch (InterruptedException ignored) {
}
JDIUtils.sleepMillis(10);
}
}
throw new DebugVariableException("Failed to invoke remote VM method as the invocation thread is busy");
Expand Down

0 comments on commit cce8df1

Please sign in to comment.