Skip to main content
summaryrefslogtreecommitdiffstats
blob: 63cfc09cc8627c74891524e2ad539e258d56faaa (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*******************************************************************************
 * Copyright (c) 2000, 2012 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Wind River Systems - Adapter to use with DSF
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.ui.sourcelookup;


import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.cdt.dsf.concurrent.ThreadSafe;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.debug.service.IRunControl;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.debug.service.IStack;
import org.eclipse.cdt.dsf.debug.service.IStack.IFrameDMContext;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationPresentation;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * This class tracks instruction pointer contexts for a single DSF session.
 */
@ThreadSafe
class InstructionPointerManager {

	/**
	 * Current instruction pointer annotation type.
	 */
	private static final String ID_CURRENT_IP= IDebugUIConstants.ANNOTATION_TYPE_INSTRUCTION_POINTER_CURRENT;
	
    /**
	 * Secondary instruction pointer annotation type.
	 */
	private static final String ID_SECONDARY_IP= IDebugUIConstants.ANNOTATION_TYPE_INSTRUCTION_POINTER_SECONDARY;

	/**
     * Editor annotation object for instruction pointers.
     */
    static class IPAnnotation extends Annotation implements IAnnotationPresentation {
        
        /** The image for this annotation. */
        private Image fImage;
        
        /** Frame DMC that this IP is for **/
        private IStack.IFrameDMContext fFrame;
        
        /**
         * Constructs an instruction pointer image.
         * 
         * @param frame stack frame the instruction pointer is associated with
         * @param annotationType the type of annotation to display (annotation identifier)
         * @param text the message to display with the annotation as hover help
         * @param image the image used to display the annotation
         */
        IPAnnotation(IStack.IFrameDMContext frame, String annotationType, String text, Image image) {
            super(annotationType, false, text);
            fFrame = frame;
            fImage = image;
        }
        
        /**
         * Returns this annotation's image.
         * 
         * @return image
         */
        protected Image getImage() {
            return fImage;
        }
        
        /* (non-Javadoc)
         * @see java.lang.Object#equals(java.lang.Object)
         */
        @Override
        public boolean equals(Object other) {
            if (other instanceof IPAnnotation) {
                return fFrame.equals(((IPAnnotation)other).fFrame);            
            }
            return false;
        }
        
        /* (non-Javadoc)
         * @see java.lang.Object#hashCode()
         */
        @Override
        public int hashCode() {
            return fFrame.hashCode();
        }

        /*
         * @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
         */
        @Override
		public void paint(GC gc, Canvas canvas, Rectangle bounds) {
            Rectangle imageBounds = fImage.getBounds();
            gc.drawImage(fImage, bounds.x + (bounds.width - imageBounds.width) / 2 , bounds.y + (bounds.height - imageBounds.height) / 2);
        }

        /*
         * @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
         */
        @Override
		public int getLayer() {
            return 5;
        }

    }
    
    /**
     * Represents the context for a single instruction pointer.  This is a convenience class
     * used to store the three objects that comprise an instruction pointer 'context' so it
     * can be stored in collections.
     */
    static class AnnotationWrapper {

        /** The text editor for this context. */
        private ITextEditor fTextEditor;
        
        /** Stack frame that this annotation is for */
        private IStack.IFrameDMContext fFrameDmc;
        
        /** The vertical ruler annotation for this context. */
        private Annotation fAnnotation;

        AnnotationWrapper(ITextEditor textEditor, Annotation annotation, IStack.IFrameDMContext frameDmc) {
            fTextEditor = textEditor;
            fAnnotation = annotation;
            fFrameDmc = frameDmc;
        }
        
        /**
         * @see java.lang.Object#equals(java.lang.Object)
         */
        @Override
        public boolean equals(Object other) {
            if (other instanceof AnnotationWrapper) {
                AnnotationWrapper otherContext = (AnnotationWrapper) other;
                return getAnnotation().equals(otherContext.getAnnotation());
            }
            return false;
        }
        
        /**
         * @see java.lang.Object#hashCode()
         */
        @Override
        public int hashCode() {
            return getAnnotation().hashCode();
        }

        ITextEditor getTextEditor() { return fTextEditor; }
        Annotation getAnnotation() { return fAnnotation; }
        IStack.IFrameDMContext getFrameDMC() { return fFrameDmc; }
    }
    
	/**
	 * Mapping of IDebugTarget objects to (mappings of IThread objects to lists of instruction
	 * pointer contexts).
	 */
	private final List<AnnotationWrapper> fAnnotationWrappers;

	/**
	 * For customized instruction pointer presentation.
	 */
	private final IInstructionPointerPresentation fPresentation;

	/**
	 * Clients must not instantiate this class.
	 */
	public InstructionPointerManager() {
		this(null);
	}

	/**
	 * Clients must not instantiate this class.
	 * 
	 * @param presentation
	 *            the custom instruction pointer presentation or
	 *            <code>null</code> to use the default presentation
	 */
	public InstructionPointerManager(IInstructionPointerPresentation presentation) {
		fPresentation = presentation;
        fAnnotationWrappers = new LinkedList<AnnotationWrapper>();
	}
		
	/**
	 * Add an instruction pointer annotation in the specified editor for the 
	 * specified stack frame.
	 */
	public void addAnnotation(ITextEditor textEditor, IStack.IFrameDMContext frame, Position position, boolean isTopFrame) {
		
		IDocumentProvider docProvider = textEditor.getDocumentProvider();
		IEditorInput editorInput = textEditor.getEditorInput();
        // If there is no annotation model, there's nothing more to do
        IAnnotationModel annModel = docProvider.getAnnotationModel(editorInput);
        if (annModel == null) {
            return;
        }        

        if (isTopFrame) {
        	// remove other top-frame IP annotation(s) for this execution-context
        	removeAnnotations(DMContexts.getAncestorOfType(frame.getParents()[0], IExecutionDMContext.class));
        }
        Annotation annotation = createAnnotation(textEditor, frame); 
        
		// Add the annotation at the position to the editor's annotation model.
		annModel.removeAnnotation(annotation);
		annModel.addAnnotation(annotation, position);	
		
		// Add to list of existing wrappers
		synchronized (fAnnotationWrappers) {
			fAnnotationWrappers.add(new AnnotationWrapper(textEditor, annotation, frame));
		}
	}

	private Annotation createAnnotation(ITextEditor editorPart, IStack.IFrameDMContext frame) {
        String id = null;
        String text = null;
        Image image = null;
		if (fPresentation != null) {
			Annotation annotation = fPresentation.getInstructionPointerAnnotation(editorPart, frame);
			if (annotation != null) {
				return annotation;
			}
			id = fPresentation.getInstructionPointerAnnotationType(editorPart, frame);
			if (id == null) {
				image = fPresentation.getInstructionPointerImage(editorPart, frame);
			}
			text = fPresentation.getInstructionPointerText(editorPart, frame);
		}
		if (id == null) {
	        if (frame.getLevel() == 0) {
	        	id = ID_CURRENT_IP;
	        	if (text == null) {
	        		text = Messages.IPAnnotation_primary;
	        	}
	        	if (image == null) {
	        		image = DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_INSTRUCTION_POINTER_TOP);
	        	}
	        } else {
	            id = ID_SECONDARY_IP;
	            if (text == null) {
	            	text = Messages.IPAnnotation_secondary;
	            }
	            if (image == null) {
	            	image = DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_INSTRUCTION_POINTER);
	            }
	        }
	        return new IPAnnotation(frame, id, text, image);
		}
		return new Annotation(id, false, text);
	}
	
	/**
	 * Remove all annotations associated with the specified debug target that this class
	 * is tracking.
	 */
	public void removeAnnotations(IRunControl.IExecutionDMContext execDmc) {
		// Retrieve the mapping of threads to context lists
        synchronized(fAnnotationWrappers) {
            for (Iterator<AnnotationWrapper> wrapperItr = fAnnotationWrappers.iterator(); wrapperItr.hasNext();) {
                AnnotationWrapper wrapper = wrapperItr.next();
                if (DMContexts.isAncestorOf(wrapper.getFrameDMC(), execDmc)) {
                    removeAnnotation(wrapper.getTextEditor(), wrapper.getAnnotation());
                    wrapperItr.remove();
                }
            }
        }
	}

	/**
	 * Remove all top-frame annotations associated with the specified debug target that this class
	 * is tracking.
	 */
	public void removeTopFrameAnnotations(IRunControl.IExecutionDMContext execDmc) {
		// Retrieve the mapping of threads to context lists
        synchronized(fAnnotationWrappers) {
            for (Iterator<AnnotationWrapper> wrapperItr = fAnnotationWrappers.iterator(); wrapperItr.hasNext();) {
                AnnotationWrapper wrapper = wrapperItr.next();
                final IFrameDMContext frameDmc= wrapper.getFrameDMC();
				if (DMContexts.isAncestorOf(frameDmc, execDmc) && frameDmc.getLevel() == 0) {
                    removeAnnotation(wrapper.getTextEditor(), wrapper.getAnnotation());
                    wrapperItr.remove();
                }
            }
        }
	}

    /** Removes all annotations tracked by this manager */
    public void removeAllAnnotations() {
        synchronized(fAnnotationWrappers) {
            for (AnnotationWrapper wrapper : fAnnotationWrappers) {
                removeAnnotation(wrapper.getTextEditor(), wrapper.getAnnotation());
            }   
            fAnnotationWrappers.clear();
        }
    }
    
	/**
	 * Remove the specified annotation from the specified text editor.
	 */
	private void removeAnnotation(ITextEditor textEditor, Annotation annotation) {
		IDocumentProvider docProvider = textEditor.getDocumentProvider();
		if (docProvider != null) {
			IAnnotationModel annotationModel = docProvider.getAnnotationModel(textEditor.getEditorInput());
			if (annotationModel != null) {
				annotationModel.removeAnnotation(annotation);
			}
		}
	}
}

Back to the top