1 /***********************************************************************************************************************************
2 * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
3 * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: IBM Corporation - Initial implementation Vicente Fernando - www.alfersoft.com.ar Christian Perkonig - remote debug
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.debug.core;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.InputStreamReader;
13 import java.io.OutputStream;
14 import java.net.ServerSocket;
15 import java.net.Socket;
16 import java.net.SocketTimeoutException;
18 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
19 import net.sourceforge.phpdt.internal.debug.core.logview.LogView;
20 import net.sourceforge.phpdt.internal.debug.core.model.IPHPDebugTarget;
21 import net.sourceforge.phpdt.internal.debug.core.model.PHPStackFrame;
22 import net.sourceforge.phpdt.internal.debug.core.model.PHPThread;
23 import net.sourceforge.phpdt.internal.debug.core.model.PHPVariable;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.debug.core.DebugException;
29 import org.eclipse.debug.core.DebugPlugin;
30 import org.eclipse.debug.core.model.IBreakpoint;
31 import org.eclipse.ui.IViewPart;
32 import org.eclipse.ui.IWorkbenchPage;
34 public class PHPDBGProxy {
36 private ServerSocket server = null;
38 private Socket socket;
40 private BufferedReader reader = null;
42 private PHPDBGInterface DBGInt = null;
44 private IPHPDebugTarget debugTarget = null;
46 private PHPLoop phpLoop;
48 private PHPThread PHPMainThread;
50 private PHPDBGProxy thisProxy = null;
54 private boolean remote;
56 private IPath remoteSourcePath;
58 public PHPDBGProxy() {
62 public PHPDBGProxy(boolean remote, String remoteSourcePath) {
65 this.remoteSourcePath = new Path(remoteSourcePath);
74 phpLoop.setShouldStop();
76 DBGInt.setShouldStop();
79 getDebugTarget().getProcess().terminate();
80 } catch (DebugException e) {
87 protected ServerSocket getServerSocket() throws IOException {
94 protected void createServerSocket() {
95 // port = SocketUtil.findUnusedLocalPort("localhost", 10001, 10101);
98 PHPDebugCorePlugin.log(5, "Cannot find free port!!!!");
102 if (server == null) {
103 server = new ServerSocket(port);
104 //System.out.println("ServerSocket on port: " + port);
106 } catch (IOException e) {
108 PHPDebugCorePlugin.log(e);
113 public Socket getSocket() throws IOException {
117 protected void setDBGInterface(PHPDBGInterface DBGInt) {
118 this.DBGInt = DBGInt;
121 public BufferedReader getReader() throws IOException {
122 if (reader == null) {
123 reader = new BufferedReader(new InputStreamReader(this.getSocket().getInputStream(), "ISO8859_1"));
128 public BufferedReader getReader(Socket socket) throws IOException {
130 return new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO8859_1"));
135 public OutputStream getOutputStream() throws IOException {
136 return this.getSocket().getOutputStream();
139 protected void setBreakPoints() throws IOException, CoreException {
140 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
141 for (int i = 0; i < breakpoints.length; i++) {
142 addBreakpoint(breakpoints[i]);
146 public void addBreakpoint(IBreakpoint breakpoint) {
151 PHPLineBreakpoint phpLBP;
152 if (breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
154 phpLBP = (PHPLineBreakpoint) breakpoint;
155 // bpNo= DBGInt.addBreakpoint(phpLBP.getMarker().getResource().getLocation().toOSString(), phpLBP.getLineNumber());
157 filename = remoteSourcePath.append(phpLBP.getMarker().getResource().getProjectRelativePath());
159 filename = phpLBP.getMarker().getResource().getLocation();
160 bpNo = DBGInt.addBreakpoint(filename.toOSString(), phpLBP.getLineNumber());
161 phpLBP.setDBGBpNo(bpNo);
163 } catch (IOException e) {
164 PHPDebugCorePlugin.log(e);
166 } catch (CoreException e) {
167 PHPDebugCorePlugin.log(e);
172 public void removeBreakpoint(IBreakpoint breakpoint) {
176 PHPLineBreakpoint phpLBP;
177 if (breakpoint.getModelIdentifier() == PHPDebugCorePlugin.getUniqueIdentifier()) {
178 phpLBP = (PHPLineBreakpoint) breakpoint;
181 filename = remoteSourcePath.append(phpLBP.getMarker().getResource().getProjectRelativePath());
183 filename = phpLBP.getMarker().getResource().getLocation();
184 // bpNo= DBGInt.addBreakpoint(filename.toOSString(), phpLBP.getLineNumber());
185 DBGInt.removeBreakpoint(filename.toOSString(), phpLBP.getLineNumber(), phpLBP.getDBGBpNo());
187 } catch (IOException e) {
188 PHPDebugCorePlugin.log(e);
190 } catch (CoreException e) {
191 PHPDebugCorePlugin.log(e);
196 public void phpLoopNotify() {
197 phpLoop.notifyWait();
200 public void startPHPLoop() {
201 phpLoop = new PHPLoop();
205 public void resume() {
207 DBGInt.continueExecution();
208 phpLoop.notifyWait();
209 } catch (IOException e) {
210 PHPDebugCorePlugin.log(e);
215 public void pause() {
217 DBGInt.pauseExecution();
218 } catch (IOException e) {
219 PHPDebugCorePlugin.log(e);
224 protected IPHPDebugTarget getDebugTarget() {
228 public void setDebugTarget(IPHPDebugTarget debugTarget) {
229 this.debugTarget = debugTarget;
230 debugTarget.setPHPDBGProxy(this);
233 public PHPVariable[] readVariables(PHPStackFrame frame) {
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 PHPVariable[] eval(PHPStackFrame frame, String evalString) {
247 return DBGInt.evalBlock(frame, evalString);
248 // return DBGInt.getVariables(frame);
249 } catch (IOException ioex) {
250 ioex.printStackTrace();
251 throw new RuntimeException(ioex.getMessage());
252 } catch (DebugException ex) {
253 ex.printStackTrace();
254 throw new RuntimeException(ex.getMessage());
258 public void readStepOverEnd(PHPStackFrame stackFrame) {
261 phpLoop.notifyWait();
262 } catch (Exception e) {
263 PHPDebugCorePlugin.log(e);
267 public void readStepReturnEnd(PHPStackFrame stackFrame) {
270 phpLoop.notifyWait();
271 } catch (Exception e) {
272 PHPDebugCorePlugin.log(e);
276 public void readStepIntoEnd(PHPStackFrame stackFrame) {
279 phpLoop.notifyWait();
280 } catch (Exception e) {
281 PHPDebugCorePlugin.log(e);
286 * public PHPStackFrame[] readFrames(PHPThread thread) { //try { //this.println("th " + thread.getId() + " ; f "); //return new
287 * FramesReader(getMultiReaderStrategy()).readFrames(thread); return null; //} catch (IOException e) { //
288 * PHPDebugCorePlugin.log(e); // return null; //}
292 public void closeSocket() throws IOException {
293 if (socket != null) {
298 public void closeServerSocket() throws IOException {
299 if (server != null) {
304 public int getPort() {
308 class PHPLoop extends Thread {
309 private boolean shouldStop;
313 this.setName("PHPDebuggerLoop");
316 public synchronized void setShouldStop() {
320 public synchronized void notifyWait() {
326 char[] buf = new char[16];
328 long interval = 200; // 200ms
330 PHPStackFrame[] StackList;
331 boolean endFile = false;
332 boolean newconnect = false;
333 Socket newSocket = null;
334 PHPDBGInterface newDBGInt;
337 // synchronized (this) {
341 PHPMainThread = new PHPThread(getDebugTarget(), getPort());
342 PHPMainThread.setName("Thread [main]");
345 // while ((getDebugTarget() == null) && (timeout < 100)) {
349 // Be sure debug target is set
350 // PHPMainThread.setDebugTarget(getDebugTarget());
351 getDebugTarget().addThread(PHPMainThread);
353 //System.out.println("Waiting for breakpoints.");
354 while (!shouldStop) {
357 newSocket = server.accept();
358 //System.out.println("Accepted! : " + socket.toString());
359 } catch (SocketTimeoutException e) {
360 // no one wants to connect
362 } catch (IOException e) {
363 PHPDebugCorePlugin.log(e);
369 server.setSoTimeout(1);
370 newDBGInt = new PHPDBGInterface(getReader(newSocket), newSocket.getOutputStream(), thisProxy);
371 newDBGInt.waitResponse(1000);
372 newDBGInt.flushAllPackets();
373 // Check version and session ID
374 if ((DBGInt == null) || (DBGInt.getSID() == newDBGInt.getSID())) {
378 } catch (IOException e) {
379 PHPDebugCorePlugin.log(e);
384 DBGInt.continueExecution();
386 newDBGInt.continueExecution();
391 if (DBGInt.waitResponse(interval)) {
393 DBGInt.flushAllPackets();
395 if (DBGInt.BPUnderHit != 0) {
396 StackList = DBGInt.getStackList();
397 if (StackList.length > 0) {
398 for (i = 0; i < StackList.length; i++) {
399 StackList[i].setThread(PHPMainThread);
400 if (DBGInt.getModByNo(StackList[i].getModNo()).equals("")) {
401 DBGInt.getSourceTree();
403 StackList[i].setFile(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()) {
427 } catch (Exception ex) {
428 PHPDebugCorePlugin.log(ex);
429 System.out.println(ex);
432 getDebugTarget().terminate();
435 } catch (IOException e) {
436 PHPDebugCorePlugin.log(e);
439 //System.out.println("Socket loop finished.");