Search in sources :

Example 1 with SuspendedCallback

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

the class NestedContextTest method testRecursiveEval.

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

        public void onSuspend(SuspendedEvent event) {
            checkState(event, 3, true, "STATEMENT");
            // recursive evaluation should not trigger a suspended event
            context.eval(testSource);
            suspensionCount.incrementAndGet();
        }
    })) {
        session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
        context.eval(testSource);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 2 with SuspendedCallback

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

the class TimeBoxingTest method testTimeBoxing.

@Test
public void testTimeBoxing() throws Exception {
    final Context context = Context.create();
    Source source = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(LOOP(infinity,STATEMENT))", "NotEnoughTime").buildLiteral();
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
            debugger.startSession(new SuspendedCallback() {

                public void onSuspend(SuspendedEvent event) {
                    event.prepareKill();
                }
            }).suspendNextExecution();
        }
    }, 1000);
    try {
        // throws KillException, wrapped by PolyglotException
        context.eval(source);
        Assert.fail();
    } catch (PolyglotException pex) {
        Assert.assertEquals("com.oracle.truffle.api.debug.KillException", pex.getMessage());
    }
}
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) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 3 with SuspendedCallback

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

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

the class TruffleTCK method testRootNodeName.

/**
 * @since 0.15
 */
@Test
public void testRootNodeName() throws Exception {
    final int[] haltCount = new int[1];
    final String name = applyNumbers();
    final String[] actualName = new String[1];
    final PolyglotEngine engine = prepareVM(PolyglotEngine.newBuilder());
    final PolyglotEngine.Value apply = engine.findGlobalSymbol(name);
    final int value = RANDOM.nextInt(100);
    final TruffleObject fn = JavaInterop.asTruffleFunction(ObjectBinaryOperation.class, new ConstantFunction(value));
    try (DebuggerSession session = Debugger.find(engine).startSession(new SuspendedCallback() {

        public void onSuspend(SuspendedEvent ev) {
            actualName[0] = ev.getTopStackFrame().getName();
            haltCount[0] = haltCount[0] + 1;
        }
    })) {
        session.suspendNextExecution();
        apply.execute(fn).as(Number.class);
    }
    assertEquals(1, haltCount[0]);
    assertEquals(name, actualName[0]);
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 5 with SuspendedCallback

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

the class DebuggerTesterSnippets method expectSuspended.

/**
 * Expects an suspended event and returns it for potential assertions. If the execution
 * completed or was killed instead then an assertion error is thrown. The returned suspended
 * event is only valid until on of {@link #expectKilled()},
 * {@link #expectSuspended(SuspendedCallback)} or {@link #expectDone()} is called again. Throws
 * an {@link IllegalStateException} if the tester is already closed.
 *
 * @param callback handler to be called when the execution is suspended
 * @since 0.16
 */
public void expectSuspended(SuspendedCallback callback) {
    if (closed) {
        throw new IllegalStateException("Already closed.");
    }
    SuspendedCallback previous = this.handler;
    this.handler = callback;
    notifyNextAction();
    Object event;
    try {
        event = takeEvent();
        String e = getErr();
        if (!e.isEmpty()) {
            throw new AssertionError("Error output is not empty: " + e);
        }
    } catch (InterruptedException e) {
        throw new AssertionError(e);
    }
    if (event instanceof ExecutingSource) {
        ExecutingSource s = (ExecutingSource) event;
        if (s.error != null) {
            throw new AssertionError("Error in eval", s.error);
        }
        throw new AssertionError("Expected suspended event got return value " + s.returnValue);
    } else if (event instanceof SuspendedEvent) {
        this.handler = previous;
    } else {
        if (event instanceof Error) {
            throw (Error) event;
        }
        if (event instanceof RuntimeException) {
            throw (RuntimeException) event;
        }
        throw new AssertionError("Got unknown event.", (event instanceof Throwable ? (Throwable) event : null));
    }
}
Also used : SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback)

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