Search in sources :

Example 21 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class CallFrame method createJSON.

private JSONObject createJSON() {
    JSONObject json = new JSONObject();
    json.put("callFrameId", Integer.toString(depth));
    try {
        String functionName = frame.getName();
        if (functionName == null) {
            functionName = "";
        }
        json.put("functionName", functionName);
    } catch (DebugException ex) {
        json.put("functionName", ex.getLocalizedMessage());
    }
    json.put("location", location.toJSON());
    json.putOpt("functionLocation", (functionLocation != null) ? functionLocation.toJSON() : null);
    json.put("url", url);
    json.put("scopeChain", Scope.createScopesJSON(scopes));
    if (thisObject != null) {
        json.put("this", thisObject.toJSON());
    } else {
        json.put("this", JSONObject.NULL);
    }
    if (returnObject != null) {
        json.put("returnValue", returnObject.toJSON());
    }
    return json;
}
Also used : JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) DebugException(com.oracle.truffle.api.debug.DebugException)

Example 22 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class CustomPreview method value2JSON.

public static Object value2JSON(DebugValue value, InspectorExecutionContext context) {
    if (value.isArray()) {
        JSONArray json = new JSONArray();
        List<DebugValue> array = value.getArray();
        if (array.size() == 2 && array.get(0).isReadable() && array.get(1).isReadable() && "object".equals(array.get(0).asString()) && array.get(1).getProperty("config") != null) {
            // Child object:
            json.put(value2JSON(array.get(0), context));
            DebugValue child = array.get(1);
            DebugValue object = child.getProperty("object");
            DebugValue config = child.getProperty("config");
            RemoteObject remoteConfig = context.getRemoteObjectsHandler().getRemote(object);
            JSONObject childJSON = remoteConfig.toJSON();
            JSONObject customPreview = create(object, config, object.getOriginalLanguage(), context);
            if (customPreview != null) {
                childJSON.put("customPreview", customPreview);
            }
            json.put(childJSON);
        } else {
            for (DebugValue element : array) {
                if (element.isReadable()) {
                    json.put(value2JSON(element, context));
                } else {
                    json.put(InspectorExecutionContext.VALUE_NOT_READABLE);
                }
            }
        }
        return json;
    } else {
        Collection<DebugValue> properties = value.getProperties();
        if (properties != null) {
            JSONObject json = new JSONObject();
            for (DebugValue property : properties) {
                try {
                    if (property.isReadable() && !property.canExecute() && !property.isInternal()) {
                        Object rawValue = getPrimitiveValue(property);
                        // Do not allow inner objects
                        if (rawValue != null) {
                            json.put(property.getName(), rawValue);
                        }
                    }
                } catch (DebugException ex) {
                    if (ex.isInternalError()) {
                        PrintWriter err = context.getErr();
                        if (err != null) {
                            ex.printStackTrace(err);
                        }
                    } else {
                        throw ex;
                    }
                }
            }
            return json;
        } else {
            return getPrimitiveValue(value);
        }
    }
}
Also used : DebugValue(com.oracle.truffle.api.debug.DebugValue) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) DebugException(com.oracle.truffle.api.debug.DebugException) PrintWriter(java.io.PrintWriter)

Example 23 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class CustomPreview method create.

private static JSONObject create(DebugValue debugValue, DebugValue config, LanguageInfo language, InspectorExecutionContext context) {
    DebuggerSession debuggerSession = debugValue.getSession();
    DebugScope topScope = debuggerSession.getTopScope(language.getId());
    DebugValue formatters = null;
    while (topScope != null) {
        formatters = topScope.getDeclaredValue(DEVTOOLS_FORMATTERS);
        if (formatters != null) {
            break;
        } else {
            topScope = topScope.getParent();
        }
    }
    if (formatters == null || !formatters.isArray()) {
        return null;
    }
    for (DebugValue formatter : formatters.getArray()) {
        DebugValue headerFunction;
        try {
            headerFunction = formatter.getProperty(HEADER);
            if (headerFunction == null || !headerFunction.canExecute()) {
                continue;
            }
        } catch (DebugException ex) {
            continue;
        }
        DebugValue header = (config != null) ? headerFunction.execute(debugValue, config) : headerFunction.execute(debugValue);
        if (header.isNull()) {
            continue;
        }
        boolean hasBody = false;
        DebugValue hasBodyFunction;
        try {
            hasBodyFunction = formatter.getProperty(HAS_BODY);
            if (hasBodyFunction != null && hasBodyFunction.canExecute()) {
                DebugValue hasBodyValue = (config != null) ? hasBodyFunction.execute(debugValue, config) : hasBodyFunction.execute(debugValue);
                hasBody = hasBodyValue.isBoolean() ? hasBodyValue.asBoolean() : false;
            }
        } catch (DebugException ex) {
            PrintWriter err = context.getErr();
            if (err != null) {
                ex.printStackTrace(err);
            }
        }
        DebugValue bodyFunction = null;
        if (hasBody) {
            try {
                bodyFunction = formatter.getProperty(BODY);
                if (bodyFunction != null && !bodyFunction.canExecute()) {
                    bodyFunction = null;
                }
            } catch (DebugException ex) {
                PrintWriter err = context.getErr();
                if (err != null) {
                    ex.printStackTrace(err);
                }
            }
        }
        return createJSON(debugValue, config, header, bodyFunction, context);
    }
    return null;
}
Also used : DebugScope(com.oracle.truffle.api.debug.DebugScope) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) DebugException(com.oracle.truffle.api.debug.DebugException) PrintWriter(java.io.PrintWriter)

