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);
}
}
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());
}
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;
}
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));
}
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<line number>_</code>" where <line number> 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<line start number>-<line end number>_</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 <= 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]);
}
}
}
Aggregations