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;
44 private ILaunch launch;
45 private PHPThread[] threads = new PHPThread[0];
46 private PHPDBGProxy phpDBGProxy;
49 private boolean isTerminated = false;
50 private boolean isSuspended = false;
52 boolean isTerminated() {
56 boolean isSuspended() {
60 void setTerminated(boolean terminated) {
61 this.isTerminated = terminated;
64 void setSuspended(boolean suspended) {
66 throw new IllegalStateException();
67 this.isSuspended = suspended;
71 private final State state = new State();
73 public PHPDebugTarget(ILaunch launch, IProcess process) {
75 if (null == launch && null == process)
76 throw new IllegalArgumentException();
78 this.process = process;
79 // TODO XXX remove breakpoint listener at termination to avoid live leak
80 IBreakpointManager manager = DebugPlugin.getDefault()
81 .getBreakpointManager();
82 manager.addBreakpointListener(this);
83 DebugPlugin.getDefault().addDebugEventListener(this);
87 protected synchronized void initialize() {
88 DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
89 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
92 public void addThread(PHPThread phpThread) {
94 PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
96 for (i = 0; i < threads.length; i++) {
97 updatedThreads[i] = threads[i];
99 updatedThreads[i] = phpThread;
100 threads = updatedThreads;
103 fireThreadCreateEvent(phpThread);
106 public void updateThreads (PHPThread phpThread) {
108 fireThreadCreateEvent (phpThread);
111 private void fireChangeEvent() {
112 DebugEvent ev = new DebugEvent(this, DebugEvent.CHANGE);
113 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
116 private void fireThreadCreateEvent(PHPThread phpThread) {
117 DebugEvent ev = new DebugEvent(phpThread, DebugEvent.CREATE);
118 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
121 // protected PHPThread getThreadById(int id) {
122 // for (int i = 0; i < threads.length; i++) {
123 // if (threads[i].getId() == id) {
124 // return threads[i];
130 public IThread[] getThreads() {
134 public boolean hasThreads() throws DebugException {
135 return threads.length > 0;
138 public String getName() throws DebugException {
139 return "PHP Debugger at localhost:" + getPHPDBGProxy().getPort();
142 public boolean supportsBreakpoint(IBreakpoint arg0) {
143 if (arg0.getModelIdentifier().equals(PHPDebugCorePlugin.PLUGIN_ID)) {
149 public String getModelIdentifier() {
150 return PHPDebugCorePlugin.PLUGIN_ID;
153 public IStackFrame[] getStackFrames () throws DebugException {
154 return (IStackFrame[]) this.phpDBGProxy.getDBGInterface ().getStackList ();
157 public IDebugTarget getDebugTarget() {
161 public ILaunch getLaunch() {
165 public synchronized boolean canTerminate() {
166 return !isTerminated();
169 public synchronized boolean isTerminated() {
170 return state.isTerminated();
173 private synchronized void terminateThreads () {
177 for (i = 0; i < threads.length; i++) {
178 threads[i].terminate ();
180 } catch (DebugException e) {
184 public synchronized void terminate() {
185 // This method is synchronized to control a race condition between the
186 // UI thread that terminates the debugging session, and the slave
187 // thread that executes PHPLoop.run
189 // Avoid terminating twice...
191 state.setTerminated(true);
194 this.threads = new PHPThread[0];
196 IBreakpointManager manager = DebugPlugin.getDefault()
197 .getBreakpointManager();
198 manager.removeBreakpointListener(this);
199 DebugPlugin.getDefault().removeDebugEventListener(this);
202 public synchronized boolean canResume() {
205 return isSuspended();
208 public synchronized boolean canSuspend() {
211 return !isSuspended();
214 public synchronized boolean isSuspended() {
215 return state.isSuspended();
218 public synchronized void resume() throws DebugException {
221 state.setSuspended(false);
222 this.getPHPDBGProxy().resume();
223 IThread[] threads = getThreads();
224 for (int i = 0; i < threads.length; ++i)
228 public synchronized void suspend() throws DebugException {
231 this.getPHPDBGProxy().pause();
232 state.setSuspended(true);
233 IThread[] threads = getThreads();
234 for (int i = 0; i < threads.length; ++i)
235 threads[i].suspend();
238 public void breakpointAdded(IBreakpoint breakpoint) {
239 this.getPHPDBGProxy().addBreakpoint(breakpoint);
242 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
243 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
247 * The method will be called when the user enables/disables
248 * breakpoints. In this case we add or remove the breakpoint.
249 * It's also called when leaving the breakpoint properties dialog
250 * (skip count and breakpoint condition) with the OK button.
252 * This method is also called whenever a source file has changed.
253 * In this case we terminate since the source will be out of sync with the debugger.
254 * TODO Is it correct to call this method when a sourcefile is modified?
257 public void breakpointChanged (IBreakpoint breakpoint, IMarkerDelta arg1) {
258 PHPLineBreakpoint bp;
259 bp = (PHPLineBreakpoint) breakpoint;
262 if (breakpoint.isEnabled () && // Check if breakpoint state changed from disabled to enabled
263 !arg1.getAttribute ("org.eclipse.debug.core.enabled", false)) {
264 this.getPHPDBGProxy().addBreakpoint(breakpoint);
266 else if (!breakpoint.isEnabled () && // Check if breakpoint state changed from enabled to disabled
267 arg1.getAttribute ("org.eclipse.debug.core.enabled", true)) {
268 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
270 else if (bp.getChangeID() != arg1.getAttribute ("net.sourceforge.phpeclipse.debug.changeID", 0)) {
271 if (breakpoint.isEnabled()) { // If the breakpoint is already enabled
272 this.getPHPDBGProxy().removeBreakpoint(breakpoint); // we remove this breakpoint first
273 this.getPHPDBGProxy().addBreakpoint(breakpoint); // and then we add again (else DBG would have two breakpoints!).
276 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
279 else { // All other cases will terminate the debugger
282 } catch (CoreException e) {
287 public boolean canDisconnect() {
291 public void disconnect() throws DebugException {
294 public boolean isDisconnected() {
298 public boolean supportsStorageRetrieval() {
302 public IMemoryBlock getMemoryBlock(long arg0, long arg1)
303 throws DebugException {
307 public Object getAdapter(Class arg0) {
308 if (IWorkbenchAdapter.class.equals(arg0)) {
309 return new IWorkbenchAdapter() {
310 public Object[] getChildren(Object o) {
311 Object[] children = null;
312 IThread[] threads = getThreads();
313 if (null != threads) {
314 children = new Object[threads.length];
315 for (int i = 0; i < threads.length; ++i)
316 children[i] = threads[i];
321 public ImageDescriptor getImageDescriptor(Object object) {
325 public String getLabel(Object o) {
326 String label = "(Unable to look up name... check error log)";
329 } catch (DebugException x) {
330 PHPeclipsePlugin.log(label, x);
335 public Object getParent(Object o) {
336 return PHPDebugTarget.this.getLaunch();
341 if (arg0 == PHPDebugElement.class) {
345 return super.getAdapter(arg0);
349 public IProcess getProcess() {
353 public void setProcess(IProcess process) {
354 this.process = process;
357 public PHPDBGProxy getPHPDBGProxy() {
361 public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
362 this.phpDBGProxy = phpDBGProxy;
366 * @see ILaunchListener#launchRemoved(ILaunch)
368 public void launchRemoved(ILaunch launch) {
369 if (!isTerminated()) {
372 if (launch.equals(getLaunch())) {
373 // This target has been deregistered, but it hasn't successfully
375 // Update internal state to reflect that it is disconnected
381 * @see ILaunchListener#launchAdded(ILaunch)
383 public void launchAdded(ILaunch launch) {
387 * @see ILaunchListener#launchChanged(ILaunch)
389 public void launchChanged(ILaunch launch) {
393 * When a debug target or process terminates, terminate DBG Proxy.
395 * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
397 public void handleDebugEvents(DebugEvent[] events) {
398 for (int i = 0; i < events.length; i++) {
399 DebugEvent event = events[i];
400 if (event.getKind() == DebugEvent.TERMINATE) {
401 Object source = event.getSource();
402 if (source instanceof PHPDebugTarget
403 || source instanceof IDebugTarget) {
404 getPHPDBGProxy().stop();
405 } else if (source instanceof IProcess) {
406 if (getDebugTarget().getProcess() == (IProcess) source) {
407 getPHPDBGProxy().stop();
410 } else if (event.getKind() == DebugEvent.SUSPEND) {
411 getPHPDBGProxy().pause();