Search in sources :

Example 1 with SourceBreakpoint

use of com.oracle.truffle.tools.dap.types.SourceBreakpoint 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)

Aggregations

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