Search in sources :

Example 26 with SourceSection

use of com.oracle.truffle.api.source.SourceSection 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 27 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.

the class DebuggerTesterSnippets method assertBreakpointsBreakEverywhere.

/**
 * Utility method that tests if a breakpoint submitted to any location in the source code
 * suspends the execution. A two-pass test is performed. In the first pass, line breakpoints are
 * submitted to every line. In the second pass, breakpoints are submitted to every line and
 * column combination, even outside the source scope. It is expected that the breakpoints
 * resolve to a nearest suspendable location and it is checked that all breakpoints are hit.
 *
 * @param source a source to evaluate with breakpoints submitted everywhere
 * @since 0.33
 */
public void assertBreakpointsBreakEverywhere(Source source) {
    int numLines = source.getLineCount();
    int numColumns = 0;
    for (int i = 1; i <= numLines; i++) {
        int ll = source.getLineLength(i);
        if (ll > numColumns) {
            numColumns = ll;
        }
    }
    com.oracle.truffle.api.source.Source tsource = DebuggerTester.getSourceImpl(source);
    final List<Breakpoint> breakpoints = new ArrayList<>();
    final Set<Breakpoint> breakpointsResolved = new HashSet<>();
    final List<Breakpoint> breakpointsHit = new ArrayList<>();
    Breakpoint.ResolveListener resolveListener = new Breakpoint.ResolveListener() {

        @Override
        public void breakpointResolved(Breakpoint breakpoint, SourceSection section) {
            Assert.assertFalse(breakpointsResolved.contains(breakpoint));
            breakpointsResolved.add(breakpoint);
        }
    };
    // Test all line breakpoints
    for (int l = 1; l < (numLines + 5); l++) {
        Breakpoint breakpoint = Breakpoint.newBuilder(tsource).lineIs(l).oneShot().resolveListener(resolveListener).build();
        breakpoints.add(breakpoint);
    }
    assertBreakpoints(source, breakpoints, breakpointsResolved, breakpointsHit);
    breakpoints.clear();
    breakpointsResolved.clear();
    breakpointsHit.clear();
    // Test all line/column breakpoints
    for (int l = 1; l < (numLines + 5); l++) {
        for (int c = 1; c < (numColumns + 5); c++) {
            Breakpoint breakpoint = Breakpoint.newBuilder(tsource).lineIs(l).columnIs(c).oneShot().resolveListener(resolveListener).build();
            breakpoints.add(breakpoint);
        }
    }
    assertBreakpoints(source, breakpoints, breakpointsResolved, breakpointsHit);
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) ArrayList(java.util.ArrayList) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) SourceSection(com.oracle.truffle.api.source.SourceSection) HashSet(java.util.HashSet)

Example 28 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.

the class SLStatementNode method getSourceSection.

/*
     * The creation of source section can be implemented lazily by looking up the root node source
     * and then creating the source section object using the indices stored in the node. This avoids
     * the eager creation of source section objects during parsing and creates them only when they
     * are needed. Alternatively, if the language uses source sections to implement language
     * semantics, then it might be more efficient to eagerly create source sections and store it in
     * the AST.
     *
     * For more details see {@link InstrumentableNode}.
     */
