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() {
58 public IStackFrame[] getStackFrames() throws DebugException {
59 XDebugStackFrame[] newStackFrames = null;
62 if (fStepCount > fCurrentStepCount) {
63 //newStackFrames = ((XDebugTarget) getDebugTarget()).getStackFrames();
65 XDebugResponse dr = ((XDebugTarget) getDebugTarget()).getStackFrames();
66 newStackFrames = _getStackFrames(dr);
68 for (int i = 0; i < newStackFrames.length; i++) {
69 newStackFrames[i].getVariables();
72 if (fStackFrames != null) {
73 if (newStackFrames.length >= fStackFrames.length) {
74 int delta = newStackFrames.length - fStackFrames.length + 1;
76 for (int i = fStackFrames.length - 1; i >= 0; i--) {
77 if (fStackFrames[i].equals(newStackFrames[newStackFrames.length - delta])) {
79 //((XDebugStackFrame) newStackFrames[newStackFrames.length - delta]).evaluateChange((XDebugStackFrame) fStackFrames[i]);
80 } else if (fStackFrames[i].isSameStackFrame(newStackFrames[newStackFrames.length - delta])) {
82 //((XDebugStackFrame) newStackFrames[newStackFrames.length - delta]).evaluateChange((XDebugStackFrame) fStackFrames[i]);
93 fStackFrames = newStackFrames;
97 return new IStackFrame[0];
101 private XDebugStackFrame[] _getStackFrames(XDebugResponse lastResponse) {
102 //IStackFrame[] newStackFrames = null;
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));
116 frame.setType(PHPDebugUtils.getAttributeValue(stackNode,"type"));
117 String fileName=PHPDebugUtils.unescapeString(PHPDebugUtils.getAttributeValue(stackNode,"filename"));
118 String lineNo=PHPDebugUtils.getAttributeValue(stackNode,"lineno");
120 if (!"".equals(lineNo))
121 frame.setLineNumber(Integer.parseInt(lineNo));
123 frame.setWhere(PHPDebugUtils.getAttributeValue(stackNode,"where"));
126 frame.setFullName(new URL(fileName));
127 } catch (MalformedURLException e) {
131 frame.incrementStepCounter();
133 theFrames[i] = frame;
135 return /*newStackFrames*/theFrames;
139 * @see org.eclipse.debug.core.model.IThread#hasStackFrames()
141 public boolean hasStackFrames() throws DebugException {
142 return isSuspended();
146 * @see org.eclipse.debug.core.model.IThread#getPriority()
148 public int getPriority() throws DebugException {
153 * @see org.eclipse.debug.core.model.IThread#getTopStackFrame()
155 public IStackFrame getTopStackFrame() throws DebugException {
156 IStackFrame[] frames = getStackFrames();
157 if (frames.length > 0) {
165 * @see org.eclipse.debug.core.model.IThread#getName()
167 public String getName() throws DebugException {
172 * @see org.eclipse.debug.core.model.IThread#getBreakpoints()
174 public IBreakpoint[] getBreakpoints() {
175 if (fBreakpoints == null) {
176 return new IBreakpoint[0];
182 * Sets the breakpoints this thread is suspended at, or <code>null</code>
185 * @param breakpoints the breakpoints this thread is suspended at, or <code>null</code>
188 protected void setBreakpoints(IBreakpoint[] breakpoints) {
189 fBreakpoints = breakpoints;
193 * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
195 public boolean canResume() {
196 return isSuspended();
200 * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
202 public boolean canSuspend() {
203 return !isTerminated() && !isSuspended();
207 * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
209 public boolean isSuspended() {
210 return fTarget.isSuspended();
214 * @see org.eclipse.debug.core.model.ISuspendResume#resume()
216 public void resume() throws DebugException {
222 * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
224 public void suspend() throws DebugException {
229 * @see org.eclipse.debug.core.model.IStep#canStepInto()
231 public boolean canStepInto() {
232 return isSuspended();
236 * @see org.eclipse.debug.core.model.IStep#canStepOver()
238 public boolean canStepOver() {
239 return isSuspended();
243 * @see org.eclipse.debug.core.model.IStep#canStepReturn()
245 public boolean canStepReturn() {
246 if (fStackFrames != null) {
247 return (fStackFrames.length > 1);
254 * @see org.eclipse.debug.core.model.IStep#isStepping()
256 public boolean isStepping() {
261 * @see org.eclipse.debug.core.model.IStep#stepInto()
263 public void stepInto() throws DebugException {
269 * @see org.eclipse.debug.core.model.IStep#stepOver()
271 public void stepOver() throws DebugException {
277 * @see org.eclipse.debug.core.model.IStep#stepReturn()
279 public void stepReturn() throws DebugException {
285 * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
287 public boolean canTerminate() {
288 return !isTerminated();
292 * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
294 public boolean isTerminated() {
299 * @see org.eclipse.debug.core.model.ITerminate#terminate()
301 public void terminate() throws DebugException {
302 fTarget.getDebugConnection().stop();
306 public void terminated() throws DebugException {
311 * Sets whether this thread is stepping
313 * @param stepping whether stepping
315 protected void setStepping(boolean stepping) {
316 fStepping = stepping;
319 public void handleDebugEvents(DebugEvent[] events) {
320 DebugEvent de = events[0];
321 System.out.println(de.toString());
324 public void removeEventListeners() {
325 DebugPlugin.getDefault().removeDebugEventListener(this);