1 package net.mograsim.plugin.launch;
3 import java.util.Arrays;
5 import org.eclipse.core.resources.IMarkerDelta;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.core.runtime.PlatformObject;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.debug.core.DebugEvent;
10 import org.eclipse.debug.core.DebugException;
11 import org.eclipse.debug.core.DebugPlugin;
12 import org.eclipse.debug.core.ILaunch;
13 import org.eclipse.debug.core.ILaunchConfiguration;
14 import org.eclipse.debug.core.model.IBreakpoint;
15 import org.eclipse.debug.core.model.IDebugElement;
16 import org.eclipse.debug.core.model.IDebugTarget;
17 import org.eclipse.debug.core.model.IMemoryBlock;
18 import org.eclipse.debug.core.model.IProcess;
19 import org.eclipse.debug.core.model.IStepFilters;
20 import org.eclipse.debug.core.model.IThread;
22 import net.mograsim.machine.Machine;
23 import net.mograsim.plugin.MograsimActivator;
25 public class MachineDebugTarget extends PlatformObject implements IDebugTarget
27 private final MachineProcess process;
29 public MachineDebugTarget(MachineProcess process)
31 this.process = process;
33 DebugPlugin.getDefault().addDebugEventListener(es -> Arrays.stream(es).filter(e -> e.getSource() == process).forEach(e ->
37 case DebugEvent.RESUME:
38 fireResumeEvent(e.getDetail());
40 case DebugEvent.SUSPEND:
41 fireSuspendEvent(e.getDetail());
43 case DebugEvent.TERMINATE:
51 getLaunch().addDebugTarget(this);
55 public Machine getMachine()
57 return process.getMachine();
61 public String getName() throws DebugException
63 return process.getName();
67 public String getModelIdentifier()
69 return MograsimActivator.PLUGIN_ID;
73 public IDebugTarget getDebugTarget()
79 public ILaunch getLaunch()
81 return process.getLaunch();
84 public void setExecutionSpeed(double speed)
86 process.setExecutionSpeed(speed);
90 public boolean isSuspended()
92 return process.isSuspended();
96 public boolean canSuspend()
98 return process.canSuspend();
102 public void suspend() throws DebugException
108 public boolean canResume()
110 return process.canResume();
114 public void resume() throws DebugException
120 public boolean isTerminated()
122 return process.isTerminated();
126 public boolean canTerminate()
128 return process.canTerminate();
132 public void terminate() throws DebugException
138 public boolean supportsBreakpoint(IBreakpoint breakpoint)
144 public void breakpointAdded(IBreakpoint breakpoint)
146 // ignore; we don't support breakpoints
150 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta)
152 // ignore; we don't support breakpoints
156 public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta)
158 // ignore; we don't support breakpoints
162 public boolean isDisconnected()
168 public boolean canDisconnect()
174 public void disconnect() throws DebugException
176 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.NOT_SUPPORTED,
177 "Can't disconnect from a MachineDebugTarget", null));
181 public boolean supportsStorageRetrieval()
187 public IMemoryBlock getMemoryBlock(long startAddress, long length) throws DebugException
189 return new MainMemoryBlock(this, startAddress, length);
193 public MachineProcess getProcess()
199 public boolean hasThreads() throws DebugException
205 public IThread[] getThreads() throws DebugException
207 return new IThread[0];
210 @SuppressWarnings("unchecked")
212 public <T> T getAdapter(Class<T> adapter)
214 if (adapter == IDebugElement.class)
217 // leave this here; maybe we implement IStepFilters someday
218 if (adapter == IStepFilters.class)
219 if (this instanceof IStepFilters)
220 return (T) getDebugTarget();
222 if (adapter == IDebugTarget.class)
223 return (T) getDebugTarget();
225 if (adapter == ILaunch.class)
226 return (T) getLaunch();
228 if (adapter == IProcess.class)
229 return (T) getProcess();
232 if (adapter == ILaunchConfiguration.class)
233 return (T) getLaunch().getLaunchConfiguration();
235 return super.getAdapter(adapter);
239 * Fires a creation event for this debug element.
241 private void fireCreationEvent()
243 fireEvent(new DebugEvent(this, DebugEvent.CREATE));
247 * Fires a resume for this debug element with the specified detail code.
249 * @param detail detail code for the resume event, such as <code>DebugEvent.STEP_OVER</code>
251 private void fireResumeEvent(int detail)
253 fireEvent(new DebugEvent(this, DebugEvent.RESUME, detail));
257 * Fires a suspend event for this debug element with the specified detail code.
259 * @param detail detail code for the suspend event, such as <code>DebugEvent.BREAKPOINT</code>
261 private void fireSuspendEvent(int detail)
263 fireEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
267 * Fires a terminate event for this debug element.
269 private void fireTerminateEvent()
271 fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
275 * Fires a debug event.
277 * @param event debug event to fire
279 private static void fireEvent(DebugEvent event)
281 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { event });