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 org.eclipse.core.resources.IMarkerDelta;
15 import org.eclipse.debug.core.DebugEvent;
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.DebugPlugin;
18 import org.eclipse.debug.core.IBreakpointManager;
19 import org.eclipse.debug.core.IDebugEventSetListener;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.ILaunchListener;
22 import org.eclipse.debug.core.model.IDebugTarget;
23 import org.eclipse.debug.core.model.IBreakpoint;
24 import org.eclipse.debug.core.model.IMemoryBlock;
25 import org.eclipse.debug.core.model.IProcess;
26 import org.eclipse.debug.core.model.IThread;
28 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
29 import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
32 * Debug target for PHP debug model.
34 public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugEventSetListener {
36 private IProcess process;
37 private boolean isTerminated;
38 private boolean isSuspended;
39 private ILaunch launch;
40 private PHPThread[] threads;
41 private PHPDBGProxy phpDBGProxy;
43 public PHPDebugTarget(ILaunch launch, IProcess process) {
44 this.isSuspended = false;
46 this.process = process;
47 this.threads = new PHPThread[0];
48 IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
49 manager.addBreakpointListener(this);
50 DebugPlugin.getDefault().addDebugEventListener(this);
54 protected synchronized void initialize() {
55 DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
56 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
59 public void addThread(PHPThread phpThread) {
61 PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
63 for(i=0; i < threads.length; i++) {
64 updatedThreads[i] = threads[i];
66 updatedThreads[i] = phpThread;
67 threads = updatedThreads;
72 private void fireChangeEvent() {
73 DebugEvent ev = new DebugEvent(this, DebugEvent.CHANGE);
74 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
77 protected PHPThread getThreadById(int id) {
78 for (int i = 0; i < threads.length; i++) {
79 if (threads[i].getId() == id) {
86 public IThread[] getThreads() {
90 public boolean hasThreads() throws DebugException {
91 return threads.length > 0;
94 public String getName() throws DebugException {
95 return "PHP Debugger at localhost:" + getPHPDBGProxy().getPort();
98 public boolean supportsBreakpoint(IBreakpoint arg0) {
102 public String getModelIdentifier() {
103 return PHPDebugCorePlugin.PLUGIN_ID;
106 public IDebugTarget getDebugTarget() {
110 public ILaunch getLaunch() {
114 public boolean canTerminate() {
115 return !isTerminated;
118 public boolean isTerminated() {
122 public void terminate() {
124 this.threads = new PHPThread[0];
129 public boolean canResume() {
130 if(isTerminated) return false;
134 public boolean canSuspend() {
135 if(isTerminated) return false;
139 public boolean isSuspended() {
143 public void resume() throws DebugException {
144 this.getPHPDBGProxy().resume();
148 public void suspend() throws DebugException {
149 this.getPHPDBGProxy().pause();
153 public void breakpointAdded(IBreakpoint breakpoint) {
154 this.getPHPDBGProxy().addBreakpoint(breakpoint) ;
157 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
158 this.getPHPDBGProxy().removeBreakpoint(breakpoint) ;
161 public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta arg1) {
162 // is called e.g. after a line has been inserted before a breakpoint
163 // but then the debugger is out of sync with the file anyway, so debugging
164 // should be stopped here.
167 public boolean canDisconnect() {
171 public void disconnect() throws DebugException {
174 public boolean isDisconnected() {
178 public boolean supportsStorageRetrieval() {
182 public IMemoryBlock getMemoryBlock(long arg0, long arg1) throws DebugException {
186 public Object getAdapter(Class arg0) {
190 public IProcess getProcess() {
194 public void setProcess(IProcess process) {
195 this.process = process;
198 public PHPDBGProxy getPHPDBGProxy() {
202 public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
203 this.phpDBGProxy = phpDBGProxy;
207 * @see ILaunchListener#launchRemoved(ILaunch)
209 public void launchRemoved(ILaunch launch) {
210 if (!isTerminated()) {
213 if (launch.equals(getLaunch())) {
214 // This target has been deregistered, but it hasn't successfully terminated.
215 // Update internal state to reflect that it is disconnected
221 * @see ILaunchListener#launchAdded(ILaunch)
223 public void launchAdded(ILaunch launch) {
227 * @see ILaunchListener#launchChanged(ILaunch)
229 public void launchChanged(ILaunch launch) {
233 * When a debug target or process terminates, terminate DBG Proxy.
235 * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
237 public void handleDebugEvents(DebugEvent[] events) {
238 for (int i = 0; i < events.length; i++) {
239 DebugEvent event = events[i];
240 if (event.getKind() == DebugEvent.TERMINATE) {
241 Object source = event.getSource();
242 if (source instanceof PHPDebugTarget || source instanceof IDebugTarget) {
243 getPHPDBGProxy().stop();
244 } else if(source instanceof IProcess) {
245 if(getDebugTarget().getProcess() == (IProcess)source) {
246 getPHPDBGProxy().stop();
249 } else if (event.getKind() == DebugEvent.SUSPEND) {
250 getPHPDBGProxy().pause();