2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2010-11-24 16:57:40 +01:00
|
|
|
|
|
|
|
|
#include "disassemblerlines.h"
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QFile>
|
2014-05-02 17:46:41 +02:00
|
|
|
#include <QTextStream>
|
2011-02-10 13:43:02 +01:00
|
|
|
|
2022-07-05 15:37:08 +02:00
|
|
|
namespace Debugger::Internal {
|
2010-11-24 16:57:40 +01:00
|
|
|
|
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.
|
2020-09-18 13:05:37 +02:00
|
|
|
if (pos > 19 && unparsed.mid(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)
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
void DisassemblerLines::appendSourceLine(const QString &fileName, int lineNumber)
|
2011-04-13 13:52:03 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2019-07-24 18:40:10 +02:00
|
|
|
if (lineNumber >= cache->lines.size())
|
2011-04-13 13:52:03 +02:00
|
|
|
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;
|
2020-09-18 13:05:37 +02:00
|
|
|
dl.offset = address.mid(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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 15:37:08 +02:00
|
|
|
} // Debugger::Internal
|