Search in sources :

Example 26 with DebugException

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());
    }
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) DebugException(com.oracle.truffle.api.debug.DebugException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 27 with DebugException

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());
    }
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SourceSection(com.oracle.truffle.api.source.SourceSection) DebugException(com.oracle.truffle.api.debug.DebugException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 28 with DebugException

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();
        }
    }
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebugValue(com.oracle.truffle.api.debug.DebugValue) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) DebugException(com.oracle.truffle.api.debug.DebugException) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebugProtocolClient(com.oracle.truffle.tools.dap.types.DebugProtocolClient)

Example 29 with DebugException

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DebugValue(com.oracle.truffle.api.debug.DebugValue) DebugException(com.oracle.truffle.api.debug.DebugException)

Example 30 with DebugException

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;
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebugScope(com.oracle.truffle.api.debug.DebugScope) Scope(com.oracle.truffle.tools.dap.types.Scope) DebugScope(com.oracle.truffle.api.debug.DebugScope) DebugValue(com.oracle.truffle.api.debug.DebugValue) ArrayList(java.util.ArrayList) DebugException(com.oracle.truffle.api.debug.DebugException) PrintWriter(java.io.PrintWriter)

Aggregations

DebugException (com.oracle.truffle.api.debug.DebugException)34 DebugValue (com.oracle.truffle.api.debug.DebugValue)20 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)12 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)12 JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)12 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)11 Source (org.graalvm.polyglot.Source)11 SourceSection (com.oracle.truffle.api.source.SourceSection)10 Test (org.junit.Test)10 PrintWriter (java.io.PrintWriter)8 DebugScope (com.oracle.truffle.api.debug.DebugScope)7 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)7 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)6 NoSuspendedThreadException (com.oracle.truffle.tools.chromeinspector.InspectorExecutionContext.NoSuspendedThreadException)6 JSONArray (com.oracle.truffle.tools.utils.json.JSONArray)6 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)5 RemoteObject (com.oracle.truffle.tools.chromeinspector.types.RemoteObject)5 ArrayList (java.util.ArrayList)5 CatchLocation (com.oracle.truffle.api.debug.DebugException.CatchLocation)3 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)3