Search in sources :

Example 1 with StepConfig

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

Aggregations

Breakpoint (com.oracle.truffle.api.debug.Breakpoint)1 DebugValue (com.oracle.truffle.api.debug.DebugValue)1 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)1 SourceElement (com.oracle.truffle.api.debug.SourceElement)1 StepConfig (com.oracle.truffle.api.debug.StepConfig)1 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 Source (org.graalvm.polyglot.Source)1