Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cce489456e5600fa8692d7328047359e3f57a2f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*******************************************************************************
 * Copyright (c) 2008, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Anton Leherbauer (Wind River Systems) - initial API and implementation
 *     Patrick Chuong (Texas Instruments) - bug 300053
 *******************************************************************************/

package org.eclipse.cdt.dsf.debug.internal.ui.disassembly.model;

import java.math.BigInteger;
import java.util.Iterator;

import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.debug.core.model.ICAddressBreakpoint;
import org.eclipse.cdt.debug.core.model.ICLineBreakpoint;
import org.eclipse.cdt.debug.internal.ui.disassembly.dsf.AddressRangePosition;
import org.eclipse.cdt.debug.internal.ui.disassembly.dsf.LabelPosition;
import org.eclipse.cdt.dsf.debug.internal.ui.disassembly.provisional.IBreakpointLocationProvider;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointListener;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.ILineBreakpoint;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.texteditor.MarkerAnnotation;
import org.eclipse.ui.texteditor.SimpleMarkerAnnotation;

/**
 * Annotation model for breakpoints in the disassembly.
 * Works only with {@link DisassemblyDocument}.
 */
public class BreakpointsAnnotationModel extends AnnotationModel implements IBreakpointListener, IDocumentListener {
	
	private Runnable fCatchup;

	@Override
	public void connect(IDocument document) {
		super.connect(document);
		if (document instanceof DisassemblyDocument) {
			final IBreakpointManager bpMgr= DebugPlugin.getDefault().getBreakpointManager();
			addBreakpoints(bpMgr.getBreakpoints());
			bpMgr.addBreakpointListener(this);
			document.addDocumentListener(this);
		}
	}

	@Override
	public void disconnect(IDocument document) {
		if (document instanceof DisassemblyDocument) {
			final IBreakpointManager bpMgr= DebugPlugin.getDefault().getBreakpointManager();
			bpMgr.removeBreakpointListener(this);
			document.removeDocumentListener(this);
			fCatchup= null;
		}
		super.disconnect(document);
	}

	private void catchupWithBreakpoints() {
		removeAllAnnotations(false);
		final IBreakpointManager bpMgr= DebugPlugin.getDefault().getBreakpointManager();
		addBreakpoints(bpMgr.getBreakpoints());
	}

	private void addBreakpoints(IBreakpoint[] breakpoints) {
		for (IBreakpoint breakpoint : breakpoints) {
			addBreakpointAnnotation(breakpoint, false);
		}
		fireModelChanged();
	}

	/*
	 * @see org.eclipse.debug.core.IBreakpointListener#breakpointAdded(org.eclipse.debug.core.model.IBreakpoint)
	 */
	public void breakpointAdded(IBreakpoint breakpoint) {
		addBreakpointAnnotation(breakpoint, true);
	}

