Search in sources :

Example 1 with Source

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

the class CompilationOfProfilerNodesTest method testInvalidationsDontPoluteShadowStack.

@Test
@SuppressWarnings("try")
public void testInvalidationsDontPoluteShadowStack() {
    // Test assumes that foo will be inlined into bar
    try (TruffleCompilerOptions.TruffleOptionsOverrideScope inline = TruffleCompilerOptions.overrideOptions(TruffleCompilerOptions.TruffleFunctionInlining, true)) {
        sampler.setStackLimit(3);
        sampler.setCollecting(true);
        final Source source = Source.create(InstrumentationTestLanguage.ID, defaultSourceForSampling);
        context.eval(InstrumentationTestLanguage.ID, source.getCharacters());
        Assert.assertFalse(sampler.hasStackOverflowed());
    }
}
Also used : TruffleCompilerOptions(org.graalvm.compiler.truffle.common.TruffleCompilerOptions) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 2 with Source

use of org.graalvm.polyglot.Source 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 3 with Source

use of org.graalvm.polyglot.Source 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 4 with Source

use of org.graalvm.polyglot.Source 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 5 with Source

use of org.graalvm.polyglot.Source 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)

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