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 {
100 public int getStackFramesSize () {
101 return frames.length;
104 public boolean hasStackFrames () {
105 if (frames == null) {
109 return frames.length > 0;
112 public int getPriority () throws DebugException {
116 public IStackFrame getTopStackFrame () throws DebugException {
117 if (frames == null || frames.length == 0) {
120 return (IStackFrame) frames[0];
123 public IBreakpoint[] getBreakpoints() {
124 return new IBreakpoint[0];
127 public String getModelIdentifier() {
128 return this.getDebugTarget().getModelIdentifier();
131 public IDebugTarget getDebugTarget() {
135 public void setDebugTarget(PHPDebugTarget target) {
136 this.target = target;
139 public ILaunch getLaunch() {
140 return this.getDebugTarget().getLaunch();
143 public synchronized boolean canResume() {
144 return isSuspended();
147 public synchronized boolean canSuspend() {
148 return !isSuspended();
151 public synchronized boolean isSuspended() {
152 return state.isSuspended;
157 * Is called from PHPstackframe whenever a stepInto, stepOver or stepReturn is
162 protected void prepareForResume (int de) {
165 state.setSuspended (false); // We will leave the suspended state
166 this.frames = null; // Reset the stackframes
167 ev = new DebugEvent (this, DebugEvent.RESUME, de); // Create an event resume by stepping
169 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev }); // Fire the event
175 public synchronized void resume () throws DebugException {
176 if (!isSuspended ()) { // Is the thread in suspended state?
177 return; // No, leave here
180 this.prepareForResume (DebugEvent.STEP_OVER); // Use a STEP_OVER here because a 0 leads to a collapsing variable tree in UI
182 ((PHPDebugTarget) this.getDebugTarget ()).getPHPDBGProxy ().resume ();
186 * public void doSuspend(SuspensionPoint suspensionPoint) { //
187 * this.getPHPDebuggerProxy().readFrames(this);
188 * this.createName(suspensionPoint) ; this.suspend() ; }
191 public synchronized void suspend () throws DebugException {
194 if (isSuspended ()) { // Is the thread in suspend state?
195 return; // Yes, leave here
198 state.setSuspended (true); // Set thread to suspended state
199 state.setStepping (false); // Reset thread from stepping state
201 getDebugTarget ().suspend (); //
203 ev = new DebugEvent (this, DebugEvent.SUSPEND, DebugEvent.BREAKPOINT);
205 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
211 public boolean canStepInto () {
212 return isSuspended () && // Is the thread in suspended mode (stopped)
213 isStepping () && // and ???
214 this.hasStackFrames (); // and does this thread have stack frames?
220 public boolean canStepOver () {
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 canStepReturn () {
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 isStepping () {
239 return state.isStepping ();
245 public void stepInto () throws DebugException {
247 state.setStepping (true); // Store the info about what we do
249 catch (IllegalStateException x) {
250 throw new DebugException (PHPeclipsePlugin.error (x));
255 frames[0].stepInto ();
261 public void stepOver () throws DebugException {
262 state.setStepping (true);
266 frames[0].stepOver ();
272 public void stepReturn () throws DebugException {
278 public boolean canTerminate () {
279 return !isTerminated ();
285 public boolean isTerminated () {
286 return state.isTerminated ();
292 public synchronized void terminate () throws DebugException {
293 if (isTerminated ()) {
297 state.setTerminated (true);
299 getDebugTarget ().terminate ();
307 public Object getAdapter (Class arg0) {
308 if (IWorkbenchAdapter.class.equals (arg0)) {
309 return new IWorkbenchAdapter() {
310 public Object[] getChildren(Object o) {
311 Object[] children = null;
314 IStackFrame[] frames = getStackFrames();
316 if (null != frames) {
317 children = new Object[frames.length];
318 for (int i = 0; i < frames.length; ++i) {
319 children[i] = frames[i];
322 } catch (DebugException x) {
323 PHPeclipsePlugin.log ("Unable to get stack frames.", x);
329 public ImageDescriptor getImageDescriptor(Object object) {
333 public String getLabel(Object o) {
334 throw new UnsupportedOperationException();
337 public Object getParent(Object o) {
338 return getDebugTarget();
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) {