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
9 IBM Corporation - Initial implementation
10 Vicente Fernando - www.alfersoft.com.ar
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.model;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import org.eclipse.debug.core.DebugEvent;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.ILaunch;
20 import org.eclipse.debug.core.model.IBreakpoint;
21 import org.eclipse.debug.core.model.IDebugTarget;
22 import org.eclipse.debug.core.model.IStackFrame;
23 import org.eclipse.debug.core.model.IThread;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.ui.model.IWorkbenchAdapter;
27 public class PHPThread extends PHPDebugElement implements IThread {
29 private PHPStackFrame[] frames; // The stackframes which belongs to this thread
30 private PHPDebugTarget target; // This is doppel moppel, fTarget exists in PHPDebugElement
31 private String name; //
32 private int id; // The port number through which we communicate to DBG
33 private boolean fDebug = true;
36 private boolean isSuspended = false;
37 private boolean isTerminated = false;
38 private boolean isStepping = false;
40 boolean isSuspended () {
44 boolean isTerminated () {
48 boolean isStepping () {
52 void setSuspended (boolean suspended) {
53 if (isTerminated ()) {
54 throw new IllegalStateException();
57 if (suspended && isStepping ()) {
58 throw new IllegalStateException ();
61 isSuspended = suspended;
64 void setStepping (boolean stepping) {
65 if (stepping && !isSuspended ()) {
66 throw new IllegalStateException ();
69 if (isTerminated ()) {
70 throw new IllegalStateException ();
73 isStepping = stepping;
76 void setTerminated(boolean terminated) {
77 isTerminated = terminated;
81 private final State state = new State ();
85 * @param id The port number through which we communicate to DBG
87 public PHPThread (PHPDebugTarget target, int id) {
97 public IStackFrame[] getStackFrames () throws DebugException {
100 System.out.println ("PHPThread getStackFrames");
103 return ((PHPDebugTarget)getDebugTarget()).getStackFrames();
105 return new IStackFrame[0];
109 // public int getStackFramesSize () {
110 // return frames.length;
113 public boolean hasStackFrames () {
114 if (frames == null) {
118 return frames.length > 0;
121 public int getPriority () throws DebugException {
125 public IStackFrame getTopStackFrame () throws DebugException {
126 if (frames == null || frames.length == 0) {
129 return (IStackFrame) frames[0];
132 public IBreakpoint[] getBreakpoints() {
133 return new IBreakpoint[0];
136 public String getModelIdentifier() {
137 return this.getDebugTarget().getModelIdentifier();
140 public IDebugTarget getDebugTarget() {
144 // public void setDebugTarget(PHPDebugTarget target) {
145 // this.target = target;
148 public ILaunch getLaunch() {
149 return this.getDebugTarget().getLaunch();
152 public synchronized boolean canResume() {
153 return isSuspended();
156 public synchronized boolean canSuspend() {
157 return !isSuspended();
160 public synchronized boolean isSuspended() {
161 return state.isSuspended;
166 * Is called from PHPstackframe whenever a stepInto, stepOver or stepReturn is
171 protected void prepareForResume (int de) {
174 state.setSuspended (false); // We will leave the suspended state
175 frames = null; // Reset the stackframes
176 ev = new DebugEvent (this, DebugEvent.RESUME, de); // Create an event resume by stepping
178 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev }); // Fire the event
184 public synchronized void resume () throws DebugException {
185 if (!isSuspended ()) { // Is the thread in suspended state?
186 return; // No, leave here
189 this.prepareForResume (DebugEvent.STEP_OVER); // Use a STEP_OVER here because a 0 leads to a collapsing variable tree in UI
191 ((PHPDebugTarget) this.getDebugTarget ()).getPHPDBGProxy ().resume ();
195 * public void doSuspend(SuspensionPoint suspensionPoint) { //
196 * this.getPHPDebuggerProxy().readFrames(this);
197 * this.createName(suspensionPoint) ; this.suspend() ; }
200 public synchronized void suspend () throws DebugException {
203 if (isSuspended ()) { // Is the thread in suspend state?
204 return; // Yes, leave here
207 state.setSuspended (true); // Set thread to suspended state
208 state.setStepping (false); // Reset thread from stepping state
210 getDebugTarget ().suspend (); //
212 ev = new DebugEvent (this, DebugEvent.SUSPEND, DebugEvent.BREAKPOINT);
214 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
220 public boolean canStepInto () {
221 return isSuspended () && // Is the thread in suspended mode (stopped)
222 isStepping () && // and ???
223 this.hasStackFrames (); // and does this thread have stack frames?
229 public boolean canStepOver () {
230 return isSuspended () && // Is the thread in suspended mode (stopped)
231 isStepping () && // and ???
232 this.hasStackFrames (); // and does this thread have stack frames?
238 public boolean canStepReturn () {
239 return isSuspended () && // Is the thread in suspended mode (stopped)
240 isStepping () && // and ???
241 this.hasStackFrames (); // and does this thread have stack frames?
247 public boolean isStepping () {
248 return state.isStepping ();
254 public void stepInto () throws DebugException {
256 state.setStepping (true); // Store the info about what we do
258 catch (IllegalStateException x) {
259 throw new DebugException (PHPeclipsePlugin.error (x));
264 frames[0].stepInto ();
270 public void stepOver () throws DebugException {
271 state.setStepping (true);
275 frames[0].stepOver ();
281 public void stepReturn () throws DebugException {
287 public boolean canTerminate () {
288 return !isTerminated ();
294 public boolean isTerminated () {
295 return state.isTerminated ();
301 public synchronized void terminate () throws DebugException {
302 if (isTerminated ()) {
306 state.setTerminated (true);
308 getDebugTarget ().terminate ();
309 fireTerminateEvent ();
317 public Object getAdapter (Class arg0) {
318 if (IWorkbenchAdapter.class.equals (arg0)) {
319 return new IWorkbenchAdapter() {
320 public Object[] getChildren(Object o) {
322 return getStackFrames ();
323 } catch (DebugException x) {
324 PHPeclipsePlugin.log ("Unable to get stack frames.", x);
327 return new Object[0];
330 public ImageDescriptor getImageDescriptor(Object object) {
334 public String getLabel(Object o) {
335 throw new UnsupportedOperationException();
338 public Object getParent(Object o) {
339 return getDebugTarget();
343 return super.getAdapter(arg0);
349 public void setStackFrames(PHPStackFrame[] frames) {
350 this.frames = frames;
356 public String getName () {
361 if (isSuspended ()) {
362 name = name + " (suspended)";
368 public void setName (String name) {
373 * protected void createName(SuspensionPoint suspensionPoint) { this.name =
374 * "PHP Thread - " + this.getId() ; if (suspensionPoint != null) { this.name += " (" +
375 * suspensionPoint + ")" ; } }
382 public void setId(int id) {