Search in sources :

Example 6 with JSONObject

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

the class MultiEngineTest method checkSuspendAndResume.

private static void checkSuspendAndResume(String path) throws Exception {
    CountDownLatch closed = new CountDownLatch(1);
    AtomicBoolean paused = new AtomicBoolean(false);
    AtomicReference<Exception> exception = new AtomicReference<>(null);
    final String url = "ws://" + InetAddress.getLoopbackAddress().getHostAddress() + ":" + PORT + "/" + path;
    WebSocketClient wsc = new WebSocketClient(new URI(url)) {

        @Override
        public void onOpen(ServerHandshake sh) {
        }

        @Override
        public void onMessage(String message) {
            JSONObject msg = new JSONObject(message);
            if ("Debugger.paused".equals(msg.opt("method"))) {
                paused.set(true);
                send("{\"id\":5,\"method\":\"Debugger.resume\"}");
            }
        }

        @Override
        public void onClose(int i, String message, boolean bln) {
            closed.countDown();
        }

        @Override
        public void onError(Exception excptn) {
            excptn.printStackTrace();
            exception.set(excptn);
        }
    };
    final boolean connectionSucceeded = wsc.connectBlocking();
    assertTrue("Connection has not succeeded: " + url, connectionSucceeded);
    for (String message : INITIAL_MESSAGES) {
        wsc.send(message);
    }
    closed.await();
    wsc.close();
    if (exception.get() != null) {
        throw exception.get();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerHandshake(com.oracle.truffle.tools.utils.java_websocket.handshake.ServerHandshake) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocketClient(com.oracle.truffle.tools.utils.java_websocket.client.WebSocketClient) URI(java.net.URI) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 7 with JSONObject

use of com.oracle.truffle.tools.utils.json.JSONObject 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 8 with JSONObject

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

the class BreakpointsHandler method createFunctionBreakpoint.

Params createFunctionBreakpoint(DebugValue functionValue, String condition) {
    SourceSection functionLocation = functionValue.getSourceLocation();
    Breakpoint.Builder builder;
    if (functionLocation != null) {
        builder = Breakpoint.newBuilder(functionLocation);
    } else {
        builder = Breakpoint.newBuilder((URI) null);
    }
    builder.rootInstance(functionValue);
    builder.sourceElements(SourceElement.ROOT);
    Breakpoint bp = builder.build();
    if (condition != null && !condition.isEmpty()) {
        bp.setCondition(condition);
    }
    ds.install(bp);
    long id;
    synchronized (bpIDs) {
        id = ++lastID;
        bpIDs.put(bp, id);
    }
    JSONObject json = new JSONObject();
    json.put("breakpointId", Long.toString(id));
    return new Params(json);
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) SourceSection(com.oracle.truffle.api.source.SourceSection) URI(java.net.URI)

Example 9 with JSONObject

use of com.oracle.truffle.tools.utils.json.JSONObject 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 10 with JSONObject

use of com.oracle.truffle.tools.utils.json.JSONObject 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)

Aggregations

JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)318 JSONArray (com.oracle.truffle.tools.utils.json.JSONArray)71 ArrayList (java.util.ArrayList)20 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)18 DebugValue (com.oracle.truffle.api.debug.DebugValue)14 DebugException (com.oracle.truffle.api.debug.DebugException)11 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)10 SourceSection (com.oracle.truffle.api.source.SourceSection)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Collection (java.util.Collection)7 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)6 Source (com.oracle.truffle.api.source.Source)6 Map (java.util.Map)6 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)5 NoSuspendedThreadException (com.oracle.truffle.tools.chromeinspector.InspectorExecutionContext.NoSuspendedThreadException)5 RemoteObject (com.oracle.truffle.tools.chromeinspector.types.RemoteObject)5 JSONTokener (com.oracle.truffle.tools.utils.json.JSONTokener)5 Test (org.junit.Test)5 Location (com.oracle.truffle.tools.chromeinspector.types.Location)4