Search in sources :

Example 46 with DebuggerSession

use of com.oracle.truffle.api.debug.DebuggerSession 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 47 with DebuggerSession

use of com.oracle.truffle.api.debug.DebuggerSession 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 48 with DebuggerSession

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

Example 49 with DebuggerSession

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

the class TruffleTCK method testRootNodeName.

/**
 * @since 0.15
 */
@Test
public void testRootNodeName() throws Exception {
    final int[] haltCount = new int[1];
    final String name = applyNumbers();
    final String[] actualName = new String[1];
    final PolyglotEngine engine = prepareVM(PolyglotEngine.newBuilder());
    final PolyglotEngine.Value apply = engine.findGlobalSymbol(name);
    final int value = RANDOM.nextInt(100);
    final TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
    try (DebuggerSession session = Debugger.find(engine).startSession(new SuspendedCallback() {

        public void onSuspend(SuspendedEvent ev) {
            actualName[0] = ev.getTopStackFrame().getName();
            haltCount[0] = haltCount[0] + 1;
        }
    })) {
        session.suspendNextExecution();
        apply.execute(fn).as(Number.class);
    }
    assertEquals(1, haltCount[0]);
    assertEquals(name, actualName[0]);
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 50 with DebuggerSession

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

the class DebuggerTesterSnippets method assertColumnBreakpointsResolution.

/**
 * Utility method that checks proper resolution of column breakpoints. Breakpoints are submitted
 * to marked positions and their resolution location is checked.
 * <p>
 * The source need to contain both breakpoint submission marks in the form of "B&lt;number&gt;_"
 * and breakpoint resolution marks in the form of "R&lt;number&gt;_" where &lt;number&gt; is an
 * identification of the breakpoint. These marks need to be placed at the proper breakpoint
 * submission/resolution line/column position. When several submitted breakpoints resolve to the
 * same position, an interval can be specified in the form of "R&lt;start number&gt;-&lt;end
 * number&gt;_", 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>
 * // B1_test
 * function B2_test(n) {B3_
 *   if (R1-4_n &lt;= B4_1) {B5_
 *     return R5_2 * n;
 *   }
 *   return R6_n - 1;
 * B6_}
 * </pre>
 *
 * @param sourceWithMarks a source text, which contains the resolution marks
 * @param breakpointMarkName the breakpoint submission mark name. It is used in a regular
 *            expression, therefore the mark must not have a special meaning in a regular
 *            expression.
 * @param resolvedMarkName the resolved 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 assertColumnBreakpointsResolution(String sourceWithMarks, String breakpointMarkName, String resolvedMarkName, String language) throws IOException {
    Pattern br = Pattern.compile("([" + breakpointMarkName + resolvedMarkName + "]\\d+_|" + resolvedMarkName + "\\d+-\\d+_)");
    Map<Integer, int[]> bps = new HashMap<>();
    String sourceString = sourceWithMarks;
    Matcher bm = br.matcher(sourceString);
    while (bm.find()) {
        String bg = bm.group();
        int index = bm.start();
        int state = (bg.charAt(0) == 'B') ? 0 : 1;
        String bpNums = bg.substring(1, bg.length() - 1);
        int bn1;
        int bn2;
        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++) {
            int[] bp = bps.get(bn);
            if (bp == null) {
                bp = new int[2];
                bps.put(bn, bp);
            }
            if (bp[state] > 0) {
                Assert.fail(bg + " specified more than once.");
            }
            bp[state] = index + 1;
        }
        sourceString = bm.replaceFirst("");
        bm.reset(sourceString);
    }
    if (TRACE) {
        trace("sourceString = '" + sourceString + "'");
    }
    final Source source = Source.newBuilder(language, sourceString, "testMisplacedColumnBreakpoint." + language).build();
    for (Map.Entry<Integer, int[]> bentry : bps.entrySet()) {
        int bpId = bentry.getKey();
        int[] bp = bentry.getValue();
        Assert.assertTrue(Integer.toString(bpId), bp[0] > 0);
        Assert.assertTrue(Integer.toString(bpId), bp[1] > 0);
        int line = source.getLineNumber(bp[0] - 1);
        int column = source.getColumnNumber(bp[0] - 1);
        if (TRACE) {
            trace("TESTING BP_" + bpId + ": " + bp[0] + " (" + line + ":" + column + ") => " + bp[1] + ":");
        }
        try (DebuggerSession session = startSession()) {
            startEval(source);
            int[] resolvedIndexPtr = new int[] { 0 };
            Breakpoint breakpoint = session.install(Breakpoint.newBuilder(DebuggerTester.getSourceImpl(source)).lineIs(line).columnIs(column).oneShot().resolveListener(new Breakpoint.ResolveListener() {

                @Override
                public void breakpointResolved(Breakpoint brkp, SourceSection section) {
                    resolvedIndexPtr[0] = section.getCharIndex() + 1;
                    if (TRACE) {
                        trace("  resolved: " + (resolvedIndexPtr[0]));
                    }
                }
            }).build());
            expectSuspended((SuspendedEvent event) -> {
                Assert.assertEquals("Expected " + bp[0] + " => " + bp[1] + ", resolved at " + resolvedIndexPtr[0], bp[1], event.getSourceSection().getCharIndex() + 1);
                Assert.assertSame(breakpoint, event.getBreakpoints().iterator().next());
                event.prepareContinue();
            });
            expectDone();
            Assert.assertEquals("Expected resolved " + bp[0] + " => " + bp[1], bp[1], 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)

Aggregations

DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)110 Source (org.graalvm.polyglot.Source)103 Test (org.junit.Test)102 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)97 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)49 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)20 DebugValue (com.oracle.truffle.api.debug.DebugValue)16 Debugger (com.oracle.truffle.api.debug.Debugger)14 Context (org.graalvm.polyglot.Context)11 SourceSection (com.oracle.truffle.api.source.SourceSection)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 DebugContext (com.oracle.truffle.api.debug.DebugContext)6 SuspendedCallback (com.oracle.truffle.api.debug.SuspendedCallback)6 DebugScope (com.oracle.truffle.api.debug.DebugScope)5 Engine (org.graalvm.polyglot.Engine)5 HashMap (java.util.HashMap)3 Value (org.graalvm.polyglot.Value)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2