Search in sources :

Example 1 with DebugProtocolClient

use of com.oracle.truffle.tools.dap.types.DebugProtocolClient in project graal by oracle.

the class BreakpointsHandler method setBreakpoints.

public List<com.oracle.truffle.tools.dap.types.Breakpoint> setBreakpoints(SetBreakpointsArguments args) {
    Source source;
    Integer sourceReference = args.getSource().getSourceReference();
    String path = args.getSource().getPath();
    String srcId;
    if (sourceReference != null && sourceReference > 0) {
        source = context.getLoadedSourcesHandler().getSource(sourceReference);
        srcId = (path != null ? path : "") + '#' + sourceReference;
    } else {
        source = context.getLoadedSourcesHandler().getSource(path);
        srcId = path;
    }
    Map<SourceBreakpoint, Integer> toAdd = new HashMap<>();
    Map<SourceBreakpoint, Integer> toRemove;
    synchronized (bp2IDs) {
        toRemove = sourceBreakpoints.put(srcId, toAdd);
    }
    List<com.oracle.truffle.tools.dap.types.Breakpoint> breakpoints = new ArrayList<>(args.getBreakpoints().size());
    if (source != null) {
        for (SourceBreakpoint sourceBreakpoint : args.getBreakpoints()) {
            Integer id;
            Breakpoint bp;
            SourceSection section;
            String message = null;
            synchronized (bp2IDs) {
                id = toRemove != null ? toRemove.remove(sourceBreakpoint) : null;
            }
            if (id == null) {
                Breakpoint.Builder builder = Breakpoint.newBuilder(source).lineIs(context.clientToDebuggerLine(sourceBreakpoint.getLine()));
                if (sourceBreakpoint.getColumn() != null) {
                    builder.columnIs(context.clientToDebuggerColumn(sourceBreakpoint.getColumn()));
                }
                bp = builder.resolveListener(resolvedHandler).build();
                if (sourceBreakpoint.getCondition() != null && !sourceBreakpoint.getCondition().isEmpty()) {
                    bp.setCondition(sourceBreakpoint.getCondition());
                }
                String[] hitCondition = null;
                if (sourceBreakpoint.getHitCondition() != null) {
                    String trimmedHitCondition = sourceBreakpoint.getHitCondition().trim();
                    if (!trimmedHitCondition.isEmpty()) {
                        Matcher matcher = HITCONDITION_REGEXP.matcher(trimmedHitCondition);
                        if (matcher.matches() && matcher.groupCount() == 2) {
                            if (matcher.group(0) == null) {
                                builder.ignoreCount(Integer.parseInt(matcher.group(2)));
                            } else {
                                hitCondition = new String[] { matcher.group(1), matcher.group(2) };
                            }
                        } else {
                            message = "Invalid hit condition: " + trimmedHitCondition;
                        }
                    }
                }
                bp = debuggerSession.install(bp);
                String logMessage = sourceBreakpoint.getLogMessage() != null ? sourceBreakpoint.getLogMessage().trim() : "";
                synchronized (bp2IDs) {
                    id = ++lastId;
                    bp2IDs.put(bp, id);
                    id2Bps.put(id, bp);
                    if (!logMessage.isEmpty()) {
                        logMessages.put(bp, logMessage);
                    }
                    if (hitCondition != null) {
                        hitConditions.put(bp, hitCondition);
                    }
                }
            } else {
                bp = id2Bps.get(id);
            }
            synchronized (bp2IDs) {
                toAdd.put(sourceBreakpoint, id);
                section = resolvedBreakpoints.get(bp);
            }
            if (section != null) {
                breakpoints.add(// 
                com.oracle.truffle.tools.dap.types.Breakpoint.create(message == null).setId(id).setMessage(message).setLine(context.debuggerToClientLine(section.getStartLine())).setColumn(// 
                context.debuggerToClientColumn(section.getStartColumn())).setEndLine(context.debuggerToClientLine(section.getEndLine())).setEndColumn(context.debuggerToClientColumn(section.getEndColumn())));
            } else {
                breakpoints.add(// 
                com.oracle.truffle.tools.dap.types.Breakpoint.create(false).setId(id).setMessage(message).setLine(sourceBreakpoint.getLine()).setColumn(sourceBreakpoint.getColumn()));
            }
        }
        if (toRemove != null) {
            for (Integer id : toRemove.values()) {
                synchronized (bp2IDs) {
                    Breakpoint bp = id2Bps.remove(id);
                    if (bp != null) {
                        bp.dispose();
                        bp2IDs.remove(bp);
                        logMessages.remove(bp);
                        hitConditions.remove(bp);
                        resolvedBreakpoints.remove(bp);
                    }
                }
            }
        }
    } else {
        context.getLoadedSourcesHandler().runOnLoad(args.getSource().getPath(), null);
        List<Consumer<Source>> tasks = new ArrayList<>(args.getBreakpoints().size());
        for (SourceBreakpoint sourceBreakpoint : args.getBreakpoints()) {
            Integer id;
            synchronized (bp2IDs) {
                id = toRemove != null ? toRemove.remove(sourceBreakpoint) : null;
                if (id == null) {
                    id = ++lastId;
                    toAdd.put(sourceBreakpoint, id);
                }
                final Integer finalId = id;
                final String logMessage = sourceBreakpoint.getLogMessage() != null ? sourceBreakpoint.getLogMessage().trim() : "";
                String[] hitCondition = null;
                String message = null;
                if (sourceBreakpoint.getHitCondition() != null) {
                    String trimmedHitCondition = sourceBreakpoint.getHitCondition().trim();
                    if (!trimmedHitCondition.isEmpty()) {
                        Matcher matcher = HITCONDITION_REGEXP.matcher(trimmedHitCondition);
                        if (matcher.matches() && matcher.groupCount() == 2) {
                            hitCondition = new String[] { matcher.group(1), matcher.group(2) };
                        } else {
                            message = "Invalid hit condition: " + trimmedHitCondition;
                        }
                    }
                }
                final String[] finalHitCondition = hitCondition;
                final String finalMessage = message;
                tasks.add((src) -> {
                    Breakpoint.Builder builder = Breakpoint.newBuilder(src).lineIs(context.clientToDebuggerLine(sourceBreakpoint.getLine()));
                    if (sourceBreakpoint.getColumn() != null) {
                        builder.columnIs(context.clientToDebuggerColumn(sourceBreakpoint.getColumn()));
                    }
                    Breakpoint bp = builder.resolveListener(resolvedHandler).build();
                    if (sourceBreakpoint.getCondition() != null && !sourceBreakpoint.getCondition().isEmpty()) {
                        bp.setCondition(sourceBreakpoint.getCondition());
                    }
                    bp = debuggerSession.install(bp);
                    SourceSection section;
                    synchronized (bp2IDs) {
                        bp2IDs.put(bp, finalId);
                        id2Bps.put(finalId, bp);
                        if (!logMessage.isEmpty()) {
                            logMessages.put(bp, logMessage);
                        }
                        if (finalHitCondition != null) {
                            hitConditions.put(bp, finalHitCondition);
                        }
                        section = resolvedBreakpoints.get(bp);
                    }
                    if (section != null) {
                        DebugProtocolClient client = context.getClient();
                        if (client != null) {
                            client.breakpoint(BreakpointEvent.EventBody.create("changed", // 
                            com.oracle.truffle.tools.dap.types.Breakpoint.create(finalMessage == null).setId(finalId).setMessage(finalMessage).setLine(context.debuggerToClientLine(section.getStartLine())).setColumn(// 
                            context.debuggerToClientColumn(section.getStartColumn())).setEndLine(context.debuggerToClientLine(section.getEndLine())).setEndColumn(context.debuggerToClientColumn(section.getEndColumn()))));
                        }
                    }
                });
                breakpoints.add(// 
                com.oracle.truffle.tools.dap.types.Breakpoint.create(false).setId(id).setMessage(finalMessage).setLine(sourceBreakpoint.getLine()).setColumn(sourceBreakpoint.getColumn()));
            }
        }
        if (!tasks.isEmpty()) {
            context.getLoadedSourcesHandler().runOnLoad(args.getSource().getPath(), (src) -> {
                for (Consumer<Source> task : tasks) {
                    task.accept(src);
                }
            });
        }
    }
    return breakpoints;
}
Also used : SourceBreakpoint(com.oracle.truffle.tools.dap.types.SourceBreakpoint) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) FunctionBreakpoint(com.oracle.truffle.tools.dap.types.FunctionBreakpoint) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Source(com.oracle.truffle.api.source.Source) SourceBreakpoint(com.oracle.truffle.tools.dap.types.SourceBreakpoint) Consumer(java.util.function.Consumer) DebugProtocolClient(com.oracle.truffle.tools.dap.types.DebugProtocolClient) SourceSection(com.oracle.truffle.api.source.SourceSection)

