Added a comment about fixing a live leak later.
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPDebugTarget.java
index 9ecd65e..4a9dccb 100644 (file)
@@ -11,6 +11,9 @@ Contributors:
 **********************************************************************/
 package net.sourceforge.phpdt.internal.debug.core.model;
 
+import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
+import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
+
 import org.eclipse.core.resources.IMarkerDelta;
 import org.eclipse.debug.core.DebugEvent;
 import org.eclipse.debug.core.DebugException;
@@ -19,15 +22,12 @@ import org.eclipse.debug.core.IBreakpointManager;
 import org.eclipse.debug.core.IDebugEventSetListener;
 import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchListener;
-import org.eclipse.debug.core.model.IDebugTarget;
 import org.eclipse.debug.core.model.IBreakpoint;
+import org.eclipse.debug.core.model.IDebugTarget;
 import org.eclipse.debug.core.model.IMemoryBlock;
 import org.eclipse.debug.core.model.IProcess;
 import org.eclipse.debug.core.model.IThread;
 
-import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
-import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
-
 /**
  * Debug target for PHP debug model.
  */
@@ -35,14 +35,17 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
                
        private IProcess process;
        private boolean isTerminated;
+       private boolean isSuspended;
        private ILaunch launch;
        private PHPThread[] threads;
        private PHPDBGProxy phpDBGProxy;
 
        public PHPDebugTarget(ILaunch launch, IProcess process) {
+               this.isSuspended = false;
                this.launch = launch;
                this.process = process;
                this.threads = new PHPThread[0];
+               // TODO XXX remove breakpoint listener at termination to avoid live leak
                IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
                manager.addBreakpointListener(this);
                DebugPlugin.getDefault().addDebugEventListener(this);
@@ -94,11 +97,14 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
        }
 
        public boolean supportsBreakpoint(IBreakpoint arg0) {
+           if(arg0.getModelIdentifier().equals(PHPDebugCorePlugin.PLUGIN_ID)) {
+            return true;
+           }
                return false;
        }
 
        public String getModelIdentifier() {
-               return PHPDebugCorePlugin.getUniqueIdentifier();
+               return PHPDebugCorePlugin.PLUGIN_ID;
        }
 
        public IDebugTarget getDebugTarget() {
@@ -117,7 +123,13 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
                return isTerminated;
        }
 
-       public void terminate() {
+       public synchronized void terminate() {
+               // This method is synchronized to control a race condition between the 
+               // UI thread that terminates the debugging session, and the slave 
+               // thread that executes PHPLoop.run
+               if (isTerminated)
+                       // Avoid terminating twice...
+                       return;
                phpDBGProxy.stop();
                this.threads = new PHPThread[0];
                isTerminated = true;
@@ -125,21 +137,27 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
        }
 
        public boolean canResume() {
-               return false;
+               if(isTerminated) return false;
+               return isSuspended;
        }
 
        public boolean canSuspend() {
-               return false;
+               if(isTerminated) return false;
+               return !isSuspended;
        }
 
        public boolean isSuspended() {
-               return false;
+               return isSuspended;
        }
 
        public void resume() throws DebugException {
+               this.getPHPDBGProxy().resume();
+               isSuspended= false;
        }
 
        public void suspend() throws DebugException {
+               this.getPHPDBGProxy().pause();
+               isSuspended= true;
        }
 
        public void breakpointAdded(IBreakpoint breakpoint) {
@@ -231,9 +249,15 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
                        DebugEvent event = events[i];
                        if (event.getKind() == DebugEvent.TERMINATE) {
                                Object source = event.getSource();
-                               if (source instanceof PHPDebugTarget || source instanceof IDebugTarget || source instanceof IProcess) {
+                               if (source instanceof PHPDebugTarget || source instanceof IDebugTarget) {
                                        getPHPDBGProxy().stop();
+                               } else if(source instanceof IProcess) {
+                                       if(getDebugTarget().getProcess() == (IProcess)source) {
+                                               getPHPDBGProxy().stop();
+                                       }
                                }
+                       } else if (event.getKind() == DebugEvent.SUSPEND) {
+                               getPHPDBGProxy().pause();
                        }
                }
        }