Search in sources :

Example 16 with Debugger

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

the class DebuggerThreadsTest method testContextThreads.

@Test
public void testContextThreads() throws Exception {
    Engine engine = Engine.create();
    final Source source = testSource("STATEMENT()");
    TestThreadsListener threadsListener = new TestThreadsListener();
    List<ThreadEvent> events = threadsListener.events;
    int numThreads = 10;
    Thread[] threads = new Thread[numThreads];
    Debugger debugger = engine.getInstruments().get("debugger").lookup(Debugger.class);
    try (DebuggerSession session = debugger.startSession(null)) {
        session.setThreadsListener(threadsListener, false);
        for (int i = 0; i < numThreads; i++) {
            threads[i] = new Thread() {

                @Override
                public void run() {
                    try (Context context = Context.newBuilder().engine(engine).build()) {
                        context.eval(source);
                    }
                }
            };
        }
        for (int i = 0; i < numThreads; i++) {
            threads[i].start();
        }
        for (int i = 0; i < numThreads; i++) {
            threads[i].join();
        }
    }
    assertEquals(2 * numThreads, events.size());
    List<ThreadEvent> startEvents = new ArrayList<>(numThreads);
    for (ThreadEvent event : events) {
        if (event.isNew) {
            startEvents.add(event);
        }
    }
    assertEquals(numThreads, startEvents.size());
    // Verify that we got all threads in the events:
    Set<Thread> allThreads = new HashSet<>(Arrays.asList(threads));
    for (int i = 0; i < numThreads; i++) {
        assertTrue(allThreads.remove(startEvents.get(i).thread));
    }
    assertTrue(allThreads.toString(), allThreads.isEmpty());
    // Verify that we got 'numThreads' number of contexts:
    Set<DebugContext> distinctContexts = new HashSet<>();
    for (ThreadEvent event : events) {
        distinctContexts.add(event.context);
    }
    assertEquals(numThreads, distinctContexts.size());
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebugContext(com.oracle.truffle.api.debug.DebugContext) Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) DebugContext(com.oracle.truffle.api.debug.DebugContext) Source(org.graalvm.polyglot.Source) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) Engine(org.graalvm.polyglot.Engine) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with Debugger

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

the class DebuggerThreadsTest method testGetStartedThreads.

@Test
public void testGetStartedThreads() throws Exception {
    Engine engine = Engine.create();
    TestThreadsListener threadsListener = new TestThreadsListener();
    List<ThreadEvent> events = threadsListener.events;
    Debugger debugger = engine.getInstruments().get("debugger").lookup(Debugger.class);
    CountDownLatch latch = new CountDownLatch(1);
    DebuggerSession[] sessionPtr = new DebuggerSession[1];
    try (DebuggerSession session = debugger.startSession(new SuspendedCallback() {

        @Override
        public void onSuspend(SuspendedEvent event) {
            latch.countDown();
            sessionPtr[0].setThreadsListener(threadsListener, true);
            assertEquals(events.toString(), 2, events.size());
            assertTrue(events.get(0).isNew);
            assertNotNull(events.get(0).context);
            assertTrue(events.get(1).isNew);
            assertEquals(events.get(0).context, events.get(1).context);
            assertNotEquals(events.get(0).thread, events.get(1).thread);
        }
    })) {
        session.suspendNextExecution();
        sessionPtr[0] = session;
        try (Context context = Context.newBuilder().engine(engine).allowCreateThread(true).build()) {
            Source source = testSource("ROOT(DEFINE(f1, EXPRESSION), SPAWN(f1), JOIN())");
            context.eval(source);
            source = testSource("ROOT(DEFINE(foo, STATEMENT), SPAWN(foo))");
            context.eval(source);
            latch.await();
            context.eval(testSource("JOIN()"));
            assertEquals(events.toString(), 3, events.size());
            assertFalse(events.get(2).isNew);
            assertTrue(events.get(0).thread == events.get(2).thread || events.get(1).thread == events.get(2).thread);
        }
        assertEquals(4, events.size());
    }
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebugContext(com.oracle.truffle.api.debug.DebugContext) Context(org.graalvm.polyglot.Context) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Source(org.graalvm.polyglot.Source) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) Engine(org.graalvm.polyglot.Engine) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 18 with Debugger

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

