Patch from Anders Betn�- prevent disabled breakpoints from being sent to the debugger...
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / PHPDBGProxy.java
index 47262d7..2926870 100644 (file)
@@ -14,13 +14,14 @@ import java.io.OutputStream;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketTimeoutException;
+import java.util.Map;
 
 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
-import net.sourceforge.phpdt.internal.debug.core.logview.LogView;
 import net.sourceforge.phpdt.internal.debug.core.model.IPHPDebugTarget;
 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 org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
@@ -28,8 +29,6 @@ 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.ui.IViewPart;
-import org.eclipse.ui.IWorkbenchPage;
 
 public class PHPDBGProxy {
 
@@ -53,16 +52,22 @@ public class PHPDBGProxy {
 
   private boolean remote;
 
+  private boolean pathtranslation;
+  
+  private Map pathmap;
+  
   private IPath remoteSourcePath;
 
   public PHPDBGProxy() {
     thisProxy = this;
   }
 
-  public PHPDBGProxy(boolean remote, String remoteSourcePath) {
+  public PHPDBGProxy(boolean remote, String remoteSourcePath,boolean pathTranslate,Map paths) {
     thisProxy = this;
     this.remote = remote;
     this.remoteSourcePath = new Path(remoteSourcePath);
+    this.pathmap=paths;
+    this.pathtranslation=pathTranslate;
   }
 
   public void start() {
@@ -137,12 +142,43 @@ public class PHPDBGProxy {
   }
 
   protected void setBreakPoints() throws IOException, CoreException {
-    IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
-    for (int i = 0; i < breakpoints.length; i++) {
-      addBreakpoint(breakpoints[i]);
-    }
-  }
-
+               IBreakpoint[] breakpoints = DebugPlugin.getDefault()
+                               .getBreakpointManager().getBreakpoints();
+               for (int i = 0; i < breakpoints.length; i++) {
+                       if (breakpoints[i].isEnabled()) {
+                               addBreakpoint(breakpoints[i]);
+                       }
+               }
+       }
+
+  private String MapPath(PHPLineBreakpoint phpLBP) {
+               IPath filename;
+               if (remote) {
+                       filename = phpLBP.getMarker().getResource()
+                                       .getProjectRelativePath();
+                       filename = remoteSourcePath.append(filename);
+               } else
+                       filename = phpLBP.getMarker().getResource().getLocation();
+               String path = filename.toOSString();
+               if (pathmap != null && remote) {
+                       java.util.Iterator i = pathmap.keySet().iterator();
+                       while (i.hasNext()) {
+                               String k = (String) i.next();
+                               if (path.startsWith(k)) {
+                                       path = pathmap.get(k) + path.substring(k.length());
+                                       break;
+                               }
+                       }
+               }
+               if (pathtranslation && remote) {
+                       if (path.substring(0, 1).equals("/"))
+                               path = path.replace('\\', '/');
+                       else
+                               path = path.replace('/', '\\');
+               }
+               return path;
+       }
+  
   public void addBreakpoint(IBreakpoint breakpoint) {
     if (DBGInt == null)
       return;
@@ -150,14 +186,10 @@ public class PHPDBGProxy {
     try {
       PHPLineBreakpoint phpLBP;
       if (breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
-        IPath filename;
         phpLBP = (PHPLineBreakpoint) breakpoint;
         //                             bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
-        if (remote)
-          filename = remoteSourcePath.append(phpLBP.getMarker().getResource().getProjectRelativePath());
-        else
-          filename = phpLBP.getMarker().getResource().getLocation();
-        bpNo = DBGInt.addBreakpoint(filename.toOSString(), phpLBP.getLineNumber());
+
+        bpNo = DBGInt.addBreakpoint(MapPath(phpLBP), phpLBP.getLineNumber());
         phpLBP.setDBGBpNo(bpNo);
       }
     } catch (IOException e) {
@@ -176,13 +208,9 @@ public class PHPDBGProxy {
       PHPLineBreakpoint phpLBP;
       if (breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
         phpLBP = (PHPLineBreakpoint) breakpoint;
-        IPath filename;
-        if (remote)
-          filename = remoteSourcePath.append(phpLBP.getMarker().getResource().getProjectRelativePath());
-        else
-          filename = phpLBP.getMarker().getResource().getLocation();
+
         //                                     bpNo= DBGInt.addBreakpoint(filename.toOSString(), phpLBP.getLineNumber());
-        DBGInt.removeBreakpoint(filename.toOSString(), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
+        DBGInt.removeBreakpoint(MapPath(phpLBP), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
       }
     } catch (IOException e) {
       PHPDebugCorePlugin.log(e);
@@ -207,14 +235,19 @@ public class PHPDBGProxy {
       DBGInt.continueExecution();
       phpLoop.notifyWait();
     } catch (IOException e) {
-      PHPDebugCorePlugin.log(e);
+      PHPeclipsePlugin.log("Debugging session ended.", e);
       stop();
     }
   }
 
   public void pause() {
     try {
-      DBGInt.pauseExecution();
+      if (null != DBGInt)
+       DBGInt.pauseExecution();
+      else {
+       // TODO Make sure the Suspend action is grayed out 
+       // when DBGInt is null
+      }
     } catch (IOException e) {
       PHPDebugCorePlugin.log(e);
       stop();
@@ -314,8 +347,17 @@ public class PHPDBGProxy {
     }
 
     public synchronized void setShouldStop() {
-      shouldStop = true;
-    }
+               shouldStop = true;
+               try {
+                       // If the loop thread is blocked on the server socket, 
+                       // forcibly unblock it to avoid leaking the thread, 
+                       // the socket and the port
+                       closeServerSocket();
+               } catch (IOException x) {
+                       // Log this as a warning?
+                       PHPDebugCorePlugin.log(x);
+               }
+       }
 
     public synchronized void notifyWait() {
       notify();
@@ -440,4 +482,4 @@ public class PHPDBGProxy {
       }
     }
   }
-}
\ No newline at end of file
+}