Search in sources :

Example 61 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SLDebugDirectTest method stepInStepOver.

@Test
public void stepInStepOver() throws Throwable {
    final Source factorial = createFactorial();
    context.eval(factorial);
    session.suspendNextExecution();
    assertLocation("test", 2, true, "res = fac(2)", "res", UNASSIGNED);
    stepInto(1);
    assertLocation("fac", 7, true, "n <= 1", "n", "2", "nMinusOne", UNASSIGNED, "nMOFact", UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 10, true, "nMinusOne = n - 1", "n", "2", "nMinusOne", UNASSIGNED, "nMOFact", UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 11, true, "nMOFact = fac(nMinusOne)", "n", "2", "nMinusOne", "1", "nMOFact", UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 12, true, "res = n * nMOFact", "n", "2", "nMinusOne", "1", "nMOFact", "1", "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 13, true, "return res", "n", "2", "nMinusOne", "1", "nMOFact", "1", "res", "2");
    stepOver(1);
    assertLocation("test", 2, false, "fac(2)", "res", UNASSIGNED);
    stepOver(1);
    assertLocation("test", 3, true, "println(res)", "res", "2");
    stepOut();
    Value value = context.getBindings("sl").getMember("test");
    assertTrue(value.canExecute());
    Value resultValue = value.execute();
    String resultStr = resultValue.toString();
    Number result = resultValue.asInt();
    assertExecutedOK();
    assertNotNull(result);
    assertEquals("Factorial computed OK", 2, result.intValue());
    assertEquals("Factorial computed OK", "2", resultStr);
}
Also used : Value(org.graalvm.polyglot.Value) DebugValue(com.oracle.truffle.api.debug.DebugValue) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 62 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SLDebugDirectTest method testNull.

@Test
public void testNull() throws Throwable {
    final Source nullTest = createNull();
    context.eval(nullTest);
    session.suspendNextExecution();
    assertLocation("nullTest", 2, true, "res = doNull()", "res", UNASSIGNED);
    stepInto(1);
    assertLocation("nullTest", 3, true, "return res", "res", "NULL");
    continueExecution();
    Value value = context.getBindings("sl").getMember("nullTest").execute();
    assertExecutedOK();
    String val = value.toString();
    assertNotNull(val);
    assertEquals("SL displays null as NULL", "NULL", val);
}
Also used : Value(org.graalvm.polyglot.Value) DebugValue(com.oracle.truffle.api.debug.DebugValue) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 63 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SLDebugTest method testDebugger.

@Test
public void testDebugger() throws Throwable {
    /*
         * Test AlwaysHalt is working.
         */
    final Source factorial = slCode("function main() {\n" + "  return fac(5);\n" + "}\n" + "function fac(n) {\n" + "  if (n <= 1) {\n" + // // break
    "    debugger; return 1;\n" + "  }\n" + "  return n * fac(n - 1);\n" + "}\n");
    try (DebuggerSession session = startSession()) {
        startEval(factorial);
        // make javac happy and use the session
        session.getBreakpoints();
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "fac", 6, true, "debugger", "n", "1").prepareContinue();
        });
        expectDone();
    }
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 64 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SLDebugTest method testStack.

