Search in sources :

Example 6 with DebugException

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

the class InspectorRuntime method getProperties.

@Override
public Params getProperties(String objectId, boolean ownProperties, boolean accessorPropertiesOnly, boolean generatePreview) throws CommandProcessException {
    if (objectId == null) {
        throw new CommandProcessException("An objectId required.");
    }
    RemoteObject object = context.getRemoteObjectsHandler().getRemote(objectId);
    String objectGroup = context.getRemoteObjectsHandler().getObjectGroupOf(objectId);
    JSONObject json = new JSONObject();
    if (object != null) {
        DebugValue value = object.getDebugValue();
        RemoteObject.IndexRange indexRange = object.getIndexRange();
        try {
            if (value != null) {
                context.executeInSuspendThread(new SuspendThreadExecutable<Void>() {

                    @Override
                    public Void executeCommand() throws CommandProcessException {
                        TypeMark typeMark = object.getTypeMark();
                        if (typeMark != null) {
                            switch(typeMark) {
                                case MAP_ENTRIES:
                                    putMapEntries(json, value, indexRange, generatePreview, objectGroup);
                                    break;
                                case MAP_ENTRY:
                                    putMapEntry(json, value, generatePreview, objectGroup);
                                    break;
                                default:
                                    throw new CommandProcessException("Unknown type mark " + typeMark);
                            }
                            return null;
                        }
                        Collection<DebugValue> properties = null;
                        if (!value.hasHashEntries()) {
                            properties = value.getProperties();
                        }
                        if (properties == null) {
                            properties = Collections.emptyList();
                        } else if (indexRange != null && indexRange.isNamed()) {
                            List<DebugValue> list = new ArrayList<>(properties);
                            properties = list.subList(indexRange.start(), indexRange.end());
                        }
                        Collection<DebugValue> array;
                        if (!value.isArray()) {
                            array = Collections.emptyList();
                        } else if (indexRange != null && !indexRange.isNamed()) {
                            List<DebugValue> arr = value.getArray();
                            array = arr.subList(indexRange.start(), indexRange.end());
                        } else {
                            array = value.getArray();
                        }
                        putResultProperties(json, value, properties, array, generatePreview, objectGroup);
                        return null;
                    }

                    @Override
                    public Void processException(DebugException ex) {
                        fillExceptionDetails(json, ex);
                        return null;
                    }
                });
            } else {
                final DebugScope scope = object.getScope();
                context.executeInSuspendThread(new SuspendThreadExecutable<Void>() {

                    @Override
                    public Void executeCommand() throws CommandProcessException {
                        Collection<DebugValue> properties = new ArrayList<>();
                        DebugValue scopeReceiver = object.getScopeReceiver();
                        if (scopeReceiver != null) {
                            properties.add(scopeReceiver);
                        }
                        for (DebugValue p : scope.getDeclaredValues()) {
                            properties.add(p);
                        }
                        putResultProperties(json, null, properties, Collections.emptyList(), generatePreview, objectGroup);
                        return null;
                    }

                    @Override
                    public Void processException(DebugException ex) {
                        fillExceptionDetails(json, ex);
                        return null;
                    }
                });
            }
        } catch (NoSuspendedThreadException ex) {
            // Not suspended, no properties
            json.put("result", new JSONArray());
        }
    }
    return new Params(json);
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) TypeMark(com.oracle.truffle.tools.chromeinspector.types.RemoteObject.TypeMark) DebugValue(com.oracle.truffle.api.debug.DebugValue) ArrayList(java.util.ArrayList) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) NoSuspendedThreadException(com.oracle.truffle.tools.chromeinspector.InspectorExecutionContext.NoSuspendedThreadException) DebugException(com.oracle.truffle.api.debug.DebugException) DebugScope(com.oracle.truffle.api.debug.DebugScope) RemoteObject(com.oracle.truffle.tools.chromeinspector.types.RemoteObject) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) Collection(java.util.Collection)

Example 7 with DebugException

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

the class DebugValueHashMapTest method checkIntEntriesIterator.

