1
/*******************************************************************************
2
* Copyright (c) 2004, 2006 QNX Software Systems 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
* QNX Software Systems - Initial API and implementation
10
* Mike Caraman (Frescale) - https://bugs.eclipse.org/bugs/show_bug.cgi?id=210863
11
*******************************************************************************/
12
package org.eclipse.cdt.debug.internal.core.model;
15
import java.io.FileNotFoundException;
16
import java.io.FileReader;
17
import java.io.IOException;
18
import java.io.LineNumberReader;
20
import org.eclipse.cdt.core.IAddress;
21
import org.eclipse.cdt.core.IAddressFactory;
22
import org.eclipse.cdt.core.model.ITranslationUnit;
23
import org.eclipse.cdt.debug.core.cdi.model.ICDIInstruction;
24
import org.eclipse.cdt.debug.core.cdi.model.ICDIMixedInstruction;
25
import org.eclipse.cdt.debug.core.model.IAsmInstruction;
26
import org.eclipse.cdt.debug.core.model.IAsmSourceLine;
27
import org.eclipse.cdt.debug.core.model.ICStackFrame;
28
import org.eclipse.cdt.debug.core.model.IDisassembly;
29
import org.eclipse.cdt.debug.core.model.IDisassemblyBlock;
30
import org.eclipse.cdt.debug.core.model.IExecFileInfo;
31
import org.eclipse.cdt.debug.core.sourcelookup.ICSourceLocator;
32
import org.eclipse.core.resources.IFile;
33
import org.eclipse.core.resources.IStorage;
34
import org.eclipse.core.runtime.IAdaptable;
35
import org.eclipse.debug.core.model.ISourceLocator;
36
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
39
* CDI-based implementation of <code>IDisassemblyBlock</code>.
41
public class DisassemblyBlock implements IDisassemblyBlock, IAdaptable {
43
private IDisassembly fDisassembly;
45
private Object fSourceElement;
47
private IAsmSourceLine[] fSourceLines;
49
private IAddress fStartAddress = null;
51
private IAddress fEndAddress = null;
53
private boolean fMixedMode = false;
56
* Constructor for DisassemblyBlock.
58
private DisassemblyBlock( IDisassembly disassembly ) {
59
fDisassembly = disassembly;
62
public static DisassemblyBlock create( IDisassembly disassembly, ICDIMixedInstruction[] instructions ) {
63
DisassemblyBlock block = new DisassemblyBlock( disassembly );
64
ISourceLocator locator = disassembly.getDebugTarget().getLaunch().getSourceLocator();
65
IAddressFactory factory = ((CDebugTarget)disassembly.getDebugTarget()).getAddressFactory();
66
block.initialize( factory, locator, instructions );
70
public static DisassemblyBlock create( IDisassembly disassembly, ICDIInstruction[] instructions ) {
71
DisassemblyBlock block = new DisassemblyBlock( disassembly );
72
IAddressFactory factory = ((CDebugTarget)disassembly.getDebugTarget()).getAddressFactory();
73
block.initialize( factory, instructions );
77
private void initialize( IAddressFactory factory, ICDIInstruction[] instructions ) {
78
setMixedMode( false );
79
createSourceLines( factory, instructions );
80
initializeAddresses();
83
private void initialize( IAddressFactory factory, ISourceLocator locator, ICDIMixedInstruction[] mi ) {
85
createSourceLines( factory, locator, mi );
86
initializeAddresses();
90
* @see org.eclipse.cdt.debug.core.model.IDisassemblyBlock#getDisassembly()
92
public IDisassembly getDisassembly() {
97
* @see org.eclipse.cdt.debug.core.model.IDisassemblyBlock#getModuleFile()
99
public String getModuleFile() {
100
IDisassembly d = getDisassembly();
102
IExecFileInfo info = (IExecFileInfo)d.getAdapter( IExecFileInfo.class );
103
if ( info != null && info.getExecFile() != null ) {
104
return info.getExecFile().getPath().toOSString();
107
return ""; //$NON-NLS-1$
111
* @see org.eclipse.cdt.debug.core.model.IDisassemblyBlock#getSourceElement()
113
public Object getSourceElement() {
114
return fSourceElement;
118
* @see org.eclipse.cdt.debug.core.model.IDisassemblyBlock#contains(org.eclipse.cdt.debug.core.model.ICStackFrame)
120
public boolean contains( ICStackFrame frame ) {
121
if ( !getDisassembly().getDebugTarget().equals( frame.getDebugTarget() ) )
123
if ( fStartAddress == null || fEndAddress == null )
125
IAddress address = frame.getAddress(); // will return null if frame was disposed
126
return (address != null && address.compareTo( fStartAddress ) >= 0 && address.compareTo( fEndAddress ) <= 0);
132
* @see org.eclipse.cdt.debug.core.model.IDisassemblyBlock#getSourceLines()
134
public IAsmSourceLine[] getSourceLines() {
139
* @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
141
public Object getAdapter( Class adapter ) {
146
* @see org.eclipse.cdt.debug.core.model.IDisassemblyBlock#isMixedMode()
148
public boolean isMixedMode() {
152
public void dispose() {
155
private void createSourceLines( IAddressFactory factory, ISourceLocator locator, ICDIMixedInstruction[] mi ) {
156
IAsmSourceLine[] result = new IAsmSourceLine[mi.length];
157
LineNumberReader reader = null;
158
if ( result.length > 0 && locator != null ) {
159
String fileName = mi[0].getFileName();
160
Object element = null;
161
if ( locator instanceof ISourceLookupDirector ) {
162
element = ((ISourceLookupDirector)locator).getSourceElement( fileName );
164
if ( locator instanceof ICSourceLocator ) {
165
element = ((ICSourceLocator)locator).findSourceElement( fileName );
167
fSourceElement = element;
169
if ( element instanceof IFile ) {
170
file = ((IFile)element).getLocation().toFile();
172
else if ( element instanceof ITranslationUnit ) {
173
file = ((ITranslationUnit)element).getLocation().toFile();
175
else if ( element instanceof IStorage ) {
176
file = ((IStorage)element).getFullPath().toFile();
178
if ( file != null ) {
180
reader = new LineNumberReader( new FileReader( file ) );
182
catch( FileNotFoundException e ) {
186
for ( int i = 0; i < result.length; ++i ) {
188
boolean failed = false;
189
int lineNumber = mi[i].getLineNumber();
190
if ( reader != null ) {
191
while( reader.getLineNumber() + 1 < lineNumber ) {
193
if ( reader.readLine() == null ) {
194
// break if the end of file is reached (see bug #123745)
199
catch( IOException e ) {
202
if ( !failed && reader.getLineNumber() + 1 == lineNumber ) {
204
text = reader.readLine() + '\n';
206
catch( IOException e ) {
210
result[i] = new AsmSourceLine( factory, text, lineNumber, mi[i].getInstructions() );
212
fSourceLines = result;
215
private void createSourceLines( IAddressFactory factory, ICDIInstruction[] instructions ) {
216
fSourceLines = new IAsmSourceLine[] { new AsmSourceLine( factory, "", instructions ) }; //$NON-NLS-1$
219
private void initializeAddresses() {
220
for ( int i = 0; i < fSourceLines.length; ++i ) {
221
IAsmInstruction[] instr = fSourceLines[i].getInstructions();
222
if ( instr.length > 0 ) {
223
if ( fStartAddress == null )
224
fStartAddress = instr[0].getAdress();
225
fEndAddress = instr[instr.length - 1].getAdress();
230
private void setMixedMode( boolean mixedMode ) {
231
this.fMixedMode = mixedMode;
234
protected boolean contains( IAddress address ) {
235
return ( address.compareTo( fStartAddress ) >= 0 && address.compareTo( fEndAddress ) <= 0 );