X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge/phpeclipse/xdebug/php/model/XDebugTarget.java b/net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge/phpeclipse/xdebug/php/model/XDebugTarget.java index 9f31828..396b317 100644 --- a/net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge/phpeclipse/xdebug/php/model/XDebugTarget.java +++ b/net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge/phpeclipse/xdebug/php/model/XDebugTarget.java @@ -1,14 +1,20 @@ /** - * + * */ package net.sourceforge.phpeclipse.xdebug.php.model; +//import java.io.IOException; import java.util.List; -import net.sourceforge.phpeclipse.xdebug.core.AbstractDebugConnection; -import net.sourceforge.phpeclipse.xdebug.core.IDebugConnection; +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; +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.XDebugProxy; @@ -18,112 +24,125 @@ 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.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; 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; 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.IVariable; +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; +//import org.xml.sax.SAXException; -import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.DebugResponse; +import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener; +import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugConnection; +import net.sourceforge.phpeclipse.xdebug.core.xdebug.XDebugResponse; /** * @author Christian * */ -public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunchListener, IDebugEventSetListener, IProxyEventListener{ - // associated system process (VM) +public class XDebugTarget extends XDebugElement implements IDebugTarget, IDebugEventSetListener, IProxyEventListener { private IProcess fProcess; - - // containing launch object + private ILaunch fLaunch; - - // debugPort + private int fDebugPort; - - // suspend state + private boolean fSuspended = false; - - // terminated state + private boolean fTerminated = false; - - // threads + private XDebugThread fThread; private IThread[] fThreads; - - private AbstractDebugConnection fDebugConnection; - private String fIdeKey; + private XDebugConnection fDebugConnection; + private ResponseListener fResponseListener; - public XDebugTarget() { - super(null); - } + private String fIdeKey; - /** - * 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 - * @exception CoreException if unable to connect to host - */ - - public XDebugTarget(ILaunch launch, IProcess process, String ideKey) throws CoreException { - init(launch, process, ideKey); - } /** * 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 pathMap Pathmap for the debug session + * @param ideKey * @exception CoreException if unable to connect to host */ - - /*public XDebugTarget(ILaunch launch, IProcess process, String ideKey, List pathMap) throws CoreException { - super(null); - init(launch, process, ideKey,pathMap); - }*/ - - private void init(ILaunch launch, IProcess process, String ideKey/*,List pathMap*/) { + public XDebugTarget(ILaunch launch, IProcess process, String ideKey) throws CoreException { fLaunch = launch; fProcess = process; - fTarget = this; 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(); - + + 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() */ @@ -134,7 +153,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch /* (non-Javadoc) * @see org.eclipse.debug.core.model.IDebugTarget#getThreads() */ - public IThread[] getThreads() throws DebugException { + public IThread[] getThreads() { return fThreads; } @@ -142,7 +161,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch * @see org.eclipse.debug.core.model.IDebugTarget#hasThreads() */ public boolean hasThreads() throws DebugException { - return (fThreads.length>0); + return (fThreads.length > 0); } /* (non-Javadoc) @@ -156,7 +175,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch * @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; @@ -197,27 +216,29 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch * @see org.eclipse.debug.core.model.ITerminate#terminate() */ public void terminate() throws DebugException { - //IThread t = fThreads.length(); - //fTerminated=true; - 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(); + 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); + } } /* (non-Javadoc) * @see org.eclipse.debug.core.model.ISuspendResume#canResume() */ public boolean canResume() { -// return !isTerminated() && isSuspended(); return false; } @@ -225,7 +246,6 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend() */ public boolean canSuspend() { -// return !isTerminated() && !isSuspended(); return false; } @@ -242,30 +262,31 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch public void resume() throws DebugException { if (fDebugConnection != null) { 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() */ @@ -276,10 +297,11 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch * @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) { @@ -287,49 +309,65 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch 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./*removeFirstSegments(1).*/makeAbsolute(); - int matchedSegments = local.segmentCount(); - if (local.matchingFirstSegments(cp) == matchedSegments) { - IPath newPath = pmi.getRemotePath(); - //newPath = newPath.removeFirstSegments(1); - newPath = newPath.append(path.removeFirstSegments(matchedSegments)); - newPath = newPath.makeAbsolute(); - if (supportsBreakpoint(breakpoint)) { - try { - fDebugConnection.addBreakpoint(breakpoint, newPath); - } catch (DebugException e) { - e.printStackTrace(); + 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), + marker.getAttribute (XDebugBreakpoint.CONDITION_ENABLED, false), + marker.getAttribute (XDebugBreakpoint.CONDITION, "")); + 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(); } } - } - } else { - if (supportsBreakpoint(breakpoint)) { - try { - fDebugConnection.addBreakpoint(breakpoint, path); - } catch (DebugException e) { - e.printStackTrace(); - } + } + else { + XDebugCorePlugin.log (IStatus.WARNING, "path do not match: local path: " + local.toString () + " breakpoint file: " + path.toString ()); } } } } - + /* (non-Javadoc) * @see org.eclipse.debug.core.IBreakpointListener#breakpointRemoved(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta) */ public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) { + IMarker marker = breakpoint.getMarker(); // Get the breakpoints marker info (It's the local workspace path) + if (supportsBreakpoint(breakpoint)) { - try { - fDebugConnection.removeBreakpoint(breakpoint); - } catch (CoreException e) { - } + int id = marker.getAttribute (XDebugLineBreakpoint.BREAKPOINT_ID, -1); + + if (id > 0) { + fDebugConnection.breakpointRemove(id); + } } } @@ -337,16 +375,32 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch * @see org.eclipse.debug.core.IBreakpointListener#breakpointChanged(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta) */ public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) { -// if (supportsBreakpoint(breakpoint)) { -// try { -// if (breakpoint.isEnabled()) { -// breakpointAdded(breakpoint); -// } else { -// breakpointRemoved(breakpoint, null); -// } -// } catch (CoreException e) { -// } -// } + IMarker oldmarker = breakpoint.getMarker (); + + if (supportsBreakpoint(breakpoint)) { + try { + if (breakpoint.isEnabled () && // Check if breakpoint state changed from disabled to enabled + !delta.getAttribute ("org.eclipse.debug.core.enabled", false)) { + breakpointAdded (breakpoint); + } + else if (!breakpoint.isEnabled () && // Check if breakpoint state changed from enabled to disabled + delta.getAttribute ("org.eclipse.debug.core.enabled", true)) { + breakpointRemoved (breakpoint, null); + } + else if (oldmarker.getAttribute (XDebugLineBreakpoint.CHANGE_ID, 1) != + delta.getAttribute (XDebugLineBreakpoint.CHANGE_ID, 0)) { + if (breakpoint.isEnabled ()) { // If the breakpoint is already enabled + breakpointRemoved (breakpoint, null); // we remove this breakpoint first + breakpointAdded (breakpoint); // and then we add again (else XDebug would have two breakpoints!). + } + else { + breakpointRemoved (breakpoint, null); + } + } + } catch (CoreException e) { + // Do nothing + } + } } /* (non-Javadoc) @@ -366,7 +420,7 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch * @see org.eclipse.debug.core.model.IDisconnect#isDisconnected() */ public boolean isDisconnected() { - return (fDebugConnection==null); + return (false); } /* (non-Javadoc) @@ -391,31 +445,34 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch fThread.setBreakpoints(null); fThread.setStepping(false); - /*boolean CanDisconnect =*/ Integer.parseInt(fDebugConnection.featureGet("detach").getValue()) /*!= 0*/; + int id = fDebugConnection.featureGet("detach"); + + XDebugResponse response = getResponse(id); + Integer.parseInt(response.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)"); + + // Dirty hack + // Need to refactory plugin to get variables in lazy mode. + int id1 = fDebugConnection.featureSet("max_depth", "1024" ); + XDebugResponse response1 = getResponse(id1); + if (response1.getAttributeValue("success").equals("1") ) { + System.out.println("Set depth to 1024 (hack)"); + } + int id2 = fDebugConnection.featureSet("max_children", "1024" ); + XDebugResponse response2 = getResponse(id2); + if (response2.getAttributeValue("success").equals("1") ) { + System.out.println("Set children to 1024 (hack)"); } - + installDeferredBreakpoints(); try { resume(); -// step(); } catch (DebugException e) { e.printStackTrace(); } } - + /** * Install breakpoints that are already registered with the breakpoint * manager. @@ -426,32 +483,22 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch breakpointAdded(breakpoints[i]); } } - - /** - * Called when this debug target terminates. - */ - public void terminated() { - fTerminated = true; - fSuspended = false; - XDebugCorePlugin.getBreakpointManager().removeBreakpointListener(this); - fireTerminateEvent(); - DebugPlugin.getDefault().removeDebugEventListener(this); - fThread.removeEventListeners(); - } - + /** * Returns the current stack frames in the target. - * + * * @return the current stack frames in the target * @throws DebugException if unable to perform the request */ - protected IStackFrame[] getStackFrames() throws DebugException { - return fDebugConnection.getStackFrames(fThread); + public XDebugResponse getStackFrames() throws DebugException { + int id = fDebugConnection.stackGet(); + XDebugResponse lastResponse = getResponse(id); + return lastResponse; } - + /** * Single step the interpreter. - * + * * @throws DebugException if the request fails */ protected void step_over() throws DebugException { @@ -459,10 +506,10 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch resumed(DebugEvent.STEP_OVER); fDebugConnection.stepOver(); } - + /** * Single step the interpreter. - * + * * @throws DebugException if the request fails */ protected void step_into() throws DebugException { @@ -470,10 +517,10 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch resumed(DebugEvent.STEP_INTO); fDebugConnection.stepInto(); } - + /** * Single step the interpreter. - * + * * @throws DebugException if the request fails */ protected void step_out() throws DebugException { @@ -481,136 +528,262 @@ public class XDebugTarget extends XDebugElement implements IDebugTarget, ILaunch resumed(DebugEvent.STEP_RETURN); fDebugConnection.stepOut(); } - - /** - * Returns the current value of the given variable. - * - * @param variable - * @return variable value - * @throws DebugException if the request fails - */ - 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 { - return new IValue[0]; - } - + public boolean setVarValue(String name, String value) { - return fDebugConnection.setVarValue(name,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) { + int id = fDebugConnection.eval(expression); + //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) { + e.printStackTrace(); + } + //try { + doc = builder.newDocument(); // .parse(""); + evalProperty = doc.createElement("value"); + /*} catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }*/ + } + + return evalProperty; + } + public void handleDebugEvents(DebugEvent[] events) { - for (int i=0;i 0) { + if (marker.getAttribute(XDebugLineBreakpoint.HIT_COUNT, 0) == hitCount) { + return (breakpoint); + } + } else { + return (breakpoint); + } + } + } + } + } catch (CoreException e) { + } + } + } + } + } + + return null; } - - public IVariable[] getVariables(XDebugStackFrame StackFrame, int Level) { - return fDebugConnection.getVariables(StackFrame, Level); + + public void startListener() { + fResponseListener.schedule(); + } + + public void stopListener() { + fResponseListener.cancel(); + } + public XDebugResponse getResponse(int id) { + XDebugResponse response = fResponseListener.getResponse(id); + + return response; } -} \ No newline at end of file +}