Search in sources :

Example 61 with DebuggerSession

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

the class DebugALot method onCreate.

@Override
protected void onCreate(Env env) {
    Boolean debugALot = env.getOptions().get(DebugALot);
    failFast = env.getOptions().get(FailFast);
    doEval = env.getOptions().get(Eval);
    boolean isLogFile = env.getOptions().hasBeenSet(LogFile);
    if (!(Boolean.TRUE.equals(debugALot) || failFast || doEval || isLogFile)) {
        return;
    }
    if (isLogFile) {
        String logFilePath = env.getOptions().get(LogFile);
        try {
            logger = new PrintWriter(new FileWriter(logFilePath));
        } catch (IOException ioex) {
            logger = new PrintWriter(env.out());
            logger.print(ioex.getLocalizedMessage());
        }
    } else {
        logger = new PrintWriter(env.out());
    }
    Debugger debugger = env.lookup(env.getInstruments().get("debugger"), Debugger.class);
    DebuggerSession debuggerSession = debugger.startSession(this);
    debuggerSession.suspendNextExecution();
    debuggerSession.setSteppingFilter(SuspensionFilter.newBuilder().ignoreLanguageContextInitialization(true).build());
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) FileWriter(java.io.FileWriter) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 62 with DebuggerSession

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

the class DebuggerTesterSnippets method assertLineBreakpointsResolution.

