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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "disassembleragent.h"
#include "breakhandler.h"
#include "debuggeractions.h"
#include "debuggercore.h"
#include "debuggerengine.h"
#include "debuggerinternalconstants.h"
#include "disassemblerlines.h"
#include "sourceutils.h"
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/documentmodel.h>
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/textmark.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h>
#include <utils/savedaction.h>
#include <QTextBlock>
#include <QDir>
using namespace Core;
using namespace TextEditor;
namespace Debugger {
namespace Internal {
///////////////////////////////////////////////////////////////////////
//
// DisassemblerBreakpointMarker
//
///////////////////////////////////////////////////////////////////////
// The red blob on the left side in the cpp editor.
class DisassemblerBreakpointMarker : public TextMark
{
public:
DisassemblerBreakpointMarker(const Breakpoint &bp, int lineNumber)
: TextMark(Utils::FileName(), lineNumber, Constants::TEXT_MARK_CATEGORY_BREAKPOINT), m_bp(bp)
{
setIcon(bp->icon());
setPriority(TextMark::NormalPriority);
}
bool isClickable() const final { return true; }
void clicked() final
{
QTC_ASSERT(m_bp, return);
m_bp->deleteGlobalOrThisBreakpoint();
}
public:
Breakpoint m_bp;
};
///////////////////////////////////////////////////////////////////////
//
// FrameKey
//
///////////////////////////////////////////////////////////////////////
class FrameKey
{
public:
FrameKey() = default;
inline bool matches(const Location &loc) const;
QString functionName;
QString fileName;
quint64 startAddress = 0;
quint64 endAddress = 0;
};
bool FrameKey::matches(const Location &loc) const
{
return loc.address() >= startAddress
&& loc.address() <= endAddress
&& loc.fileName() == fileName
&& loc.functionName() == functionName;
}
using CacheEntry = QPair<FrameKey, DisassemblerLines>;
///////////////////////////////////////////////////////////////////////
//
// DisassemblerAgentPrivate
//
///////////////////////////////////////////////////////////////////////
class DisassemblerAgentPrivate
{
public:
DisassemblerAgentPrivate(DebuggerEngine *engine);
~DisassemblerAgentPrivate();
void configureMimeType();
int lineForAddress(quint64 address) const;
public:
QPointer<TextDocument> document;
Location location;
QPointer<DebuggerEngine> engine;
LocationMark locationMark;
QList<DisassemblerBreakpointMarker *> breakpointMarks;
QList<CacheEntry> cache;
QString mimeType;
bool resetLocationScheduled;
};
DisassemblerAgentPrivate::DisassemblerAgentPrivate(DebuggerEngine *engine)
: document(nullptr),
engine(engine),
locationMark(engine, Utils::FileName(), 0),
mimeType("text/x-qtcreator-generic-asm"),
resetLocationScheduled(false)
{}
DisassemblerAgentPrivate::~DisassemblerAgentPrivate()
{
EditorManager::closeDocuments(QList<IDocument *>() << document);
document = nullptr;
qDeleteAll(breakpointMarks);
}
int DisassemblerAgentPrivate::lineForAddress(quint64 address) const
{
for (int i = 0, n = cache.size(); i != n; ++i) {
const CacheEntry &entry = cache.at(i);
if (entry.first.matches(location))
return entry.second.lineForAddress(address);
}
return 0;
}
///////////////////////////////////////////////////////////////////////
//
// DisassemblerAgent
//
///////////////////////////////////////////////////////////////////////
/*!
\class Debugger::Internal::DisassemblerAgent
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.
*/
DisassemblerAgent::DisassemblerAgent(DebuggerEngine *engine)
: d(new DisassemblerAgentPrivate(engine))
{
connect(action(IntelFlavor), &Utils::SavedAction::valueChanged,
this, &DisassemblerAgent::reload);
}
DisassemblerAgent::~DisassemblerAgent()
{
delete d;
d = nullptr;
}
int DisassemblerAgent::indexOf(const Location &loc) const
{
for (int i = 0; i < d->cache.size(); i++)
if (d->cache.at(i).first.matches(loc))
return i;
return -1;
}
void DisassemblerAgent::cleanup()
{
d->cache.clear();
}
void DisassemblerAgent::scheduleResetLocation()
{
d->resetLocationScheduled = true;
}
void DisassemblerAgent::resetLocation()
{
if (!d->document)
return;
if (d->resetLocationScheduled) {
d->resetLocationScheduled = false;
d->document->removeMark(&d->locationMark);
}
}
const Location &DisassemblerAgent::location() const
{
return d->location;
}
void DisassemblerAgent::reload()
{
d->cache.clear();
d->engine->fetchDisassembler(this);
}
void DisassemblerAgent::setLocation(const Location &loc)
{
d->location = loc;
int index = indexOf(loc);
if (index != -1) {
// Refresh when not displaying a function and there is not sufficient
// context left past the address.
if (d->cache.at(index).first.endAddress - loc.address() < 24) {
index = -1;
d->cache.removeAt(index);
}
}
if (index != -1) {
const FrameKey &key = d->cache.at(index).first;
const QString msg =
QString("Using cached disassembly for 0x%1 (0x%2-0x%3) in \"%4\"/ \"%5\"")
.arg(loc.address(), 0, 16)
.arg(key.startAddress, 0, 16).arg(key.endAddress, 0, 16)
.arg(loc.functionName(), QDir::toNativeSeparators(loc.fileName()));
d->engine->showMessage(msg);
setContentsToDocument(d->cache.at(index).second);
d->resetLocationScheduled = false; // In case reset from previous run still pending.
} else {
d->engine->fetchDisassembler(this);
}
}
void DisassemblerAgentPrivate::configureMimeType()
{
QTC_ASSERT(document, return);
document->setMimeType(mimeType);
Utils::MimeType mtype = Utils::mimeTypeForName(mimeType);
if (mtype.isValid()) {
foreach (IEditor *editor, DocumentModel::editorsForDocument(document))
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget()))
widget->configureGenericHighlighter();
} else {
qWarning("Assembler mimetype '%s' not found.", qPrintable(mimeType));
}
}
QString DisassemblerAgent::mimeType() const
{
return d->mimeType;
}
void DisassemblerAgent::setMimeType(const QString &mt)
{
if (mt == d->mimeType)
return;
d->mimeType = mt;
if (d->document)
d->configureMimeType();
}
void DisassemblerAgent::setContents(const DisassemblerLines &contents)
{
QTC_ASSERT(d, return);
if (contents.size()) {
const quint64 startAddress = contents.startAddress();
const quint64 endAddress = contents.endAddress();
if (startAddress) {
FrameKey key;
key.fileName = d->location.fileName();
key.functionName = d->location.functionName();
key.startAddress = startAddress;
key.endAddress = endAddress;
d->cache.append(CacheEntry(key, contents));
}
}
setContentsToDocument(contents);
}
void DisassemblerAgent::setContentsToDocument(const DisassemblerLines &contents)
{
QTC_ASSERT(d, return);
if (!d->document) {
QString titlePattern = QLatin1String("Disassembler");
IEditor *editor = EditorManager::openEditorWithContents(
Core::Constants::K_DEFAULT_TEXT_EDITOR_ID,
&titlePattern);
QTC_ASSERT(editor, return);
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
widget->setReadOnly(true);
widget->setRequestMarkEnabled(true);
}
d->document = qobject_cast<TextDocument *>(editor->document());
QTC_ASSERT(d->document, return);
d->document->setTemporary(true);
// FIXME: This is accumulating quite a bit out-of-band data.
// Make that a proper TextDocument reimplementation.
d->document->setProperty(Debugger::Constants::OPENED_BY_DEBUGGER, true);
d->document->setProperty(Debugger::Constants::OPENED_WITH_DISASSEMBLY, true);
d->document->setProperty(Debugger::Constants::DISASSEMBLER_SOURCE_FILE, d->location.fileName());
d->configureMimeType();
} else {
EditorManager::activateEditorForDocument(d->document);
}
d->document->setPlainText(contents.toString());
d->document->setPreferredDisplayName(QString("Disassembler (%1)")
.arg(d->location.functionName()));
const Breakpoints bps = d->engine->breakHandler()->breakpoints();
for (const Breakpoint bp : bps)
updateBreakpointMarker(bp);
updateLocationMarker();
}
void DisassemblerAgent::updateLocationMarker()
{
if (!d->document)
return;
int lineNumber = d->lineForAddress(d->location.address());
if (d->location.needsMarker()) {
d->document->removeMark(&d->locationMark);
d->locationMark.updateLineNumber(lineNumber);
d->document->addMark(&d->locationMark);
}
d->locationMark.updateIcon();
// Center cursor.
if (EditorManager::currentDocument() == d->document)
if (auto textEditor = qobject_cast<BaseTextEditor *>(EditorManager::currentEditor()))
textEditor->gotoLine(lineNumber);
}
void DisassemblerAgent::removeBreakpointMarker(const Breakpoint &bp)
{
if (!d->document)
return;
for (DisassemblerBreakpointMarker *marker : d->breakpointMarks) {
if (marker->m_bp == bp) {
d->breakpointMarks.removeOne(marker);
d->document->removeMark(marker);
delete marker;
return;
}
}
}
void DisassemblerAgent::updateBreakpointMarker(const Breakpoint &bp)
{
removeBreakpointMarker(bp);
const quint64 address = bp->address();
if (!address)
return;
int lineNumber = d->lineForAddress(address);
if (!lineNumber)
return;
// HACK: If it's a FileAndLine breakpoint, and there's a source line
// above, move the marker up there. That allows setting and removing
// normal breakpoints from within the disassembler view.
if (bp->type() == BreakpointByFileAndLine) {
ContextData context = getLocationContext(d->document, lineNumber - 1);
if (context.type == LocationByFile)
--lineNumber;
}
auto marker = new DisassemblerBreakpointMarker(bp, lineNumber);
d->breakpointMarks.append(marker);
QTC_ASSERT(d->document, return);
d->document->addMark(marker);
}
quint64 DisassemblerAgent::address() const
{
return d->location.address();
}
} // namespace Internal
} // namespace Debugger
|