c86f72be438f81a90e16e28806ed889411ff1fad
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPDebugTarget.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Vicente Fernando - www.alfersoft.com.ar
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.model;
13
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;
27
28 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
29 import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
30
31 /**
32  * Debug target for PHP debug model.
33  */
34 public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugEventSetListener {
35                 
36         private IProcess process;
37         private boolean isTerminated;
38         private boolean isSuspended;
39         private ILaunch launch;
40         private PHPThread[] threads;
41         private PHPDBGProxy phpDBGProxy;
42
43         public PHPDebugTarget(ILaunch launch, IProcess process) {
44                 this.isSuspended = false;
45                 this.launch = launch;
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);
51                 initialize();
52         }
53
54         protected synchronized void initialize() {
55                 DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
56                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });    
57         }
58
59         public void addThread(PHPThread phpThread) {
60                 int i;
61                 PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
62                 
63                 for(i=0; i < threads.length; i++) {
64                         updatedThreads[i] = threads[i];
65                 }
66                 updatedThreads[i] = phpThread;
67                 threads = updatedThreads;
68
69                 fireChangeEvent();
70         }
71
72         private void fireChangeEvent() {
73                 DebugEvent ev = new DebugEvent(this, DebugEvent.CHANGE);
74                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
75         }
76
77         protected PHPThread getThreadById(int id) {
78                 for (int i = 0; i < threads.length; i++) {
79                         if (threads[i].getId() == id) {
80                                 return threads[i];
81                         }
82                 }
83                 return null;
84         }
85
86         public IThread[] getThreads() {
87                 return threads;
88         }
89
90         public boolean hasThreads() throws DebugException {
91                 return threads.length > 0;
92         }
93
94         public String getName() throws DebugException {
95                 return "PHP Debugger at localhost:" + getPHPDBGProxy().getPort();
96         }
97
98         public boolean supportsBreakpoint(IBreakpoint arg0) {
99                 return false;
100         }
101
102         public String getModelIdentifier() {
103                 return PHPDebugCorePlugin.PLUGIN_ID;
104         }
105
106         public IDebugTarget getDebugTarget() {
107                 return this;
108         }
109
110         public ILaunch getLaunch() {
111                 return launch;
112         }
113
114         public boolean canTerminate() {
115                 return !isTerminated;
116         }
117
118         public boolean isTerminated() {
119                 return isTerminated;
120         }
121
122         public synchronized void terminate() {
123                 // This method is synchronized to control a race condition between the 
124                 // UI thread that terminates the debugging session, and the slave 
125                 // thread that executes PHPLoop.run
126                 if (isTerminated)
127                         // Avoid terminating twice...
128                         return;
129                 phpDBGProxy.stop();
130                 this.threads = new PHPThread[0];
131                 isTerminated = true;
132                 fireChangeEvent();
133         }
134
135         public boolean canResume() {
136                 if(isTerminated) return false;
137                 return isSuspended;
138         }
139
140         public boolean canSuspend() {
141                 if(isTerminated) return false;
142                 return !isSuspended;
143         }
144
145         public boolean isSuspended() {
146                 return isSuspended;
147         }
148
149         public void resume() throws DebugException {
150                 this.getPHPDBGProxy().resume();
151                 isSuspended= false;
152         }
153
154         public void suspend() throws DebugException {
155                 this.getPHPDBGProxy().pause();
156                 isSuspended= true;
157         }
158
159         public void breakpointAdded(IBreakpoint breakpoint) {
160                 this.getPHPDBGProxy().addBreakpoint(breakpoint) ;
161         }
162
163         public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
164                 this.getPHPDBGProxy().removeBreakpoint(breakpoint) ;            
165         }
166
167         public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta arg1) {
168                 // is called e.g. after a line has been inserted before a breakpoint
169                 // but then the debugger is out of sync with the file anyway, so debugging
170                 // should be stopped here.
171         }
172
173         public boolean canDisconnect() {
174                 return false;
175         }
176
177         public void disconnect() throws DebugException {
178         }
179
180         public boolean isDisconnected() {
181                 return false;
182         }
183
184         public boolean supportsStorageRetrieval() {
185                 return false;
186         }
187
188         public IMemoryBlock getMemoryBlock(long arg0, long arg1) throws DebugException {
189                 return null;
190         }
191
192         public Object getAdapter(Class arg0) {
193                 return null;
194         }
195
196         public IProcess getProcess() {
197                 return process;
198         }
199
200         public void setProcess(IProcess process) {
201                 this.process = process;
202         }
203
204         public PHPDBGProxy getPHPDBGProxy() {
205                 return phpDBGProxy;
206         }
207
208         public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
209                 this.phpDBGProxy = phpDBGProxy;
210         }
211         
212         /**
213          * @see ILaunchListener#launchRemoved(ILaunch)
214          */
215         public void launchRemoved(ILaunch launch) {
216                 if (!isTerminated()) {
217                         return;
218                 }
219                 if (launch.equals(getLaunch())) {
220                         // This target has been deregistered, but it hasn't successfully terminated.
221                         // Update internal state to reflect that it is disconnected
222                         terminate();
223                 }
224         }
225         
226         /**
227          * @see ILaunchListener#launchAdded(ILaunch)
228          */
229         public void launchAdded(ILaunch launch) {
230         }
231         
232         /**
233          * @see ILaunchListener#launchChanged(ILaunch)
234          */
235         public void launchChanged(ILaunch launch) {
236         }
237         
238         /**
239          * When a debug target or process terminates, terminate DBG Proxy.
240          * 
241          * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
242          */
243         public void handleDebugEvents(DebugEvent[] events) {
244                 for (int i = 0; i < events.length; i++) {
245                         DebugEvent event = events[i];
246                         if (event.getKind() == DebugEvent.TERMINATE) {
247                                 Object source = event.getSource();
248                                 if (source instanceof PHPDebugTarget || source instanceof IDebugTarget) {
249                                         getPHPDBGProxy().stop();
250                                 } else if(source instanceof IProcess) {
251                                         if(getDebugTarget().getProcess() == (IProcess)source) {
252                                                 getPHPDBGProxy().stop();
253                                         }
254                                 }
255                         } else if (event.getKind() == DebugEvent.SUSPEND) {
256                                 getPHPDBGProxy().pause();
257                         }
258                 }
259         }
260 }
261