disassembleragent.cpp 9.94 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 7 8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15 16 17 18 19 20 21 22 23 24
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31 32 33
**
**************************************************************************/

34
#include "disassembleragent.h"
35

Friedemann Kleint's avatar
Friedemann Kleint committed
36
#include "disassemblerlines.h"
37
#include "breakhandler.h"
38
#include "debuggerengine.h"
39
#include "debuggercore.h"
40
#include "debuggerstringutils.h"
41
#include "stackframe.h"
42

43 44 45
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
46
#include <coreplugin/mimedatabase.h>
47

48
#include <texteditor/basetextdocument.h>
49 50
#include <texteditor/basetexteditor.h>
#include <texteditor/basetextmark.h>
51
#include <texteditor/plaintexteditor.h>
52 53 54 55
#include <texteditor/texteditorconstants.h>

#include <utils/qtcassert.h>

56
#include <QtGui/QTextBlock>
57
#include <QtGui/QIcon>
Friedemann Kleint's avatar
Friedemann Kleint committed
58
#include <QtCore/QPointer>
Bradley T. Hughes's avatar
Compile  
Bradley T. Hughes committed
59

60 61
using namespace Core;

62 63 64
namespace Debugger {
namespace Internal {

65 66
///////////////////////////////////////////////////////////////////////
//
67
// DisassemblerAgent
68 69 70 71 72 73 74 75
//
///////////////////////////////////////////////////////////////////////

class LocationMark2 : public TextEditor::ITextMark
{
public:
    LocationMark2() {}

76
    QIcon icon() const { return debuggerCore()->locationMarkIcon(); }
77 78 79 80 81 82
    void updateLineNumber(int /*lineNumber*/) {}
    void updateBlock(const QTextBlock & /*block*/) {}
    void removedFromEditor() {}
    void documentClosing() {}
};

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
class BreakpointMarker2 : public TextEditor::ITextMark
{
public:
    BreakpointMarker2(const QIcon &icon) : m_icon(icon) {}

