Search in sources :

Example 46 with SuspendedEvent

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]);
}
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 47 with SuspendedEvent

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&lt;number&gt;_"
 * and breakpoint resolution marks in the form of "R&lt;number&gt;_" where &lt;number&gt; 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&lt;start number&gt;-&lt;end
 * number&gt;_", 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 &lt;= 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]);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(org.graalvm.polyglot.Source) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SourceSection(com.oracle.truffle.api.source.SourceSection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with SuspendedEvent

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));
    }
}
Also used : SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback)

Example 49 with SuspendedEvent

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());
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent)

Example 50 with SuspendedEvent

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();
        }
    }
}
Also used : 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)

Aggregations

SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)100 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)97 Source (org.graalvm.polyglot.Source)95 Test (org.junit.Test)93 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)45 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)19 DebugValue (com.oracle.truffle.api.debug.DebugValue)14 Debugger (com.oracle.truffle.api.debug.Debugger)11 SuspendedCallback (com.oracle.truffle.api.debug.SuspendedCallback)9 Context (org.graalvm.polyglot.Context)9 SourceSection (com.oracle.truffle.api.source.SourceSection)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 DebugScope (com.oracle.truffle.api.debug.DebugScope)5 HashMap (java.util.HashMap)3 DebugContext (com.oracle.truffle.api.debug.DebugContext)2 Map (java.util.Map)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Matcher (java.util.regex.Matcher)2