Search in sources :

Example 11 with Breakpoint

use of com.google.api.services.clouddebugger.v2.model.Breakpoint in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudBreakpointHandler method registerBreakpoint.

/**
 * Called by IntelliJ when the user has enabled or created a new breakpoint. Creates the server
 * breakpoint.
 *
 * @param ideBreakpoint breakpoint to register
 */
@Override
public void registerBreakpoint(@NotNull final XLineBreakpoint<CloudLineBreakpointProperties> ideBreakpoint) {
    if (ideBreakpoint.getSourcePosition() == null || !ideBreakpoint.isEnabled() || !(ideBreakpoint.getType() instanceof CloudLineBreakpointType)) {
        return;
    }
    com.intellij.debugger.ui.breakpoints.Breakpoint cloudIdeBreakpoint = BreakpointManager.getJavaBreakpoint(ideBreakpoint);
    if (!(cloudIdeBreakpoint instanceof CloudLineBreakpointType.CloudLineBreakpoint)) {
        LOG.error("breakpoint was not of the correct type to create on the cloud.  It was not a " + "CloudLineBreakpoint");
        return;
    }
    final CloudLineBreakpointType.CloudLineBreakpoint cloudIdeLineBreakpoint = (CloudLineBreakpointType.CloudLineBreakpoint) cloudIdeBreakpoint;
    if (ideBreakpoint.getProperties().isCreatedByServer()) {
        // gets called during construction.
        return;
    }
    if (ideBreakpoint.getProperties().isAddedOnServer() && !ideBreakpoint.getProperties().isDisabledByServer()) {
        // disabled breakpoint
        return;
    }
    PsiFile javaFile = psiManager.findFile(ideBreakpoint.getSourcePosition().getFile());
    if (!(javaFile instanceof PsiJavaFile)) {
        return;
    }
    SourceLocation location = new SourceLocation();
    // Sending the file as com/package/example/Class.java to Cloud Debugger because it plays nice
    // with the CDB plugin. See ServerToIdeFileResolver.
    location.setPath(ServerToIdeFileResolver.getCloudPathFromJavaFile((PsiJavaFile) javaFile));
    location.setLine(ideBreakpoint.getSourcePosition().getLine() + 1);
    Breakpoint serverNewBreakpoint = new Breakpoint();
    serverNewBreakpoint.setLocation(location);
    if (ideBreakpoint.getConditionExpression() != null) {
        serverNewBreakpoint.setCondition(ideBreakpoint.getConditionExpression().getExpression());
    }
    List<String> watches = cloudIdeLineBreakpoint.getWatchExpressions();
    if (watches != null) {
        serverNewBreakpoint.setExpressions(watches);
    }
    // The breakpoint will enter error state asynchronously.  For now, we state that its verified.
    process.getStateController().setBreakpointAsync(serverNewBreakpoint, new SetBreakpointHandler() {

        @Override
        public void onSuccess(@NotNull final String id) {
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    if (!Strings.isNullOrEmpty(id)) {
                        if (!cloudIdeLineBreakpoint.isEnabled()) {
                            process.getStateController().deleteBreakpointAsync(// race condition
                            id);
                        } else {
                            // Success.
                            // Mark as added so we don't add it again.
                            ideBreakpoint.getProperties().setAddedOnServer(true);
                            cloudIdeLineBreakpoint.setErrorMessage(null);
                            process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
                        }
                    } else {
                        // TODO(joaomartins): Why couldn't the breakpoint be set? Improve this
                        // message.
                        cloudIdeLineBreakpoint.setErrorMessage(GctBundle.getString("clouddebug.errorset"));
                        process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
                    }
                    if (!Strings.isNullOrEmpty(id)) {
                        ideBreakpoint.getProperties().setDisabledByServer(false);
                        String oldId = ideBreakpoint.getUserData(CLOUD_ID);
                        if (!Strings.isNullOrEmpty(oldId)) {
                            ideBreakpoints.remove(oldId);
                        }
                        ideBreakpoint.putUserData(CLOUD_ID, id);
                        ideBreakpoints.put(id, ideBreakpoint);
                    }
                }
            };
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                runnable.run();
            } else {
                SwingUtilities.invokeLater(runnable);
            }
        }

        @Override
        public void onError(String errorMessage) {
            cloudIdeLineBreakpoint.setErrorMessage(errorMessage);
            process.updateBreakpointPresentation(cloudIdeLineBreakpoint);
        }
    });
}
Also used : SourceLocation(com.google.api.services.clouddebugger.v2.model.SourceLocation) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) XBreakpoint(com.intellij.xdebugger.breakpoints.XBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) XLineBreakpoint(com.intellij.xdebugger.breakpoints.XLineBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) CloudLineBreakpoint(com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint) SetBreakpointHandler(com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler) PsiJavaFile(com.intellij.psi.PsiJavaFile) PsiFile(com.intellij.psi.PsiFile)