@Test
public void testStack() {
    final Source stackSource = slCode("function main() {\n" + "  return fac(10);\n" + "}\n" + "function fac(n) {\n" + "  if (n <= 1) {\n" + // break
    "    return 1;\n" + "  }\n" + "  return n * fac(n - 1);\n" + "}\n");
    try (DebuggerSession session = startSession()) {
        session.install(Breakpoint.newBuilder(getSourceImpl(stackSource)).lineIs(6).build());
        startEval(stackSource);
        expectSuspended((SuspendedEvent event) -> {
            Iterator<DebugStackFrame> sfIt = event.getStackFrames().iterator();
            assertTrue(sfIt.hasNext());
            DebugStackFrame dsf = sfIt.next();
            assertEquals("fac", dsf.getName());
            assertEquals(6, dsf.getSourceSection().getStartLine());
            assertFalse(dsf.isInternal());
            int numStacksAt8 = 10 - 1;
            for (int i = 0; i < numStacksAt8; i++) {
                assertTrue(sfIt.hasNext());
                dsf = sfIt.next();
                assertEquals("fac", dsf.getName());
                assertEquals(8, dsf.getSourceSection().getStartLine());
                assertFalse(dsf.isInternal());
            }
            assertTrue(sfIt.hasNext());
            dsf = sfIt.next();
            assertEquals("main", dsf.getName());
            assertEquals(2, dsf.getSourceSection().getStartLine());
            assertFalse(dsf.isInternal());
            // skip internal frames
            while (sfIt.hasNext()) {
                dsf = sfIt.next();
                assertTrue(dsf.isInternal());
            }
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Test(org.junit.Test)

Example 65 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SLDebugTest method testBreakpoint.

@Test
public void testBreakpoint() throws Throwable {
    /*
         * Wrappers need to remain inserted for recursive functions to work for debugging. Like in
         * this test case when the breakpoint is in the exit condition and we want to step out.
         */
    final Source factorial = slCode("function main() {\n" + "  return fac(5);\n" + "}\n" + "function fac(n) {\n" + "  if (n <= 1) {\n" + // break
    "    return 1;\n" + "  }\n" + "  return n * fac(n - 1);\n" + "}\n");
    try (DebuggerSession session = startSession()) {
        startEval(factorial);
        Breakpoint breakpoint = session.install(Breakpoint.newBuilder(getSourceImpl(factorial)).lineIs(6).build());
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "fac", 6, true, "return 1", "n", "1");
            checkArgs(event.getTopStackFrame(), "n", "1");
            Iterator<DebugStackFrame> sfi = event.getStackFrames().iterator();
            for (int i = 1; i <= 5; i++) {
                checkArgs(sfi.next(), "n", Integer.toString(i));
            }
            // main
            checkArgs(sfi.next());
            assertSame(breakpoint, event.getBreakpoints().iterator().next());
            event.prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "fac", 8, false, "fac(n - 1)", "n", "2");
            checkArgs(event.getTopStackFrame(), "n", "2");
            assertEquals("1", event.getReturnValue().as(String.class));
            assertTrue(event.getBreakpoints().isEmpty());
            event.prepareStepOut(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "fac", 8, false, "fac(n - 1)", "n", "3");
            assertEquals("2", event.getReturnValue().as(String.class));
            assertTrue(event.getBreakpoints().isEmpty());
            event.prepareStepOut(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "fac", 8, false, "fac(n - 1)", "n", "4");
            assertEquals("6", event.getReturnValue().as(String.class));
            assertTrue(event.getBreakpoints().isEmpty());
            event.prepareStepOut(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "fac", 8, false, "fac(n - 1)", "n", "5");
            assertEquals("24", event.getReturnValue().as(String.class));
            assertTrue(event.getBreakpoints().isEmpty());
            event.prepareStepOut(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "main", 2, false, "fac(5)");
            checkArgs(event.getTopStackFrame());
            assertEquals("120", event.getReturnValue().as(String.class));
            assertTrue(event.getBreakpoints().isEmpty());
            event.prepareStepOut(1);
        });
        assertEquals("120", 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) Source(org.graalvm.polyglot.Source) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Test(org.junit.Test)

Aggregations

Source (org.graalvm.polyglot.Source)196 Test (org.junit.Test)165 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)103 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)95 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)51 Value (org.graalvm.polyglot.Value)31 Context (org.graalvm.polyglot.Context)25 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)21 DebugValue (com.oracle.truffle.api.debug.DebugValue)20 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)14 Instrument (org.graalvm.polyglot.Instrument)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 SourceSection (com.oracle.truffle.api.source.SourceSection)12 EventContext (com.oracle.truffle.api.instrumentation.EventContext)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Engine (org.graalvm.polyglot.Engine)9 ArrayList (java.util.ArrayList)8 PolyglotException (org.graalvm.polyglot.PolyglotException)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 AbstractInstrumentationTest (com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)7