Search in sources :

Example 36 with SourceSection

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

the class TruffleTCK method testValueWithSource.

/**
 * @since 0.22
 */
@Test
public void testValueWithSource() throws Exception {
    String id = valueWithSource();
    if (id == null) {
        return;
    }
    PolyglotEngine.Value valueFunction = findGlobalSymbol(id);
    SourceSection sourceLocation;
    PolyglotRuntime.Instrument instr = vm().getRuntime().getInstruments().get(TckInstrument.ID);
    instr.setEnabled(true);
    try {
        PolyglotEngine.Value value = valueFunction.execute();
        TckInstrument tckInstrument = instr.lookup(TckInstrument.class);
        assertNotNull(tckInstrument);
        TruffleInstrument.Env env = tckInstrument.getEnvironment();
        assertNotNull(env);
        sourceLocation = value.getSourceLocation();
        assertNotNull(sourceLocation);
        List<SourceSection> lss = env.getInstrumenter().querySourceSections(SourceSectionFilter.ANY);
        assertTrue("Source section not among loaded sections", lss.contains(sourceLocation));
    } finally {
        instr.setEnabled(false);
    }
}
Also used : TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) TckInstrument(com.oracle.truffle.tck.impl.TckInstrument) SourceSection(com.oracle.truffle.api.source.SourceSection) Test(org.junit.Test)

Example 37 with SourceSection

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

the class DebugALot method logValue.

private void logValue(String prefix, DebugValue v) {
    LanguageInfo language = v.getOriginalLanguage();
    if (language != null) {
        logger.print(prefix);
        logger.print("From: ");
        logger.println(language.getId());
    }
    DebugValue metaObject = v.getMetaObject();
    if (metaObject != null) {
        logger.print(prefix);
        logger.print("Type: ");
        logger.println(metaObject.as(String.class));
    }
    SourceSection sourceLocation = v.getSourceLocation();
    if (sourceLocation != null) {
        logger.print(prefix);
        logger.print("SourceSection: ");
        logSourceSection(sourceLocation);
    }
    if (v.isArray()) {
        List<DebugValue> array = v.getArray();
        int length = array.size();
        logger.print(prefix);
        logger.print("Array of length: ");
        logger.println(Integer.toString(length));
        for (int i = 0; i < length && i < 10; i++) {
            logger.print(prefix);
            logger.print("  element #");
            logger.print(Integer.toString(i));
            logger.print(" : ");
            logger.println(array.get(i).as(String.class));
        }
    }
    Collection<DebugValue> properties = v.getProperties();
    logger.print(prefix);
    if (properties == null || properties.isEmpty()) {
        logger.println("Properties: none");
    } else {
        logger.print("Properties: ");
        logger.println(Integer.toString(properties.size()));
    }
    logger.print(prefix);
    logger.print("Internal: ");
    logger.println(v.isInternal());
    logger.print(prefix);
    logger.print("Readable: ");
    logger.println(v.isReadable());
    logger.print(prefix);
    logger.print("Writable: ");
    logger.println(v.isWritable());
}
Also used : LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) DebugValue(com.oracle.truffle.api.debug.DebugValue) SourceSection(com.oracle.truffle.api.source.SourceSection)

Example 38 with SourceSection

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

the class SLNodeFactory method finishFunction.

public void finishFunction(SLStatementNode bodyNode) {
    if (bodyNode == null) {
        // a state update that would otherwise be performed by finishBlock
        lexicalScope = lexicalScope.outer;
    } else {
        methodNodes.add(bodyNode);
        final int bodyEndPos = bodyNode.getSourceEndIndex();
        final SourceSection functionSrc = source.createSection(functionStartPos, bodyEndPos - functionStartPos);
        final SLStatementNode methodBlock = finishBlock(methodNodes, functionBodyStartPos, bodyEndPos - functionBodyStartPos);
        assert lexicalScope == null : "Wrong scoping of blocks in parser";
        final SLFunctionBodyNode functionBodyNode = new SLFunctionBodyNode(methodBlock);
        functionBodyNode.setSourceSection(functionSrc.getCharIndex(), functionSrc.getCharLength());
        final SLRootNode rootNode = new SLRootNode(language, frameDescriptor, functionBodyNode, functionSrc, functionName);
        allFunctions.put(functionName, Truffle.getRuntime().createCallTarget(rootNode));
    }
    functionStartPos = 0;
    functionName = null;
    functionBodyStartPos = 0;
    parameterCount = 0;
    frameDescriptor = null;
    lexicalScope = null;
}
Also used : SLFunctionBodyNode(com.oracle.truffle.sl.nodes.controlflow.SLFunctionBodyNode) SourceSection(com.oracle.truffle.api.source.SourceSection) SLRootNode(com.oracle.truffle.sl.nodes.SLRootNode) SLStatementNode(com.oracle.truffle.sl.nodes.SLStatementNode)

Example 39 with SourceSection

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

the class ProfilerTest method testTimingEnabled.

