1 package net.mograsim.plugin.launch;
3 import org.eclipse.core.resources.IMarkerDelta;
4 import org.eclipse.core.runtime.IStatus;
5 import org.eclipse.core.runtime.PlatformObject;
6 import org.eclipse.core.runtime.Status;
7 import org.eclipse.debug.core.DebugEvent;
8 import org.eclipse.debug.core.DebugException;
9 import org.eclipse.debug.core.DebugPlugin;
10 import org.eclipse.debug.core.ILaunch;
11 import org.eclipse.debug.core.ILaunchConfiguration;
12 import org.eclipse.debug.core.model.IBreakpoint;
13 import org.eclipse.debug.core.model.IDebugElement;
14 import org.eclipse.debug.core.model.IDebugTarget;
15 import org.eclipse.debug.core.model.IMemoryBlock;
16 import org.eclipse.debug.core.model.IMemoryBlockExtension;
17 import org.eclipse.debug.core.model.IMemoryBlockRetrievalExtension;
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.logic.model.LogicExecuter;
23 import net.mograsim.machine.Machine;
24 import net.mograsim.machine.MachineDefinition;
25 import net.mograsim.plugin.MograsimActivator;
27 public class MachineDebugTarget extends PlatformObject implements IDebugTarget, IMemoryBlockRetrievalExtension
29 private final ILaunch launch;
30 private final Machine machine;
31 private final LogicExecuter exec;
33 private boolean running;
35 public MachineDebugTarget(ILaunch launch, MachineDefinition machineDefinition)
38 this.machine = machineDefinition.createNew();
39 this.exec = new LogicExecuter(machine.getTimeline());
41 exec.startLiveExecution();
44 getLaunch().addDebugTarget(this);
48 public Machine getMachine()
54 public String getName() throws DebugException
56 return "Mograsim machine \"" + machine.getDefinition().getId() + '"';
60 public String getModelIdentifier()
62 return MograsimActivator.PLUGIN_ID;
66 public IDebugTarget getDebugTarget()
72 public ILaunch getLaunch()
77 public double getExecutionSpeed()
79 return exec.getSpeedFactor();
82 public void setExecutionSpeed(double speed)
84 if (getExecutionSpeed() != speed)
85 exec.setSpeedFactor(speed);
89 public boolean isSuspended()
91 return exec.isPaused();
95 public boolean canSuspend()
97 return !isTerminated() && !isSuspended();
101 public void suspend() throws DebugException
104 throwDebugException("Can't suspend a terminated MachineProcess");
106 throwDebugException("Can't suspend a suspended MachineProcess");
108 exec.pauseLiveExecution();
109 fireSuspendEvent(DebugEvent.CLIENT_REQUEST);
113 public boolean canResume()
115 return !isTerminated() && isSuspended();
119 public void resume() throws DebugException
122 throwDebugException("Can't resume a terminated MachineProcess");
124 throwDebugException("Can't resume a non-suspended MachineProcess");
126 exec.unpauseLiveExecution();
127 fireResumeEvent(DebugEvent.CLIENT_REQUEST);
131 public boolean isTerminated()
137 public boolean canTerminate()
139 return !isTerminated();
143 public void terminate() throws DebugException
148 exec.stopLiveExecution();
150 fireTerminateEvent();
154 public boolean supportsBreakpoint(IBreakpoint breakpoint)
160 public void breakpointAdded(IBreakpoint breakpoint)
162 // ignore; we don't support breakpoints
166 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta)
168 // ignore; we don't support breakpoints
172 public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta)
174 // ignore; we don't support breakpoints
178 public boolean isDisconnected()
184 public boolean canDisconnect()
190 public void disconnect() throws DebugException
192 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.NOT_SUPPORTED,
193 "Can't disconnect from a MachineDebugTarget", null));
197 public boolean supportsStorageRetrieval()
202 @SuppressWarnings("deprecation") // TODO can we throw a DebugException instead?
204 public IMemoryBlock getMemoryBlock(long startAddress, long length) throws DebugException
206 return new MainMemoryBlock(this, startAddress, length);
210 public IMemoryBlockExtension getExtendedMemoryBlock(String expression, Object context) throws DebugException
212 return new MainMemoryBlockExtension(this, expression, context);
216 public IProcess getProcess()
222 public boolean hasThreads() throws DebugException
228 public IThread[] getThreads() throws DebugException
230 return new IThread[0];
233 @SuppressWarnings("unchecked")
235 public <T> T getAdapter(Class<T> adapter)
237 if (adapter == IDebugElement.class)
240 // leave this here; maybe we implement IStepFilters someday
241 if (adapter == IStepFilters.class)
242 if (this instanceof IStepFilters)
243 return (T) getDebugTarget();
245 if (adapter == IDebugTarget.class)
246 return (T) getDebugTarget();
248 if (adapter == ILaunch.class)
249 return (T) getLaunch();
252 if (adapter == ILaunchConfiguration.class)
253 return (T) getLaunch().getLaunchConfiguration();
255 return super.getAdapter(adapter);
259 * Fires a creation event for this debug element.
261 private void fireCreationEvent()
263 fireEvent(new DebugEvent(this, DebugEvent.CREATE));
267 * Fires a resume for this debug element with the specified detail code.
269 * @param detail detail code for the resume event, such as <code>DebugEvent.STEP_OVER</code>
271 private void fireResumeEvent(int detail)
273 fireEvent(new DebugEvent(this, DebugEvent.RESUME, detail));
277 * Fires a suspend event for this debug element with the specified detail code.
279 * @param detail detail code for the suspend event, such as <code>DebugEvent.BREAKPOINT</code>
281 private void fireSuspendEvent(int detail)
283 fireEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
287 * Fires a terminate event for this debug element.
289 private void fireTerminateEvent()
291 fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
295 * Fires a debug event.
297 * @param event debug event to fire
299 private static void fireEvent(DebugEvent event)
301 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { event });
304 private static void throwDebugException(String message) throws DebugException
306 throw new DebugException(
307 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED, message, null));