Search in sources :

Example 1 with DebugContext

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

the class DebuggerContextsTest method testMultipleContexts.

@Test
public void testMultipleContexts() {
    Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
    Engine engine = Engine.create();
    TestContextsListener contextsListener = new TestContextsListener();
    List<ContextEvent> events = contextsListener.events;
    int numContexts = 5;
    Debugger debugger = engine.getInstruments().get("debugger").lookup(Debugger.class);
    try (DebuggerSession session = debugger.startSession(null)) {
        session.setContextsListener(contextsListener, false);
        for (int i = 0; i < numContexts; i++) {
            try (Context context = Context.newBuilder().engine(engine).build()) {
                assertEquals(6 * i + 1, events.size());
                context.eval(source);
            }
            assertEquals(6 * i + 6, events.size());
        }
        assertEquals(6 * numContexts, events.size());
        DebugContext lastContext = null;
        for (int i = 0; i < numContexts; i++) {
            int ci = 6 * i;
            assertTrue(events.get(ci).created);
            assertNull(events.get(ci).language);
            assertNotEquals(lastContext, events.get(ci).context);
            lastContext = events.get(ci).context;
            assertTrue(events.get(ci + 1).created);
            assertNotNull(events.get(ci + 1).language);
            assertEquals(lastContext, events.get(ci + 1).context);
            assertTrue(events.get(ci + 2).languageInitialized);
            assertTrue(events.get(ci + 3).languageFinalized);
            assertNotNull(events.get(ci + 4).language);
            assertNull(events.get(ci + 5).language);
        }
        engine.close();
    }
    // No more events
    assertEquals(6 * numContexts, events.size());
}
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) DebugContext(com.oracle.truffle.api.debug.DebugContext) Source(org.graalvm.polyglot.Source) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 2 with DebugContext

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

the class DebuggerContextsTest method testInContextEval.

@Test
public void testInContextEval() {
    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(null)) {
            session.setContextsListener(contextsListener, false);
            Context context = Context.newBuilder().engine(engine).build();
            assert context != null;
            assertEquals(1, events.size());
            assertTrue(events.get(0).created);
            DebugContext dc = events.get(0).context;
            assertNotNull(dc);
            assertNull(events.get(0).language);
            // "Global" evaluation in the brand new context
            DebugValue result = dc.evaluate("VARIABLE(a, 10)", InstrumentationTestLanguage.ID);
            assertEquals(3, events.size());
            assertTrue(events.get(1).created);
            assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
            assertTrue(events.get(2).languageInitialized);
            String type = dc.runInContext(() -> {
                assertEquals("10", result.as(String.class));
                DebugValue metaObj = result.getMetaObject();
                return metaObj.as(String.class);
            });
            assertEquals("Integer", type);
            assertEquals(3, events.size());
        }
    }
}
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) DebugValue(com.oracle.truffle.api.debug.DebugValue) DebugContext(com.oracle.truffle.api.debug.DebugContext) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 3 with DebugContext

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

the class DebuggerThreadsTest method testSpawnThreads.

@Test
public void testSpawnThreads() {
    int numThreads = 10;
    final Source source = testSource("ROOT(DEFINE(foo, STATEMENT), LOOP(" + numThreads + ", SPAWN(foo)), JOIN())");
    TestThreadsListener threadsListener = new TestThreadsListener();
    List<ThreadEvent> events = threadsListener.events;
    try (DebuggerSession session = startSession()) {
        session.setThreadsListener(threadsListener, false);
        startEval(source);
        expectDone();
    }
    assertEquals(1 + 2 * numThreads, events.size());
    List<ThreadEvent> startEvents = new ArrayList<>(1 + numThreads);
    for (ThreadEvent event : events) {
        if (event.isNew) {
            startEvents.add(event);
        }
    }
    assertEquals(1 + numThreads, startEvents.size());
    DebugContext dcontext = events.get(0).context;
    for (ThreadEvent event : events) {
        assertEquals(dcontext, event.context);
    }
    events.clear();
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) ArrayList(java.util.ArrayList) DebugContext(com.oracle.truffle.api.debug.DebugContext) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 4 with DebugContext

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

Aggregations

DebugContext (com.oracle.truffle.api.debug.DebugContext)4 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)4 Test (org.junit.Test)4 Debugger (com.oracle.truffle.api.debug.Debugger)3 Context (org.graalvm.polyglot.Context)3 Engine (org.graalvm.polyglot.Engine)3 Source (org.graalvm.polyglot.Source)3 ArrayList (java.util.ArrayList)2 DebugValue (com.oracle.truffle.api.debug.DebugValue)1 HashSet (java.util.HashSet)1