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;
19 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
20 import net.sourceforge.phpdt.internal.debug.core.logview.LogView;
21 import net.sourceforge.phpdt.internal.debug.core.model.IPHPDebugTarget;
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;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.debug.core.DebugException;
30 import org.eclipse.debug.core.DebugPlugin;
31 import org.eclipse.debug.core.model.IBreakpoint;
32 import org.eclipse.ui.IViewPart;
33 import org.eclipse.ui.IWorkbenchPage;
35 public class PHPDBGProxy {
37 private ServerSocket server = null;
39 private Socket socket;
41 private BufferedReader reader = null;
43 private PHPDBGInterface DBGInt = null;
45 private IPHPDebugTarget debugTarget = null;
47 private PHPLoop phpLoop;
49 private PHPThread PHPMainThread;
51 private PHPDBGProxy thisProxy = null;
55 private boolean remote;
57 private boolean pathtranslation;
61 private IPath remoteSourcePath;
63 public PHPDBGProxy() {
67 public PHPDBGProxy(boolean remote, String remoteSourcePath,boolean pathTranslate,Map paths) {
70 this.remoteSourcePath = new Path(remoteSourcePath);
72 this.pathtranslation=pathTranslate;
81 phpLoop.setShouldStop();
83 DBGInt.setShouldStop();
86 getDebugTarget().getProcess().terminate();
87 } catch (DebugException e) {
94 protected ServerSocket getServerSocket() throws IOException {
101 protected void createServerSocket() {
102 port = SocketUtil.findUnusedLocalPort("localhost", 10001, 10101);
105 PHPDebugCorePlugin.log(5, "Cannot find free port!!!!");
109 if (server == null) {
110 server = new ServerSocket(port);
111 //System.out.println("ServerSocket on port: " + port);
113 } catch (IOException e) {
115 PHPDebugCorePlugin.log(e);
120 public Socket getSocket() throws IOException {
124 protected void setDBGInterface(PHPDBGInterface DBGInt) {
125 this.DBGInt = DBGInt;
128 public BufferedReader getReader() throws IOException {
129 if (reader == null) {
130 reader = new BufferedReader(new InputStreamReader(this.getSocket().getInputStream(), "ISO8859_1"));
135 public BufferedReader getReader(Socket socket) throws IOException {
137 return new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO8859_1"));
142 public OutputStream getOutputStream() throws IOException {
143 return this.getSocket().getOutputStream();
146 protected void setBreakPoints() throws IOException, CoreException {
147 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
148 for (int i = 0; i < breakpoints.length; i++) {
149 addBreakpoint(breakpoints[i]);
153 private String MapPath(PHPLineBreakpoint phpLBP)
158 filename = phpLBP.getMarker().getResource().getProjectRelativePath();
159 filename = remoteSourcePath.append(filename);
162 filename = phpLBP.getMarker().getResource().getLocation();
163 String path=filename.toOSString();
164 if(pathmap!=null&&remote)
166 java.util.Iterator i=pathmap.keySet().iterator();
169 String k=(String)i.next();
170 if(path.substring(0,k.length()).equals(k))
172 path=pathmap.get(k)+path.substring(k.length());
177 if(pathtranslation&&remote)
179 if(path.substring(0,1).equals("/"))
180 path=path.replace('\\','/');
182 path=path.replace('/','\\');
187 public void addBreakpoint(IBreakpoint breakpoint) {
192 PHPLineBreakpoint phpLBP;
193 if (breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
194 phpLBP = (PHPLineBreakpoint) breakpoint;
195 // bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
197 bpNo = DBGInt.addBreakpoint(MapPath(phpLBP), phpLBP.getLineNumber());
198 phpLBP.setDBGBpNo(bpNo);
200 } catch (IOException e) {
201 PHPDebugCorePlugin.log(e);
203 } catch (CoreException e) {
204 PHPDebugCorePlugin.log(e);
209 public void removeBreakpoint(IBreakpoint breakpoint) {
213 PHPLineBreakpoint phpLBP;
214 if (breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
215 phpLBP = (PHPLineBreakpoint) breakpoint;
217 // bpNo= DBGInt.addBreakpoint(filename.toOSString(), phpLBP.getLineNumber());
218 DBGInt.removeBreakpoint(MapPath(phpLBP), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
220 } catch (IOException e) {
221 PHPDebugCorePlugin.log(e);
223 } catch (CoreException e) {
224 PHPDebugCorePlugin.log(e);
229 public void phpLoopNotify() {
230 phpLoop.notifyWait();
233 public void startPHPLoop() {
234 phpLoop = new PHPLoop();
238 public void resume() {
240 DBGInt.continueExecution();
241 phpLoop.notifyWait();
242 } catch (IOException e) {
243 PHPDebugCorePlugin.log(e);
248 public void pause() {
250 DBGInt.pauseExecution();
251 } catch (IOException e) {
252 PHPDebugCorePlugin.log(e);
257 protected IPHPDebugTarget getDebugTarget() {
261 public void setDebugTarget(IPHPDebugTarget debugTarget) {
262 this.debugTarget = debugTarget;
263 debugTarget.setPHPDBGProxy(this);
266 public PHPVariable[] readVariables(PHPStackFrame frame) {
268 return DBGInt.getVariables(frame);
269 } catch (IOException ioex) {
270 ioex.printStackTrace();
271 throw new RuntimeException(ioex.getMessage());
272 } catch (DebugException ex) {
273 ex.printStackTrace();
274 throw new RuntimeException(ex.getMessage());
278 public PHPVariable[] eval(PHPStackFrame frame, String evalString) {
280 return DBGInt.evalBlock(frame, evalString);
281 // return DBGInt.getVariables(frame);
282 } catch (IOException ioex) {
283 ioex.printStackTrace();
284 throw new RuntimeException(ioex.getMessage());
285 } catch (DebugException ex) {
286 ex.printStackTrace();
287 throw new RuntimeException(ex.getMessage());
291 public void readStepOverEnd(PHPStackFrame stackFrame) {
294 phpLoop.notifyWait();
295 } catch (Exception e) {
296 PHPDebugCorePlugin.log(e);
300 public void readStepReturnEnd(PHPStackFrame stackFrame) {
303 phpLoop.notifyWait();
304 } catch (Exception e) {
305 PHPDebugCorePlugin.log(e);
309 public void readStepIntoEnd(PHPStackFrame stackFrame) {
312 phpLoop.notifyWait();
313 } catch (Exception e) {
314 PHPDebugCorePlugin.log(e);
319 * public PHPStackFrame[] readFrames(PHPThread thread) { //try { //this.println("th " + thread.getId() + " ; f "); //return new
320 * FramesReader(getMultiReaderStrategy()).readFrames(thread); return null; //} catch (IOException e) { //
321 * PHPDebugCorePlugin.log(e); // return null; //}
325 public void closeSocket() throws IOException {
326 if (socket != null) {
331 public void closeServerSocket() throws IOException {
332 if (server != null) {
337 public int getPort() {
341 class PHPLoop extends Thread {
342 private boolean shouldStop;
346 this.setName("PHPDebuggerLoop");
349 public synchronized void setShouldStop() {
353 public synchronized void notifyWait() {
359 char[] buf = new char[16];
361 long interval = 200; // 200ms
363 PHPStackFrame[] StackList;
364 boolean endFile = false;
365 boolean newconnect = false;
366 Socket newSocket = null;
367 PHPDBGInterface newDBGInt;
370 // synchronized (this) {
374 PHPMainThread = new PHPThread(getDebugTarget(), getPort());
375 PHPMainThread.setName("Thread [main]");
378 // while ((getDebugTarget() == null) && (timeout < 100)) {
382 // Be sure debug target is set
383 // PHPMainThread.setDebugTarget(getDebugTarget());
384 getDebugTarget().addThread(PHPMainThread);
386 //System.out.println("Waiting for breakpoints.");
387 while (!shouldStop) {
390 newSocket = server.accept();
391 //System.out.println("Accepted! : " + socket.toString());
392 } catch (SocketTimeoutException e) {
393 // no one wants to connect
395 } catch (IOException e) {
396 PHPDebugCorePlugin.log(e);
402 server.setSoTimeout(1);
403 newDBGInt = new PHPDBGInterface(getReader(newSocket), newSocket.getOutputStream(), thisProxy);
404 newDBGInt.waitResponse(1000);
405 newDBGInt.flushAllPackets();
406 // Check version and session ID
407 if ((DBGInt == null) || (DBGInt.getSID() == newDBGInt.getSID())) {
411 } catch (IOException e) {
412 PHPDebugCorePlugin.log(e);
417 DBGInt.continueExecution();
419 newDBGInt.continueExecution();
424 if (DBGInt.waitResponse(interval)) {
426 DBGInt.flushAllPackets();
428 if (DBGInt.BPUnderHit != 0) {
429 StackList = DBGInt.getStackList();
430 if (StackList.length > 0) {
431 for (i = 0; i < StackList.length; i++) {
432 StackList[i].setThread(PHPMainThread);
433 if (DBGInt.getModByNo(StackList[i].getModNo()).equals("")) {
434 DBGInt.getSourceTree();
436 StackList[i].setFile(DBGInt.getModByNo(StackList[i].getModNo()));
438 PHPMainThread.setStackFrames(StackList);
441 PHPMainThread.suspend();
443 synchronized (this) {
449 if (PHPMainThread.isTerminated()) {
454 if (PHPMainThread.isTerminated() || getDebugTarget().getProcess().isTerminated()) {
460 } catch (Exception ex) {
461 PHPDebugCorePlugin.log(ex);
462 System.out.println(ex);
465 getDebugTarget().terminate();
468 } catch (IOException e) {
469 PHPDebugCorePlugin.log(e);
472 //System.out.println("Socket loop finished.");