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.SocketUtil;
30 import net.sourceforge.phpdt.internal.debug.core.PHPDBGInterface;
31 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
32 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
34 public class PHPDBGProxy {
36 private ServerSocket server= null;
37 private Socket socket;
38 private BufferedReader reader= null;
39 private PHPDBGInterface DBGInt= null;
40 private IPHPDebugTarget debugTarget;
41 private PHPLoop phpLoop;
42 private PHPThread PHPMainThread;
45 public PHPDBGProxy() {
54 phpLoop.setShouldStop();
55 if(DBGInt != null) DBGInt.setShouldStop();
59 protected ServerSocket getServerSocket() throws IOException {
66 protected void createServerSocket() {
67 port = SocketUtil.findUnusedLocalPort("localhost", 10001, 10101);
69 PHPDebugCorePlugin.log(5, "Cannot find free port!!!!");
74 server = new ServerSocket(port);
75 System.out.println("ServerSocket on port: " + port);
77 } catch (IOException e) {
79 PHPDebugCorePlugin.log(e);
84 protected Socket getSocket() throws IOException {
88 protected void setDBGInterface(PHPDBGInterface DBGInt) {
92 public BufferedReader getReader() throws IOException {
94 reader = new BufferedReader(new InputStreamReader(this.getSocket().getInputStream(), "ISO8859_1"));
99 public OutputStream getOutputStream() throws IOException {
100 return this.getSocket().getOutputStream();
103 protected void setBreakPoints() throws IOException, CoreException {
104 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
105 for (int i = 0; i < breakpoints.length; i++) {
106 addBreakpoint(breakpoints[i]);
110 public void addBreakpoint(IBreakpoint breakpoint) {
111 if (DBGInt == null) return;
114 PHPLineBreakpoint phpLBP;
115 if(breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getDefault().getDescriptor().getUniqueIdentifier()) {
116 phpLBP= (PHPLineBreakpoint)breakpoint;
117 bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
118 phpLBP.setDBGBpNo(bpNo);
120 } catch (IOException e) {
121 PHPDebugCorePlugin.log(e);
123 } catch (CoreException e) {
124 PHPDebugCorePlugin.log(e);
129 public void removeBreakpoint(IBreakpoint breakpoint) {
130 if (DBGInt == null) return;
132 PHPLineBreakpoint phpLBP;
133 if(breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getDefault().getDescriptor().getUniqueIdentifier()) {
134 phpLBP= (PHPLineBreakpoint)breakpoint;
135 DBGInt.removeBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
137 } catch (IOException e) {
138 PHPDebugCorePlugin.log(e);
140 } catch (CoreException e) {
141 PHPDebugCorePlugin.log(e);
146 public void startPHPLoop() {
147 phpLoop = new PHPLoop();
151 public void resume(PHPThread thread) {
153 DBGInt.continueExecution();
154 phpLoop.notifyWait();
155 } catch (IOException e) {
156 PHPDebugCorePlugin.log(e);
161 protected IPHPDebugTarget getDebugTarget() {
165 public void setDebugTarget(IPHPDebugTarget debugTarget) {
166 this.debugTarget = debugTarget;
167 debugTarget.setPHPDBGProxy(this);
170 public PHPVariable[] readVariables(PHPStackFrame frame) {
172 return DBGInt.getVariables(frame);
173 } catch (IOException ioex) {
174 ioex.printStackTrace();
175 throw new RuntimeException(ioex.getMessage());
176 } catch (DebugException ex) {
177 ex.printStackTrace();
178 throw new RuntimeException(ex.getMessage());
182 public PHPVariable[] readInstanceVariables(PHPVariable variable) {
184 return DBGInt.getInstVars(variable);
185 } catch (DebugException ex) {
186 ex.printStackTrace();
187 throw new RuntimeException(ex.getMessage());
192 public void readStepOverEnd(PHPStackFrame stackFrame) {
195 phpLoop.notifyWait();
196 } catch (Exception e) {
197 PHPDebugCorePlugin.log(e);
201 public void readStepReturnEnd(PHPStackFrame stackFrame) {
204 phpLoop.notifyWait();
205 } catch (Exception e) {
206 PHPDebugCorePlugin.log(e);
210 public void readStepIntoEnd(PHPStackFrame stackFrame) {
213 phpLoop.notifyWait();
214 } catch (Exception e) {
215 PHPDebugCorePlugin.log(e);
220 public PHPStackFrame[] readFrames(PHPThread thread) {
222 //this.println("th " + thread.getId() + " ; f ");
223 //return new FramesReader(getMultiReaderStrategy()).readFrames(thread);
225 //} catch (IOException e) {
226 // PHPDebugCorePlugin.log(e);
233 public void closeSocket() throws IOException {
234 if (socket != null) {
239 public void closeServerSocket() throws IOException {
240 if (server != null) {
245 public int getPort() {
249 class PHPLoop extends Thread {
250 private boolean shouldStop;
254 this.setName("PHPDebuggerLoop");
257 public synchronized void setShouldStop() {
261 public synchronized void notifyWait() {
267 char[] buf= new char[16];
269 long interval= 1000*20;
271 PHPStackFrame[] StackList;
273 System.out.println("Waiting for breakpoints.");
275 socket = server.accept();
276 System.out.println("Accepted! : " + socket.toString());
277 } catch (IOException e) {
278 PHPDebugCorePlugin.log(e);
282 PHPMainThread= new PHPThread(getDebugTarget(), 100);
283 PHPMainThread.setName("Thread [main]");
284 getDebugTarget().addThread(PHPMainThread);
285 setDBGInterface(new PHPDBGInterface(getReader(), getOutputStream()));
287 DBGInt.flushAllPackets();
289 // Check version and session ID
292 DBGInt.continueExecution();
294 while (!shouldStop) {
295 DBGInt.waitResponse(interval);
296 DBGInt.flushAllPackets();
298 if (DBGInt.BPUnderHit != 0) {
299 StackList= DBGInt.getStackList();
300 if(StackList.length > 0) {
301 for(i=0; i < StackList.length; i++) {
302 StackList[i].setThread(PHPMainThread);
304 PHPMainThread.setStackFrames(StackList);
306 PHPMainThread.suspend();
312 if(PHPMainThread.isTerminated() || getDebugTarget().getProcess().isTerminated()) break;
314 } catch (Exception ex) {
315 PHPDebugCorePlugin.log(ex);
316 System.out.println(ex);
318 getDebugTarget().terminate();
322 } catch (IOException e) {
323 PHPDebugCorePlugin.log(e);
326 System.out.println("Socket loop finished.");