Search in sources :

Example 11 with DebugException

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

the class DebugExceptionTest method testLocationBreakpointOnException.

@Test
public void testLocationBreakpointOnException() {
    final Source source = testSource("ROOT(\n" + "  TRY(STATEMENT(THROW(a, b)), CATCH(a, EXPRESSION)),\n" + "  STATEMENT\n" + ")\n");
    Breakpoint breakpoint = Breakpoint.newBuilder(getSourceImpl(source)).lineIs(2).suspendAnchor(SuspendAnchor.AFTER).build();
    try (DebuggerSession session = startSession()) {
        session.install(breakpoint);
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            assertSame(breakpoint, event.getBreakpoints().iterator().next());
            assertSame(SuspendAnchor.AFTER, event.getSuspendAnchor());
            DebugException exception = event.getException();
            assertEquals("b", exception.getMessage());
            CatchLocation catchLocation = exception.getCatchLocation();
            assertEquals("TRY(STATEMENT(THROW(a, b))", catchLocation.getSourceSection().getCharacters());
            assertEquals(event.getTopStackFrame(), catchLocation.getFrame());
            SourceSection throwLocation = exception.getThrowLocation();
            assertEquals("THROW(a, b)", throwLocation.getCharacters());
            assertEquals("a: b", exception.getExceptionObject().toDisplayString());
        });
        expectDone();
        // Add exception breakpoints to test which breakpoints were hit
        Breakpoint uncaughtBreakpoint = Breakpoint.newExceptionBuilder(false, true).build();
        Breakpoint caughtBreakpoint = Breakpoint.newExceptionBuilder(true, false).build();
        session.install(uncaughtBreakpoint);
        session.install(caughtBreakpoint);
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            assertEquals(2, event.getBreakpoints().size());
            assertSame(caughtBreakpoint, event.getBreakpoints().get(0));
            assertSame(breakpoint, event.getBreakpoints().get(1));
            DebugException exception = event.getException();
            assertEquals("b", exception.getMessage());
        });
        expectDone();
    }
}
Also used : 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 12 with DebugException

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

the class DebugExceptionTest method testGetRawUncaughtExceptionRestricted.

@Test
public void testGetRawUncaughtExceptionRestricted() {
    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();
            // no access from other languages
            assertEquals(null, exception.getRawException(ProxyLanguage.class));
        });
        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) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test)

Example 13 with DebugException

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

the class LanguageExceptionsLegacyTest method testBuggyLanguageCalls.

private void testBuggyLanguageCalls(ProxyLanguage language, SuspendedCallback callback, String prefix) {
    ProxyLanguage.setDelegate(language);
    DebuggerSession session = tester.startSession();
    session.suspendNextExecution();
    Source source = Source.create(ProxyLanguage.ID, prefix + "1");
    tester.startEval(source);
    tester.expectSuspended((SuspendedEvent event) -> {
        try {
            callback.onSuspend(event);
            Assert.fail("No DebugException is thrown!");
        } catch (DebugException dex) {
            Assert.assertEquals("1", dex.getLocalizedMessage());
            verifyExStack(dex, IllegalStateException.class.getName());
        } catch (Throwable t) {
            Assert.fail(t.getLocalizedMessage());
        }
    });
    tester.expectDone();
    source = Source.create(ProxyLanguage.ID, prefix + "2");
    session.suspendNextExecution();
    tester.startEval(source);
    tester.expectSuspended((SuspendedEvent event) -> {
        try {
            callback.onSuspend(event);
            Assert.fail("No DebugException is thrown!");
        } catch (DebugException dex) {
            Assert.assertEquals("A TruffleException", dex.getLocalizedMessage());
            Assert.assertNull(Objects.toString(dex.getCause()), dex.getCause());
        } catch (Throwable t) {
            Assert.fail(t.getLocalizedMessage());
        }
    });
    tester.expectDone();
    source = Source.create(ProxyLanguage.ID, prefix + "3");
    session.suspendNextExecution();
    tester.startEval(source);
    tester.expectSuspended((SuspendedEvent event) -> {
        try {
            callback.onSuspend(event);
            Assert.fail("No DebugException is thrown!");
        } catch (DebugException dex) {
            Assert.assertEquals("3", dex.getLocalizedMessage());
            verifyExStack(dex, AssertionError.class.getName());
        } catch (Throwable t) {
            Assert.fail(t.getLocalizedMessage());
        }
    });
    tester.expectDone();
}
Also used : 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)

Example 14 with DebugException

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

the class DebugValueIteratorTest method testIterable.

