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);
}
}
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());
}
}
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);
}
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]);
}
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));
}
}
Aggregations