Example 12 with Breakpoint

use of com.google.api.services.clouddebugger.v2.model.Breakpoint in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugGlobalPoller method queryServerForBreakpoints.

private void queryServerForBreakpoints(CloudDebugProcessState state, Debugger client) throws IOException {
    if (state.getDebuggeeId() == null) {
        throw new IllegalStateException("CloudDebugProcessState.getDebuggeeId() was null");
    }
    Debuggees debuggees = client.debuggees();
    Breakpoints breakpoints = debuggees.breakpoints();
    Breakpoints.List listRequest = breakpoints.list(state.getDebuggeeId()).setIncludeInactive(Boolean.TRUE).setActionValue("CAPTURE").setStripResults(Boolean.TRUE).setWaitToken(state.getWaitToken());
    ListBreakpointsResponse response = listRequest.setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
    List<Breakpoint> currentList = response.getBreakpoints();
    String responseWaitToken = response.getNextWaitToken();
    state.setWaitToken(responseWaitToken);
    if (currentList != null) {
        Collections.sort(currentList, BreakpointComparer.getDefaultInstance());
    }
    state.setCurrentServerBreakpointList(currentList != null ? ContainerUtil.immutableList(currentList) : ContainerUtil.immutableList(new ArrayList<Breakpoint>()));
}
Also used : Breakpoints(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger.Debuggees.Breakpoints) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) ListBreakpointsResponse(com.google.api.services.clouddebugger.v2.model.ListBreakpointsResponse) Debuggees(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger.Debuggees) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService)

Example 13 with Breakpoint

use of com.google.api.services.clouddebugger.v2.model.Breakpoint in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateController method setBreakpointAsync.

/**
 * Called from the {@link CloudDebugProcessHandler} to set a breakpoint.
 */
@SuppressWarnings("FutureReturnValueIgnored")
void setBreakpointAsync(@NotNull final Breakpoint serverBreakpoint, @NotNull final SetBreakpointHandler handler) {
    if (state == null) {
        handler.onError(GctBundle.getString("clouddebug.invalid.state"));
        return;
    }
    final Debugger client = CloudDebuggerClient.getLongTimeoutClient(state);
    if (client == null) {
        LOG.warn("no client available attempting to setBreakpoint");
        handler.onError(GctBundle.getString("clouddebug.bad.login.message"));
        return;
    }
    final String debuggeeId = state.getDebuggeeId();
    assert debuggeeId != null;
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        try {
            // Delete old breakpoints at this location.
            List<Breakpoint> currentList = state.getCurrentServerBreakpointList();
            SourceLocation location = serverBreakpoint.getLocation();
            for (Breakpoint serverBp : currentList) {
                if (!Boolean.TRUE.equals(serverBp.getIsFinalState()) && serverBp.getLocation().getLine() != null && serverBp.getLocation().getLine().equals(location.getLine()) && !Strings.isNullOrEmpty(serverBp.getLocation().getPath()) && serverBp.getLocation().getPath().equals(location.getPath())) {
                    // should not be async here.
                    deleteBreakpoint(serverBp.getId());
                }
            }
            SetBreakpointResponse addResponse = client.debuggees().breakpoints().set(debuggeeId, serverBreakpoint).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
            if (addResponse != null && addResponse.getBreakpoint() != null) {
                Breakpoint result = addResponse.getBreakpoint();
                if (result.getStatus() != null && Boolean.TRUE.equals(result.getStatus().getIsError()) && result.getStatus().getDescription() != null) {
                    handler.onError(BreakpointUtil.getUserErrorMessage(result.getStatus()));
                }
                handler.onSuccess(addResponse.getBreakpoint().getId());
            } else {
                handler.onError(GctBundle.getString("clouddebug.no.response"));
            }
        } catch (IOException ex) {
            LOG.error("exception setting a breakpoint", ex);
            handler.onError(ex.toString());
        }
    });
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) SourceLocation(com.google.api.services.clouddebugger.v2.model.SourceLocation) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) SetBreakpointResponse(com.google.api.services.clouddebugger.v2.model.SetBreakpointResponse) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService) IOException(java.io.IOException)

Example 14 with Breakpoint

use of com.google.api.services.clouddebugger.v2.model.Breakpoint in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateController method deleteBreakpoint.

/**
 * Called from the {@link CloudBreakpointHandler} to remove breakpoints from the server.
 *
 * @param breakpointId the {@link Breakpoint} Id to delete
 */
