Search in sources :

Example 6 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray 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() : ScriptsHandler.compareURLs((String) url, script.getUrl())) {
                Breakpoint bp = createBuilder(script.getSourceLoaded(), 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);
}
Also used : Pattern(java.util.regex.Pattern) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) SourceSection(com.oracle.truffle.api.source.SourceSection) LoadScriptListener(com.oracle.truffle.tools.chromeinspector.ScriptsHandler.LoadScriptListener) Location(com.oracle.truffle.tools.chromeinspector.types.Location)

Example 7 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class InspectorRuntime method putMapEntries.

private void putMapEntries(JSONObject json, DebugValue value, RemoteObject.IndexRange indexRange, boolean generatePreview, String objectGroup) {
    int start = 0;
    int end = Integer.MAX_VALUE;
    if (indexRange != null && !indexRange.isNamed()) {
        start = indexRange.start();
        end = indexRange.end();
    }
    JSONArray result = new JSONArray();
    for (int index = 0; value.hasIteratorNextElement() && index < end; index++) {
        DebugValue next = value.getIteratorNextElement();
        if (index < start) {
            continue;
        }
        String name = Integer.toString(index);
        JSONObject jsonEntry = createPropertyJSON(next, name, generatePreview, objectGroup, TypeMark.MAP_ENTRY);
        result.put(jsonEntry);
    }
    json.put("result", result);
}
Also used : DebugValue(com.oracle.truffle.api.debug.DebugValue) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray)

Example 8 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class InspectorRuntime method getProperties.

@Override
public Params getProperties(String objectId, boolean ownProperties, boolean accessorPropertiesOnly, boolean generatePreview) throws CommandProcessException {
    if (objectId == null) {
        throw new CommandProcessException("An objectId required.");
    }
    RemoteObject object = context.getRemoteObjectsHandler().getRemote(objectId);
    String objectGroup = context.getRemoteObjectsHandler().getObjectGroupOf(objectId);
    JSONObject json = new JSONObject();
    if (object != null) {
        DebugValue value = object.getDebugValue();
        RemoteObject.IndexRange indexRange = object.getIndexRange();
        try {
            if (value != null) {
                context.executeInSuspendThread(new SuspendThreadExecutable<Void>() {

                    @Override
                    public Void executeCommand() throws CommandProcessException {
                        TypeMark typeMark = object.getTypeMark();
                        if (typeMark != null) {
                            switch(typeMark) {
                                case MAP_ENTRIES:
                                    putMapEntries(json, value, indexRange, generatePreview, objectGroup);
                                    break;
                                case MAP_ENTRY:
                                    putMapEntry(json, value, generatePreview, objectGroup);
                                    break;
                                default:
                                    throw new CommandProcessException("Unknown type mark " + typeMark);
                            }
                            return null;
                        }
                        Collection<DebugValue> properties = null;
                        if (!value.hasHashEntries()) {
                            properties = value.getProperties();
                        }
                        if (properties == null) {
                            properties = Collections.emptyList();
                        } else if (indexRange != null && indexRange.isNamed()) {
                            List<DebugValue> list = new ArrayList<>(properties);
                            properties = list.subList(indexRange.start(), indexRange.end());
                        }
                        Collection<DebugValue> array;
                        if (!value.isArray()) {
                            array = Collections.emptyList();
                        } else if (indexRange != null && !indexRange.isNamed()) {
                            List<DebugValue> arr = value.getArray();
                            array = arr.subList(indexRange.start(), indexRange.end());
                        } else {
                            array = value.getArray();
                        }
                        putResultProperties(json, value, properties, array, generatePreview, objectGroup);
                        return null;
                    }

                    @Override
                    public Void processException(DebugException ex) {
                        fillExceptionDetails(json, ex);
                        return null;
                    }
                });
            } else {
                final DebugScope scope = object.getScope();
                context.executeInSuspendThread(new SuspendThreadExecutable<Void>() {

                    @Override
                    public Void executeCommand() throws CommandProcessException {
                        Collection<DebugValue> properties = new ArrayList<>();
                        DebugValue scopeReceiver = object.getScopeReceiver();
                        if (scopeReceiver != null) {
                            properties.add(scopeReceiver);
                        }
                        for (DebugValue p : scope.getDeclaredValues()) {
                            properties.add(p);
                        }
                        putResultProperties(json, null, properties, Collections.emptyList(), generatePreview, objectGroup);
                        return null;
                    }

                    @Override
                    public Void processException(DebugException ex) {
                        fillExceptionDetails(json, ex);
                        return null;
                    }
                });
            }
        } catch (NoSuspendedThreadException ex) {
            // Not suspended, no properties
            json.put("result", new JSONArray());
        }
    }
    return new Params(json);
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) TypeMark(com.oracle.truffle.tools.chromeinspector.types.RemoteObject.TypeMark) DebugValue(com.oracle.truffle.api.debug.DebugValue) ArrayList(java.util.ArrayList) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) NoSuspendedThreadException(com.oracle.truffle.tools.chromeinspector.InspectorExecutionContext.NoSuspendedThreadException) DebugException(com.oracle.truffle.api.debug.DebugException) DebugScope(com.oracle.truffle.api.debug.DebugScope) RemoteObject(com.oracle.truffle.tools.chromeinspector.types.RemoteObject) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) Collection(java.util.Collection)

