1 11 package org.eclipse.ant.internal.ui.debug.model; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.ant.internal.ui.debug.IAntDebugConstants; 18 import org.eclipse.ant.internal.ui.debug.IAntDebugController; 19 import org.eclipse.core.resources.IMarkerDelta; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.variables.VariablesPlugin; 22 import org.eclipse.debug.core.DebugEvent; 23 import org.eclipse.debug.core.DebugException; 24 import org.eclipse.debug.core.DebugPlugin; 25 import org.eclipse.debug.core.IBreakpointManager; 26 import org.eclipse.debug.core.IBreakpointManagerListener; 27 import org.eclipse.debug.core.IDebugEventSetListener; 28 import org.eclipse.debug.core.ILaunch; 29 import org.eclipse.debug.core.model.IBreakpoint; 30 import org.eclipse.debug.core.model.IDebugTarget; 31 import org.eclipse.debug.core.model.ILineBreakpoint; 32 import org.eclipse.debug.core.model.IMemoryBlock; 33 import org.eclipse.debug.core.model.IProcess; 34 import org.eclipse.debug.core.model.IThread; 35 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants; 36 37 40 public class AntDebugTarget extends AntDebugElement implements IDebugTarget, IDebugEventSetListener, IBreakpointManagerListener { 41 42 private IProcess fProcess; 44 45 private ILaunch fLaunch; 47 48 private String fName; 50 51 private boolean fSuspended= false; 53 54 private boolean fTerminated= false; 56 57 private AntThread fThread; 59 private IThread[] fThreads; 60 61 private IAntDebugController fController; 62 63 private List fRunToLineBreakpoints; 64 65 73 public AntDebugTarget(ILaunch launch, IProcess process, IAntDebugController controller) { 74 super(null); 75 fLaunch = launch; 76 fProcess = process; 77 78 fController= controller; 79 80 fThread = new AntThread(this); 81 fThreads = new IThread[] {fThread}; 82 83 DebugPlugin.getDefault().getBreakpointManager().addBreakpointManagerListener(this); 84 DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this); 85 DebugPlugin.getDefault().addDebugEventListener(this); 86 } 87 88 91 public IProcess getProcess() { 92 return fProcess; 93 } 94 95 98 public IThread[] getThreads() { 99 return fThreads; 100 } 101 102 105 public boolean hasThreads() throws DebugException { 106 return !fTerminated && fThreads.length > 0; 107 } 108 109 112 public String getName() throws DebugException { 113 if (fName == null) { 114 try { 115 fName= getLaunch().getLaunchConfiguration().getAttribute(IExternalToolConstants.ATTR_LOCATION, DebugModelMessages.AntDebugTarget_0); 116 fName= VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(fName); 117 } catch (CoreException e) { 118 fName = DebugModelMessages.AntDebugTarget_0; 119 } 120 } 121 return fName; 122 } 123 124 127 public boolean supportsBreakpoint(IBreakpoint breakpoint) { 128 if (breakpoint.getModelIdentifier().equals(IAntDebugConstants.ID_ANT_DEBUG_MODEL)) { 129 return true; 132 } 133 return false; 134 } 135 136 139 public IDebugTarget getDebugTarget() { 140 return this; 141 } 142 143 146 public ILaunch getLaunch() { 147 return fLaunch; 148 } 149 150 153 public boolean canTerminate() { 154 return !fTerminated; 155 } 156 157 160 public boolean isTerminated() { 161 return fTerminated; 162 } 163 164 167 public void terminate() throws DebugException { 168 terminated(); 169 } 170 171 174 public boolean canResume() { 175 return !isTerminated() && isSuspended(); 176 } 177 178 181 public boolean canSuspend() { 182 return !isTerminated() && !isSuspended(); 183 } 184 185 188 public boolean isSuspended() { 189 return fSuspended; 190 } 191 192 195 public void resume() throws DebugException { 196 fSuspended= false; 197 fController.resume(); 198 } 199 200 205 public void suspended(int detail) { 206 fSuspended = true; 207 fThread.setStepping(false); 208 fThread.fireSuspendEvent(detail); 209 } 210 211 214 public void suspend() throws DebugException { 215 fController.suspend(); 216 } 217 218 221 public void breakpointAdded(IBreakpoint breakpoint) { 222 fController.handleBreakpoint(breakpoint, true); 223 if (breakpoint instanceof AntLineBreakpoint) { 224 if (((AntLineBreakpoint) breakpoint).isRunToLine()) { 225 if (fRunToLineBreakpoints == null) { 226 fRunToLineBreakpoints= new ArrayList (); 227 } 228 fRunToLineBreakpoints.add(breakpoint); 229 } 230 } 231 } 232 233 236 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) { 237 fController.handleBreakpoint(breakpoint, false); 238 if (fRunToLineBreakpoints != null) { 239 if (fRunToLineBreakpoints.remove(breakpoint) && fRunToLineBreakpoints.isEmpty()) { 240 fRunToLineBreakpoints= null; 241 } 242 } 243 } 244 245 248 public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) { 249 if (supportsBreakpoint(breakpoint)) { 250 try { 251 if (breakpoint.isEnabled() && DebugPlugin.getDefault().getBreakpointManager().isEnabled()) { 252 breakpointAdded(breakpoint); 253 } else { 254 breakpointRemoved(breakpoint, null); 255 } 256 } catch (CoreException e) { 257 } 258 } 259 } 260 261 264 public boolean canDisconnect() { 265 return false; 266 } 267 268 271 public void disconnect() throws DebugException { 272 } 273 274 277 public boolean isDisconnected() { 278 return false; 279 } 280 281 284 public boolean supportsStorageRetrieval() { 285 return false; 286 } 287 288 291 public IMemoryBlock getMemoryBlock(long startAddress, long length) throws DebugException { 292 return null; 293 } 294 295 299 public void buildStarted() { 300 fireCreationEvent(); 301 installDeferredBreakpoints(); 302 try { 303 resume(); 304 } catch (DebugException e) { 305 } 306 } 307 308 312 private void installDeferredBreakpoints() { 313 IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager(); 314 if (!manager.isEnabled()) { 315 return; 316 } 317 IBreakpoint[] breakpoints = manager.getBreakpoints(IAntDebugConstants.ID_ANT_DEBUG_MODEL); 318 for (int i = 0; i < breakpoints.length; i++) { 319 IBreakpoint breakpoint= breakpoints[i]; 320 try { 321 if (breakpoint.isEnabled()) { 322 breakpointAdded(breakpoints[i]); 323 } 324 } catch (CoreException e) { 325 } 326 } 327 } 328 329 332 protected void terminated() { 333 fThreads= new IThread[0]; 334 fTerminated = true; 335 fSuspended = false; 336 if (DebugPlugin.getDefault() != null) { 337 DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this); 338 DebugPlugin.getDefault().removeDebugEventListener(this); 339 DebugPlugin.getDefault().getBreakpointManager().removeBreakpointManagerListener(this); 340 } 341 if (!getProcess().isTerminated()) { 342 try { 343 fProcess.terminate(); 344 resume(); 345 } catch (DebugException e) { 346 } 347 } 348 if (DebugPlugin.getDefault() != null) { 349 fireTerminateEvent(); 350 } 351 } 352 353 358 protected void stepOver() { 359 fSuspended= false; 360 fController.stepOver(); 361 } 362 363 368 protected void stepInto() { 369 fSuspended= false; 370 fController.stepInto(); 371 } 372 373 379 protected void breakpointHit(String event) { 380 String [] datum= event.split(DebugMessageIds.MESSAGE_DELIMITER); 382 String fileName= datum[1]; 383 int lineNumber = Integer.parseInt(datum[2]); 384 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(IAntDebugConstants.ID_ANT_DEBUG_MODEL); 385 boolean found= false; 386 for (int i = 0; i < breakpoints.length; i++) { 387 ILineBreakpoint lineBreakpoint = (ILineBreakpoint)breakpoints[i]; 388 if (setThreadBreakpoint(lineBreakpoint, lineNumber, fileName)) { 389 found= true; 390 break; 391 } 392 } 393 if (!found && fRunToLineBreakpoints != null) { 394 Iterator iter= fRunToLineBreakpoints.iterator(); 395 while (iter.hasNext()) { 396 ILineBreakpoint lineBreakpoint = (ILineBreakpoint) iter.next(); 397 if (setThreadBreakpoint(lineBreakpoint, lineNumber, fileName)) { 398 break; 399 } 400 } 401 } 402 suspended(DebugEvent.BREAKPOINT); 403 } 404 405 private boolean setThreadBreakpoint(ILineBreakpoint lineBreakpoint, int lineNumber, String fileName) { 406 try { 407 if (lineBreakpoint.getLineNumber() == lineNumber && 408 fileName.equals(lineBreakpoint.getMarker().getResource().getLocation().toOSString())) { 409 fThread.setBreakpoints(new IBreakpoint[]{lineBreakpoint}); 410 return true; 411 } 412 } catch (CoreException e) { 413 } 414 return false; 415 } 416 417 public void breakpointHit (IBreakpoint breakpoint) { 418 fThread.setBreakpoints(new IBreakpoint[]{breakpoint}); 419 suspended(DebugEvent.BREAKPOINT); 420 } 421 422 protected void getStackFrames() { 423 fController.getStackFrames(); 424 } 425 426 protected void getProperties() { 427 fController.getProperties(); 428 } 429 430 433 public void handleDebugEvents(DebugEvent[] events) { 434 for (int i = 0; i < events.length; i++) { 435 DebugEvent event = events[i]; 436 if (event.getKind() == DebugEvent.TERMINATE && event.getSource().equals(fProcess)) { 437 terminated(); 438 } 439 } 440 } 441 442 448 public void breakpointManagerEnablementChanged(boolean enabled) { 449 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(IAntDebugConstants.ID_ANT_DEBUG_MODEL); 450 for (int i = 0; i < breakpoints.length; i++) { 451 IBreakpoint breakpoint = breakpoints[i]; 452 if (enabled) { 453 breakpointAdded(breakpoint); 454 } else { 455 breakpointRemoved(breakpoint, null); 456 } 457 } 458 } 459 460 public IAntDebugController getAntDebugController() { 461 return fController; 462 } 463 } 464 | Popular Tags |