Search in sources :

Example 1 with SourceSection

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

the class DebugStackFrameTest method testSourceSections.

@Test
public void testSourceSections() {
    final Source source = testSource("ROOT(DEFINE(a,ROOT(\n" + "  STATEMENT())\n" + "),\n" + "DEFINE(b,ROOT(\n" + "  CALL(a))\n" + "), \n" + "CALL(b))\n");
    try (DebuggerSession session = startSession()) {
        session.suspendNextExecution();
        startEval(source);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            SourceSection ss = frame.getSourceSection();
            assertSection(ss, "STATEMENT()", 2, 3, 2, 13);
            SourceSection fss = getFunctionSourceSection(frame);
            assertSection(fss, "ROOT(\n  STATEMENT())\n", 1, 15, 2, 15);
            Iterator<DebugStackFrame> stackFrames = event.getStackFrames().iterator();
            // The top one
            assertEquals(frame, stackFrames.next());
            // b
            frame = stackFrames.next();
            ss = frame.getSourceSection();
            assertSection(ss, "CALL(a)", 5, 3, 5, 9);
            fss = getFunctionSourceSection(frame);
            assertSection(fss, "ROOT(\n  CALL(a))\n", 4, 10, 5, 11);
            // root
            frame = stackFrames.next();
            ss = frame.getSourceSection();
            assertSection(ss, "CALL(b)", 7, 1, 7, 7);
            fss = getFunctionSourceSection(frame);
            assertSection(fss, source.getCharacters().toString(), 1, 1, 7, 9);
            assertFalse(stackFrames.hasNext());
            event.prepareContinue();
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 2 with SourceSection

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

the class SourceSectionTest method emptySourceSectionOnLongSource.

@Test
public void emptySourceSectionOnLongSource() {
    SourceSection section = longSource.createSection(longSource.getCharacters().length() - 1, 0);
    assertNotNull(section);
    assertEquals(longSource.getCharacters().length() - 1, section.getCharIndex());
    assertEquals(0, section.getCharLength(), 0);
    assertEquals(3, section.getStartLine());
    assertEquals(3, section.getEndLine());
    assertEquals(2, section.getStartColumn());
    assertEquals(2, section.getEndColumn());
    SourceSection other = longSource.createSection(longSource.getCharacters().length() - 1, 0);
    assertTrue(section.equals(other));
    assertEquals(other.hashCode(), section.hashCode());
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection) Test(org.junit.Test)

Example 3 with SourceSection

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

the class SourceSectionTest method emptySourceTest0.

@Test
public void emptySourceTest0() {
    SourceSection section = emptySource.createSection(0, 0);
    assertNotNull(section);
    assertEquals(section.getCharacters(), "");
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection) Test(org.junit.Test)

Example 4 with SourceSection

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

the class SourceSectionTest method onceObtainedAlwaysTheSame.

@Test
public void onceObtainedAlwaysTheSame() throws Exception {
    File sample = File.createTempFile("hello", ".txt");
    sample.deleteOnExit();
    try (FileWriter w = new FileWriter(sample)) {
        w.write("Hello world!");
    }
    Source complexHello = Source.newBuilder(sample).build();
    SourceSection helloTo = complexHello.createSection(6, 5);
    assertEquals("world", helloTo.getCharacters());
    try (FileWriter w = new FileWriter(sample)) {
        w.write("Hi world!");
    }
    Source simpleHi = Source.newBuilder(sample).build();
    SourceSection hiTo = simpleHi.createSection(3, 5);
    assertEquals("world", hiTo.getCharacters());
    assertEquals("Previously allocated sections remain the same", "world", helloTo.getCharacters());
    sample.delete();
}
Also used : FileWriter(java.io.FileWriter) SourceSection(com.oracle.truffle.api.source.SourceSection) File(java.io.File) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 5 with SourceSection

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

the class SuspendableLocationFinder method findSuspendableLocations.

static Iterable<SourceSection> findSuspendableLocations(SourceSection range, boolean restrictToSingleFunction, TruffleInstrument.Env env) {
    Source source = range.getSource();
    int startIndex = range.getCharIndex();
    int endIndex = range.getCharEndIndex();
    SectionsCollector sectionsCollector = collectSuspendableLocations(source, startIndex, endIndex, restrictToSingleFunction, env);
    List<SourceSection> sections = sectionsCollector.getSections();
    if (sections.isEmpty()) {
        SourceSectionFilter filter = SourceSectionFilter.newBuilder().sourceIs(source).indexIn(IndexRange.between(startIndex, endIndex)).build();
        ContainerNodeCollector nodeCollector = new ContainerNodeCollector(startIndex);
        EventBinding<ContainerNodeCollector> binding = env.getInstrumenter().attachLoadSourceSectionListener(filter, nodeCollector, true);
        binding.dispose();
        if (nodeCollector.getContainerNode() != null) {
            Node suspendableNode = nodeCollector.getContainerNode().findNearestNodeAt(startIndex, SUSPENDABLE_TAGS_SET);
            if (suspendableNode != null) {
                startIndex = Math.min(startIndex, suspendableNode.getSourceSection().getCharIndex());
                endIndex = Math.max(endIndex, suspendableNode.getSourceSection().getCharEndIndex());
                sectionsCollector = collectSuspendableLocations(source, startIndex, endIndex, restrictToSingleFunction, env);
                sections = sectionsCollector.getSections();
            }
        }
    }
    return sections;
}
Also used : Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) SourceSection(com.oracle.truffle.api.source.SourceSection) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) Source(com.oracle.truffle.api.source.Source)

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