use of com.oracle.truffle.tools.chromeinspector.types.Script 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.tools.chromeinspector.types.Script 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.tools.chromeinspector.types.Script in project graal by oracle.
the class TruffleProfiler method getCoverage.
private Params getCoverage(Collection<CPUTracer.Payload> payloads) {
JSONObject json = new JSONObject();
Map<Source, Map<String, Collection<CPUTracer.Payload>>> sourceToRoots = new HashMap<>();
payloads.forEach(payload -> {
Map<String, Collection<CPUTracer.Payload>> rootsToPayloads = sourceToRoots.computeIfAbsent(payload.getSourceSection().getSource(), s -> new LinkedHashMap<>());
Collection<CPUTracer.Payload> pls = rootsToPayloads.computeIfAbsent(payload.getRootName(), t -> new LinkedList<>());
pls.add(payload);
});
JSONArray result = new JSONArray();
sourceToRoots.entrySet().stream().map(sourceEntry -> {
List<FunctionCoverage> functions = new ArrayList<>();
sourceEntry.getValue().entrySet().forEach(rootEntry -> {
boolean isBlockCoverage = false;
List<CoverageRange> ranges = new ArrayList<>();
for (CPUTracer.Payload payload : rootEntry.getValue()) {
isBlockCoverage |= payload.getTags().contains(StandardTags.StatementTag.class);
ranges.add(new CoverageRange(payload.getSourceSection().getCharIndex(), payload.getSourceSection().getCharEndIndex(), payload.getCount()));
}
functions.add(new FunctionCoverage(rootEntry.getKey(), isBlockCoverage, ranges.toArray(new CoverageRange[ranges.size()])));
});
int scriptId = slh.getScriptId(sourceEntry.getKey());
Script script = scriptId < 0 ? null : slh.getScript(scriptId);
return new ScriptCoverage(script != null ? script.getId() : 0, script != null ? script.getUrl() : "", functions.toArray(new FunctionCoverage[functions.size()]));
}).forEachOrdered(scriptCoverage -> {
result.put(scriptCoverage.toJSON());
});
json.put("result", result);
return new Params(json);
}
use of com.oracle.truffle.tools.chromeinspector.types.Script in project graal by oracle.
the class TruffleDebugger method getScriptSource.
@Override
public Params getScriptSource(String scriptId) throws CommandProcessException {
if (scriptId == null) {
throw new CommandProcessException("A scriptId required.");
}
Script script;
try {
script = slh.getScript(Integer.parseInt(scriptId));
if (script == null) {
throw new CommandProcessException("Unknown scriptId: " + scriptId);
}
} catch (NumberFormatException nfe) {
throw new CommandProcessException(nfe.getMessage());
}
JSONObject json = new JSONObject();
json.put("scriptSource", script.getSource().getCharacters());
return new Params(json);
}
use of com.oracle.truffle.tools.chromeinspector.types.Script in project graal by oracle.
the class TruffleProfiler method getTypeProfile.
private Params getTypeProfile(Collection<TypeHandler.SectionTypeProfile> profiles) {
JSONObject json = new JSONObject();
Map<Source, Collection<TypeHandler.SectionTypeProfile>> sourceToProfiles = new HashMap<>();
profiles.forEach(profile -> {
Collection<TypeHandler.SectionTypeProfile> pfs = sourceToProfiles.computeIfAbsent(profile.getSourceSection().getSource(), t -> new LinkedList<>());
pfs.add(profile);
});
JSONArray result = new JSONArray();
sourceToProfiles.entrySet().forEach(entry -> {
List<TypeProfileEntry> entries = new ArrayList<>();
entry.getValue().forEach(sectionProfile -> {
List<TypeObject> types = new ArrayList<>();
sectionProfile.getTypes().forEach(type -> {
types.add(new TypeObject(type));
});
if (!types.isEmpty()) {
entries.add(new TypeProfileEntry(sectionProfile.getSourceSection().getCharEndIndex(), types.toArray(new TypeObject[types.size()])));
}
});
int scriptId = slh.getScriptId(entry.getKey());
Script script = scriptId < 0 ? null : slh.getScript(scriptId);
result.put(new ScriptTypeProfile(script != null ? script.getId() : 0, script != null ? script.getUrl() : "", entries.toArray(new TypeProfileEntry[entries.size()])).toJSON());
});
json.put("result", result);
return new Params(json);
}
Aggregations