use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.
the class DebugExceptionTest method testGetRawUncaughtException.
@Test
public void testGetRawUncaughtException() {
Source testSource = testSource("STATEMENT(THROW(a, b))");
Breakpoint uncaughtBreakpoint = Breakpoint.newExceptionBuilder(false, true).build();
try (DebuggerSession session = startSession()) {
session.install(uncaughtBreakpoint);
// Exception breakpoints are resolved right
assertTrue(uncaughtBreakpoint.isResolved());
// away
startEval(testSource);
expectSuspended((SuspendedEvent event) -> {
assertSame(uncaughtBreakpoint, event.getBreakpoints().iterator().next());
assertSame(SuspendAnchor.AFTER, event.getSuspendAnchor());
DebugException exception = event.getException();
assertEquals(InstrumentationTestLanguage.ThrowNode.TestLanguageException.class, exception.getRawException(InstrumentationTestLanguage.class).getClass());
});
Throwable t = expectThrowable();
assertEquals("b", t.getMessage());
}
}
use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.
the class SLDebugTest method testExceptions.
@Test
public void testExceptions() {
final Source source = slCode("function main() {\n" + " i = \"0\";\n" + " return invert(i);\n" + "}\n" + "function invert(n) {\n" + " x = 10 / n;\n" + " return x;\n" + "}\n");
try (DebuggerSession session = startSession()) {
Breakpoint excBreakpoint = Breakpoint.newExceptionBuilder(true, true).build();
session.install(excBreakpoint);
startEval(source);
expectSuspended((SuspendedEvent event) -> {
DebugException exception = event.getException();
Assert.assertNotNull(exception);
assertTrue(exception.getMessage(), exception.getMessage().startsWith("Type error"));
SourceSection throwLocation = exception.getThrowLocation();
assertEquals(6, throwLocation.getStartLine());
// Rewind and then repair the 'n' argument.
event.prepareUnwindFrame(event.getTopStackFrame());
});
expectSuspended((SuspendedEvent event) -> {
assert event != null;
// Step into after unwind to change the arguments.
event.prepareStepInto(1);
});
expectSuspended((SuspendedEvent event) -> {
event.getTopStackFrame().getScope().getDeclaredValue("n").set(event.getTopStackFrame().eval("function main() {return 2;}"));
// Continue after unwind
});
assertEquals("5", expectDone());
}
}
use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.
the class ThreadsHandler method threadSuspended.
public void threadSuspended(Thread thread, SuspendedEvent event) {
SuspendedThreadInfo info = null;
synchronized (thread2Ids) {
Integer id = thread2Ids.get(thread);
if (id != null) {
info = new SuspendedThreadInfo(id, event);
suspendedThreads.put(id, info);
}
}
if (info != null) {
String reason = null;
String description = null;
List<String> logMessages = new ArrayList<>();
boolean stop = true;
for (Breakpoint bp : event.getBreakpoints()) {
String logMessage = context.getBreakpointsHandler().getLogMessage(bp);
if (logMessage != null) {
logMessages.add(logMessage);
}
stop &= context.getBreakpointsHandler().checkConditions(bp, event.getTopStackFrame());
switch(bp.getKind()) {
case EXCEPTION:
reason = "exception";
description = "Paused on exception";
break;
case HALT_INSTRUCTION:
reason = "debugger_statement";
description = "Paused on debugger statement";
break;
case SOURCE_LOCATION:
reason = "breakpoint";
description = "Paused on breakpoint";
}
}
if (stop) {
if (logMessages.isEmpty()) {
DebugException exception = event.getException();
if (exception != null) {
boolean uncaught = exception.getCatchLocation() == null;
description = uncaught ? "Paused on uncaught exception" : "Paused on caught exception";
}
if (reason == null) {
reason = "debugger_statement";
description = "Paused on debugger statement";
}
DebugProtocolClient client = context.getClient();
if (client != null) {
client.stopped(StoppedEvent.EventBody.create(reason).setThreadId(info.getThreadId()).setDescription(description));
}
} else {
info.executables.add(i -> {
for (String logMessage : logMessages) {
Matcher matcher = LOGMESSAGE_VARIABLE_REGEXP.matcher(logMessage);
StringBuilder sb = new StringBuilder();
int idx = 0;
while (matcher.find()) {
String expression = matcher.group(1);
DebugValue value = VariablesHandler.getDebugValue(event.getTopStackFrame(), expression);
sb.append(logMessage.substring(idx, matcher.start())).append(value.toDisplayString());
idx = matcher.end();
}
sb.append(logMessage.substring(idx));
context.getInfo().println(sb.toString());
}
return true;
});
}
info.runExecutables();
}
}
}
use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.
the class DebugProtocolServerImpl method exceptionInfo.
@Override
public CompletableFuture<ExceptionInfoResponse.ResponseBody> exceptionInfo(ExceptionInfoArguments args) {
CompletableFuture<ExceptionInfoResponse.ResponseBody> future = new CompletableFuture<>();
context.getThreadsHandler().executeInSuspendedThread(args.getThreadId(), (info) -> {
if (info == null) {
future.completeExceptionally(Errors.invalidThread(args.getThreadId()));
} else {
DebugException exception = info.getSuspendedEvent().getException();
if (exception == null) {
future.completeExceptionally(Errors.noStoredException());
} else {
DebugValue exceptionObject = exception.getExceptionObject();
String description = exceptionObject != null && exceptionObject.isReadable() ? exceptionObject.toDisplayString() : null;
DebugValue metaObject = exceptionObject != null ? exceptionObject.getMetaObject() : null;
String exceptionId = metaObject != null ? metaObject.getMetaSimpleName() : null;
future.complete(ExceptionInfoResponse.ResponseBody.create(exceptionId != null ? exceptionId : "Error", "unhandled").setDescription(description));
}
}
return false;
});
return future;
}
use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.
the class StackFramesHandler method getScopes.
public List<Scope> getScopes(ThreadsHandler.SuspendedThreadInfo info, int frameId) {
FrameWrapper frameWrapper = info.getById(FrameWrapper.class, frameId);
DebugStackFrame frame = frameWrapper != null ? frameWrapper.getFrame() : null;
if (frame != null) {
List<Scope> scopes = new ArrayList<>();
DebugScope dscope;
try {
dscope = frame.getScope();
} catch (DebugException ex) {
PrintWriter err = context.getErr();
if (err != null) {
err.println("getScope() has caused " + ex);
ex.printStackTrace(err);
}
dscope = null;
}
String scopeName = "Block";
boolean wasFunction = false;
ScopeWrapper topScopeWrapper = null;
DebugValue thisValue = null;
while (dscope != null) {
if (wasFunction) {
scopeName = "Closure";
} else if (dscope.isFunctionScope()) {
scopeName = "Local";
thisValue = dscope.getReceiver();
wasFunction = true;
}
if (dscope.isFunctionScope() || dscope.getDeclaredValues().iterator().hasNext()) {
// provide only scopes that have some variables
if (scopes.isEmpty()) {
topScopeWrapper = new ScopeWrapper(frameWrapper, dscope);
scopes.add(Scope.create(scopeName, info.getId(topScopeWrapper), false));
} else {
scopes.add(Scope.create(scopeName, info.getId(new ScopeWrapper(frameWrapper, dscope)), false));
}
}
dscope = getParent(dscope);
}
if (thisValue != null && topScopeWrapper != null) {
topScopeWrapper.thisValue = thisValue;
}
try {
dscope = debuggerSession.getTopScope(frame.getSourceSection().getSource().getLanguage());
} catch (DebugException ex) {
PrintWriter err = context.getErr();
if (err != null) {
err.println("getTopScope() has caused " + ex);
ex.printStackTrace(err);
}
}
while (dscope != null) {
if (dscope.isFunctionScope() || dscope.getDeclaredValues().iterator().hasNext()) {
// provide only scopes that have some variables
scopes.add(Scope.create("Global", info.getId(dscope), true));
}
dscope = getParent(dscope);
}
return scopes;
}
return null;
}
Aggregations