use of com.oracle.truffle.api.debug.SuspendedEvent 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.SuspendedEvent in project graal by oracle.
the class DebuggerTesterSnippets method assertColumnBreakpointsResolution.
/**
* Utility method that checks proper resolution of column breakpoints. Breakpoints are submitted
* to marked positions and their resolution location is checked.
* <p>
* The source need to contain both breakpoint submission marks in the form of "B<number>_"
* and breakpoint resolution marks in the form of "R<number>_" where <number> is an
* identification of the breakpoint. These marks need to be placed at the proper breakpoint
* submission/resolution line/column position. When several submitted breakpoints resolve to the
* same position, an interval can be specified in the form of "R<start number>-<end
* number>_", where both start and end line numbers are inclusive. The marks are stripped off
* before execution.
* <p>
* The guest language code with marks may look like:
*
* <pre>
* // B1_test
* function B2_test(n) {B3_
* if (R1-4_n <= B4_1) {B5_
* return R5_2 * n;
* }
* return R6_n - 1;
* B6_}
* </pre>
*
* @param sourceWithMarks a source text, which contains the resolution marks
* @param breakpointMarkName the breakpoint submission mark name. It is used in a regular
* expression, therefore the mark must not have a special meaning in a regular
* expression.
* @param resolvedMarkName the resolved mark name. It is used in a regular expression, therefore
* the mark must not have a special meaning in a regular expression.
* @param language the source language
* @since 0.33
*/
public void assertColumnBreakpointsResolution(String sourceWithMarks, String breakpointMarkName, String resolvedMarkName, String language) throws IOException {
Pattern br = Pattern.compile("([" + breakpointMarkName + resolvedMarkName + "]\\d+_|" + resolvedMarkName + "\\d+-\\d+_)");
Map<Integer, int[]> bps = new HashMap<>();
String sourceString = sourceWithMarks;
Matcher bm = br.matcher(sourceString);
while (bm.find()) {
String bg = bm.group();
int index = bm.start();
int state = (bg.charAt(0) == 'B') ? 0 : 1;
String bpNums = bg.substring(1, bg.length() - 1);
int bn1;
int bn2;
int rangeIndex = bpNums.indexOf('-');
if (rangeIndex > 0) {
bn1 = Integer.parseInt(bpNums.substring(0, rangeIndex));
bn2 = Integer.parseInt(bpNums.substring(rangeIndex + 1));
} else {
bn1 = bn2 = Integer.parseInt(bpNums);
}
for (int bn = bn1; bn <= bn2; bn++) {
int[] bp = bps.get(bn);
if (bp == null) {
bp = new int[2];
bps.put(bn, bp);
}
if (bp[state] > 0) {
Assert.fail(bg + " specified more than once.");
}
bp[state] = index + 1;
}
sourceString = bm.replaceFirst("");
bm.reset(sourceString);
}
if (TRACE) {
trace("sourceString = '" + sourceString + "'");
}
final Source source = Source.newBuilder(language, sourceString, "testMisplacedColumnBreakpoint." + language).build();
for (Map.Entry<Integer, int[]> bentry : bps.entrySet()) {
int bpId = bentry.getKey();
int[] bp = bentry.getValue();
Assert.assertTrue(Integer.toString(bpId), bp[0] > 0);
Assert.assertTrue(Integer.toString(bpId), bp[1] > 0);
int line = source.getLineNumber(bp[0] - 1);
int column = source.getColumnNumber(bp[0] - 1);
if (TRACE) {
trace("TESTING BP_" + bpId + ": " + bp[0] + " (" + line + ":" + column + ") => " + bp[1] + ":");
}
try (DebuggerSession session = startSession()) {
startEval(source);
int[] resolvedIndexPtr = new int[] { 0 };
Breakpoint breakpoint = session.install(Breakpoint.newBuilder(DebuggerTester.getSourceImpl(source)).lineIs(line).columnIs(column).oneShot().resolveListener(new Breakpoint.ResolveListener() {
@Override
public void breakpointResolved(Breakpoint brkp, SourceSection section) {
resolvedIndexPtr[0] = section.getCharIndex() + 1;
if (TRACE) {
trace(" resolved: " + (resolvedIndexPtr[0]));
}
}
}).build());
expectSuspended((SuspendedEvent event) -> {
Assert.assertEquals("Expected " + bp[0] + " => " + bp[1] + ", resolved at " + resolvedIndexPtr[0], bp[1], event.getSourceSection().getCharIndex() + 1);
Assert.assertSame(breakpoint, event.getBreakpoints().iterator().next());
event.prepareContinue();
});
expectDone();
Assert.assertEquals("Expected resolved " + bp[0] + " => " + bp[1], bp[1], resolvedIndexPtr[0]);
}
}
}
use of com.oracle.truffle.api.debug.SuspendedEvent 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));
}
}
use of com.oracle.truffle.api.debug.SuspendedEvent in project graal by oracle.
the class DebuggerTesterSnippets method assertBreakpoints.
private void assertBreakpoints(Source source, List<Breakpoint> breakpoints, Set<Breakpoint> breakpointsResolved, List<Breakpoint> breakpointsHit) {
try (DebuggerSession session = startSession()) {
for (Breakpoint breakpoint : breakpoints) {
session.install(breakpoint);
}
startEval(source);
while (breakpointsHit.size() != breakpoints.size()) {
expectSuspended((SuspendedEvent event) -> {
breakpointsHit.addAll(event.getBreakpoints());
event.prepareContinue();
});
}
expectDone();
}
Assert.assertEquals(breakpoints.size(), breakpointsResolved.size());
Assert.assertEquals(breakpoints.size(), breakpointsHit.size());
}
use of com.oracle.truffle.api.debug.SuspendedEvent 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();
}
}
}
Aggregations