Search in sources :

Example 1 with SuspendedEvent

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

the class NestedContextTest method testRecursiveEval.

@Test
public void testRecursiveEval() throws Exception {
    final Source testSource = testSource("ROOT(\n" + "  STATEMENT,\n" + "  STATEMENT\n" + ")\n");
    final Context context = Context.create();
    final AtomicInteger suspensionCount = new AtomicInteger(0);
    Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
    try (DebuggerSession session = debugger.startSession(new SuspendedCallback() {

        public void onSuspend(SuspendedEvent event) {
            checkState(event, 3, true, "STATEMENT");
            // recursive evaluation should not trigger a suspended event
            context.eval(testSource);
            suspensionCount.incrementAndGet();
        }
    })) {
        session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
        context.eval(testSource);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 2 with SuspendedEvent

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

the class ReenterStackFrameTest method testReenterDeep.

@Test
public void testReenterDeep() throws Throwable {
    final Source source = testSource("ROOT(DEFINE(a, ROOT(\n" + " STATEMENT(),\n" + " DEFINE(aa, ROOT(\n" + "  STATEMENT(EXPRESSION),\n" + "  DEFINE(aaa, ROOT(\n" + "   STATEMENT(EXPRESSION, EXPRESSION))\n" + "  ),\n" + "  CALL(aaa))\n" + " ),\n" + " CALL(aa))\n" + "), \n" + "CALL(a))\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        final Exception[] exception = new Exception[1];
        final int[] suspendHits = new int[1];
        final int[] firstStatementNumJavaFrames = new int[1];
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("STATEMENT()", currentFrame.getSourceSection().getCharacters());
                event.prepareStepInto(2);
                suspendHits[0]++;
                firstStatementNumJavaFrames[0] = Thread.currentThread().getStackTrace().length;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("STATEMENT(EXPRESSION, EXPRESSION)", currentFrame.getSourceSection().getCharacters());
                Iterator<DebugStackFrame> sfIter = event.getStackFrames().iterator();
                // the top one (aaa)
                sfIter.next();
                // the second one (aa)
                event.prepareUnwindFrame(sfIter.next());
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("CALL(aa)", currentFrame.getSourceSection().getCharacters());
                // "a"
                event.prepareUnwindFrame(event.getStackFrames().iterator().next());
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("CALL(a)", currentFrame.getSourceSection().getCharacters());
                event.prepareStepInto(2);
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("STATEMENT(EXPRESSION)", currentFrame.getSourceSection().getCharacters());
                event.prepareStepInto(1);
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("STATEMENT(EXPRESSION, EXPRESSION)", currentFrame.getSourceSection().getCharacters());
                Iterator<DebugStackFrame> sfIter = event.getStackFrames().iterator();
                // the top one (aaa)
                sfIter.next();
                // the second one (aa)
                sfIter.next();
                // the third one (a)
                event.prepareUnwindFrame(sfIter.next());
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("CALL(a)", currentFrame.getSourceSection().getCharacters());
                event.prepareStepInto(1);
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("STATEMENT()", currentFrame.getSourceSection().getCharacters());
                Assert.assertEquals("Same Java depth", firstStatementNumJavaFrames[0], Thread.currentThread().getStackTrace().length);
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectDone();
        if (exception[0] != null) {
            throw exception[0];
        }
        Assert.assertEquals(8, suspendHits[0]);
    }
}
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) Test(org.junit.Test)

Example 3 with SuspendedEvent

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

the class ReenterStackFrameTest method testVariables.

@Test
public void testVariables() throws Throwable {
    // Test that after a re-enter, variables are cleared.
    final Source source = testSource("ROOT(DEFINE(a, ROOT(\n" + "  STATEMENT(),\n" + "  VARIABLE(x, 42),\n" + "  VARIABLE(n, 100),\n" + "  VARIABLE(m, 200),\n" + "  STATEMENT()\n" + ")),\n" + "CALL(a))\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        final Exception[] exception = new Exception[1];
        final int[] suspendHits = new int[1];
        final int numRepeats = 5;
        for (int i = numRepeats - 1; i >= 0; i--) {
            expectSuspended((SuspendedEvent event) -> {
                DebugStackFrame currentFrame = event.getTopStackFrame();
                try {
                    Assert.assertEquals("STATEMENT()", event.getSourceSection().getCharacters());
                    Assert.assertEquals(2, event.getSourceSection().getStartLine());
                    checkStack(currentFrame);
                    event.prepareStepOver(1);
                    suspendHits[0]++;
                } catch (Exception ex) {
                    exception[0] = ex;
                }
            });
            final boolean doUnwind = i > 0;
            expectSuspended((SuspendedEvent event) -> {
                DebugStackFrame currentFrame = event.getTopStackFrame();
                try {
                    Assert.assertEquals("STATEMENT()", event.getSourceSection().getCharacters());
                    Assert.assertEquals(6, event.getSourceSection().getStartLine());
                    checkStack(currentFrame, "n", "100", "m", "200", "x", "42");
                    if (doUnwind) {
                        event.prepareUnwindFrame(currentFrame);
                    }
                    suspendHits[0]++;
                } catch (Exception ex) {
                    exception[0] = ex;
                }
            });
            if (exception[0] != null) {
                throw exception[0];
            }
            if (!doUnwind) {
                break;
            }
            expectSuspended((SuspendedEvent event) -> {
                DebugStackFrame currentFrame = event.getTopStackFrame();
                try {
                    Assert.assertEquals("CALL(a)", currentFrame.getSourceSection().getCharacters());
                    Iterator<DebugStackFrame> frames = event.getStackFrames().iterator();
                    Assert.assertEquals("", frames.next().getName());
                    Assert.assertFalse(frames.hasNext());
                    checkStack(currentFrame);
                    // Enter into "a"
                    event.prepareStepInto(1);
                    suspendHits[0]++;
                } catch (Exception ex) {
                    exception[0] = ex;
                }
            });
            if (exception[0] != null) {
                throw exception[0];
            }
        }
        expectDone();
        if (exception[0] != null) {
            throw exception[0];
        }
        Assert.assertEquals(3 * numRepeats - 1, suspendHits[0]);
    }
}
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) Test(org.junit.Test)

