Search in sources :

Example 6 with SuspendedCallback

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

the class DebuggerTesterSnippets method testDebugging.

// BEGIN: DebuggerTesterSnippets.testDebugging
public void testDebugging() {
    try (DebuggerTester tester = new DebuggerTester()) {
        // use your guest language source here
        Source source = null;
        try (DebuggerSession session = tester.startSession()) {
            session.suspendNextExecution();
            tester.startEval(source);
            tester.expectSuspended(new SuspendedCallback() {

                @Override
                public void onSuspend(SuspendedEvent event) {
                    // assert suspended event is proper here
                    event.prepareStepInto(1);
                }
            });
            tester.expectSuspended(new SuspendedCallback() {

                @Override
                public void onSuspend(SuspendedEvent event) {
                    // assert another suspended event is proper here
                    event.prepareContinue();
                }
            });
            // expect no more suspended events
            tester.expectDone();
        }
    }
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback)

Example 7 with SuspendedCallback

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

the class DebuggerContextsTest method testGetActiveContexts.

@Test
public void testGetActiveContexts() {
    final DebuggerSession[] sessionPtr = new DebuggerSession[1];
    try (Engine engine = Engine.create()) {
        TestContextsListener contextsListener = new TestContextsListener();
        List<ContextEvent> events = contextsListener.events;
        Debugger debugger = engine.getInstruments().get("debugger").lookup(Debugger.class);
        try (DebuggerSession session = debugger.startSession(new SuspendedCallback() {

            @Override
            public void onSuspend(SuspendedEvent event) {
                sessionPtr[0].setContextsListener(contextsListener, true);
                assertEquals(3, events.size());
                assertTrue(events.get(0).created);
                assertNull(events.get(0).language);
                assertTrue(events.get(1).created);
                assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
                assertTrue(events.get(2).languageInitialized);
            }
        })) {
            session.setContextsListener(contextsListener, true);
            try (Context context = Context.newBuilder().engine(engine).build()) {
                context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
                assertEquals(3, events.size());
                // Have creation of polyglot context and a language
                assertTrue(events.get(0).created);
                assertNotNull(events.get(0).context);
                assertNull(events.get(0).language);
                assertTrue(events.get(1).created);
                assertNotNull(events.get(1).language);
                assertTrue(events.get(2).languageInitialized);
            }
            assertEquals(6, events.size());
            session.setContextsListener(null, true);
            events.clear();
            // Contexts created and closed:
            try (Context context = Context.newBuilder().engine(engine).build()) {
                context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
                context.eval(Source.create(InstrumentationTestLanguage.ID, "CONTEXT(STATEMENT())"));
            }
            assertEquals(0, events.size());
            sessionPtr[0] = session;
            session.suspendNextExecution();
            Context context = Context.newBuilder().engine(engine).build();
            context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(CONTEXT(EXPRESSION()), CONTEXT(STATEMENT))"));
        }
    }
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebugContext(com.oracle.truffle.api.debug.DebugContext) Context(org.graalvm.polyglot.Context) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Engine(org.graalvm.polyglot.Engine) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 8 with SuspendedCallback

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

the class DebuggerSessionTest method testClosing2.

@Test
public void testClosing2() throws Exception {
    Source testSource = testSource("ROOT(\n" + "STATEMENT,\n" + "STATEMENT)");
    Context context = Context.create();
    final AtomicBoolean suspend = new AtomicBoolean();
    Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
    DebuggerSession session = debugger.startSession(new SuspendedCallback() {

        public void onSuspend(SuspendedEvent event) {
            suspend.set(true);
        }
    });
    context.eval(testSource);
    context.close();
    // if the engine disposes the session should still work
    session.suspendNextExecution();
    suspend(session, Thread.currentThread());
    suspendAll(session);
    session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(2).build());
    resume(session, Thread.currentThread());
    session.resumeAll();
    session.getDebugger();
    session.getBreakpoints();
    // after closing the session none of these methods should work
    session.close();
    try {
        session.suspendNextExecution();
        Assert.fail();
    } catch (IllegalStateException e) {
    }
    try {
        suspend(session, Thread.currentThread());
        Assert.fail();
    } catch (IllegalStateException e) {
    }
    try {
        suspendAll(session);
        Assert.fail();
    } catch (IllegalStateException e) {
    }
    try {
        session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(2).build());
        Assert.fail();
    } catch (IllegalStateException e) {
    }
    try {
        resume(session, Thread.currentThread());
        Assert.fail();
    } catch (IllegalStateException e) {
    }
    try {
        session.resumeAll();
        Assert.fail();
    } catch (IllegalStateException e) {
    }
    try {
        session.getBreakpoints();
        Assert.fail();
    } catch (IllegalStateException e) {
    }
    // still works after closing
    session.getDebugger();
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) 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 SuspendedCallback

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

Aggregations

SuspendedCallback (com.oracle.truffle.api.debug.SuspendedCallback)9 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)9 Test (org.junit.Test)7 Debugger (com.oracle.truffle.api.debug.Debugger)6 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)6 Context (org.graalvm.polyglot.Context)6 Source (org.graalvm.polyglot.Source)6 DebugContext (com.oracle.truffle.api.debug.DebugContext)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 Engine (org.graalvm.polyglot.Engine)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 PolyglotException (org.graalvm.polyglot.PolyglotException)1