Search in sources :

Example 11 with DebuggerException

use of org.eclipse.che.api.debugger.server.exceptions.DebuggerException in project che by eclipse.

the class JavaDebuggerUtils method findFqnByPosition.

/**
     * Return nested class fqn if line with number {@code lineNumber} contains such element, otherwise return outer class fqn.
     *
     * @param projectPath
     *         project path which contains class with {@code outerClassFqn}
     * @param outerClassFqn
     *         fqn outer class
     * @param lineNumber
     *         line position to search
     * @throws DebuggerException
     */
public String findFqnByPosition(String projectPath, String outerClassFqn, int lineNumber) throws DebuggerException {
    if (projectPath == null) {
        return outerClassFqn;
    }
    IJavaProject project = MODEL.getJavaProject(projectPath);
    IType outerClass;
    IMember iMember;
    try {
        outerClass = project.findType(outerClassFqn);
        if (outerClass == null) {
            return outerClassFqn;
        }
        String source;
        if (outerClass.isBinary()) {
            IClassFile classFile = outerClass.getClassFile();
            source = classFile.getSource();
        } else {
            ICompilationUnit unit = outerClass.getCompilationUnit();
            source = unit.getSource();
        }
        Document document = new Document(source);
        IRegion region = document.getLineInformation(lineNumber);
        int start = region.getOffset();
        int end = start + region.getLength();
        iMember = binSearch(outerClass, start, end);
    } catch (JavaModelException e) {
        throw new DebuggerException(format("Unable to find source for class with fqn '%s' in the project '%s'", outerClassFqn, project), e);
    } catch (BadLocationException e) {
        throw new DebuggerException("Unable to calculate breakpoint location", e);
    }
    if (iMember instanceof IType) {
        return ((IType) iMember).getFullyQualifiedName();
    }
    if (iMember != null) {
        return iMember.getDeclaringType().getFullyQualifiedName();
    }
    return outerClassFqn;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClassFile(org.eclipse.jdt.core.IClassFile) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) Document(org.eclipse.jface.text.Document) IMember(org.eclipse.jdt.core.IMember) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) IType(org.eclipse.jdt.core.IType)

Example 12 with DebuggerException

use of org.eclipse.che.api.debugger.server.exceptions.DebuggerException in project che by eclipse.

the class JavaDebugger method resume.

@Override
public void resume(ResumeAction action) throws DebuggerException {
    lock.lock();
    try {
        invalidateCurrentThread();
        vm.resume();
        LOG.debug("Resume VM");
    } catch (VMCannotBeModifiedException e) {
        throw new DebuggerException(e.getMessage(), e);
    } finally {
        lock.unlock();
    }
}
Also used : DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) VMCannotBeModifiedException(com.sun.jdi.VMCannotBeModifiedException)

Example 13 with DebuggerException

use of org.eclipse.che.api.debugger.server.exceptions.DebuggerException in project che by eclipse.

the class JavaDebugger method addBreakpoint.

@Override
public void addBreakpoint(Breakpoint breakpoint) throws DebuggerException {
    final String className = findFQN(breakpoint);
    final int lineNumber = breakpoint.getLocation().getLineNumber();
    List<ReferenceType> classes = vm.classesByName(className);
    // it may mean that class doesn't loaded by a target JVM yet
    if (classes.isEmpty()) {
        deferBreakpoint(breakpoint);
        throw new DebuggerException("Class not loaded");
    }
    ReferenceType clazz = classes.get(0);
    List<com.sun.jdi.Location> locations;
    try {
        locations = clazz.locationsOfLine(lineNumber);
    } catch (AbsentInformationException | ClassNotPreparedException e) {
        throw new DebuggerException(e.getMessage(), e);
    }
    if (locations.isEmpty()) {
        throw new DebuggerException("Line " + lineNumber + " not found in class " + className);
    }
    com.sun.jdi.Location location = locations.get(0);
    if (location.method() == null) {
        // Line is out of method.
        throw new DebuggerException("Invalid line " + lineNumber + " in class " + className);
    }
    // Ignore new breakpoint if already have breakpoint at the same location.
    EventRequestManager requestManager = getEventManager();
    for (BreakpointRequest breakpointRequest : requestManager.breakpointRequests()) {
        if (location.equals(breakpointRequest.location())) {
            LOG.debug("Breakpoint at {} already set", location);
            return;
        }
    }
    try {
        EventRequest breakPointRequest = requestManager.createBreakpointRequest(location);
        breakPointRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
        String expression = breakpoint.getCondition();
        if (!(expression == null || expression.isEmpty())) {
            ExpressionParser parser = ExpressionParser.newInstance(expression);
            breakPointRequest.putProperty("org.eclipse.che.ide.java.debug.condition.expression.parser", parser);
        }
        breakPointRequest.setEnabled(true);
    } catch (NativeMethodException | IllegalThreadStateException | InvalidRequestStateException e) {
        throw new DebuggerException(e.getMessage(), e);
    }
    debuggerCallback.onEvent(new BreakpointActivatedEventImpl(new BreakpointImpl(breakpoint.getLocation(), true, breakpoint.getCondition())));
    LOG.debug("Add breakpoint: {}", location);
}
Also used : NativeMethodException(com.sun.jdi.NativeMethodException) BreakpointImpl(org.eclipse.che.api.debug.shared.model.impl.BreakpointImpl) AbsentInformationException(com.sun.jdi.AbsentInformationException) DebuggerAbsentInformationException(org.eclipse.che.plugin.jdb.server.exceptions.DebuggerAbsentInformationException) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) BreakpointRequest(com.sun.jdi.request.BreakpointRequest) EventRequest(com.sun.jdi.request.EventRequest) ClassNotPreparedException(com.sun.jdi.ClassNotPreparedException) EventRequestManager(com.sun.jdi.request.EventRequestManager) Breakpoint(org.eclipse.che.api.debug.shared.model.Breakpoint) ReferenceType(com.sun.jdi.ReferenceType) BreakpointActivatedEventImpl(org.eclipse.che.api.debug.shared.model.impl.event.BreakpointActivatedEventImpl) ExpressionParser(org.eclipse.che.plugin.jdb.server.expression.ExpressionParser) InvalidRequestStateException(com.sun.jdi.request.InvalidRequestStateException) Location(org.eclipse.che.api.debug.shared.model.Location)

