Search in sources :

Example 11 with Debugger

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

the class ConcurrentDebuggingTest method testConcurrentBreakpoints.

@Test
public void testConcurrentBreakpoints() {
    int numThreads = 100;
    String code = "ROOT(DEFINE(foo,\n" + "  STATEMENT\n" + "),\n" + "LOOP(" + numThreads + ", SPAWN(foo)),\n" + "JOIN())";
    final Source source = Source.create(InstrumentationTestLanguage.ID, code);
    Breakpoint breakpoint = Breakpoint.newBuilder(source.getURI()).lineIs(2).build();
    Context context = Context.newBuilder().allowCreateThread(true).build();
    Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
    AtomicInteger hits = new AtomicInteger(0);
    try (DebuggerSession session = debugger.startSession((SuspendedEvent event) -> {
        assertEquals(1, event.getBreakpoints().size());
        assertEquals(breakpoint, event.getBreakpoints().get(0));
        hits.incrementAndGet();
    })) {
        session.install(breakpoint);
        context.eval(source);
    }
    assertEquals(numThreads, hits.get());
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 12 with Debugger

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

the class DebugScopeTest method testArguments.

@Test
public void testArguments() {
    final Source source = testSource("DEFINE(function, ROOT(\n" + "  STATEMENT()\n" + "))\n");
    Context context = Context.create();
    context.eval(source);
    Value functionValue = context.getBindings(InstrumentationTestLanguage.ID).getMember("function");
    assertNotNull(functionValue);
    Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
    boolean[] suspended = new boolean[] { false };
    DebuggerSession session = debugger.startSession((SuspendedEvent event) -> {
        assertFalse(suspended[0]);
        Iterable<DebugValue> arguments = event.getTopStackFrame().getScope().getArguments();
        assertNotNull(arguments);
        Iterator<DebugValue> iterator = arguments.iterator();
        assertTrue(iterator.hasNext());
        DebugValue arg = iterator.next();
        assertEquals("0", arg.getName());
        assertEquals("true", arg.as(String.class));
        assertTrue(iterator.hasNext());
        arg = iterator.next();
        assertEquals("1", arg.getName());
        assertEquals("10", arg.as(String.class));
        assertFalse(iterator.hasNext());
        event.prepareContinue();
        suspended[0] = true;
    });
    session.suspendNextExecution();
    functionValue.execute(true, 10);
    session.close();
    assertTrue(suspended[0]);
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Value(org.graalvm.polyglot.Value) DebugValue(com.oracle.truffle.api.debug.DebugValue) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 13 with Debugger

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

the class DebugValueTest method testValueAttributes.

/**
 * Test of {@link DebugValue#isReadable()}, {@link DebugValue#isWritable()} and
 * {@link DebugValue#isInternal()}.
 */
@Test
public void testValueAttributes() throws Throwable {
    final Source source = testSource("DEFINE(function, ROOT(\n" + "  ARGUMENT(a), \n" + "  STATEMENT()\n" + "))\n");
    Context context = Context.create();
    context.eval(source);
    Value functionValue = context.getBindings(InstrumentationTestLanguage.ID).getMember("function");
    assertNotNull(functionValue);
    Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
    // Test of the default attribute values:
    NoAttributesTruffleObject nao = new NoAttributesTruffleObject();
    boolean[] suspended = new boolean[] { false };
    DebuggerSession session = debugger.startSession((SuspendedEvent event) -> {
        assertFalse(suspended[0]);
        DebugValue value = event.getTopStackFrame().getScope().getDeclaredValue("a");
        assertNotNull(value);
        DebugValue attributesTOValue = value.getProperties().iterator().next();
        assertEquals("property", attributesTOValue.getName());
        // Property is readable by default
        assertTrue(attributesTOValue.isReadable());
        // Property is writable by default
        assertTrue(attributesTOValue.isWritable());
        // Property is not internal by default
        assertFalse(attributesTOValue.isInternal());
        event.prepareContinue();
        suspended[0] = true;
    });
    session.install(Breakpoint.newBuilder(getSourceImpl(source)).lineIs(3).build());
    functionValue.execute(nao);
    session.close();
    assertTrue(suspended[0]);
    // Test of the modified attribute values:
    suspended[0] = false;
    final ModifiableAttributesTruffleObject mao = new ModifiableAttributesTruffleObject();
    session = debugger.startSession((SuspendedEvent event) -> {
        assertFalse(suspended[0]);
        DebugValue value = event.getTopStackFrame().getScope().getDeclaredValue("a");
        assertNotNull(value);
        DebugValue attributesTOValue = value.getProperties().iterator().next();
        assertEquals("property", attributesTOValue.getName());
        // All false initially
        assertFalse(attributesTOValue.isReadable());
        assertFalse(attributesTOValue.isWritable());
        assertFalse(attributesTOValue.isInternal());
        mao.setIsReadable(true);
        attributesTOValue = value.getProperties().iterator().next();
        assertTrue(attributesTOValue.isReadable());
        mao.setIsWritable(true);
        attributesTOValue = value.getProperties().iterator().next();
        assertTrue(attributesTOValue.isWritable());
        mao.setIsInternal(true);
        attributesTOValue = value.getProperties().iterator().next();
        assertTrue(attributesTOValue.isInternal());
        event.prepareContinue();
        suspended[0] = true;
    });
    session.install(Breakpoint.newBuilder(getSourceImpl(source)).lineIs(3).build());
    functionValue.execute(mao);
    session.close();
    assertTrue(suspended[0]);
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Value(org.graalvm.polyglot.Value) DebugValue(com.oracle.truffle.api.debug.DebugValue) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 14 with Debugger

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

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

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