Search in sources :

Example 16 with DebugStackFrame

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

the class SLDebugTest method testSourceLocation.

@Test
public void testSourceLocation() {
    final Source varsSource = slCode("function main() {\n" + "  a = doNull();\n" + "  c = 10;\n" + "  d = \"str\";\n" + "  e = new();\n" + "  f = doNull;\n" + "  return;\n" + "}\n" + "function doNull() {}\n");
    try (DebuggerSession session = startSession()) {
        session.install(Breakpoint.newBuilder(getSourceImpl(varsSource)).lineIs(7).build());
        startEval(varsSource);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            DebugScope scope = frame.getScope();
            DebugValue v = scope.getDeclaredValue("a");
            assertNull(v.getSourceLocation());
            v = scope.getDeclaredValue("c");
            assertNull(v.getSourceLocation());
            v = scope.getDeclaredValue("d");
            assertNull(v.getSourceLocation());
            v = scope.getDeclaredValue("e");
            assertNull(v.getSourceLocation());
            v = scope.getDeclaredValue("f");
            SourceSection sourceLocation = v.getSourceLocation();
            Assert.assertNotNull(sourceLocation);
            assertEquals(9, sourceLocation.getStartLine());
            assertEquals(9, sourceLocation.getEndLine());
            assertEquals("doNull() {}", sourceLocation.getCharacters());
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebugScope(com.oracle.truffle.api.debug.DebugScope) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 17 with DebugStackFrame

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

the class SLDebugTest method testStackInterop.

@Test
public void testStackInterop() {
    final Source stackSource = slCode("function fac(n, multiply) {\n" + "  if (n <= 1) {\n" + "    debugger;\n" + "    return 1;\n" + "  }\n" + "  return multiply.multiply(n, fac, n - 1);\n" + "}\n");
    Context context = Context.create("sl");
    context.eval(stackSource);
    Value fac = context.getBindings("sl").getMember("fac");
    Object multiply = new Multiply();
    Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
    boolean[] done = new boolean[1];
    try (DebuggerSession session = debugger.startSession((event) -> {
        Iterator<DebugStackFrame> sfIt = event.getStackFrames().iterator();
        assertTrue(sfIt.hasNext());
        DebugStackFrame dsf = sfIt.next();
        assertEquals("fac", dsf.getName());
        assertEquals(3, dsf.getSourceSection().getStartLine());
        assertFalse(dsf.isInternal());
        int numStacksAt6 = 10 - 1;
        int numInteropStacks = 0;
        for (int i = 0; i < numStacksAt6; ) {
            assertTrue(sfIt.hasNext());
            dsf = sfIt.next();
            boolean inFac = dsf.getName() != null && !dsf.isInternal();
            if (inFac) {
                // Frame in fac function
                assertEquals("fac", dsf.getName());
                assertEquals(6, dsf.getSourceSection().getStartLine());
                assertFalse(dsf.isInternal());
                i++;
            } else {
                // Frame in an interop method, internal
                assertNull(dsf.getSourceSection());
                assertTrue(dsf.isInternal());
                numInteropStacks++;
            }
        }
        // There were at least as many interop internal frames as frames in fac function:
        assertTrue("numInteropStacks = " + numInteropStacks, numInteropStacks >= numStacksAt6);
        // Some more internal frames remain
        while (sfIt.hasNext()) {
            dsf = sfIt.next();
            assertNull(dsf.getSourceSection());
            assertTrue(dsf.isInternal());
        }
        done[0] = true;
    })) {
        Assert.assertNotNull(session);
        Value ret = fac.execute(10, multiply);
        assertNumber(ret.asLong(), 3628800L);
    }
    assertTrue(done[0]);
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) Value(org.graalvm.polyglot.Value) DebugValue(com.oracle.truffle.api.debug.DebugValue) Source(org.graalvm.polyglot.Source) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Test(org.junit.Test)

Example 18 with DebugStackFrame

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

the class SLDebugTest method testValuesScope.

@Test
public void testValuesScope() throws Throwable {
    final Source varsSource = slCode("function main() {\n" + "  a = 1;\n" + "  if (a > 0) {\n" + "    b = 10;\n" + "    println(b);\n" + "  }\n" + "  println(b);\n" + "  println(a);\n" + "  println(\"END.\");\n" + "}");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(varsSource);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            // No variables first:
            assertFalse(frame.getScope().getDeclaredValues().iterator().hasNext());
            event.prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            // "a" only:
            DebugScope scope = frame.getScope();
            Iterator<DebugValue> varIt = scope.getDeclaredValues().iterator();
            assertTrue(varIt.hasNext());
            DebugValue a = varIt.next();
            assertEquals("a", a.getName());
            assertEquals(scope, a.getScope());
            assertFalse(varIt.hasNext());
            event.prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            // "a" only:
            DebugScope scope = frame.getScope();
            Iterator<DebugValue> varIt = scope.getParent().getDeclaredValues().iterator();
            assertTrue(varIt.hasNext());
            DebugValue a = varIt.next();
            assertEquals("a", a.getName());
            assertEquals(scope.getParent(), a.getScope());
            assertFalse(varIt.hasNext());
            event.prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            // "a" and "b":
            DebugScope scope = frame.getScope();
            Iterator<DebugValue> varIt = scope.getDeclaredValues().iterator();
            assertTrue(varIt.hasNext());
            DebugValue b = varIt.next();
            assertEquals("b", b.getName());
            assertEquals(scope, b.getScope());
            // "a" is in the parent:
            assertFalse(varIt.hasNext());
            varIt = scope.getParent().getDeclaredValues().iterator();
            assertTrue(varIt.hasNext());
            DebugValue a = varIt.next();
            assertEquals("a", a.getName());
            assertEquals(scope.getParent(), a.getScope());
            assertFalse(varIt.hasNext());
            event.prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            // "a" only again:
            DebugScope scope = frame.getScope();
            Iterator<DebugValue> varIt = scope.getDeclaredValues().iterator();
            assertTrue(varIt.hasNext());
            DebugValue a = varIt.next();
            assertEquals("a", a.getName());
            assertEquals(scope, a.getScope());
            assertFalse(varIt.hasNext());
            event.prepareContinue();
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebugScope(com.oracle.truffle.api.debug.DebugScope) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 19 with DebugStackFrame

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

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

the class DebugALot method logFrames.

private void logFrames(Iterable<DebugStackFrame> stackFrames) {
    logger.print("Stack: ");
    List<DebugStackFrame> frames = new ArrayList<>();
    for (DebugStackFrame frame : stackFrames) {
        frames.add(frame);
    }
    logger.print(frames.size());
    logger.println((frames.size() == 1) ? " frame" : " frames");
    for (int i = 0; i < frames.size(); i++) {
        logger.print(i + 1);
        logger.print(". ");
        int offset = Integer.toString(i + 1).length() + 2;
        String framePrefix = getPrefix(offset);
        logFrame(framePrefix, frames.get(i));
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) ArrayList(java.util.ArrayList)

Aggregations

DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)24 Source (org.graalvm.polyglot.Source)21 Test (org.junit.Test)21 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)20 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)19 DebugValue (com.oracle.truffle.api.debug.DebugValue)12 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)9 DebugScope (com.oracle.truffle.api.debug.DebugScope)7 SourceSection (com.oracle.truffle.api.source.SourceSection)5 ArrayList (java.util.ArrayList)2 Value (org.graalvm.polyglot.Value)2 TruffleException (com.oracle.truffle.api.TruffleException)1 Debugger (com.oracle.truffle.api.debug.Debugger)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 Source (com.oracle.truffle.api.source.Source)1 GuestLanguageException (com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.GuestLanguageException)1 NoSuspendedThreadException (com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.NoSuspendedThreadException)1 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)1 CallFrame (com.oracle.truffle.tools.chromeinspector.types.CallFrame)1