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.IThread;
33 import org.eclipse.jface.resource.ImageDescriptor;
34 import org.eclipse.ui.model.IWorkbenchAdapter;
37 * Debug target for PHP debug model.
39 public class PHPDebugTarget extends PHPDebugElement implements IPHPDebugTarget, ILaunchListener,
40 IDebugEventSetListener {
42 private IProcess process;
44 private ILaunch launch;
46 private PHPThread[] threads = new PHPThread[0];
48 private PHPDBGProxy phpDBGProxy;
51 private boolean isTerminated = false;
53 private boolean isSuspended = false;
55 boolean isTerminated() {
59 boolean isSuspended() {
63 void setTerminated(boolean terminated) {
64 this.isTerminated = terminated;
67 void setSuspended(boolean suspended) {
69 throw new IllegalStateException();
70 this.isSuspended = suspended;
74 private final State state = new State();
76 public PHPDebugTarget(ILaunch launch, IProcess process) {
78 if (null == launch && null == process)
79 throw new IllegalArgumentException();
81 this.process = process;
82 // TODO XXX remove breakpoint listener at termination to avoid live leak
83 IBreakpointManager manager = DebugPlugin.getDefault()
84 .getBreakpointManager();
85 manager.addBreakpointListener(this);
86 DebugPlugin.getDefault().addDebugEventListener(this);
90 protected synchronized void initialize() {
91 DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
92 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
95 public void addThread(PHPThread phpThread) {
97 PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
99 for (i = 0; i < threads.length; i++) {
100 updatedThreads[i] = threads[i];
102 updatedThreads[i] = phpThread;
103 threads = updatedThreads;
106 fireThreadCreateEvent(phpThread);
109 private void fireChangeEvent() {
110 DebugEvent ev = new DebugEvent(this, DebugEvent.CHANGE);
111 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
114 private void fireThreadCreateEvent(PHPThread phpThread) {
115 DebugEvent ev = new DebugEvent(phpThread, DebugEvent.CREATE);
116 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
119 protected PHPThread getThreadById(int id) {
120 for (int i = 0; i < threads.length; i++) {
121 if (threads[i].getId() == id) {
128 public IThread[] getThreads() {
132 public boolean hasThreads() throws DebugException {
133 return threads.length > 0;
136 public String getName() throws DebugException {
137 return "PHP Debugger at localhost:" + getPHPDBGProxy().getPort();
140 public boolean supportsBreakpoint(IBreakpoint arg0) {
141 if (arg0.getModelIdentifier().equals(PHPDebugCorePlugin.PLUGIN_ID)) {
147 public String getModelIdentifier() {
148 return PHPDebugCorePlugin.PLUGIN_ID;
151 public IDebugTarget getDebugTarget() {
155 public ILaunch getLaunch() {
159 public synchronized boolean canTerminate() {
160 return !isTerminated();
163 public synchronized boolean isTerminated() {
164 return state.isTerminated();
167 public synchronized void terminate() {
168 // This method is synchronized to control a race condition between the
169 // UI thread that terminates the debugging session, and the slave
170 // thread that executes PHPLoop.run
172 // Avoid terminating twice...
174 state.setTerminated(true);
176 this.threads = new PHPThread[0];
178 IBreakpointManager manager = DebugPlugin.getDefault()
179 .getBreakpointManager();
180 manager.removeBreakpointListener(this);
181 DebugPlugin.getDefault().removeDebugEventListener(this);
184 public synchronized boolean canResume() {
187 return isSuspended();
190 public synchronized boolean canSuspend() {
193 return !isSuspended();
196 public synchronized boolean isSuspended() {
197 return state.isSuspended();
200 public synchronized void resume() throws DebugException {
203 state.setSuspended(false);
204 this.getPHPDBGProxy().resume();
205 IThread[] threads = getThreads();
206 for (int i = 0; i < threads.length; ++i)
210 public synchronized void suspend() throws DebugException {
213 this.getPHPDBGProxy().pause();
214 state.setSuspended(true);
215 IThread[] threads = getThreads();
216 for (int i = 0; i < threads.length; ++i)
217 threads[i].suspend();
220 public void breakpointAdded(IBreakpoint breakpoint) {
221 this.getPHPDBGProxy().addBreakpoint(breakpoint);
224 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
225 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
229 * The method will be called when the user enables/disables
230 * breakpoints. In this case we add or remove the breakpoint.
231 * It's also called when leaving the breakpoint properties dialog
232 * (skip count and breakpoint condition) with the OK button.
234 * This method is also called whenever a source file has changed.
235 * In this case we terminate since the source will be out of sync with the debugger.
236 * TODO Is it correct to call this method when a sourcefile is modified?
239 public void breakpointChanged (IBreakpoint breakpoint, IMarkerDelta arg1) {
240 PHPLineBreakpoint bp;
241 bp = (PHPLineBreakpoint) breakpoint;
244 if (breakpoint.isEnabled () && // Check if breakpoint state changed from disabled to enabled
245 !arg1.getAttribute ("org.eclipse.debug.core.enabled", false)) {
246 this.getPHPDBGProxy().addBreakpoint(breakpoint);
248 else if (!breakpoint.isEnabled () && // Check if breakpoint state changed from enabled to disabled
249 arg1.getAttribute ("org.eclipse.debug.core.enabled", true)) {
250 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
252 else if (bp.getChangeID() != arg1.getAttribute ("net.sourceforge.phpeclipse.debug.changeID", 0)) {
253 if (breakpoint.isEnabled()) { // If the breakpoint is already enabled
254 this.getPHPDBGProxy().removeBreakpoint(breakpoint); // we remove this breakpoint first
255 this.getPHPDBGProxy().addBreakpoint(breakpoint); // and then we add again (else DBG would have two breakpoints!).
258 this.getPHPDBGProxy().removeBreakpoint(breakpoint);
261 else { // All other cases will terminate the debugger
264 } catch (CoreException e) {
269 public boolean canDisconnect() {
273 public void disconnect() throws DebugException {
276 public boolean isDisconnected() {
280 public boolean supportsStorageRetrieval() {
284 public IMemoryBlock getMemoryBlock(long arg0, long arg1)
285 throws DebugException {
289 public Object getAdapter(Class arg0) {
290 if (IWorkbenchAdapter.class.equals(arg0)) {
291 return new IWorkbenchAdapter() {
292 public Object[] getChildren(Object o) {
293 Object[] children = null;
294 IThread[] threads = getThreads();
295 if (null != threads) {
296 children = new Object[threads.length];
297 for (int i = 0; i < threads.length; ++i)
298 children[i] = threads[i];
303 public ImageDescriptor getImageDescriptor(Object object) {
307 public String getLabel(Object o) {
308 String label = "(Unable to look up name... check error log)";
311 } catch (DebugException x) {
312 PHPeclipsePlugin.log(label, x);
317 public Object getParent(Object o) {
318 return PHPDebugTarget.this.getLaunch();
325 public IProcess getProcess() {
329 public void setProcess(IProcess process) {
330 this.process = process;
333 public PHPDBGProxy getPHPDBGProxy() {
337 public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
338 this.phpDBGProxy = phpDBGProxy;
342 * @see ILaunchListener#launchRemoved(ILaunch)
344 public void launchRemoved(ILaunch launch) {
345 if (!isTerminated()) {
348 if (launch.equals(getLaunch())) {
349 // This target has been deregistered, but it hasn't successfully
351 // Update internal state to reflect that it is disconnected
357 * @see ILaunchListener#launchAdded(ILaunch)
359 public void launchAdded(ILaunch launch) {
363 * @see ILaunchListener#launchChanged(ILaunch)
365 public void launchChanged(ILaunch launch) {
369 * When a debug target or process terminates, terminate DBG Proxy.
371 * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
373 public void handleDebugEvents(DebugEvent[] events) {
374 for (int i = 0; i < events.length; i++) {
375 DebugEvent event = events[i];
376 if (event.getKind() == DebugEvent.TERMINATE) {
377 Object source = event.getSource();
378 if (source instanceof PHPDebugTarget
379 || source instanceof IDebugTarget) {
380 getPHPDBGProxy().stop();
381 } else if (source instanceof IProcess) {
382 if (getDebugTarget().getProcess() == (IProcess) source) {
383 getPHPDBGProxy().stop();
386 } else if (event.getKind() == DebugEvent.SUSPEND) {
387 getPHPDBGProxy().pause();