2 * Created on 23.11.2004
4 * TODO To change the template for this generated file go to
5 * Window - Preferences - Java - Code Style - Code Templates
7 package net.sourceforge.phpeclipse.xdebug.php.model;
9 import java.net.MalformedURLException;
12 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
13 import net.sourceforge.phpeclipse.xdebug.core.xdebug.ResponseListener.XDebugResponse;
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;
28 * TODO To change the template for this generated type comment go to
29 * Window - Preferences - Java - Code Style - Code Templates
31 public class XDebugThread extends XDebugElement implements IThread, IDebugEventSetListener {
32 private XDebugStackFrame[] fStackFrames;
34 private IBreakpoint[] fBreakpoints;
36 /* Whether this thread is stepping */
37 private boolean fStepping = false;
38 private boolean fTerminated = false;
40 private int fStepCount = 0;
41 private int fCurrentStepCount = 0;
44 * Constructs a new thread for the given target
48 public XDebugThread(XDebugTarget target) {
50 DebugPlugin.getDefault().addDebugEventListener(this);
54 public void incrementStepCounter() {
57 public IStackFrame[] getStackFrames() throws DebugException {
59 return new IStackFrame[0];
62 if (fStepCount > fCurrentStepCount) {
63 XDebugResponse dr = ((XDebugTarget) getDebugTarget()).getStackFrames();
64 XDebugStackFrame[] newStackFrames = _getStackFrames(dr);
66 /*if (fStackFrames != null) {
67 if (newStackFrames.length >= fStackFrames.length) {
68 int delta = newStackFrames.length - fStackFrames.length + 1;
70 for (int i = fStackFrames.length - 1; i >= 0; i--) {
71 if (fStackFrames[i].equals(newStackFrames[newStackFrames.length - delta])) {
73 //((XDebugStackFrame) newStackFrames[newStackFrames.length - delta]).evaluateChange((XDebugStackFrame) fStackFrames[i]);
74 } else if (fStackFrames[i].isSameStackFrame(newStackFrames[newStackFrames.length - delta])) {
76 //((XDebugStackFrame) newStackFrames[newStackFrames.length - delta]).evaluateChange((XDebugStackFrame) fStackFrames[i]);
82 fStackFrames = newStackFrames;
85 fStackFrames = newStackFrames;
90 fStackFrames = newStackFrames;
97 private XDebugStackFrame[] _getStackFrames(XDebugResponse lastResponse) {
98 if (lastResponse.isError()) {
99 return new XDebugStackFrame[0];
102 Node response = lastResponse.getParentNode();
103 NodeList frames = response.getChildNodes();
104 XDebugStackFrame[] theFrames = new XDebugStackFrame[frames.getLength()];
105 for (int i = 0; i < frames.getLength(); i++) {
106 Node stackNode = frames.item(i);
107 XDebugStackFrame frame = new XDebugStackFrame(this/*fThread*/, i);
108 String level =PHPDebugUtils.getAttributeValue(stackNode,"level");
109 if (!"".equals(level))
110 frame.setLevel(Integer.parseInt(level));
112 frame.setType(PHPDebugUtils.getAttributeValue(stackNode,"type"));
113 String fileName=PHPDebugUtils.unescapeString(PHPDebugUtils.getAttributeValue(stackNode,"filename"));
114 String lineNo=PHPDebugUtils.getAttributeValue(stackNode,"lineno");
116 if (!"".equals(lineNo))
117 frame.setLineNumber(Integer.parseInt(lineNo));
119 frame.setWhere(PHPDebugUtils.getAttributeValue(stackNode,"where"));
122 frame.setFullName(new URL(fileName));
123 } catch (MalformedURLException e) {
127 frame.incrementStepCounter();
129 theFrames[i] = frame;
136 * @see org.eclipse.debug.core.model.IThread#hasStackFrames()
138 public boolean hasStackFrames() throws DebugException {
139 return isSuspended();
143 * @see org.eclipse.debug.core.model.IThread#getPriority()
145 public int getPriority() throws DebugException {
150 * @see org.eclipse.debug.core.model.IThread#getTopStackFrame()
152 public IStackFrame getTopStackFrame() throws DebugException {
153 IStackFrame[] frames = getStackFrames();
154 if (frames.length > 0) {
162 * @see org.eclipse.debug.core.model.IThread#getName()
164 public String getName() throws DebugException {
169 * @see org.eclipse.debug.core.model.IThread#getBreakpoints()
171 public IBreakpoint[] getBreakpoints() {
172 if (fBreakpoints == null) {
173 return new IBreakpoint[0];
179 * Sets the breakpoints this thread is suspended at, or <code>null</code>
182 * @param breakpoints the breakpoints this thread is suspended at, or <code>null</code>
185 protected void setBreakpoints(IBreakpoint[] breakpoints) {
186 fBreakpoints = breakpoints;
190 * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
192 public boolean canResume() {
193 return isSuspended();
197 * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
199 public boolean canSuspend() {
200 return !isTerminated() && !isSuspended();
204 * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
206 public boolean isSuspended() {
207 // return fTarget.isSuspended();
208 return getDebugTarget().isSuspended();
212 * @see org.eclipse.debug.core.model.ISuspendResume#resume()
214 public void resume() throws DebugException {
216 getDebugTarget().resume();
220 * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
222 public void suspend() throws DebugException {
223 getDebugTarget().suspend();
227 * @see org.eclipse.debug.core.model.IStep#canStepInto()
229 public boolean canStepInto() {
230 return isSuspended();
234 * @see org.eclipse.debug.core.model.IStep#canStepOver()
236 public boolean canStepOver() {
237 return isSuspended();
241 * @see org.eclipse.debug.core.model.IStep#canStepReturn()
243 public boolean canStepReturn() {
244 if (fStackFrames != null) {
245 return (fStackFrames.length > 1);
252 * @see org.eclipse.debug.core.model.IStep#isStepping()
254 public boolean isStepping() {
259 * @see org.eclipse.debug.core.model.IStep#stepInto()
261 public void stepInto() throws DebugException {
263 ((XDebugTarget) getDebugTarget()).step_into();
267 * @see org.eclipse.debug.core.model.IStep#stepOver()
269 public void stepOver() throws DebugException {
271 ((XDebugTarget) getDebugTarget()).step_over();
275 * @see org.eclipse.debug.core.model.IStep#stepReturn()
277 public void stepReturn() throws DebugException {
279 ((XDebugTarget) getDebugTarget()).step_out();
283 * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
285 public boolean canTerminate() {
286 return !isTerminated();
290 * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
292 public boolean isTerminated() {
297 * @see org.eclipse.debug.core.model.ITerminate#terminate()
299 public void terminate() throws DebugException {
300 ((XDebugTarget) getDebugTarget()).getDebugConnection().stop();
304 public void terminated() throws DebugException {
309 * Sets whether this thread is stepping
311 * @param stepping whether stepping
313 protected void setStepping(boolean stepping) {
314 fStepping = stepping;
317 public void handleDebugEvents(DebugEvent[] events) {
318 DebugEvent de = events[0];
319 System.out.println(de.toString());
322 public void removeEventListeners() {
323 DebugPlugin.getDefault().removeDebugEventListener(this);
327 * Fires a debug event
329 * @param event the event to be fired
331 protected void fireEvent(DebugEvent event) {
332 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {event});
336 * Fires a <code>CREATE</code> event for this element.
338 public void fireCreationEvent() {
339 fireEvent(new DebugEvent(this, DebugEvent.CREATE));
343 * Fires a <code>RESUME</code> event for this element with
346 * @param detail event detail code
348 public void fireResumeEvent(int detail) {
349 fireEvent(new DebugEvent(this, DebugEvent.RESUME, detail));
353 * Fires a <code>SUSPEND</code> event for this element with
356 * @param detail event detail code
358 public void fireSuspendEvent(int detail) {
359 fireEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
363 * Fires a <code>TERMINATE</code> event for this element.
365 protected void fireTerminateEvent() {
366 fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));