Search in sources :

Example 56 with SuspendedEvent

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

the class SLDebugTest method testArgumentsAndValues.

@Test
public void testArgumentsAndValues() throws Throwable {
    // Test that after a re-enter, arguments are kept and variables are cleared.
    final Source source = slCode("function main() {\n" + "  i = 10;\n" + "  return fnc(i = i + 1, 20);\n" + "}\n" + "function fnc(n, m) {\n" + "  x = n + m;\n" + "  n = m - n;\n" + "  m = m / 2;\n" + "  x = x + n * m;\n" + "  return x;\n" + "}\n");
    try (DebuggerSession session = startSession()) {
        session.install(Breakpoint.newBuilder(getSourceImpl(source)).lineIs(6).build());
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            assertEquals(6, frame.getSourceSection().getStartLine());
            checkArgs(frame, "n", "11", "m", "20");
            checkStack(frame, "fnc", "n", "11", "m", "20");
            event.prepareStepOver(4);
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            assertEquals(10, frame.getSourceSection().getStartLine());
            checkArgs(frame, "n", "11", "m", "20");
            checkStack(frame, "fnc", "n", "9", "m", "10", "x", "121");
            event.prepareUnwindFrame(frame);
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            assertEquals(3, frame.getSourceSection().getStartLine());
            checkArgs(frame);
            checkStack(frame, "main", "i", "11");
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            assertEquals(6, frame.getSourceSection().getStartLine());
            checkArgs(frame, "n", "11", "m", "20");
            checkStack(frame, "fnc", "n", "11", "m", "20");
        });
        assertEquals("121", 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) Test(org.junit.Test)

Example 57 with SuspendedEvent

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

the class DebuggerTesterSnippets method assertLineBreakpointsResolution.

/**
 * Utility method that checks proper resolution of line breakpoints. Breakpoints are submitted
 * to every line of the source and their resolution location is checked.
 * <p>
 * The source need to contain resolution marks in the form of
 * "<code>R&lt;line number&gt;_</code>" where &lt;line number&gt; is line number of the original
 * breakpoint position and <code>R</code> is the resolved mark name. These marks need to be
 * placed at the proper breakpoint resolution line/column position, for a breakpoint submitted
 * to every line. When several submitted breakpoints resolve to the same position, an interval
 * can be specified in the form of
 * "<code>R&lt;line start number&gt;-&lt;line end number&gt;_</code>", where both start and end
 * line numbers are inclusive. The marks are stripped off before execution.
 * <p>
 * The guest language code with marks may look like:
 *
 * <pre>
 * // test
 * function test(n) {
 *   if (R1-3_n &lt;= 1) {
 *     return R4_2 * n;
 *   }
 *   return R5-7_n - 1;
 * }
 * </pre>
 *
 * @param sourceWithMarks a source text, which contains the resolution marks
 * @param resolvedMarkName the mark name. It is used in a regular expression, therefore the mark
 *            must not have a special meaning in a regular expression.
 * @param language the source language
 * @since 0.33
 */
