Importing the XDebugProxy code in the HEAD. The repo was tagged with T_BEFORE_XDEBUGP...
[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 org.eclipse.debug.core.DebugEvent;
10 import org.eclipse.debug.core.DebugException;
11 import org.eclipse.debug.core.DebugPlugin;
12 import org.eclipse.debug.core.IDebugEventSetListener;
13 import org.eclipse.debug.core.model.IBreakpoint;
14 import org.eclipse.debug.core.model.IStackFrame;
15 import org.eclipse.debug.core.model.IThread;
16
17 /**
18  * @author Axel
19  *
20  * TODO To change the template for this generated type comment go to
21  * Window - Preferences - Java - Code Style - Code Templates
22  */
23 public class XDebugThread extends XDebugElement implements IThread, IDebugEventSetListener {
24         private IStackFrame[]  fStackFrames;
25         
26         private IBreakpoint[] fBreakpoints;
27         
28         /* Whether this thread is stepping */
29         private boolean fStepping = false;
30         private boolean fTerminated = false;
31         
32         private int fStepCount = 0;
33         private int fCurrentStepCount = 0;
34         
35         /**
36          * Constructs a new thread for the given target
37          * 
38          * @param target VM
39          */
40         public XDebugThread(XDebugTarget target) {
41                 super(target);
42                 DebugPlugin.getDefault().addDebugEventListener(this);
43                 fStackFrames = null;
44         }
45         
46         public void incrementStepCounter() {
47                 fStepCount++;
48         }
49         
50         public IStackFrame[] getStackFrames() throws DebugException {
51                 IStackFrame[] newStackFrames = null;
52                 
53                 if (isSuspended()) {    
54                         if (fStepCount > fCurrentStepCount) {
55                                 newStackFrames = ((XDebugTarget) getDebugTarget()).getStackFrames();
56                                 
57                                 for (int i = 0; i < newStackFrames.length; i++) {
58                                         ((XDebugStackFrame)newStackFrames[i]).getVariables();
59                                 }
60                                 
61                                 if (fStackFrames != null) {
62                                         if (newStackFrames.length >= fStackFrames.length) {
63                                                 int delta = newStackFrames.length - fStackFrames.length + 1;
64                                                 
65                                                 for (int i = fStackFrames.length - 1; i >= 0; i--) {
66                                                         if (((XDebugStackFrame) fStackFrames[i]).equals(((XDebugStackFrame) newStackFrames[newStackFrames.length  - delta]))) {
67                                                                 int b = 2; b++;
68                                                                 //((XDebugStackFrame) newStackFrames[newStackFrames.length - delta]).evaluateChange((XDebugStackFrame) fStackFrames[i]);                                                                
69                                                         } else if (((XDebugStackFrame) fStackFrames[i]).isSameStackFrame(newStackFrames[newStackFrames.length  - delta])) {
70                                                                 //((XDebugStackFrame) newStackFrames[newStackFrames.length - delta]).evaluateChange((XDebugStackFrame) fStackFrames[i]);                                                                
71                                                         }
72                                                         
73                                                         delta ++;
74                                                 }
75                                         } else {
76                                         }
77                                 }
78
79                                 fCurrentStepCount++;
80
81                                 fStackFrames = newStackFrames;
82                         }
83                         return fStackFrames;
84                 } else {
85                         return new IStackFrame[0];
86                 }
87         }
88         
89         /* (non-Javadoc)
90          * @see org.eclipse.debug.core.model.IThread#hasStackFrames()
91          */
92         public boolean hasStackFrames() throws DebugException {
93                 return isSuspended();
94         }
95         
96         /* (non-Javadoc)
97          * @see org.eclipse.debug.core.model.IThread#getPriority()
98          */
99         public int getPriority() throws DebugException {
100                 return 0;
101         }
102         
103         /* (non-Javadoc)
104          * @see org.eclipse.debug.core.model.IThread#getTopStackFrame()
105          */
106         public IStackFrame getTopStackFrame() throws DebugException {
107                 IStackFrame[] frames = getStackFrames();
108                 if (frames.length > 0) {
109                         return frames[0];
110                 }
111                 
112                 return null;
113         }
114         
115         /* (non-Javadoc)
116          * @see org.eclipse.debug.core.model.IThread#getName()
117          */
118         public String getName() throws DebugException {
119                 return "Thread[1]";
120         }
121
122         /* (non-Javadoc)
123          * @see org.eclipse.debug.core.model.IThread#getBreakpoints()
124          */
125         public IBreakpoint[] getBreakpoints() {
126                 if (fBreakpoints == null) {
127                         return new IBreakpoint[0];
128                 }
129                 return fBreakpoints;
130         }
131         
132         /**
133          * Sets the breakpoints this thread is suspended at, or <code>null</code>
134          * if none.
135          * 
136          * @param breakpoints the breakpoints this thread is suspended at, or <code>null</code>
137          * if none
138          */
139         protected void setBreakpoints(IBreakpoint[] breakpoints) {
140                 fBreakpoints = breakpoints;
141         }
142         
143         /* (non-Javadoc)
144          * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
145          */
146         public boolean canResume() {
147                 return isSuspended();
148         }
149         
150         /* (non-Javadoc)
151          * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
152          */
153         public boolean canSuspend() {
154                 return !isTerminated() && !isSuspended();
155         }
156         
157         /* (non-Javadoc)
158          * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
159          */
160         public boolean isSuspended() {
161                 return fTarget.isSuspended();
162         }
163         
164         /* (non-Javadoc)
165          * @see org.eclipse.debug.core.model.ISuspendResume#resume()
166          */
167         public void resume() throws DebugException {
168                 fBreakpoints=null;
169                 fTarget.resume();
170         }
171         
172         /* (non-Javadoc)
173          * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
174          */
175         public void suspend() throws DebugException {
176                 fTarget.suspend();
177         }
178         
179         /* (non-Javadoc)
180          * @see org.eclipse.debug.core.model.IStep#canStepInto()
181          */
182         public boolean canStepInto() {
183                 return isSuspended();
184         }
185         
186         /* (non-Javadoc)
187          * @see org.eclipse.debug.core.model.IStep#canStepOver()
188          */
189         public boolean canStepOver() {
190                 return isSuspended();
191         }
192         
193         /* (non-Javadoc)
194          * @see org.eclipse.debug.core.model.IStep#canStepReturn()
195          */
196         public boolean canStepReturn() {
197                 if (fStackFrames != null) {
198                         return (fStackFrames.length > 1);
199                 } else {
200                         return false;
201                 }
202         }
203
204         /* (non-Javadoc)
205          * @see org.eclipse.debug.core.model.IStep#isStepping()
206          */
207         public boolean isStepping() {
208                 return fStepping;
209         }
210
211         /* (non-Javadoc)
212          * @see org.eclipse.debug.core.model.IStep#stepInto()
213          */
214         public void stepInto() throws DebugException {
215                 fBreakpoints=null;
216                 fTarget.step_into();
217         }
218
219         /* (non-Javadoc)
220          * @see org.eclipse.debug.core.model.IStep#stepOver()
221          */
222         public void stepOver() throws DebugException {
223                 fBreakpoints=null;
224                 fTarget.step_over();
225         }
226
227         /* (non-Javadoc)
228          * @see org.eclipse.debug.core.model.IStep#stepReturn()
229          */
230         public void stepReturn() throws DebugException {
231                 fBreakpoints=null;
232                 fTarget.step_out();
233         }
234
235         /* (non-Javadoc)
236          * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
237          */
238         public boolean canTerminate() {
239                 return !isTerminated();
240         }
241
242         /* (non-Javadoc)
243          * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
244          */
245         public boolean isTerminated() {
246                 return fTerminated;
247         }
248
249         /* (non-Javadoc)
250          * @see org.eclipse.debug.core.model.ITerminate#terminate()
251          */
252         public void terminate() throws DebugException {
253                 fTarget.getDebugConnection().stop();
254                 fTerminated = true;
255         }
256         
257         public void terminated() throws DebugException {
258                 fTerminated = true;
259         }
260
261         /**
262          * Sets whether this thread is stepping
263          * 
264          * @param stepping whether stepping
265          */
266         protected void setStepping(boolean stepping) {
267                 fStepping = stepping;
268         }
269
270         public void handleDebugEvents(DebugEvent[] events) {
271                 DebugEvent de = events[0];
272                 System.out.println(de.toString());      
273         }
274
275         public void removeEventListeners() {
276                 DebugPlugin.getDefault().removeDebugEventListener(this);
277         }
278 }