Search in sources :

Example 16 with DebuggerSession

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

the class ValueLanguageTest method testValueLanguage.

@Test
public void testValueLanguage() {
    Source source1 = Source.create(ValuesLanguage1.ID, "i=10\n" + "s=test\n" + "a=null\n" + "b={}\n" + "b.a={}\n" + "b.j=100\n" + "b.k=200\n");
    Source source2 = Source.create(ValuesLanguage2.ID, "j=20\n" + "s=test2\n" + "d=null\n" + "e={}\n" + "b.c={}\n" + "e.d={}\n" + "e.k=200\n");
    try (DebuggerSession session = startSession()) {
        Breakpoint bp1 = Breakpoint.newBuilder(getSourceImpl(source1)).lineIs(7).build();
        session.install(bp1);
        startEval(source1);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            DebugValue value = frame.getScope().getDeclaredValue("i");
            assertNull(value.getOriginalLanguage());
            assertEquals("L1:10", value.as(String.class));
            value = frame.getScope().getDeclaredValue("s");
            assertNull(value.getOriginalLanguage());
            assertEquals("L1:test", value.as(String.class));
            value = frame.getScope().getDeclaredValue("a");
            assertNull(value.getOriginalLanguage());
            assertEquals("null", value.as(String.class));
            value = frame.getScope().getDeclaredValue("b");
            LanguageInfo lang = value.getOriginalLanguage();
            assertNotNull(lang);
            assertEquals(ValuesLanguage1.NAME, lang.getName());
            assertEquals("{a={}, j=100}", value.as(String.class));
            event.prepareContinue();
        });
        expectDone();
        Breakpoint bp2 = Breakpoint.newBuilder(getSourceImpl(source2)).lineIs(7).build();
        session.install(bp2);
        startEval(source2);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            DebugValue value = frame.getScope().getDeclaredValue("j");
            assertNull(value.getOriginalLanguage());
            assertEquals("L2:20", value.as(String.class));
            value = frame.getScope().getDeclaredValue("s");
            assertNull(value.getOriginalLanguage());
            assertEquals("L2:test2", value.as(String.class));
            value = frame.getScope().getDeclaredValue("e");
            LanguageInfo lang2 = value.getOriginalLanguage();
            assertNotNull(lang2);
            assertEquals(ValuesLanguage2.NAME, lang2.getName());
            assertEquals("{d={}}", value.as(String.class));
            value = frame.getScope().getDeclaredValue("b");
            LanguageInfo lang1 = value.getOriginalLanguage();
            assertNotNull(lang1);
            assertNotEquals(lang1, lang2);
            assertEquals(ValuesLanguage1.NAME, lang1.getName());
            // info from current lang2:
            assertEquals("Object", value.as(String.class));
            assertEquals("L2:Object", value.getMetaObject().as(String.class));
            // info from original lang1:
            value = value.asInLanguage(lang1);
            assertEquals("{a={}, j=100, k=200, c={}}", value.as(String.class));
            assertEquals("L1:Map", value.getMetaObject().as(String.class));
            assertEquals("L2:Map", value.getMetaObject().asInLanguage(lang2).as(String.class));
            // Properties are always in the original language:
            value = frame.getScope().getDeclaredValue("b");
            DebugValue a = value.getProperties().iterator().next();
            assertEquals(lang1, a.getOriginalLanguage());
            Iterator<DebugValue> it = value.getProperties().iterator();
            it.next();
            it.next();
            it.next();
            DebugValue c = it.next();
            assertEquals(lang2, c.getOriginalLanguage());
            value = value.asInLanguage(lang2);
            a = value.getProperties().iterator().next();
            assertEquals(lang1, a.getOriginalLanguage());
            it = value.getProperties().iterator();
            it.next();
            it.next();
            it.next();
            c = it.next();
            assertEquals(lang2, c.getOriginalLanguage());
            value = frame.getScope().getDeclaredValue("j");
            assertNull(value.getSourceLocation());
            value = value.asInLanguage(lang1);
            assertEquals("L1:20", value.as(String.class));
            assertNull(value.getSourceLocation());
            value = frame.getScope().getDeclaredValue("d");
            assertEquals("null", value.as(String.class));
            value = value.asInLanguage(lang1);
            assertEquals("null", value.as(String.class));
            value = frame.getScope().getDeclaredValue("e");
            assertEquals(getSourceImpl(source2).createSection(4, 3, 2), value.getSourceLocation());
            value = value.asInLanguage(lang1);
            assertNull(value.getSourceLocation());
            event.prepareContinue();
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 17 with DebuggerSession

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

the class DebugScopeTest method testTopScope.

// Local scopes are well tested by DebugStackFrameTest and others.
@Test
public void testTopScope() {
    final Source source = testSource("ROOT(DEFINE(function1,ROOT(\n" + "  EXPRESSION()\n" + "  )\n" + "),\n" + "DEFINE(g,ROOT(\n" + "  EXPRESSION()\n" + "  )\n" + "),\n" + "STATEMENT())\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            DebugScope topScope = session.getTopScope(event.getSourceSection().getSource().getLanguage());
            assertNotNull(topScope);
            DebugValue function1 = topScope.getDeclaredValue("function1");
            assertNotNull(function1);
            assertTrue(function1.as(String.class).contains("Function"));
            DebugValue functionType = function1.getMetaObject();
            assertEquals("Function", functionType.as(String.class));
            assertEquals(function1.getOriginalLanguage(), functionType.getOriginalLanguage());
            DebugValue g = topScope.getDeclaredValue("g");
            assertNotNull(g);
            assertTrue(g.as(String.class).contains("Function"));
        });
        expectDone();
    }
}
Also used : DebugScope(com.oracle.truffle.api.debug.DebugScope) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 18 with DebuggerSession

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