    QIcon icon() const { return m_icon; }
    void updateLineNumber(int) {}
    void updateBlock(const QTextBlock &) {}
    void removedFromEditor() {}
    void documentClosing() {}

private:
    QIcon m_icon;
};


99
class DisassemblerAgentPrivate
100
{
101
public:
102 103
    DisassemblerAgentPrivate();
    ~DisassemblerAgentPrivate();
104
    void configureMimeType();
105

106
public:
107
    QPointer<TextEditor::ITextEditor> editor;
108
    Location location;
109
    bool tryMixed;
110
    QPointer<DebuggerEngine> engine;
111 112 113
    TextEditor::ITextMark *locationMark;
    QList<TextEditor::ITextMark *> breakpointMarks;
    
114
    QHash<QString, DisassemblerLines> cache;
115
    QString mimeType;
116 117
};

118
DisassemblerAgentPrivate::DisassemblerAgentPrivate()
119
  : editor(0),
120 121 122
    tryMixed(true),
    locationMark(new LocationMark2),
    mimeType(_("text/x-qtcreator-generic-asm"))
123
{
124
}
125

126
DisassemblerAgentPrivate::~DisassemblerAgentPrivate()
127 128 129 130 131 132 133 134
{
    if (editor) {
        EditorManager *editorManager = EditorManager::instance();
        editorManager->closeEditors(QList<IEditor *>() << editor);
    }
    editor = 0;
    delete locationMark;
}
135

136
/*!
137
    \class DisassemblerAgent
138 139 140 141 142 143

     Objects from this class are created in response to user actions in
     the Gui for showing disassembled memory from the inferior. After creation
     it handles communication between the engine and the editor.
*/

144 145
DisassemblerAgent::DisassemblerAgent(DebuggerEngine *engine)
    : QObject(0), d(new DisassemblerAgentPrivate)
146
{
147
    d->engine = engine;
148 149
}

150
DisassemblerAgent::~DisassemblerAgent()
151 152
{
    delete d;
153
    d = 0;
154 155
}

156
void DisassemblerAgent::cleanup()
157 158 159 160
{
    d->cache.clear();
}

161
void DisassemblerAgent::resetLocation()
162
{
163 164 165
    if (!d->editor)
        return;
    d->editor->markableInterface()->removeMark(d->locationMark);
166 167
}

168
static QString frameKey(const Location &loc)
169
{
170 171
    return _("%1:%2:%3").arg(loc.functionName())
        .arg(loc.fileName()).arg(loc.address());
172 173
}

174
const Location &DisassemblerAgent::location() const
175
{
176
    return d->location;
177 178
}

179
bool DisassemblerAgent::isMixed() const
180 181
{
    return d->tryMixed
182 183 184
        && d->location.lineNumber() > 0
        && !d->location.functionName().isEmpty()
        && d->location.functionName() != _("??");
185 186
}

187
void DisassemblerAgent::setLocation(const Location &loc)
188
{
189
    d->location = loc;
190
    if (isMixed()) {
191
        QHash<QString, DisassemblerLines>::ConstIterator it =
192
            d->cache.find(frameKey(loc));
193
        if (it != d->cache.end()) {
Tobias Hunger's avatar
Tobias Hunger committed
194
            QString msg = _("Use cache disassembler for '%1' in '%2'")
195
                .arg(loc.functionName()).arg(loc.fileName());
196
            d->engine->showMessage(msg);
197
            setContents(*it);
198 199
            updateBreakpointMarkers();
            updateLocationMarker();
200 201
            return;
        }
202
    }
203
    d->engine->fetchDisassembler(this);
204 205
}

206
void DisassemblerAgentPrivate::configureMimeType()
207 208 209
{
    QTC_ASSERT(editor, return);

210 211
    TextEditor::BaseTextDocument *doc =
        qobject_cast<TextEditor::BaseTextDocument *>(editor->file());
212 213 214
    QTC_ASSERT(doc, return);
    doc->setMimeType(mimeType);

215 216
    TextEditor::PlainTextEditor *pe =
        qobject_cast<TextEditor::PlainTextEditor *>(editor->widget());
217 218
    QTC_ASSERT(pe, return);

219 220
    MimeType mtype = ICore::instance()->mimeDatabase()->findByType(mimeType);
    if (mtype)
221
        pe->configure(mtype);
222
    else
223 224 225
        qWarning("Assembler mimetype '%s' not found.", qPrintable(mimeType));
}

226
QString DisassemblerAgent::mimeType() const
227 228 229 230
{
    return d->mimeType;
}

231
void DisassemblerAgent::setMimeType(const QString &mt)
232 233 234 235 236 237 238 239
{
    if (mt == d->mimeType)
        return;
    d->mimeType = mt;
    if (d->editor)
       d->configureMimeType();
}

240
void DisassemblerAgent::setContents(const DisassemblerLines &contents)
241
{
242
    QTC_ASSERT(d, return);
243 244 245 246 247 248 249 250
    using namespace Core;
    using namespace TextEditor;

    EditorManager *editorManager = EditorManager::instance();
    if (!d->editor) {
        QString titlePattern = "Disassembler";
        d->editor = qobject_cast<ITextEditor *>(
            editorManager->openEditorWithContents(
251
                Core::Constants::K_DEFAULT_TEXT_EDITOR_ID,
252
                &titlePattern));
253
        QTC_ASSERT(d->editor, return);
254 255
        d->editor->setProperty(Debugger::Constants::OPENED_BY_DEBUGGER, true);
        d->editor->setProperty(Debugger::Constants::OPENED_WITH_DISASSEMBLY, true);
256
        d->configureMimeType();
257 258 259 260 261

        BaseTextEditor *baseTextEdit =
                qobject_cast<BaseTextEditor *>(d->editor->widget());
        if (baseTextEdit)
            baseTextEdit->setRequestMarkEnabled(true);
262 263
    }

264
    editorManager->activateEditor(d->editor);
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279
    QPlainTextEdit *plainTextEdit =
        qobject_cast<QPlainTextEdit *>(d->editor->widget());
    QTC_ASSERT(plainTextEdit, return);

    QString str;
    for (int i = 0, n = contents.size(); i != n; ++i) {
        const DisassemblerLine &dl = contents.at(i);
        if (dl.address) {
            str += QString("0x");
            str += QString::number(dl.address, 16);
            str += "  ";
        }
        str += dl.data;
        str += "\n";
280
    }
281 282
    plainTextEdit->setPlainText(str);
    plainTextEdit->setReadOnly(true);
283

284 285 286
    d->cache.insert(frameKey(d->location), contents);
    d->editor->setDisplayName(_("Disassembler (%1)")
        .arg(d->location.functionName()));
287 288 289 290 291

    updateBreakpointMarkers();
    updateLocationMarker();
}

292
void DisassemblerAgent::updateLocationMarker()
293 294
{
    QTC_ASSERT(d->editor, return);
295

296 297
    const DisassemblerLines &contents = d->cache.value(frameKey(d->location));
    int lineNumber = contents.lineForAddress(d->location.address());
298

299
    if (d->location.needsMarker()) {
300 301 302 303 304 305 306 307
        d->editor->markableInterface()->removeMark(d->locationMark);
        if (lineNumber)
            d->editor->markableInterface()->addMark(d->locationMark, lineNumber);
    }

    QPlainTextEdit *plainTextEdit =
        qobject_cast<QPlainTextEdit *>(d->editor->widget());
    QTC_ASSERT(plainTextEdit, return); 
308 309 310 311
    QTextCursor tc = plainTextEdit->textCursor();
    QTextBlock block = tc.document()->findBlockByNumber(lineNumber - 1);
    tc.setPosition(block.position());
    plainTextEdit->setTextCursor(tc);
312 313
}

314
void DisassemblerAgent::updateBreakpointMarkers()
315 316 317 318 319 320 321 322 323
{
    if (!d->editor)
        return;

    BreakHandler *handler = breakHandler();
    BreakpointIds ids = handler->engineBreakpointIds(d->engine);
    if (ids.isEmpty())
        return;

324
    const DisassemblerLines &contents = d->cache.value(frameKey(d->location));
325 326 327 328 329 330 331 332 333

    foreach (TextEditor::ITextMark *marker, d->breakpointMarks)
        d->editor->markableInterface()->removeMark(marker);
    d->breakpointMarks.clear();
    foreach (BreakpointId id, ids) {
        const quint64 address = handler->address(id);
        if (!address)
            continue;
        const int lineNumber = contents.lineForAddress(address);
hjk's avatar
hjk committed
334 335
        if (!lineNumber)
            continue;
336 337 338 339 340 341
        BreakpointMarker2 *marker = new BreakpointMarker2(handler->icon(id));
        d->breakpointMarks.append(marker);
        d->editor->markableInterface()->addMark(marker, lineNumber);
    }
}

342
quint64 DisassemblerAgent::address() const
343
{
344
    return d->location.address();
345 346
}

347
// Return address of an assembly line "0x0dfd  bla"
348
quint64 DisassemblerAgent::addressFromDisassemblyLine(const QString &line)
349 350 351 352
{
    return DisassemblerLine(line).address;
}

353 354 355 356 357
void DisassemblerAgent::setTryMixed(bool on)
{
    d->tryMixed = on;
}

358 359
} // namespace Internal
} // namespace Debugger