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.model;
14 import org.eclipse.core.resources.IMarkerDelta;
15 import org.eclipse.debug.core.DebugEvent;
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.debug.core.DebugPlugin;
18 import org.eclipse.debug.core.IBreakpointManager;
19 import org.eclipse.debug.core.IDebugEventSetListener;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.ILaunchListener;
22 import org.eclipse.debug.core.model.IDebugTarget;
23 import org.eclipse.debug.core.model.IBreakpoint;
24 import org.eclipse.debug.core.model.IMemoryBlock;
25 import org.eclipse.debug.core.model.IProcess;
26 import org.eclipse.debug.core.model.IThread;
28 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
29 import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
32 * Debug target for PHP debug model.
34 public class PHPDebugTarget implements IPHPDebugTarget, ILaunchListener, IDebugEventSetListener {
36 private IProcess process;
37 private boolean isTerminated;
38 private ILaunch launch;
39 private PHPThread[] threads;
40 private PHPDBGProxy phpDBGProxy;
42 public PHPDebugTarget(ILaunch launch, IProcess process) {
44 this.process = process;
45 this.threads = new PHPThread[0];
46 IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
47 manager.addBreakpointListener(this);
48 DebugPlugin.getDefault().addDebugEventListener(this);
52 protected synchronized void initialize() {
53 DebugEvent ev = new DebugEvent(this, DebugEvent.CREATE);
54 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
57 public void addThread(PHPThread phpThread) {
59 PHPThread[] updatedThreads = new PHPThread[threads.length + 1];
61 for(i=0; i < threads.length; i++) {
62 updatedThreads[i] = threads[i];
64 updatedThreads[i] = phpThread;
65 threads = updatedThreads;
70 private void fireChangeEvent() {
71 DebugEvent ev = new DebugEvent(this, DebugEvent.CHANGE);
72 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
75 protected PHPThread getThreadById(int id) {
76 for (int i = 0; i < threads.length; i++) {
77 if (threads[i].getId() == id) {
84 public IThread[] getThreads() {
88 public boolean hasThreads() throws DebugException {
89 return threads.length > 0;
92 public String getName() throws DebugException {
93 return "PHP Debugger at localhost:" + getPHPDBGProxy().getPort();
96 public boolean supportsBreakpoint(IBreakpoint arg0) {
100 public String getModelIdentifier() {
101 return PHPDebugCorePlugin.getUniqueIdentifier();
104 public IDebugTarget getDebugTarget() {
108 public ILaunch getLaunch() {
112 public boolean canTerminate() {
113 return !isTerminated;
116 public boolean isTerminated() {
120 public void terminate() {
122 this.threads = new PHPThread[0];
127 public boolean canResume() {
131 public boolean canSuspend() {
135 public boolean isSuspended() {
139 public void resume() throws DebugException {
142 public void suspend() throws DebugException {
145 public void breakpointAdded(IBreakpoint breakpoint) {
146 this.getPHPDBGProxy().addBreakpoint(breakpoint) ;
149 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta arg1) {
150 this.getPHPDBGProxy().removeBreakpoint(breakpoint) ;
153 public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta arg1) {
154 // is called e.g. after a line has been inserted before a breakpoint
155 // but then the debugger is out of sync with the file anyway, so debugging
156 // should be stopped here.
159 public boolean canDisconnect() {
163 public void disconnect() throws DebugException {
166 public boolean isDisconnected() {
170 public boolean supportsStorageRetrieval() {
174 public IMemoryBlock getMemoryBlock(long arg0, long arg1) throws DebugException {
178 public Object getAdapter(Class arg0) {
182 public IProcess getProcess() {
186 public void setProcess(IProcess process) {
187 this.process = process;
190 public PHPDBGProxy getPHPDBGProxy() {
194 public void setPHPDBGProxy(PHPDBGProxy phpDBGProxy) {
195 this.phpDBGProxy = phpDBGProxy;
199 * @see ILaunchListener#launchRemoved(ILaunch)
201 public void launchRemoved(ILaunch launch) {
202 if (!isTerminated()) {
205 if (launch.equals(getLaunch())) {
206 // This target has been deregistered, but it hasn't successfully terminated.
207 // Update internal state to reflect that it is disconnected
213 * @see ILaunchListener#launchAdded(ILaunch)
215 public void launchAdded(ILaunch launch) {
219 * @see ILaunchListener#launchChanged(ILaunch)
221 public void launchChanged(ILaunch launch) {
225 * When a debug target or process terminates, terminate DBG Proxy.
227 * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
229 public void handleDebugEvents(DebugEvent[] events) {
230 for (int i = 0; i < events.length; i++) {
231 DebugEvent event = events[i];
232 if (event.getKind() == DebugEvent.TERMINATE) {
233 Object source = event.getSource();
234 if (source instanceof PHPDebugTarget || source instanceof IDebugTarget) {
235 getPHPDBGProxy().stop();
236 } else if(source instanceof IProcess) {
237 if(getDebugTarget().getProcess() == (IProcess)source) {
238 getPHPDBGProxy().stop();