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 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core;
14 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.io.OutputStream;
18 import java.net.Socket;
19 import java.net.ServerSocket;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.debug.core.DebugPlugin;
23 import org.eclipse.debug.core.DebugException;
24 import org.eclipse.debug.core.model.IBreakpoint;
25 import net.sourceforge.phpdt.internal.debug.core.model.IPHPDebugTarget;
26 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
27 import net.sourceforge.phpdt.internal.debug.core.model.PHPThread;
28 import net.sourceforge.phpdt.internal.debug.core.model.PHPVariable;
29 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
31 public class PHPDBGProxy {
33 private ServerSocket server= null;
34 private Socket socket;
35 private BufferedReader reader= null;
36 private PHPDBGInterface DBGInt= null;
37 private IPHPDebugTarget debugTarget= null;
38 private PHPLoop phpLoop;
39 private PHPThread PHPMainThread;
40 private PHPDBGProxy thisProxy= null;
43 public PHPDBGProxy() {
53 phpLoop.setShouldStop();
54 if(DBGInt != null) DBGInt.setShouldStop();
58 protected ServerSocket getServerSocket() throws IOException {
65 protected void createServerSocket() {
66 port = SocketUtil.findUnusedLocalPort("localhost", 10001, 10101);
68 PHPDebugCorePlugin.log(5, "Cannot find free port!!!!");
73 server = new ServerSocket(port);
74 //System.out.println("ServerSocket on port: " + port);
76 } catch (IOException e) {
78 PHPDebugCorePlugin.log(e);
83 public Socket getSocket() throws IOException {
87 protected void setDBGInterface(PHPDBGInterface DBGInt) {
91 public BufferedReader getReader() throws IOException {
93 reader = new BufferedReader(new InputStreamReader(this.getSocket().getInputStream(), "ISO8859_1"));
98 public OutputStream getOutputStream() throws IOException {
99 return this.getSocket().getOutputStream();
102 protected void setBreakPoints() throws IOException, CoreException {
103 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
104 for (int i = 0; i < breakpoints.length; i++) {
105 addBreakpoint(breakpoints[i]);
109 public void addBreakpoint(IBreakpoint breakpoint) {
110 if (DBGInt == null) return;
113 PHPLineBreakpoint phpLBP;
114 if(breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getDefault().getDescriptor().getUniqueIdentifier()) {
115 phpLBP= (PHPLineBreakpoint)breakpoint;
116 bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
117 phpLBP.setDBGBpNo(bpNo);
119 } catch (IOException e) {
120 PHPDebugCorePlugin.log(e);
122 } catch (CoreException e) {
123 PHPDebugCorePlugin.log(e);
128 public void removeBreakpoint(IBreakpoint breakpoint) {
129 if (DBGInt == null) return;
131 PHPLineBreakpoint phpLBP;
132 if(breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getDefault().getDescriptor().getUniqueIdentifier()) {
133 phpLBP= (PHPLineBreakpoint)breakpoint;
134 DBGInt.removeBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
136 } catch (IOException e) {
137 PHPDebugCorePlugin.log(e);
139 } catch (CoreException e) {
140 PHPDebugCorePlugin.log(e);
145 public void startPHPLoop() {
146 phpLoop = new PHPLoop();
150 public void resume(PHPThread thread) {
152 DBGInt.continueExecution();
153 phpLoop.notifyWait();
154 } catch (IOException e) {
155 PHPDebugCorePlugin.log(e);
160 protected IPHPDebugTarget getDebugTarget() {
164 public void setDebugTarget(IPHPDebugTarget debugTarget) {
165 this.debugTarget = debugTarget;
166 debugTarget.setPHPDBGProxy(this);
169 public PHPVariable[] readVariables(PHPStackFrame frame) {
171 return DBGInt.getVariables(frame);
172 } catch (IOException ioex) {
173 ioex.printStackTrace();
174 throw new RuntimeException(ioex.getMessage());
175 } catch (DebugException ex) {
176 ex.printStackTrace();
177 throw new RuntimeException(ex.getMessage());
181 public PHPVariable[] readInstanceVariables(PHPVariable variable) {
183 return DBGInt.getInstVars(variable);
184 } catch (DebugException ex) {
185 ex.printStackTrace();
186 throw new RuntimeException(ex.getMessage());
191 public void readStepOverEnd(PHPStackFrame stackFrame) {
194 phpLoop.notifyWait();
195 } catch (Exception e) {
196 PHPDebugCorePlugin.log(e);
200 public void readStepReturnEnd(PHPStackFrame stackFrame) {
203 phpLoop.notifyWait();
204 } catch (Exception e) {
205 PHPDebugCorePlugin.log(e);
209 public void readStepIntoEnd(PHPStackFrame stackFrame) {
212 phpLoop.notifyWait();
213 } catch (Exception e) {
214 PHPDebugCorePlugin.log(e);
219 public PHPStackFrame[] readFrames(PHPThread thread) {
221 //this.println("th " + thread.getId() + " ; f ");
222 //return new FramesReader(getMultiReaderStrategy()).readFrames(thread);
224 //} catch (IOException e) {
225 // PHPDebugCorePlugin.log(e);
232 public void closeSocket() throws IOException {
233 if (socket != null) {
238 public void closeServerSocket() throws IOException {
239 if (server != null) {
244 public int getPort() {
248 class PHPLoop extends Thread {
249 private boolean shouldStop;
253 this.setName("PHPDebuggerLoop");
256 public synchronized void setShouldStop() {
260 public synchronized void notifyWait() {
266 char[] buf= new char[16];
268 long interval= 1000*20;
270 PHPStackFrame[] StackList;
272 //System.out.println("Waiting for breakpoints.");
274 socket = server.accept();
275 //System.out.println("Accepted! : " + socket.toString());
276 } catch (IOException e) {
277 PHPDebugCorePlugin.log(e);
281 PHPMainThread= new PHPThread(getDebugTarget(), getPort());
282 PHPMainThread.setName("Thread [main]");
284 while((getDebugTarget() == null) && (timeout < 100)) {
288 // Be sure debug target is set
289 PHPMainThread.setDebugTarget(getDebugTarget());
290 getDebugTarget().addThread(PHPMainThread);
291 setDBGInterface(new PHPDBGInterface(getReader(), getOutputStream(), thisProxy));
293 DBGInt.waitResponse(1000);
294 DBGInt.flushAllPackets();
296 // Check version and session ID
298 DBGInt.continueExecution();
300 while (!shouldStop) {
301 DBGInt.waitResponse(interval);
302 DBGInt.flushAllPackets();
304 if (DBGInt.BPUnderHit != 0) {
305 StackList= DBGInt.getStackList();
306 if(StackList.length > 0) {
307 for(i=0; i < StackList.length; i++) {
308 StackList[i].setThread(PHPMainThread);
309 if(DBGInt.getModByNo(StackList[i].getModNo()).equals("")) {
310 DBGInt.getSourceTree();
312 StackList[i].setFile(DBGInt.getModByNo(StackList[i].getModNo()));
314 PHPMainThread.setStackFrames(StackList);
317 PHPMainThread.suspend();
323 if(PHPMainThread.isTerminated() || getDebugTarget().getProcess().isTerminated()) break;
325 } catch (Exception ex) {
326 PHPDebugCorePlugin.log(ex);
327 System.out.println(ex);
330 getDebugTarget().terminate();
333 } catch (IOException e) {
334 PHPDebugCorePlugin.log(e);
337 //System.out.println("Socket loop finished.");