use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class TruffleDebugger method getPossibleBreakpoints.
@Override
public Params getPossibleBreakpoints(Location start, Location end, boolean restrictToFunction) throws CommandProcessException {
int scriptId = start.getScriptId();
if (scriptId != end.getScriptId()) {
throw new CommandProcessException("Different location scripts: " + scriptId + ", " + end.getScriptId());
}
Script script = slh.getScript(scriptId);
if (script == null) {
throw new CommandProcessException("Unknown scriptId: " + scriptId);
}
Source source = script.getSource();
int o1 = source.getLineStartOffset(start.getLine());
if (start.getColumn() > 0) {
o1 += start.getColumn() - 1;
}
int o2;
if (end.getLine() > source.getLineCount()) {
o2 = source.getLength();
} else {
o2 = source.getLineStartOffset(end.getLine());
if (end.getColumn() > 0) {
o2 += end.getColumn() - 1;
}
}
SourceSection range = source.createSection(o1, o2 - o1);
Iterable<SourceSection> locations = SuspendableLocationFinder.findSuspendableLocations(range, restrictToFunction, context.getEnv());
JSONObject json = new JSONObject();
JSONArray arr = new JSONArray();
for (SourceSection ss : locations) {
arr.put(new Location(scriptId, ss.getStartLine(), ss.getStartColumn()).toJSON());
}
json.put("locations", arr);
return new Params(json);
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class TruffleDebugger method createCallFrames.
private CallFrame[] createCallFrames(Iterable<DebugStackFrame> frames) {
List<CallFrame> cfs = new ArrayList<>();
int depth = 0;
for (DebugStackFrame frame : frames) {
SourceSection sourceSection = frame.getSourceSection();
if (sourceSection == null) {
continue;
}
if (frame.isInternal()) {
continue;
}
Source source = sourceSection.getSource();
if (source.isInternal()) {
// should not be, double-check
continue;
}
slh.assureLoaded(source);
Script script = slh.getScript(slh.getScriptId(source));
List<Scope> scopes = new ArrayList<>();
DebugScope dscope;
try {
dscope = frame.getScope();
} catch (Exception ex) {
PrintWriter err = context.getErr();
if (err != null) {
err.println("getScope() has caused " + ex);
ex.printStackTrace(err);
}
dscope = null;
}
String scopeType = "block";
boolean wasFunction = false;
SourceSection functionSourceSection = null;
while (dscope != null) {
if (wasFunction) {
scopeType = "closure";
} else if (dscope.isFunctionScope()) {
scopeType = "local";
functionSourceSection = dscope.getSourceSection();
wasFunction = true;
}
if (dscope.isFunctionScope() || dscope.getDeclaredValues().iterator().hasNext()) {
// provide only scopes that have some variables
scopes.add(createScope(scopeType, dscope));
}
dscope = getParent(dscope);
}
try {
dscope = ds.getTopScope(source.getLanguage());
} catch (Exception ex) {
PrintWriter err = context.getErr();
if (err != null) {
err.println("getTopScope() has caused " + ex);
ex.printStackTrace(err);
}
}
while (dscope != null) {
if (dscope.getDeclaredValues().iterator().hasNext()) {
// provide only scopes that have some variables
scopes.add(createScope("global", dscope));
}
dscope = getParent(dscope);
}
CallFrame cf = new CallFrame(frame, depth++, script, sourceSection, functionSourceSection, null, scopes.toArray(new Scope[scopes.size()]));
cfs.add(cf);
}
return cfs.toArray(new CallFrame[cfs.size()]);
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class BreakpointsHandler method createURLBreakpoint.
Params createURLBreakpoint(Object url, int line, int column, String condition) {
JSONArray locations = new JSONArray();
long id;
LoadScriptListener scriptListener;
synchronized (bpIDs) {
id = ++lastID;
scriptListener = script -> {
if (url instanceof Pattern ? ((Pattern) url).matcher(script.getUrl()).matches() : url.equals(script.getUrl())) {
Breakpoint bp = createBuilder(script.getSource(), line, column).resolveListener(resolvedHandler).build();
if (condition != null && !condition.isEmpty()) {
bp.setCondition(condition);
}
bp = ds.install(bp);
synchronized (bpIDs) {
bpIDs.put(bp, id);
SourceSection section = resolvedBreakpoints.remove(bp);
if (section != null) {
Location resolvedLocation = new Location(script.getId(), section.getStartLine(), section.getStartColumn());
locations.put(resolvedLocation.toJSON());
}
}
}
};
scriptListeners.put(id, scriptListener);
}
slh.addLoadScriptListener(scriptListener);
JSONObject json = new JSONObject();
json.put("breakpointId", Long.toString(id));
json.put("locations", locations);
return new Params(json);
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class CPUSamplerCLI method needsColumnSpecifier.
private static boolean needsColumnSpecifier(ProfilerNode<CPUSampler.Payload> firstNode) {
boolean needsColumnsSpecifier = false;
SourceSection sourceSection = firstNode.getSourceSection();
for (ProfilerNode<CPUSampler.Payload> node : firstNode.getParent().getChildren()) {
if (node.getSourceSection() == sourceSection) {
continue;
}
if (intersectsLines(node.getSourceSection(), sourceSection)) {
needsColumnsSpecifier = true;
break;
}
}
return needsColumnsSpecifier;
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SLDebugTest method checkExpressionStepPositions.
private void checkExpressionStepPositions(String stepPositions, boolean includeStatements, StepDepth... steps) {
Source source = slCode("function main() {\n" + " x = 2;\n" + " while (x >= 0 && 5 >= 0) {\n" + " a = 2 * x;\n" + " b = (a * a) / (x * x + 1);\n" + " x = x - transform(a, b);\n" + " }\n" + " return x / 1;\n" + "}\n" + "function transform(a, b) {\n" + " return (1 + 1) * (a + b);\n" + "}\n");
SourceElement[] elements;
if (includeStatements) {
elements = new SourceElement[] { SourceElement.EXPRESSION, SourceElement.STATEMENT };
} else {
elements = new SourceElement[] { SourceElement.EXPRESSION };
}
try (DebuggerSession session = startSession(elements)) {
session.suspendNextExecution();
startEval(source);
// Step through the program
StepDepth lastStep = steps[0];
int stepIndex = 0;
StepConfig expressionStepConfig = StepConfig.newBuilder().sourceElements(elements).build();
for (String stepPos : stepPositions.split("\n")) {
if (stepIndex < steps.length) {
lastStep = steps[stepIndex++];
}
final StepDepth stepDepth = lastStep;
expectSuspended((SuspendedEvent event) -> {
if (!includeStatements) {
assertTrue("Needs to be an expression", event.hasSourceElement(SourceElement.EXPRESSION));
} else {
assertTrue("Needs to be an expression or statement", event.hasSourceElement(SourceElement.EXPRESSION) || event.hasSourceElement(SourceElement.STATEMENT));
}
SourceSection ss = event.getSourceSection();
DebugValue[] inputValues = event.getInputValues();
String input = "";
if (inputValues != null) {
StringBuilder inputBuilder = new StringBuilder("(");
for (DebugValue v : inputValues) {
if (inputBuilder.length() > 1) {
inputBuilder.append(',');
}
if (v != null) {
inputBuilder.append(v.as(String.class));
} else {
inputBuilder.append("null");
}
}
inputBuilder.append(") ");
input = inputBuilder.toString();
}
DebugValue returnValue = event.getReturnValue();
String ret = (returnValue != null) ? returnValue.as(String.class) : "<none>";
String actualPos = "<" + ss.getStartLine() + ":" + ss.getStartColumn() + " - " + ss.getEndLine() + ":" + ss.getEndColumn() + "> " + input + ret;
assertEquals(stepPos, actualPos);
switch(stepDepth) {
case INTO:
event.prepareStepInto(expressionStepConfig);
break;
case OVER:
event.prepareStepOver(expressionStepConfig);
break;
case OUT:
event.prepareStepOut(expressionStepConfig);
break;
}
});
}
expectDone();
}
}
Aggregations