1) Although dbg will be dropped from being supported or bundled with PHPeclipse,...
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / PHPDBGProxy.java
index e09edcb..1001de9 100644 (file)
@@ -17,24 +17,28 @@ import java.net.SocketTimeoutException;
 import java.util.Map;
 import java.util.Vector;
 
+//import net.sourceforge.phpdt.internal.core.JavaProject;
 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
 import net.sourceforge.phpdt.internal.debug.core.model.PHPDebugTarget;
 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
 import net.sourceforge.phpdt.internal.debug.core.model.PHPThread;
 import net.sourceforge.phpdt.internal.debug.core.model.PHPVariable;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+/*import net.sourceforge.phpeclipse.actions.PHPEclipseShowAction;*/
 
+//import org.eclipse.core.internal.resources.Project;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.debug.core.DebugException;
 import org.eclipse.debug.core.DebugPlugin;
 import org.eclipse.debug.core.model.IBreakpoint;
+//import org.eclipse.swt.browser.Browser;
 
 public class PHPDBGProxy {
 
        private ServerSocket            server          = null;
-       private BufferedReader          reader          = null;
+       //private BufferedReader                reader          = null;
        private PHPDBGInterface         DBGInt          = null;         // The DBG interface which is linked with the proxy
        private PHPDebugTarget          debugTarget = null;
        private PHPDBGProxy             thisProxy       = null;
@@ -44,6 +48,7 @@ public class PHPDBGProxy {
        private int                             port;
        private boolean                         remote;
        private boolean                         pathtranslation;
+       private boolean             bRelaunch;               // Relaunch the debugger after script termination 
        private Map                             pathmap;
        private IPath                           remoteSourcePath;
 
@@ -54,17 +59,37 @@ public class PHPDBGProxy {
        }
 
        /**
+        * updateView
+        * Clean up the view, but leave the Debug session running.
+        * added by ed_mann
+        */
+       public void updateView () {
+/*
+               try {
+                   server.close ();
+                   server = null;
+                   start ();
+               }
+               catch (IOException e) {
+               }
+*/
+        getDebugTarget ().updateThreads (PHPMainThread);
+       }
+
+       /**
         * @param remote
         * @param remoteSourcePath
         * @param pathTranslate
         * @param paths
+        * @param 
         */
-       public PHPDBGProxy (boolean remote, String remoteSourcePath, boolean pathTranslate, Map paths) {
+       public PHPDBGProxy (boolean remote, String remoteSourcePath, boolean pathTranslate, Map paths, boolean bRelaunchOnScriptTermination) {
                thisProxy             = this;
                this.remote               = remote;
                this.remoteSourcePath = new Path (remoteSourcePath);
                this.pathmap          = paths;
                this.pathtranslation  = pathTranslate;
+               this.bRelaunch        = bRelaunchOnScriptTermination; 
        }
 
        /**
@@ -81,22 +106,29 @@ public class PHPDBGProxy {
         */
        public void stop () {
                phpLoop.setShouldStop ();                                   // Notify the thread's 'run loop' to stop
-
                if (DBGInt != null) {                                       // If we have a DBG interface linked with this proxy
                DBGInt.setShouldStop ();                                //  Notify the DBG interface to stop the waiting for response
-               }
-
-               if (!remote) {                                              // If it's not a remote proxy session
-               try {
-                       getDebugTarget ().getProcess ().terminate ();       //
-               } catch (DebugException e) {
-                       e.printStackTrace ();
-               }
                }
 
+//             if (!remote) {                                              // If it's not a remote proxy session
+//             try {
+//                     getDebugTarget ().getProcess ().terminate ();       //
+//             } catch (DebugException e) {
+//                     e.printStackTrace ();
+//             }
+//             }
+
                phpLoop.notifyWait ();
        }
 
+       public void setTerminated () {
+               try {
+                       PHPMainThread.terminate ();
+               }
+               catch (DebugException e) {
+               }
+       }
+
        /**
         * TODO Is this method called from anywhere?
         *
@@ -106,24 +138,35 @@ public class PHPDBGProxy {
         *
         * @return A server socket
         */
-       protected ServerSocket getServerSocket () throws IOException {
-               if (server == null) {                                                                           // Do we have already a server socket
-               createServerSocket ();                                                                  //  No, then create one
-               }
-
-               return server;                                                                                          // Return the server socket
-       }
+//     protected ServerSocket getServerSocket () throws IOException {
+//             if (server == null) {                                                                           // Do we have already a server socket
+//             createServerSocket ();                                                                  //  No, then create one
+//             }
+//
+//             return server;                                                                                          // Return the server socket
+//     }
 
        /**
+        * Find a free unused port between 10001 and 10101 if the current debug session
+        * is for remote debugging, and a unused port 7869 if it is used as non remote debugging.
+        *
+        * For remote debugging the used port is submitted with the URL.
+        * E.g. http://localhost/index.php?DBGSESSID=1@localhost:10001
+        * For non remote debugging (if PHPeclipse used e.g. php cli directly) no port
+        * can be submitted by parameter, and only the default port (7869) can be used.
+        *
+        * @note: The free dbg version doesn't allow to set the appropriate port within php.ini!
+        *
         *
-        * TODO The example for setting up DBG within PHP.ini shows ports from 10000 to 10016 ???
-        * if my interpretation is correct.
-        * How can we find the correct DBG port?
         */
        protected void createServerSocket () {
-               port = SocketUtil.findUnusedLocalPort ("localhost", 10001, 10101);      // Get the first free port in the range from 10001 to 10101
+               if (this.remote) {
+                       port = SocketUtil.findUnusedLocalPort ("localhost", 10001, 10101);      // Get the first free port in the range from 10001 to 10101
+               }
+               else {
+                       port = SocketUtil.findUnusedLocalPort ("localhost", 7869, 7869);        // Get the first free port in the range from 7869 to 7869
+               }
 
-//         port = 10001;
                if (port == -1) {                                                   // Did we get a free port?
                PHPDebugCorePlugin.log (5, "Cannot find free port!!!!");        //  No, output a error message
 
@@ -135,7 +178,6 @@ public class PHPDBGProxy {
                        //System.out.println("ServerSocket on port: " + port);
                }
                } catch (IOException e) {
-               // IO Error
                PHPDebugCorePlugin.log (e);
                stop ();
                }
@@ -153,22 +195,31 @@ public class PHPDBGProxy {
         *
         * @paran DBGInt The DGB interface which is linked with this proxy
         */
-       protected void setDBGInterface (PHPDBGInterface DBGInt) {
-               this.DBGInt = DBGInt;
+//     protected void setDBGInterface (PHPDBGInterface DBGInt) {
+//             this.DBGInt = DBGInt;
+//     }
+
+       /**
+        * Get the DBG interface which is linked to this proxy
+        *
+        * @paran DBGInt The DGB interface which is linked with this proxy
+        */
+       public PHPDBGInterface getDBGInterface () {
+               return DBGInt;
        }
 
        /**
         * Give back a buffered input stream for the socket which is
         * linked with this proxy
         */
-       public BufferedReader getReader () throws IOException {
-               if (reader == null) {                                               // Do we already have a buffered input stream
-               reader = new BufferedReader (new InputStreamReader (this.getSocket ().getInputStream (),
-                                                                       "ISO8859_1"));
-               }
-
-         return reader;                                                      // Return the buffered input stream
-       }
+//     public BufferedReader getReader () throws IOException {
+//             if (reader == null) {                                               // Do we already have a buffered input stream
+//             reader = new BufferedReader (new InputStreamReader (this.getSocket ().getInputStream (),
+//                                                                     "ISO8859_1"));
+//             }
+//
+//       return reader;                                                      // Return the buffered input stream
+//     }
 
        /**
         *
@@ -208,13 +259,17 @@ public class PHPDBGProxy {
         *
         */
        private String MapPath (PHPLineBreakpoint phpLBP) {
-               IPath filename;
+               IPath    filename;
+               IPath    remotePath;
+               IPath    newpath;
+               IPath    localPath;
+               String   local;
 
                if (remote) {
                        filename = phpLBP.getMarker().getResource().getProjectRelativePath();
                        filename = remoteSourcePath.append (filename);
                } else {
-                       filename = phpLBP.getMarker().getResource().getLocation();
+                       filename = phpLBP.getMarker().getResource().getFullPath();
                }
 
                String path = filename.toOSString();
@@ -231,14 +286,43 @@ public class PHPDBGProxy {
                        }
                }
 
-               if (pathtranslation && remote) {
-                       if (remoteSourcePath.toString ().substring (0, 1).equals ("/")) {
-                               path = path.replace ('\\', '/');
-                       }
-                       else {
-                               path = path.replace ('/', '\\');
+               if (remoteSourcePath.isEmpty ()) {
+                       if ((pathmap != null) && remote) {
+                               java.util.Iterator iterator = pathmap.keySet().iterator();
+
+                               while (iterator.hasNext ()) {
+                                       local      = (String) iterator.next ();                 // Get the local/client side path of the mapping
+                                       remotePath = new Path ((String) pathmap.get (local));   // Get the remote/server side path of the mapping
+                                       localPath  = new Path (local);                          // Get the remote/server side path of the mapping
+
+                                       if (localPath.isPrefixOf (filename)) {                  // Starts the remote/server side file path with the remote/server side mapping path
+                                                                                                                                                       // dann prefix abhängen und den remote path davorhägen
+                                               newpath = filename.removeFirstSegments (localPath.matchingFirstSegments (filename));
+                                               newpath = remotePath.append (newpath);
+                                               path    = newpath.toString ();
+
+                                               if (path.substring (0, 1).equals ("/")) {
+                                                       path = path.replace ('\\', '/');
+                                               }
+                                               else {
+                                                       path = path.replace ('/', '\\');
+                                               }
+
+                                               return path;
+                                       }
+                               }
                        }
-               }
+               }
+               else {
+                       if (pathtranslation && remote) {
+                               if (remoteSourcePath.toString ().substring (0, 1).equals ("/")) {
+                                       path = path.replace ('\\', '/');
+                               }
+                               else {
+                                       path = path.replace ('/', '\\');
+                               }
+                       }
+               }
 
                return path;
        }
@@ -260,8 +344,19 @@ public class PHPDBGProxy {
                        phpLBP = (PHPLineBreakpoint) breakpoint;
 
                        //      bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
+                       if (phpLBP.isConditionEnabled ()) {
+                               bpNo = DBGInt.addBreakpoint (MapPath(phpLBP),
+                                                                                        phpLBP.getLineNumber(),
+                                                                phpLBP.getHitCount(),
+                                                                phpLBP.getCondition ());
+                       }
+                       else {
+                               bpNo = DBGInt.addBreakpoint (MapPath(phpLBP),
+                                                                                        phpLBP.getLineNumber(),
+                                                                phpLBP.getHitCount(),
+                                                                "");
+                       }
 
-                       bpNo = DBGInt.addBreakpoint(MapPath(phpLBP), phpLBP.getLineNumber());
                        phpLBP.setDBGBpNo(bpNo);
                }
                } catch (IOException e) {
@@ -303,9 +398,9 @@ public class PHPDBGProxy {
        /**
         *
         */
-       public void phpLoopNotify () {
-               phpLoop.notifyWait ();
-       }
+//     public void phpLoopNotify () {
+//             phpLoop.notifyWait ();
+//     }
 
        /**
         *
@@ -355,6 +450,7 @@ public class PHPDBGProxy {
        }
 
        /**
+        * Is called by the DebuggerRunner
         *
         * @param debugTarget
         */
@@ -403,8 +499,9 @@ public class PHPDBGProxy {
 
        public void readStepOverEnd (PHPStackFrame stackFrame) {
                try {
-               DBGInt.stepOver();
-               phpLoop.notifyWait();
+               if (DBGInt.stepOver ()) {
+                   phpLoop.notifyWait();
+               }
                } catch (Exception e) {
                PHPDebugCorePlugin.log(e);
                }
@@ -412,8 +509,9 @@ public class PHPDBGProxy {
 
        public void readStepReturnEnd (PHPStackFrame stackFrame) {
                try {
-               DBGInt.stepOut();
-                   phpLoop.notifyWait();
+               if (DBGInt.stepOut ()) {
+                       phpLoop.notifyWait();
+               }
                } catch (Exception e) {
                PHPDebugCorePlugin.log(e);
                }
@@ -421,14 +519,15 @@ public class PHPDBGProxy {
 
        public void readStepIntoEnd (PHPStackFrame stackFrame) {
                try {
-               DBGInt.stepInto();
-               phpLoop.notifyWait();
+               if (DBGInt.stepInto ()) {
+                       phpLoop.notifyWait();
+               }
                } catch (Exception e) {
                PHPDebugCorePlugin.log(e);
                }
        }
 
-       /*
+       /**
         * public PHPStackFrame[] readFrames(PHPThread thread) { //try { //this.println("th " + thread.getId() + " ; f "); //return new
         * FramesReader(getMultiReaderStrategy()).readFrames(thread); return null; //} catch (IOException e) { //
         * PHPDebugCorePlugin.log(e); // return null; //}
@@ -494,7 +593,6 @@ public class PHPDBGProxy {
                public void run () {
                try {
                        int                     i;
-                               int                     timeout;
                        long                    interval        = 200;                                  // Wait 200 ms maximum for a DBG response
                        boolean                 newconnect      = false;                                //
                        Socket                  newSocket       = null;
@@ -507,7 +605,6 @@ public class PHPDBGProxy {
 
                        PHPMainThread = new PHPThread (getDebugTarget (), getPort ());
                        PHPMainThread.setName ("Thread [main]");
-                       timeout       = 0;
 
                        //                              while ((getDebugTarget() == null) && (timeout < 100)) {
                        //                                      sleep(100);
@@ -538,9 +635,10 @@ public class PHPDBGProxy {
                                        server.setSoTimeout(1);                                 // ???
                                                }
 
-                                       newDBGInt = new PHPDBGInterface (getReader (newSocket),                 // Create a new interface
-                                                                                                                newSocket.getOutputStream (),
-                                                                                                                thisProxy);
+                                       newDBGInt = new PHPDBGInterface (getReader (newSocket),                 // The input buffer (frames from dbg)
+                                                                                                                newSocket.getOutputStream (),  // The output buffer (frames to dbg) 
+                                                                                                                thisProxy,
+                                                                                                                bRelaunch);                                    // Whether the debugger should be relaunched after script termination
                                        newDBGInt.waitResponse (1000);                          // Wait for the initial DBG response
                                        newDBGInt.flushAllPackets ();               // Read and process the DBG response