the class DebugStackFrameTest method testEvalAndSideEffects.

@Test
public void testEvalAndSideEffects() throws Throwable {
    final Source source = testSource("ROOT(DEFINE(a,ROOT( \n" + "  VARIABLE(a, 42), \n" + "  VARIABLE(b, 43), \n" + "  VARIABLE(c, 44), \n" + // will start stepping here
    "  STATEMENT(),\n" + "  STATEMENT())\n" + "), \n" + "VARIABLE(a, 42), VARIABLE(b, 43), VARIABLE(c, 44), \n" + "CALL(a))\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            Iterator<DebugStackFrame> stackFrames = event.getStackFrames().iterator();
            // assert changes to the current frame
            DebugStackFrame frame = stackFrames.next();
            assertDynamicFrame(frame);
            DebugValue aValue = frame.getScope().getDeclaredValue("a");
            String aStringValue = aValue.as(String.class);
            // assert changes to a parent frame
            frame = stackFrames.next();
            assertDynamicFrame(frame);
            // assign from one stack frame to another one
            frame.getScope().getDeclaredValue("a").set(aValue);
            assertEquals(aStringValue, frame.getScope().getDeclaredValue("a").as(String.class));
            event.prepareContinue();
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 19 with DebuggerSession

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

the class DebugStackFrameTest method testSourceSections.

@Test
public void testSourceSections() {
    final Source source = testSource("ROOT(DEFINE(a,ROOT(\n" + "  STATEMENT())\n" + "),\n" + "DEFINE(b,ROOT(\n" + "  CALL(a))\n" + "), \n" + "CALL(b))\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            SourceSection ss = frame.getSourceSection();
            assertSection(ss, "STATEMENT()", 2, 3, 2, 13);
            SourceSection fss = getFunctionSourceSection(frame);
            assertSection(fss, "ROOT(\n  STATEMENT())\n", 1, 15, 2, 15);
            Iterator<DebugStackFrame> stackFrames = event.getStackFrames().iterator();
            // The top one
            assertEquals(frame, stackFrames.next());
            // b
            frame = stackFrames.next();
            ss = frame.getSourceSection();
            assertSection(ss, "CALL(a)", 5, 3, 5, 9);
            fss = getFunctionSourceSection(frame);
            assertSection(fss, "ROOT(\n  CALL(a))\n", 4, 10, 5, 11);
            // root
            frame = stackFrames.next();
            ss = frame.getSourceSection();
            assertSection(ss, "CALL(b)", 7, 1, 7, 7);
            fss = getFunctionSourceSection(frame);
            assertSection(fss, source.getCharacters().toString(), 1, 1, 7, 9);
            assertFalse(stackFrames.hasNext());
            event.prepareContinue();
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 20 with DebuggerSession

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

the class DebuggerContextsTest method testMultipleContexts.

@Test
public void testMultipleContexts() {
    Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
    Engine engine = Engine.create();
    TestContextsListener contextsListener = new TestContextsListener();
    List<ContextEvent> events = contextsListener.events;
    int numContexts = 5;
    Debugger debugger = engine.getInstruments().get("debugger").lookup(Debugger.class);
    try (DebuggerSession session = debugger.startSession(null)) {
        session.setContextsListener(contextsListener, false);
        for (int i = 0; i < numContexts; i++) {
            try (Context context = Context.newBuilder().engine(engine).build()) {
                assertEquals(6 * i + 1, events.size());
                context.eval(source);
            }
            assertEquals(6 * i + 6, events.size());
        }
        assertEquals(6 * numContexts, events.size());
        DebugContext lastContext = null;
        for (int i = 0; i < numContexts; i++) {
            int ci = 6 * i;
            assertTrue(events.get(ci).created);
            assertNull(events.get(ci).language);
            assertNotEquals(lastContext, events.get(ci).context);
            lastContext = events.get(ci).context;
            assertTrue(events.get(ci + 1).created);
            assertNotNull(events.get(ci + 1).language);
            assertEquals(lastContext, events.get(ci + 1).context);
            assertTrue(events.get(ci + 2).languageInitialized);
            assertTrue(events.get(ci + 3).languageFinalized);
            assertNotNull(events.get(ci + 4).language);
            assertNull(events.get(ci + 5).language);
        }
        engine.close();
    }
    // No more events
    assertEquals(6 * numContexts, events.size());
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebugContext(com.oracle.truffle.api.debug.DebugContext) Context(org.graalvm.polyglot.Context) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugContext(com.oracle.truffle.api.debug.DebugContext) Source(org.graalvm.polyglot.Source) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Aggregations

DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)110 Source (org.graalvm.polyglot.Source)103 Test (org.junit.Test)102 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)97 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)49 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)20 DebugValue (com.oracle.truffle.api.debug.DebugValue)16 Debugger (com.oracle.truffle.api.debug.Debugger)14 Context (org.graalvm.polyglot.Context)11 SourceSection (com.oracle.truffle.api.source.SourceSection)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 DebugContext (com.oracle.truffle.api.debug.DebugContext)6 SuspendedCallback (com.oracle.truffle.api.debug.SuspendedCallback)6 DebugScope (com.oracle.truffle.api.debug.DebugScope)5 Engine (org.graalvm.polyglot.Engine)5 HashMap (java.util.HashMap)3 Value (org.graalvm.polyglot.Value)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2