@Test
public void testTimingEnabled() throws IOException {
    profiler.setCollecting(true);
    profiler.setTiming(false);
    Map<SourceSection, Profiler.Counter> counters = profiler.getCounters();
    Assert.assertEquals(0, counters.size());
    Assert.assertTrue(profiler.isCollecting());
    Assert.assertFalse(profiler.isTiming());
    Assert.assertFalse(profiler.hasData());
    Assert.assertNull(profiler.getMimeTypes());
    assertEvalOut(source, "");
    counters = profiler.getCounters();
    Assert.assertEquals(4, counters.size());
    Assert.assertTrue(profiler.isCollecting());
    Assert.assertFalse(profiler.isTiming());
    Assert.assertTrue(profiler.hasData());
    final com.oracle.truffle.api.source.Source sourceImpl = getSourceImpl(source);
    final SourceSection rootSection = sourceImpl.createSection(0, 140);
    final SourceSection leafSection = sourceImpl.createSection(17, 16);
    final SourceSection callfooSection = sourceImpl.createSection(47, 27);
    final SourceSection callbarSection = sourceImpl.createSection(88, 27);
    Profiler.Counter root = counters.get(rootSection);
    Profiler.Counter leaf = counters.get(leafSection);
    Profiler.Counter callfoo = counters.get(callfooSection);
    Profiler.Counter callbar = counters.get(callbarSection);
    Assert.assertNotNull(root);
    Assert.assertNotNull(leaf);
    Assert.assertNotNull(callfoo);
    Assert.assertNotNull(callbar);
    final Profiler.Counter.TimeKind testTimeKind = Profiler.Counter.TimeKind.INTERPRETED_AND_COMPILED;
    Assert.assertEquals(0, root.getTotalTime(testTimeKind));
    Assert.assertEquals(0, leaf.getTotalTime(testTimeKind));
    Assert.assertEquals(0, callfoo.getTotalTime(testTimeKind));
    Assert.assertEquals(0, callbar.getTotalTime(testTimeKind));
    Assert.assertEquals(0, root.getSelfTime(testTimeKind));
    Assert.assertEquals(0, leaf.getSelfTime(testTimeKind));
    Assert.assertEquals(0, callfoo.getSelfTime(testTimeKind));
    Assert.assertEquals(0, callbar.getSelfTime(testTimeKind));
    profiler.setTiming(true);
    Assert.assertTrue(profiler.isTiming());
    Assert.assertTrue(profiler.hasData());
    assertEvalOut(source, "");
    Assert.assertTrue(root.getTotalTime(testTimeKind) > 0);
    Assert.assertTrue(leaf.getTotalTime(testTimeKind) > 0);
    Assert.assertTrue(callfoo.getTotalTime(testTimeKind) > 0);
    Assert.assertTrue(callbar.getTotalTime(testTimeKind) > 0);
    Assert.assertTrue(root.getSelfTime(testTimeKind) > 0);
    Assert.assertTrue(leaf.getSelfTime(testTimeKind) > 0);
    Assert.assertTrue(callfoo.getSelfTime(testTimeKind) > 0);
    Assert.assertTrue(callbar.getSelfTime(testTimeKind) > 0);
    profiler.setTiming(false);
    Assert.assertFalse(profiler.isTiming());
    Assert.assertTrue(profiler.hasData());
    assertEvalOut(source, "");
    Assert.assertTrue(root.getTotalTime(testTimeKind) > 0);
    Assert.assertTrue(leaf.getTotalTime(testTimeKind) > 0);
    Assert.assertTrue(callfoo.getTotalTime(testTimeKind) > 0);
    Assert.assertTrue(callbar.getTotalTime(testTimeKind) > 0);
    Assert.assertTrue(root.getSelfTime(testTimeKind) > 0);
    Assert.assertTrue(leaf.getSelfTime(testTimeKind) > 0);
    Assert.assertTrue(callfoo.getSelfTime(testTimeKind) > 0);
    Assert.assertTrue(callbar.getSelfTime(testTimeKind) > 0);
    profiler.clearData();
    assertEvalOut(source, "");
    Assert.assertFalse(profiler.isTiming());
    Assert.assertTrue(profiler.hasData());
    Assert.assertEquals(0, root.getTotalTime(testTimeKind));
    Assert.assertEquals(0, leaf.getTotalTime(testTimeKind));
    Assert.assertEquals(0, callfoo.getTotalTime(testTimeKind));
    Assert.assertEquals(0, callbar.getTotalTime(testTimeKind));
    Assert.assertEquals(0, root.getSelfTime(testTimeKind));
    Assert.assertEquals(0, leaf.getSelfTime(testTimeKind));
    Assert.assertEquals(0, callfoo.getSelfTime(testTimeKind));
    Assert.assertEquals(0, callbar.getSelfTime(testTimeKind));
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection) Test(org.junit.Test) AbstractInstrumentationTest(com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)

Example 40 with SourceSection

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

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