1) Improvements for the XDebug plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugTarget.java
index 06064eb..79e6366 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * 
+ *
  */
 package net.sourceforge.phpeclipse.xdebug.php.model;
 
@@ -11,6 +11,7 @@ import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.xdebug.core.IPHPDebugEvent;
 import net.sourceforge.phpeclipse.xdebug.core.IProxyEventListener;
 import net.sourceforge.phpeclipse.xdebug.core.IXDebugPreferenceConstants;
@@ -36,6 +37,8 @@ import org.eclipse.debug.core.model.ILineBreakpoint;
 import org.eclipse.debug.core.model.IMemoryBlock;
 import org.eclipse.debug.core.model.IProcess;
 import org.eclipse.debug.core.model.IThread;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.ui.model.IWorkbenchAdapter;
 import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
@@ -51,18 +54,18 @@ import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugResponse;
  */
 public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugEventSetListener, IProxyEventListener {
        private IProcess fProcess;
-       
+
        private ILaunch fLaunch;
-       
+
        private int fDebugPort;
-       
+
        private boolean fSuspended = false;
-       
+
        private boolean fTerminated = false;
-       
+
        private XDebugThread fThread;
        private IThread[] fThreads;
-       
+
        private XDebugConnection fDebugConnection;
 
        private ResponseListener fResponseListener;
@@ -73,13 +76,13 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
        /**
         * Constructs a new debug target in the given launch and waits until
         * someone with the ideKey connects to the Debugproxy
-        *  
-        * 
+        *
+        *
         * @param launch containing launch
         * @param process process of the interpreter
-        * @param ideKey 
+        * @param ideKey
         * @exception CoreException if unable to connect to host
-        */     
+        */
        public XDebugTarget(ILaunch launch, IProcess process, String ideKey) throws CoreException {
                fLaunch = launch;
                fProcess = process;
@@ -87,16 +90,58 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                fThread = null;
                fThreads = new IThread[0];
                fIdeKey = ideKey;
-               
-               fDebugPort = XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);                
+
+               fDebugPort = XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
                if (fDebugPort == 0) {
                        fDebugPort = IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
                }
-               
+
                DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this);
                DebugPlugin.getDefault().addDebugEventListener(this);
        }
 
+       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 XDebugTarget.this.getLaunch();
+                       }
+                   };
+               }
+               else {
+                   if (arg0 == XDebugElement.class) {
+                       return this;
+                   }
+
+                   return super.getAdapter(arg0);
+               }
+           }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugTarget#getProcess()
         */
@@ -107,7 +152,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugTarget#getThreads()
         */
-       public IThread[] getThreads() throws DebugException {
+       public IThread[] getThreads() {
                return fThreads;
        }
 
@@ -129,7 +174,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
         * @see org.eclipse.debug.core.model.IDebugTarget#supportsBreakpoint(org.eclipse.debug.core.model.IBreakpoint)
         */
        public boolean supportsBreakpoint(IBreakpoint breakpoint) {
-               if (breakpoint.getModelIdentifier().equals(IXDebugConstants.ID_PHP_DEBUG_MODEL)) {
+               if (breakpoint.getModelIdentifier().equals(IXDebugConstants.ID_PHP_BREAKPOINT_MODEL)) {
                        return true;
                }
                return false;
@@ -173,16 +218,16 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                if(fTerminated) {
                        return;
                }
-               
+
                if (XDebugCorePlugin.getDefault() != null) {
                        XDebugProxy proxy = XDebugCorePlugin.getDefault().getXDebugProxy();
                        proxy.removeProxyEventListener(this, fIdeKey);
-                       
+
                        System.out.println("XDebug.Target: ProxyEventlistener removed");
-                       
+
                        fTerminated = true;
                        fSuspended = false;
-                       
+
                        XDebugCorePlugin.getBreakpointManager().removeBreakpointListener(this);
                        fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
                        DebugPlugin.getDefault().removeDebugEventListener(this);
@@ -218,29 +263,29 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        fThread.setBreakpoints(null);
                        resumed(DebugEvent.RESUME);
                        fDebugConnection.run();
-               }               
+               }
        }
-       
+
        /**
         * Notification the target has resumed for the given reason
-        * 
+        *
         * @param detail reason for the resume
         */
        private void resumed(int detail) {
                fSuspended = false;
                fThread.fireResumeEvent(detail);
        }
-       
+
        /**
         * Notification the target has suspended for the given reason
-        * 
+        *
         * @param detail reason for the suspend
         */
        public void suspended(int detail) {
                fSuspended = true;
                fThread.fireSuspendEvent(detail);
-       }       
-       
+       }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
         */
