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 Vicente Fernando - www.alfersoft.com.ar - Initial implementation
10 Christian Perkonig - remote debug
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core;
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;
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;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.debug.core.DebugException;
29 import java.io.RandomAccessFile;
32 * The interface object are created by the proxy
35 public class PHPDBGInterface {
37 public boolean sessionEnded = false;
38 public int sessType = -1;
39 public int BPUnderHit = 0;
40 public String sessID = new String ();
42 private int[] LastBPRead = new int[10];
43 private Vector DBGBPList = new Vector ();
44 private Vector DBGVarList = new Vector ();
45 private PHPStackFrame[] DBGStackList;
46 private Vector DBGMods = new Vector (); // The module names and their numbers
47 private Vector stackListOld = new Vector ();
48 private BufferedReader in;
49 private OutputStream os; // The stream which goes to DBG
50 private boolean shouldStop = false;
51 private String evalRet = new String ("");
52 private String serGlobals = new String ("");
53 private int rawCounter = 1000; // An rawData frame ID counter
54 private PHPDBGProxy proxy = null;
55 private int lastCmd = -1;
57 private boolean stopOnError = false;
58 private char[] lastCommand = new char[4];
61 * @param in The input stream (communication from DBG).
62 * @param os The output stream (communication to DBG).
63 * @param proxy The proxy to which this interface belongs.
65 public PHPDBGInterface (BufferedReader in, OutputStream os, PHPDBGProxy proxy) {
75 * @param mod_name The module (source file) to which we add the breakpoint.
76 * @param line The line where the breakpoint is set.
77 * @return Breakpoint ID ???.
79 public int addBreakpoint (String mod_name, int line) throws IOException {
80 return setBreakpoint (mod_name, "", line, PHPDBGBase.BPS_ENABLED + PHPDBGBase.BPS_UNRESOLVED, 0, 0, 0, 0, 0);
85 * @param mod_name The module (source file) to which we add the breakpoint.
86 * @param line The line where the breakpoint is set.
87 * @param bpNo The breakpoint ID ???.
89 public void removeBreakpoint (String mod_name, int line, int bpNo) throws IOException {
90 setBreakpoint (mod_name, "", line, PHPDBGBase.BPS_DISABLED, 0, 0, 0, bpNo, 0);
94 * Is this method used anywhere?
97 public void requestDBGVersion () throws IOException {
98 PHPDBGPacket DBGPacket; // A DBG message packet
99 PHPDBGFrame DBGFrame; // A frame within a DBG packet
101 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST); // A request for DBG
102 DBGFrame = new PHPDBGFrame (PHPDBGBase.FRAME_VER); // We want the version of DBG
104 DBGPacket.addFrame (DBGFrame); // Add the 'what we want' to the DBG packet
106 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
110 DBGPacket.sendPacket (os); // Send the request to DBG
114 * Called by the proxy
117 public void getSourceTree () throws IOException {
118 PHPDBGPacket DBGPacket; // A DBG message packet
119 PHPDBGFrame DBGFrame; // A frame within a DBG packet
121 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST); // A request for DBG
122 DBGFrame = new PHPDBGFrame (PHPDBGBase.FRAME_SRC_TREE); // We want a source tree from DBG
124 DBGPacket.addFrame (DBGFrame); // Add the 'what we want' to the DBG packet
126 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
130 DBGPacket.sendPacket (os); // Send the request to DBG
132 waitResponse (1000); // Wait for the DBG response (1 second)
133 flushAllPackets (); // Read and process the response from DBG
137 * Is this method called from anywhere?
139 * @param modName The modul (filename).
141 public void addDBGModName (String modName) throws IOException {
142 PHPDBGPacket DBGPacket; // A DBG message packet
143 PHPDBGFrame DBGFrame; // A frame within a DBG packet
145 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST); // A request for DBG
146 DBGFrame = new PHPDBGFrame (PHPDBGBase.FRAME_RAWDATA); // We want Module name from DBG
148 rawCounter++; // Increment the rawData ID counter
149 DBGFrame.addInt (rawCounter); // FRAME_RAWDATA ID
150 DBGFrame.addInt (modName.length () + 1); // The length of rawdata string (incl. null char termination)
151 DBGFrame.addString (modName); // The file name (module name)
152 DBGFrame.addChar ('\0'); // Add the C-String null termination
154 DBGPacket.addFrame (DBGFrame);
156 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
160 DBGPacket.sendPacket (os);
164 * This method is called for adding or removing breakpoints.
166 * @param mod_name The module name (file name).
167 * @param condition Info about the condition when to break (not used at the moment).
168 * @param line The breakpoints line.
169 * @param state Info whether this breakpoint has to be dis- or enabled.
170 * @param istep Always 0.
171 * @param hitcount Always 0.
172 * @param skiphits Always 0.
173 * @param bpno The breakpoint ID.
174 * @param isunderhit ???
177 private int setBreakpoint (String mod_name, String condition, int line, int state, int istemp, int hitcount, int skiphits, int bpno, int isunderhit) throws IOException {
178 PHPDBGPacket DBGPacket;
179 PHPDBGFrame DBGFrame1;
180 PHPDBGFrame DBGFrame2;
183 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST);
184 DBGFrame1 = new PHPDBGFrame (PHPDBGBase.FRAME_BPS);
185 DBGFrame2 = new PHPDBGFrame (PHPDBGBase.FRAME_RAWDATA);
187 modNo = getModByName (mod_name); // Get the module ID by name
189 if (modNo >= 0) { // Did we find a module ID for the module name?
190 DBGFrame1.addInt (modNo); // Add the module ID to frame 1
192 DBGFrame1.addInt (0); // mod number (0 use file name)
195 DBGFrame1.addInt (line); // line number
197 if (modNo >= 0) { // Did we find a module ID for the module name?
198 DBGFrame1.addInt (0); // use mod number
201 DBGFrame1.addInt (rawCounter); // ID of FRAME_RAWDATA to send file name
204 DBGFrame1.addInt (state); // state BPS_*
205 DBGFrame1.addInt (istemp); // istep
206 DBGFrame1.addInt (hitcount); // hit count
207 DBGFrame1.addInt (skiphits); // skip hits
208 DBGFrame1.addInt (0); // ID of condition
209 DBGFrame1.addInt (bpno); // breakpoint number
210 DBGFrame1.addInt (isunderhit); // is under hit
212 if (modNo < 0) { // Did we find a module ID for the module name?
213 DBGFrame2.addInt (rawCounter); // FRAME_RAWDATA ID
214 DBGFrame2.addInt (mod_name.length() + 1); // length of rawdata (+ null char)
215 DBGFrame2.addString (mod_name); // file name
216 DBGFrame2.addChar ('\0'); // null char
218 DBGPacket.addFrame (DBGFrame2); // First add file name data
221 DBGPacket.addFrame (DBGFrame1); // Second add command data
223 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
227 DBGPacket.sendPacket (os); // Send the request to DBG
231 waitResponse (1000); // Wait for the DBG response (1 second)
232 flushAllPackets (); // Read and process the response from DBG
234 return LastBPRead[8]; // Return what ???
240 private void clearLastBP () {
243 for (i = 0; i < LastBPRead.length; i++) {
251 private void copyToLastBP (int[] BPBody) {
254 for (i = 0; i < LastBPRead.length; i++) {
255 LastBPRead[i] = BPBody[i];
262 public void continueExecution () throws IOException {
263 PHPDBGPacket DBGPacket;
266 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_CONTINUE);
268 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
272 DBGPacket.sendPacket (os); // Send the request to DBG
274 lastCommand = PHPDBGBase.DBGA_CONTINUE; // Store the info about the command we sent
280 public void pauseExecution () throws IOException {
281 PHPDBGPacket DBGPacket;
283 DBGPacket = new PHPDBGPacket (PHPDBGBase.IntToChar4 (PHPDBGBase.DBGC_PAUSE));
285 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
289 DBGPacket.sendPacket (os); // Send the request to DBG
295 private int getBPUnderHit () {
298 int[] dbg_bpl_body = new int[10];
300 for (i = 0; i < DBGBPList.size (); i++) { // look for bp under hit
301 dbg_bpl_body = (int[]) DBGBPList.get (i);
303 if (dbg_bpl_body[9] == 1) {
304 BPUnder = dbg_bpl_body[8];
311 public int getLastCmd()
321 public void setLastCmd (int cmd)
329 public void stepInto () throws IOException {
330 PHPDBGPacket DBGPacket;
333 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_STEPINTO);
335 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
339 DBGPacket.sendPacket (os); // Send the request to DBG
341 lastCommand = PHPDBGBase.DBGA_STEPINTO; // Store the info about the command we sent
347 public void stepOver () throws IOException {
348 PHPDBGPacket DBGPacket;
351 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_STEPOVER);
353 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
357 DBGPacket.sendPacket (os); // Send the request to DBG
359 lastCommand = PHPDBGBase.DBGA_STEPOVER; // Store the info about the command we sent
365 public void stepOut () throws IOException {
366 PHPDBGPacket DBGPacket;
369 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_STEPOUT);
371 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
375 DBGPacket.sendPacket (os); // Send the request to DBG
377 lastCommand = PHPDBGBase.DBGA_STEPOUT; // Store the info about the command we sent
383 public void stopExecution () throws IOException {
384 PHPDBGPacket DBGPacket;
387 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_STOP);
389 if (proxy.getSocket ().isClosed ()) { // Can we communiate with DBG?
393 DBGPacket.sendPacket (os); // Send the request to DBG
397 * This method updates the 'static' variables list.
398 * It does a replication between the 'static' list (the variable list which
399 * is a member of this DBG interface object) and the DBG variable list
400 * (the list of variables which is received from PHP via DBG with the current suspend)
401 * Replication is done in the following way:
403 * <li> It looks for new variables within the DBG variables list and
404 * adds them to the 'static' list.
405 * <li> It looks for changed variables copies the current value to the variable within
406 * the 'static list' and mark these variables as 'hasChanged' (which uses the UI
407 * for showing the variable with a different color).
408 * <li> It looks for variables within the 'static' list, and removes them
409 * from the 'static' list in case the do not appear within the DBG list.
412 * @param varListOld The 'static' list of variables which are to be updated.
413 * @param varListNew The new list of (current) variables from DBG.
415 private void updateVariableList (Vector varListOld, Vector varListNew)
417 PHPVariable varOld; // The variable from the 'static' list
418 PHPVariable varNew; // The variable from the DBG list
419 PHPValue valOld; // The value of the current variable from 'static' list
420 PHPValue valNew; // The value of the current variable from DBG list
421 int n; // Index for the DBG list
422 int o; // Index for the static list
424 // Add the variables (and childs) to the static list if they are new
425 // and update the values of variables which are already existend within
426 // the 'static' list.
428 for (n = 0; n < varListNew.size (); n++) { // For every variable in 'DBG list'
429 varNew = (PHPVariable) varListNew.get (n); // Get the DBG variable
431 for (o = 0; o < varListOld.size (); o++) { // For every variable in static list
432 varOld = (PHPVariable) varListOld.get (o); // Get the static variable
434 if (varNew.getName ().equals (varOld.getName ())) { // Did we found the variable within the 'static' list?
435 valOld = (PHPValue) varOld.getValue (); // Get the value from 'static'
436 valNew = (PHPValue) varNew.getValue (); // Get the value from DBG
439 if (valOld.hasVariables () || // If the 'static' value has child variables
440 valNew.hasVariables ()) { // or if the DBG value has child variables
441 updateVariableList (valOld.getChildVariables (), // Update the variable list for the child variables
442 valNew.getChildVariables ());
444 else if (!valOld.getValueString ().equals (valNew.getValueString ())) { // Has the value changed?
445 valOld.setValueString (valNew.getValueString ()); // Yes, set the 'static' value (variable) to the new value
446 varOld.setValueChanged (true); // and set the 'has changed' flag, so that the variable view
447 // could show the user the changed status with a different
451 varOld.setValueChanged (false); // Reset the 'has changed' flag
454 catch (DebugException e) { // That's, because of the hasVariables method
457 break; // Found the variable,
461 if (o == varListOld.size ()) { // Did we found the variable within the static list?
462 varListOld.add (varNew); // No, then add the DBG variable to the static list
466 // Look for the variables we can remove from the 'static' list
468 for (o = 0; o < varListOld.size (); o++) { // For every variable in 'static' list
469 varOld = (PHPVariable) varListOld.get (o); // Get the static variable
471 for (n = 0; n < varListNew.size (); n++) { // For all variables in 'DBG' list
472 varNew = (PHPVariable) varListNew.get (n); // Get the variable from the 'DBG' list
474 if (varNew.getName ().equals (varOld.getName ())) { // Did we found the 'static' list variable within the 'DBG' list?
475 break; // Yes we found the variable, then leave the loop
479 if (n == varListNew.size ()) { // Did not find the 'static' list variable within the 'DBG' list?
480 varListOld.remove (o); // then remove the 'static' list variable from list
481 o -= 1; // Adjust the 'static' list index
487 * This method is called by the proxy.
488 * It sends a request to DBG to get the current variables
489 * with their values. It waits for the response and processes
490 * the input from DBG.
492 * @param stack The stackframe for which we want the variables.
493 * @return The array of variables
495 public Vector getVariables (PHPStackFrame stack) throws IOException, DebugException {
496 PHPDBGPacket DBGPacket;
497 PHPDBGFrame DBGFrame1;
498 PHPDBGEvalString evalStr;
500 DBGPacket = new PHPDBGPacket (PHPDBGBase.DBGA_REQUEST); //
501 DBGFrame1 = new PHPDBGFrame (PHPDBGBase.FRAME_EVAL); //
503 DBGFrame1.addInt (0); // istr = raw data ID
504 DBGFrame1.addInt (1); // scope_id = -1 means current location, 0 never used, +1 first depth
506 DBGPacket.addFrame (DBGFrame1); // Add command data
508 if (proxy.getSocket ().isClosed ()) { // Do we have a socket for DBG communication?
509 return null; // No, then leave here
512 DBGPacket.sendPacket (os); // Send the request to DBG
514 waitResponse (1000); // Wait for the DBG response (1 second)
515 flushAllPackets (); // Read and process the response from DBG
517 evalStr = new PHPDBGEvalString (stack, serGlobals); // Process serialized variables
518 updateVariableList (DBGVarList, evalStr.getVariables ()); // Replicate the 'static' variable list and the via DBG received variable list
520 return DBGVarList; // Convert the list to an array and return the array
527 public void log(String logString) throws IOException, DebugException {
528 PHPDBGPacket DBGPacket= new PHPDBGPacket(PHPDBGBase.DBGA_REQUEST);
529 PHPDBGFrame DBGFrame1= new PHPDBGFrame(PHPDBGBase.FRAME_LOG);
530 PHPDBGFrame DBGFrame2= new PHPDBGFrame(PHPDBGBase.FRAME_RAWDATA);
533 DBGFrame1.addInt(rawCounter); // ilog
534 DBGFrame1.addInt(1); // type
535 DBGFrame1.addInt(0); // mod_no
536 DBGFrame1.addInt(0); // line_no
537 DBGFrame1.addInt(0); // imod_name
538 DBGFrame1.addInt(0); // ext_info
540 DBGFrame2.addInt(rawCounter); // FRAME_RAWDATA ID
541 DBGFrame2.addInt(logString.length() + 1); // length of rawdata (+ null char)
542 DBGFrame2.addString(logString); // log string
543 DBGFrame2.addChar('\0'); // null char
545 // Add raw data first
546 DBGPacket.addFrame(DBGFrame2);
548 DBGPacket.addFrame(DBGFrame1);
550 if(proxy.getSocket().isClosed()) return;
551 DBGPacket.sendPacket(os);
557 public PHPVariable[] evalBlock(PHPStackFrame stack, String evalString) throws IOException, DebugException {
558 PHPDBGPacket DBGPacket= new PHPDBGPacket(PHPDBGBase.DBGA_REQUEST);
559 PHPDBGFrame DBGFrame1= new PHPDBGFrame(PHPDBGBase.FRAME_EVAL);
560 PHPDBGFrame DBGFrame2= new PHPDBGFrame(PHPDBGBase.FRAME_RAWDATA);
563 DBGFrame1.addInt(rawCounter); // istr = raw data ID
564 DBGFrame1.addInt(1); // scope_id = -1 means current location, 0 never used, +1 first depth
566 DBGFrame2.addInt(rawCounter); // FRAME_RAWDATA ID
567 DBGFrame2.addInt(evalString.length() + 1); // length of rawdata (+ null char)
568 DBGFrame2.addString(evalString); // eval block
569 DBGFrame2.addChar('\0'); // null char
571 // Add raw data first
572 DBGPacket.addFrame(DBGFrame2);
574 DBGPacket.addFrame(DBGFrame1);
576 if(proxy.getSocket().isClosed()) return null;
577 DBGPacket.sendPacket(os);
582 PHPDBGEvalString evalStr=new PHPDBGEvalString(stack,evalRet);
584 return evalStr.getVars();
589 * Read and process everthing we got from DBG
591 public void flushAllPackets () throws IOException {
592 while (readResponse() != 0);
596 * Get the modules name by its number
598 * @param modNo The number (id) of the module
599 * @return The name of the module
601 public String getModByNo (int modNo) {
605 for (i = 0; i < DBGMods.size (); i++) { // For all the modules we have within the array
606 dbg_mod = (PHPDBGMod) DBGMods.get (i); // Get the module
608 if (dbg_mod.getNo () == modNo) { // Is the module from the array the module we want?
609 return dbg_mod.getName (); // Yes, return the name of the module
613 return ""; // If nothing was found return emtpy string
618 * @param modName The name of the module for which we want the ID
620 * - The ID of the module
621 * - -1 if nothing was found
623 private int getModByName (String modName) {
627 for (i = 0; i < DBGMods.size (); i++) { // For all the modules we have within the array
628 dbg_mod = (PHPDBGMod) DBGMods.get (i); // Get the module
630 if (dbg_mod.getName ().equalsIgnoreCase (modName)) { // Is the module from the array the module we want?
631 return dbg_mod.getNo (); // Yes, return the name of the module
635 return -1; // If nothing was found return -1
639 * Return the string for the given frame number
641 * @param framesInfo The buffer which is to read
642 * @param frameNo The frame number
645 private String getRawFrameData (char[] framesInfo, int frameNo) {
646 int nextFrame = 0; // The current read position within the buffer
647 int[] dbg_frame = new int[2]; // The two frame header numbers
649 while (nextFrame < framesInfo.length) { // As long we have something within the buffer
650 dbg_frame[0] = PHPDBGBase.Char4ToInt (framesInfo, nextFrame); // The frame type
651 dbg_frame[1] = PHPDBGBase.Char4ToInt (framesInfo, nextFrame + 4); // The frame size
653 nextFrame += 8; // The current read position
655 if (dbg_frame[1] == 0) { // If frame size is 0
656 return ""; // return an emtpy string
659 switch (dbg_frame[0]) { // Switch for the frame type
660 case PHPDBGBase.FRAME_RAWDATA: // The only frame type we are interrested in
661 if (frameNo == PHPDBGBase.Char4ToInt (framesInfo, nextFrame)) { // Is it correct number of the frame
664 toRead = PHPDBGBase.Char4ToInt (framesInfo, nextFrame + 4); // The size of the string
666 return String.copyValueOf (framesInfo, nextFrame + 8, toRead); // Copy frame content to String and return
671 nextFrame += dbg_frame[1]; // Go for the next frame (add the length of the current one)
674 return ""; // We did not found any FRAM_RAWDATA, so return an emtpy strin
679 * The stackList contains the currently read stackframes which were sent
680 * from DBG. The DBG interface holds a list of the active stack frames.
681 * This method replicates the 'static' stackframe list with the DBG stackframe list
682 * Replication is done in the following way:
684 * <li> It looks for new stackframes within the DBG stackframe list and
685 * adds them to the 'static' list.
686 * <li> It looks for stackframes within the 'static' list, and removes them
687 * from the 'static' list in case the do not appear within the DBG list.
688 * <li> It looks for stackframes which are already existent and replicates the
689 * line number and the index number.
690 * <li> At the end the 'static' stackfram list has to be sorted by the stackframes
694 * Removes the unused stackframes from the list, or adds stackframes which
695 * are not yet in the list.
700 private void updateStackFrameList (Vector stackList) {
703 PHPStackFrame stackFrameNew;
704 PHPStackFrame stackFrameOld;
705 PHPStackFrame[] newStackList;
707 for (i = 0; i < stackList.size (); i++) { // For all stackList entries
708 stackFrameNew = (PHPStackFrame) stackList.get(i);
710 for (n = 0; n < stackListOld.size (); n++) { // For all StackFrames in the StackFrame list
711 stackFrameOld = (PHPStackFrame) stackListOld.get (n); // ---
713 if (stackFrameNew.getModNo () == stackFrameOld.getModNo ()) { // Did we find the sent stackframe within the list of old stackframes?
714 stackFrameOld.setLineNumber (stackFrameNew.getLineNumber ());
715 stackFrameOld.setIndex (stackFrameNew.getIndex ());
717 break; // Yes, then break;
721 if (n == stackListOld.size ()) { // Did not find the new stackframe within the list
722 stackListOld.add (stackFrameNew); // then add the new stackframe
726 // And now for removing unused stackframes from list
728 for (n = 0; n < stackListOld.size (); n++) { // For all StackFrames in the StackFrame list
729 stackFrameOld = (PHPStackFrame) stackListOld.get (n); // ---
731 for (i = 0; i < stackList.size (); i++) { // For all stackList entries
732 stackFrameNew = (PHPStackFrame) stackList.get (i);
734 if (stackFrameNew.getModNo () == stackFrameOld.getModNo ()) { // Did we find the sent stackframe within the list of old stackframes?
735 break; // Yes, then break;
739 if (i == stackList.size ()) { // Did not find the old stackframe within the list of new ones
740 stackListOld.remove (n); // then remove the old stackframe from list
741 n -= 1; // Adjust the stack list index
745 Collections.sort (stackListOld); // Sort the 'static' stackframe list by the stackframe index numbers.
747 newStackList = new PHPStackFrame[stackListOld.size ()];
748 newStackList = (PHPStackFrame[]) stackListOld.toArray (newStackList);
749 DBGStackList = newStackList;
753 * Read the response from DBG and process the frame
756 * - The received command
757 * - or 0 if something was wrong
759 public int readResponse () throws IOException {
760 int bytesToRead = 0; // The number of byte to read for the current DBG block
761 int nextFrame = 0; // The current read position within entirePack
765 boolean errorStack = false;
766 char[] dbg_header_struct_read = new char[16]; // The buffer for the first 16 bytes of a block
767 int[] dbg_header_struct = new int[4]; // The first four numbers (long) of a block
768 int[] dbg_bpl_tmp = new int[10];
769 int[] dbg_frame = new int[2];
770 int[] dbg_eval_tmp = new int[3];
771 int[] dbg_src_tree_tmp = new int[4]; //
772 int[] dbg_error_tmp = new int[2];
773 Vector rawList = new Vector();
774 Vector stackList = new Vector(); // Intermediate stacklist which is build up in FRAME_STACK frame
780 while (readInput (dbg_header_struct_read, 16) != 0) { // Read 16 byte from input stream
781 dbg_header_struct[0] = PHPDBGBase.Char4ToInt (dbg_header_struct_read, 0); // Debug sync header
782 dbg_header_struct[1] = PHPDBGBase.Char4ToInt (dbg_header_struct_read, 4); // Command
783 dbg_header_struct[2] = PHPDBGBase.Char4ToInt (dbg_header_struct_read, 8); //
784 dbg_header_struct[3] = PHPDBGBase.Char4ToInt (dbg_header_struct_read, 12); // Bytes within this block
786 if (dbg_header_struct[0] != 0x5953) { // Check DBG sync bytes
787 return 0; // Wrong header
790 cmdReceived = dbg_header_struct[1]; // Get the command
791 setLastCmd (cmdReceived); // Store the info about the current command
792 bytesToRead = dbg_header_struct[3]; // Get the number of bytes to read for this block
794 //System.out.println("Response Received: " + cmdReceived);
795 char[] entirePack = new char[bytesToRead]; // Store the block data into buffer 'entirePack'
797 if (bytesToRead > 0) { // If there is something within the frame
798 if (readInput (entirePack, bytesToRead) < bytesToRead) { // Read the frame into the buffer
799 return 0; // We did not read enough bytes, error
803 nextFrame = 0; // Start with the first frame
805 while (nextFrame < bytesToRead) { // As long as we have something within this block
806 dbg_frame[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame); // The name of the frame
807 dbg_frame[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4); // The size of the frame
808 nextFrame += 8; // The next read position
810 if (dbg_frame[1] == 0) { // Something within the frame?
811 return 0; // Nothing to read, error
814 switch (dbg_frame[0]) {
815 case PHPDBGBase.FRAME_STACK:
816 int[] dbg_stack_new = new int[4]; //
818 dbg_stack_new[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0); // Source line number
819 dbg_stack_new[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4); // Module number
820 dbg_stack_new[2] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 8); // Scope id
821 dbg_stack_new[3] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 12); // ID of description string
823 if ((dbg_stack_new[1] != 0) && !errorStack) {
824 PHPStackFrame newStack;
827 newStack = new PHPStackFrame (null, // The thread
828 getModByNo (dbg_stack_new[1]), // The name of the module (file)
829 dbg_stack_new[0], // The source line number
831 getRawFrameData (entirePack, // Get the string from this packet
832 dbg_stack_new[3]), // The frame ID for which we want the string
833 dbg_stack_new[1]); // The module number
834 stackList.add (newStack);
840 case PHPDBGBase.FRAME_SOURCE: // Nothing to be done here
841 break; // TODO: what's with that frame? Something interesting
843 case PHPDBGBase.FRAME_SRC_TREE: //
844 dbg_src_tree_tmp[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0); // The parent module number
845 dbg_src_tree_tmp[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4); // The parent line number (not used)
846 dbg_src_tree_tmp[2] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 8); // The module number
847 dbg_src_tree_tmp[3] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 12); // The filename number
849 if (getModByNo (dbg_src_tree_tmp[2]).equals ("")) {
852 fileName = new String (getRawFrameData (entirePack, dbg_src_tree_tmp[3])); // Get the filename
854 if (fileName.length () > 0) { // If we have a filename
855 fileName = fileName.substring (0, fileName.length () - 1); // Remove '\0' char
858 if (dbg_src_tree_tmp[2] != 0) { // If there is a module number
861 modNew = new PHPDBGMod (dbg_src_tree_tmp[2], fileName); // Create a module object
863 DBGMods.add (modNew); // And store it to array
868 case PHPDBGBase.FRAME_RAWDATA: // Nothing to be done here
869 break; // FRAME_RAWDATA are processed within getRawFrameData
871 case PHPDBGBase.FRAME_ERROR: // An error frame
872 errorStack = true; // Yes, we have an error stack
873 dbg_error_tmp[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0); // Error type
874 dbg_error_tmp[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4); // Error message ID
876 String error = "\n"; //
878 switch (dbg_error_tmp[0]) { // Switch on error type
879 case PHPDBGBase.E_ERROR: error += "[Error]"; break;
880 case PHPDBGBase.E_WARNING: error += "[Warning]"; break;
881 case PHPDBGBase.E_PARSE: error += "[Parse Error]"; break;
882 case PHPDBGBase.E_NOTICE: error += "[Notice]"; break;
883 case PHPDBGBase.E_CORE_ERROR: error += "[Core Error]"; break;
884 case PHPDBGBase.E_CORE_WARNING: error += "[Core Warning]"; break;
885 case PHPDBGBase.E_COMPILE_ERROR: error += "[Compile Error]"; break;
886 case PHPDBGBase.E_COMPILE_WARNING: error += "[Compile Warning]"; break;
887 case PHPDBGBase.E_USER_ERROR: error += "[User Error]"; break;
888 case PHPDBGBase.E_USER_WARNING: error += "[User Warning]"; break;
889 case PHPDBGBase.E_USER_NOTICE: error += "[User Notice]"; break;
890 default: error += "[Unexpected Error]"; break;
894 error += new String (getRawFrameData (entirePack, dbg_error_tmp[1])); // Add the error string for this error message ID
896 if (error.length () > 0) { // If we have a error message
897 error = error.substring (0, error.length () - 1); // Remove '\0' char
900 error += "\n"; // Append a CR
902 PHPDebugCorePlugin.log (new DebugException (new Status (IStatus.WARNING,
903 PHPDebugCorePlugin.PLUGIN_ID,
907 // To print errors on the console, I must execute a code in the
908 // php context, that write the stderr... I didn't found a better way
909 // TODO: Find a better way????
911 // String codeExec= "";
912 // codeExec= "fwrite(fopen('php://stderr', 'w'),\\\"" + error + "\\\");";
914 // evalBlock("eval(\"" + codeExec + "\");");
915 // } catch (DebugException e) {
916 // PHPDebugCorePlugin.log(e);
919 if (!stopOnError) { // Is always false (Did not see where this is set to true!?)
920 if (lastCommand.equals (PHPDBGBase.DBGA_CONTINUE)) { // If last command for PHP was a 'continue',
921 continueExecution (); // send continue again
922 } else if (lastCommand.equals (PHPDBGBase.DBGA_STEPINTO)) { // If last command for PHP was a 'step into',
923 stepInto (); // send 'step into' again
924 } else if (lastCommand.equals (PHPDBGBase.DBGA_STEPOUT)) { // If last command for PHP was a 'step out',
925 stepOut (); // send 'step out' again
926 } else if (lastCommand.equals (PHPDBGBase.DBGA_STEPOVER)) { // If last command for PHP was a 'step over',
927 stepOver (); // send 'step over' again
932 case PHPDBGBase.FRAME_EVAL:
935 evalString = new String ("");
936 dbg_eval_tmp[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0); // istr
937 dbg_eval_tmp[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4); // iresult
938 dbg_eval_tmp[2] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 8); // ierror
940 evalRet = getRawFrameData (entirePack, dbg_eval_tmp[1]); //
941 evalString = getRawFrameData (entirePack, dbg_eval_tmp[0]); //
942 serGlobals = evalRet; //
945 case PHPDBGBase.FRAME_BPS: //
948 case PHPDBGBase.FRAME_BPL:
951 dbg_bpl_new = new int[10];
952 dbg_bpl_new[0] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 0);
953 dbg_bpl_new[1] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 4);
954 dbg_bpl_new[2] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 8);
955 dbg_bpl_new[3] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 12);
956 dbg_bpl_new[4] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 16);
957 dbg_bpl_new[5] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 20);
958 dbg_bpl_new[6] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 24);
959 dbg_bpl_new[7] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 28);
960 dbg_bpl_new[8] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 32);
961 dbg_bpl_new[9] = PHPDBGBase.Char4ToInt (entirePack, nextFrame + 36);
963 // look if breakpoint already exists in vector
964 for (i = 0; i < DBGBPList.size (); i++) {
965 dbg_bpl_tmp = (int[]) DBGBPList.get (i);
967 if (dbg_bpl_tmp[8] == dbg_bpl_new[8]) {
968 DBGBPList.remove (i);
974 // add breakpoint to vector
975 DBGBPList.add (dbg_bpl_new);
976 copyToLastBP (dbg_bpl_new);
979 if (getModByNo (dbg_bpl_new[0]).equals ("")) {
982 fileName = new String (getRawFrameData (entirePack, dbg_bpl_new[2]));
984 if (fileName.length () > 0) { // If we have filename
985 fileName = fileName.substring (0, fileName.length () - 1); // Remove '\0' char
988 if (dbg_bpl_new[0] != 0) {
991 modNew = new PHPDBGMod (dbg_bpl_new[0], fileName);
993 DBGMods.add (modNew);
998 case PHPDBGBase.FRAME_VER:
1001 case PHPDBGBase.FRAME_SID:
1002 sid = PHPDBGBase.Char4ToInt(entirePack, nextFrame + 0);
1005 case PHPDBGBase.FRAME_SRCLINESINFO:
1008 case PHPDBGBase.FRAME_SRCCTXINFO:
1011 case PHPDBGBase.FRAME_LOG:
1014 case PHPDBGBase.FRAME_PROF:
1017 case PHPDBGBase.FRAME_PROF_C:
1020 case PHPDBGBase.FRAME_SET_OPT:
1024 nextFrame += dbg_frame[1]; // go to next frame
1027 // Now process command
1028 switch(cmdReceived) {
1029 case PHPDBGBase.DBGC_REPLY:
1032 case PHPDBGBase.DBGC_STARTUP:
1035 case PHPDBGBase.DBGC_END:
1036 sessionEnded = true;
1039 case PHPDBGBase.DBGC_BREAKPOINT:
1040 BPUnderHit = getBPUnderHit ();
1041 updateStackFrameList (stackList);
1044 case PHPDBGBase.DBGC_STEPINTO_DONE:
1045 case PHPDBGBase.DBGC_STEPOVER_DONE:
1046 case PHPDBGBase.DBGC_STEPOUT_DONE:
1047 case PHPDBGBase.DBGC_EMBEDDED_BREAK:
1048 case PHPDBGBase.DBGC_PAUSE:
1050 updateStackFrameList (stackList);
1053 case PHPDBGBase.DBGC_ERROR:
1055 updateStackFrameList (stackList);
1058 case PHPDBGBase.DBGC_LOG:
1061 case PHPDBGBase.DBGC_SID:
1066 return cmdReceived; // Return the command we received with this block
1073 public PHPStackFrame[] getStackList() {
1074 return DBGStackList;
1078 * Reads from input buffer (response sent from DBG) the given number of chars
1079 * into frame buffer.
1081 * @param buffer The frame buffer where to store the read data from DBG.
1082 * @param bytes The number of bytes (chars) which are to read from input stream.
1083 * @return The number of bytes actually read.
1085 private int readInput (char[] buffer, int bytes) throws IOException {
1086 int bytesRead = 0; // Reset the bytes read counter
1088 for (int i = 0; i < bytes; i++) { // For the number of bytes we should read
1089 if (in.ready ()) { // If input stream is ready for reading
1090 buffer[i] = (char) (in.read () & 0x00FF); // Read a char and store only the least significant 8-bits
1091 bytesRead++; // Increment the bytes read counter
1093 else { // Input stream is not ready
1094 break; // Break the loop
1098 return bytesRead; // Return the number of bytes actually read
1102 * PHPProxy could stop the waiting for a response with this method.
1105 public void setShouldStop () {
1106 this.shouldStop = true;
1110 * @param milliseconds The maximum time in milliseconds we wait for something
1111 * to be send from DBG.
1112 * @return - true if something was received from DBG
1113 * - false if nothing was send from DBG within the given time
1116 public boolean waitResponse (long milliseconds) throws IOException {
1119 timeout = System.currentTimeMillis () + milliseconds; // Calculate the system time till we wait.
1121 while (System.currentTimeMillis () < timeout) { // Is waiting time running out?
1122 if (in.ready () || shouldStop) { // No, so did we get something or should we stop now
1123 break; // Yes, break the waiting
1127 return in.ready (); // true if we got something from DBG