Search in sources :

Example 6 with Debugger

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

the class InstrumentLookupTest method testCanAccessDebugger.

@Test
@SuppressWarnings("deprecation")
public void testCanAccessDebugger() {
    com.oracle.truffle.api.vm.PolyglotRuntime runtime = com.oracle.truffle.api.vm.PolyglotRuntime.newBuilder().build();
    com.oracle.truffle.api.vm.PolyglotRuntime.Instrument debuggerInstrument = runtime.getInstruments().get("debugger");
    assertNotNull(debuggerInstrument);
    com.oracle.truffle.api.vm.PolyglotRuntime.Instrument accessDebuggerInstrument = runtime.getInstruments().get("testAccessInstrument");
    Debugger debugger = accessDebuggerInstrument.lookup(DebuggerProvider.class).getDebugger();
    assertNotNull(debugger);
    assertEquals(debugger, debuggerInstrument.lookup(Debugger.class));
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) Test(org.junit.Test)

Example 7 with Debugger

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

the class BreakpointTest method testGlobalBreakpointsInMultipleSessions.

@Test
public void testGlobalBreakpointsInMultipleSessions() throws Throwable {
    try {
        Class.forName("java.beans.PropertyChangeListener");
    } catch (ClassNotFoundException ex) {
        // skip the test if running only with java.base JDK9 module
        return;
    }
    final Source source = testSource("ROOT(\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT\n" + ")\n");
    Debugger debugger = getDebugger();
    Breakpoint globalBreakpoint1 = Breakpoint.newBuilder(getSourceImpl(source)).lineIs(2).build();
    debugger.install(globalBreakpoint1);
    Breakpoint globalBreakpoint2 = Breakpoint.newBuilder(getSourceImpl(source)).lineIs(4).build();
    debugger.install(globalBreakpoint2);
    Breakpoint globalBreakpoint3 = Breakpoint.newBuilder(getSourceImpl(source)).lineIs(6).build();
    debugger.install(globalBreakpoint3);
    Breakpoint globalBreakpoint4 = Breakpoint.newBuilder(getSourceImpl(source)).lineIs(8).build();
    debugger.install(globalBreakpoint4);
    // Breakpoints are in the install order:
    List<Breakpoint> breakpoints = debugger.getBreakpoints();
    Assert.assertEquals(4, breakpoints.size());
    Assert.assertTrue(breakpoints.get(0).getLocationDescription().contains("line=2"));
    Assert.assertTrue(breakpoints.get(1).getLocationDescription().contains("line=4"));
    Assert.assertTrue(breakpoints.get(2).getLocationDescription().contains("line=6"));
    DebuggerSession session1 = startSession();
    // global breakpoints are not among session breakpoints
    Assert.assertTrue(session1.getBreakpoints().isEmpty());
    try (DebuggerSession session2 = startSession()) {
        // global breakpoints are not among session breakpoints
        Assert.assertTrue(session2.getBreakpoints().isEmpty());
        startEval(source);
        // Both sessions should break here:
        expectSuspended((SuspendedEvent event) -> {
            assertSame(session1, event.getSession());
            checkState(event, 2, true, "STATEMENT").prepareContinue();
        });
        expectSuspended((SuspendedEvent event) -> {
            assertSame(session2, event.getSession());
            checkState(event, 2, true, "STATEMENT").prepareContinue();
        });
        // We close session2 after the next BP:
        expectSuspended((SuspendedEvent event) -> {
            assertSame(session1, event.getSession());
            checkState(event, 4, true, "STATEMENT");
        });
    }
    expectSuspended((SuspendedEvent event) -> {
        assertNotSame(session1, event.getSession());
        checkState(event, 4, true, "STATEMENT").prepareContinue();
    });
    // The last breakpoint is hit once only in the session1:
    expectSuspended((SuspendedEvent event) -> {
        assertSame(session1, event.getSession());
        checkState(event, 6, true, "STATEMENT").prepareStepOver(1);
    });
    expectSuspended((SuspendedEvent event) -> {
        assertSame(session1, event.getSession());
        checkState(event, 7, true, "STATEMENT");
        session1.close();
        event.prepareContinue();
    });
    // Breakpoint at line 8 was not hit at all, the session was closed right before continue
    // from line 7.
    expectDone();
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) 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) Test(org.junit.Test)

Example 8 with Debugger

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

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

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

the class DebugALot method onCreate.

@Override
protected void onCreate(Env env) {
    Boolean debugALot = env.getOptions().get(DebugALot);
    failFast = env.getOptions().get(FailFast);
    doEval = env.getOptions().get(Eval);
    boolean isLogFile = env.getOptions().hasBeenSet(LogFile);
    if (!(Boolean.TRUE.equals(debugALot) || failFast || doEval || isLogFile)) {
        return;
    }
    if (isLogFile) {
        String logFilePath = env.getOptions().get(LogFile);
        try {
            logger = new PrintWriter(new FileWriter(logFilePath));
        } catch (IOException ioex) {
            logger = new PrintWriter(env.out());
            logger.print(ioex.getLocalizedMessage());
        }
    } else {
        logger = new PrintWriter(env.out());
    }
    Debugger debugger = env.lookup(env.getInstruments().get("debugger"), Debugger.class);
    DebuggerSession debuggerSession = debugger.startSession(this);
    debuggerSession.suspendNextExecution();
    debuggerSession.setSteppingFilter(SuspensionFilter.newBuilder().ignoreLanguageContextInitialization(true).build());
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) FileWriter(java.io.FileWriter) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

Debugger (com.oracle.truffle.api.debug.Debugger)20 Test (org.junit.Test)17 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)14 Context (org.graalvm.polyglot.Context)13 Source (org.graalvm.polyglot.Source)13 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)11 SuspendedCallback (com.oracle.truffle.api.debug.SuspendedCallback)6 Engine (org.graalvm.polyglot.Engine)6 DebugContext (com.oracle.truffle.api.debug.DebugContext)5 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)4 DebugValue (com.oracle.truffle.api.debug.DebugValue)4 Value (org.graalvm.polyglot.Value)3 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1