1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: IBM Corporation - Initial implementation Vicente Fernando - www.alfersoft.com.ar Christian Perkonig - remote debug
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.debug.core;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.InputStreamReader;
13 import java.io.OutputStream;
14 import java.net.ServerSocket;
15 import java.net.Socket;
16 import java.net.SocketTimeoutException;
18 import java.util.Vector;
20 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
21 import net.sourceforge.phpdt.internal.debug.core.model.PHPDebugTarget;
22 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
23 import net.sourceforge.phpdt.internal.debug.core.model.PHPThread;
24 import net.sourceforge.phpdt.internal.debug.core.model.PHPVariable;
25 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.debug.core.DebugException;
31 import org.eclipse.debug.core.DebugPlugin;
32 import org.eclipse.debug.core.model.IBreakpoint;
34 public class PHPDBGProxy {
36 private ServerSocket server = null;
37 private BufferedReader reader = null;
38 private PHPDBGInterface DBGInt = null; // The DBG interface which is linked with the proxy
39 private PHPDebugTarget debugTarget = null;
40 private PHPDBGProxy thisProxy = null;
41 private PHPLoop phpLoop;
42 private PHPThread PHPMainThread;
43 private Socket socket;
45 private boolean remote;
46 private boolean pathtranslation;
48 private IPath remoteSourcePath;
52 public PHPDBGProxy () {
58 * @param remoteSourcePath
59 * @param pathTranslate
62 public PHPDBGProxy (boolean remote, String remoteSourcePath, boolean pathTranslate, Map paths) {
65 this.remoteSourcePath = new Path (remoteSourcePath);
67 this.pathtranslation = pathTranslate;
73 public void start () {
74 createServerSocket (); // Create a server socket for communicatio with DBG
76 this.startPHPLoop (); //
83 phpLoop.setShouldStop (); // Notify the thread's 'run loop' to stop
85 if (DBGInt != null) { // If we have a DBG interface linked with this proxy
86 DBGInt.setShouldStop (); // Notify the DBG interface to stop the waiting for response
89 if (!remote) { // If it's not a remote proxy session
91 getDebugTarget ().getProcess ().terminate (); //
92 } catch (DebugException e) {
97 phpLoop.notifyWait ();
101 * TODO Is this method called from anywhere?
103 * Returns a already created server socket, or
104 * creates a server socket if none exists, and
105 * returns the newly created one.
107 * @return A server socket
109 protected ServerSocket getServerSocket () throws IOException {
110 if (server == null) { // Do we have already a server socket
111 createServerSocket (); // No, then create one
114 return server; // Return the server socket
119 * TODO The example for setting up DBG within PHP.ini shows ports from 10000 to 10016 ???
120 * if my interpretation is correct.
121 * How can we find the correct DBG port?
123 protected void createServerSocket () {
124 port = SocketUtil.findUnusedLocalPort ("localhost", 10001, 10101); // Get the first free port in the range from 10001 to 10101
127 if (port == -1) { // Did we get a free port?
128 PHPDebugCorePlugin.log (5, "Cannot find free port!!!!"); // No, output a error message
130 return; // And return
133 if (server == null) { // If there is no server socket yet
134 server = new ServerSocket (port); // create a server socket for the free port
135 //System.out.println("ServerSocket on port: " + port);
137 } catch (IOException e) {
139 PHPDebugCorePlugin.log (e);
147 public Socket getSocket () throws IOException {
148 return socket; // Return the socket
152 * Set the DBG interface which is linked to this proxy
154 * @paran DBGInt The DGB interface which is linked with this proxy
156 protected void setDBGInterface (PHPDBGInterface DBGInt) {
157 this.DBGInt = DBGInt;
161 * Give back a buffered input stream for the socket which is
162 * linked with this proxy
164 public BufferedReader getReader () throws IOException {
165 if (reader == null) { // Do we already have a buffered input stream
166 reader = new BufferedReader (new InputStreamReader (this.getSocket ().getInputStream (),
170 return reader; // Return the buffered input stream
176 public BufferedReader getReader (Socket socket) throws IOException {
177 if (socket != null) { // Is a socket provided
178 return new BufferedReader (new InputStreamReader (socket.getInputStream (),
179 "ISO8859_1")); // Then create a buffered input stream
182 return null; // Without a socket we can't create a input stream
188 * @return The output stream for this proxy's socket
190 public OutputStream getOutputStream () throws IOException {
191 return this.getSocket ().getOutputStream ();
197 protected void setBreakPoints () throws IOException, CoreException {
198 IBreakpoint[] breakpoints = DebugPlugin.getDefault ().getBreakpointManager ().getBreakpoints ();
200 for (int i = 0; i < breakpoints.length; i++) {
201 if (breakpoints[i].isEnabled ()) {
202 addBreakpoint (breakpoints[i]);
210 private String MapPath (PHPLineBreakpoint phpLBP) {
214 filename = phpLBP.getMarker().getResource().getProjectRelativePath();
215 filename = remoteSourcePath.append (filename);
217 filename = phpLBP.getMarker().getResource().getLocation();
220 String path = filename.toOSString();
222 if ((pathmap != null) && remote) {
223 java.util.Iterator i = pathmap.keySet().iterator();
225 while (i.hasNext()) {
226 String k = (String) i.next();
227 if (path.startsWith(k)) {
228 path = pathmap.get(k) + path.substring(k.length());
234 if (pathtranslation && remote) {
235 if (remoteSourcePath.toString ().substring (0, 1).equals ("/")) {
236 path = path.replace ('/', '\\');
239 path = path.replace ('\\', '/');
249 public void addBreakpoint (IBreakpoint breakpoint) {
250 if (DBGInt == null) {
257 PHPLineBreakpoint phpLBP;
259 if (breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
260 phpLBP = (PHPLineBreakpoint) breakpoint;
262 // bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
264 bpNo = DBGInt.addBreakpoint(MapPath(phpLBP), phpLBP.getLineNumber());
265 phpLBP.setDBGBpNo(bpNo);
267 } catch (IOException e) {
268 PHPDebugCorePlugin.log(e);
270 } catch (CoreException e) {
271 PHPDebugCorePlugin.log(e);
279 public void removeBreakpoint (IBreakpoint breakpoint) {
280 if (DBGInt == null) {
285 PHPLineBreakpoint phpLBP;
287 if (breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier ()) {
288 phpLBP = (PHPLineBreakpoint) breakpoint;
290 // bpNo= DBGInt.addBreakpoint(filename.toOSString(), phpLBP.getLineNumber());
292 DBGInt.removeBreakpoint(MapPath(phpLBP), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
294 } catch (IOException e) {
295 PHPDebugCorePlugin.log (e);
297 } catch (CoreException e) {
298 PHPDebugCorePlugin.log (e);
306 public void phpLoopNotify () {
307 phpLoop.notifyWait ();
313 public void startPHPLoop () {
314 phpLoop = new PHPLoop (); // Create a DBG communication loop object
316 phpLoop.start (); // And start the communication loop
322 public void resume () {
324 DBGInt.continueExecution();
325 phpLoop.notifyWait();
326 } catch (IOException e) {
327 PHPeclipsePlugin.log("Debugging session ended.", e);
335 public void pause () {
337 if (null != DBGInt) {
338 DBGInt.pauseExecution();
341 // TODO Make sure the Suspend action is grayed out
342 // when DBGInt is null
344 } catch (IOException e) {
345 PHPDebugCorePlugin.log (e);
353 protected PHPDebugTarget getDebugTarget() {
361 public void setDebugTarget (PHPDebugTarget debugTarget) {
362 this.debugTarget = debugTarget;
363 debugTarget.setPHPDBGProxy(this);
367 * This method is called by a stackframe.
368 * It reads the variables from PHP via DBG
370 * @param frame The stackframe which wants the variables.
371 * @return The list of variables for this stackframe.
373 public Vector readVariables (PHPStackFrame frame) {
375 return DBGInt.getVariables (frame); // Get the variables from DBG interface
376 } catch (IOException ioex) {
377 ioex.printStackTrace ();
378 throw new RuntimeException (ioex.getMessage ());
379 } catch (DebugException ex) {
380 ex.printStackTrace ();
381 throw new RuntimeException (ex.getMessage ());
391 public PHPVariable[] eval (PHPStackFrame frame, String evalString) {
393 return DBGInt.evalBlock (frame, evalString);
394 //return DBGInt.getVariables(frame);
395 } catch (IOException ioex) {
396 ioex.printStackTrace();
397 throw new RuntimeException(ioex.getMessage());
398 } catch (DebugException ex) {
399 ex.printStackTrace();
400 throw new RuntimeException(ex.getMessage());
404 public void readStepOverEnd (PHPStackFrame stackFrame) {
407 phpLoop.notifyWait();
408 } catch (Exception e) {
409 PHPDebugCorePlugin.log(e);
413 public void readStepReturnEnd (PHPStackFrame stackFrame) {
416 phpLoop.notifyWait();
417 } catch (Exception e) {
418 PHPDebugCorePlugin.log(e);
422 public void readStepIntoEnd (PHPStackFrame stackFrame) {
425 phpLoop.notifyWait();
426 } catch (Exception e) {
427 PHPDebugCorePlugin.log(e);
432 * public PHPStackFrame[] readFrames(PHPThread thread) { //try { //this.println("th " + thread.getId() + " ; f "); //return new
433 * FramesReader(getMultiReaderStrategy()).readFrames(thread); return null; //} catch (IOException e) { //
434 * PHPDebugCorePlugin.log(e); // return null; //}
438 public void closeSocket() throws IOException {
439 if (socket != null) {
444 public void closeServerSocket() throws IOException {
445 if (server != null) {
450 public int getPort() {
458 class PHPLoop extends Thread {
459 private boolean shouldStop;
463 this.setName ("PHPDebuggerLoop");
469 public synchronized void setShouldStop () {
470 shouldStop = true; // The run loop should stop
473 // If the loop thread is blocked on the server socket,
474 // forcibly unblock it to avoid leaking the thread,
475 // the socket and the port
476 closeServerSocket ();
477 } catch (IOException x) {
478 // Log this as a warning?
479 PHPDebugCorePlugin.log (x);
486 public synchronized void notifyWait () {
498 long interval = 200; // Wait 200 ms maximum for a DBG response
499 boolean newconnect = false; //
500 Socket newSocket = null;
501 PHPStackFrame[] StackList;
502 PHPDBGInterface newDBGInt;
504 // synchronized (this) {
508 PHPMainThread = new PHPThread (getDebugTarget (), getPort ());
509 PHPMainThread.setName ("Thread [main]");
512 // while ((getDebugTarget() == null) && (timeout < 100)) {
516 // Be sure debug target is set
517 // PHPMainThread.setDebugTarget(getDebugTarget());
519 getDebugTarget ().addThread (PHPMainThread);
521 //System.out.println("Waiting for breakpoints.");
523 while (!shouldStop) { // As long as nobody will stop us
524 newconnect = true; // The first time
527 newSocket = server.accept(); // Waits until DBG want to connect
528 //System.out.println("Accepted! : " + socket.toString());
529 } catch (SocketTimeoutException e) {
530 newconnect = false; // No one wants to connect (connection already done)
531 } catch (IOException e) {
532 PHPDebugCorePlugin.log(e);
536 if (newconnect) { // Is it just after a new connection
537 if (DBGInt == null) { // Do we have a DBG interface?
538 server.setSoTimeout(1); // ???
541 newDBGInt = new PHPDBGInterface (getReader (newSocket), // Create a new interface
542 newSocket.getOutputStream (),
544 newDBGInt.waitResponse (1000); // Wait for the initial DBG response
545 newDBGInt.flushAllPackets (); // Read and process the DBG response
547 // Check version and session ID
548 if ((DBGInt == null) || // If we have no interface
549 (DBGInt.getSID () == newDBGInt.getSID ())) {// or the new session ID is different to the old one
550 DBGInt = newDBGInt; // Set the new interface as current one
555 catch (IOException e) {
556 PHPDebugCorePlugin.log (e);
562 DBGInt.continueExecution (); // Notify DBG that PHP should continue
565 newDBGInt.continueExecution (); // Notify DBG that PHP should continue
570 if (DBGInt.waitResponse (interval)) { // Wait for a DBG response (200 ms)
571 DBGInt.flushAllPackets (); // If we got something, read and process it
573 if (DBGInt.BPUnderHit != 0) { // ???
574 StackList = DBGInt.getStackList (); // Get the stack list from DBGInterface
576 if (StackList.length > 0) { // If there is something in stack list
577 for (i = 0; i < StackList.length; i++) { // For all stack list
578 StackList[i].setThread (PHPMainThread); // Set the PHPTread for all PHPStackFrames
580 if (DBGInt.getModByNo (StackList[i].getModNo ()).equals ("")) {
581 DBGInt.getSourceTree ();
584 StackList[i].setFile (DBGInt.getModByNo (StackList[i].getModNo ()));
587 PHPMainThread.setStackFrames (StackList);
590 PHPMainThread.suspend (); // Fire debug event
592 synchronized (this) {
599 if (PHPMainThread.isTerminated ()) {
602 break; // Go for terminating the thread
605 if (PHPMainThread.isTerminated () ||
606 getDebugTarget ().getProcess ().isTerminated ()) {
609 break; // Go for terminating the thread
613 } catch (Exception ex) {
614 PHPDebugCorePlugin.log (ex);
615 System.out.println (ex);
618 getDebugTarget ().terminate ();
620 closeServerSocket ();
621 } catch (IOException e) {
622 PHPDebugCorePlugin.log (e);
627 //System.out.println("Socket loop finished.");