2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-11-24 16:57:40 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-11-24 16:57:40 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-11-24 16:57:40 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-11-24 16:57:40 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-12-17 17:14:20 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-11-24 16:57:40 +01:00
|
|
|
|
|
|
|
|
#include "disassemblerlines.h"
|
2015-07-20 09:37:54 +02:00
|
|
|
#include "sourceutils.h"
|
2010-11-24 16:57:40 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QFile>
|
2014-05-02 17:46:41 +02:00
|
|
|
#include <QRegExp>
|
|
|
|
|
#include <QTextStream>
|
2011-02-10 13:43:02 +01:00
|
|
|
|
2010-11-24 16:57:40 +01:00
|
|
|
namespace Debugger {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2011-04-12 17:32:41 +02:00
|
|
|
void DisassemblerLine::fromString(const QString &unparsed)
|
2010-11-24 16:57:40 +01:00
|
|
|
{
|
2011-02-10 13:43:02 +01:00
|
|
|
int pos = -1;
|
|
|
|
|
for (int i = 0; i != unparsed.size(); ++i) {
|
2011-09-02 09:34:25 +02:00
|
|
|
const uint c = unparsed.at(i).unicode();
|
2011-02-10 13:43:02 +01:00
|
|
|
if (c == ' ' || c == ':' || c == '\t') {
|
|
|
|
|
pos = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-24 16:57:40 +01:00
|
|
|
// Mac gdb has an overflow reporting 64bit addresses causing the instruction
|
|
|
|
|
// to follow the last digit "0x000000013fff4810mov 1,1". Truncate here.
|
2019-01-17 01:38:54 +01:00
|
|
|
if (pos > 19 && unparsed.midRef(3, 16).toULongLong())
|
2011-02-10 13:43:02 +01:00
|
|
|
pos = 19;
|
|
|
|
|
|
2010-11-24 16:57:40 +01:00
|
|
|
QString addr = unparsed.left(pos);
|
|
|
|
|
// MSVC 64bit: Remove 64bit separator 00000000`00a45000'.
|
2018-10-07 22:38:47 +03:00
|
|
|
if (addr.size() >= 9 && addr.at(8) == '`')
|
2010-11-24 16:57:40 +01:00
|
|
|
addr.remove(8, 1);
|
|
|
|
|
|
2018-10-07 22:38:47 +03:00
|
|
|
if (addr.endsWith(':')) // clang
|
2010-11-24 16:57:40 +01:00
|
|
|
addr.chop(1);
|
2018-10-07 22:38:47 +03:00
|
|
|
if (addr.startsWith("0x"))
|
2010-11-24 16:57:40 +01:00
|
|
|
addr.remove(0, 2);
|
2011-09-02 09:34:25 +02:00
|
|
|
bool ok = false;
|
|
|
|
|
address = addr.toULongLong(&ok, 16);
|
|
|
|
|
if (ok)
|
2010-11-24 16:57:40 +01:00
|
|
|
data = unparsed.mid(pos + 1);
|
|
|
|
|
else
|
|
|
|
|
data = unparsed;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-01 16:57:17 +02:00
|
|
|
quint64 DisassemblerLines::startAddress() const
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < m_data.size(); ++i)
|
|
|
|
|
if (m_data.at(i).address)
|
|
|
|
|
return m_data.at(i).address;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quint64 DisassemblerLines::endAddress() const
|
|
|
|
|
{
|
|
|
|
|
for (int i = m_data.size()- 1; i >= 0; --i)
|
|
|
|
|
if (m_data.at(i).address)
|
|
|
|
|
return m_data.at(i).address;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-24 16:57:40 +01:00
|
|
|
int DisassemblerLines::lineForAddress(quint64 address) const
|
|
|
|
|
{
|
|
|
|
|
return m_rowCache.value(address);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DisassemblerLines::coversAddress(quint64 address) const
|
|
|
|
|
{
|
|
|
|
|
return m_rowCache.value(address) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-12 17:32:41 +02:00
|
|
|
void DisassemblerLines::appendLine(const DisassemblerLine &dl)
|
2010-11-24 16:57:40 +01:00
|
|
|
{
|
|
|
|
|
m_data.append(dl);
|
2011-04-12 17:32:41 +02:00
|
|
|
m_rowCache[dl.address] = m_data.size();
|
2010-11-24 16:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
2011-09-02 09:34:25 +02:00
|
|
|
// Append source line: Cache current file.
|
2011-04-13 13:52:03 +02:00
|
|
|
struct SourceFileCache
|
|
|
|
|
{
|
|
|
|
|
QString fileName;
|
|
|
|
|
QStringList lines;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Q_GLOBAL_STATIC(SourceFileCache, sourceFileCache)
|
|
|
|
|
|
|
|
|
|
void DisassemblerLines::appendSourceLine(const QString &fileName, uint lineNumber)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (fileName.isEmpty() || lineNumber == 0)
|
|
|
|
|
return;
|
2011-09-02 09:34:25 +02:00
|
|
|
lineNumber--; // Fix 1..n range.
|
2011-04-13 13:52:03 +02:00
|
|
|
SourceFileCache *cache = sourceFileCache();
|
|
|
|
|
if (fileName != cache->fileName) {
|
|
|
|
|
cache->fileName = fileName;
|
|
|
|
|
cache->lines.clear();
|
|
|
|
|
QFile file(fileName);
|
|
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
|
|
|
QTextStream ts(&file);
|
2018-10-07 22:38:47 +03:00
|
|
|
cache->lines = ts.readAll().split('\n');
|
2011-09-02 09:34:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
2011-04-13 13:52:03 +02:00
|
|
|
if (lineNumber >= uint(cache->lines.size()))
|
|
|
|
|
return;
|
|
|
|
|
DisassemblerLine dl;
|
|
|
|
|
dl.lineNumber = lineNumber;
|
|
|
|
|
dl.data = cache->lines.at(lineNumber);
|
|
|
|
|
appendLine(dl);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-18 17:25:21 +01:00
|
|
|
void DisassemblerLines::appendComment(const QString &line)
|
|
|
|
|
{
|
|
|
|
|
DisassemblerLine dl;
|
|
|
|
|
dl.data = line;
|
|
|
|
|
appendLine(dl);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-12 17:32:41 +02:00
|
|
|
void DisassemblerLines::appendUnparsed(const QString &unparsed)
|
2010-11-24 16:57:40 +01:00
|
|
|
{
|
2011-04-12 17:32:41 +02:00
|
|
|
QString line = unparsed.trimmed();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
return;
|
2018-10-07 22:38:47 +03:00
|
|
|
if (line.startsWith("Current language:"))
|
2011-04-12 17:32:41 +02:00
|
|
|
return;
|
2018-10-07 22:38:47 +03:00
|
|
|
if (line.startsWith("Dump of assembler")) {
|
2011-04-12 17:32:41 +02:00
|
|
|
m_lastFunction.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-07 22:38:47 +03:00
|
|
|
if (line.startsWith("The current source"))
|
2011-04-12 17:32:41 +02:00
|
|
|
return;
|
2018-10-07 22:38:47 +03:00
|
|
|
if (line.startsWith("End of assembler")) {
|
2011-04-12 17:32:41 +02:00
|
|
|
m_lastFunction.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-07 22:38:47 +03:00
|
|
|
if (line.startsWith("=> "))
|
2011-04-12 17:32:41 +02:00
|
|
|
line = line.mid(3);
|
2018-10-07 22:38:47 +03:00
|
|
|
if (line.startsWith("0x")) {
|
2014-02-18 17:25:21 +01:00
|
|
|
// Address line. Split at the tab.
|
2018-10-07 22:38:47 +03:00
|
|
|
int tab1 = line.indexOf('\t');
|
2014-04-30 14:56:51 +02:00
|
|
|
if (tab1 == -1) {
|
2014-02-18 17:25:21 +01:00
|
|
|
appendComment(line);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-07 22:38:47 +03:00
|
|
|
int tab2 = line.indexOf('\t', tab1 + 1);
|
2014-04-30 14:56:51 +02:00
|
|
|
if (tab2 == -1)
|
|
|
|
|
tab2 = tab1;
|
|
|
|
|
QString address = line.left(tab1);
|
2018-10-07 22:38:47 +03:00
|
|
|
if (address.endsWith(':'))
|
2014-02-18 17:25:21 +01:00
|
|
|
address.chop(1);
|
2018-10-07 22:38:47 +03:00
|
|
|
int pos1 = address.indexOf('<') + 1;
|
2011-09-08 17:38:47 +02:00
|
|
|
DisassemblerLine dl;
|
2014-04-30 14:56:51 +02:00
|
|
|
dl.bytes = line.mid(tab1, tab2 - tab1).trimmed();
|
|
|
|
|
m_bytesLength = qMax(m_bytesLength, tab2 - tab1);
|
|
|
|
|
dl.data = line.mid(tab2).trimmed();
|
2018-10-07 22:38:47 +03:00
|
|
|
if (pos1 && address.indexOf("<UNDEFINED> instruction:") == -1) {
|
|
|
|
|
if (address.endsWith('>'))
|
2014-02-18 17:25:21 +01:00
|
|
|
address.chop(1);
|
2018-10-07 22:38:47 +03:00
|
|
|
int pos2 = address.indexOf('+', pos1);
|
2014-02-18 17:25:21 +01:00
|
|
|
if (pos1 < pos2) {
|
|
|
|
|
QString function = address.mid(pos1, pos2 - pos1);
|
2011-09-08 17:38:47 +02:00
|
|
|
if (function != m_lastFunction) {
|
|
|
|
|
DisassemblerLine dl;
|
2016-06-07 17:04:53 +02:00
|
|
|
dl.data = "Function: " + function;
|
2011-09-08 17:38:47 +02:00
|
|
|
m_data.append(dl);
|
|
|
|
|
m_lastFunction = function;
|
|
|
|
|
}
|
2011-04-12 17:32:41 +02:00
|
|
|
}
|
2018-07-23 22:28:49 +02:00
|
|
|
dl.address = address.left(pos1 - 1).toULongLong(nullptr, 0);
|
2011-09-08 17:38:47 +02:00
|
|
|
dl.function = m_lastFunction;
|
2019-01-17 01:38:54 +01:00
|
|
|
dl.offset = address.midRef(pos2).toUInt();
|
2011-09-08 17:38:47 +02:00
|
|
|
} else {
|
|
|
|
|
// Plain data like "0x0000cd64:\tadd\tlr, pc, lr\n"
|
2018-07-23 22:28:49 +02:00
|
|
|
dl.address = address.toULongLong(nullptr, 0);
|
2011-09-08 17:38:47 +02:00
|
|
|
dl.function = m_lastFunction;
|
|
|
|
|
dl.offset = 0;
|
2011-04-12 17:32:41 +02:00
|
|
|
}
|
|
|
|
|
m_rowCache[dl.address] = m_data.size() + 1;
|
|
|
|
|
m_data.append(dl);
|
|
|
|
|
} else {
|
2014-05-02 17:46:41 +02:00
|
|
|
// Comment or code line.
|
|
|
|
|
QTextStream ts(&line);
|
2011-04-12 17:32:41 +02:00
|
|
|
DisassemblerLine dl;
|
2014-05-02 17:46:41 +02:00
|
|
|
ts >> dl.lineNumber;
|
|
|
|
|
dl.data = line.mid(ts.pos());
|
2011-04-12 17:32:41 +02:00
|
|
|
m_data.append(dl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 14:56:51 +02:00
|
|
|
QString DisassemblerLine::toString(int maxOp) const
|
2011-04-12 17:32:41 +02:00
|
|
|
{
|
2016-06-07 17:04:53 +02:00
|
|
|
const QString someSpace = " ";
|
2011-04-12 17:32:41 +02:00
|
|
|
QString str;
|
|
|
|
|
if (isAssembler()) {
|
|
|
|
|
if (address)
|
2016-06-07 17:04:53 +02:00
|
|
|
str += QString("0x%1 ").arg(address, 0, 16);
|
2014-02-18 17:25:21 +01:00
|
|
|
if (offset)
|
2018-08-04 22:43:06 +03:00
|
|
|
str += QString("<+%1> ").arg(offset, 5);
|
2014-02-18 17:25:21 +01:00
|
|
|
else
|
2018-08-04 22:43:06 +03:00
|
|
|
str += " ";
|
2016-06-07 17:04:53 +02:00
|
|
|
str += QString(" %1 ").arg(bytes);
|
2018-10-07 22:38:47 +03:00
|
|
|
str += QString(maxOp - bytes.size(), ' ');
|
2011-04-12 17:32:41 +02:00
|
|
|
str += data;
|
|
|
|
|
} else if (isCode()) {
|
|
|
|
|
str += someSpace;
|
2014-05-02 17:46:41 +02:00
|
|
|
str += QString::number(lineNumber);
|
|
|
|
|
if (hunk)
|
2016-06-07 17:04:53 +02:00
|
|
|
str += QString(" [%1]").arg(hunk);
|
2014-05-02 17:46:41 +02:00
|
|
|
else
|
2016-06-07 17:04:53 +02:00
|
|
|
str += " ";
|
2011-04-12 17:32:41 +02:00
|
|
|
str += data;
|
|
|
|
|
} else {
|
|
|
|
|
str += someSpace;
|
|
|
|
|
str += data;
|
|
|
|
|
}
|
|
|
|
|
return str;
|
2010-11-24 16:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 14:56:51 +02:00
|
|
|
QString DisassemblerLines::toString() const
|
|
|
|
|
{
|
|
|
|
|
QString str;
|
|
|
|
|
for (int i = 0, n = size(); i != n; ++i) {
|
|
|
|
|
str += m_data.at(i).toString(m_bytesLength);
|
2018-10-07 22:38:47 +03:00
|
|
|
str += '\n';
|
2014-04-30 14:56:51 +02:00
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-24 16:57:40 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Debugger
|