refactory whole plugin
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugTarget.java
index b8d3f86..d032172 100644 (file)
@@ -3,21 +3,31 @@
  */
 package net.sourceforge.phpeclipse.xdebug.php.model;
 
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.List;
+
 import net.sourceforge.phpeclipse.xdebug.core.Base64;
-import net.sourceforge.phpeclipse.xdebug.core.DebugConnection;
+import net.sourceforge.phpeclipse.xdebug.core.IPHPDebugEvent;
+import net.sourceforge.phpeclipse.xdebug.core.IProxyEventListener;
 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
+import net.sourceforge.phpeclipse.xdebug.core.PathMapItem;
 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
-import net.sourceforge.phpeclipse.xdebug.core.DebugConnection.DebugResponse;
+import net.sourceforge.phpeclipse.xdebug.core.XDebugProxy;
 import net.sourceforge.phpeclipse.xdebug.php.launching.IXDebugConstants;
 
 import org.eclipse.core.resources.IMarker;
 import org.eclipse.core.resources.IMarkerDelta;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
 import org.eclipse.debug.core.DebugEvent;
 import org.eclipse.debug.core.DebugException;
 import org.eclipse.debug.core.DebugPlugin;
 import org.eclipse.debug.core.IDebugEventSetListener;
 import org.eclipse.debug.core.ILaunch;
+
+import org.eclipse.debug.core.ILaunchListener;
+
 import org.eclipse.debug.core.model.IBreakpoint;
 import org.eclipse.debug.core.model.IDebugTarget;
 import org.eclipse.debug.core.model.ILineBreakpoint;
@@ -25,90 +35,98 @@ 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 org.eclipse.debug.core.model.IValue;
+//import org.eclipse.debug.core.model.IValue;
+import org.eclipse.debug.core.model.IVariable;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection;
+import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.DebugResponse;
+
 /**
  * @author Christian
  *
  */
