1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
9 IBM Corporation - Initial implementation
10 Vicente Fernando - www.alfersoft.com.ar
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.model;
14 import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
15 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
16 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import org.eclipse.core.resources.IMarkerDelta;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.debug.core.DebugEvent;
22 import org.eclipse.debug.core.DebugException;
23 import org.eclipse.debug.core.DebugPlugin;
24 import org.eclipse.debug.core.IBreakpointManager;
25 import org.eclipse.debug.core.IDebugEventSetListener;
26 import org.eclipse.debug.core.ILaunch;
27 import org.eclipse.debug.core.ILaunchListener;
28 import org.eclipse.debug.core.model.IBreakpoint;
29 import org.eclipse.debug.core.model.IDebugTarget;
30 import org.eclipse.debug.core.model.IMemoryBlock;
31 import org.eclipse.debug.core.model.IProcess;
32 import org.eclipse.debug.core.model.IStackFrame;
33 import org.eclipse.debug.core.model.IThread;
34 import org.eclipse.jface.resource.ImageDescriptor;
35 import org.eclipse.ui.model.IWorkbenchAdapter;
38 * Debug target for PHP debug model.
40 public class PHPDebugTarget extends PHPDebugElement implements IPHPDebugTarget, ILaunchListener,
41 IDebugEventSetListener {
43 private IProcess process;
45 private ILaunch launch;
47 private PHPThread[] threads = new PHPThread[0];
49 private PHPDBGProxy phpDBGProxy;
52 private boolean isTerminated = false;
54 private boolean isSuspended = false;
56 boolean isTerminated() {
60 boolean isSuspended() {
64 void setTerminated(boolean terminated) {
65 this.isTerminated = terminated;
68 void setSuspended(boolean suspended) {
70 throw new IllegalStateException();
71 this.isSuspended = suspended;
75 private final State state = new State();
77 public PHPDebugTarget(ILaunch launch, IProcess process) {
79 if (null == launch && null == process)
80 throw new IllegalArgumentException();
82 this.process = process;
83 // TODO XXX remove breakpoint listener at termination to avoid live leak
84 IBreakpointManager manager = DebugPlugin.getDefault()
85 .getBreakpointManager();
86 manager.addBreakpointListener(this);
87 DebugPlugin.getDefault().addDebugEventListener(this);
91 protected synchronized void initialize() {
92 DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
93 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
96 public void addThread(PHPThread phpThread) {
98 PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
100 for (i = 0; i < threads.length; i++) {
101 updatedThreads[i] = threads[i];
103 updatedThreads[i] = phpThread;
104 threads = updatedThreads;
107 fireThreadCreateEvent(phpThread);
110 private void fireChangeEvent() {
111 DebugEvent ev = new DebugEvent(this, DebugEvent.CHANGE);
112 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
115 private void fireThreadCreateEvent(PHPThread phpThread) {
116 DebugEvent ev = new DebugEvent(phpThread, DebugEvent.CREATE);
117 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
120 protected PHPThread getThreadById(int id) {
121 for (int i = 0; i < threads.length; i++) {
122 if (threads[i].getId() == id) {
129 public IThread[] getThreads() {
133 public boolean hasThreads() throws DebugException {
134 return threads.length > 0;
137 public String getName() throws DebugException {
138 return "PHP Debugger at localhost:" + getPHPDBGProxy().getPort();
141 public boolean supportsBreakpoint(IBreakpoint arg0) {
142 if (arg0.getModelIdentifier().equals(PHPDebugCorePlugin.PLUGIN_ID)) {
148 public String getModelIdentifier() {
149 return PHPDebugCorePlugin.PLUGIN_ID;
152 public IStackFrame[] getStackFrames () throws DebugException {
153 return (IStackFrame[]) this.phpDBGProxy.getDBGInterface ().getStackList ();
156 public IDebugTarget getDebugTarget() {
160 public ILaunch getLaunch() {
164 public synchronized boolean canTerminate() {
165 return !isTerminated();
168 public synchronized boolean isTerminated() {
169 return state.isTerminated();
172 public synchronized void terminate() {
173 // This method is synchronized to control a race condition between the
174 // UI thread that terminates the debugging session, and the slave
175 // thread that executes PHPLoop.run
177 // Avoid terminating twice...
179 state.setTerminated(true);
181 this.threads = new PHPThread[0];
183 IBreakpointManager manager = DebugPlugin.getDefault()
184 .getBreakpointManager();
185 manager.removeBreakpointListener(this);
186 DebugPlugin.getDefault().removeDebugEventListener(this);
189 public synchronized boolean canResume() {
192 return isSuspended();
195 public synchronized boolean canSuspend() {
198 return !isSuspended();
201 public synchronized boolean isSuspended() {
202 return state.isSuspended();
205 public synchronized void resume() throws DebugException {
208 state.setSuspended(false);
209 this.getPHPDBGProxy().resume();
210 IThread[] threads = getThreads();
211 for (int i = 0; i < threads.length; ++i)
215 public synchronized void suspend() throws DebugException {
218 this.getPHPDBGProxy().pause();
219 state.setSuspended(true);
220 IThread[] threads = getThreads();
221 for (int i = 0; i < threads.length; ++i)
222 threads[i].suspend();
225 public void breakpointAdded(IBreakpoint breakpoint) {
226 this.getPHPDBGProxy().addBreakpoint(breakpoint);
229 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
230 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
234 * The method will be called when the user enables/disables
235 * breakpoints. In this case we add or remove the breakpoint.
236 * It's also called when leaving the breakpoint properties dialog
237 * (skip count and breakpoint condition) with the OK button.
239 * This method is also called whenever a source file has changed.
240 * In this case we terminate since the source will be out of sync with the debugger.
241 * TODO Is it correct to call this method when a sourcefile is modified?
244 public void breakpointChanged (IBreakpoint breakpoint, IMarkerDelta arg1) {
245 PHPLineBreakpoint bp;
246 bp = (PHPLineBreakpoint) breakpoint;
249 if (breakpoint.isEnabled () && // Check if breakpoint state changed from disabled to enabled
250 !arg1.getAttribute ("org.eclipse.debug.core.enabled", false)) {
251 this.getPHPDBGProxy().addBreakpoint(breakpoint);
253 else if (!breakpoint.isEnabled () && // Check if breakpoint state changed from enabled to disabled
254 arg1.getAttribute ("org.eclipse.debug.core.enabled", true)) {
255 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
257 else if (bp.getChangeID() != arg1.getAttribute ("net.sourceforge.phpeclipse.debug.changeID", 0)) {
258 if (breakpoint.isEnabled()) { // If the breakpoint is already enabled
259 this.getPHPDBGProxy().removeBreakpoint(breakpoint); // we remove this breakpoint first
260 this.getPHPDBGProxy().addBreakpoint(breakpoint); // and then we add again (else DBG would have two breakpoints!).
263 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
266 else { // All other cases will terminate the debugger
269 } catch (CoreException e) {
274 public boolean canDisconnect() {
278 public void disconnect() throws DebugException {
281 public boolean isDisconnected() {
285 public boolean supportsStorageRetrieval() {
289 public IMemoryBlock getMemoryBlock(long arg0, long arg1)
290 throws DebugException {
294 public Object getAdapter(Class arg0) {
295 if (IWorkbenchAdapter.class.equals(arg0)) {
296 return new IWorkbenchAdapter() {
297 public Object[] getChildren(Object o) {
298 Object[] children = null;
299 IThread[] threads = getThreads();
300 if (null != threads) {
301 children = new Object[threads.length];
302 for (int i = 0; i < threads.length; ++i)
303 children[i] = threads[i];
308 public ImageDescriptor getImageDescriptor(Object object) {
312 public String getLabel(Object o) {
313 String label = "(Unable to look up name... check error log)";
316 } catch (DebugException x) {
317 PHPeclipsePlugin.log(label, x);
322 public Object getParent(Object o) {
323 return PHPDebugTarget.this.getLaunch();
328 if (arg0 == PHPDebugElement.class) {
332 return super.getAdapter(arg0);
336 public IProcess getProcess() {
340 public void setProcess(IProcess process) {
341 this.process = process;
344 public PHPDBGProxy getPHPDBGProxy() {
348 public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
349 this.phpDBGProxy = phpDBGProxy;
353 * @see ILaunchListener#launchRemoved(ILaunch)
355 public void launchRemoved(ILaunch launch) {
356 if (!isTerminated()) {
359 if (launch.equals(getLaunch())) {
360 // This target has been deregistered, but it hasn't successfully
362 // Update internal state to reflect that it is disconnected
368 * @see ILaunchListener#launchAdded(ILaunch)
370 public void launchAdded(ILaunch launch) {
374 * @see ILaunchListener#launchChanged(ILaunch)
376 public void launchChanged(ILaunch launch) {
380 * When a debug target or process terminates, terminate DBG Proxy.
382 * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
384 public void handleDebugEvents(DebugEvent[] events) {
385 for (int i = 0; i < events.length; i++) {
386 DebugEvent event = events[i];
387 if (event.getKind() == DebugEvent.TERMINATE) {
388 Object source = event.getSource();
389 if (source instanceof PHPDebugTarget
390 || source instanceof IDebugTarget) {
391 getPHPDBGProxy().stop();
392 } else if (source instanceof IProcess) {
393 if (getDebugTarget().getProcess() == (IProcess) source) {
394 getPHPDBGProxy().stop();
397 } else if (event.getKind() == DebugEvent.SUSPEND) {
398 getPHPDBGProxy().pause();