2010-11-24 16:57:40 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2010-11-24 16:57:40 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2010-11-24 16:57:40 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-11-24 16:57:40 +01:00
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 17:14:20 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-11-24 16:57:40 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "disassemblerlines.h"
|
2011-04-12 17:32:41 +02:00
|
|
|
#include "debuggerstringutils.h"
|
2010-11-24 16:57:40 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QRegExp>
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#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.
|
2011-02-10 13:43:02 +01:00
|
|
|
if (pos > 19 && unparsed.mid(3, 16).toULongLong())
|
|
|
|
|
pos = 19;
|
|
|
|
|
|
2010-11-24 16:57:40 +01:00
|
|
|
QString addr = unparsed.left(pos);
|
|
|
|
|
// MSVC 64bit: Remove 64bit separator 00000000`00a45000'.
|
|
|
|
|
if (addr.size() >= 9 && addr.at(8) == QLatin1Char('`'))
|
|
|
|
|
addr.remove(8, 1);
|
|
|
|
|
|
2011-12-21 14:02:52 +01:00
|
|
|
if (addr.endsWith(QLatin1Char(':'))) // clang
|
2010-11-24 16:57:40 +01:00
|
|
|
addr.chop(1);
|
|
|
|
|
if (addr.startsWith(QLatin1String("0x")))
|
|
|
|
|
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-04-12 17:32:41 +02:00
|
|
|
quint64 DisassemblerLine::addressFromDisassemblyLine(const QString &line)
|
|
|
|
|
{
|
|
|
|
|
DisassemblerLine l;
|
|
|
|
|
l.fromString(line);
|
|
|
|
|
return l.address;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
cache->lines = ts.readAll().split(QLatin1Char('\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);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2011-12-21 14:02:52 +01:00
|
|
|
if (line.startsWith(QLatin1String("Current language:")))
|
2011-04-12 17:32:41 +02:00
|
|
|
return;
|
2011-12-21 14:02:52 +01:00
|
|
|
if (line.startsWith(QLatin1String("Dump of assembler"))) {
|
2011-04-12 17:32:41 +02:00
|
|
|
m_lastFunction.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-12-21 14:02:52 +01:00
|
|
|
if (line.startsWith(QLatin1String("The current source")))
|
2011-04-12 17:32:41 +02:00
|
|
|
return;
|
2011-12-21 14:02:52 +01:00
|
|
|
if (line.startsWith(QLatin1String("End of assembler"))) {
|
2011-04-12 17:32:41 +02:00
|
|
|
m_lastFunction.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-12-21 14:02:52 +01:00
|
|
|
if (line.startsWith(QLatin1String("=> ")))
|
2011-04-12 17:32:41 +02:00
|
|
|
line = line.mid(3);
|
2011-12-21 14:02:52 +01:00
|
|
|
if (line.startsWith(QLatin1String("0x"))) {
|
2011-04-12 17:32:41 +02:00
|
|
|
// Address line.
|
2011-12-21 14:02:52 +01:00
|
|
|
int pos1 = line.indexOf(QLatin1Char('<')) + 1;
|
|
|
|
|
int posc = line.indexOf(QLatin1Char(':'));
|
2011-09-08 17:38:47 +02:00
|
|
|
DisassemblerLine dl;
|
2011-12-21 14:02:52 +01:00
|
|
|
if (pos1 && line.indexOf(QLatin1String("<UNDEFINED> instruction:")) == -1) {
|
|
|
|
|
int pos2 = line.indexOf(QLatin1Char('+'), pos1);
|
|
|
|
|
int pos3 = line.indexOf(QLatin1Char('>'), pos1);
|
2011-09-08 17:38:47 +02:00
|
|
|
if (pos1 < pos2 && pos2 < pos3) {
|
|
|
|
|
QString function = line.mid(pos1, pos2 - pos1);
|
|
|
|
|
if (function != m_lastFunction) {
|
|
|
|
|
DisassemblerLine dl;
|
|
|
|
|
dl.data = _("Function: ") + function;
|
|
|
|
|
m_data.append(dl);
|
|
|
|
|
m_lastFunction = function;
|
|
|
|
|
}
|
2011-04-12 17:32:41 +02:00
|
|
|
}
|
2011-09-08 17:38:47 +02:00
|
|
|
dl.address = line.left(pos1 - 1).toULongLong(0, 0);
|
|
|
|
|
dl.function = m_lastFunction;
|
|
|
|
|
dl.offset = line.mid(pos2, pos3 - pos2).toUInt();
|
|
|
|
|
dl.data = line.mid(pos3 + 3).trimmed();
|
|
|
|
|
} else {
|
|
|
|
|
// Plain data like "0x0000cd64:\tadd\tlr, pc, lr\n"
|
|
|
|
|
dl.address = line.left(posc).toULongLong(0, 0);
|
|
|
|
|
dl.function = m_lastFunction;
|
|
|
|
|
dl.offset = 0;
|
|
|
|
|
dl.data = line.mid(posc + 1).trimmed();
|
2011-04-12 17:32:41 +02:00
|
|
|
}
|
|
|
|
|
m_rowCache[dl.address] = m_data.size() + 1;
|
|
|
|
|
m_data.append(dl);
|
|
|
|
|
} else {
|
|
|
|
|
// Comment line.
|
|
|
|
|
DisassemblerLine dl;
|
|
|
|
|
dl.data = line;
|
|
|
|
|
m_data.append(dl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DisassemblerLine::toString() const
|
|
|
|
|
{
|
|
|
|
|
const QString someSpace = _(" ");
|
|
|
|
|
QString str;
|
|
|
|
|
if (isAssembler()) {
|
|
|
|
|
if (address)
|
2011-09-02 09:43:55 +02:00
|
|
|
str += _("0x%1 <+0x%2> ").arg(address, 0, 16)
|
|
|
|
|
.arg(offset, 4, 16, QLatin1Char('0'));
|
2011-04-12 17:32:41 +02:00
|
|
|
str += _(" ");
|
|
|
|
|
str += data;
|
|
|
|
|
} else if (isCode()) {
|
|
|
|
|
str += someSpace;
|
|
|
|
|
str += data;
|
|
|
|
|
} else {
|
|
|
|
|
str += someSpace;
|
|
|
|
|
str += data;
|
|
|
|
|
}
|
|
|
|
|
return str;
|
2010-11-24 16:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Debugger
|