1 package net.mograsim.plugin.launch;
3 import java.util.Arrays;
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.core.runtime.PlatformObject;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.debug.core.DebugEvent;
9 import org.eclipse.debug.core.DebugException;
10 import org.eclipse.debug.core.DebugPlugin;
11 import org.eclipse.debug.core.ILaunch;
12 import org.eclipse.debug.core.model.IBreakpoint;
13 import org.eclipse.debug.core.model.IDebugTarget;
14 import org.eclipse.debug.core.model.IStackFrame;
15 import org.eclipse.debug.core.model.IThread;
17 import net.mograsim.machine.Machine;
18 import net.mograsim.plugin.MograsimActivator;
20 public class MachineThread extends PlatformObject implements IThread
22 private final MachineDebugTarget debugTarget;
23 private final MachineStackFrame stackFrame;
25 public MachineThread(MachineDebugTarget debugTarget)
27 this.debugTarget = debugTarget;
28 this.stackFrame = new MachineStackFrame(this);
30 DebugPlugin.getDefault().addDebugEventListener(es -> Arrays.stream(es).filter(e -> e.getSource() == debugTarget).forEach(e ->
34 case DebugEvent.RESUME:
35 fireResumeEvent(e.getDetail());
37 case DebugEvent.SUSPEND:
38 fireSuspendEvent(e.getDetail());
48 public String getModelIdentifier()
50 return MograsimActivator.PLUGIN_ID;
53 public Machine getMachine()
55 return debugTarget.getMachine();
59 public IDebugTarget getDebugTarget()
65 public ILaunch getLaunch()
67 return debugTarget.getLaunch();
71 public boolean canResume()
73 return debugTarget.canResume();
77 public boolean canSuspend()
79 return debugTarget.canSuspend();
83 public boolean isSuspended()
85 return debugTarget.isSuspended();
89 public void resume() throws DebugException
95 public void suspend() throws DebugException
97 debugTarget.suspend();
101 public boolean canStepInto()
106 // TODO step over sounds like single-step execution...
108 public boolean canStepOver()
114 public boolean canStepReturn()
120 public boolean isStepping()
126 public void stepInto() throws DebugException
128 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Can't step into for a machine thread"));
132 public void stepOver() throws DebugException
134 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Can't step over for a machine thread"));
138 public void stepReturn() throws DebugException
140 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Can't step return for a machine thread"));
144 public boolean canTerminate()
146 return debugTarget.canTerminate();
150 public boolean isTerminated()
152 return debugTarget.isTerminated();
156 public void terminate() throws DebugException
158 debugTarget.terminate();
162 public IStackFrame[] getStackFrames() throws DebugException
164 return isSuspended() ? new IStackFrame[] { stackFrame } : new IStackFrame[0];
168 public boolean hasStackFrames() throws DebugException
170 // required by Eclipse: see javadoc for org.eclipse.debug.core.DebugEvent
171 return isSuspended();
175 public int getPriority() throws DebugException
181 public IStackFrame getTopStackFrame() throws DebugException
187 public String getName() throws DebugException
189 return "pseudo thread";
193 public IBreakpoint[] getBreakpoints()
195 return new IBreakpoint[0];
199 * Fires a creation event for this debug element.
201 private void fireCreationEvent()
203 fireEvent(new DebugEvent(this, DebugEvent.CREATE));
207 * Fires a resume for this debug element with the specified detail code.
209 * @param detail detail code for the resume event, such as <code>DebugEvent.STEP_OVER</code>
211 private void fireResumeEvent(int detail)
213 fireEvent(new DebugEvent(this, DebugEvent.RESUME, detail));
217 * Fires a suspend event for this debug element with the specified detail code.
219 * @param detail detail code for the suspend event, such as <code>DebugEvent.BREAKPOINT</code>
221 private void fireSuspendEvent(int detail)
223 fireEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
227 * Fires a debug event.
229 * @param event debug event to fire
231 private static void fireEvent(DebugEvent event)
233 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { event });