@Override
@TruffleBoundary
public final SourceSection getSourceSection() {
    if (sourceCharIndex == NO_SOURCE) {
        // AST node without source
        return null;
    }
    RootNode rootNode = getRootNode();
    if (rootNode == null) {
        // not yet adopted yet
        return null;
    }
    SourceSection rootSourceSection = rootNode.getSourceSection();
    if (rootSourceSection == null) {
        return null;
    }
    Source source = rootSourceSection.getSource();
    if (sourceCharIndex == UNAVAILABLE_SOURCE) {
        return source.createUnavailableSection();
    } else {
        return source.createSection(sourceCharIndex, sourceLength);
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(com.oracle.truffle.api.source.Source) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 29 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project sulong by graalvm.

the class LLVMPrintStackTrace method fillStackTrace.

private static void fillStackTrace(SulongStackTrace stackTrace, Node node) {
    LLVMBasicBlockNode block = NodeUtil.findParent(node, LLVMBasicBlockNode.class);
    LLVMFunctionStartNode f = NodeUtil.findParent(node, LLVMFunctionStartNode.class);
    if (block == null || f == null) {
        LLVMIntrinsicExpressionNode intrinsic = NodeUtil.findParent(node, LLVMIntrinsicExpressionNode.class);
        if (intrinsic != null) {
            stackTrace.addStackTraceElement(intrinsic.toString(), null, null);
        }
        return;
    }
    LLVMSourceLocation location = null;
    if (node instanceof LLVMNode && ((LLVMNode) node).getSourceLocation() != null) {
        location = ((LLVMNode) node).getSourceLocation();
    }
    if (location == null) {
        location = block.getSourceLocation();
    }
    if (location != null) {
        stackTrace.addStackTraceElement(f.getOriginalName(), location, f.getBcName(), f.getBcSource().getName(), blockName(block));
        return;
    }
    SourceSection s = node.getSourceSection();
    if (s == null) {
        s = f.getSourceSection();
    }
    if (s == null) {
        stackTrace.addStackTraceElement(f.getBcName(), f.getBcSource().getName(), blockName(block));
    } else {
        location = LLVMSourceLocation.createUnknown(s);
        stackTrace.addStackTraceElement(f.getOriginalName(), location, f.getBcName(), f.getBcSource().getName(), blockName(block));
    }
}
Also used : LLVMFunctionStartNode(com.oracle.truffle.llvm.nodes.func.LLVMFunctionStartNode) LLVMIntrinsicExpressionNode(com.oracle.truffle.llvm.nodes.intrinsics.llvm.LLVMIntrinsicRootNode.LLVMIntrinsicExpressionNode) LLVMBasicBlockNode(com.oracle.truffle.llvm.nodes.base.LLVMBasicBlockNode) LLVMSourceLocation(com.oracle.truffle.llvm.runtime.debug.scope.LLVMSourceLocation) SourceSection(com.oracle.truffle.api.source.SourceSection) LLVMNode(com.oracle.truffle.llvm.runtime.nodes.api.LLVMNode)

Example 30 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project sulong by graalvm.

the class LLVMSourceScope method isDeclaredBefore.

private static boolean isDeclaredBefore(LLVMSourceSymbol symbol, SourceSection useLoc) {
    // any for which we can't tell
    if (useLoc == null) {
        return true;
    }
    LLVMSourceLocation symbolDecl = symbol.getLocation();
    if (symbolDecl == null) {
        return true;
    }
    SourceSection declLoc = symbolDecl.getSourceSection();
    if (declLoc == null) {
        return true;
    }
    if (declLoc.getSource().equals(useLoc.getSource())) {
        return declLoc.getCharIndex() <= useLoc.getCharIndex();
    }
    return true;
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection)

Aggregations

SourceSection (com.oracle.truffle.api.source.SourceSection)68 Test (org.junit.Test)23 Source (com.oracle.truffle.api.source.Source)15 Source (org.graalvm.polyglot.Source)12 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)11 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)8 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)8 AbstractInstrumentationTest (com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)6 Node (com.oracle.truffle.api.nodes.Node)6 RootNode (com.oracle.truffle.api.nodes.RootNode)6 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)5 DebugValue (com.oracle.truffle.api.debug.DebugValue)5 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)3 DebugScope (com.oracle.truffle.api.debug.DebugScope)3 Counter (com.oracle.truffle.api.instrumentation.test.examples.StatementProfilerExample.Counter)3 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)3 RootCallTarget (com.oracle.truffle.api.RootCallTarget)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 WrapperNode (com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode)2 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)2