@Test
public void testIterable() throws Throwable {
    List<Integer> list = Arrays.asList(1, 2, 3);
    Object iterable = new TestIteratorObject(true, false, list);
    checkDebugValueOf(iterable, value -> {
        assertTrue(value.hasIterator());
        assertFalse(value.isIterator());
        try {
            value.hasIteratorNextElement();
            fail();
        } catch (DebugException ex) {
        // O.K.
        }
        try {
            value.getIteratorNextElement();
            fail();
        } catch (DebugException ex) {
        // O.K.
        }
        // Iterable's iterator:
        DebugValue iterator = value.getIterator();
        checkIntIterator(iterator, list);
        // The iterator is re-created when obtained again:
        iterator = value.getIterator();
        checkIntIterator(iterator, list);
    });
}
Also used : DebugValue(com.oracle.truffle.api.debug.DebugValue) DebugException(com.oracle.truffle.api.debug.DebugException) Test(org.junit.Test)

Example 15 with DebugException

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

the class ObjectPreview method create.

private static JSONObject create(DebugValue debugValue, TYPE type, SUBTYPE subtype, boolean allowToStringSideEffects, LanguageInfo language, PrintWriter err, boolean isMapEntryKV) {
    JSONObject json = new JSONObject();
    json.put("type", type.getId());
    if (subtype != null) {
        json.put("subtype", subtype.getId());
    }
    boolean isArray = debugValue.isArray();
    boolean isMap = debugValue.hasHashEntries();
    if (isMapEntryKV) {
        String valueStr = RemoteObject.toString(debugValue, allowToStringSideEffects, err);
        json.putOpt("description", valueStr);
    } else {
        DebugValue metaObject = RemoteObject.getMetaObject(debugValue, language, err);
        String metaType = null;
        if (metaObject != null) {
            metaType = RemoteObject.toMetaName(metaObject, err);
            if (isArray) {
                metaType += "(" + debugValue.getArray().size() + ")";
            } else if (isMap) {
                metaType += "(" + debugValue.getHashSize() + ")";
            }
        }
        json.putOpt("description", metaType);
    }
    boolean overflow;
    JSONArray properties = new JSONArray();
    JSONArray entries = new JSONArray();
    if (isArray) {
        List<DebugValue> array = debugValue.getArray();
        int size = array.size();
        overflow = size > OVERFLOW_LIMIT_ARRAY_ELEMENTS;
        int n = Math.min(size, OVERFLOW_LIMIT_ARRAY_ELEMENTS);
        for (int i = 0; i < n; i++) {
            try {
                properties.put(createPropertyPreview(array.get(i), allowToStringSideEffects, language, err));
            } catch (DebugException ex) {
                overflow = true;
                break;
            }
        }
    } else if (isMap && !isMapEntryKV) {
        DebugValue entriesIter = debugValue.getHashEntriesIterator();
        overflow = false;
        while (entriesIter.hasIteratorNextElement()) {
            DebugValue entry = entriesIter.getIteratorNextElement();
            JSONObject entryPreview;
            try {
                entryPreview = createEntryPreview(entry, allowToStringSideEffects, language, err);
            } catch (DebugException ex) {
                overflow = true;
                break;
            }
            if (entryPreview != null) {
                if (entries.length() == OVERFLOW_LIMIT_PROPERTIES) {
                    overflow = true;
                    break;
                }
                entries.put(entryPreview);
            }
        }
    } else {
        Collection<DebugValue> valueProperties = debugValue.getProperties();
        if (valueProperties != null) {
            Iterator<DebugValue> propertyIterator = valueProperties.iterator();
            overflow = false;
            while (propertyIterator.hasNext()) {
                DebugValue property = propertyIterator.next();
                if (!property.isInternal() && !property.hasReadSideEffects() && property.isReadable()) {
                    if (properties.length() == OVERFLOW_LIMIT_PROPERTIES) {
                        overflow = true;
                        break;
                    }
                    try {
                        properties.put(createPropertyPreview(property, allowToStringSideEffects, language, err));
                    } catch (DebugException ex) {
                        overflow = true;
                        break;
                    }
                }
            }
        } else {
            overflow = false;
        }
    }
    json.put("overflow", overflow);
    json.put("properties", properties);
    if (isMap && !isMapEntryKV) {
        json.put("entries", entries);
    }
    return json;
}
Also used : JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) DebugValue(com.oracle.truffle.api.debug.DebugValue) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) Iterator(java.util.Iterator) Collection(java.util.Collection) DebugException(com.oracle.truffle.api.debug.DebugException)

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