Patches from Robert Kraske (robekras):
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPStackFrame.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Vicente Fernando - www.alfersoft.com.ar
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.model;
13
14 import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
15
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.IDebugTarget;
21 import org.eclipse.debug.core.model.IRegisterGroup;
22 import org.eclipse.debug.core.model.IStackFrame;
23 import org.eclipse.debug.core.model.IThread;
24 import org.eclipse.debug.core.model.IVariable;
25
26 public class PHPStackFrame extends PHPDebugElement implements IStackFrame {
27
28         private PHPThread               thread;                         // The thread to which this stackframe belongs
29         private String                  file;               // The file name???
30         private int                     lineNumber;         //
31         private int                     index;              //
32         private int                     modno;              //
33         private PHPVariable[]   variables;          // The array of variables
34         private String                  description;        //
35
36         /**
37          *
38          * @param thread
39          * @param file
40          * @param line
41          * @param index
42          * @param desc
43          * @param modno
44          */
45         public PHPStackFrame (PHPThread thread, String file, int line, int index, String desc, int modno) {
46                 super (null);
47
48                 this.lineNumber  = line;
49                 this.index           = index;
50                 this.file            = file;
51                 this.thread      = thread;
52                 this.description = desc;
53                 this.modno               = modno;
54         }
55
56         /**
57          *
58          * @param thread
59          * @param file
60          * @param line
61          * @param index
62          */
63         public PHPStackFrame (PHPThread thread, String file, int line, int index) {
64                 super (null);
65
66                 this.lineNumber = line;
67                 this.index              = index;
68                 this.file               = file;
69                 this.thread     = thread;
70         }
71
72         /**
73          *
74          */
75         public IThread getThread () {
76                 return thread;
77         }
78
79         /**
80          * @param thread
81          */
82         public void setThread (PHPThread thread) {
83                 this.thread = thread;
84         }
85
86         /**
87          *
88          * This function returns the array of PHPVariables for this stackframe
89          * The PHPVariables should not change (newly build up) between two steps
90          * (or breaks).
91          * A PHPVariable with the same name but with different object ID is
92          * handled as a new variable.
93          *
94          * TODO Remove the intermediate storage array
95          *
96          * @return The array of PHPVariables for this stackframe.
97          */
98         public IVariable[] getVariables() throws DebugException {
99                 PHPVariable[] variablesNew;                                 // The intermediate storage of the variable array we get from DBG proxy
100
101                 variablesNew = this.getPHPDBGProxy ().readVariables (this); // Get the variable array from DBG proxy
102                 variables    = variablesNew;                                // Store the array the stackframes member variable
103
104                 return variables;                                           // Give the array back to user interface
105         }
106
107         /**
108          * TODO Is this really used (who calls this)
109          * I think this method could be removed
110          *
111          * @param s The variables name we are looking for.
112          * @return
113          */
114         public IVariable findVariable (String s) throws DebugException {
115                 String name;
116                 int    i;
117
118                 if (this.hasVariables ()) {                                                                     // Does this stackframe have variables?
119                         name = "$" + s;                                         // Prefix the variable name with $
120
121                         for (i = 0; i < variables.length; i++) {                // For all variables
122                                 if ((variables[i].getName ()).equals (name)) {
123                                         return variables[i];
124                                 }
125                         }
126                 }
127
128                 return null;
129         }
130
131         /**
132          *
133          */
134         public boolean hasVariables () throws DebugException {
135                 if (variables == null) {                                                                        // Do we have a variables array?
136                         return false;                                                                                   // No
137                 }
138
139                 return variables.length > 0;                                // Is there something within the array?
140         }
141
142         public int getLineNumber() {
143                 return lineNumber;
144         }
145
146         public void setLineNumber(int line) {
147                 lineNumber = line;
148         }
149
150         public int getCharStart() throws DebugException {
151                 // not supported
152                 return -1;
153         }
154
155         public int getCharEnd() throws DebugException {
156                 // not supported
157                 return -1;
158         }
159
160         public String getName() {
161                 if(!this.getDescription().equals(""))
162                         return this.getDescription() + " [line: " + this.getLineNumber() + "]";
163                 else
164                         return this.getFileName() + " [line: " + this.getLineNumber() + "]";
165         }
166
167         public String getFileName() {
168                 return file;
169         }
170
171         public void setDescription(String desc) {
172                 this.description= desc;
173         }
174
175         public String getDescription() {
176                 return this.description;
177         }
178
179         public IRegisterGroup[] getRegisterGroups() throws DebugException {
180                 return null;
181         }
182
183         public boolean hasRegisterGroups() throws DebugException {
184                 return false;
185         }
186
187         public String getModelIdentifier() {
188                 return this.getThread().getModelIdentifier();
189         }
190
191         public IDebugTarget getDebugTarget() {
192                 return this.getThread().getDebugTarget();
193         }
194
195         public ILaunch getLaunch() {
196                 return this.getDebugTarget().getLaunch();
197         }
198
199         public boolean canStepInto() {
200                 return canResume();
201         }
202
203         public boolean canStepOver() {
204                 return canResume();
205         }
206
207         public boolean canStepReturn() {
208                 return canResume();
209         }
210
211         public boolean isStepping() {
212                 return false;
213         }
214
215         /**
216          *
217          */
218         public void stepInto () throws DebugException {
219                 DebugEvent      ev;
220
221         thread.prepareForResume (DebugEvent.STEP_INTO);             // Don't know why, but this is necessary
222                 this.getPHPDBGProxy ().readStepIntoEnd (PHPStackFrame.this);
223
224                 ev = new DebugEvent (this.getThread (), DebugEvent.RESUME, DebugEvent.STEP_INTO);
225                 DebugPlugin.getDefault().fireDebugEventSet (new DebugEvent[] { ev });
226         }
227
228         /**
229          *
230          */
231         public void stepOver () throws DebugException {
232                 DebugEvent      ev;
233
234         thread.prepareForResume (DebugEvent.STEP_OVER);
235                 this.getPHPDBGProxy ().readStepOverEnd (PHPStackFrame.this) ;
236
237                 ev = new DebugEvent (this.getThread (), DebugEvent.RESUME, DebugEvent.STEP_OVER);
238                 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
239         }
240
241         /**
242          *
243          */
244         public void stepReturn () throws DebugException {
245                 DebugEvent      ev;
246
247         thread.prepareForResume (DebugEvent.STEP_RETURN);
248                 this.getPHPDBGProxy ().readStepReturnEnd (PHPStackFrame.this) ;
249
250                 ev = new DebugEvent (this.getThread (), DebugEvent.RESUME, DebugEvent.STEP_RETURN);
251                 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
252         }
253
254
255         public boolean canResume() {
256                 return this.getThread().canResume();
257         }
258
259         public boolean canSuspend() {
260                 return this.getThread().canSuspend();
261         }
262
263         public boolean isSuspended() {
264                 return this.getThread().isSuspended();
265         }
266
267         public void resume() throws DebugException {
268                 this.getThread().resume();
269         }
270
271         public void suspend() throws DebugException {
272         }
273
274         public boolean canTerminate() {
275                 return this.getThread().canTerminate();
276         }
277
278         public boolean isTerminated() {
279                 return this.getThread().isTerminated();
280         }
281
282         public void terminate() throws DebugException {
283                 getPHPDBGProxy().stop();
284         }
285
286         public int getIndex() {
287                 return index;
288         }
289
290         public PHPDBGProxy getPHPDBGProxy() {
291                 PHPDebugTarget DebugTarget;
292
293                 DebugTarget = (PHPDebugTarget) thread.getDebugTarget ();
294
295                 return DebugTarget.getPHPDBGProxy ();
296         }
297
298         public void setFile(String file) {
299                 this.file = file;
300         }
301
302         public int getModNo() {
303                 return modno;
304         }
305 }