Search in sources :

Example 16 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class RemoteObject method createMapEntryElementDescription.

private static String createMapEntryElementDescription(List<DebugValue> keyAndValue, int index, boolean allowSideEffects, PrintWriter err) {
    try {
        DebugValue v = keyAndValue.get(index);
        String toString = v.toDisplayString(allowSideEffects);
        if (v.isString()) {
            toString = "\"" + toString + "\"";
        }
        return toString;
    } catch (DebugException ex) {
        if (err != null) {
            ex.printStackTrace(err);
        }
        return "?";
    }
}
Also used : DebugValue(com.oracle.truffle.api.debug.DebugValue) DebugException(com.oracle.truffle.api.debug.DebugException)

Example 17 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class InspectorRuntime method compileScript.

@Override
public Params compileScript(String expression, String sourceURL, boolean persistScript, long executionContextId) throws CommandProcessException {
    if (expression == null) {
        throw new CommandProcessException("An expression required.");
    }
    JSONObject ret = new JSONObject();
    Source source = createSource(expression, sourceURL);
    boolean parsed = false;
    String[] exceptionText = new String[1];
    if (context.getSuspendedInfo() != null) {
        try {
            parsed = context.executeInSuspendThread(new SuspendThreadExecutable<Boolean>() {

                @Override
                public Boolean executeCommand() throws CommandProcessException {
                    LanguageInfo languageInfo = context.getSuspendedInfo().getSuspendedEvent().getTopStackFrame().getLanguage();
                    if (languageInfo == null || !languageInfo.isInteractive()) {
                        exceptionText[0] = InspectorDebugger.getEvalNonInteractiveMessage();
                        return false;
                    }
                    try {
                        context.getEnv().parse(source);
                        return true;
                    } catch (ThreadDeath td) {
                        throw td;
                    } catch (Throwable ex) {
                        // Didn't manage to parse this
                        exceptionText[0] = ex.getLocalizedMessage();
                        return false;
                    }
                }

                @Override
                public Boolean processException(DebugException ex) {
                    fillExceptionDetails(ret, ex);
                    return false;
                }
            });
        } catch (NoSuspendedThreadException ex) {
            exceptionText[0] = ex.getLocalizedMessage();
        }
    } else {
        // Parse on the current thread will fail most likely due to a lack of context
        parsed = false;
        exceptionText[0] = "<Not suspended>";
    }
    if (parsed && persistScript) {
        int id = slh.assureLoaded(source).getId();
        ret.put("scriptId", Integer.toString(id));
    }
    if (exceptionText[0] != null) {
        fillExceptionDetails(ret, exceptionText[0]);
    }
    return new Params(ret);
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) NoSuspendedThreadException(com.oracle.truffle.tools.chromeinspector.InspectorExecutionContext.NoSuspendedThreadException) DebugException(com.oracle.truffle.api.debug.DebugException) Source(com.oracle.truffle.api.source.Source) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject)

Example 18 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class InspectorRuntime method putResultProperties.

