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; //
31 private String name; //
32 private int id; // The port number through which we communicate to DBG
35 private boolean isSuspended = false;
36 private boolean isTerminated = false;
37 private boolean isStepping = false;
39 boolean isSuspended () {
43 boolean isTerminated () {
47 boolean isStepping () {
51 void setSuspended (boolean suspended) {
52 if (isTerminated ()) {
53 throw new IllegalStateException();
56 if (suspended && isStepping ()) {
57 throw new IllegalStateException ();
60 isSuspended = suspended;
63 void setStepping (boolean stepping) {
64 if (stepping && !isSuspended ()) {
65 throw new IllegalStateException ();
68 if (isTerminated ()) {
69 throw new IllegalStateException ();
72 isStepping = stepping;
75 void setTerminated(boolean terminated) {
76 isTerminated = terminated;
80 private final State state = new State ();
84 * @param id The port number through which we communicate to DBG
86 public PHPThread (PHPDebugTarget target, int id) {
96 public IStackFrame[] getStackFrames () throws DebugException {
98 return ((PHPDebugTarget)getDebugTarget()).getStackFrames();
100 return new IStackFrame[0];
104 // public int getStackFramesSize () {
105 // return frames.length;
108 public boolean hasStackFrames () {
109 if (frames == null) {
113 return frames.length > 0;
116 public int getPriority () throws DebugException {
120 public IStackFrame getTopStackFrame () throws DebugException {
121 if (frames == null || frames.length == 0) {
124 return (IStackFrame) frames[0];
127 public IBreakpoint[] getBreakpoints() {
128 return new IBreakpoint[0];
131 public String getModelIdentifier() {
132 return this.getDebugTarget().getModelIdentifier();
135 public IDebugTarget getDebugTarget() {
139 // public void setDebugTarget(PHPDebugTarget target) {
140 // this.target = target;
143 public ILaunch getLaunch() {
144 return this.getDebugTarget().getLaunch();
147 public synchronized boolean canResume() {
148 return isSuspended();
151 public synchronized boolean canSuspend() {
152 return !isSuspended();
155 public synchronized boolean isSuspended() {
156 return state.isSuspended;
161 * Is called from PHPstackframe whenever a stepInto, stepOver or stepReturn is
166 protected void prepareForResume (int de) {
169 state.setSuspended (false); // We will leave the suspended state
170 this.frames = null; // Reset the stackframes
171 ev = new DebugEvent (this, DebugEvent.RESUME, de); // Create an event resume by stepping
173 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev }); // Fire the event
179 public synchronized void resume () throws DebugException {
180 if (!isSuspended ()) { // Is the thread in suspended state?
181 return; // No, leave here
184 this.prepareForResume (DebugEvent.STEP_OVER); // Use a STEP_OVER here because a 0 leads to a collapsing variable tree in UI
186 ((PHPDebugTarget) this.getDebugTarget ()).getPHPDBGProxy ().resume ();
190 * public void doSuspend(SuspensionPoint suspensionPoint) { //
191 * this.getPHPDebuggerProxy().readFrames(this);
192 * this.createName(suspensionPoint) ; this.suspend() ; }
195 public synchronized void suspend () throws DebugException {
198 if (isSuspended ()) { // Is the thread in suspend state?
199 return; // Yes, leave here
202 state.setSuspended (true); // Set thread to suspended state
203 state.setStepping (false); // Reset thread from stepping state
205 getDebugTarget ().suspend (); //
207 ev = new DebugEvent (this, DebugEvent.SUSPEND, DebugEvent.BREAKPOINT);
209 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
215 public boolean canStepInto () {
216 return isSuspended () && // Is the thread in suspended mode (stopped)
217 isStepping () && // and ???
218 this.hasStackFrames (); // and does this thread have stack frames?
224 public boolean canStepOver () {
225 return isSuspended () && // Is the thread in suspended mode (stopped)
226 isStepping () && // and ???
227 this.hasStackFrames (); // and does this thread have stack frames?
233 public boolean canStepReturn () {
234 return isSuspended () && // Is the thread in suspended mode (stopped)
235 isStepping () && // and ???
236 this.hasStackFrames (); // and does this thread have stack frames?
242 public boolean isStepping () {
243 return state.isStepping ();
249 public void stepInto () throws DebugException {
251 state.setStepping (true); // Store the info about what we do
253 catch (IllegalStateException x) {
254 throw new DebugException (PHPeclipsePlugin.error (x));
259 frames[0].stepInto ();
265 public void stepOver () throws DebugException {
266 state.setStepping (true);
270 frames[0].stepOver ();
276 public void stepReturn () throws DebugException {
282 public boolean canTerminate () {
283 return !isTerminated ();
289 public boolean isTerminated () {
290 return state.isTerminated ();
296 public synchronized void terminate () throws DebugException {
297 if (isTerminated ()) {
301 state.setTerminated (true);
303 getDebugTarget ().terminate ();
304 fireTerminateEvent ();
312 public Object getAdapter (Class arg0) {
313 if (IWorkbenchAdapter.class.equals (arg0)) {
314 return new IWorkbenchAdapter() {
315 public Object[] getChildren(Object o) {
317 return getStackFrames ();
318 } catch (DebugException x) {
319 PHPeclipsePlugin.log ("Unable to get stack frames.", x);
322 return new Object[0];
325 public ImageDescriptor getImageDescriptor(Object object) {
329 public String getLabel(Object o) {
330 throw new UnsupportedOperationException();
333 public Object getParent(Object o) {
334 return getDebugTarget();
338 return super.getAdapter(arg0);
344 public void setStackFrames(PHPStackFrame[] frames) {
345 this.frames = frames;
351 public String getName () {
356 if (isSuspended ()) {
357 name = name + " (suspended)";
363 public void setName (String name) {
368 * protected void createName(SuspensionPoint suspensionPoint) { this.name =
369 * "PHP Thread - " + this.getId() ; if (suspensionPoint != null) { this.name += " (" +
370 * suspensionPoint + ")" ; } }
377 public void setId(int id) {