Example 4 with SuspendedEvent

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

the class ReenterStackFrameTest method testReenterCurrent.

@Test
public void testReenterCurrent() throws Throwable {
    final Source source = testSource("ROOT(DEFINE(a, ROOT(\n" + "  STATEMENT(),\n" + "  STATEMENT(EXPRESSION)\n" + ")),\n" + "CALL(a))\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        final Exception[] exception = new Exception[1];
        final int[] suspendHits = new int[1];
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("STATEMENT()", currentFrame.getSourceSection().getCharacters());
                event.prepareStepOver(1);
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("STATEMENT(EXPRESSION)", currentFrame.getSourceSection().getCharacters());
                event.prepareUnwindFrame(currentFrame);
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("CALL(a)", currentFrame.getSourceSection().getCharacters());
                Iterator<DebugStackFrame> frames = event.getStackFrames().iterator();
                Assert.assertEquals("", frames.next().getName());
                Assert.assertFalse(frames.hasNext());
                // Enter into "a"
                event.prepareStepInto(1);
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame currentFrame = event.getTopStackFrame();
            try {
                Assert.assertEquals("STATEMENT()", currentFrame.getSourceSection().getCharacters());
                suspendHits[0]++;
            } catch (Exception ex) {
                exception[0] = ex;
            }
        });
        expectDone();
        if (exception[0] != null) {
            throw exception[0];
        }
        Assert.assertEquals(4, suspendHits[0]);
    }
}
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) Test(org.junit.Test)

Example 5 with SuspendedEvent

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

the class StepTest method testMultipleActions.

@Test
public void testMultipleActions() throws Throwable {
    final Source source = testSource(// 1
    "ROOT(\n" + "  DEFINE(bar, STATEMENT),\n" + "  DEFINE(foo, ROOT(STATEMENT(CALL(bar)), \n" + "                   STATEMENT(CALL(loop)))),\n" + // 5
    "  DEFINE(loop,\n" + "    LOOP(3,\n" + "      STATEMENT),\n" + "    STATEMENT\n" + "  ),\n" + // 10
    "  STATEMENT(CALL(foo)),\n" + "  STATEMENT(CALL(foo)),\n" + "  STATEMENT,\n" + "  STATEMENT(CALL(loop)),\n" + "  STATEMENT,\n" + // 15
    "  STATEMENT,\n" + "  STATEMENT(CALL(loop)),\n" + "  STATEMENT\n" + ")\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        Breakpoint bp14 = Breakpoint.newBuilder(getSourceImpl(source)).lineIs(14).build();
        Breakpoint bp17 = Breakpoint.newBuilder(getSourceImpl(source)).lineIs(17).build();
        session.install(bp14);
        session.install(bp17);
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 10, true, "STATEMENT(CALL(foo))").prepareStepInto(1).prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT").prepareStepOut(1).prepareStepInto(2).prepareStepOver(3);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 8, true, "STATEMENT").prepareStepOut(2).prepareStepInto(3);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT").prepareStepOver(1).prepareStepInto(2);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 7, true, "STATEMENT").prepareStepOver(3);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 8, true, "STATEMENT").prepareStepOut(2).prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 12, true, "STATEMENT").prepareStepOver(1).prepareContinue();
        });
        // Breakpoint is hit
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 14, true, "STATEMENT").prepareStepInto(5).prepareKill();
        });
        // Breakpoint on line 17 not hit because of the kill
        expectKilled();
        Assert.assertEquals(1, bp14.getHitCount());
        Assert.assertEquals(0, bp17.getHitCount());
    }
}
Also used : 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) Test(org.junit.Test)

Aggregations

SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)100 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)97 Source (org.graalvm.polyglot.Source)95 Test (org.junit.Test)93 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)45 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)19 DebugValue (com.oracle.truffle.api.debug.DebugValue)14 Debugger (com.oracle.truffle.api.debug.Debugger)11 SuspendedCallback (com.oracle.truffle.api.debug.SuspendedCallback)9 Context (org.graalvm.polyglot.Context)9 SourceSection (com.oracle.truffle.api.source.SourceSection)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 DebugScope (com.oracle.truffle.api.debug.DebugScope)5 HashMap (java.util.HashMap)3 DebugContext (com.oracle.truffle.api.debug.DebugContext)2 Map (java.util.Map)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Matcher (java.util.regex.Matcher)2