Fixed the debugger client to work in Eclipse 3.1.
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPDebugTarget.java
index 52f2cd5..c2ffdee 100644 (file)
@@ -11,6 +11,10 @@ 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 net.sourceforge.phpeclipse.PHPeclipsePlugin;
+
 import org.eclipse.core.resources.IMarkerDelta;
 import org.eclipse.debug.core.DebugEvent;
 import org.eclipse.debug.core.DebugException;
@@ -19,14 +23,14 @@ 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.IStackFrame;
 import org.eclipse.debug.core.model.IThread;
-
-import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
-import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.ui.model.IWorkbenchAdapter;
 
 /**
  * Debug target for PHP debug model.
@@ -45,6 +49,7 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
                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);
@@ -67,6 +72,7 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
                threads = updatedThreads;
 
                fireChangeEvent();
+               fireThreadCreateEvent(phpThread);
        }
 
        private void fireChangeEvent() {
@@ -74,6 +80,11 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
                DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
        }
 
+       private void fireThreadCreateEvent(PHPThread phpThread) {
+               DebugEvent ev = new DebugEvent(phpThread, DebugEvent.CREATE);
+               DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
+       }
+
        protected PHPThread getThreadById(int id) {
                for (int i = 0; i < threads.length; i++) {
                        if (threads[i].getId() == id) {
@@ -96,11 +107,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() {
@@ -119,11 +133,20 @@ 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;
                fireChangeEvent();
+               IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
+               manager.removeBreakpointListener(this);
+               DebugPlugin.getDefault().removeDebugEventListener(this);
        }
 
        public boolean canResume() {
@@ -184,6 +207,35 @@ public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugE
        }
 
        public Object getAdapter(Class arg0) {
+               if (IWorkbenchAdapter.class.equals(arg0)) {
+                       return new IWorkbenchAdapter() {
+                               public Object[] getChildren(Object o) {
+                                       Object[] children = null;
+                                       IThread[] threads = getThreads();
+                                       if (null != threads) {
+                                               children = new Object[threads.length];
+                                               for (int i = 0; i < threads.length; ++i) 
+                                                       children[i] = threads[i];
+                                       }
+                                       return children;
+                               }
+                               public ImageDescriptor getImageDescriptor(Object object) {
+                                       return null;
+                               }
+                               public String getLabel(Object o) {
+                                       String label = "(Unable to look up name... check error log)";
+                                       try {
+                                               label = getName();
+                                       } catch (DebugException x) {
+                                               PHPeclipsePlugin.log(label, x);
+                                       }
+                                       return label;
+                               }
+                               public Object getParent(Object o) {
+                                       return PHPDebugTarget.this.getLaunch();
+                               }
+                       };
+               }
                return null;
        }