	/*
	 * @see org.eclipse.debug.core.IBreakpointListener#breakpointChanged(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
	 */
	public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) {
		Annotation a= findAnnotation(breakpoint.getMarker());
		if (a != null) {
			if (a instanceof SimpleMarkerAnnotation) {
				((SimpleMarkerAnnotation)a).update();
			}
			synchronized (getLockObject()) {
				getAnnotationModelEvent().annotationChanged(a);
			}
			fireModelChanged();
		} else {
			addBreakpointAnnotation(breakpoint, true);
		}
	}

	/*
	 * @see org.eclipse.debug.core.IBreakpointListener#breakpointRemoved(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
	 */
	public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) {
		Annotation a= findAnnotation(breakpoint.getMarker());
		if (a != null) {
			removeAnnotation(a, true);
		}
	}

	@SuppressWarnings("unchecked")
	private Annotation findAnnotation(IMarker marker) {
		for (Iterator<SimpleMarkerAnnotation> it= getAnnotationIterator(false); it.hasNext();) {
			SimpleMarkerAnnotation a= it.next();
			if (a.getMarker().equals(marker)) {
				return a;
			}
		}
		return null;
	}

	private void addBreakpointAnnotation(IBreakpoint breakpoint, boolean fireEvent) {
		final IMarker marker= breakpoint.getMarker();
		if (marker == null) {
			return;
		}
		try {
			Position position = createPositionFromBreakpoint(breakpoint);
			if (position != null) {
				addAnnotation(new MarkerAnnotation(marker), position, fireEvent);
			}
		} catch (CoreException exc) {
			// ignore problems accessing attributes
		} catch (BadLocationException exc) {
			// ignore wrong positions
		}
	}
	
	private Position createPositionFromBreakpoint(IBreakpoint breakpoint) throws CoreException {
		IBreakpointLocationProvider locationProvider = (IBreakpointLocationProvider) breakpoint.getAdapter(IBreakpointLocationProvider.class);
			
		/* if there is a location provider, than use the provider to retrieve the location */
		if (locationProvider != null) {						

			/* if there is source info, than create a source line position */
			String sourceFile = locationProvider.getSourceFile(breakpoint);
			if (sourceFile != null) {
				int lineNumber = locationProvider.getLineNumber(breakpoint) - 1;
				return createPositionFromSourceLine(sourceFile, lineNumber);
			
			} else {
				/* if there is label info, than create a label position */
				IAddress labelAddress = locationProvider.getLabelAddress(breakpoint);
				if (labelAddress != null) {
					return createPositionFromLabel(labelAddress.getValue());
				
				/* Otherwise, create an address position */
				} else {				
				
					// Discussion with Anton - comment #5 (Bug 300053)
					//
					// Since there can only be one annotation per marker and in order to support multiple 
					// annotations per breakpoint, we need a specialized annotation type.
					//
					// So for now, we only create an annotation for the first valid address. We can add 
					// support for multiple annotations per breakpoint when it's needed.
					IAddress[] addresses = locationProvider.getAddresses(breakpoint);
					for (int i = 0; addresses != null && i < addresses.length; ++i) {
						BigInteger address = addresses[i].getValue();
						Position position = createPositionFromAddress(address);
						if (position != null)
							return position;
					}
				}
			}
			
		/* otherwise, use legacy ICBreakpoint location info */
		} else {
			if (breakpoint instanceof ICAddressBreakpoint) {
				ICAddressBreakpoint addressBreakpoint= (ICAddressBreakpoint) breakpoint;
				return createPositionFromAddress(decodeAddress(addressBreakpoint.getAddress()));
			} else if (breakpoint instanceof ILineBreakpoint) {
				ILineBreakpoint lineBreakpoint= (ILineBreakpoint) breakpoint;
				Position position= null;
				final int lineNumber= lineBreakpoint.getLineNumber() - 1;
				final IMarker marker= breakpoint.getMarker();
				if (marker.getResource().getType() == IResource.FILE) {
					position= createPositionFromSourceLine((IFile) marker.getResource(), lineNumber);
				} else if (breakpoint instanceof ICLineBreakpoint) {
					ICLineBreakpoint cBreakpoint= (ICLineBreakpoint) breakpoint;
					position= createPositionFromSourceLine(cBreakpoint.getFileName(), lineNumber);
					if (position == null) {
						position= createPositionFromAddress(decodeAddress(cBreakpoint.getAddress()));
					}
				} else {
					String fileName= marker.getAttribute(ICLineBreakpoint.SOURCE_HANDLE, null);
					if (fileName != null) {
						position= createPositionFromSourceLine(fileName, lineNumber);
					}
				}
				return position;
			}
		}
		
		return null;
	}

	private Position createPositionFromSourceLine(String fileName, int lineNumber) {
		return getDisassemblyDocument().getSourcePosition(fileName, lineNumber);
	}

	private Position createPositionFromSourceLine(IFile file, int lineNumber) {
		return getDisassemblyDocument().getSourcePosition(file, lineNumber);
	}

	private Position createPositionFromAddress(BigInteger address) {
		if (address != null) {
			AddressRangePosition p= getDisassemblyDocument().getDisassemblyPosition(address);
			if (p != null && p.fValid) {
				return new Position(p.offset, p.length);
			}
		}
		return null;
	}
	
	private Position createPositionFromLabel(BigInteger address) {
		if (address != null) {
			LabelPosition p = getDisassemblyDocument().getLabelPosition(address);
			if (p != null && p.fValid) {
				return new Position(p.offset, p.length);
			}
		}
		return null;
	}

	private DisassemblyDocument getDisassemblyDocument() {
		return (DisassemblyDocument) fDocument;
	}

	/**
	 * Decode given string representation of a non-negative integer. A
	 * hexadecimal encoded integer is expected to start with <code>0x</code>.
	 * 
	 * @param string
	 *            decimal or hexadecimal representation of an non-negative integer
	 * @return address value as <code>BigInteger</code> or <code>null</code> in case of a <code>NumberFormatException</code>
	 */
	private static BigInteger decodeAddress(String string) {
		try {
			if (string.startsWith("0x")) { //$NON-NLS-1$
				return new BigInteger(string.substring(2), 16);
			}
			if (string.length() > 0) {
				return new BigInteger(string);
			}
		} catch (NumberFormatException nfe) {
			// don't propagate
		}
		return null;
	}

	/*
	 * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
	 */
	public void documentAboutToBeChanged(DocumentEvent event) {
	}

	/*
	 * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
	 */
	public void documentChanged(DocumentEvent event) {
		if (fCatchup == null && event.fText != null && event.fText.length() > 0) {
			fCatchup= new Runnable() {
				public void run() {
					if (fCatchup == this) {
						catchupWithBreakpoints();
						fCatchup= null;
					}
				}};
			Display.getCurrent().timerExec(50, fCatchup);
		}
	}

}

Back to the top