fa29d34f3f6776ace0454ab2132010630ef8d10a
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugThread.java
1 /*
2  * Created on 23.11.2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package net.sourceforge.phpeclipse.xdebug.php.model;
8
9 import java.net.MalformedURLException;
10 import java.net.URL;
11
12 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
13 import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.XDebugResponse;
14
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.IDebugEventSetListener;
19 import org.eclipse.debug.core.model.IBreakpoint;
20 import org.eclipse.debug.core.model.IStackFrame;
21 import org.eclipse.debug.core.model.IThread;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NodeList;
24
25 /**
26  * @author Axel
27  *
28  * TODO To change the template for this generated type comment go to
29  * Window - Preferences - Java - Code Style - Code Templates
30  */
31 public class XDebugThread extends XDebugElement implements IThread, IDebugEventSetListener {
32         private XDebugStackFrame[]  fStackFrames;
33         
34         private IBreakpoint[] fBreakpoints;
35         
36         /* Whether this thread is stepping */
37         private boolean fStepping = false;
38         private boolean fTerminated = false;
39         
40         private int fStepCount = 0;
41         private int fCurrentStepCount = 0;
42         
43         /**
44          * Constructs a new thread for the given target
45          * 
46          * @param target VM
47          */
48         public XDebugThread(XDebugTarget target) {
49                 super(target);
50                 DebugPlugin.getDefault().addDebugEventListener(this);
51                 fStackFrames = null;
52         }
53         
54         public void incrementStepCounter() {
55                 fStepCount++;
56         }
57         
58         public IStackFrame[] getStackFrames() throws DebugException {
59                 XDebugStackFrame[] newStackFrames = null;
60                 
61                 if (isSuspended()) {    
62                         if (fStepCount > fCurrentStepCount) {
63                                 //newStackFrames = ((XDebugTarget) getDebugTarget()).getStackFrames();
64                                 
65                                 XDebugResponse dr = ((XDebugTarget) getDebugTarget()).getStackFrames();
66                                 newStackFrames = _getStackFrames(dr);
67
68                                 for (int i = 0; i < newStackFrames.length; i++) {
69                                         newStackFrames[i].getVariables();
70                                 }
71                                 
72                                 if (fStackFrames != null) {
73                                         if (newStackFrames.length >= fStackFrames.length) {
74                                                 int delta = newStackFrames.length - fStackFrames.length + 1;
75                                                 
76                                                 for (int i = fStackFrames.length - 1; i >= 0; i--) {
77                                                         if (fStackFrames[i].equals(newStackFrames[newStackFrames.length - delta])) {
78                                                                 int b = 2; b++;
79                                                                 //((XDebugStackFrame) newStackFrames[newStackFrames.length - delta]).evaluateChange((XDebugStackFrame) fStackFrames[i]);                                                                
80                                                         } else if (fStackFrames[i].isSameStackFrame(newStackFrames[newStackFrames.length - delta])) {
81                                                                 int b = 2; b++;
82                                                                 //((XDebugStackFrame) newStackFrames[newStackFrames.length - delta]).evaluateChange((XDebugStackFrame) fStackFrames[i]);                                                                
83                                                         }
84                                                         
85                                                         delta ++;
86                                                 }
87                                         } else {
88                                         }
89                                 }
90
91                                 fCurrentStepCount++;
92
93                                 fStackFrames = newStackFrames;
94                         }
95                         return fStackFrames;
96                 } else {
97                         return new IStackFrame[0];
98                 }
99         }
100         
101         private XDebugStackFrame[] _getStackFrames(XDebugResponse lastResponse) {
102                 //IStackFrame[] newStackFrames = null;
103
104         if (lastResponse.isError())
105                 return new XDebugStackFrame[0];
106         Node response = lastResponse.getParentNode();
107         NodeList frames = response.getChildNodes();
108         XDebugStackFrame[] theFrames = new XDebugStackFrame[frames.getLength()];
109         for (int i = 0; i < frames.getLength(); i++) {
110                 Node stackNode = frames.item(i);
111                 XDebugStackFrame frame = new XDebugStackFrame(this/*fThread*/, i);
112                 String level =PHPDebugUtils.getAttributeValue(stackNode,"level");
113                 if (!"".equals(level))
114                         frame.setLevel(Integer.parseInt(level));
115
116                 frame.setType(PHPDebugUtils.getAttributeValue(stackNode,"type"));
117                 String fileName=PHPDebugUtils.unescapeString(PHPDebugUtils.getAttributeValue(stackNode,"filename"));
118                 String lineNo=PHPDebugUtils.getAttributeValue(stackNode,"lineno");
119
120                 if (!"".equals(lineNo))
121                         frame.setLineNumber(Integer.parseInt(lineNo));
122                 
123                 frame.setWhere(PHPDebugUtils.getAttributeValue(stackNode,"where"));
124                 
125                 try {
126                         frame.setFullName(new URL(fileName));
127                 } catch (MalformedURLException e) {
128                         e.printStackTrace();
129                 }
130         
131                 frame.incrementStepCounter();
132                 
133                 theFrames[i] = frame;
134         }
135                 return /*newStackFrames*/theFrames;
136         }
137                         
138         /* (non-Javadoc)
139          * @see org.eclipse.debug.core.model.IThread#hasStackFrames()
140          */
141         public boolean hasStackFrames() throws DebugException {
142                 return isSuspended();
143         }
144         
145         /* (non-Javadoc)
146          * @see org.eclipse.debug.core.model.IThread#getPriority()
147          */
148         public int getPriority() throws DebugException {
149                 return 0;
150         }
151         
152         /* (non-Javadoc)
153          * @see org.eclipse.debug.core.model.IThread#getTopStackFrame()
154          */
155         public IStackFrame getTopStackFrame() throws DebugException {
156                 IStackFrame[] frames = getStackFrames();
157                 if (frames.length > 0) {
158                         return frames[0];
159                 }
160                 
161                 return null;
162         }
163         
164         /* (non-Javadoc)
165          * @see org.eclipse.debug.core.model.IThread#getName()
166          */
167         public String getName() throws DebugException {
168                 return "Thread[1]";
169         }
170
171         /* (non-Javadoc)
172          * @see org.eclipse.debug.core.model.IThread#getBreakpoints()
173          */
174         public IBreakpoint[] getBreakpoints() {
175                 if (fBreakpoints == null) {
176                         return new IBreakpoint[0];
177                 }
178                 return fBreakpoints;
179         }
180         
181         /**
182          * Sets the breakpoints this thread is suspended at, or <code>null</code>
183          * if none.
184          * 
185          * @param breakpoints the breakpoints this thread is suspended at, or <code>null</code>
186          * if none
187          */
188         protected void setBreakpoints(IBreakpoint[] breakpoints) {
189                 fBreakpoints = breakpoints;
190         }
191         
192         /* (non-Javadoc)
193          * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
194          */
195         public boolean canResume() {
196                 return isSuspended();
197         }
198         
199         /* (non-Javadoc)
200          * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
201          */
202         public boolean canSuspend() {
203                 return !isTerminated() && !isSuspended();
204         }
205         
206         /* (non-Javadoc)
207          * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
208          */
209         public boolean isSuspended() {
210                 return fTarget.isSuspended();
211         }
212         
213         /* (non-Javadoc)
214          * @see org.eclipse.debug.core.model.ISuspendResume#resume()
215          */
216         public void resume() throws DebugException {
217                 fBreakpoints=null;
218                 fTarget.resume();
219         }
220         
221         /* (non-Javadoc)
222          * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
223          */
224         public void suspend() throws DebugException {
225                 fTarget.suspend();
226         }
227         
228         /* (non-Javadoc)
229          * @see org.eclipse.debug.core.model.IStep#canStepInto()
230          */
231         public boolean canStepInto() {
232                 return isSuspended();
233         }
234         
235         /* (non-Javadoc)
236          * @see org.eclipse.debug.core.model.IStep#canStepOver()
237          */
238         public boolean canStepOver() {
239                 return isSuspended();
240         }
241         
242         /* (non-Javadoc)
243          * @see org.eclipse.debug.core.model.IStep#canStepReturn()
244          */
245         public boolean canStepReturn() {
246                 if (fStackFrames != null) {
247                         return (fStackFrames.length > 1);
248                 } else {
249                         return false;
250                 }
251         }
252
253         /* (non-Javadoc)
254          * @see org.eclipse.debug.core.model.IStep#isStepping()
255          */
256         public boolean isStepping() {
257                 return fStepping;
258         }
259
260         /* (non-Javadoc)
261          * @see org.eclipse.debug.core.model.IStep#stepInto()
262          */
263         public void stepInto() throws DebugException {
264                 fBreakpoints=null;
265                 fTarget.step_into();
266         }
267
268         /* (non-Javadoc)
269          * @see org.eclipse.debug.core.model.IStep#stepOver()
270          */
271         public void stepOver() throws DebugException {
272                 fBreakpoints=null;
273                 fTarget.step_over();
274         }
275
276         /* (non-Javadoc)
277          * @see org.eclipse.debug.core.model.IStep#stepReturn()
278          */
279         public void stepReturn() throws DebugException {
280                 fBreakpoints=null;
281                 fTarget.step_out();
282         }
283
284         /* (non-Javadoc)
285          * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
286          */
287         public boolean canTerminate() {
288                 return !isTerminated();
289         }
290
291         /* (non-Javadoc)
292          * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
293          */
294         public boolean isTerminated() {
295                 return fTerminated;
296         }
297
298         /* (non-Javadoc)
299          * @see org.eclipse.debug.core.model.ITerminate#terminate()
300          */
301         public void terminate() throws DebugException {
302                 fTarget.getDebugConnection().stop();
303                 fTerminated = true;
304         }
305         
306         public void terminated() throws DebugException {
307                 fTerminated = true;
308         }
309
310         /**
311          * Sets whether this thread is stepping
312          * 
313          * @param stepping whether stepping
314          */
315         protected void setStepping(boolean stepping) {
316                 fStepping = stepping;
317         }
318
319         public void handleDebugEvents(DebugEvent[] events) {
320                 DebugEvent de = events[0];
321                 System.out.println(de.toString());      
322         }
323
324         public void removeEventListeners() {
325                 DebugPlugin.getDefault().removeDebugEventListener(this);
326         }
327 }