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 IBM Corporation - Initial implementation
10 Vicente Fernando - www.alfersoft.com.ar
11 Christian Perkonig - remote debug
12 **********************************************************************/
13 package net.sourceforge.phpdt.internal.debug.core;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStreamReader;
18 import java.io.OutputStream;
19 import java.net.Socket;
20 import java.net.ServerSocket;
21 import java.net.SocketTimeoutException;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.debug.core.DebugPlugin;
27 import org.eclipse.debug.core.DebugException;
28 import org.eclipse.debug.core.model.IBreakpoint;
29 import net.sourceforge.phpdt.internal.debug.core.model.IPHPDebugTarget;
30 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
31 import net.sourceforge.phpdt.internal.debug.core.model.PHPThread;
32 import net.sourceforge.phpdt.internal.debug.core.model.PHPVariable;
33 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
35 public class PHPDBGProxy {
37 private ServerSocket server= null;
38 private Socket socket;
39 private BufferedReader reader= null;
40 private PHPDBGInterface DBGInt= null;
41 private IPHPDebugTarget debugTarget= null;
42 private PHPLoop phpLoop;
43 private PHPThread PHPMainThread;
44 private PHPDBGProxy thisProxy= null;
46 private boolean remote;
47 private IPath remoteSourcePath;
49 public PHPDBGProxy() {
53 public PHPDBGProxy(boolean remote,String remoteSourcePath) {
56 this.remoteSourcePath= new Path(remoteSourcePath);
65 phpLoop.setShouldStop();
66 if(DBGInt != null) DBGInt.setShouldStop();
69 getDebugTarget().getProcess().terminate();
70 } catch (DebugException e) {
77 protected ServerSocket getServerSocket() throws IOException {
84 protected void createServerSocket() {
85 // port = SocketUtil.findUnusedLocalPort("localhost", 10001, 10101);
88 PHPDebugCorePlugin.log(5, "Cannot find free port!!!!");
93 server = new ServerSocket(port);
94 //System.out.println("ServerSocket on port: " + port);
96 } catch (IOException e) {
98 PHPDebugCorePlugin.log(e);
103 public Socket getSocket() throws IOException {
107 protected void setDBGInterface(PHPDBGInterface DBGInt) {
111 public BufferedReader getReader() throws IOException {
112 if (reader == null) {
113 reader = new BufferedReader(new InputStreamReader(this.getSocket().getInputStream(), "ISO8859_1"));
118 public BufferedReader getReader(Socket socket) throws IOException {
120 return new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO8859_1"));
125 public OutputStream getOutputStream() throws IOException {
126 return this.getSocket().getOutputStream();
129 protected void setBreakPoints() throws IOException, CoreException {
130 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
131 for (int i = 0; i < breakpoints.length; i++) {
132 addBreakpoint(breakpoints[i]);
136 public void addBreakpoint(IBreakpoint breakpoint) {
137 if (DBGInt == null) return;
140 PHPLineBreakpoint phpLBP;
141 if(breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
143 phpLBP= (PHPLineBreakpoint)breakpoint;
144 // bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
146 filename=remoteSourcePath.append(phpLBP.getMarker().getResource().getProjectRelativePath());
148 filename=phpLBP.getMarker().getResource().getLocation();
149 bpNo= DBGInt.addBreakpoint(filename.toOSString(), phpLBP.getLineNumber());
150 phpLBP.setDBGBpNo(bpNo);
152 } catch (IOException e) {
153 PHPDebugCorePlugin.log(e);
155 } catch (CoreException e) {
156 PHPDebugCorePlugin.log(e);
161 public void removeBreakpoint(IBreakpoint breakpoint) {
162 if (DBGInt == null) return;
164 PHPLineBreakpoint phpLBP;
165 if(breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
166 phpLBP= (PHPLineBreakpoint)breakpoint;
169 filename=remoteSourcePath.append(phpLBP.getMarker().getResource().getProjectRelativePath());
171 filename=phpLBP.getMarker().getResource().getLocation();
172 // bpNo= DBGInt.addBreakpoint(filename.toOSString(), phpLBP.getLineNumber());
173 DBGInt.removeBreakpoint(filename.toOSString(), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
175 } catch (IOException e) {
176 PHPDebugCorePlugin.log(e);
178 } catch (CoreException e) {
179 PHPDebugCorePlugin.log(e);
184 public void phpLoopNotify (){
185 phpLoop.notifyWait();
188 public void startPHPLoop() {
189 phpLoop = new PHPLoop();
193 public void resume() {
195 DBGInt.continueExecution();
196 phpLoop.notifyWait();
197 } catch (IOException e) {
198 PHPDebugCorePlugin.log(e);
203 public void pause() {
205 DBGInt.pauseExecution();
206 } catch (IOException e) {
207 PHPDebugCorePlugin.log(e);
211 protected IPHPDebugTarget getDebugTarget() {
215 public void setDebugTarget(IPHPDebugTarget debugTarget) {
216 this.debugTarget = debugTarget;
217 debugTarget.setPHPDBGProxy(this);
220 public PHPVariable[] readVariables(PHPStackFrame frame) {
222 return DBGInt.getVariables(frame);
223 } catch (IOException ioex) {
224 ioex.printStackTrace();
225 throw new RuntimeException(ioex.getMessage());
226 } catch (DebugException ex) {
227 ex.printStackTrace();
228 throw new RuntimeException(ex.getMessage());
232 public PHPVariable[] eval(PHPStackFrame frame,String evalString) {
234 return DBGInt.evalBlock(frame,evalString);
235 // return DBGInt.getVariables(frame);
236 } catch (IOException ioex) {
237 ioex.printStackTrace();
238 throw new RuntimeException(ioex.getMessage());
239 } catch (DebugException ex) {
240 ex.printStackTrace();
241 throw new RuntimeException(ex.getMessage());
245 public void readStepOverEnd(PHPStackFrame stackFrame) {
248 phpLoop.notifyWait();
249 } catch (Exception e) {
250 PHPDebugCorePlugin.log(e);
254 public void readStepReturnEnd(PHPStackFrame stackFrame) {
257 phpLoop.notifyWait();
258 } catch (Exception e) {
259 PHPDebugCorePlugin.log(e);
263 public void readStepIntoEnd(PHPStackFrame stackFrame) {
266 phpLoop.notifyWait();
267 } catch (Exception e) {
268 PHPDebugCorePlugin.log(e);
273 public PHPStackFrame[] readFrames(PHPThread thread) {
275 //this.println("th " + thread.getId() + " ; f ");
276 //return new FramesReader(getMultiReaderStrategy()).readFrames(thread);
278 //} catch (IOException e) {
279 // PHPDebugCorePlugin.log(e);
286 public void closeSocket() throws IOException {
287 if (socket != null) {
292 public void closeServerSocket() throws IOException {
293 if (server != null) {
298 public int getPort() {
302 class PHPLoop extends Thread {
303 private boolean shouldStop;
307 this.setName("PHPDebuggerLoop");
310 public synchronized void setShouldStop() {
314 public synchronized void notifyWait() {
320 char[] buf= new char[16];
322 long interval= 200; // 200ms
324 PHPStackFrame[] StackList;
325 boolean endFile=false;
326 boolean newconnect=false;
327 Socket newSocket=null;
328 PHPDBGInterface newDBGInt;
331 // synchronized (this) {
335 PHPMainThread = new PHPThread(getDebugTarget(), getPort());
336 PHPMainThread.setName("Thread [main]");
339 // while ((getDebugTarget() == null) && (timeout < 100)) {
343 // Be sure debug target is set
344 // PHPMainThread.setDebugTarget(getDebugTarget());
345 getDebugTarget().addThread(PHPMainThread);
347 //System.out.println("Waiting for breakpoints.");
352 newSocket = server.accept();
353 //System.out.println("Accepted! : " + socket.toString());
354 } catch (SocketTimeoutException e) {
355 // no one wants to connect
357 } catch (IOException e) {
358 PHPDebugCorePlugin.log(e);
365 server.setSoTimeout(1);
366 newDBGInt= new PHPDBGInterface(getReader(newSocket), newSocket.getOutputStream(), thisProxy);
367 newDBGInt.waitResponse(1000);
368 newDBGInt.flushAllPackets();
369 // Check version and session ID
370 if ((DBGInt==null) || (DBGInt.getSID()==newDBGInt.getSID()))
375 } catch (IOException e) {
376 PHPDebugCorePlugin.log(e);
381 DBGInt.continueExecution();
384 newDBGInt.continueExecution();
389 if(DBGInt.waitResponse(interval))
392 DBGInt.flushAllPackets();
394 if (DBGInt.BPUnderHit != 0) {
395 StackList = DBGInt.getStackList();
396 if (StackList.length > 0) {
397 for (i = 0; i < StackList.length; i++) {
398 StackList[i].setThread(PHPMainThread);
399 if (DBGInt.getModByNo(StackList[i].getModNo()).equals("")) {
400 DBGInt.getSourceTree();
402 StackList[i].setFile(
403 DBGInt.getModByNo(StackList[i].getModNo()));
405 PHPMainThread.setStackFrames(StackList);
408 PHPMainThread.suspend();
410 synchronized (this) {
416 if (PHPMainThread.isTerminated()) {
421 if (PHPMainThread.isTerminated() || getDebugTarget().getProcess().isTerminated()) {
428 catch (Exception ex) {
429 PHPDebugCorePlugin.log(ex);
430 System.out.println(ex);
434 getDebugTarget().terminate();
437 } catch (IOException e) {
438 PHPDebugCorePlugin.log(e);
441 //System.out.println("Socket loop finished.");