89ef3a255ddbdc1eeb2ac4cd990e118e0e41eea3
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / PHPDBGInterface.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         Vicente Fernando - www.alfersoft.com.ar - Initial implementation
10         Christian Perkonig - remote debug
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core;
13
14 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.util.Vector;
18 import java.util.Collections;
19
20 import net.sourceforge.phpdt.internal.debug.core.model.PHPDBGEvalString;
21 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
22 import net.sourceforge.phpdt.internal.debug.core.model.PHPVariable;
23 import net.sourceforge.phpdt.internal.debug.core.model.PHPValue;
24
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.debug.core.DebugException;
28
29 /**
30  * The interface object are created by the proxy
31  *
32  */
33 public class PHPDBGInterface {
34         public boolean                  sessionEnded = false;
35         public int                              sessType         = -1;
36         public int                              BPUnderHit       = 0;
37         public String                   sessID           = new String ();
38
39         private int[]                   LastBPRead   = new int[10];
40         private Vector                  DBGBPList    = new Vector ();
41         private Vector          DBGVarList   = new Vector ();
42         private PHPStackFrame[] DBGStackList;
43         private Vector                  DBGMods          = new Vector ();                       // The module names and their numbers
44         private Vector          stackListOld = new Vector ();
45         private BufferedReader  in;
46         private OutputStream    os;                                                                             // The stream which goes to DBG
47         private boolean                 shouldStop       = false;
48         private String                  evalRet          = new String ("");
49         private String                  serGlobals       = new String ("");
50         private int                     rawCounter       = 1000;                                        // An rawData frame ID counter
51         private PHPDBGProxy     proxy            = null;
52         private int                     lastCmd          = -1;
53         private int                     sid                      = 0;
54         private boolean                 stopOnError      = false;
55         private char[]                  lastCommand      = new char[4];
56
57         /**
58          * @param in    The input stream (communication from DBG).
59          * @param os    The output stream (communication to DBG).
60          * @param proxy The proxy to which this interface belongs.
61          */
62         public PHPDBGInterface (BufferedReader in, OutputStream os, PHPDBGProxy proxy) {
63                 DBGBPList.clear ();
64
65                 this.in         = in;
66                 this.os         = os;
67                 this.proxy      = proxy;
68         }
69
70         /**
71          *
72          * @param mod_name  The module (source file) to which we add the breakpoint.
73          * @param line      The line where the breakpoint is set.
74          * @param hitCount  The number of hit counts before suspend.
75          * @param condition The break condition
76          * @return          Breakpoint ID ???.
77          */
78         public int addBreakpoint (String mod_name, int line, int hitCount, String condition) throws IOException {
79                 return setBreakpoint (mod_name, condition, line, PHPDBGBase.BPS_ENABLED + PHPDBGBase.BPS_UNRESOLVED, 0, hitCount, 0, 0, 0);
80         }
81
82         /**
83          *
84          * @param mod_name The module (source file) to which we add the breakpoint.
85          * @param line     The line where the breakpoint is set.
86          * @param bpNo     The breakpoint ID ???.
87          */
88         public void removeBreakpoint (String mod_name, int line, int bpNo) throws IOException {
89                 setBreakpoint (mod_name, "", line, PHPDBGBase.BPS_DISABLED, 0, 0, 0, bpNo, 0);
90         }
91
92         /**
93          * Is this method used anywhere?
94          *
95          */
96         public void requestDBGVersion () throws IOException {
97                 PHPDBGPacket DBGPacket;                                     // A DBG message packet
98                 PHPDBGFrame  DBGFrame;                                      // A frame within a DBG packet
99
100                 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST);     // A request for DBG
101                 DBGFrame  = new PHPDBGFrame (PHPDBGBase.FRAME_VER);         // We want the version of DBG
102
103                 DBGPacket.addFrame (DBGFrame);                              // Add the 'what we want' to the DBG packet
104
105                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
106                         return;                                                 //  No
107                 }
108
109                 DBGPacket.sendPacket (os);                                                                      // Send the request to DBG
110         }
111
112         /**
113          * Called by the proxy
114          *
115          */
116         public void getSourceTree () throws IOException {
117                 PHPDBGPacket DBGPacket;                                     // A DBG message packet
118                 PHPDBGFrame  DBGFrame;                                      // A frame within a DBG packet
119
120                 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST);     // A request for DBG
121                 DBGFrame  = new PHPDBGFrame (PHPDBGBase.FRAME_SRC_TREE);        // We want a source tree from DBG
122
123                 DBGPacket.addFrame (DBGFrame);                              // Add the 'what we want' to the DBG packet
124
125                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
126                         return;                                                                                                 //  No
127                 }
128
129                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
130
131                 waitResponse (1000);                                        // Wait for the DBG response (1 second)
132                 flushAllPackets ();                                                                             // Read and process the response from DBG
133         }
134
135         /**
136          * Is this method called from anywhere?
137          *
138          * @param modName The modul (filename).
139          */
140         public void addDBGModName (String modName) throws IOException {
141                 PHPDBGPacket DBGPacket;                                     // A DBG message packet
142                 PHPDBGFrame  DBGFrame;                                      // A frame within a DBG packet
143
144                 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST);     // A request for DBG
145                 DBGFrame  = new PHPDBGFrame (PHPDBGBase.FRAME_RAWDATA);     // We want Module name from DBG
146
147                 rawCounter++;                                               // Increment the rawData ID counter
148                 DBGFrame.addInt (rawCounter);                                                           // FRAME_RAWDATA ID
149                 DBGFrame.addInt (modName.length () + 1);                                        // The length of rawdata string (incl. null char termination)
150                 DBGFrame.addString (modName);                                                           // The file name (module name)
151                 DBGFrame.addChar ('\0');                                                                        // Add the C-String null termination
152
153                 DBGPacket.addFrame (DBGFrame);
154
155                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
156                         return;                                                                                                 //  No
157                 }
158
159                 DBGPacket.sendPacket (os);
160         }
161
162         /**
163          * This method is called for adding or removing breakpoints.
164          *
165          * @param mod_name      The module name (file name).
166          * @param condition     Info about the condition when to break (not used at the moment).
167          * @param line          The breakpoints line.
168          * @param state         Info whether this breakpoint has to be dis- or enabled.
169          * @param istep         Always 0.
170          * @param hitcount      Always 0.
171          * @param skiphits      Always 0.
172          * @param bpno          The breakpoint ID.
173          * @param isunderhit    ???
174          * @return
175          */
176         private int setBreakpoint (String mod_name, String condition, int line, int state, int istemp, int hitcount, int skiphits, int bpno, int isunderhit) throws IOException {
177                 PHPDBGPacket    DBGPacket;
178                 PHPDBGFrame     DBGFrame1;
179                 PHPDBGFrame     DBGFrame2;
180                 PHPDBGFrame             DBGFrame3;
181                 int                     modNo;
182                 
183                 DBGPacket       = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST);
184                 DBGFrame1       = new PHPDBGFrame (PHPDBGBase.FRAME_BPS);
185                 DBGFrame2       = new PHPDBGFrame (PHPDBGBase.FRAME_RAWDATA);
186                 DBGFrame3       = new PHPDBGFrame (PHPDBGBase.FRAME_RAWDATA);
187
188                 modNo           = getModByName (mod_name);                                              // Get the module ID by name
189
190                 if (modNo >= 0) {                                           // Did we find a module ID for the module name?
191                         DBGFrame1.addInt (modNo);                                                               // Add the module ID to frame 1
192                 } else {
193                         DBGFrame1.addInt (0);                                                                   // mod number (0 use file name)
194                 }
195
196                 DBGFrame1.addInt (line);                                                                        // line number
197
198                 if (modNo >= 0) {                                           // Did we find a module ID for the module name?
199                         DBGFrame1.addInt (0);                                                                   // use mod number
200                 } else {
201                         rawCounter++;
202                         DBGFrame1.addInt (rawCounter);                                                  // ID of FRAME_RAWDATA to send file name
203                 }
204
205                 if (modNo < 0) {                                            // Did we find a module ID for the module name?
206                         DBGFrame2.addInt (rawCounter);                                      // FRAME_RAWDATA ID
207                         DBGFrame2.addInt (mod_name.length() + 1);                   // length of rawdata (+ null char)
208                         DBGFrame2.addString (mod_name);                                     // file name
209                         DBGFrame2.addChar ('\0');                                                   // null char
210
211                         DBGPacket.addFrame (DBGFrame2);                         // First add file name data
212                 }
213
214                 DBGFrame1.addInt (state);                                       // state BPS_*
215                 DBGFrame1.addInt (istemp);                                      // istemp
216                 DBGFrame1.addInt (0);                                               // hit count; this is not supported as one might think
217                 DBGFrame1.addInt (hitcount);                                // skip hits is what we think is hit count.
218
219                 if (!condition.equals ("")) {                                                           // Do we have a condition for breakpoint
220                         rawCounter++;                                                                                   // Set to new ID
221                         DBGFrame1.addInt (rawCounter);                          // ID of condition
222                         
223                         DBGFrame3.addInt (rawCounter);                                      // FRAME_RAWDATA ID
224                         DBGFrame3.addInt (condition.length() + 1);                  // length of rawdata (+ null char)
225                         DBGFrame3.addString (condition);                                        // The break condition
226                         DBGFrame3.addChar ('\0');                                                   // null char
227
228                         DBGPacket.addFrame (DBGFrame3);                         // First add break condition            
229                 }
230                 else {
231                         DBGFrame1.addInt (0);                                           // ID of condition is 0, because there is no condition
232                 }
233
234                 
235                 DBGFrame1.addInt (bpno);                                            // breakpoint number
236                 DBGFrame1.addInt (isunderhit);                                  // is under hit
237                 
238                 DBGPacket.addFrame (DBGFrame1);                                                         // Second add command data
239
240                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
241                         return 0;                                               //  No
242                 }
243
244                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
245
246                 clearLastBP ();
247
248                 waitResponse (1000);                                        // Wait for the DBG response (1 second)
249                 flushAllPackets ();                                         // Read and process the response from DBG
250
251                 return LastBPRead[8];                                                                           // Return what ???
252         }
253
254         /**
255          *
256          */
257         private void clearLastBP () {
258                 int i;
259
260                 for (i = 0; i < LastBPRead.length; i++) {
261                         LastBPRead[i] = 0;
262                 }
263         }
264
265         /**
266          *
267          */
268         private void copyToLastBP (int[] BPBody) {
269                 int i;
270
271                 for (i = 0; i < LastBPRead.length; i++) {
272                         LastBPRead[i] = BPBody[i];
273                 }
274         }
275
276         /**
277          *
278          */
279         public void continueExecution () throws IOException {
280                 PHPDBGPacket DBGPacket;
281
282                 BPUnderHit = 0;
283                 DBGPacket  = new PHPDBGPacket (PHPDBGBase.DBGA_CONTINUE);
284
285                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
286                         return;                                                 //  No
287                 }
288
289                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
290
291                 lastCommand = PHPDBGBase.DBGA_CONTINUE;                     // Store the info about the command we sent
292         }
293
294         /**
295          *
296          */
297         public void pauseExecution () throws IOException {
298                 PHPDBGPacket DBGPacket;
299
300                 DBGPacket = new PHPDBGPacket (PHPDBGBase.IntToChar4 (PHPDBGBase.DBGC_PAUSE));
301
302                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
303                          return;                                                //  No
304                 }
305
306                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
307         }
308
309         /**
310          *
311          */
312         private int getBPUnderHit () {
313                 int i;
314                 int BPUnder             = 0;
315                 int[] dbg_bpl_body      = new int[10];
316
317                 for (i = 0; i < DBGBPList.size (); i++) {                               // look for bp under hit
318                         dbg_bpl_body = (int[]) DBGBPList.get (i);
319
320                         if (dbg_bpl_body[9] == 1) {
321                                 BPUnder = dbg_bpl_body[8];
322                         }
323                 }
324
325                 return BPUnder;
326         }
327
328         public int getLastCmd()
329         {
330                 return lastCmd;
331         }
332
333         public int getSID()
334         {
335           return sid;
336         }
337
338         public void setLastCmd (int cmd)
339         {
340                 lastCmd = cmd;
341         }
342
343         /**
344          *
345          */
346         public void stepInto () throws IOException {
347                 PHPDBGPacket DBGPacket;
348
349                 BPUnderHit = 0;
350                 DBGPacket  = new PHPDBGPacket (PHPDBGBase.DBGA_STEPINTO);
351
352                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
353                         return;                                                 //  No
354                 }
355
356                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
357
358                 lastCommand = PHPDBGBase.DBGA_STEPINTO;                                         // Store the info about the command we sent
359         }
360
361         /**
362          *
363          */
364         public void stepOver () throws IOException {
365                 PHPDBGPacket DBGPacket;
366
367                 BPUnderHit = 0;
368                 DBGPacket  = new PHPDBGPacket (PHPDBGBase.DBGA_STEPOVER);
369
370                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
371                         return;                                                 //  No
372                 }
373
374                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
375
376                 lastCommand = PHPDBGBase.DBGA_STEPOVER;                     // Store the info about the command we sent
377         }
378
379         /**
380          *
381          */
382         public void stepOut () throws IOException {
383                 PHPDBGPacket DBGPacket;
384
385                 BPUnderHit = 0;
386                 DBGPacket  = new PHPDBGPacket (PHPDBGBase.DBGA_STEPOUT);
387
388                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
389                         return;                                                 //  No
390                 }
391
392                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
393
394                 lastCommand = PHPDBGBase.DBGA_STEPOUT;                      // Store the info about the command we sent
395         }
396
397         /**
398          *
399          */
400         public void stopExecution () throws IOException {
401                 PHPDBGPacket DBGPacket;
402
403                 BPUnderHit = 0;
404                 DBGPacket  = new PHPDBGPacket (PHPDBGBase.DBGA_STOP);
405
406                 if (proxy.getSocket ().isClosed ()) {                                           // Can we communiate with DBG?
407                         return;                                                 //  No
408                 }
409
410                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
411         }
412
413         /**
414          * This method is called by the proxy.
415          * It sends a request to DBG to get the current variables
416          * with their values. It waits for the response and processes
417          * the input from DBG.
418          *
419          * @param stack The stackframe for which we want the variables.
420          * @return      The array of variables
421          */
422         public Vector getVariables (PHPStackFrame stack) throws IOException, DebugException  {
423                 PHPDBGPacket            DBGPacket;
424                 PHPDBGFrame             DBGFrame1;
425                 PHPDBGEvalString        evalStr;
426
427                 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST);     //
428             DBGFrame1 = new PHPDBGFrame (PHPDBGBase.FRAME_EVAL);        //
429
430                 DBGFrame1.addInt (0);                                                                           // istr = raw data ID
431                 DBGFrame1.addInt (1);                                                                           // scope_id = -1 means current location, 0 never used, +1 first depth
432
433                 DBGPacket.addFrame (DBGFrame1);                             // Add command data
434
435                 if (proxy.getSocket ().isClosed ()) {                       // Do we have a socket for DBG communication?
436                         return null;                                                                                    //  No, then leave here
437                 }
438
439                 DBGPacket.sendPacket (os);                                  // Send the request to DBG
440
441                 waitResponse (1000);                                        // Wait for the DBG response (1 second)
442                 flushAllPackets ();                                         // Read and process the response from DBG
443
444                 evalStr         = new PHPDBGEvalString (stack, serGlobals); // Process serialized variables
445                 DBGVarList      = evalStr.getVariables ();
446
447                 if (DBGVarList.size () > 0) {                                                           // Did we get back variables?
448                         PHPVariable var = (PHPVariable) DBGVarList.get (0);             // Yes, then get the first PHPVariable
449                         PHPValue    val = (PHPValue) var.getValue ();           // Get the value
450
451                         if (var.getName ().equals ("")) {                                               // Is the root node an empty node (usually it is)
452                                 DBGVarList = val.getChildVariables ();              // Then remove the empty node.
453                                                                                                                                         // With removing the empty root node, it wouldn't be necessary to
454                                                                                                                                         // set the name to an empty string. So the code below is just for
455                                                                                                                                         // info or in case the users want to have the empty root node.
456
457                                                                                                                                         // The eclipse variable view cannot handle Variables which have an empty name
458                                                                                                                                         // when it comes to variable tree restore operation. Without a name, no restore!
459                                 var.setName (" ");                                                              // Give a name to the variable root node. Even if it is only a space :-)
460                         }                                                                                                               // TODO the best would be to remove the empty root node, but this would
461                                                                                                                                         // require a understanding and reworking of the PHPDBGEvalstring class.
462                 }
463
464                 return DBGVarList;                                                                                      // Return the variables as list
465         }
466
467         /**
468          *
469          * @param logString
470          */
471         public void log(String logString) throws IOException, DebugException  {
472                 PHPDBGPacket DBGPacket= new PHPDBGPacket(PHPDBGBase.DBGA_REQUEST);
473                 PHPDBGFrame DBGFrame1= new PHPDBGFrame(PHPDBGBase.FRAME_LOG);
474                 PHPDBGFrame DBGFrame2= new PHPDBGFrame(PHPDBGBase.FRAME_RAWDATA);
475
476                 rawCounter++;
477                 DBGFrame1.addInt(rawCounter);                           // ilog
478                 DBGFrame1.addInt(1);                                            // type
479                 DBGFrame1.addInt(0);                                            // mod_no
480                 DBGFrame1.addInt(0);                                            // line_no
481                 DBGFrame1.addInt(0);                                            // imod_name
482                 DBGFrame1.addInt(0);                                            // ext_info
483
484                 DBGFrame2.addInt(rawCounter);                           // FRAME_RAWDATA ID
485                 DBGFrame2.addInt(logString.length() + 1);       // length of rawdata (+ null char)
486                 DBGFrame2.addString(logString);                         // log string
487                 DBGFrame2.addChar('\0');                                        // null char
488
489                 // Add raw data first
490                 DBGPacket.addFrame(DBGFrame2);
491                 // Add command data
492                 DBGPacket.addFrame(DBGFrame1);
493
494                 if (proxy.getSocket ().isClosed ()) {                   // Do we have a socket for DBG communication?
495                         return;                                                                                         //  No, then leave here
496                 }
497
498                 DBGPacket.sendPacket(os);
499
500                 waitResponse(1000);
501                 flushAllPackets();
502         }
503
504         public PHPVariable[] evalBlock(PHPStackFrame stack, String evalString) throws IOException, DebugException  {
505                 PHPDBGPacket DBGPacket= new PHPDBGPacket(PHPDBGBase.DBGA_REQUEST);
506                 PHPDBGFrame DBGFrame1= new PHPDBGFrame(PHPDBGBase.FRAME_EVAL);
507                 PHPDBGFrame DBGFrame2= new PHPDBGFrame(PHPDBGBase.FRAME_RAWDATA);
508
509                 rawCounter++;
510                 DBGFrame1.addInt(rawCounter);                           // istr = raw data ID
511                 DBGFrame1.addInt(1);                                            // scope_id = -1 means current location, 0 never used, +1 first depth
512
513                 DBGFrame2.addInt(rawCounter);                           // FRAME_RAWDATA ID
514                 DBGFrame2.addInt(evalString.length() + 1);      // length of rawdata (+ null char)
515                 DBGFrame2.addString(evalString);                        // eval block
516                 DBGFrame2.addChar('\0');                                        // null char
517
518                 // Add raw data first
519                 DBGPacket.addFrame(DBGFrame2);
520                 // Add command data
521                 DBGPacket.addFrame(DBGFrame1);
522
523                 if (proxy.getSocket ().isClosed ()) {                       // Do we have a socket for DBG communication?
524                         return null;                                                                                    //  No, then leave here
525                 }
526                 DBGPacket.sendPacket(os);
527
528                 waitResponse(1000);
529                 flushAllPackets();
530
531                 PHPDBGEvalString evalStr=new PHPDBGEvalString(stack,evalRet);
532
533                 return evalStr.getVars();
534
535         }
536
537         /**
538          * Read and process everthing we got from DBG
539          */
540         public void flushAllPackets () throws IOException {
541                 while (readResponse() != 0);
542         }
543
544         /**
545          * Get the modules name by its number
546          *
547          * @param modNo The number (id) of the module
548          * @return      The name of the module
549          */
550         public String getModByNo (int modNo) {
551                 int             i;
552                 PHPDBGMod       dbg_mod;
553
554                 for (i = 0; i < DBGMods.size (); i++) {                         // For all the modules we have within the array
555                         dbg_mod = (PHPDBGMod) DBGMods.get (i);                      // Get the module
556
557                         if (dbg_mod.getNo () == modNo) {                            // Is the module from the array the module we want?
558                                 return dbg_mod.getName ();                              //  Yes, return the name of the module
559                         }
560                 }
561
562                 return "";                                                      // If nothing was found return emtpy string
563         }
564
565         /**
566          *
567          * @param  modName The name of the module for which we want the ID
568          * @return
569          * - The ID of the module
570          * - -1 if nothing was found
571          */
572         private int getModByName (String modName) {
573                 int             i;
574                 PHPDBGMod       dbg_mod;
575
576                 for (i = 0; i < DBGMods.size (); i++) {                         // For all the modules we have within the array
577                         dbg_mod = (PHPDBGMod) DBGMods.get (i);                      // Get the module
578
579                         if (dbg_mod.getName ().equalsIgnoreCase (modName)) {            // Is the module from the array the module we want?
580                                 return dbg_mod.getNo ();                                //  Yes, return the name of the module
581                         }
582                 }
583
584                 return -1;                                                      // If nothing was found return -1
585         }
586
587         /**
588          * Return the string for the given frame number
589          *
590          * @param framesInfo The buffer which is to read
591          * @param frameNo    The frame number
592          * @return
593          */
594         private String getRawFrameData (char[] framesInfo, int frameNo) {
595                 int   nextFrame = 0;                                                    // The current read position within the buffer
596                 int[] dbg_frame = new int[2];                                           // The two frame header numbers
597
598                 while (nextFrame < framesInfo.length) {                                 // As long we have something within the buffer
599                         dbg_frame[0] = PHPDBGBase.Char4ToInt (framesInfo, nextFrame);           // The frame type
600                         dbg_frame[1] = PHPDBGBase.Char4ToInt (framesInfo, nextFrame + 4);       // The frame size
601
602                         nextFrame   += 8;                                                   // The current read position
603
604                         if (dbg_frame[1] == 0) {                                            // If frame size is 0
605                                 return "";                                                      //  return an emtpy string
606                         }
607
608                         switch (dbg_frame[0]) {                                                     // Switch for the frame type
609                                 case PHPDBGBase.FRAME_RAWDATA:                                                  // The only frame type we are interrested in
610                                         if (frameNo == PHPDBGBase.Char4ToInt (framesInfo, nextFrame)) {         // Is it correct  number of the frame
611                                                 int toRead;                                                     //
612
613                                                 toRead = PHPDBGBase.Char4ToInt (framesInfo, nextFrame + 4);     // The size of the string
614
615                                                 if ((int) framesInfo[nextFrame + 8 + toRead - 1] == 0) {                                // Is there a string termination at the end?
616                                                         return String.copyValueOf (framesInfo, nextFrame + 8, toRead - 1);      // Then copy frame content to String without the \0 and return
617                                                 }
618                                                 
619                                                 return String.copyValueOf (framesInfo, nextFrame + 8, toRead);  // Copy frame content to String and return
620                                         }
621                                         break;
622                         }
623
624                         nextFrame += dbg_frame[1];                                                                      // Go for the next frame (add the length of the current one)
625                 }
626
627                 return "";                                                                                                                                              // We did not found any FRAM_RAWDATA, so return an emtpy strin
628         }
629
630         /**
631          *
632          * The stackList contains the currently read stackframes which were sent
633          * from DBG. The DBG interface holds a list of the active stack frames.
634          * This method replicates the 'static' stackframe list with the DBG stackframe list
635          * Replication is done in the following way:
636          * <ul>
637          * <li> It looks for new stackframes within the DBG stackframe list and
638          *      adds them to the 'static' list.
639          * <li> It looks for stackframes within the 'static' list, and removes them
640          *              from the 'static' list in case the do not appear within the DBG list.
641          * <li> It looks for stackframes which are already existent and replicates the
642          *              line number and the index number.
643          * <li> At the end the 'static' stackfram list has to be sorted by the stackframes
644          *              index numbers.
645          * </ul>
646          *
647          * Removes the unused stackframes from the list, or adds stackframes which
648          * are not yet in the list.
649          *
650          *
651          * @param stackList
652          */
653         private void updateStackFrameList (Vector stackList) {
654                 int                     i;
655                 int             n;
656                 PHPStackFrame   stackFrameNew;
657                 PHPStackFrame   stackFrameOld;
658                 PHPStackFrame[] newStackList;
659
660                 for (i = 0; i < stackList.size (); i++) {                                                               // For all stackList entries
661                         stackFrameNew = (PHPStackFrame) stackList.get(i);
662
663                         for (n = 0; n < stackListOld.size (); n++) {                                    // For all StackFrames in the StackFrame list
664                                 stackFrameOld = (PHPStackFrame) stackListOld.get (n);                   //
665
666                                 if (stackFrameNew.getDescription ().equals (stackFrameOld.getDescription ()) &&
667                                         stackFrameNew.getLineNumber() == stackFrameOld.getLineNumber()) {// Did we find the sent stackframe within the list of old stackframes?
668                                         stackFrameOld.setLineNumber (stackFrameNew.getLineNumber ());
669                                         stackFrameOld.setIndex (stackFrameNew.getIndex ());
670
671                                         break;                                                                  //  Yes, then break;
672                                 }
673                         }
674
675                         if (n == stackListOld.size ()) {                                    // Did not find the new stackframe within the list
676                                  stackListOld.add (stackFrameNew);                              //  then add the new stackframe
677                         }
678                 }
679
680                 // And now for removing unused stackframes from list
681
682                 for (n = 0; n < stackListOld.size (); n++) {                                            // For all StackFrames in the StackFrame list
683                         stackFrameOld = (PHPStackFrame) stackListOld.get (n);                           //
684
685                         for (i = 0; i < stackList.size (); i++) {                                                       // For all stackList entries
686                                 stackFrameNew = (PHPStackFrame) stackList.get (i);
687
688                                 if (stackFrameNew.getDescription ().equals (stackFrameOld.getDescription ()) &&
689                                         stackFrameNew.getLineNumber() == stackFrameOld.getLineNumber()) {// Did we find the sent stackframe within the list of old stackframes?
690                                         break;                                                                  //  Yes, then break;
691                                 }
692                         }
693
694                         if (i == stackList.size ()) {                                           // Did not find the old stackframe within the list of new ones
695                                  stackListOld.remove (n);                                       //  then remove the old stackframe from list
696                                  n -= 1;                                                        // Adjust the stack list index
697                         }
698                 }
699
700                 Collections.sort (stackListOld);                                                                                // Sort the 'static' stackframe list by the stackframe index numbers.
701                                                                                                                                                                 //
702                 newStackList = new PHPStackFrame[stackListOld.size ()];
703                 newStackList = (PHPStackFrame[]) stackListOld.toArray (newStackList);
704                 DBGStackList = newStackList;
705         }
706
707     /**
708      * Read the response from DBG and process the frame
709      *
710          * @return
711          * - The received command
712          * - or 0 if something was wrong
713      */
714         public int readResponse () throws IOException {
715         int     bytesToRead            = 0;                         // The number of byte to read for the current DBG block
716         int     nextFrame              = 0;                         // The current read position within entirePack
717         int     i                      = 0;
718         int     cmdReceived            = 0;
719         int     stackIndex             = 0;
720         boolean errorStack             = false;
721         char[]  dbg_header_struct_read = new char[16];              // The buffer for the first 16 bytes of a block
722         int[]   dbg_header_struct      = new int[4];                // The first four numbers (long) of a block
723         int[]   dbg_bpl_tmp            = new int[10];
724         int[]   dbg_frame              = new int[2];
725         int[]   dbg_eval_tmp           = new int[3];
726         int[]   dbg_src_tree_tmp       = new int[4];                //
727         int[]   dbg_error_tmp          = new int[2];
728         Vector  rawList                = new Vector();
729         Vector  stackList              = new Vector();              // Intermediate stacklist which is build up in FRAME_STACK frame
730
731         rawList.clear ();
732         stackList.clear ();
733
734                 // Read from input
735         while (readInput (dbg_header_struct_read, 16) != 0) {                               // Read 16 byte from input stream
736                         dbg_header_struct[0] = PHPDBGBase.Char4ToInt (dbg_header_struct_read, 0);               // Debug sync header
737             dbg_header_struct[1] = PHPDBGBase.Char4ToInt (dbg_header_struct_read, 4);           // Command
738             dbg_header_struct[2] = PHPDBGBase.Char4ToInt (dbg_header_struct_read, 8);       //
739             dbg_header_struct[3] = PHPDBGBase.Char4ToInt (dbg_header_struct_read, 12);      // Bytes within this block
740
741             if (dbg_header_struct[0] != 0x5953) {                                           // Check DBG sync bytes
742                 return 0;                                                                   // Wrong header
743             }
744
745             cmdReceived = dbg_header_struct[1];                                             // Get the command
746             setLastCmd (cmdReceived);                                                                                                           // Store the info about the current command
747             bytesToRead = dbg_header_struct[3];                                                                                         // Get the number of bytes to read for this block
748
749                         //System.out.println("Response Received: " + cmdReceived);
750                         char[] entirePack = new char[bytesToRead];                                      // Store the block data into buffer 'entirePack'
751
752             if (bytesToRead > 0) {                                                          // If there is something within the frame
753                 if (readInput (entirePack, bytesToRead) < bytesToRead) {                    // Read the frame into the buffer
754                     return 0;                                                               // We did not read enough bytes, error
755                 }
756                         }
757                         
758                         nextFrame = 0;                                                                  // Start with the first frame
759
760                         while (nextFrame < bytesToRead) {                                               // As long as we have something within this block
761                                 dbg_frame[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame);                           // The name of the frame
762                                 dbg_frame[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4);               // The size of the frame
763                                 nextFrame   += 8;                                                           // The next read position
764
765                                 if (dbg_frame[1] == 0) {                                                    // Something within the frame?
766                                         return 0;                                                               //  Nothing to read, error
767                                 }
768
769                                 switch (dbg_frame[0]) {
770                                         case PHPDBGBase.FRAME_STACK:
771                                                 int[] dbg_stack_new = new int[4];                                       //
772
773                                                 dbg_stack_new[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0);   // Source line number
774                                                 dbg_stack_new[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4);   // Module number
775                                                 dbg_stack_new[2] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 8);   // Scope id
776                                                 dbg_stack_new[3] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 12);  // ID of description string
777
778                                                 if ((dbg_stack_new[1] != 0) && !errorStack) {
779                                                         PHPStackFrame newStack;
780
781                                                         stackIndex++;
782                                                         newStack = new PHPStackFrame (null,                                 // The thread
783                                                                                       getModByNo (dbg_stack_new[1]),        // The name of the module (file)
784                                                                                       dbg_stack_new[0],                     // The source line number
785                                                                                                                   stackIndex,
786                                                                                                                   getRawFrameData (entirePack,          // Get the string from this packet
787                                                                                                                                    dbg_stack_new[3]),   // The frame ID for which we want the string
788                                                                                                                   dbg_stack_new[1]);                                    // The module number
789                                                         stackList.add (newStack);
790                                                 }
791
792                                                 errorStack = false;
793                                                 break;
794
795                                         case PHPDBGBase.FRAME_SOURCE:                                                   // Nothing to be done here
796                                                 break;                                                                      // TODO: what's with that frame? Something interesting
797
798                                         case PHPDBGBase.FRAME_SRC_TREE:                                                 //
799                                                 dbg_src_tree_tmp[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0);        // The parent module number
800                                                 dbg_src_tree_tmp[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4);        // The parent line number (not used)
801                                                 dbg_src_tree_tmp[2] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 8);        // The module number
802                                                 dbg_src_tree_tmp[3] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 12);       // The filename number
803
804                                                 if (getModByNo (dbg_src_tree_tmp[2]).equals ("")) {
805                                                         String fileName;
806
807                                                         fileName = new String (getRawFrameData (entirePack, dbg_src_tree_tmp[3]));      // Get the filename
808
809                                                         if (dbg_src_tree_tmp[2] != 0) {                                             // If there is a module number
810                                                                 PHPDBGMod modNew;
811
812                                                                 modNew = new PHPDBGMod (dbg_src_tree_tmp[2], fileName);                 // Create a module object
813
814                                                                 DBGMods.add (modNew);                                                   // And store it to array
815                                                         }
816                                                 }
817                                                 break;
818
819                                         case PHPDBGBase.FRAME_RAWDATA:                                                      // Nothing to be done here
820                                                 break;                                                                          //  FRAME_RAWDATA are processed within getRawFrameData
821
822                                         case PHPDBGBase.FRAME_ERROR:                                                                                                            // An error frame
823                                                 errorStack       = true;                                                                                                                // Yes, we have an error stack
824                                                 dbg_error_tmp[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0);                   // Error type
825                                                 dbg_error_tmp[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4);                   // Error message ID
826
827                                                 String error = "\n";                                                            //
828
829                                                 switch (dbg_error_tmp[0]) {                                                                                                             // Switch on error type
830                                                         case PHPDBGBase.E_ERROR:                        error += "[Error]";                     break;
831                                                         case PHPDBGBase.E_WARNING:                      error += "[Warning]";                   break;
832                                                         case PHPDBGBase.E_PARSE:                        error += "[Parse Error]";               break;
833                                                         case PHPDBGBase.E_NOTICE:                       error += "[Notice]";                    break;
834                                                         case PHPDBGBase.E_CORE_ERROR:           error += "[Core Error]";                break;
835                                                         case PHPDBGBase.E_CORE_WARNING:         error += "[Core Warning]";              break;
836                                                         case PHPDBGBase.E_COMPILE_ERROR:        error += "[Compile Error]";             break;
837                                                         case PHPDBGBase.E_COMPILE_WARNING:      error += "[Compile Warning]";   break;
838                                                         case PHPDBGBase.E_USER_ERROR:           error += "[User Error]";                break;
839                                                         case PHPDBGBase.E_USER_WARNING:         error += "[User Warning]";              break;
840                                                         case PHPDBGBase.E_USER_NOTICE:          error += "[User Notice]";               break;
841                                                         default:                                                        error += "[Unexpected Error]";  break;
842                                                 }
843
844                                                 error += ": ";
845                                                 error += new String (getRawFrameData (entirePack, dbg_error_tmp[1]));                   // Add the error string for this error message ID
846                                                 error += "\n";                                                                  // Append a CR
847
848                                                 PHPDebugCorePlugin.log (new DebugException (new Status (IStatus.WARNING,
849                                                                                                         PHPDebugCorePlugin.PLUGIN_ID,
850                                                                                                                                                                 IStatus.OK,
851                                                                                                                                                                 error, null)));
852
853                                                 // To print errors on the console, I must execute a code in the
854                                                 // php context, that write the stderr... I didn't found a better way
855                                                 // TODO: Find a better way????
856
857 //                                              String codeExec= "";
858 //                                              codeExec= "fwrite(fopen('php://stderr', 'w'),\\\"" + error + "\\\");";
859 //                                              try {
860 //                                                      evalBlock("eval(\"" + codeExec + "\");");
861 //                                              } catch (DebugException e) {
862 //                                                      PHPDebugCorePlugin.log(e);
863 //                                              }
864 //
865                                                 if (!stopOnError) {                                                             // Is always false (Did not see where this is set to true!?)
866                                                         if (lastCommand.equals (PHPDBGBase.DBGA_CONTINUE)) {                        // If last command for PHP was a 'continue',
867                                                                 continueExecution ();                                                   //  send continue again
868                                                         } else if (lastCommand.equals (PHPDBGBase.DBGA_STEPINTO)) {                 // If last command for PHP was a 'step into',
869                                                                 stepInto ();                                                            //  send 'step into' again
870                                                         } else if (lastCommand.equals (PHPDBGBase.DBGA_STEPOUT)) {                  // If last command for PHP was a 'step out',
871                                                                 stepOut ();                                                             //  send 'step out' again
872                                                         } else if (lastCommand.equals (PHPDBGBase.DBGA_STEPOVER)) {                 // If last command for PHP was a 'step over',
873                                                                 stepOver ();                                                            //  send 'step over' again
874                                                         }
875                                                 }
876                                                 break;
877
878                                         case PHPDBGBase.FRAME_EVAL:
879                                                 String evalString;
880
881                                                 evalString      = new String ("");
882                                                 dbg_eval_tmp[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0);                    // istr
883                                                 dbg_eval_tmp[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4);                    // iresult
884                                                 dbg_eval_tmp[2] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 8);                    // ierror
885
886                                                 evalRet                 = getRawFrameData (entirePack, dbg_eval_tmp[1]);                //
887                                                 evalString              = getRawFrameData (entirePack, dbg_eval_tmp[0]);                //
888                                                 serGlobals              = evalRet;                                                      //
889                                                 break;
890
891                                         case PHPDBGBase.FRAME_BPS:                                                          //
892                                                 break;                                                                          //
893
894                                         case PHPDBGBase.FRAME_BPL:
895                                                 int[] dbg_bpl_new;
896
897                                                 dbg_bpl_new        = new int[10];
898                                                 dbg_bpl_new[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0);
899                                                 dbg_bpl_new[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4);
900                                                 dbg_bpl_new[2] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 8);
901                                                 dbg_bpl_new[3] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 12);
902                                                 dbg_bpl_new[4] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 16);
903                                                 dbg_bpl_new[5] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 20);
904                                                 dbg_bpl_new[6] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 24);
905                                                 dbg_bpl_new[7] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 28);
906                                                 dbg_bpl_new[8] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 32);
907                                                 dbg_bpl_new[9] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 36);
908
909                                                 // look if breakpoint already exists in vector
910                                                 for (i = 0; i < DBGBPList.size (); i++) {
911                                                         dbg_bpl_tmp = (int[]) DBGBPList.get (i);
912
913                                                         if (dbg_bpl_tmp[8] == dbg_bpl_new[8]) {
914                                                                 DBGBPList.remove (i);
915
916                                                                 break;
917                                                         }
918                                                 }
919
920                                                 // add breakpoint to vector
921                                                 DBGBPList.add (dbg_bpl_new);
922                                                 copyToLastBP (dbg_bpl_new);
923
924                                                 // mod no returned?
925                                                 if (getModByNo (dbg_bpl_new[0]).equals ("")) {
926                                                         String fileName;
927
928                                                         fileName = new String (getRawFrameData (entirePack, dbg_bpl_new[2]));
929
930                                                         if (dbg_bpl_new[0] != 0) {
931                                                                 PHPDBGMod modNew;
932
933                                                                 modNew = new PHPDBGMod (dbg_bpl_new[0], fileName);
934
935                                                                 DBGMods.add (modNew);
936                                                         }
937                                                 }
938                                                 break;
939
940                                         case PHPDBGBase.FRAME_VER:
941                                                 break;
942
943                                         case PHPDBGBase.FRAME_SID:
944                                                 sid = PHPDBGBase.Char4ToInt(entirePack, nextFrame + 0);
945                                                 break;
946
947                                         case PHPDBGBase.FRAME_SRCLINESINFO:
948                                                 break;
949
950                                         case PHPDBGBase.FRAME_SRCCTXINFO:
951                                                 break;
952
953                                         case PHPDBGBase.FRAME_LOG:
954                                                 break;
955
956                                         case PHPDBGBase.FRAME_PROF:
957                                                 break;
958
959                                         case PHPDBGBase.FRAME_PROF_C:
960                                                 break;
961
962                                         case PHPDBGBase.FRAME_SET_OPT:
963                                                 break;
964                                 }
965
966                                 nextFrame += dbg_frame[1];                                                      // go to next frame
967                         }
968
969                         // Now process command
970                         switch(cmdReceived) {
971                                 case PHPDBGBase.DBGC_REPLY:
972                                         break;
973
974                                 case PHPDBGBase.DBGC_STARTUP:
975                                         break;
976
977                                 case PHPDBGBase.DBGC_END:
978                                         sessionEnded = true;
979                                         this.proxy.setTerminated();
980                                         break;
981
982                                 case PHPDBGBase.DBGC_BREAKPOINT:
983                                         BPUnderHit   = getBPUnderHit ();
984                                         updateStackFrameList (stackList);
985                                         break;
986
987                                 case PHPDBGBase.DBGC_STEPINTO_DONE:
988                                 case PHPDBGBase.DBGC_STEPOVER_DONE:
989                                 case PHPDBGBase.DBGC_STEPOUT_DONE:
990                                 case PHPDBGBase.DBGC_EMBEDDED_BREAK:
991                                 case PHPDBGBase.DBGC_PAUSE:
992                                         BPUnderHit   = 1;
993                                         updateStackFrameList (stackList);
994                                         break;
995
996                                 case PHPDBGBase.DBGC_ERROR:
997                                         stackList.clear ();
998                                         updateStackFrameList (stackList);
999                                         break;
1000
1001                                 case PHPDBGBase.DBGC_LOG:
1002                                         break;
1003
1004                                 case PHPDBGBase.DBGC_SID:
1005                                         break;
1006                         }
1007                 }
1008
1009                 return cmdReceived;                                         // Return the command we received with this block
1010         }
1011
1012     /**
1013      *
1014      */
1015
1016         public PHPStackFrame[] getStackList() {
1017                 return DBGStackList;
1018         }
1019
1020         /**
1021          * Reads from input buffer (response sent from DBG) the given number of chars
1022          * into frame buffer.
1023          *
1024          * @param buffer  The frame buffer where to store the read data from DBG.
1025          * @param bytes   The number of bytes (chars) which are to read from input stream.
1026          * @return        The number of bytes actually read.
1027          */
1028         private int readInput (char[] buffer, int bytes) throws IOException {
1029                 int bytesRead = 0;                                                                                      // Reset the bytes read counter
1030
1031                 for (int i = 0; i < bytes; i++) {                           // For the number of bytes we should read
1032                         if (in.ready ()) {                                                                              // If input stream is ready for reading
1033                                 buffer[i] = (char) (in.read () & 0x00FF);           // Read a char and store only the least significant 8-bits
1034                                 bytesRead++;                                        // Increment the bytes read counter
1035                         }
1036                         else {                                                  // Input stream is not ready
1037                                 break;                                              // Break the loop
1038                         }
1039                 }
1040
1041                 return bytesRead;                                                                                       // Return the number of bytes actually read
1042         }
1043
1044         /**
1045          * PHPProxy could stop the waiting for a response with this method.
1046          *
1047          */
1048         public void setShouldStop () {
1049                 this.shouldStop = true;
1050         }
1051
1052         /**
1053          * @param milliseconds The maximum time in milliseconds we wait for something
1054          *                     to be send from DBG.
1055          * @return             - true if something was received from DBG
1056          *                                         - false if nothing was send from DBG within the given time
1057          *
1058          */
1059         public boolean waitResponse (long milliseconds) throws IOException {
1060                 long timeout;
1061
1062                 timeout = System.currentTimeMillis () + milliseconds;           // Calculate the system time till we wait.
1063
1064                 while (System.currentTimeMillis () < timeout) {             // Is waiting time running out?
1065                         if (in.ready () || shouldStop) {                        //  No, so did we get something or should we stop now
1066                                 break;                                              //   Yes, break the waiting
1067                         }
1068                 }
1069
1070                 return in.ready ();                                         // true if we got something from DBG
1071         }
1072 }