Example 14 with DebuggerException

use of org.eclipse.che.api.debugger.server.exceptions.DebuggerException in project che by eclipse.

the class JavaDebugger method connect.

/**
     * Attach to a JVM that is already running at specified host.
     *
     * @throws DebuggerException
     *         when connection to Java VM is not established
     */
private void connect() throws DebuggerException {
    final String connectorName = "com.sun.jdi.SocketAttach";
    AttachingConnector connector = connector(connectorName);
    if (connector == null) {
        throw new DebuggerException(String.format("Unable connect to target Java VM. Requested connector '%s' not found. ", connectorName));
    }
    Map<String, Connector.Argument> arguments = connector.defaultArguments();
    arguments.get("hostname").setValue(host);
    ((Connector.IntegerArgument) arguments.get("port")).setValue(port);
    int attempt = 0;
    for (; ; ) {
        try {
            Thread.sleep(2000);
            vm = connector.attach(arguments);
            vm.suspend();
            break;
        } catch (UnknownHostException | IllegalConnectorArgumentsException e) {
            throw new DebuggerException(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            if (++attempt > 10) {
                throw new DebuggerException(e.getMessage(), e);
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ignored) {
            }
        } catch (InterruptedException ignored) {
        }
    }
    eventsCollector = new EventsCollector(vm.eventQueue(), this);
    LOG.debug("Connect {}:{}", host, port);
}
Also used : IllegalConnectorArgumentsException(com.sun.jdi.connect.IllegalConnectorArgumentsException) UnknownHostException(java.net.UnknownHostException) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) AttachingConnector(com.sun.jdi.connect.AttachingConnector) IOException(java.io.IOException) Breakpoint(org.eclipse.che.api.debug.shared.model.Breakpoint)

Example 15 with DebuggerException

use of org.eclipse.che.api.debugger.server.exceptions.DebuggerException in project che by eclipse.

the class JavaDebuggerFactory method create.

@Override
public Debugger create(Map<String, String> properties, Debugger.DebuggerCallback debuggerCallback) throws DebuggerException {
    Map<String, String> normalizedProps = properties.entrySet().stream().collect(toMap(e -> e.getKey().toLowerCase(), Map.Entry::getValue));
    String host = normalizedProps.get("host");
    if (host == null) {
        throw new DebuggerException("Can't establish connection: host property is unknown.");
    }
    String portProp = normalizedProps.get("port");
    if (portProp == null) {
        throw new DebuggerException("Can't establish connection: port property is unknown.");
    }
    int port;
    try {
        port = Integer.parseInt(portProp);
    } catch (NumberFormatException e) {
        throw new DebuggerException("Unknown port property format: " + portProp);
    }
    return new JavaDebugger(host, port, debuggerCallback);
}
Also used : DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Debugger(org.eclipse.che.api.debugger.server.Debugger) DebuggerFactory(org.eclipse.che.api.debugger.server.DebuggerFactory) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map)

Aggregations

DebuggerException (org.eclipse.che.api.debugger.server.exceptions.DebuggerException)31 IOException (java.io.IOException)13 GdbParseException (org.eclipse.che.plugin.gdb.server.exception.GdbParseException)10 GdbTerminatedException (org.eclipse.che.plugin.gdb.server.exception.GdbTerminatedException)10 SuspendEventImpl (org.eclipse.che.api.debug.shared.model.impl.event.SuspendEventImpl)7 Breakpoint (org.eclipse.che.api.debug.shared.model.Breakpoint)6 Location (org.eclipse.che.api.debug.shared.model.Location)6 Map (java.util.Map)5 Collectors.toMap (java.util.stream.Collectors.toMap)4 BreakpointActivatedEventImpl (org.eclipse.che.api.debug.shared.model.impl.event.BreakpointActivatedEventImpl)4 Debugger (org.eclipse.che.api.debugger.server.Debugger)4 DebuggerFactory (org.eclipse.che.api.debugger.server.DebuggerFactory)4 ArrayList (java.util.ArrayList)3 GdbInfoLine (org.eclipse.che.plugin.gdb.server.parser.GdbInfoLine)3 NodeJsDebuggerException (org.eclipse.che.plugin.nodejsdbg.server.exception.NodeJsDebuggerException)3 NodeJsDebuggerTerminatedException (org.eclipse.che.plugin.nodejsdbg.server.exception.NodeJsDebuggerTerminatedException)3 AbsentInformationException (com.sun.jdi.AbsentInformationException)2 InvalidStackFrameException (com.sun.jdi.InvalidStackFrameException)2 NativeMethodException (com.sun.jdi.NativeMethodException)2 ReferenceType (com.sun.jdi.ReferenceType)2