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[] readInstanceVariables(PHPVariable variable) {
234 return DBGInt.getInstVars(variable);
235 } catch (DebugException ex) {
236 ex.printStackTrace();
237 throw new RuntimeException(ex.getMessage());
242 public void readStepOverEnd(PHPStackFrame stackFrame) {
245 phpLoop.notifyWait();
246 } catch (Exception e) {
247 PHPDebugCorePlugin.log(e);
251 public void readStepReturnEnd(PHPStackFrame stackFrame) {
254 phpLoop.notifyWait();
255 } catch (Exception e) {
256 PHPDebugCorePlugin.log(e);
260 public void readStepIntoEnd(PHPStackFrame stackFrame) {
263 phpLoop.notifyWait();
264 } catch (Exception e) {
265 PHPDebugCorePlugin.log(e);
270 public PHPStackFrame[] readFrames(PHPThread thread) {
272 //this.println("th " + thread.getId() + " ; f ");
273 //return new FramesReader(getMultiReaderStrategy()).readFrames(thread);
275 //} catch (IOException e) {
276 // PHPDebugCorePlugin.log(e);
283 public void closeSocket() throws IOException {
284 if (socket != null) {
289 public void closeServerSocket() throws IOException {
290 if (server != null) {
295 public int getPort() {
299 class PHPLoop extends Thread {
300 private boolean shouldStop;
304 this.setName("PHPDebuggerLoop");
307 public synchronized void setShouldStop() {
311 public synchronized void notifyWait() {
317 char[] buf= new char[16];
319 long interval= 200; // 200ms
321 PHPStackFrame[] StackList;
322 boolean endFile=false;
323 boolean newconnect=false;
324 Socket newSocket=null;
325 PHPDBGInterface newDBGInt;
328 // synchronized (this) {
332 PHPMainThread = new PHPThread(getDebugTarget(), getPort());
333 PHPMainThread.setName("Thread [main]");
336 // while ((getDebugTarget() == null) && (timeout < 100)) {
340 // Be sure debug target is set
341 // PHPMainThread.setDebugTarget(getDebugTarget());
342 getDebugTarget().addThread(PHPMainThread);
344 //System.out.println("Waiting for breakpoints.");
349 newSocket = server.accept();
350 //System.out.println("Accepted! : " + socket.toString());
351 } catch (SocketTimeoutException e) {
352 // no one wants to connect
354 } catch (IOException e) {
355 PHPDebugCorePlugin.log(e);
362 server.setSoTimeout(1);
363 newDBGInt= new PHPDBGInterface(getReader(newSocket), newSocket.getOutputStream(), thisProxy);
364 newDBGInt.waitResponse(1000);
365 newDBGInt.flushAllPackets();
366 // Check version and session ID
367 if ((DBGInt==null) || (DBGInt.getSID()==newDBGInt.getSID()))
372 } catch (IOException e) {
373 PHPDebugCorePlugin.log(e);
378 DBGInt.continueExecution();
381 newDBGInt.continueExecution();
386 if(DBGInt.waitResponse(interval))
389 DBGInt.flushAllPackets();
391 if (DBGInt.BPUnderHit != 0) {
392 StackList = DBGInt.getStackList();
393 if (StackList.length > 0) {
394 for (i = 0; i < StackList.length; i++) {
395 StackList[i].setThread(PHPMainThread);
396 if (DBGInt.getModByNo(StackList[i].getModNo()).equals("")) {
397 DBGInt.getSourceTree();
399 StackList[i].setFile(
400 DBGInt.getModByNo(StackList[i].getModNo()));
402 PHPMainThread.setStackFrames(StackList);
405 PHPMainThread.suspend();
407 synchronized (this) {
413 if (PHPMainThread.isTerminated()) {
418 if (PHPMainThread.isTerminated() || getDebugTarget().getProcess().isTerminated()) {
425 catch (Exception ex) {
426 PHPDebugCorePlugin.log(ex);
427 System.out.println(ex);
431 getDebugTarget().terminate();
434 } catch (IOException e) {
435 PHPDebugCorePlugin.log(e);
438 //System.out.println("Socket loop finished.");