@@ -251,10 +296,11 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
         * @see org.eclipse.debug.core.IBreakpointListener#breakpointAdded(org.eclipse.debug.core.model.IBreakpoint)
         */
        public void breakpointAdded(IBreakpoint breakpoint) {
-               IMarker marker = breakpoint.getMarker();
-               IPath path = marker.getResource().getLocation();
-               IPath cp = path.removeLastSegments(1);
-               List pathMap = null;
+               IMarker marker   = breakpoint.getMarker();                  // Get the breakpoints marker info (It's the local workspace path)
+               IPath   path     = marker.getResource().getLocation();      // Get the full path + file for the given breakpoint (It's the local real path)
+               IPath   cp       = path.removeLastSegments(1);              // Get the full path only (without the file name)
+               List    pathMap  = null;
+
                try {
                        pathMap = fLaunch.getLaunchConfiguration().getAttribute(IXDebugConstants.ATTR_PHP_PATHMAP,(List)null);
                } catch (CoreException e2) {
@@ -262,63 +308,68 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        e2.printStackTrace();
                }
 
-               if (fDebugConnection != null)
-               if (!fDebugConnection.isClosed()) {
-                       if (fProcess == null) {
-                               PathMapItem pmi = null;
-                               for (int i = 0; i < pathMap.size(); i++) {
-                                       pmi = new PathMapItem((String) pathMap.get(i));
-                                       IPath local = (IPath)pmi.getLocalPath().clone();
-                                       local = local.makeAbsolute();
-                                       int matchedSegments = local.segmentCount();
-                                       if (local.matchingFirstSegments(cp) == matchedSegments) {
-                                               IPath newPath = pmi.getRemotePath();
-                                               newPath = newPath.append(path.removeFirstSegments(matchedSegments));
-                                               newPath = newPath.makeAbsolute();
-                                               if (supportsBreakpoint(breakpoint)) {
-                                                       try {
-                                                               if (breakpoint.isEnabled()) {
-                                                                       if (marker != null) {
-                                                                               int id = fDebugConnection.breakpointSet(newPath.toString(), ((ILineBreakpoint)breakpoint).getLineNumber(), marker.getAttribute(XDebugBreakpoint.HIT_COUNT,-1));
-                                                                               XDebugResponse dr = getResponse(id);
-                                                                               
-                                                                               String bpid = dr.getAttributeValue("id");
-                                                                               
-                                                                               if (!"".equals(bpid))
-                                                                                       marker.setAttribute(XDebugLineBreakpoint.BREAKPOINT_ID,Integer.parseInt(bpid));
-                                                                       }
+               if ((fDebugConnection != null) &&                           // If there is a connection to XDebug
+                   (!fDebugConnection.isClosed ()) &&                      // and this connection is not closed
+                       (fProcess == null)) {                                   //
+                       PathMapItem pmi = null;
+
+                       for (int i = 0; i < pathMap.size(); i++) {              // For every path map pair the user have set
+                               pmi                 = new PathMapItem((String) pathMap.get(i));   // Get the path map pair
+                               IPath local         = (IPath)pmi.getLocalPath().clone();          // Get the local
+                               local               = local.makeAbsolute();
+                               int matchedSegments = local.segmentCount();
+
+                               if (local.matchingFirstSegments(cp) == matchedSegments) {
+                                       IPath newPath = pmi.getRemotePath();
+                                       newPath       = newPath.append(path.removeFirstSegments(matchedSegments));
+                                       newPath       = newPath.makeAbsolute();
+
+                                       if (supportsBreakpoint(breakpoint)) {
+                                               try {
+                                                       if (breakpoint.isEnabled()) {
+                                                               if (marker != null) {
+                                                                       int id = fDebugConnection.breakpointSet(newPath.toString(), ((ILineBreakpoint)breakpoint).getLineNumber(), marker.getAttribute(XDebugBreakpoint.HIT_COUNT,-1));
+                                                                       XDebugResponse dr = getResponse(id);
+
+                                                                       String bpid = dr.getAttributeValue("id");
+
+                                                                       if (!"".equals(bpid))
+                                                                               marker.setAttribute(XDebugLineBreakpoint.BREAKPOINT_ID,Integer.parseInt(bpid));
                                                                }
-                                                       } catch (DebugException e) {
-                                                               e.printStackTrace();
-                                                       } catch (CoreException e) {
-                                                               e.printStackTrace();
                                                        }
+                                               } catch (DebugException e) {
+                                                       e.printStackTrace();
+                                               } catch (CoreException e) {
+                                                       e.printStackTrace();
                                                }
                                        }
-                               }                       
-                       } else {
-                               if (supportsBreakpoint(breakpoint)) {
-                                       try {
-                                               if (breakpoint.isEnabled()) {
-                                                       if (marker != null) {
-                                                               int id = fDebugConnection.breakpointSet(path.toString(), ((ILineBreakpoint)breakpoint).getLineNumber(), marker.getAttribute(XDebugBreakpoint.HIT_COUNT,-1));
-                                                               XDebugResponse dr = getResponse(id);
-                                                               String bpid = dr.getAttributeValue("id");
-                                                               
-                                                               if (!"".equals(bpid))
-                                                                       marker.setAttribute(XDebugLineBreakpoint.BREAKPOINT_ID,Integer.parseInt(bpid));
+                               }
+                       }
+               } else {
+                       if (supportsBreakpoint(breakpoint)) {
+                               try {
+                                       if (breakpoint.isEnabled()) {
+                                               if (marker != null) {
+                                                       int            id   = fDebugConnection.breakpointSet (path.toString(),
+                                                                                                             ((ILineBreakpoint) breakpoint).getLineNumber(),
+                                                                                                                                                                 marker.getAttribute (XDebugBreakpoint.HIT_COUNT, -1));
+                                                       XDebugResponse dr   = getResponse(id);
+                                                       String         bpid = dr.getAttributeValue("id");
+
+                                                       if (!"".equals(bpid)) {
+                                                               marker.setAttribute(XDebugLineBreakpoint.BREAKPOINT_ID,Integer.parseInt(bpid));
                                                        }
                                                }
-                                       } catch (DebugException e) {
-                                               e.printStackTrace();
-                                       } catch (CoreException e) {
-                                               e.printStackTrace();
                                        }
+                               } catch (DebugException e) {
+                                       e.printStackTrace();
+                               } catch (CoreException e) {
+                                       e.printStackTrace();
                                }
                        }
                }
        }
-       
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.IBreakpointListener#breakpointRemoved(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
         */
@@ -410,7 +461,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                if (response2.getAttributeValue("success").equals("1") ) {
                        System.out.println("Set children to 1024 (hack)");
                }
-               
+
                installDeferredBreakpoints();
                try {
                        resume();
@@ -418,7 +469,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        e.printStackTrace();
                }
        }
-       
+
        /**
         * Install breakpoints that are already registered with the breakpoint
         * manager.
@@ -429,10 +480,10 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        breakpointAdded(breakpoints[i]);
                }
        }
-       
+
        /**
         * Returns the current stack frames in the target.
-        * 
+        *
         * @return the current stack frames in the target
         * @throws DebugException if unable to perform the request
         */
@@ -441,10 +492,10 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                XDebugResponse lastResponse = getResponse(id);
                return lastResponse;
        }
-       
+
        /**
         * Single step the interpreter.
-        * 
+        *
         * @throws DebugException if the request fails
         */
        protected void step_over() throws DebugException {
@@ -452,10 +503,10 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                resumed(DebugEvent.STEP_OVER);
                fDebugConnection.stepOver();
        }
-       
+
        /**
         * Single step the interpreter.
-        * 
+        *
         * @throws DebugException if the request fails
         */
        protected void step_into() throws DebugException {
@@ -463,10 +514,10 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                resumed(DebugEvent.STEP_INTO);
                fDebugConnection.stepInto();
        }
-       
+
        /**
         * Single step the interpreter.
-        * 
+        *
         * @throws DebugException if the request fails
         */
        protected void step_out() throws DebugException {
@@ -474,18 +525,18 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                resumed(DebugEvent.STEP_RETURN);
                fDebugConnection.stepOut();
        }
-       
+
        public boolean setVarValue(String name, String value) {
                int id = fDebugConnection.setVarValue(name,value);
                XDebugResponse response = getResponse(id);
-               
+
                if ((response.getAttributeValue("success")).equals("1")) {
                        return true;
                } else {
                        return false;
                }
        }
-       
+
        public Node eval(String expression) throws DebugException {
                Node evalProperty = null;
                if (fDebugConnection != null) {
@@ -493,17 +544,17 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        //Node evalProperty = new Node("");
                        //if (id > 0) {
                                XDebugResponse response = getResponse(id);
-               
+
                                Node evalResponse = response.getParentNode();
                                /*Node*/ evalProperty = evalResponse.getFirstChild();
                        //} /*else {
-                               
+
                        //}*/
                } else {
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder builder = null;
                        Document doc = null;
-                       
+
                        try {
                                builder = factory.newDocumentBuilder();
                        } catch (ParserConfigurationException e) {
@@ -518,14 +569,14 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                                e.printStackTrace();
                        }*/
                }
-               
+
                return evalProperty;
        }
-       
+
        public void handleDebugEvents(DebugEvent[] events) {
                for (int i = 0; i < events.length; i++) {
                        DebugEvent event = events[i];
-                       
+
                        if (fResponseListener != null) {
                                Object s = null;
                                s = event.getSource();
@@ -537,7 +588,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        } else {
                                return;
                        }
-                       
+
                        if (event.getKind() == DebugEvent.MODEL_SPECIFIC) {
                                switch (event.getDetail()) {
                                        case IPHPDebugEvent.BREAKPOINT_HIT:
@@ -545,7 +596,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                                                XDebugResponse lastResponse = getResponse(id);
 
                                                IBreakpoint breakpoint = breakpointHit(lastResponse.getParentNode());
-                                               
+
                                                if (breakpoint != null) {
                                                        fThread.setBreakpoints(new IBreakpoint[]{breakpoint});
                                                        fThread.incrementStepCounter();
@@ -569,7 +620,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        }
                }
        }
-       
+
        public void stopped() {
                if(fDebugConnection == null) {
                        return;
@@ -593,15 +644,15 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        fDebugConnection = null;
                        fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CONTENT));
                }
-               
+
                fThread.removeEventListeners();
                fThread = null;
                fThreads = new IThread[0];
        }
-       
+
        public void handleProxyEvent(XDebugConnection connection) {
                //System.out.println("* New Connection - XDebug.Target: " + fDebugConnection.getSessionID());
-               
+
                if (setDebugConnection(connection)) {
                        fThread = new XDebugThread(this);
                        fThreads = new IThread[] {fThread};
@@ -609,7 +660,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        try {
                                started();
                        } catch( DebugException e ){
-                               e.printStackTrace();            
+                               e.printStackTrace();
                        }
                }
        }
@@ -619,22 +670,22 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        fDebugConnection = connection;
                        fResponseListener = new ResponseListener(connection);
                        startListener();
-                       
+
                        return true;
                } else {
                        connection.close();
-                       
+
                        return false;
                }
        }
-       
+
        /**
         * @return Returns the fDebugConnection.
         */
        public XDebugConnection getDebugConnection() {
                return fDebugConnection;
-       }       
-       
+       }
+
        public void addProcess(IProcess p) {
                fProcess = p;
 
@@ -642,47 +693,47 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
        public Node getLocalVariables(int level) throws DebugException {
                int id = fDebugConnection.contextGet(level, 0);
                XDebugResponse response = getResponse(id);
-               
+
                return response.getParentNode();
        }
-       
+
        public Node getGlobalVariables(int level) throws DebugException {
                int id = fDebugConnection.contextGet(level, 1);
                XDebugResponse response = getResponse(id);
-               
+
                return response.getParentNode();
        }
-       
+
        public void stop() {
                fDebugConnection.stop();
        }
-       
+
        protected IBreakpoint breakpointHit(Node node) {
                Node child = node.getFirstChild();
                if (child.getNodeName().equals("stack")) {
                        int lineNumber = Integer.parseInt(PHPDebugUtils.getAttributeValue(child, "lineno"));
-                       String filename = PHPDebugUtils.getAttributeValue(child, "filename");  
+                       String filename = PHPDebugUtils.getAttributeValue(child, "filename");
                        IBreakpoint[] breakpoints = XDebugCorePlugin.getBreakpoints();
                        for (int i = 0; i < breakpoints.length; i++) {
                                IBreakpoint breakpoint = breakpoints[i];
                                if (supportsBreakpoint(breakpoint)) {
                                        if (breakpoint instanceof ILineBreakpoint) {
                                                ILineBreakpoint lineBreakpoint = (ILineBreakpoint) breakpoint;
-                                               try {                                           
+                                               try {
                                                        if (breakpoint.isEnabled()) {
                                                                IMarker marker = breakpoint.getMarker();
                                                                if (marker != null) {
                                                                        String endfilename;
-                                                                       
+
                                                                        if (getProcess() == null) {
-                                                                               endfilename = marker.getResource().getLocation().lastSegment(); 
+                                                                               endfilename = marker.getResource().getLocation().lastSegment();
                                                                        } else {
                                                                                endfilename = marker.getResource().getLocation().toOSString();
                                                                        }
-                                                                       
+
                                                                        int id = fDebugConnection.breakpointGet(marker.getAttribute(XDebugLineBreakpoint.BREAKPOINT_ID,-1));
                                                                        XDebugResponse dr = getResponse(id);
-                                                                       
+
                                                                        Node hitCo = dr.getParentNode().getFirstChild();
                                                                        int hitCount = 0;
                                                                        if (hitCo.hasAttributes()) {
@@ -697,7 +748,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                                                                                        && (lineBreakpoint.getLineNumber() == lineNumber) ) {
                                                                                if (marker.getAttribute(XDebugLineBreakpoint.HIT_COUNT, 0) > 0) {
                                                                                        if (marker.getAttribute(XDebugLineBreakpoint.HIT_COUNT, 0) == hitCount) {
-                                                                                               return (breakpoint);                                                                                            
+                                                                                               return (breakpoint);
                                                                                        }
                                                                                } else {
                                                                                        return (breakpoint);
@@ -711,14 +762,14 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                                }
                        }
                }
-               
+
                return null;
        }
-       
+
        public void startListener() {
                fResponseListener.schedule();
        }
-       
+
        public void stopListener() {
                fResponseListener.cancel();
        }
@@ -727,4 +778,4 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
 
                return response;
        }
-}
\ No newline at end of file
+}