-public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugEventSetListener{
-       // associated system process (VM)
+public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunchListener, IDebugEventSetListener, IProxyEventListener {
        private IProcess fProcess;
        
-       // containing launch object
        private ILaunch fLaunch;
        
-       // debugPort
        private int fDebugPort;
        
-       // program name
-//     private String fName;
-       
-       
-       // suspend state
-       private boolean fSuspended = true;
+       private boolean fSuspended = false;
        
-       // terminated state
        private boolean fTerminated = false;
        
-       // threads
        private XDebugThread fThread;
        private IThread[] fThreads;
        
-       // event dispatch job
-//     private EventDispatchJob fEventDispatch;
-       
+       private XDebugConnection fDebugConnection;
 
-       private DebugConnection fDebugConnection;
-//     private DebugResponse lastResponse;
+       private String fIdeKey;
 
 
-       
        /**
-        * Constructs a new debug target in the given launch for the 
-        * associated PDA VM process.
+        * 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 debugPort port to read events from
+        * @param process process of the interpreter
+        * @param ideKey 
+        * @param pathMap Pathmap for the debug session
         * @exception CoreException if unable to connect to host
-        */
-       public XDebugTarget(ILaunch launch, IProcess process, int debugPort) throws CoreException {
-               super(null);
+        */     
+       public XDebugTarget(ILaunch launch, IProcess process, String ideKey) throws CoreException {
                fLaunch = launch;
                fProcess = process;
                fTarget = this;
-               fDebugConnection= new DebugConnection(this,debugPort);
-               fThread = new XDebugThread(this);
-               fThreads = new IThread[] {fThread};
+               fDebugConnection = null;
+               fThread = null;
+               fThreads = new IThread[0];
+               fIdeKey = ideKey;
+               
+               XDebugProxy proxy = XDebugCorePlugin.getDefault().getXDebugProxy();
+               proxy.addProxyEventListener(this,ideKey);
+               if (!proxy.isRunning())
+                       proxy.start();
+               
+               proxy.setTarget(this);
+
+               fDebugPort = proxy.getProxyPort();
+               
                DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this);
                DebugPlugin.getDefault().addDebugEventListener(this);
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugTarget#getProcess()
         */
        public IProcess getProcess() {
                return fProcess;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugTarget#getThreads()
         */
        public IThread[] getThreads() throws DebugException {
                return fThreads;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugTarget#hasThreads()
         */
        public boolean hasThreads() throws DebugException {
                return (fThreads.length>0);
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugTarget#getName()
         */
        public String getName() throws DebugException {
                return "PHP XDebug Client at localhost:" + fDebugPort;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugTarget#supportsBreakpoint(org.eclipse.debug.core.model.IBreakpoint)
         */
@@ -118,25 +136,30 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                }
                return false;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
         */
        public IDebugTarget getDebugTarget() {
                return this;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
         */
        public ILaunch getLaunch() {
                return fLaunch;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
         */
        public boolean canTerminate() {
-               return getProcess().canTerminate();
-//             return false;
+               if (getProcess()!=null)  // ther is no running Process in remote debugging
+                       return getProcess().canTerminate();
+               return true;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
         */
@@ -144,35 +167,54 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
 //             return getProcess().isTerminated();
                return fTerminated;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.ITerminate#terminate()
         */
        public void terminate() throws DebugException {
-               fDebugConnection.sendRequest ("stop");
+               XDebugProxy proxy=XDebugCorePlugin.getDefault().getXDebugProxy();
+               proxy.stop();
+               proxy.removeProxyEventListener(this,fIdeKey);
+               System.out.println("XDebug.Target: ProxyEventlistener removed");
+               fTerminated = true;
+               //fDisconnected = true;
+               fSuspended = false;
+               XDebugCorePlugin.getBreakpointManager().removeBreakpointListener(this);
+               fireTerminateEvent();
+               DebugPlugin.getDefault().removeDebugEventListener(this);
+               if (fThread!=null)
+                       fThread.removeEventListeners();
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
         */
        public boolean canResume() {
-               return !isTerminated() && isSuspended();
+               return false;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
         */
        public boolean canSuspend() {
-               return !isTerminated() && !isSuspended();
+               return false;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
         */
        public boolean isSuspended() {
                return fSuspended;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.ISuspendResume#resume()
         */
        public void resume() throws DebugException {
-               fDebugConnection.sendRequest("run");
+               if (fDebugConnection != null) {
+                       fThread.setBreakpoints(null);
+                       fDebugConnection.run();
+               }                       
        }
        
        /**
@@ -200,33 +242,76 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
         */
        public void suspend() throws DebugException {
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.IBreakpointListener#breakpointAdded(org.eclipse.debug.core.model.IBreakpoint)
         */
        public void breakpointAdded(IBreakpoint breakpoint) {
-               
-               if (supportsBreakpoint(breakpoint)) {
-                       try {
-                               if (breakpoint.isEnabled()) {
-                                       IMarker marker = breakpoint.getMarker();
-                                       if (marker != null) {
-                                               try {
-                                                       String fileName = PHPDebugUtils.escapeString(marker.getResource().getLocation().toString());
-                                                       String arg="-t line -f file:///"+fileName+" -n "+((ILineBreakpoint)breakpoint).getLineNumber();
-                                                       int id =fDebugConnection.sendRequest("breakpoint_set",arg);
-       // set the marker Attribute to make later idetification possible
-// TODO: make sure that attribute is set before response from debugger is beeing prosessed                                             
-                                                       marker.setAttribute(XDebugLineBreakpoint.BREAKPOINT_ID,id);
-                                                       
-                                               } catch (CoreException e) {
+               IMarker marker = breakpoint.getMarker();
+               IPath path = marker.getResource().getLocation();
+               IPath cp = path.removeLastSegments(1);
+               List pathMap = null;
+               try {
+                       pathMap = fLaunch.getLaunchConfiguration().getAttribute(IXDebugConstants.ATTR_PHP_PATHMAP,(List)null);
+               } catch (CoreException e2) {
+                       // TODO Auto-generated catch block
+                       e2.printStackTrace();
+               }
+
+               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) {
+                                                                               //fDebugConnection.addBreakpoint(breakpoint, newPath);
+                                                                               DebugResponse dr = fDebugConnection.breakpointSet(newPath.toString(), ((ILineBreakpoint)breakpoint).getLineNumber());
+                                                                               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();
+                                                       }
+                                               }
+                                       }
+                               }                       
+                       } else {
+                               if (supportsBreakpoint(breakpoint)) {
+                                       try {
+                                               if (breakpoint.isEnabled()) {
+                                                       if (marker != null) {
+                                                               DebugResponse dr = fDebugConnection.breakpointSet(path.toString(), ((ILineBreakpoint)breakpoint).getLineNumber());
+                                                               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 (CoreException e) {
-
                        }
                }
        }
+       
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.IBreakpointListener#breakpointRemoved(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
         */
@@ -235,11 +320,12 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                        try {
                                int id =((XDebugLineBreakpoint)breakpoint).getID();
                                if (id >0)
-                                       fDebugConnection.sendRequest("breakpoint_remove","-d "+id);
+                                       fDebugConnection.breakpointRemove(id);
                        } catch (CoreException e) {
                        }
                }
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.IBreakpointListener#breakpointChanged(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
         */
@@ -255,29 +341,34 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
 //                     }
 //             }
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDisconnect#canDisconnect()
         */
        public boolean canDisconnect() {
                return false;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDisconnect#disconnect()
         */
        public void disconnect() throws DebugException {
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IDisconnect#isDisconnected()
         */
        public boolean isDisconnected() {
-               return false;
+               return (fDebugConnection==null);
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IMemoryBlockRetrieval#supportsStorageRetrieval()
         */
        public boolean supportsStorageRetrieval() {
                return false;
        }
+
        /* (non-Javadoc)
         * @see org.eclipse.debug.core.model.IMemoryBlockRetrieval#getMemoryBlock(long, long)
         */
@@ -286,19 +377,34 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
        }
 
        /**
-        * Notification we have connected to the PHP debugger and it has started.
+        * Notification we have connected to the PHP debugger and it has been started.
         * Resume the the debugger.
         */
-       public void started() {
-
+       public void started() throws DebugException {
                fThread.setBreakpoints(null);
                fThread.setStepping(false);
 
+               Integer.parseInt(fDebugConnection.featureGet("detach").getValue());
+
+               System.out.println("in Target.started()");
+               DebugResponse response = fDebugConnection.featureGet("max_children");
+               String a1 = response.getValue();
+               System.out.println("max children:"+a1);
+               DebugResponse response1 = fDebugConnection.featureGet("max_children");
+               String a2 = response1.getValue();
+               System.out.println("max depth:"+a2);
+               
+               
+               boolean res = fDebugConnection.featureSet("max_depth", "100" );
+               if( res == true ) {
+                       System.out.println("Set depth to 100 (hack)");
+               }
+               
                installDeferredBreakpoints();
                try {
                        resume();
-//                     step();
                } catch (DebugException e) {
+                       e.printStackTrace();
                }
        }
        
@@ -322,7 +428,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                XDebugCorePlugin.getBreakpointManager().removeBreakpointListener(this);
                fireTerminateEvent();
                DebugPlugin.getDefault().removeDebugEventListener(this);
-               fThread.removeEventListeners();
+               fThread.removeEventListeners(); 
        }
        
        /**
@@ -331,9 +437,9 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
         * @return the current stack frames in the target
         * @throws DebugException if unable to perform the request
         */
-       protected IStackFrame[] getStackFrames() throws DebugException {
-               int id=fDebugConnection.sendRequest("stack_get");
-               DebugResponse lastResponse=fDebugConnection.waitforTransID(id);
+       public IStackFrame[] getStackFrames() throws DebugException {
+               DebugResponse lastResponse = fDebugConnection.stackGet(0);
+
                if (lastResponse.isError())
                        return new IStackFrame[0];
                Node response = lastResponse.getParentNode();
@@ -341,8 +447,31 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                IStackFrame[] theFrames = new IStackFrame[frames.getLength()];
                for (int i = 0; i < frames.getLength(); i++) {
                        Node stackNode = frames.item(i);
-                       theFrames[i] = new XDebugStackFrame(fThread, stackNode, i);
+                       XDebugStackFrame frame = new XDebugStackFrame(fThread, i);
+                       String level =PHPDebugUtils.getAttributeValue(stackNode,"level");
+                       if (!"".equals(level))
+                               frame.setLevel(Integer.parseInt(level));
+
+                       frame.setType(PHPDebugUtils.getAttributeValue(stackNode,"type"));
+                       String fileName=PHPDebugUtils.unescapeString(PHPDebugUtils.getAttributeValue(stackNode,"filename"));
+                       String lineNo=PHPDebugUtils.getAttributeValue(stackNode,"lineno");
+
+                       if (!"".equals(lineNo))
+                               frame.setLineNumber(Integer.parseInt(lineNo));
+                       
+                       frame.setWhere(PHPDebugUtils.getAttributeValue(stackNode,"where"));
+                       
+                       try {
+                               frame.setFullName(new URL(fileName));
+                       } catch (MalformedURLException e) {
+                               e.printStackTrace();
+                       }
+
+                       frame.incrementStepCounter();
+                       
+                       theFrames[i] = frame;
                }
+               
                return theFrames;
        }
        
@@ -354,7 +483,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
        protected void step_over() throws DebugException {
                fThread.setStepping(true);
                resumed(DebugEvent.STEP_OVER);
-               fDebugConnection.sendRequest("step_over");
+               fDebugConnection.stepOver();
        }
        
        /**
@@ -365,7 +494,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
        protected void step_into() throws DebugException {
                fThread.setStepping(true);
                resumed(DebugEvent.STEP_INTO);
-               fDebugConnection.sendRequest("step_into");
+               fDebugConnection.stepInto();
        }
        
        /**
@@ -376,10 +505,8 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
        protected void step_out() throws DebugException {
                fThread.setStepping(true);
                resumed(DebugEvent.STEP_RETURN);
-               fDebugConnection.sendRequest("step_out");
+               fDebugConnection.stepOut();
        }
-
-
        
        /**
         * Returns the current value of the given variable.
@@ -388,104 +515,221 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
         * @return variable value
         * @throws DebugException if the request fails
         */
-       protected IValue getVariableValue(XDebugVariable variable) throws DebugException {
-//             synchronized (fDebugSocket) {
-//                     fDebugConnection.sendRequest("var","" + variable.getStackFrame().getIdentifier() + " " + variable.getName());
-//                     try {
-//                             String value = fDebugReader.readLine();
-//                             //return new XDebugValue(this, value);
-//                             
-//                     } catch (IOException e) {
-//                             abort(MessageFormat.format("Unable to retrieve value for variable {0}", new String[]{variable.getName()}), e);
-//                     }
-//             }
+       /*protected IValue getVariableValue(XDebugVariable variable) throws DebugException {
                return null;
-       }
+       }*/
        
        /**
         * Returns the values on the data stack (top down)
         * 
         * @return the values on the data stack (top down)
         */
-       public IValue[] getDataStack() throws DebugException {
-//             synchronized (fDebugSocket) {
-//                     fDebugConnection.sendRequest ("data");
-//                     try {
-//                             String valueString = fDebugReader.readLine();
-//                             if (valueString != null && valueString.length() > 0) {
-//                                     String[] values = valueString.split("\\|");
-//                                     IValue[] theValues = new IValue[values.length];
-//                                     for (int i = 0; i < values.length; i++) {
-//                                             String value = values[values.length - i - 1];
-////                                           theValues[i] = new XDebugValue(this, value);
-//                                     }
-//                                     return theValues;
-//                             }
-//                     } catch (IOException e) {
-//                             abort("Unable to retrieve data stack", e);
-//                     }
-//             }
+       /*public IValue[] getDataStack() throws DebugException {
                return new IValue[0];           
-       }
+       }*/
        
        public boolean setVarValue(String name, String value) {
-               int id=-1;
-               String str=Base64.encodeBytes(value.getBytes());
-               int len=str.length();
+               return fDebugConnection.setVarValue(name,value);
+       }
+       
+       public void handleDebugEvents(DebugEvent[] events) {
+               for (int i = 0; i < events.length; i++) {
+                       DebugEvent event = events[i];
+                       if (event.getKind() == DebugEvent.MODEL_SPECIFIC) {
+                               switch (event.getDetail()) {
+                                       case IPHPDebugEvent.BREAKPOINT_HIT:
+                                               DebugResponse lastResponse = ((XDebugConnection) fDebugConnection).stackGet(0);
 
-               try {
-                       id =fDebugConnection.sendRequest("property_set","-n "+name+" -l "+len + " -- "+str);
-               } catch (DebugException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
+                                               IBreakpoint breakpoint = breakpointHit(lastResponse.getParentNode());
+       
+                                               fThread.setBreakpoints(new IBreakpoint[]{breakpoint});
+                                               fThread.incrementStepCounter();
+                                               suspended(DebugEvent.BREAKPOINT);
+                                               break;
+                                       case IPHPDebugEvent.STEP_END:
+                                               fThread.incrementStepCounter();
+                                               suspended(DebugEvent.STEP_END);
+                                               break;
+                                       case IPHPDebugEvent.STOPPED:
+                                               fThread.removeEventListeners();
+                                               fThread = null;
+                                               fThreads = new IThread[0];
+
+                                               fDebugConnection.close();
+
+                                               fSuspended = false;
+
+                                               // Dirty hack to check debugging mode (remote or local)
+                                               if (fProcess!=null) {
+                                                       try {
+                                                               terminate();
+                                                       } catch (DebugException e) {
+                                                               e.printStackTrace();
+                                                       }
+                                               } else {
+                                                       fDebugConnection = null;
+                                                       fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CONTENT));
+                                               }
+                                               break;
+                               }
+                       }
                }
-               DebugResponse dr=getResponse(id);
-               if ((dr.getAttributeValue("success")).equals("1"))
-                       return true;
-               
-               return false;
        }
-       
-       public DebugResponse getResponse(int id) {
-               return fDebugConnection.waitforTransID(id);
+
+       public void handleProxyEvent(String ideKey, String initString, /*AbstractDebugConnection*/ XDebugConnection connection) {
+               System.out.println("* New Connection - XDebug.Target: "+ideKey);
+               setDebugConnection(connection);
+               
+               XDebugProxy proxy=XDebugCorePlugin.getDefault().getXDebugProxy();
+               fDebugPort=proxy.getProxyPort();
+               fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CHANGE));
+               
+               fThread = new XDebugThread(this);
+               fThreads = new IThread[] {fThread};
+               try {
+                       started();
+               } catch( DebugException e ){
+                       e.printStackTrace();            
+               }               
        }
 
+       private void setDebugConnection(XDebugConnection connection) {
+               if (connection != null) {
+                       fDebugConnection = connection;
+                       fDebugConnection.startListener();
+               }
+       }
+       
        /**
-        * Sends a request to the Debugengine and waits for an OK.
-        * 
-        * @param command debug command
-        * @throws DebugException if the request fails
+        * @return Returns the fDebugConnection.
         */
+       public XDebugConnection getDebugConnection() {
+               return fDebugConnection;
+       }       
        
-       public int sendRequest(String command) throws DebugException {  
-               return fDebugConnection.sendRequest(command,"");
+       public void addProcess(IProcess p) {
+               fProcess = p;
+
+       }
+
+       public void launchRemoved(ILaunch launch) {
        }
 
-       
        /**
-        * Sends a request to the Debugengine and waits for an OK.
+        * Notifies this listener that the specified launch
+        * has been added.
         * 
-        * @param command debug command
-        * @arguments arguments for the command
-        * @throws DebugException if the request fails
+        * @param launch the newly added launch
+        * @since 2.0
         */
-       
-       public int sendRequest(String command, String arguments) throws DebugException {
-               return fDebugConnection.sendRequest(command,arguments);
+       public void launchAdded(ILaunch launch){
        }
-       
+
        /**
-        * Notification a breakpoint was encountered. Determine
-        * which breakpoint was hit and fire a suspend event.
+        * Notifies this listener that the specified launch
+        * has changed. For example, a process or debug target
+        * has been added to the launch.
         * 
-        * @param event debug event
+        * @param launch the changed launch
+        * @since 2.0
         */
-       public void breakpointHit(Node node) {
-               // determine which breakpoint was hit, and set the thread's breakpoint
+       public void launchChanged(ILaunch launch) {
+       }
+       
+       public IVariable[] getVariables(XDebugStackFrame frame, int level)  throws DebugException {
+               IVariable[] variables = null;
+               
+               DebugResponse response = fDebugConnection.contextGet(level, 0);
+               Node responseNode = response.getParentNode();
+               NodeList property = responseNode.getChildNodes();
+               
+               DebugResponse responseGlobal = fDebugConnection.contextGet(level, 1);
+               Node responseGlobalNode = responseGlobal.getParentNode();
+               NodeList propertyGlobal = responseGlobalNode.getChildNodes();
+               
+               variables = new IVariable[property.getLength() + propertyGlobal.getLength()];
+//             variables = new IVariable[property.getLength()]; // + propertyGlobal.getLength()];
+               
+               int length = property.getLength();
+               for (int i = 0; i < length; i++) {
+                       Node propertyNode = property.item(i);
+                       XDebugVariable var=/*fDebugConnection.*/getVariableFromNode(frame,propertyNode);
+                       variables[i]=var;
+               }
+
+               int globalLength = propertyGlobal.getLength();
+               for (int k = 0; k < globalLength; k++) {
+                       Node propertyGlobalNode = propertyGlobal.item(k);
+                       XDebugVariable var=/*fDebugConnection.*/getVariableFromNode(frame,propertyGlobalNode);
+                       variables[k + length]=var;
+               }
+       
+               return variables;
+       }       
+
+       public XDebugVariable getVariableFromNode(XDebugStackFrame frame, Node property) {
+               String varFullName = PHPDebugUtils.getAttributeValue(property, "fullname");
+               String varName = PHPDebugUtils.getAttributeValue(property, "name");
+               String varEncoding = PHPDebugUtils.getAttributeValue(property, "encoding");
+               
+               int varNumChildren = 0;
+               if (PHPDebugUtils.getAttributeValue(property, "numchildren").equals("")) {
+                       varNumChildren = 0;
+               } else {
+                       varNumChildren = Integer.parseInt(PHPDebugUtils.getAttributeValue(property, "numchildren"));
+               }
+
+               String typeName = PHPDebugUtils.getAttributeValue(property,"type");
+
+               XDebugVariable variable = new XDebugVariable(frame,varFullName,varName,typeName);
+               variable.setEncoding(varEncoding);
+               variable.setNumChildren(varNumChildren);
+               XDebugAbstractValue val=null;
+               try {
+                       val = (XDebugAbstractValue) variable.getValue();
+               } catch (DebugException e1) {
+                       // TODO Auto-generated catch block
+                       e1.printStackTrace();
+               }
+               if (val.getType()!= XDebugAbstractValue.VALUETYPE_UNINITIALIZED) {
+                       if (variable.hasChildren()) {
+                               NodeList varNodes = property.getChildNodes();
+                               val.renderValueString(""+varNodes.getLength());
+                               IVariable[] variables = new IVariable[varNodes.getLength()];
+                               for (int i = 0; i<varNodes.getLength(); i++) {
+                                       Node propertyNode = varNodes.item(i);
+                                       variables[i] = getVariableFromNode(frame, propertyNode);
+                               }
+                               val.setChildVariables(variables);
+                       }else {
+                               String str="";
+                               try {
+                                       str=property.getFirstChild().getNodeValue();
+                               } catch (NullPointerException e) {
+                                       str="";
+                               }
+                               if (variable.getEncoding().equals("base64")) {
+                                       if (str.length()!=0)
+                                               str=new String(Base64.decode(str));
+                                       else
+                                               str="";
+                               }
+                               val.renderValueString(str);
+                       }
+                       
+                       String className=PHPDebugUtils.getAttributeValue(property,"classname");
+                       if(!"".equals(className))
+                               val.renderValueString(className);
+               }
+               return variable;
+               
+       }
+
+       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").substring(8);  // remove file:///
+                       String filename=PHPDebugUtils.getAttributeValue(child, "filename");  
                        IBreakpoint[] breakpoints = XDebugCorePlugin.getBreakpoints();
                        for (int i = 0; i < breakpoints.length; i++) {
                                IBreakpoint breakpoint = breakpoints[i];
@@ -496,14 +740,18 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                                                        if (breakpoint.isEnabled()) {
                                                                IMarker marker = breakpoint.getMarker();
                                                                if (marker != null) {
+                                                                       String endfilename;
                                                                        
-                                                                       String name = marker.getResource().getLocation().toOSString();
-                                                                       if(name.equals(PHPDebugUtils.unescapeString(filename)) && (lineBreakpoint.getLineNumber() == lineNumber)) {
-                                                                               fThread.setBreakpoints(new IBreakpoint[]{breakpoint});
-                                                                               break;
+                                                                       if (getProcess() == null) {
+                                                                               endfilename = marker.getResource().getLocation().lastSegment(); 
+                                                                       } else {
+                                                                               endfilename = marker.getResource().getLocation().toOSString();
+                                                                       }
+
+                                                                       if(PHPDebugUtils.unescapeString(filename).endsWith(endfilename) && (lineBreakpoint.getLineNumber() == lineNumber) ) {
+                                                                               return (breakpoint);
                                                                        }
                                                                }
-                                                       
                                                        }
                                                } catch (CoreException e) {
                                                }
@@ -511,18 +759,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugE
                                }
                        }
                }
-               suspended(DebugEvent.BREAKPOINT);
-       }
-       public void handleDebugEvents(DebugEvent[] events) {
-               for (int i=0;i<events.length;i++) {
-                       DebugEvent event=events[i];
-                       if((event.getKind()==DebugEvent.CREATE) && (event.getSource() instanceof XDebugElement)) {
-                               if(((XDebugElement)event.getSource()).getModelIdentifier()==IXDebugConstants.ID_PHP_DEBUG_MODEL) {
-                                       if (event.getKind()==DebugEvent.CREATE)
-                                               started();
-                               }
-                       }
-               }
                
+               return null;
        }       
 }
\ No newline at end of file