Example 9 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class InspectorProfiler method getCoverage.

private Params getCoverage(Collection<CPUTracer.Payload> payloads) {
    JSONObject json = new JSONObject();
    Map<Source, Map<String, Collection<CPUTracer.Payload>>> sourceToRoots = new LinkedHashMap<>();
    payloads.forEach(payload -> {
        SourceSection sourceSection = payload.getSourceSection();
        if (sourceSection != null) {
            Map<String, Collection<CPUTracer.Payload>> rootsToPayloads = sourceToRoots.computeIfAbsent(sourceSection.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()])));
        });
        Script script = slh.assureLoaded(sourceEntry.getKey());
        return new ScriptCoverage(script.getId(), script.getUrl(), functions.toArray(new FunctionCoverage[functions.size()]));
    }).forEachOrdered(scriptCoverage -> {
        result.put(scriptCoverage.toJSON());
    });
    json.put("result", result);
    return new Params(json);
}
Also used : ProfileNode(com.oracle.truffle.tools.chromeinspector.types.ProfileNode) StandardTags(com.oracle.truffle.api.instrumentation.StandardTags) TypeProfileInstrument(com.oracle.truffle.tools.chromeinspector.instrument.TypeProfileInstrument) CoverageRange(com.oracle.truffle.tools.chromeinspector.types.CoverageRange) Script(com.oracle.truffle.tools.chromeinspector.types.Script) HashMap(java.util.HashMap) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) Enabler(com.oracle.truffle.tools.chromeinspector.instrument.Enabler) TypeProfileEntry(com.oracle.truffle.tools.chromeinspector.types.TypeProfileEntry) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) FunctionCoverage(com.oracle.truffle.tools.chromeinspector.types.FunctionCoverage) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) Map(java.util.Map) ScriptTypeProfile(com.oracle.truffle.tools.chromeinspector.types.ScriptTypeProfile) CPUSamplerData(com.oracle.truffle.tools.profiler.CPUSamplerData) CPUTracer(com.oracle.truffle.tools.profiler.CPUTracer) SourceSection(com.oracle.truffle.api.source.SourceSection) LinkedList(java.util.LinkedList) ProfilerDomain(com.oracle.truffle.tools.chromeinspector.domains.ProfilerDomain) RuntimeCallFrame(com.oracle.truffle.tools.chromeinspector.types.RuntimeCallFrame) Profile(com.oracle.truffle.tools.chromeinspector.types.Profile) Collection(java.util.Collection) ProfilerNode(com.oracle.truffle.tools.profiler.ProfilerNode) InstrumentInfo(com.oracle.truffle.api.InstrumentInfo) ScriptCoverage(com.oracle.truffle.tools.chromeinspector.types.ScriptCoverage) TruffleContext(com.oracle.truffle.api.TruffleContext) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Source(com.oracle.truffle.api.source.Source) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) CPUSamplerInstrument(com.oracle.truffle.tools.profiler.impl.CPUSamplerInstrument) CPUSampler(com.oracle.truffle.tools.profiler.CPUSampler) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ConnectionWatcher(com.oracle.truffle.tools.chromeinspector.server.ConnectionWatcher) TypeObject(com.oracle.truffle.tools.chromeinspector.types.TypeObject) CPUTracerInstrument(com.oracle.truffle.tools.profiler.impl.CPUTracerInstrument) Script(com.oracle.truffle.tools.chromeinspector.types.Script) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) Source(com.oracle.truffle.api.source.Source) StandardTags(com.oracle.truffle.api.instrumentation.StandardTags) LinkedHashMap(java.util.LinkedHashMap) ScriptCoverage(com.oracle.truffle.tools.chromeinspector.types.ScriptCoverage) FunctionCoverage(com.oracle.truffle.tools.chromeinspector.types.FunctionCoverage) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) CPUTracer(com.oracle.truffle.tools.profiler.CPUTracer) CoverageRange(com.oracle.truffle.tools.chromeinspector.types.CoverageRange) Collection(java.util.Collection) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) SourceSection(com.oracle.truffle.api.source.SourceSection) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class GDSChannel method loadArtifacts.