Example 24 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class DebugExceptionTest method testCaughtException2.

@Test
public void testCaughtException2() {
    Source testSource = testSource("ROOT(\n" + "DEFINE(CaughtThrow,\n" + "  TRY(STATEMENT(CALL(ThrownNPE)),\n" + "      CATCH(NPE, STATEMENT))\n" + "),\n" + "DEFINE(ThrownNPE,\n" + "  STATEMENT(THROW(NPE, TestExceptionMessage))\n" + "),\n" + "CALL(CaughtThrow))");
    Breakpoint caughtBreakpoint = Breakpoint.newExceptionBuilder(true, false).build();
    try (DebuggerSession session = startSession()) {
        session.install(caughtBreakpoint);
        assertTrue(caughtBreakpoint.isResolved());
        startEval(testSource);
        expectSuspended((SuspendedEvent event) -> {
            assertSame(caughtBreakpoint, event.getBreakpoints().iterator().next());
            assertSame(SuspendAnchor.AFTER, event.getSuspendAnchor());
            DebugException exception = event.getException();
            assertEquals("TestExceptionMessage", exception.getMessage());
            CatchLocation catchLocation = exception.getCatchLocation();
            assertEquals("TRY(STATEMENT(CALL(ThrownNPE))", catchLocation.getSourceSection().getCharacters());
            Iterator<DebugStackFrame> stackFrames = event.getStackFrames().iterator();
            stackFrames.next();
            DebugStackFrame nextFrame = stackFrames.next();
            assertEquals(nextFrame, catchLocation.getFrame());
            SourceSection throwLocation = exception.getThrowLocation();
            assertEquals("THROW(NPE, TestExceptionMessage)", throwLocation.getCharacters());
            assertEquals("NPE: TestExceptionMessage", exception.getExceptionObject().toDisplayString());
            assertDebugStackTrace(exception.getDebugStackTrace(), "ThrownNPE <7:13, 7:44>", "CaughtThrow <3:17, 3:31>", " <9:1, 9:17>");
            assertStack(exception.getStackTrace(), "<instrumentation-test-language>.ThrownNPE(Unnamed:7)", "<instrumentation-test-language>.CaughtThrow(Unnamed:3)", "<instrumentation-test-language>.(Unnamed:9)");
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) CatchLocation(com.oracle.truffle.api.debug.DebugException.CatchLocation) SourceSection(com.oracle.truffle.api.source.SourceSection) DebugException(com.oracle.truffle.api.debug.DebugException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 25 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class DebugExceptionTest method testEvalExceptional.

@Test
public void testEvalExceptional() {
    Source testSource = testSource("ROOT(\n" + "  DEFINE(foo,\n" + "    STATEMENT\n" + "  ),\n" + "  CALL(foo)\n" + ")\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(testSource);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 3, true, "STATEMENT");
            try {
                event.getTopStackFrame().eval("STATEMENT(THROW(NPE, TestExceptionMessage))");
                fail();
            } catch (DebugException dex) {
                SourceSection throwLocation = dex.getThrowLocation();
                assertEquals("THROW(NPE, TestExceptionMessage)", throwLocation.getCharacters());
                assertNull(dex.getCatchLocation());
                assertEquals("NPE: TestExceptionMessage", dex.getExceptionObject().toDisplayString());
                List<DebugStackTraceElement> debugStackTrace = dex.getDebugStackTrace();
                assertEquals(2, debugStackTrace.size());
                assertEquals("THROW(NPE, TestExceptionMessage)", debugStackTrace.get(0).getSourceSection().getCharacters());
                assertEquals("CALL(foo)", debugStackTrace.get(1).getSourceSection().getCharacters());
            }
        });
    }
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) List(java.util.List) SourceSection(com.oracle.truffle.api.source.SourceSection) DebugException(com.oracle.truffle.api.debug.DebugException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

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