the class DebugSpeedBench method beforeTesting.

@Setup
public void beforeTesting() throws IOException {
    source = Source.newBuilder("instrumentation-test-language", CODE_STEP, "StepTest.instr").build();
    context = Context.create();
    Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
    session = debugger.startSession(this);
    session.suspendNextExecution();
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) Setup(org.openjdk.jmh.annotations.Setup)

Example 19 with Debugger

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

the class BreakpointTest method testGlobalBreakpoints.

@Test
// auto-closeable resource session is never referenced in body of
@SuppressWarnings("try")
public // corresponding try statement
void testGlobalBreakpoints() 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" + ")\n");
    Debugger debugger = getDebugger();
    assertTrue(debugger.getBreakpoints().isEmpty());
    Breakpoint globalBreakpoint = Breakpoint.newBuilder(getSourceImpl(source)).lineIs(2).build();
    boolean[] notified = new boolean[] { false };
    BreakpointListener newBPListener = BreakpointListener.register(notified, debugger, globalBreakpoint);
    debugger.install(globalBreakpoint);
    Assert.assertTrue(notified[0]);
    Assert.assertEquals(1, debugger.getBreakpoints().size());
    Breakpoint newBP = debugger.getBreakpoints().get(0);
    Assert.assertTrue(globalBreakpoint.isModifiable());
    Assert.assertFalse(newBP.isModifiable());
    try {
        newBP.dispose();
        Assert.fail("Public dispose must not be possible for global breakpoints.");
    } catch (IllegalStateException ex) {
    // O.K.
    }
    try {
        newBP.setCondition("Something");
        Assert.fail();
    } catch (IllegalStateException ex) {
    // O.K.
    }
    try {
        newBP.setCondition(null);
        Assert.fail();
    } catch (IllegalStateException ex) {
    // O.K.
    }
    try {
        newBP.setEnabled(false);
        Assert.fail();
    } catch (IllegalStateException ex) {
    // O.K.
    }
    try {
        newBP.setIgnoreCount(10);
        Assert.fail();
    } catch (IllegalStateException ex) {
    // O.K.
    }
    Assert.assertNull(newBP.getCondition());
    Assert.assertEquals(0, newBP.getHitCount());
    Assert.assertTrue(newBP.isEnabled());
    Assert.assertFalse(newBP.isDisposed());
    Assert.assertFalse(newBP.isResolved());
    try (DebuggerSession session = startSession()) {
        // global breakpoint is not among session breakpoints
        Assert.assertTrue(session.getBreakpoints().isEmpty());
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT");
            assertEquals(1, event.getBreakpoints().size());
            Breakpoint eventBP = event.getBreakpoints().get(0);
            try {
                eventBP.dispose();
                Assert.fail("Public dispose must not be possible for global breakpoints.");
            } catch (IllegalStateException ex) {
            // O.K.
            }
            Assert.assertTrue(eventBP.isResolved());
            event.prepareContinue();
        });
    }
    assertFalse(newBP.isDisposed());
    assertFalse(newBP.isResolved());
    expectDone();
    try (DebuggerSession session = startSession()) {
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT");
            event.prepareContinue();
        });
    }
    expectDone();
    newBPListener.unregister();
    notified[0] = false;
    BreakpointDisposeListener.register(notified, debugger, globalBreakpoint);
    globalBreakpoint.dispose();
    Assert.assertTrue(notified[0]);
    Assert.assertEquals(0, debugger.getBreakpoints().size());
}
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 20 with Debugger

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

the class TruffleDebugger method doEnable.

private void doEnable() {
    Debugger tdbg = context.getEnv().lookup(context.getEnv().getInstruments().get("debugger"), Debugger.class);
    ds = tdbg.startSession(new SuspendedCallbackImpl());
    ds.setSteppingFilter(SuspensionFilter.newBuilder().ignoreLanguageContextInitialization(true).includeInternal(false).build());
    slh = context.getScriptsHandler();
    bph = new BreakpointsHandler(ds, slh, () -> eventHandler);
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger)

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