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();
56 getDebugTarget().getProcess().terminate();
57 } catch (DebugException e) {
63 protected ServerSocket getServerSocket() throws IOException {
70 protected void createServerSocket() {
71 port = SocketUtil.findUnusedLocalPort("localhost", 10001, 10101);
73 PHPDebugCorePlugin.log(5, "Cannot find free port!!!!");
78 server = new ServerSocket(port);
79 //System.out.println("ServerSocket on port: " + port);
81 } catch (IOException e) {
83 PHPDebugCorePlugin.log(e);
88 public Socket getSocket() throws IOException {
92 protected void setDBGInterface(PHPDBGInterface DBGInt) {
96 public BufferedReader getReader() throws IOException {
98 reader = new BufferedReader(new InputStreamReader(this.getSocket().getInputStream(), "ISO8859_1"));
103 public OutputStream getOutputStream() throws IOException {
104 return this.getSocket().getOutputStream();
107 protected void setBreakPoints() throws IOException, CoreException {
108 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
109 for (int i = 0; i < breakpoints.length; i++) {
110 addBreakpoint(breakpoints[i]);
114 public void addBreakpoint(IBreakpoint breakpoint) {
115 if (DBGInt == null) return;
118 PHPLineBreakpoint phpLBP;
119 if(breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getDefault().getDescriptor().getUniqueIdentifier()) {
120 phpLBP= (PHPLineBreakpoint)breakpoint;
121 bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
122 phpLBP.setDBGBpNo(bpNo);
124 } catch (IOException e) {
125 PHPDebugCorePlugin.log(e);
127 } catch (CoreException e) {
128 PHPDebugCorePlugin.log(e);
133 public void removeBreakpoint(IBreakpoint breakpoint) {
134 if (DBGInt == null) return;
136 PHPLineBreakpoint phpLBP;
137 if(breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getDefault().getDescriptor().getUniqueIdentifier()) {
138 phpLBP= (PHPLineBreakpoint)breakpoint;
139 DBGInt.removeBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
141 } catch (IOException e) {
142 PHPDebugCorePlugin.log(e);
144 } catch (CoreException e) {
145 PHPDebugCorePlugin.log(e);
150 public void startPHPLoop() {
151 phpLoop = new PHPLoop();
155 public void resume() {
157 DBGInt.continueExecution();
158 phpLoop.notifyWait();
159 } catch (IOException e) {
160 PHPDebugCorePlugin.log(e);
165 public void pause() {
167 DBGInt.pauseExecution();
168 } catch (IOException e) {
169 PHPDebugCorePlugin.log(e);
173 protected IPHPDebugTarget getDebugTarget() {
177 public void setDebugTarget(IPHPDebugTarget debugTarget) {
178 this.debugTarget = debugTarget;
179 debugTarget.setPHPDBGProxy(this);
182 public PHPVariable[] readVariables(PHPStackFrame frame) {
184 return DBGInt.getVariables(frame);
185 } catch (IOException ioex) {
186 ioex.printStackTrace();
187 throw new RuntimeException(ioex.getMessage());
188 } catch (DebugException ex) {
189 ex.printStackTrace();
190 throw new RuntimeException(ex.getMessage());
194 public PHPVariable[] readInstanceVariables(PHPVariable variable) {
196 return DBGInt.getInstVars(variable);
197 } catch (DebugException ex) {
198 ex.printStackTrace();
199 throw new RuntimeException(ex.getMessage());
204 public void readStepOverEnd(PHPStackFrame stackFrame) {
207 phpLoop.notifyWait();
208 } catch (Exception e) {
209 PHPDebugCorePlugin.log(e);
213 public void readStepReturnEnd(PHPStackFrame stackFrame) {
216 phpLoop.notifyWait();
217 } catch (Exception e) {
218 PHPDebugCorePlugin.log(e);
222 public void readStepIntoEnd(PHPStackFrame stackFrame) {
225 phpLoop.notifyWait();
226 } catch (Exception e) {
227 PHPDebugCorePlugin.log(e);
232 public PHPStackFrame[] readFrames(PHPThread thread) {
234 //this.println("th " + thread.getId() + " ; f ");
235 //return new FramesReader(getMultiReaderStrategy()).readFrames(thread);
237 //} catch (IOException e) {
238 // PHPDebugCorePlugin.log(e);
245 public void closeSocket() throws IOException {
246 if (socket != null) {
251 public void closeServerSocket() throws IOException {
252 if (server != null) {
257 public int getPort() {
261 class PHPLoop extends Thread {
262 private boolean shouldStop;
266 this.setName("PHPDebuggerLoop");
269 public synchronized void setShouldStop() {
273 public synchronized void notifyWait() {
279 char[] buf= new char[16];
281 long interval= 1000*20;
283 PHPStackFrame[] StackList;
285 //System.out.println("Waiting for breakpoints.");
287 socket = server.accept();
288 //System.out.println("Accepted! : " + socket.toString());
289 } catch (IOException e) {
290 PHPDebugCorePlugin.log(e);
294 PHPMainThread= new PHPThread(getDebugTarget(), getPort());
295 PHPMainThread.setName("Thread [main]");
297 while((getDebugTarget() == null) && (timeout < 100)) {
301 // Be sure debug target is set
302 PHPMainThread.setDebugTarget(getDebugTarget());
303 getDebugTarget().addThread(PHPMainThread);
304 setDBGInterface(new PHPDBGInterface(getReader(), getOutputStream(), thisProxy));
306 DBGInt.waitResponse(1000);
307 DBGInt.flushAllPackets();
309 // Check version and session ID
311 DBGInt.continueExecution();
313 while (!shouldStop) {
314 DBGInt.waitResponse(interval);
315 DBGInt.flushAllPackets();
317 if (DBGInt.BPUnderHit != 0) {
318 StackList= DBGInt.getStackList();
319 if(StackList.length > 0) {
320 for(i=0; i < StackList.length; i++) {
321 StackList[i].setThread(PHPMainThread);
322 if(DBGInt.getModByNo(StackList[i].getModNo()).equals("")) {
323 DBGInt.getSourceTree();
325 StackList[i].setFile(DBGInt.getModByNo(StackList[i].getModNo()));
327 PHPMainThread.setStackFrames(StackList);
330 PHPMainThread.suspend();
336 if(PHPMainThread.isTerminated() || getDebugTarget().getProcess().isTerminated()) break;
338 } catch (Exception ex) {
339 PHPDebugCorePlugin.log(ex);
340 System.out.println(ex);
343 getDebugTarget().terminate();
346 } catch (IOException e) {
347 PHPDebugCorePlugin.log(e);
350 //System.out.println("Socket loop finished.");