static void checkIntEntriesIterator(DebugValue iterator, Collection<Map.Entry<HashElement, Object>> entries) {
    assertTrue(iterator.isIterator());
    // it's not iterable
    assertFalse(iterator.hasIterator());
    try {
        iterator.getIterator();
        fail();
    } catch (DebugException ex) {
    // O.K.
    }
    for (Map.Entry<HashElement, Object> entry : entries) {
        assertTrue(iterator.hasIteratorNextElement());
        DebugValue entryArray = iterator.getIteratorNextElement();
        assertTrue(entryArray.isArray());
        List<DebugValue> array = entryArray.getArray();
        assertEquals(2, array.size());
        DebugValue key = array.get(0);
        DebugValue value = array.get(1);
        assertTrue(key.isReadable());
        assertEquals(entry.getKey().isRemovable(), key.isWritable());
        assertEquals(entry.getKey().getKey(), key.asInt());
        assertEquals(entry.getKey().isReadable(), value.isReadable());
        assertEquals(entry.getKey().isWritable(), value.isWritable());
        if (entry.getKey().isReadable()) {
            assertEquals(entry.getValue(), value.asInt());
        } else {
            try {
                value.asInt();
                fail();
            } catch (UnsupportedOperationException ex) {
            // O.K.
            }
        }
    }
    assertFalse(iterator.hasIteratorNextElement());
    try {
        iterator.getIteratorNextElement();
        fail();
    } catch (NoSuchElementException ex) {
    // O.K.
    }
}
Also used : DebugValue(com.oracle.truffle.api.debug.DebugValue) HashElement(com.oracle.truffle.api.debug.test.TestHashObject.HashElement) DebugException(com.oracle.truffle.api.debug.DebugException) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with DebugException

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

the class DebugExceptionTest method testUncaughtException1.

@Test
public void testUncaughtException1() {
    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();
            assertNull(exception.getCatchLocation());
            assertEquals("b", exception.getMessage());
            SourceSection throwLocation = exception.getThrowLocation();
            assertEquals(10, throwLocation.getCharIndex());
            assertEquals(21, throwLocation.getCharEndIndex());
            assertEquals("a: b", exception.getExceptionObject().toDisplayString());
            assertDebugStackTrace(exception.getDebugStackTrace(), " <1:11, 1:21>");
            assertStack(exception.getStackTrace(), "<instrumentation-test-language>.(Unnamed:1)");
        });
        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) SourceSection(com.oracle.truffle.api.source.SourceSection) DebugException(com.oracle.truffle.api.debug.DebugException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 9 with DebugException

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

the class DebugExceptionTest method testUncaughtException2.

@Test
public void testUncaughtException2() {
    Source testSource = testSource("ROOT(\n" + "DEFINE(UncaughtThrow,\n" + "  TRY(STATEMENT, STATEMENT(THROW(IllegalState, TestExceptionMessage)),\n" + "      CATCH(a, STATEMENT))\n" + "),\n" + "CALL(UncaughtThrow))");
    Breakpoint uncaughtBreakpoint = Breakpoint.newExceptionBuilder(false, true).build();
    try (DebuggerSession session = startSession()) {
        session.install(uncaughtBreakpoint);
        assertTrue(uncaughtBreakpoint.isResolved());
        startEval(testSource);
        expectSuspended((SuspendedEvent event) -> {
            assertSame(uncaughtBreakpoint, event.getBreakpoints().iterator().next());
            assertSame(SuspendAnchor.AFTER, event.getSuspendAnchor());
            DebugException exception = event.getException();
            assertNull(exception.getCatchLocation());
            assertEquals("TestExceptionMessage", exception.getMessage());
            SourceSection throwLocation = exception.getThrowLocation();
            assertEquals("THROW(IllegalState, TestExceptionMessage)", throwLocation.getCharacters());
            assertEquals("IllegalState: TestExceptionMessage", exception.getExceptionObject().toDisplayString());
            assertDebugStackTrace(exception.getDebugStackTrace(), "UncaughtThrow <3:28, 3:68>", " <6:1, 6:19>");
            assertStack(exception.getStackTrace(), "<instrumentation-test-language>.UncaughtThrow(Unnamed:3)", "<instrumentation-test-language>.(Unnamed:6)");
        });
        Throwable t = expectThrowable();
        assertEquals("TestExceptionMessage", 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) SourceSection(com.oracle.truffle.api.source.SourceSection) DebugException(com.oracle.truffle.api.debug.DebugException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 10 with DebugException

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

the class DebugExceptionTest method testCaughtException1.

@Test
public void testCaughtException1() {
    Source testSource = testSource("TRY(STATEMENT(THROW(NPE, TestExceptionMessage)), CATCH(NPE, STATEMENT))\n");
    Breakpoint caughtBreakpoint = Breakpoint.newExceptionBuilder(true, true).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(THROW(NPE, TestExceptionMessage))", catchLocation.getSourceSection().getCharacters());
            assertEquals(event.getTopStackFrame(), catchLocation.getFrame());
            SourceSection throwLocation = exception.getThrowLocation();
            assertEquals("THROW(NPE, TestExceptionMessage)", throwLocation.getCharacters());
            assertEquals("NPE: TestExceptionMessage", exception.getExceptionObject().toDisplayString());
            assertDebugStackTrace(exception.getDebugStackTrace(), " <1:15, 1:46>");
            assertStack(exception.getStackTrace(), "<instrumentation-test-language>.(Unnamed:1)");
        });
        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)

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