/**
 * Utility method that checks proper resolution of line breakpoints. Breakpoints are submitted
 * to every line of the source and their resolution location is checked.
 * <p>
 * The source need to contain resolution marks in the form of
 * "<code>R&lt;line number&gt;_</code>" where &lt;line number&gt; is line number of the original
 * breakpoint position and <code>R</code> is the resolved mark name. These marks need to be
 * placed at the proper breakpoint resolution line/column position, for a breakpoint submitted
 * to every line. When several submitted breakpoints resolve to the same position, an interval
 * can be specified in the form of
 * "<code>R&lt;line start number&gt;-&lt;line end number&gt;_</code>", 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>
 * // test
 * function test(n) {
 *   if (R1-3_n &lt;= 1) {
 *     return R4_2 * n;
 *   }
 *   return R5-7_n - 1;
 * }
 * </pre>
 *
 * @param sourceWithMarks a source text, which contains the resolution marks
 * @param resolvedMarkName the 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 assertLineBreakpointsResolution(String sourceWithMarks, String resolvedMarkName, String language) throws IOException {
    Pattern br = Pattern.compile("(" + resolvedMarkName + "\\d+_|" + resolvedMarkName + "\\d+-\\d+_)");
    Map<Integer, Integer> bps = new HashMap<>();
    String sourceString = sourceWithMarks;
    Matcher bm = br.matcher(sourceString);
    while (bm.find()) {
        String bg = bm.group();
        int index = bm.start();
        int bn1;
        int bn2;
        String bpNums = bg.substring(1, bg.length() - 1);
        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++) {
            Integer bp = bps.get(bn);
            if (bp == null) {
                bps.put(bn, index + 1);
            } else {
                Assert.fail(bg + " specified more than once.");
            }
        }
        sourceString = bm.replaceFirst("");
        bm.reset(sourceString);
    }
    if (TRACE) {
        trace("sourceString = '" + sourceString + "'");
    }
    final Source source = Source.newBuilder(language, sourceString, "testMisplacedLineBreakpoint." + language).build();
    com.oracle.truffle.api.source.Source tsource = DebuggerTester.getSourceImpl(source);
    for (int l = 1; l < source.getLineCount(); l++) {
        if (!bps.containsKey(l)) {
            Assert.fail("Line " + l + " is missing.");
        }
    }
    for (Map.Entry<Integer, Integer> bentry : bps.entrySet()) {
        int line = bentry.getKey();
        int indexResolved = bentry.getValue();
        int lineResolved = source.getLineNumber(indexResolved - 1);
        if (TRACE) {
            trace("TESTING breakpoint '" + line + "' => " + lineResolved + ":");
        }
        try (DebuggerSession session = startSession()) {
            startEval(source);
            int[] resolvedIndexPtr = new int[] { 0 };
            Breakpoint breakpoint = session.install(Breakpoint.newBuilder(tsource).lineIs(line).oneShot().resolveListener(new Breakpoint.ResolveListener() {

                @Override
                public void breakpointResolved(Breakpoint brkp, SourceSection section) {
                    resolvedIndexPtr[0] = section.getCharIndex() + 1;
                    if (TRACE) {
                        trace("BREAKPOINT resolved to " + section.getStartLine() + ":" + section.getStartColumn());
                    }
                }
            }).build());
            expectSuspended((SuspendedEvent event) -> {
                Assert.assertEquals("Expected " + line + " => " + lineResolved, lineResolved, event.getSourceSection().getStartLine());
                Assert.assertSame(breakpoint, event.getBreakpoints().iterator().next());
                event.prepareContinue();
            });
            expectDone();
            Assert.assertEquals("Expected resolved " + line + " => " + indexResolved, indexResolved, 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 63 with DebuggerSession

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

the class NestedContextTest method testNestedStepping.

private void testNestedStepping(int depth) {
    if (depth == 0) {
        return;
    }
    Source testSource = testSource("ROOT(\n" + "  STATEMENT,\n" + "  STATEMENT\n" + ")\n");
    pushContext();
    try (DebuggerSession session = startSession()) {
        Breakpoint breakpoint3 = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
        session.suspendNextExecution();
        startEval(testSource);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT");
            assertEquals(0, event.getBreakpoints().size());
            testNestedStepping(depth - 1);
            event.prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 3, true, "STATEMENT");
            assertEquals(1, event.getBreakpoints().size());
            assertSame(breakpoint3, event.getBreakpoints().iterator().next());
            testNestedStepping(depth - 1);
            event.prepareStepInto(1);
        });
        expectDone();
    }
    popContext();
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source)

Example 64 with DebuggerSession

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

the class StepTest method testStepInto.

@Test
public void testStepInto() throws Throwable {
    final Source source = testSource("ROOT(\n" + "  DEFINE(bar, STATEMENT),\n" + "  DEFINE(foo, ROOT(STATEMENT(CALL(bar)), \n" + "                   STATEMENT(CALL(bar)))),\n" + "  STATEMENT(CALL(foo)),\n" + "  STATEMENT(CALL(foo))\n" + ")\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 5, true, "STATEMENT(CALL(foo))").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 3, true, "STATEMENT(CALL(bar))").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 3, false, "CALL(bar)").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 4, true, "STATEMENT(CALL(bar))").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 2, true, "STATEMENT").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 4, false, "CALL(bar)").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 5, false, "CALL(foo)").prepareStepInto(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            checkState(event, 6, true, "STATEMENT(CALL(foo))").prepareContinue();
        });
        expectDone();
    }
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 65 with DebuggerSession

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

the class StepTest method testBlock.

@Test
public void testBlock() throws Throwable {
    final Source source = testSource("ROOT(\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT,\n" + "  STATEMENT\n" + ")\n");
    try (DebuggerSession session = startSession()) {
        startEval(source);
        // make javac happy and use the sessiononce.
        session.getDebugger();
        expectDone();
    }
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Aggregations

DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)110 Source (org.graalvm.polyglot.Source)103 Test (org.junit.Test)102 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)97 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)49 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)20 DebugValue (com.oracle.truffle.api.debug.DebugValue)16 Debugger (com.oracle.truffle.api.debug.Debugger)14 Context (org.graalvm.polyglot.Context)11 SourceSection (com.oracle.truffle.api.source.SourceSection)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 DebugContext (com.oracle.truffle.api.debug.DebugContext)6 SuspendedCallback (com.oracle.truffle.api.debug.SuspendedCallback)6 DebugScope (com.oracle.truffle.api.debug.DebugScope)5 Engine (org.graalvm.polyglot.Engine)5 HashMap (java.util.HashMap)3 Value (org.graalvm.polyglot.Value)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2