Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a9ab9685ac4dfb4b35449bd1bee69868316289ee (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 ARM Limited 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:
 * ARM Limited - Initial API and implementation
 * IBM Corporation
 *******************************************************************************/

package org.eclipse.cdt.debug.internal.ui.elements.adapters;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import com.ibm.icu.text.MessageFormat;

import org.eclipse.cdt.debug.core.CDebugUtils;
import org.eclipse.cdt.debug.core.model.IDisassemblyInstruction;
import org.eclipse.cdt.debug.core.model.IDisassemblySourceLine;
import org.eclipse.cdt.debug.internal.ui.disassembly.editor.DisassemblyEditorPresentation;
import org.eclipse.cdt.debug.ui.disassembly.IDocumentElementLabelProvider;
import org.eclipse.cdt.debug.ui.disassembly.IDocumentElementLabelUpdate;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.model.ISourceLocator;
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;

/**
 * org.eclipse.cdt.debug.internal.ui.elements.adapters.DisassemblyElementLabelProvider: 
 * //TODO Add description.
 */
public class DisassemblyElementLabelProvider implements IDocumentElementLabelProvider {

    /* (non-Javadoc)
     * @see org.eclipse.cdt.debug.ui.disassembly.IDocumentElementLabelProvider#update(org.eclipse.cdt.debug.ui.disassembly.IDocumentElementLabelUpdate[])
     */
    public void update( final IDocumentElementLabelUpdate[] updates ) {
        Job job = new Job( "Label update" ) { //$NON-NLS-1$

            /* (non-Javadoc)
             * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
             */
            @Override
            protected IStatus run( IProgressMonitor monitor ) {
                for( int i = 0; i < updates.length; i++ ) {
                    IDocumentElementLabelUpdate update = updates[i];
                    if ( !update.isCanceled() ) {
                        retrieveLabel( update );
                    }
                    update.done();
                }
                return Status.OK_STATUS;
            }
        };
        job.setSystem( true );
        job.schedule();
    }

    protected void retrieveLabel( IDocumentElementLabelUpdate update ) {
        IPresentationContext context = update.getPresentationContext();
        if ( context instanceof DisassemblyEditorPresentation ) {
            DisassemblyEditorPresentation presentation = (DisassemblyEditorPresentation)context;
            Object element = update.getElement();
            if ( element instanceof IDisassemblyInstruction ) {
                IDisassemblyInstruction instruction = (IDisassemblyInstruction)element;
                StringBuilder sb = new StringBuilder();
                if ( presentation.showAddresses() ) {
                    BigInteger address = instruction.getAdress().getValue();
                    sb.append( "0x" ); //$NON-NLS-1$
                    sb.append( CDebugUtils.prependString( Long.toHexString( address.longValue() ), 8, '0' ) );
                    sb.append( '\t' );
                }
                sb.append( instruction.getInstructionText() );
                update.setLabel( DisassemblyEditorPresentation.ATTR_LINE_LABEL, sb.toString() );
            }
            else if ( element instanceof IDisassemblySourceLine ) {
                IDisassemblySourceLine line = (IDisassemblySourceLine)element;
                StringBuilder sb = new StringBuilder();
                if ( presentation.showLineNumbers() ) {
                    sb.append( line.getLineNumber() );
                    sb.append( '\t' );
                }
                sb.append( getSourceLineText( line ) );
                update.setLabel( DisassemblyEditorPresentation.ATTR_LINE_LABEL, sb.toString() );
            }
        }
    }

    private String getSourceLineText( IDisassemblySourceLine line ) {
        File file = line.getFile();
        String text = MessageFormat.format( "File {0} not found.", new Object[] {file.getPath()} );
        ISourceLocator locator = line.getDebugTarget().getLaunch().getSourceLocator();
        if ( locator instanceof ISourceLookupDirector ) {
            ISourceLookupDirector director = (ISourceLookupDirector)locator;
            Object sourceElement = director.getSourceElement( file.getPath() );
            if ( sourceElement != null ) {
                File lookupFile = null;
                if ( sourceElement instanceof IFile ) {
                    lookupFile = ((IFile)sourceElement).getLocation().toFile();
                }
                else if ( sourceElement instanceof IStorage ) {
                    lookupFile = ((IStorage)sourceElement).getFullPath().toFile();
                }
                if ( lookupFile != null ) {
                    try {
                        text = readLine( lookupFile, line.getLineNumber() - 1 );
                    }
                    catch( IOException e ) {
                        text = e.getLocalizedMessage();
                    }
                }
            }            
        }
        return text;
    }

    private String readLine( File file, int lineNumber ) throws IOException {
        FileReader fr = new FileReader( file );
        BufferedReader br = new BufferedReader( fr );
        
        try {
            int count = 0;
            String result = null;
            do {
                result = br.readLine();
                if ( count++ == lineNumber )
                    return result;
            } 
            while( result != null );
        }
        finally {
            br.close();
        }
        
        throw new IOException( MessageFormat.format( "Line {0} doesn't exist in {1}.", new Object[] {Integer.valueOf( lineNumber ), file.getPath()}) );
    }
}

Back to the top