Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 1 | /* |
Tor Norbye | 9258464 | 2014-04-17 08:39:25 -0700 | [diff] [blame] | 2 | * Copyright 2000-2014 JetBrains s.r.o. |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.intellij.debugger; |
| 17 | |
| 18 | import com.intellij.debugger.engine.DebugProcessImpl; |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 19 | import com.intellij.debugger.engine.JavaDebugProcess; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 20 | import com.intellij.debugger.engine.RemoteStateState; |
| 21 | import com.intellij.debugger.engine.SuspendContextImpl; |
| 22 | import com.intellij.debugger.engine.evaluation.EvaluateException; |
| 23 | import com.intellij.debugger.engine.evaluation.EvaluationContextImpl; |
| 24 | import com.intellij.debugger.engine.events.DebuggerCommandImpl; |
| 25 | import com.intellij.debugger.impl.*; |
| 26 | import com.intellij.debugger.jdi.StackFrameProxyImpl; |
| 27 | import com.intellij.debugger.settings.DebuggerSettings; |
| 28 | import com.intellij.debugger.settings.NodeRendererSettings; |
| 29 | import com.intellij.debugger.ui.breakpoints.BreakpointManager; |
| 30 | import com.intellij.execution.ExecutionException; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 31 | import com.intellij.execution.Executor; |
| 32 | import com.intellij.execution.configurations.*; |
| 33 | import com.intellij.execution.executors.DefaultDebugExecutor; |
| 34 | import com.intellij.execution.process.ProcessAdapter; |
| 35 | import com.intellij.execution.process.ProcessEvent; |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 36 | import com.intellij.execution.process.ProcessHandler; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 37 | import com.intellij.execution.process.ProcessOutputTypes; |
| 38 | import com.intellij.execution.runners.ExecutionEnvironment; |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 39 | import com.intellij.execution.runners.ExecutionEnvironmentBuilder; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 40 | import com.intellij.execution.runners.ProgramRunner; |
| 41 | import com.intellij.openapi.application.ApplicationManager; |
| 42 | import com.intellij.openapi.application.ModalityState; |
| 43 | import com.intellij.openapi.editor.Document; |
Tor Norbye | 9258464 | 2014-04-17 08:39:25 -0700 | [diff] [blame] | 44 | import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 45 | import com.intellij.openapi.module.Module; |
| 46 | import com.intellij.openapi.options.SettingsEditor; |
| 47 | import com.intellij.openapi.project.Project; |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 48 | import com.intellij.openapi.util.Computable; |
| 49 | import com.intellij.openapi.util.InvalidDataException; |
| 50 | import com.intellij.openapi.util.Key; |
| 51 | import com.intellij.openapi.util.WriteExternalException; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 52 | import com.intellij.openapi.util.text.StringUtil; |
| 53 | import com.intellij.psi.JavaPsiFacade; |
| 54 | import com.intellij.psi.PsiClass; |
| 55 | import com.intellij.psi.PsiDocumentManager; |
Tor Norbye | c3d3a90f | 2014-09-04 13:24:04 -0700 | [diff] [blame] | 56 | import com.intellij.psi.PsiFile; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 57 | import com.intellij.psi.search.GlobalSearchScope; |
| 58 | import com.intellij.util.ui.UIUtil; |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 59 | import com.intellij.xdebugger.XDebugProcess; |
| 60 | import com.intellij.xdebugger.XDebugProcessStarter; |
| 61 | import com.intellij.xdebugger.XDebugSession; |
| 62 | import com.intellij.xdebugger.XDebuggerManager; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 63 | import org.jdom.Element; |
| 64 | import org.jetbrains.annotations.NotNull; |
| 65 | |
| 66 | import javax.swing.*; |
| 67 | import java.lang.reflect.InvocationTargetException; |
| 68 | import java.util.StringTokenizer; |
| 69 | |
| 70 | public abstract class DebuggerTestCase extends ExecutionWithDebuggerToolsTestCase { |
| 71 | protected DebuggerSession myDebuggerSession; |
| 72 | private StringBuffer myConsoleBuffer; |
| 73 | |
| 74 | @Override |
| 75 | protected void setUp() throws Exception { |
| 76 | super.setUp(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected void initApplication() throws Exception { |
| 81 | super.initApplication(); |
| 82 | setTestJDK(); |
| 83 | DebuggerSettings.getInstance().DEBUGGER_TRANSPORT = DebuggerSettings.SOCKET_TRANSPORT; |
| 84 | DebuggerSettings.getInstance().SKIP_CONSTRUCTORS = false; |
| 85 | DebuggerSettings.getInstance().SKIP_GETTERS = false; |
| 86 | NodeRendererSettings.getInstance().getClassRenderer().SHOW_DECLARED_TYPE = true; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected void runTest() throws Throwable { |
| 91 | super.runTest(); |
| 92 | if(getDebugProcess() != null) { |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 93 | getDebugProcess().getProcessHandler().startNotify(); |
| 94 | waitProcess(getDebugProcess().getProcessHandler()); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 95 | waitForCompleted(); |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 96 | //disposeSession(myDebuggerSession); |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 97 | assertNull(DebuggerManagerEx.getInstanceEx(myProject).getDebugProcess(getDebugProcess().getProcessHandler())); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 98 | myDebuggerSession = null; |
| 99 | } |
| 100 | if(myConsoleBuffer != null) { |
| 101 | //println("", b); |
| 102 | //println("Console output:", b); |
| 103 | //println(myConsoleBuffer.toString(), b); |
| 104 | } |
| 105 | checkTestOutput(); |
| 106 | } |
| 107 | |
| 108 | protected void checkTestOutput() throws Exception { |
| 109 | getChecker().checkValid(getTestProjectJdk()); |
| 110 | } |
| 111 | |
| 112 | protected void disposeSession(final DebuggerSession debuggerSession) throws InterruptedException, InvocationTargetException { |
| 113 | UIUtil.invokeAndWaitIfNeeded(new Runnable() { |
| 114 | @Override |
| 115 | public void run() { |
| 116 | debuggerSession.dispose(); |
| 117 | } |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | protected void tearDown() throws Exception { |
Tor Norbye | 9258464 | 2014-04-17 08:39:25 -0700 | [diff] [blame] | 123 | FileEditorManagerEx.getInstanceEx(getProject()).closeAllFiles(); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 124 | myConsoleBuffer = null; |
Tor Norbye | 9258464 | 2014-04-17 08:39:25 -0700 | [diff] [blame] | 125 | super.tearDown(); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | protected void createLocalProcess(String className) throws ExecutionException, InterruptedException, InvocationTargetException { |
| 129 | LOG.assertTrue(myDebugProcess == null); |
| 130 | myDebuggerSession = createLocalProcess(DebuggerSettings.SOCKET_TRANSPORT, createJavaParameters(className)); |
| 131 | myDebugProcess = myDebuggerSession.getProcess(); |
| 132 | } |
| 133 | |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 134 | protected DebuggerSession createLocalSession(final JavaParameters javaParameters) throws ExecutionException, InterruptedException { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 135 | createBreakpoints(javaParameters.getMainClass()); |
| 136 | DebuggerSettings.getInstance().DEBUGGER_TRANSPORT = DebuggerSettings.SOCKET_TRANSPORT; |
| 137 | |
| 138 | GenericDebuggerRunnerSettings debuggerRunnerSettings = new GenericDebuggerRunnerSettings(); |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 139 | debuggerRunnerSettings.LOCAL = true; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 140 | |
| 141 | final RemoteConnection debugParameters = DebuggerManagerImpl.createDebugParameters(javaParameters, debuggerRunnerSettings, false); |
| 142 | |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 143 | ExecutionEnvironment environment = new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()) |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 144 | .runnerSettings(debuggerRunnerSettings) |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 145 | .runProfile(new MockConfiguration()) |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 146 | .build(); |
Tor Norbye | 6739a8f | 2013-08-07 11:11:08 -0700 | [diff] [blame] | 147 | final JavaCommandLineState javaCommandLineState = new JavaCommandLineState(environment){ |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 148 | @Override |
| 149 | protected JavaParameters createJavaParameters() { |
| 150 | return javaParameters; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | protected GeneralCommandLine createCommandLine() throws ExecutionException { |
| 155 | return CommandLineBuilder.createFromJavaParameters(getJavaParameters()); |
| 156 | } |
| 157 | }; |
| 158 | |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 159 | ApplicationManager.getApplication().invokeAndWait(new Runnable() { |
| 160 | @Override |
| 161 | public void run() { |
| 162 | try { |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 163 | myDebuggerSession = |
| 164 | DebuggerManagerEx.getInstanceEx(myProject) |
| 165 | .attachVirtualMachine(new DefaultDebugEnvironment(new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()) |
| 166 | .runProfile(new MockConfiguration()) |
| 167 | .build(), javaCommandLineState, debugParameters, false)); |
| 168 | XDebuggerManager.getInstance(myProject).startSession(javaCommandLineState.getEnvironment(), new XDebugProcessStarter() { |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 169 | @Override |
| 170 | @NotNull |
| 171 | public XDebugProcess start(@NotNull XDebugSession session) { |
| 172 | return new JavaDebugProcess(session, myDebuggerSession); |
| 173 | } |
| 174 | }); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 175 | } |
| 176 | catch (ExecutionException e) { |
| 177 | LOG.error(e); |
| 178 | } |
| 179 | } |
| 180 | }, ModalityState.defaultModalityState()); |
| 181 | myDebugProcess = myDebuggerSession.getProcess(); |
| 182 | |
| 183 | //myConsoleBuffer = new StringBuffer(); |
| 184 | |
| 185 | myDebugProcess.addProcessListener(new ProcessAdapter() { |
| 186 | @Override |
| 187 | public void onTextAvailable(ProcessEvent event, Key outputType) { |
| 188 | //myConsoleBuffer.append(event.getText()); |
| 189 | print(event.getText(), outputType); |
| 190 | } |
| 191 | }); |
| 192 | |
| 193 | assertNotNull(myDebuggerSession); |
| 194 | assertNotNull(myDebugProcess); |
| 195 | |
| 196 | return myDebuggerSession; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | protected DebuggerSession createLocalProcess(int transport, final JavaParameters javaParameters) throws ExecutionException, InterruptedException, InvocationTargetException { |
| 201 | createBreakpoints(javaParameters.getMainClass()); |
| 202 | final DebuggerSession[] debuggerSession = new DebuggerSession[]{null}; |
| 203 | |
| 204 | DebuggerSettings.getInstance().DEBUGGER_TRANSPORT = transport; |
| 205 | |
| 206 | GenericDebuggerRunnerSettings debuggerRunnerSettings = new GenericDebuggerRunnerSettings(); |
| 207 | debuggerRunnerSettings.LOCAL = true; |
| 208 | debuggerRunnerSettings.DEBUG_PORT = "3456"; |
| 209 | |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 210 | ExecutionEnvironment environment = new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()) |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 211 | .runnerSettings(debuggerRunnerSettings) |
| 212 | .runProfile(new MockConfiguration()) |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 213 | .build(); |
Tor Norbye | 6739a8f | 2013-08-07 11:11:08 -0700 | [diff] [blame] | 214 | final JavaCommandLineState javaCommandLineState = new JavaCommandLineState(environment) { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 215 | @Override |
| 216 | protected JavaParameters createJavaParameters() { |
| 217 | return javaParameters; |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | protected GeneralCommandLine createCommandLine() throws ExecutionException { |
| 222 | return CommandLineBuilder.createFromJavaParameters(getJavaParameters()); |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | final RemoteConnection debugParameters = |
| 227 | DebuggerManagerImpl.createDebugParameters(javaCommandLineState.getJavaParameters(), debuggerRunnerSettings, true); |
| 228 | |
| 229 | UIUtil.invokeAndWaitIfNeeded(new Runnable() { |
| 230 | @Override |
| 231 | public void run() { |
| 232 | try { |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 233 | debuggerSession[0] = attachVirtualMachine(javaCommandLineState, javaCommandLineState.getEnvironment(), debugParameters, false); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 234 | } |
| 235 | catch (ExecutionException e) { |
| 236 | fail(e.getMessage()); |
| 237 | } |
| 238 | } |
| 239 | }); |
| 240 | |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 241 | final ProcessHandler processHandler = debuggerSession[0].getProcess().getProcessHandler(); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 242 | debuggerSession[0].getProcess().addProcessListener(new ProcessAdapter() { |
| 243 | @Override |
| 244 | public void onTextAvailable(ProcessEvent event, Key outputType) { |
| 245 | print(event.getText(), outputType); |
| 246 | } |
| 247 | }); |
| 248 | |
| 249 | DebugProcessImpl process = |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 250 | (DebugProcessImpl)DebuggerManagerEx.getInstanceEx(myProject).getDebugProcess(processHandler); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 251 | assertNotNull(process); |
| 252 | return debuggerSession[0]; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | protected DebuggerSession createRemoteProcess(final int transport, final boolean serverMode, JavaParameters javaParameters) |
| 257 | throws ExecutionException, InterruptedException, InvocationTargetException { |
| 258 | boolean useSockets = transport == DebuggerSettings.SOCKET_TRANSPORT; |
| 259 | |
| 260 | RemoteConnection remoteConnection = new RemoteConnection( |
| 261 | useSockets, |
| 262 | "127.0.0.1", |
| 263 | "3456", |
| 264 | serverMode); |
| 265 | |
| 266 | String launchCommandLine = remoteConnection.getLaunchCommandLine(); |
| 267 | |
| 268 | launchCommandLine = StringUtil.replace(launchCommandLine, RemoteConnection.ONTHROW, ""); |
| 269 | launchCommandLine = StringUtil.replace(launchCommandLine, RemoteConnection.ONUNCAUGHT, ""); |
| 270 | |
| 271 | launchCommandLine = StringUtil.replace(launchCommandLine, "suspend=n", "suspend=y"); |
| 272 | |
| 273 | println(launchCommandLine, ProcessOutputTypes.SYSTEM); |
| 274 | |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 275 | for(StringTokenizer tokenizer = new StringTokenizer(launchCommandLine);tokenizer.hasMoreTokens();) { |
| 276 | String token = tokenizer.nextToken(); |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 277 | javaParameters.getVMParametersList().add(token); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 280 | GeneralCommandLine commandLine = CommandLineBuilder.createFromJavaParameters(javaParameters); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 281 | |
| 282 | |
| 283 | DebuggerSession debuggerSession; |
| 284 | |
| 285 | if(serverMode) { |
| 286 | debuggerSession = attachVM(remoteConnection, false); |
| 287 | commandLine.createProcess(); |
| 288 | } else { |
| 289 | commandLine.createProcess(); |
| 290 | debuggerSession = attachVM(remoteConnection, true); |
| 291 | } |
| 292 | |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 293 | ProcessHandler processHandler = debuggerSession.getProcess().getProcessHandler(); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 294 | DebugProcessImpl process = (DebugProcessImpl)DebuggerManagerEx.getInstanceEx(myProject) |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 295 | .getDebugProcess(processHandler); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 296 | |
| 297 | assertNotNull(process); |
| 298 | return debuggerSession; |
| 299 | } |
| 300 | |
| 301 | protected DebuggerSession attachVM(final RemoteConnection remoteConnection, final boolean pollConnection) |
| 302 | throws InvocationTargetException, InterruptedException { |
Tor Norbye | 6739a8f | 2013-08-07 11:11:08 -0700 | [diff] [blame] | 303 | final RemoteState remoteState = new RemoteStateState(myProject, remoteConnection); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 304 | |
| 305 | final DebuggerSession[] debuggerSession = new DebuggerSession[1]; |
| 306 | UIUtil.invokeAndWaitIfNeeded(new Runnable() { |
| 307 | @Override |
| 308 | public void run() { |
| 309 | try { |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 310 | debuggerSession[0] = attachVirtualMachine(remoteState, new ExecutionEnvironmentBuilder(myProject, DefaultDebugExecutor.getDebugExecutorInstance()) |
| 311 | .runProfile(new MockConfiguration()) |
| 312 | .build(), remoteConnection, pollConnection); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 313 | } |
| 314 | catch (ExecutionException e) { |
| 315 | fail(e.getMessage()); |
| 316 | } |
| 317 | } |
| 318 | }); |
Tor Norbye | e782c57 | 2014-09-18 11:43:07 -0700 | [diff] [blame^] | 319 | debuggerSession[0].getProcess().getProcessHandler().addProcessListener(new ProcessAdapter() { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 320 | @Override |
| 321 | public void onTextAvailable(ProcessEvent event, Key outputType) { |
| 322 | print(event.getText(), outputType); |
| 323 | } |
| 324 | }); |
| 325 | return debuggerSession[0]; |
| 326 | } |
| 327 | |
| 328 | protected void createBreakpoints(final String className) { |
Tor Norbye | c3d3a90f | 2014-09-04 13:24:04 -0700 | [diff] [blame] | 329 | final PsiFile psiFile = ApplicationManager.getApplication().runReadAction(new Computable<PsiFile>() { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 330 | @Override |
Tor Norbye | c3d3a90f | 2014-09-04 13:24:04 -0700 | [diff] [blame] | 331 | public PsiFile compute() { |
| 332 | PsiClass psiClass = JavaPsiFacade.getInstance(myProject).findClass(className, GlobalSearchScope.allScope(myProject)); |
| 333 | return psiClass.getContainingFile(); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 334 | } |
| 335 | }); |
| 336 | |
Tor Norbye | c3d3a90f | 2014-09-04 13:24:04 -0700 | [diff] [blame] | 337 | createBreakpoints(psiFile); |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | protected EvaluationContextImpl createEvaluationContext(final SuspendContextImpl suspendContext) { |
| 341 | try { |
| 342 | return new EvaluationContextImpl( |
| 343 | suspendContext, |
| 344 | suspendContext.getFrameProxy(), |
| 345 | suspendContext.getFrameProxy().thisObject()); |
| 346 | } |
| 347 | catch (EvaluateException e) { |
| 348 | error(e); |
| 349 | return null; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | protected void waitForCompleted() { |
| 354 | final SynchronizationBasedSemaphore s = new SynchronizationBasedSemaphore(); |
| 355 | s.down(); |
| 356 | |
| 357 | final InvokeThread.WorkerThreadRequest request = getDebugProcess().getManagerThread().getCurrentRequest(); |
| 358 | final Thread thread = new Thread("Joining "+request) { |
| 359 | @Override |
| 360 | public void run() { |
| 361 | try { |
| 362 | request.join(); |
| 363 | } |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 364 | catch (Exception ignored) { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | }; |
| 368 | thread.start(); |
| 369 | if(request.isDone()) { |
| 370 | thread.interrupt(); |
| 371 | } |
| 372 | waitFor(new Runnable() { |
| 373 | @Override |
| 374 | public void run() { |
| 375 | try { |
| 376 | thread.join(); |
| 377 | } |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 378 | catch (InterruptedException ignored) { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | }); |
| 382 | |
| 383 | invokeRatherLater(new DebuggerCommandImpl() { |
| 384 | @Override |
| 385 | protected void action() throws Exception { |
| 386 | LOG.assertTrue(false); |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | protected void commandCancelled() { |
| 391 | //We wait for invokeRatherLater's |
| 392 | invokeRatherLater(new DebuggerCommandImpl() { |
| 393 | @Override |
| 394 | protected void action() throws Exception { |
| 395 | LOG.assertTrue(false); |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | protected void commandCancelled() { |
| 400 | s.up(); |
| 401 | } |
| 402 | }); |
| 403 | } |
| 404 | }); |
| 405 | |
| 406 | waitFor(new Runnable() { |
| 407 | @Override |
| 408 | public void run() { |
| 409 | s.waitFor(); |
| 410 | } |
| 411 | }); |
| 412 | } |
| 413 | |
| 414 | public DebuggerContextImpl createDebuggerContext(final SuspendContextImpl suspendContext, StackFrameProxyImpl stackFrame) { |
| 415 | final DebuggerSession[] session = new DebuggerSession[1]; |
| 416 | |
| 417 | UIUtil.invokeAndWaitIfNeeded(new Runnable() { |
| 418 | @Override |
| 419 | public void run() { |
| 420 | session[0] = DebuggerManagerEx.getInstanceEx(myProject).getSession(suspendContext.getDebugProcess()); |
| 421 | } |
| 422 | }); |
| 423 | |
| 424 | DebuggerContextImpl debuggerContext = DebuggerContextImpl.createDebuggerContext( |
| 425 | session[0], |
| 426 | suspendContext, |
| 427 | stackFrame != null ? stackFrame.threadProxy() : null, |
| 428 | stackFrame); |
| 429 | debuggerContext.initCaches(); |
| 430 | return debuggerContext; |
| 431 | } |
| 432 | |
| 433 | public DebuggerContextImpl createDebuggerContext(final SuspendContextImpl suspendContext) { |
| 434 | return createDebuggerContext(suspendContext, suspendContext.getFrameProxy()); |
| 435 | } |
| 436 | |
| 437 | protected void createBreakpointInHelloWorld() { |
| 438 | DebuggerInvocationUtil.invokeAndWait(myProject, new Runnable() { |
| 439 | @Override |
| 440 | public void run() { |
| 441 | BreakpointManager breakpointManager = DebuggerManagerEx.getInstanceEx(myProject).getBreakpointManager(); |
| 442 | PsiClass psiClass = JavaPsiFacade.getInstance(myProject).findClass("HelloWorld", GlobalSearchScope.allScope(myProject)); |
| 443 | Document document = PsiDocumentManager.getInstance(myProject).getDocument(psiClass.getContainingFile()); |
| 444 | breakpointManager.addLineBreakpoint(document, 3); |
| 445 | } |
| 446 | }, ApplicationManager.getApplication().getDefaultModalityState()); |
| 447 | } |
| 448 | |
| 449 | protected void createHelloWorldProcessWithBreakpoint() throws ExecutionException, InterruptedException, InvocationTargetException { |
| 450 | createLocalProcess("HelloWorld"); |
| 451 | |
| 452 | createBreakpointInHelloWorld(); |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | protected DebugProcessImpl getDebugProcess() { |
| 457 | return myDebuggerSession != null ? myDebuggerSession.getProcess() : null; |
| 458 | } |
| 459 | |
| 460 | public DebuggerSession getDebuggerSession() { |
| 461 | return myDebuggerSession; |
| 462 | } |
| 463 | |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 464 | protected DebuggerSession attachVirtualMachine(RunProfileState state, |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 465 | ExecutionEnvironment environment, |
| 466 | RemoteConnection remoteConnection, |
| 467 | boolean pollConnection) throws ExecutionException { |
| 468 | final DebuggerSession debuggerSession = |
Tor Norbye | 1aa2e09 | 2014-08-20 17:01:23 -0700 | [diff] [blame] | 469 | DebuggerManagerEx.getInstanceEx(myProject).attachVirtualMachine(new DefaultDebugEnvironment(environment, state, remoteConnection, pollConnection)); |
| 470 | XDebuggerManager.getInstance(myProject).startSession(environment, new XDebugProcessStarter() { |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 471 | @Override |
| 472 | @NotNull |
| 473 | public XDebugProcess start(@NotNull XDebugSession session) { |
| 474 | return new JavaDebugProcess(session, debuggerSession); |
| 475 | } |
| 476 | }); |
| 477 | return debuggerSession; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Tor Norbye | 65f60eb | 2014-07-16 18:07:37 -0700 | [diff] [blame] | 480 | public static class MockConfiguration implements ModuleRunConfiguration { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 481 | @Override |
| 482 | @NotNull |
| 483 | public Module[] getModules() { |
| 484 | return Module.EMPTY_ARRAY; //To change body of implemented methods use File | Settings | File Templates. |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public Icon getIcon() { |
| 489 | return null; //To change body of implemented methods use File | Settings | File Templates. |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public ConfigurationFactory getFactory() { |
| 494 | return null; //To change body of implemented methods use File | Settings | File Templates. |
| 495 | } |
| 496 | |
| 497 | @Override |
| 498 | public void setName(final String name) { |
| 499 | //To change body of implemented methods use File | Settings | File Templates. |
| 500 | } |
| 501 | |
Tor Norbye | 6739a8f | 2013-08-07 11:11:08 -0700 | [diff] [blame] | 502 | @NotNull |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 503 | @Override |
| 504 | public SettingsEditor<? extends RunConfiguration> getConfigurationEditor() { |
| 505 | return null; //To change body of implemented methods use File | Settings | File Templates. |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public Project getProject() { |
| 510 | return null; //To change body of implemented methods use File | Settings | File Templates. |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | @NotNull |
| 515 | public ConfigurationType getType() { |
Tor Norbye | 65f60eb | 2014-07-16 18:07:37 -0700 | [diff] [blame] | 516 | return UnknownConfigurationType.INSTANCE; |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | @Override |
Tor Norbye | 6739a8f | 2013-08-07 11:11:08 -0700 | [diff] [blame] | 520 | public ConfigurationPerRunnerSettings createRunnerSettings(final ConfigurationInfoProvider provider) { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 521 | return null; //To change body of implemented methods use File | Settings | File Templates. |
| 522 | } |
| 523 | |
| 524 | @Override |
Tor Norbye | 6739a8f | 2013-08-07 11:11:08 -0700 | [diff] [blame] | 525 | public SettingsEditor<ConfigurationPerRunnerSettings> getRunnerSettingsEditor(final ProgramRunner runner) { |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 526 | return null; //To change body of implemented methods use File | Settings | File Templates. |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public RunConfiguration clone() { |
| 531 | return null; //To change body of implemented methods use File | Settings | File Templates. |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public int getUniqueID() { |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment env) throws ExecutionException { |
| 541 | return null; //To change body of implemented methods use File | Settings | File Templates. |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public String getName() { |
Tor Norbye | c667c1f | 2014-05-28 17:06:51 -0700 | [diff] [blame] | 546 | return ""; //To change body of implemented methods use File | Settings | File Templates. |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public void checkConfiguration() throws RuntimeConfigurationException { |
| 551 | //To change body of implemented methods use File | Settings | File Templates. |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public void readExternal(final Element element) throws InvalidDataException { |
| 556 | //To change body of implemented methods use File | Settings | File Templates. |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public void writeExternal(final Element element) throws WriteExternalException { |
| 561 | //To change body of implemented methods use File | Settings | File Templates. |
| 562 | } |
| 563 | } |
Tor Norbye | c1ace1f | 2013-07-08 11:26:24 -0700 | [diff] [blame] | 564 | } |