Excercise Answers
Exercise 1
org.eclipse.debug.examples.ping/plugin.xml
5:
6: <extension
7: point="org.eclipse.debug.ui.launchConfigurationTypeImages">
8: <launchConfigurationTypeImage
9: configTypeID="ping.launchType"
10: icon="icons/ping.gif"
11: id="ping.typeImage">
12: </launchConfigurationTypeImage>
13: </extension>
org.eclipse.debug.examples.ping/org/eclipse/debug/examples/ping/PingLaunchDelegate.java
26: /**
27: * Launch configuration attribute key. The address to ping - can be an IP address or domain name.
28: */
29: public static final String PING_ADDRESS = "PING_ADDRESS";
30:
31: /* (non-Javadoc)
32: * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
33: */
34: public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
35: String address = configuration.getAttribute(PING_ADDRESS, "localhost");
36: String[] cmdLine = new String[]{"ping", address};
37: Process systemProcess = DebugPlugin.exec(cmdLine, null);
38: DebugPlugin.newProcess(launch, systemProcess, "ping " + address);
39: }
org.eclipse.debug.examples.ping/org/eclipse/debug/examples/ping/PingTab.java
79: /* (non-Javadoc)
80: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
81: */
82: public void initializeFrom(ILaunchConfiguration configuration) {
83: try {
84: fAddress.setText(configuration.getAttribute(PingLaunchDelegate.PING_ADDRESS, ""));
85: } catch (CoreException e) {
86: setErrorMessage(e.getMessage());
87: }
88: }
90: /* (non-Javadoc)
91: * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
92: */
93: public void performApply(ILaunchConfigurationWorkingCopy configuration) {
94: String address = fAddress.getText().trim();
95: if (address.length() == 0) {
96: address = null;
97: }
98: configuration.setAttribute(PingLaunchDelegate.PING_ADDRESS, address);
99: }
Exercise 2
org.eclipse.debug.examples.counter/org/eclipse/debug/examples/counter/model/CounterDebugTarget.java
org.eclipse.debug.examples.counter/org/eclipse/debug/examples/counter/model/CounterThread.java
79: /**
80: * Simulates a step over - executes the next instruction and fires events.
81: */
82: class StepOver implements Runnable {
83: /* (non-Javadoc)
84: * @see java.lang.Runnable#run()
85: */
86: public void run() {
87: synchronized (CounterThread.this) {
88: fState = STEPPING;
89: }
90: fireResumeEvent(DebugEvent.STEP_OVER);
91: int event = executeNextInstruction();
92: int detail = DebugEvent.UNSPECIFIED;
93: synchronized (CounterThread.this) {
94: // update state
95: switch (event) {
96: case EVENT_BREAKPOINT:
97: fState = SUSPENDED;
98: detail = DebugEvent.BREAKPOINT;
99: break;
100: case EVENT_NONE:
101: fState = SUSPENDED;
102: detail = DebugEvent.STEP_END;
103: break;
104: case EVENT_TERMINATED:
105: fState = TERMINATED;
106: break;
107: case EVENT_SUSPENDED:
108: fState = SUSPENDED;
109: detail = DebugEvent.CLIENT_REQUEST;
110: break;
111: }
112: }
113: switch (fState) {
114: case SUSPENDED:
115: fireSuspendEvent(detail);
116: break;
117: case TERMINATED:
118: fireTerminateEvent();
119: CounterDebugTarget target = (CounterDebugTarget)getDebugTarget();
120: target.terminated();
121: break;
122: }
123: }
124: }
209: /* (non-Javadoc)
210: * @see org.eclipse.debug.core.model.IThread#getStackFrames()
211: */
212: public IStackFrame[] getStackFrames() throws DebugException {
213: synchronized (this) {
214: if (isSuspended()) {
215: IStackFrame[] frames = new IStackFrame[fCount];
216: for (int i = 0; i < frames.length; i++) {
217: frames[i] = new CounterStackFrame(this, fCount - i);
218:
219: }
220: return frames;
221: }
222: }
223: return new IStackFrame[0];
224: }
226: /* (non-Javadoc)
227: * @see org.eclipse.debug.core.model.IThread#getTopStackFrame()
228: */
229: public IStackFrame getTopStackFrame() throws DebugException {
230: synchronized (this) {
231: if (isSuspended()) {
232: return new CounterStackFrame(this, fCount);
233: }
234: }
235: return null;
236: }
238: /* (non-Javadoc)
239: * @see org.eclipse.debug.core.model.IThread#hasStackFrames()
240: */
241: public boolean hasStackFrames() throws DebugException {
242: return isSuspended();
243: }
Exercise 3
org.eclipse.debug.examples.counter/org/eclipse/debug/examples/counter/breakpoints/CounterToggleBreakpointsTarget.java
82: /* (non-Javadoc)
83: * @see org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension#toggleBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
84: */
85: public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
86: if (selection instanceof IStructuredSelection) {
87: IStructuredSelection ss = (IStructuredSelection) selection;
88: if (ss.size() == 1) {
89: Object element = ss.getFirstElement();
90: if (element instanceof CounterDebugElement) {
91: Shell shell = part.getSite().getShell();
92: InputDialog dialog = new InputDialog(shell, "Create Limit Breakpoint", "Th&reshold:", "1", new IInputValidator() {
93: public String isValid(String newText) {
94: try {
95: int value = Integer.parseInt(newText);
96: if (value < 1 || value > 100) {
97: return "Enter a number between 0 and 101";
98: }
99: } catch (NumberFormatException e) {
100: return "Enter a number between 0 and 101";
101: }
102: return null;
103: }
104: });
105: int dialogResult = dialog.open();
106: if (dialogResult == Window.OK) {
107: String text = dialog.getValue();
108: int limit = Integer.parseInt(text);
109: try {
110: LimitBreakpoint breakpoint = new LimitBreakpoint(limit);
111: DebugPlugin.getDefault().getBreakpointManager().addBreakpoint(breakpoint);
112: } catch (CoreException e) {
113: }
114: }
115: }
116: }
117: }
118: }
org.eclipse.debug.examples.counter/org/eclipse/debug/examples/counter/model/CounterDebugTarget.java
48: /**
49: * Constructs the counting target with a single thread and installs deferred breakpoints.
50: */
51: public CounterDebugTarget(ILaunch launch) {
52: super(null);
53: fLaunch = launch;
54: fThread = new CounterThread(this);
55: IBreakpointManager manager = DebugPlugin.getDefault().getBreakpointManager();
56: manager.addBreakpointListener(this);
57: fireCreationEvent();
58: IBreakpoint[] breakpoints = manager.getBreakpoints(getModelIdentifier());
59: for (int i = 0; i < breakpoints.length; i++) {
60: breakpointAdded(breakpoints[i]);
61: }
62: }
172: /* (non-Javadoc)
173: * @see org.eclipse.debug.core.IBreakpointListener#breakpointAdded(org.eclipse.debug.core.model.IBreakpoint)
174: */
175: public void breakpointAdded(IBreakpoint breakpoint) {
176: if (breakpoint instanceof LimitBreakpoint) {
177: synchronized (fBreakpoints) {
178: fBreakpoints.add(breakpoint);
179: }
180: }
181: }
189: /* (non-Javadoc)
190: * @see org.eclipse.debug.core.IBreakpointListener#breakpointRemoved(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
191: */
192: public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) {
193: if (breakpoint instanceof LimitBreakpoint) {
194: synchronized (fBreakpoints) {
195: fBreakpoints.remove(breakpoint);
196: }
197: }
198: }
org.eclipse.debug.examples.counter/org/eclipse/debug/examples/counter/model/CounterThread.java
370: /**
371: * Executes the next instruction (sleeps and increments a counter) and returns any event
372: * that occurred during the execution:
373: * <ul>
374: * <li>EVENT_TERMINATED - execution was terminated</li>
375: * <li>EVENT_SUSPENDED - execution was suspended</li>
376: * <li>EVENT_BREAKPOINT - execution hit a breakpoint</li>
377: * <li>EVENT_NONE - instruction completed normally</li>
378: * </ul>
379: *
380: * @return event code
381: * @throws InterruptedException
382: */
383: private int executeNextInstruction() {
384: fBreakpoint = null;
385: if (fRequest == TERMINATED) {
386: return EVENT_TERMINATED;
387: }
388: try {
389: Thread.sleep(INSTRUCTION_TIME);
390: } catch (InterruptedException e) {
391: switch (fRequest) {
392: case TERMINATED:
393: return EVENT_TERMINATED;
394: case SUSPENDED:
395: return EVENT_SUSPENDED;
396: }
397: }
398: fCount++;
399: if (fCount > MAX_COUNTER) {
400: fCount = MIN_COUNTER;
401: }
402: LimitBreakpoint[] breakpoints = ((CounterDebugTarget)getDebugTarget()).getInstalledBreakpoints();
403: for (int i = 0; i < breakpoints.length; i++) {
404: LimitBreakpoint breakpoint = breakpoints[i];
405: try {
406: if (breakpoint.isEnabled() && fCount == breakpoint.getLimit()) {
407: fBreakpoint = breakpoint;
408: return EVENT_BREAKPOINT;
409: }
410: } catch (CoreException e) {
411: }
412: }
413: return EVENT_NONE;
414: }
Exercise 4
org.eclipse.debug.examples.pda.core/plugin.xml
21: <extension
22: point="org.eclipse.debug.core.sourceLocators">
23: <sourceLocator
24: class="org.eclipse.debug.examples.core.pda.sourcelookup.PDASourceLocator"
25: name="PDA Source Locator"
26: id="pda.sourceLocator"/>
27: </extension>
org.eclipse.debug.examples.pda.core/src/org/eclipse/debug/examples/core/pda/sourcelookup/PDASourceLocator.java
30: public Object getSourceElement(IStackFrame stackFrame) {
31: if (!(stackFrame instanceof PDAStackFrame)) {
32: return null;
33: }
34: IPath sourcePath = ((PDAStackFrame)stackFrame).getSourcePath();
35: URI sourceUri = URIUtil.toURI(sourcePath.makeAbsolute());
36: IFile[] sourceFiles = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(sourceUri);
37: if (sourceFiles == null || sourceFiles.length == 0) {
38: return null;
39: }
40: return sourceFiles[0];
41: }
org.eclipse.debug.examples.pda.ui/src/org/eclipse/debug/examples/ui/pda/presentation/PDAModelPresentation.java
170: /* (non-Javadoc)
171: * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(java.lang.Object)
172: */
173: public IEditorInput getEditorInput(Object element) {
174: if (element instanceof IFile) {
175: return new FileEditorInput((IFile)element);
176: }
177: return null;
178: }
179:
180: /* (non-Javadoc)
181: * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(org.eclipse.ui.IEditorInput, java.lang.Object)
182: */
183: public String getEditorId(IEditorInput input, Object element) {
184: if (element instanceof IFile) {
185: return "pda.editor";
186: }
187: return null;
188: }
Exercise 5
org.eclipse.debug.examples.pda.core/src/org/eclipse/debug/examples/core/pda/model/PDAValue.java
58: /* (non-Javadoc)
59: * @see org.eclipse.debug.core.model.IValue#getVariables()
60: */
61: public IVariable[] getVariables() throws DebugException {
62: PDAStackFrame frame = fVariable.getStackFrame();
63: PDAListResult result = (PDAListResult) sendCommand(
64: new PDAChildrenCommand(frame.getThreadIdentifier(), frame.getIdentifier(), fVariable.getName()) );
65:
66: IVariable[] children = new IVariable[result.fValues.length];
67: for(int i = 0; i < result.fValues.length; i++) {
68: children[i] = new PDAVariable(frame, result.fValues[i]);
69: }
70: return children;
71: }