Example 2 with DebugProtocolClient

use of com.oracle.truffle.tools.dap.types.DebugProtocolClient in project graal by oracle.

the class ThreadsHandler method onThreadInitialized.

@Override
public void onThreadInitialized(TruffleContext ctx, Thread thread) {
    Integer id;
    synchronized (thread2Ids) {
        id = ++lastId;
        thread2Ids.put(thread, id);
        id2threads.put(id, thread);
    }
    DebugProtocolClient client = context.getClient();
    if (client != null) {
        client.thread(ThreadEvent.EventBody.create("started", id));
    }
}
Also used : DebugProtocolClient(com.oracle.truffle.tools.dap.types.DebugProtocolClient)

Example 3 with DebugProtocolClient

use of com.oracle.truffle.tools.dap.types.DebugProtocolClient in project graal by oracle.

the class LoadedSourcesHandler method assureLoaded.

public int assureLoaded(Source sourceLoaded) {
    Source sourceResolved = debuggerSession.resolveSource(sourceLoaded);
    Source source = (sourceResolved != null) ? sourceResolved : sourceLoaded;
    int id;
    Consumer<Source> task;
    synchronized (sourceIDs) {
        Integer eid = sourceIDs.get(source);
        if (eid != null) {
            return eid;
        }
        id = sources.size() + 1;
        sourceIDs.put(source, id);
        sources.add(source);
        task = toRunOnLoad.remove(getPath(source));
    }
    if (task != null) {
        task.accept(sourceLoaded);
    }
    DebugProtocolClient client = context.getClient();
    if (client != null) {
        client.loadedSource(LoadedSourceEvent.EventBody.create("new", from(source)));
    }
    return id;
}
Also used : DebugProtocolClient(com.oracle.truffle.tools.dap.types.DebugProtocolClient) Source(com.oracle.truffle.api.source.Source)