/**
 * Loads the release index. Must be loaded from a local file.
 *
 * @param releasesIndexPath path to the downloaded releases index.
 * @return list of entries in the index
 * @throws IOException in case of I/O error.
 */
List<ComponentInfo> loadArtifacts(Path releasesIndexPath) throws IOException {
    if (edition == null) {
        edition = localRegistry.getGraalCapabilities().get(CommonConstants.CAP_EDITION);
    }
    List<ComponentInfo> result = new ArrayList<>();
    try (InputStreamReader urlReader = new InputStreamReader(new GZIPInputStream(Files.newInputStream(releasesIndexPath)))) {
        JSONTokener tokener = new JSONTokener(urlReader);
        JSONObject obj = new JSONObject(tokener);
        JSONArray releases = obj.getJSONArray(JSON_ITEMS);
        if (releases == null) {
            // malformed releases file;
            throw new IncompatibleException(fb.l10n("OLDS_InvalidReleasesFile"));
        }
        Version v = localRegistry.getGraalVersion();
        for (Object k : releases) {
            JSONObject jo = (JSONObject) k;
            ArtifactParser e;
            try {
                e = new ArtifactParser(jo);
            } catch (JSONException | IllegalArgumentException ex) {
                fb.error("OLDS_ErrorReadingRelease", ex, k, ex.getLocalizedMessage());
                continue;
            }
            if (!OS.get().equals(OS.fromName(e.getOs()))) {
                LOG.log(Level.FINER, "Incorrect OS: {0}", k);
            } else if (!ARCH.get().equals(ARCH.fromName(e.getArch()))) {
                LOG.log(Level.FINER, "Incorrect Arch: {0}", k);
            } else if (!localRegistry.getJavaVersion().equals(e.getJava())) {
                LOG.log(Level.FINER, "Incorrect Java: {0}", k);
            } else if (edition != null && !edition.equals(e.getEdition())) {
                LOG.log(Level.FINER, "Incorrect edition: {0}", k);
            } else if (!acceptsVersion(v, Version.fromString(e.getVersion()))) {
                LOG.log(Level.FINER, "Old version: {0} != {1}", new Object[] { v, Version.fromString(e.getVersion()), e.getVersion() });
            } else {
                result.add(e.asComponentInfo(gdsConnector, fb));
            }
        }
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) JSONException(com.oracle.truffle.tools.utils.json.JSONException) GZIPInputStream(java.util.zip.GZIPInputStream) JSONTokener(com.oracle.truffle.tools.utils.json.JSONTokener) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) Version(org.graalvm.component.installer.Version) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) ComponentInfo(org.graalvm.component.installer.model.ComponentInfo) IncompatibleException(org.graalvm.component.installer.IncompatibleException)

Aggregations

JSONArray (com.oracle.truffle.tools.utils.json.JSONArray)195 JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)85 ArrayList (java.util.ArrayList)74 DebugValue (com.oracle.truffle.api.debug.DebugValue)8 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)8 Collection (java.util.Collection)7 List (java.util.List)7 DebugException (com.oracle.truffle.api.debug.DebugException)6 Source (com.oracle.truffle.api.source.Source)5 SourceSection (com.oracle.truffle.api.source.SourceSection)4 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)4 HashMap (java.util.HashMap)4 Location (com.oracle.truffle.tools.chromeinspector.types.Location)3 Script (com.oracle.truffle.tools.chromeinspector.types.Script)3 CPUSampler (com.oracle.truffle.tools.profiler.CPUSampler)3 CPUSamplerData (com.oracle.truffle.tools.profiler.CPUSamplerData)3 PrintWriter (java.io.PrintWriter)3 Map (java.util.Map)3 TruffleContext (com.oracle.truffle.api.TruffleContext)2 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)2