@SuppressWarnings("FutureReturnValueIgnored")
private void deleteBreakpoint(@NotNull final String breakpointId, boolean performAsync) {
    if (state == null) {
        throw new IllegalStateException();
    }
    final Debugger client = CloudDebuggerClient.getLongTimeoutClient(state);
    if (client == null) {
        LOG.warn("no client available attempting to setBreakpoint");
        Messages.showErrorDialog(state.getProject(), GctBundle.getString("clouddebug.bad.login.message"), GctBundle.getString("clouddebug.message.title"));
        return;
    }
    final String debuggeeId = state.getDebuggeeId();
    assert debuggeeId != null;
    Runnable performDelete = () -> {
        try {
            client.debuggees().breakpoints().delete(debuggeeId, breakpointId).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
        } catch (IOException ex) {
            LOG.warn("exception deleting breakpoint " + breakpointId, ex);
        }
    };
    if (performAsync) {
        ApplicationManager.getApplication().executeOnPooledThread(performDelete);
    } else {
        performDelete.run();
    }
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) IOException(java.io.IOException)

Example 15 with Breakpoint

use of com.google.api.services.clouddebugger.v2.model.Breakpoint in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateController method resolveBreakpointAsync.

/**
 * Returns a fully realized {@link Breakpoint} with all results possibly asynchronously.
 */
@SuppressWarnings("FutureReturnValueIgnored")
public void resolveBreakpointAsync(@NotNull final String id, @NotNull final ResolveBreakpointHandler handler) {
    if (fullFinalBreakpoints.containsKey(id)) {
        handler.onSuccess(fullFinalBreakpoints.get(id));
        return;
    }
    if (state == null) {
        handler.onError(GctBundle.getString("clouddebug.invalid.state"));
        return;
    }
    final Debugger client = CloudDebuggerClient.getLongTimeoutClient(state);
    if (client == null) {
        LOG.warn("no client available attempting to resolveBreakpointAsync");
        handler.onError(GctBundle.getString("clouddebug.bad.login.message"));
        return;
    }
    List<Breakpoint> currentList = state.getCurrentServerBreakpointList();
    for (Breakpoint serverBreakpointCandidate : currentList) {
        if (serverBreakpointCandidate.getId().equals(id) && !Boolean.TRUE.equals(serverBreakpointCandidate.getIsFinalState())) {
            handler.onSuccess(serverBreakpointCandidate);
            return;
        }
    }
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        // At this point, the user has selected a final state breakpoint which is not yet
        // hydrated.
        // So we query the server to get this final on a worker thread and then run the
        // runnable
        // back on ui
        GetBreakpointResponse response;
        try {
            response = client.debuggees().breakpoints().get(state.getDebuggeeId(), id).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
            Breakpoint result = response.getBreakpoint();
            if (result != null) {
                fullFinalBreakpoints.put(id, result);
                handler.onSuccess(result);
            } else {
                handler.onError(GctBundle.getString("clouddebug.no.response"));
            }
        } catch (IOException ex) {
            LOG.warn("IOException hydrating a snapshot.  User may have deleted the snapshot", ex);
            handler.onError(ex.toString());
        }
    });
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) IOException(java.io.IOException) GetBreakpointResponse(com.google.api.services.clouddebugger.v2.model.GetBreakpointResponse)

Aggregations

Breakpoint (com.google.api.services.clouddebugger.v2.model.Breakpoint)27 CloudLineBreakpoint (com.google.cloud.tools.intellij.debugger.CloudLineBreakpointType.CloudLineBreakpoint)11 XLineBreakpoint (com.intellij.xdebugger.breakpoints.XLineBreakpoint)11 XBreakpoint (com.intellij.xdebugger.breakpoints.XBreakpoint)10 SourceLocation (com.google.api.services.clouddebugger.v2.model.SourceLocation)8 Debugger (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger)7 ArrayList (java.util.ArrayList)7 XBreakpointManager (com.intellij.xdebugger.breakpoints.XBreakpointManager)4 IOException (java.io.IOException)4 ListBreakpointsResponse (com.google.api.services.clouddebugger.v2.model.ListBreakpointsResponse)3 PluginInfoService (com.google.cloud.tools.intellij.service.PluginInfoService)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 XLineBreakpointImpl (com.intellij.xdebugger.impl.breakpoints.XLineBreakpointImpl)3 Test (org.junit.Test)3 StatusMessage (com.google.api.services.clouddebugger.v2.model.StatusMessage)2 CloudBreakpointHandler (com.google.cloud.tools.intellij.debugger.CloudBreakpointHandler)2 SetBreakpointHandler (com.google.cloud.tools.intellij.debugger.CloudDebugProcessStateController.SetBreakpointHandler)2 RelativePoint (com.intellij.ui.awt.RelativePoint)2 XDebuggerManager (com.intellij.xdebugger.XDebuggerManager)2