public void assertLineBreakpointsResolution(String sourceWithMarks, String resolvedMarkName, String language) throws IOException {
    Pattern br = Pattern.compile("(" + resolvedMarkName + "\\d+_|" + resolvedMarkName + "\\d+-\\d+_)");
    Map<Integer, Integer> bps = new HashMap<>();
    String sourceString = sourceWithMarks;
    Matcher bm = br.matcher(sourceString);
    while (bm.find()) {
        String bg = bm.group();
        int index = bm.start();
        int bn1;
        int bn2;
        String bpNums = bg.substring(1, bg.length() - 1);
        int rangeIndex = bpNums.indexOf('-');
        if (rangeIndex > 0) {
            bn1 = Integer.parseInt(bpNums.substring(0, rangeIndex));
            bn2 = Integer.parseInt(bpNums.substring(rangeIndex + 1));
        } else {
            bn1 = bn2 = Integer.parseInt(bpNums);
        }
        for (int bn = bn1; bn <= bn2; bn++) {
            Integer bp = bps.get(bn);
            if (bp == null) {
                bps.put(bn, index + 1);
            } else {
                Assert.fail(bg + " specified more than once.");
            }
        }
        sourceString = bm.replaceFirst("");
        bm.reset(sourceString);
    }
    if (TRACE) {
        trace("sourceString = '" + sourceString + "'");
    }
    final Source source = Source.newBuilder(language, sourceString, "testMisplacedLineBreakpoint." + language).build();
    com.oracle.truffle.api.source.Source tsource = DebuggerTester.getSourceImpl(source);
    for (int l = 1; l < source.getLineCount(); l++) {
        if (!bps.containsKey(l)) {
            Assert.fail("Line " + l + " is missing.");
        }
    }
    for (Map.Entry<Integer, Integer> bentry : bps.entrySet()) {
        int line = bentry.getKey();
        int indexResolved = bentry.getValue();
        int lineResolved = source.getLineNumber(indexResolved - 1);
        if (TRACE) {
            trace("TESTING breakpoint '" + line + "' => " + lineResolved + ":");
        }
        try (DebuggerSession session = startSession()) {
            startEval(source);
            int[] resolvedIndexPtr = new int[] { 0 };
            Breakpoint breakpoint = session.install(Breakpoint.newBuilder(tsource).lineIs(line).oneShot().resolveListener(new Breakpoint.ResolveListener() {

                @Override
                public void breakpointResolved(Breakpoint brkp, SourceSection section) {
                    resolvedIndexPtr[0] = section.getCharIndex() + 1;
                    if (TRACE) {
                        trace("BREAKPOINT resolved to " + section.getStartLine() + ":" + section.getStartColumn());
                    }
                }
            }).build());
            expectSuspended((SuspendedEvent event) -> {
                Assert.assertEquals("Expected " + line + " => " + lineResolved, lineResolved, event.getSourceSection().getStartLine());
                Assert.assertSame(breakpoint, event.getBreakpoints().iterator().next());
                event.prepareContinue();
            });
            expectDone();
            Assert.assertEquals("Expected resolved " + line + " => " + indexResolved, indexResolved, resolvedIndexPtr[0]);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(org.graalvm.polyglot.Source) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SourceSection(com.oracle.truffle.api.source.SourceSection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 58 with SuspendedEvent

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

the class NestedContextTest method testNestedStepping.

private void testNestedStepping(int depth) {
    if (depth == 0) {
        return;
    }
    Source testSource = testSource("ROOT(\n" + "  STATEMENT,\n" + "  STATEMENT\n" + ")\n");
    pushContext();
    try (DebuggerSession session = startSession()) {
        Breakpoint breakpoint3 = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
        session.suspendNextExecution();
        startEval(testSource);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT");
            assertEquals(0, event.getBreakpoints().size());
            testNestedStepping(depth - 1);
            event.prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 3, true, "STATEMENT");
            assertEquals(1, event.getBreakpoints().size());
            assertSame(breakpoint3, event.getBreakpoints().iterator().next());
            testNestedStepping(depth - 1);
            event.prepareStepInto(1);
        });
        expectDone();
    }
    popContext();
}
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)

Example 59 with SuspendedEvent

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

the class StepTest method testStepInto.

@Test
public void testStepInto() throws Throwable {
    final Source source = testSource("ROOT(\n" + "  DEFINE(bar, STATEMENT),\n" + "  DEFINE(foo, ROOT(STATEMENT(CALL(bar)), \n" + "                   STATEMENT(CALL(bar)))),\n" + "  STATEMENT(CALL(foo)),\n" + "  STATEMENT(CALL(foo))\n" + ")\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 5, true, "STATEMENT(CALL(foo))").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 3, true, "STATEMENT(CALL(bar))").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 3, false, "CALL(bar)").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 4, true, "STATEMENT(CALL(bar))").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 4, false, "CALL(bar)").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 5, false, "CALL(foo)").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 6, true, "STATEMENT(CALL(foo))").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 60 with SuspendedEvent

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

the class StepTest method testStepBadArgStepOver.

@Test
public void testStepBadArgStepOver() throws Throwable {
    final Source source = testSource("ROOT(\n" + "  STATEMENT\n" + ")\n");
    try (DebuggerSession session = startSession()) {
        startEval(source);
        session.suspendNextExecution();
        expectSuspended((SuspendedEvent event) -> {
            try {
                checkState(event, 2, true, "STATEMENT").prepareStepOver(0);
                fail("Exception expected");
            } catch (IllegalArgumentException e) {
                Assert.assertEquals("Step count must be > 0", e.getMessage());
            }
        });
        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