1
/*******************************************************************************
2
* Copyright (c) 2011 Mentor Graphics and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Mentor Graphics - Initial API and implementation
10
*******************************************************************************/
12
package org.eclipse.cdt.dsf.debug.ui.actions;
15
import java.util.ArrayList;
16
import java.util.Iterator;
17
import java.util.List;
19
import org.eclipse.cdt.core.IAddress;
20
import org.eclipse.cdt.dsf.debug.internal.ui.disassembly.provisional.DisassemblySelection;
21
import org.eclipse.cdt.dsf.debug.internal.ui.disassembly.provisional.IDisassemblyPart;
22
import org.eclipse.cdt.dsf.debug.internal.ui.disassembly.provisional.IDisassemblySelection;
23
import org.eclipse.core.filesystem.URIUtil;
24
import org.eclipse.core.resources.IMarker;
25
import org.eclipse.core.resources.IResource;
26
import org.eclipse.core.resources.ResourcesPlugin;
27
import org.eclipse.core.runtime.CoreException;
28
import org.eclipse.core.runtime.IPath;
29
import org.eclipse.debug.core.DebugPlugin;
30
import org.eclipse.debug.core.model.IBreakpoint;
31
import org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension;
32
import org.eclipse.jface.text.BadLocationException;
33
import org.eclipse.jface.text.IDocument;
34
import org.eclipse.jface.text.ITextSelection;
35
import org.eclipse.jface.text.Position;
36
import org.eclipse.jface.text.source.IAnnotationModel;
37
import org.eclipse.jface.viewers.ISelection;
38
import org.eclipse.ui.IWorkbenchPart;
39
import org.eclipse.ui.texteditor.SimpleMarkerAnnotation;
42
* Base class for toggle breakpoint targets for the disassembly part.
45
public abstract class AbstractDisassemblyBreakpointsTarget implements IToggleBreakpointsTargetExtension {
48
* @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleLineBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
50
public void toggleLineBreakpoints( IWorkbenchPart part, ISelection selection ) throws CoreException {
51
assert part instanceof IDisassemblyPart && selection instanceof ITextSelection;
53
if ( !(selection instanceof IDisassemblySelection) ) {
54
selection = new DisassemblySelection( (ITextSelection)selection, (IDisassemblyPart)part );
56
IDisassemblySelection disassemblySelection = (IDisassemblySelection)selection;
57
int line = disassemblySelection.getStartLine();
58
IBreakpoint[] bp = getBreakpointsAtLine( (IDisassemblyPart)part, line );
59
if ( bp == null || bp.length == 0 ) {
60
insertBreakpoint( disassemblySelection );
63
for( int i = 0; i < bp.length; i++ ) {
70
* @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleLineBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
72
public boolean canToggleLineBreakpoints( IWorkbenchPart part, ISelection selection ) {
73
return part instanceof IDisassemblyPart && selection instanceof ITextSelection;
77
* @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleMethodBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
79
public void toggleMethodBreakpoints( IWorkbenchPart part, ISelection selection ) throws CoreException {
83
* @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleMethodBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
85
public boolean canToggleMethodBreakpoints( IWorkbenchPart part, ISelection selection ) {
90
* @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#toggleWatchpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
92
public void toggleWatchpoints( IWorkbenchPart part, ISelection selection ) throws CoreException {
96
* @see org.eclipse.debug.ui.actions.IToggleBreakpointsTarget#canToggleWatchpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
98
public boolean canToggleWatchpoints( IWorkbenchPart part, ISelection selection ) {
103
* @see org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension#toggleBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
105
public void toggleBreakpoints( IWorkbenchPart part, ISelection selection ) throws CoreException {
106
toggleLineBreakpoints( part, selection );
110
* @see org.eclipse.debug.ui.actions.IToggleBreakpointsTargetExtension#canToggleBreakpoints(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
112
public boolean canToggleBreakpoints( IWorkbenchPart part, ISelection selection ) {
113
return canToggleLineBreakpoints( part, selection );
116
protected abstract void createLineBreakpoint( String sourceHandle, IResource resource, int lineNumber ) throws CoreException;
118
protected abstract void createAddressBreakpoint( IResource resource, IAddress address ) throws CoreException;
120
private IBreakpoint[] getBreakpointsAtLine( IDisassemblyPart part, int line ) {
121
List<IBreakpoint> breakpoints = new ArrayList<IBreakpoint>();
122
IAnnotationModel annotationModel = part.getTextViewer().getAnnotationModel();
123
IDocument document = part.getTextViewer().getDocument();
124
if ( annotationModel != null ) {
125
Iterator<?> iterator = annotationModel.getAnnotationIterator();
126
while( iterator.hasNext() ) {
127
Object object = iterator.next();
128
if ( object instanceof SimpleMarkerAnnotation ) {
129
SimpleMarkerAnnotation markerAnnotation = (SimpleMarkerAnnotation)object;
130
IMarker marker = markerAnnotation.getMarker();
132
if ( marker.isSubtypeOf( IBreakpoint.BREAKPOINT_MARKER ) ) {
133
Position position = annotationModel.getPosition( markerAnnotation );
134
int bpLine = document.getLineOfOffset( position.getOffset() );
135
if ( line == bpLine ) {
136
IBreakpoint breakpoint = DebugPlugin.getDefault().getBreakpointManager().getBreakpoint( marker );
137
if ( breakpoint != null ) {
138
breakpoints.add( breakpoint );
143
catch( CoreException e ) {
145
catch( BadLocationException e ) {
150
IBreakpoint[] breakpointsArray = new IBreakpoint[breakpoints.size()];
151
return breakpoints.toArray( breakpointsArray );
154
private void insertBreakpoint( IDisassemblySelection selection ) throws CoreException {
155
IAddress address = selection.getStartAddress();
156
if ( address == null ) {
159
URI fileUri = selection.getSourceLocationURI();
160
if ( fileUri != null ) {
161
String filePath = null;
162
IResource resource = selection.getSourceFile();
163
if ( resource != null ) {
164
final IPath location = resource.getLocation();
165
if ( location == null ) {
168
filePath = location.toOSString();
171
resource = ResourcesPlugin.getWorkspace().getRoot();
172
filePath = URIUtil.toPath( fileUri ).toOSString();
174
int srcLine = selection.getSourceLine();
175
createLineBreakpoint( filePath, resource, srcLine + 1 );
178
IResource resource = ResourcesPlugin.getWorkspace().getRoot();
179
createAddressBreakpoint( resource, address );