private void putResultProperties(JSONObject json, DebugValue value, Collection<DebugValue> properties, Collection<DebugValue> arrayElements, boolean generatePreview, String objectGroup) {
    final String functionLocation = "[[FunctionLocation]]";
    JSONArray result = new JSONArray();
    JSONArray internals = new JSONArray();
    boolean hasArray = !arrayElements.isEmpty();
    HashSet<String> storedPropertyNames = (hasArray && properties != null) ? new HashSet<>(properties.size()) : null;
    DebugException exception = null;
    String nameExc = null;
    // Test functionLocation for executable values only
    boolean hasFunctionLocation = value == null || !value.canExecute();
    try {
        boolean isJS = false;
        if (properties != null) {
            LanguageInfo language = (value != null) ? value.getOriginalLanguage() : null;
            isJS = LanguageChecks.isJS(language);
            Iterator<DebugValue> propertiesIterator = properties.iterator();
            while (propertiesIterator.hasNext()) {
                DebugValue v = null;
                try {
                    v = propertiesIterator.next();
                    if (v.isReadable()) {
                        if (!v.isInternal()) {
                            result.put(createPropertyJSON(v, generatePreview, objectGroup));
                            if (storedPropertyNames != null) {
                                storedPropertyNames.add(v.getName());
                            }
                        } else {
                            internals.put(createPropertyJSON(v, generatePreview, objectGroup));
                        }
                        if (!hasFunctionLocation && functionLocation.equals(v.getName())) {
                            hasFunctionLocation = true;
                        }
                    }
                } catch (DebugException ex) {
                    if (exception == null) {
                        exception = ex;
                        nameExc = (v != null) ? v.getName() : "<unknown>";
                    }
                }
            }
        }
        int i = 0;
        for (DebugValue v : arrayElements) {
            String name = Integer.toString(i++);
            try {
                if (v.isReadable() && (storedPropertyNames == null || !storedPropertyNames.contains(name))) {
                    result.put(createPropertyJSON(v, name, generatePreview, objectGroup));
                }
            } catch (DebugException ex) {
                if (exception == null) {
                    exception = ex;
                    nameExc = name;
                }
            }
        }
        if (isJS) {
            // Add __proto__ when in JavaScript:
            DebugValue prototype = value.getProperty("__proto__");
            if (prototype != null && !prototype.isNull()) {
                result.put(createPropertyJSON(prototype, null, generatePreview, true, false, objectGroup, false, null));
            }
        }
        if (value != null && value.hasHashEntries()) {
            DebugValue entries = value.getHashEntriesIterator();
            JSONObject map2 = createPropertyJSON(entries, "[[Entries]]", false, false, true, objectGroup, true, TypeMark.MAP_ENTRIES);
            internals.put(map2);
        }
    } catch (DebugException ex) {
        // From property iterators, etc.
        if (exception == null) {
            exception = ex;
        }
    }
    if (!hasFunctionLocation) {
        SourceSection sourceLocation = null;
        try {
            sourceLocation = value.getSourceLocation();
        } catch (DebugException ex) {
            // From property iterators, etc.
            if (exception == null) {
                exception = ex;
            }
        }
        if (sourceLocation != null) {
            int scriptId = slh.getScriptId(sourceLocation.getSource());
            if (scriptId >= 0) {
                // {"name":"[[FunctionLocation]]","value":{"type":"object","subtype":"internal#location","value":{"scriptId":"87","lineNumber":17,"columnNumber":26},"description":"Object"}}
                JSONObject location = new JSONObject();
                location.put("name", functionLocation);
                JSONObject locationValue = new JSONObject();
                locationValue.put("type", "object");
                locationValue.put("subtype", "internal#location");
                locationValue.put("description", "Object");
                locationValue.put("value", new Location(scriptId, sourceLocation.getStartLine(), sourceLocation.getStartColumn()).toJSON());
                location.put("value", locationValue);
                internals.put(location);
            }
        }
    }
    json.put("result", result);
    json.put("internalProperties", internals);
    if (exception != null) {
        fillExceptionDetails(json, exception);
        if (exception.isInternalError()) {
            PrintWriter err = context.getErr();
            if (err != null) {
                err.println("Exception while retrieving variable " + nameExc);
                exception.printStackTrace(err);
            }
        }
    }
}
Also used : DebugValue(com.oracle.truffle.api.debug.DebugValue) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) DebugException(com.oracle.truffle.api.debug.DebugException) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) SourceSection(com.oracle.truffle.api.source.SourceSection) Location(com.oracle.truffle.tools.chromeinspector.types.Location) PrintWriter(java.io.PrintWriter)

Example 19 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class InspectorDebugger method evaluateOnCallFrame.

