Search in sources :

Example 31 with SourceSection

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

the class DIScopeBuilder method extend.

private static SourceSection extend(SourceSection base) {
    if (base == null) {
        return null;
    }
    SourceSection section;
    try {
        final Source source = base.getSource();
        final int length = source.getLength() - base.getCharIndex();
        section = source.createSection(base.getCharIndex(), length);
    } catch (Throwable ignored) {
        section = base;
    }
    return section;
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection) Source(com.oracle.truffle.api.source.Source)

Example 32 with SourceSection

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

the class SLDebugDirectTest method assertLocation.

private void assertLocation(final String name, final int line, final boolean isBefore, final String code, final Object... expectedFrame) {
    run.addLast(() -> {
        assertNotNull(suspendedEvent);
        final SourceSection suspendedSourceSection = suspendedEvent.getSourceSection();
        Assert.assertEquals(line, suspendedSourceSection.getStartLine());
        Assert.assertEquals(code, suspendedSourceSection.getCharacters());
        Assert.assertEquals(isBefore, suspendedEvent.getSuspendAnchor() == SuspendAnchor.BEFORE);
        final DebugStackFrame frame = suspendedEvent.getTopStackFrame();
        assertEquals(name, frame.getName());
        for (int i = 0; i < expectedFrame.length; i = i + 2) {
            final String expectedIdentifier = (String) expectedFrame[i];
            final Object expectedValue = expectedFrame[i + 1];
            DebugScope scope = frame.getScope();
            DebugValue slot = scope.getDeclaredValue(expectedIdentifier);
            while (slot == null && (scope = scope.getParent()) != null) {
                slot = scope.getDeclaredValue(expectedIdentifier);
            }
            if (expectedValue != UNASSIGNED) {
                Assert.assertNotNull(expectedIdentifier, slot);
                final String slotValue = slot.as(String.class);
                Assert.assertEquals(expectedValue, slotValue);
            } else {
                Assert.assertNull(expectedIdentifier, slot);
            }
        }
        run.removeFirst().run();
    });
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebugScope(com.oracle.truffle.api.debug.DebugScope) DebugValue(com.oracle.truffle.api.debug.DebugValue) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) SourceSection(com.oracle.truffle.api.source.SourceSection) Breakpoint(com.oracle.truffle.api.debug.Breakpoint)

Example 33 with SourceSection

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

the class SLDebugTest method testSourceLocation.

@Test
public void testSourceLocation() {
    final Source varsSource = slCode("function main() {\n" + "  a = doNull();\n" + "  c = 10;\n" + "  d = \"str\";\n" + "  e = new();\n" + "  f = doNull;\n" + "  return;\n" + "}\n" + "function doNull() {}\n");
    try (DebuggerSession session = startSession()) {
        session.install(Breakpoint.newBuilder(getSourceImpl(varsSource)).lineIs(7).build());
        startEval(varsSource);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            DebugScope scope = frame.getScope();
            DebugValue v = scope.getDeclaredValue("a");
            assertNull(v.getSourceLocation());
            v = scope.getDeclaredValue("c");
            assertNull(v.getSourceLocation());
            v = scope.getDeclaredValue("d");
            assertNull(v.getSourceLocation());
            v = scope.getDeclaredValue("e");
            assertNull(v.getSourceLocation());
            v = scope.getDeclaredValue("f");
            SourceSection sourceLocation = v.getSourceLocation();
            Assert.assertNotNull(sourceLocation);
            assertEquals(9, sourceLocation.getStartLine());
            assertEquals(9, sourceLocation.getEndLine());
            assertEquals("doNull() {}", sourceLocation.getCharacters());
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) DebugScope(com.oracle.truffle.api.debug.DebugScope) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 34 with SourceSection

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

the class SLInstrumentTest method testReplaceNodeReturnValue.

/**
 * This test demonstrates that it's possible to easily replace a return value of any node using
 * {@link ExecutionEventListener#onUnwind(com.oracle.truffle.api.instrumentation.EventContext, com.oracle.truffle.api.frame.VirtualFrame, java.lang.Object)}
 * .
 */
@Test
public void testReplaceNodeReturnValue() throws Exception {
    String code = "function main() {\n" + "  a = new();\n" + "  b = a.rp1;\n" + "  return b;\n" + "}\n";
    final Source source = Source.newBuilder("sl", code, "testing").build();
    SourceSection ss = DebuggerTester.getSourceImpl(source).createSection(24, 5);
    Context context = Context.create();
    NewReplacedInstrument replaced = context.getEngine().getInstruments().get("testNewNodeReplaced").lookup(NewReplacedInstrument.class);
    replaced.attachAt(ss);
    Value ret = context.eval(source);
    assertEquals("Replaced Value", ret.toString());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) Value(org.graalvm.polyglot.Value) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 35 with SourceSection

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

the class SLStatementNode method formatSourceSection.

/**
 * Formats a source section of a node in human readable form. If no source section could be
 * found it looks up the parent hierarchy until it finds a source section. Nodes where this was
 * required append a <code>'~'</code> at the end.
 *
 * @param node the node to format.
 * @return a formatted source section string
 */
public static String formatSourceSection(Node node) {
    if (node == null) {
        return "<unknown>";
    }
    SourceSection section = node.getSourceSection();
    boolean estimated = false;
    if (section == null) {
        section = node.getEncapsulatingSourceSection();
        estimated = true;
    }
    if (section == null || section.getSource() == null) {
        return "<unknown source>";
    } else {
        String sourceName = new File(section.getSource().getName()).getName();
        int startLine = section.getStartLine();
        return String.format("%s:%d%s", sourceName, startLine, estimated ? "~" : "");
    }
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection) File(java.io.File)

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