/**********************************************************************
-Copyright (c) 2000, 2002 IBM Corp. and others.
-All rights reserved. This program and the accompanying materials
-are made available under the terms of the Common Public License v1.0
-which accompanies this distribution, and is available at
-http://www.eclipse.org/legal/cpl-v10.html
-
-Contributors:
- IBM Corporation - Initial implementation
- Vicente Fernando - www.alfersoft.com.ar
-**********************************************************************/
+ Copyright (c) 2000, 2002 IBM Corp. and others.
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Common Public License v1.0
+ which accompanies this distribution, and is available at
+ http://www.eclipse.org/legal/cpl-v10.html
+
+ Contributors:
+ IBM Corporation - Initial implementation
+ Vicente Fernando - www.alfersoft.com.ar
+ **********************************************************************/
package net.sourceforge.phpdt.internal.debug.core.model;
import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IProcess;
-import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;
/**
* Debug target for PHP debug model.
*/
-public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugEventSetListener {
-
+public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener,
+ IDebugEventSetListener {
+
private IProcess process;
- private boolean isTerminated;
- private boolean isSuspended;
+
private ILaunch launch;
- private PHPThread[] threads;
+
+ private PHPThread[] threads = new PHPThread[0];
+
private PHPDBGProxy phpDBGProxy;
+ private class State {
+ private boolean isTerminated = false;
+
+ private boolean isSuspended = false;
+
+ boolean isTerminated() {
+ return isTerminated;
+ }
+
+ boolean isSuspended() {
+ return isSuspended;
+ }
+
+ void setTerminated(boolean terminated) {
+ this.isTerminated = terminated;
+ }
+
+ void setSuspended(boolean suspended) {
+ if (isTerminated())
+ throw new IllegalStateException();
+ this.isSuspended = suspended;
+ }
+ }
+
+ private final State state = new State();
+
public PHPDebugTarget(ILaunch launch, IProcess process) {
- this.isSuspended = false;
+ if (null == launch && null == process)
+ throw new IllegalArgumentException();
this.launch = launch;
this.process = process;
- this.threads = new PHPThread[0];
// TODO XXX remove breakpoint listener at termination to avoid live leak
- IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
+ IBreakpointManager manager = DebugPlugin.getDefault()
+ .getBreakpointManager();
manager.addBreakpointListener(this);
DebugPlugin.getDefault().addDebugEventListener(this);
initialize();
protected synchronized void initialize() {
DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
- DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
+ DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
}
public void addThread(PHPThread phpThread) {
int i;
PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
-
- for(i=0; i < threads.length; i++) {
+
+ for (i = 0; i < threads.length; i++) {
updatedThreads[i] = threads[i];
}
updatedThreads[i] = phpThread;
}
public boolean supportsBreakpoint(IBreakpoint arg0) {
- if(arg0.getModelIdentifier().equals(PHPDebugCorePlugin.PLUGIN_ID)) {
- return true;
- }
+ if (arg0.getModelIdentifier().equals(PHPDebugCorePlugin.PLUGIN_ID)) {
+ return true;
+ }
return false;
}
return launch;
}
- public boolean canTerminate() {
- return !isTerminated;
+ public synchronized boolean canTerminate() {
+ return !isTerminated();
}
- public boolean isTerminated() {
- return isTerminated;
+ public synchronized boolean isTerminated() {
+ return state.isTerminated();
}
public synchronized void terminate() {
- // This method is synchronized to control a race condition between the
- // UI thread that terminates the debugging session, and the slave
+ // This method is synchronized to control a race condition between the
+ // UI thread that terminates the debugging session, and the slave
// thread that executes PHPLoop.run
- if (isTerminated)
+ if (isTerminated())
// Avoid terminating twice...
return;
+ state.setTerminated(true);
phpDBGProxy.stop();
this.threads = new PHPThread[0];
- isTerminated = true;
fireChangeEvent();
- IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
+ IBreakpointManager manager = DebugPlugin.getDefault()
+ .getBreakpointManager();
manager.removeBreakpointListener(this);
DebugPlugin.getDefault().removeDebugEventListener(this);
}
- public boolean canResume() {
- if(isTerminated) return false;
- return isSuspended;
+ public synchronized boolean canResume() {
+ if (isTerminated())
+ return false;
+ return isSuspended();
}
- public boolean canSuspend() {
- if(isTerminated) return false;
- return !isSuspended;
+ public synchronized boolean canSuspend() {
+ if (isTerminated())
+ return false;
+ return !isSuspended();
}
- public boolean isSuspended() {
- return isSuspended;
+ public synchronized boolean isSuspended() {
+ return state.isSuspended();
}
- public void resume() throws DebugException {
+ public synchronized void resume() throws DebugException {
+ if (!isSuspended())
+ return;
+ state.setSuspended(false);
this.getPHPDBGProxy().resume();
- isSuspended= false;
+ IThread[] threads = getThreads();
+ for (int i = 0; i < threads.length; ++i)
+ threads[i].resume();
}
- public void suspend() throws DebugException {
+ public synchronized void suspend() throws DebugException {
+ if (isSuspended())
+ return;
this.getPHPDBGProxy().pause();
- isSuspended= true;
+ state.setSuspended(true);
+ IThread[] threads = getThreads();
+ for (int i = 0; i < threads.length; ++i)
+ threads[i].suspend();
}
public void breakpointAdded(IBreakpoint breakpoint) {
- this.getPHPDBGProxy().addBreakpoint(breakpoint) ;
+ this.getPHPDBGProxy().addBreakpoint(breakpoint);
}
public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
- this.getPHPDBGProxy().removeBreakpoint(breakpoint) ;
+ this.getPHPDBGProxy().removeBreakpoint(breakpoint);
}
public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta arg1) {
// is called e.g. after a line has been inserted before a breakpoint
- // but then the debugger is out of sync with the file anyway, so debugging
+ // but then the debugger is out of sync with the file anyway, so
+ // debugging
// should be stopped here.
}
return false;
}
- public IMemoryBlock getMemoryBlock(long arg0, long arg1) throws DebugException {
+ public IMemoryBlock getMemoryBlock(long arg0, long arg1)
+ throws DebugException {
return null;
}
IThread[] threads = getThreads();
if (null != threads) {
children = new Object[threads.length];
- for (int i = 0; i < threads.length; ++i)
+ 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 {
}
return label;
}
+
public Object getParent(Object o) {
return PHPDebugTarget.this.getLaunch();
}
public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
this.phpDBGProxy = phpDBGProxy;
}
-
+
/**
* @see ILaunchListener#launchRemoved(ILaunch)
*/
return;
}
if (launch.equals(getLaunch())) {
- // This target has been deregistered, but it hasn't successfully terminated.
+ // This target has been deregistered, but it hasn't successfully
+ // terminated.
// Update internal state to reflect that it is disconnected
terminate();
}
}
-
+
/**
* @see ILaunchListener#launchAdded(ILaunch)
*/
public void launchAdded(ILaunch launch) {
}
-
+
/**
* @see ILaunchListener#launchChanged(ILaunch)
*/
public void launchChanged(ILaunch launch) {
}
-
+
/**
* When a debug target or process terminates, terminate DBG Proxy.
*
DebugEvent event = events[i];
if (event.getKind() == DebugEvent.TERMINATE) {
Object source = event.getSource();
- if (source instanceof PHPDebugTarget || source instanceof IDebugTarget) {
+ if (source instanceof PHPDebugTarget
+ || source instanceof IDebugTarget) {
getPHPDBGProxy().stop();
- } else if(source instanceof IProcess) {
- if(getDebugTarget().getProcess() == (IProcess)source) {
+ } else if (source instanceof IProcess) {
+ if (getDebugTarget().getProcess() == (IProcess) source) {
getPHPDBGProxy().stop();
}
}
}
}
}
-
/**********************************************************************
-Copyright (c) 2000, 2002 IBM Corp. and others.
-All rights reserved. This program and the accompanying materials
-are made available under the terms of the Common Public License v1.0
-which accompanies this distribution, and is available at
-http://www.eclipse.org/legal/cpl-v10.html
-
-Contributors:
- IBM Corporation - Initial implementation
- Vicente Fernando - www.alfersoft.com.ar
-**********************************************************************/
+ Copyright (c) 2000, 2002 IBM Corp. and others.
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Common Public License v1.0
+ which accompanies this distribution, and is available at
+ http://www.eclipse.org/legal/cpl-v10.html
+
+ Contributors:
+ IBM Corporation - Initial implementation
+ Vicente Fernando - www.alfersoft.com.ar
+ **********************************************************************/
package net.sourceforge.phpdt.internal.debug.core.model;
import net.sourceforge.phpeclipse.PHPeclipsePlugin;
public class PHPThread implements IThread {
private PHPStackFrame[] frames;
+
private IDebugTarget target;
- private boolean isSuspended = false;
- private boolean isTerminated = false;
- private boolean isStepping = false;
- private String name ;
- private int id ;
-
+
+ private String name;
+
+ private int id;
+
+ private class State {
+ private boolean isSuspended = false;
+
+ private boolean isTerminated = false;
+
+ private boolean isStepping = false;
+
+ boolean isSuspended() {
+ return isSuspended;
+ }
+
+ boolean isTerminated() {
+ return isTerminated;
+ }
+
+ boolean isStepping() {
+ return isStepping;
+ }
+
+ void setSuspended(boolean suspended) {
+ if (isTerminated())
+ throw new IllegalStateException();
+ if (suspended && isStepping())
+ throw new IllegalStateException();
+ isSuspended = suspended;
+ }
+
+ void setStepping(boolean stepping) {
+ if (stepping && !isSuspended())
+ throw new IllegalStateException();
+ if (isTerminated())
+ throw new IllegalStateException();
+ isStepping = stepping;
+ }
+
+ void setTerminated(boolean terminated) {
+ isTerminated = terminated;
+ }
+ }
+
+ private final State state = new State();
+
public PHPThread(IDebugTarget target, int id) {
this.target = target;
- this.setId(id) ;
- this.createName() ;
+ this.setId(id);
}
public IStackFrame[] getStackFrames() throws DebugException {
return (IStackFrame) frames[0];
}
-
public IBreakpoint[] getBreakpoints() {
return null;
}
}
public void setDebugTarget(IDebugTarget target) {
- this.target= target;
+ this.target = target;
}
-
+
public ILaunch getLaunch() {
return this.getDebugTarget().getLaunch();
}
- public boolean canResume() {
- return isSuspended;
- }
-
- public boolean canSuspend() {
- return !isSuspended;
+ public synchronized boolean canResume() {
+ return isSuspended();
}
- public boolean isSuspended() {
- return isSuspended;
+ public synchronized boolean canSuspend() {
+ return !isSuspended();
}
- protected void setSuspended(boolean isSuspended) {
- this.isSuspended = isSuspended;
+ public synchronized boolean isSuspended() {
+ return state.isSuspended;
}
protected void prepareForResume() {
- isSuspended = false;
- this.createName() ;
- this.frames = null ;
- DebugEvent ev = new DebugEvent(this, DebugEvent.RESUME, DebugEvent.CLIENT_REQUEST);
- DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
+ state.setSuspended(false);
+ this.frames = null;
+ DebugEvent ev = new DebugEvent(this, DebugEvent.RESUME,
+ DebugEvent.CLIENT_REQUEST);
+ DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
}
- public void resume() throws DebugException {
- this.prepareForResume() ;
+ public synchronized void resume() throws DebugException {
+ if (!isSuspended())
+ return;
+ this.prepareForResume();
((PHPDebugTarget) this.getDebugTarget()).getPHPDBGProxy().resume();
}
/*
- public void doSuspend(SuspensionPoint suspensionPoint) {
-// this.getPHPDebuggerProxy().readFrames(this);
- this.createName(suspensionPoint) ;
- this.suspend() ;
- }
- */
-
- public void suspend() {
- isStepping = false;
- isSuspended = true;
- DebugEvent ev = new DebugEvent(this, DebugEvent.SUSPEND, DebugEvent.BREAKPOINT);
+ * public void doSuspend(SuspensionPoint suspensionPoint) { //
+ * this.getPHPDebuggerProxy().readFrames(this);
+ * this.createName(suspensionPoint) ; this.suspend() ; }
+ */
+
+ public synchronized void suspend() throws DebugException {
+ if (isSuspended())
+ return;
+ state.setSuspended(true);
+ state.setStepping(false);
+ getDebugTarget().suspend();
+ DebugEvent ev = new DebugEvent(this, DebugEvent.SUSPEND,
+ DebugEvent.BREAKPOINT);
DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
}
public boolean canStepInto() {
- return isSuspended && this.hasStackFrames();
+ return isSuspended() && isStepping() && this.hasStackFrames();
}
public boolean canStepOver() {
- return isSuspended && this.hasStackFrames();
+ return isSuspended() && isStepping() && this.hasStackFrames();
}
public boolean canStepReturn() {
- return false;
+ return isSuspended() && isStepping() && this.hasStackFrames();
}
public boolean isStepping() {
- return isStepping;
+ return state.isStepping();
}
public void stepInto() throws DebugException {
- isStepping = true ;
- this.createName() ;
- this.frames = null ;
+ try { state.setStepping(true); }
+ catch (IllegalStateException x) {
+ throw new DebugException(PHPeclipsePlugin.error(x));
+ }
+ this.frames = null;
frames[0].stepInto();
}
public void stepOver() throws DebugException {
- isStepping = true ;
- this.createName() ;
- this.frames = null ;
- frames[0].stepOver() ;
+ state.setStepping(true);
+ this.frames = null;
+ frames[0].stepOver();
}
public void stepReturn() throws DebugException {
}
public boolean canTerminate() {
- return !isTerminated;
+ return !isTerminated();
}
public boolean isTerminated() {
- return isTerminated;
+ return state.isTerminated();
}
- public void terminate() throws DebugException {
- isTerminated = true;
+ public synchronized void terminate() throws DebugException {
+ if (isTerminated())
+ return;
+ state.setTerminated(true);
this.frames = null;
getDebugTarget().terminate();
}
IStackFrame[] frames = getStackFrames();
if (null != frames) {
children = new Object[frames.length];
- for (int i = 0; i < frames.length; ++i)
+ for (int i = 0; i < frames.length; ++i)
children[i] = frames[i];
}
} catch (DebugException x) {
}
return children;
}
+
public ImageDescriptor getImageDescriptor(Object object) {
return null;
}
+
public String getLabel(Object o) {
throw new UnsupportedOperationException();
}
+
public Object getParent(Object o) {
return getDebugTarget();
}
}
public String getName() {
+ String name = this.name;
+ if (isSuspended())
+ name = name + " (suspended)";
return name;
}
this.name = name;
}
- protected void createName() {
- //this.createName(null) ;
- }
-
- /*
- protected void createName(SuspensionPoint suspensionPoint) {
- this.name = "PHP Thread - " + this.getId() ;
- if (suspensionPoint != null) {
- this.name += " (" + suspensionPoint + ")" ;
- }
- }
- */
+ /*
+ * protected void createName(SuspensionPoint suspensionPoint) { this.name =
+ * "PHP Thread - " + this.getId() ; if (suspensionPoint != null) { this.name += " (" +
+ * suspensionPoint + ")" ; } }
+ */
public int getId() {
return id;