Search in sources :

Example 6 with SourceSection

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);
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) Script(com.oracle.truffle.tools.chromeinspector.types.Script) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) SourceSection(com.oracle.truffle.api.source.SourceSection) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(com.oracle.truffle.api.source.Source) Location(com.oracle.truffle.tools.chromeinspector.types.Location)

Example 7 with SourceSection

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()]);
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) Script(com.oracle.truffle.tools.chromeinspector.types.Script) ArrayList(java.util.ArrayList) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(com.oracle.truffle.api.source.Source) CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) TruffleException(com.oracle.truffle.api.TruffleException) NoSuspendedThreadException(com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.NoSuspendedThreadException) GuestLanguageException(com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.GuestLanguageException) DebugScope(com.oracle.truffle.api.debug.DebugScope) DebugScope(com.oracle.truffle.api.debug.DebugScope) Scope(com.oracle.truffle.tools.chromeinspector.types.Scope) CallFrame(com.oracle.truffle.tools.chromeinspector.types.CallFrame) SourceSection(com.oracle.truffle.api.source.SourceSection) PrintWriter(java.io.PrintWriter)

Example 8 with SourceSection

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);
}
Also used : Pattern(java.util.regex.Pattern) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) JSONObject(org.json.JSONObject) JSONArray(org.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 9 with SourceSection

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;
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection)

Example 10 with SourceSection

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();
    }
}
Also used : SourceElement(com.oracle.truffle.api.debug.SourceElement) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) StepConfig(com.oracle.truffle.api.debug.StepConfig) Source(org.graalvm.polyglot.Source) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SourceSection(com.oracle.truffle.api.source.SourceSection)

Aggregations

SourceSection (com.oracle.truffle.api.source.SourceSection)68 Test (org.junit.Test)23 Source (com.oracle.truffle.api.source.Source)15 Source (org.graalvm.polyglot.Source)12 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)11 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)8 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)8 AbstractInstrumentationTest (com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)6 Node (com.oracle.truffle.api.nodes.Node)6 RootNode (com.oracle.truffle.api.nodes.RootNode)6 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)5 DebugValue (com.oracle.truffle.api.debug.DebugValue)5 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)3 DebugScope (com.oracle.truffle.api.debug.DebugScope)3 Counter (com.oracle.truffle.api.instrumentation.test.examples.StatementProfilerExample.Counter)3 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)3 RootCallTarget (com.oracle.truffle.api.RootCallTarget)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 WrapperNode (com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode)2 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)2