Example 4 with DebugProtocolClient

use of com.oracle.truffle.tools.dap.types.DebugProtocolClient in project graal by oracle.

the class ThreadsHandler method onThreadDisposed.

@Override
public void onThreadDisposed(TruffleContext ctx, Thread thread) {
    Integer id;
    synchronized (thread2Ids) {
        id = thread2Ids.remove(thread);
        id2threads.remove(id);
    }
    if (id != null) {
        DebugProtocolClient client = context.getClient();
        if (client != null) {
            client.thread(ThreadEvent.EventBody.create("exited", id));
        }
    }
}
Also used : DebugProtocolClient(com.oracle.truffle.tools.dap.types.DebugProtocolClient)

Example 5 with DebugProtocolClient

use of com.oracle.truffle.tools.dap.types.DebugProtocolClient in project graal by oracle.

the class ThreadsHandler method threadSuspended.

public void threadSuspended(Thread thread, SuspendedEvent event) {
    SuspendedThreadInfo info = null;
    synchronized (thread2Ids) {
        Integer id = thread2Ids.get(thread);
        if (id != null) {
            info = new SuspendedThreadInfo(id, event);
            suspendedThreads.put(id, info);
        }
    }
    if (info != null) {
        String reason = null;
        String description = null;
        List<String> logMessages = new ArrayList<>();
        boolean stop = true;
        for (Breakpoint bp : event.getBreakpoints()) {
            String logMessage = context.getBreakpointsHandler().getLogMessage(bp);
            if (logMessage != null) {
                logMessages.add(logMessage);
            }
            stop &= context.getBreakpointsHandler().checkConditions(bp, event.getTopStackFrame());
            switch(bp.getKind()) {
                case EXCEPTION:
                    reason = "exception";
                    description = "Paused on exception";
                    break;
                case HALT_INSTRUCTION:
                    reason = "debugger_statement";
                    description = "Paused on debugger statement";
                    break;
                case SOURCE_LOCATION:
                    reason = "breakpoint";
                    description = "Paused on breakpoint";
            }
        }
        if (stop) {
            if (logMessages.isEmpty()) {
                DebugException exception = event.getException();
                if (exception != null) {
                    boolean uncaught = exception.getCatchLocation() == null;
                    description = uncaught ? "Paused on uncaught exception" : "Paused on caught exception";
                }
                if (reason == null) {
                    reason = "debugger_statement";
                    description = "Paused on debugger statement";
                }
                DebugProtocolClient client = context.getClient();
                if (client != null) {
                    client.stopped(StoppedEvent.EventBody.create(reason).setThreadId(info.getThreadId()).setDescription(description));
                }
            } else {
                info.executables.add(i -> {
                    for (String logMessage : logMessages) {
                        Matcher matcher = LOGMESSAGE_VARIABLE_REGEXP.matcher(logMessage);
                        StringBuilder sb = new StringBuilder();
                        int idx = 0;
                        while (matcher.find()) {
                            String expression = matcher.group(1);
                            DebugValue value = VariablesHandler.getDebugValue(event.getTopStackFrame(), expression);
                            sb.append(logMessage.substring(idx, matcher.start())).append(value.toDisplayString());
                            idx = matcher.end();
                        }
                        sb.append(logMessage.substring(idx));
                        context.getInfo().println(sb.toString());
                    }
                    return true;
                });
            }
            info.runExecutables();
        }
    }
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebugValue(com.oracle.truffle.api.debug.DebugValue) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) DebugException(com.oracle.truffle.api.debug.DebugException) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebugProtocolClient(com.oracle.truffle.tools.dap.types.DebugProtocolClient)

Aggregations

DebugProtocolClient (com.oracle.truffle.tools.dap.types.DebugProtocolClient)5 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)2 Source (com.oracle.truffle.api.source.Source)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 DebugException (com.oracle.truffle.api.debug.DebugException)1 DebugValue (com.oracle.truffle.api.debug.DebugValue)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 FunctionBreakpoint (com.oracle.truffle.tools.dap.types.FunctionBreakpoint)1 SourceBreakpoint (com.oracle.truffle.tools.dap.types.SourceBreakpoint)1 HashMap (java.util.HashMap)1 Consumer (java.util.function.Consumer)1