@Override
public Params evaluateOnCallFrame(String callFrameId, String expressionOrig, String objectGroup, boolean includeCommandLineAPI, boolean silent, boolean returnByValue, boolean generatePreview, boolean throwOnSideEffect) throws CommandProcessException {
    if (callFrameId == null) {
        throw new CommandProcessException("A callFrameId required.");
    }
    if (expressionOrig == null) {
        throw new CommandProcessException("An expression required.");
    }
    int frameId;
    try {
        frameId = Integer.parseInt(callFrameId);
    } catch (NumberFormatException ex) {
        throw new CommandProcessException(ex.getLocalizedMessage());
    }
    ConsoleUtilitiesAPI cuAPI;
    if (includeCommandLineAPI) {
        cuAPI = ConsoleUtilitiesAPI.parse(expressionOrig);
    } else {
        cuAPI = null;
    }
    final String expression;
    if (cuAPI != null) {
        expression = cuAPI.getExpression();
    } else {
        expression = expressionOrig;
    }
    JSONObject jsonResult;
    try {
        jsonResult = context.executeInSuspendThread(new SuspendThreadExecutable<JSONObject>() {

            @Override
            public JSONObject executeCommand() throws CommandProcessException {
                if (frameId >= suspendedInfo.getCallFrames().length) {
                    throw new CommandProcessException("Too big callFrameId: " + frameId);
                }
                CallFrame cf = suspendedInfo.getCallFrames()[frameId];
                JSONObject json = new JSONObject();
                if (runSpecialFunctions(expression, cf, json)) {
                    return json;
                }
                DebugValue value = getVarValue(expression, cf);
                if (value == null) {
                    try {
                        value = cf.getFrame().eval(expression);
                        suspendedInfo.refreshFrames();
                    } catch (IllegalStateException ex) {
                    // Not an interactive language
                    }
                }
                if (value == null) {
                    LanguageInfo languageInfo = cf.getFrame().getLanguage();
                    if (languageInfo == null || !languageInfo.isInteractive()) {
                        String errorMessage = getEvalNonInteractiveMessage();
                        ExceptionDetails exceptionDetails = new ExceptionDetails(errorMessage);
                        json.put("exceptionDetails", exceptionDetails.createJSON(context));
                        JSONObject err = new JSONObject();
                        err.putOpt("value", errorMessage);
                        err.putOpt("type", "string");
                        json.put("result", err);
                    }
                }
                if (value != null) {
                    if (cuAPI != null) {
                        value = cuAPI.process(value, breakpointsHandler);
                        if (value == null) {
                            return json;
                        }
                    }
                    RemoteObject ro = new RemoteObject(value, generatePreview, context);
                    context.getRemoteObjectsHandler().register(ro, objectGroup);
                    json.put("result", ro.toJSON());
                }
                return json;
            }

            @Override
            public JSONObject processException(DebugException dex) {
                JSONObject json = new JSONObject();
                InspectorRuntime.fillExceptionDetails(json, dex, context);
                DebugValue exceptionObject = dex.getExceptionObject();
                if (exceptionObject != null) {
                    RemoteObject ro = context.createAndRegister(exceptionObject, generatePreview);
                    json.put("result", ro.toJSON());
                } else {
                    JSONObject err = new JSONObject();
                    err.putOpt("value", dex.getLocalizedMessage());
                    err.putOpt("type", "string");
                    json.put("result", err);
                }
                return json;
            }
        });
    } catch (NoSuspendedThreadException e) {
        jsonResult = new JSONObject();
        JSONObject err = new JSONObject();
        err.putOpt("value", e.getLocalizedMessage());
        jsonResult.put("result", err);
    }
    return new Params(jsonResult);
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) DebugValue(com.oracle.truffle.api.debug.DebugValue) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) ExceptionDetails(com.oracle.truffle.tools.chromeinspector.types.ExceptionDetails) NoSuspendedThreadException(com.oracle.truffle.tools.chromeinspector.InspectorExecutionContext.NoSuspendedThreadException) DebugException(com.oracle.truffle.api.debug.DebugException) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) RemoteObject(com.oracle.truffle.tools.chromeinspector.types.RemoteObject) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) CallFrame(com.oracle.truffle.tools.chromeinspector.types.CallFrame)

Example 20 with DebugException

use of com.oracle.truffle.api.debug.DebugException in project graal by oracle.

the class InspectorRuntime method createCodecompletion.

static JSONObject createCodecompletion(DebugValue value, DebugScope scope, InspectorExecutionContext context, boolean resultItems) {
    JSONObject result = new JSONObject();
    Iterable<DebugValue> properties = null;
    try {
        if (value != null) {
            properties = value.getProperties();
        } else {
            properties = scope.getDeclaredValues();
        }
    } catch (DebugException ex) {
        fillExceptionDetails(result, ex, context);
        if (ex.isInternalError()) {
            PrintWriter err = context.getErr();
            if (err != null) {
                err.println("getProperties(" + ((value != null) ? value.getName() : scope.getName()) + ") has caused: " + ex);
                ex.printStackTrace(err);
            }
        }
    }
    JSONArray valueArray = new JSONArray();
    JSONArray items = new JSONArray();
    if (properties != null) {
        for (DebugValue property : properties) {
            items.put(property.getName());
        }
    }
    if (resultItems) {
        JSONObject itemsObj = new JSONObject();
        itemsObj.put("items", items);
        valueArray.put(itemsObj);
    } else {
        valueArray.put(items);
    }
    result.put("type", "object");
    result.put("value", valueArray);
    return result;
}
Also used : JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) DebugValue(com.oracle.truffle.api.debug.DebugValue) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) DebugException(com.oracle.truffle.api.debug.DebugException) PrintWriter(java.io.PrintWriter)

Aggregations

DebugException (com.oracle.truffle.api.debug.DebugException)34 DebugValue (com.oracle.truffle.api.debug.DebugValue)20 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)12 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)12 JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)12 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)11 Source (org.graalvm.polyglot.Source)11 SourceSection (com.oracle.truffle.api.source.SourceSection)10 Test (org.junit.Test)10 PrintWriter (java.io.PrintWriter)8 DebugScope (com.oracle.truffle.api.debug.DebugScope)7 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)7 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)6 NoSuspendedThreadException (com.oracle.truffle.tools.chromeinspector.InspectorExecutionContext.NoSuspendedThreadException)6 JSONArray (com.oracle.truffle.tools.utils.json.JSONArray)6 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)5 RemoteObject (com.oracle.truffle.tools.chromeinspector.types.RemoteObject)5 ArrayList (java.util.ArrayList)5 CatchLocation (com.oracle.truffle.api.debug.DebugException.CatchLocation)3 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)3