Search in sources :

Example 41 with SuspendedEvent

use of com.oracle.truffle.api.debug.SuspendedEvent 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)

Example 42 with SuspendedEvent

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

the class SLDebugTest method testNull.

@Test
public void testNull() throws Throwable {
    final Source factorial = slCode("function main() {\n" + "  res = doNull();\n" + "  return res;\n" + "}\n" + "function doNull() {}\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(factorial);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "main", 2, true, "res = doNull()").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "main", 3, true, "return res", "res", "NULL").prepareContinue();
        });
        assertEquals("NULL", 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 43 with SuspendedEvent

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

the class SLDebugTest method testTimeboxing.

@Test(expected = PolyglotException.class)
public void testTimeboxing() throws Throwable {
    final Source endlessLoop = slCode("function main() {\n" + "  i = 1; \n" + "  while(i > 0) {\n" + "    i = i + 1;\n" + "  }\n" + "  return i; \n" + "}\n");
    final Context context = Context.create("sl");
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            context.getEngine().getInstruments().get("debugger").lookup(Debugger.class).startSession(new SuspendedCallback() {

                public void onSuspend(SuspendedEvent event) {
                    event.prepareKill();
                }
            }).suspendNextExecution();
        }
    }, 1000);
    context.eval(endlessLoop);
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) Timer(java.util.Timer) TimerTask(java.util.TimerTask) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 44 with SuspendedEvent

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

the class SLDebugTest method checkExpressionStepPositions.

private void checkExpressionStepPositions(String stepPositions, boolean includeStatements, StepDepth... steps) {
    Source source = slCode("function main() {\n" + "  x = 2;\n" + "  while (x >= 0 && 5 >= 0) {\n" + "    a = 2 * x;\n" + "    b = (a * a) / (x * x + 1);\n" + "    x = x - transform(a, b);\n" + "  }\n" + "  return x / 1;\n" + "}\n" + "function transform(a, b) {\n" + "  return (1 + 1) * (a + b);\n" + "}\n");
    SourceElement[] elements;
    if (includeStatements) {
        elements = new SourceElement[] { SourceElement.EXPRESSION, SourceElement.STATEMENT };
    } else {
        elements = new SourceElement[] { SourceElement.EXPRESSION };
    }
    try (DebuggerSession session = startSession(elements)) {
        session.suspendNextExecution();
        startEval(source);
        // Step through the program
        StepDepth lastStep = steps[0];
        int stepIndex = 0;
        StepConfig expressionStepConfig = StepConfig.newBuilder().sourceElements(elements).build();
        for (String stepPos : stepPositions.split("\n")) {
            if (stepIndex < steps.length) {
                lastStep = steps[stepIndex++];
            }
            final StepDepth stepDepth = lastStep;
            expectSuspended((SuspendedEvent event) -> {
                if (!includeStatements) {
                    assertTrue("Needs to be an expression", event.hasSourceElement(SourceElement.EXPRESSION));
                } else {
                    assertTrue("Needs to be an expression or statement", event.hasSourceElement(SourceElement.EXPRESSION) || event.hasSourceElement(SourceElement.STATEMENT));
                }
                SourceSection ss = event.getSourceSection();
                DebugValue[] inputValues = event.getInputValues();
                String input = "";
                if (inputValues != null) {
                    StringBuilder inputBuilder = new StringBuilder("(");
                    for (DebugValue v : inputValues) {
                        if (inputBuilder.length() > 1) {
                            inputBuilder.append(',');
                        }
                        if (v != null) {
                            inputBuilder.append(v.as(String.class));
                        } else {
                            inputBuilder.append("null");
                        }
                    }
                    inputBuilder.append(") ");
                    input = inputBuilder.toString();
                }
                DebugValue returnValue = event.getReturnValue();
                String ret = (returnValue != null) ? returnValue.as(String.class) : "<none>";
                String actualPos = "<" + ss.getStartLine() + ":" + ss.getStartColumn() + " - " + ss.getEndLine() + ":" + ss.getEndColumn() + "> " + input + ret;
                assertEquals(stepPos, actualPos);
                switch(stepDepth) {
                    case INTO:
                        event.prepareStepInto(expressionStepConfig);
                        break;
                    case OVER:
                        event.prepareStepOver(expressionStepConfig);
                        break;
                    case OUT:
                        event.prepareStepOut(expressionStepConfig);
                        break;
                }
            });
        }
        expectDone();
    }
}
Also used : SourceElement(com.oracle.truffle.api.debug.SourceElement) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) StepConfig(com.oracle.truffle.api.debug.StepConfig) Source(org.graalvm.polyglot.Source) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SourceSection(com.oracle.truffle.api.source.SourceSection)

Example 45 with SuspendedEvent

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

the class SLDebugTest method testStepInOver.

@Test
public void testStepInOver() throws Throwable {
    /*
         * For recursive function we want to ensure that we don't step when we step over a function.
         */
    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()) {
        session.suspendNextExecution();
        startEval(factorial);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "main", 2, true, "return fac(5)").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "fac", 5, true, "n <= 1", "n", "5").prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "fac", 8, true, "return n * fac(n - 1)", "n", "5").prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, "main", 2, false, "fac(5)").prepareStepInto(1);
            assertEquals("120", event.getReturnValue().as(String.class));
        });
        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)

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