forked from qt-creator/qt-creator
debugger: refactor thread related class
This commit is contained in:
@@ -42,69 +42,6 @@
|
||||
namespace Debugger {
|
||||
namespace Internal {
|
||||
|
||||
StackFrame::StackFrame()
|
||||
: level(0), line(0)
|
||||
{}
|
||||
|
||||
void StackFrame::clear()
|
||||
{
|
||||
line = level = 0;
|
||||
function.clear();
|
||||
file.clear();
|
||||
from.clear();
|
||||
to.clear();
|
||||
address.clear();
|
||||
}
|
||||
|
||||
bool StackFrame::isUsable() const
|
||||
{
|
||||
return !file.isEmpty() && QFileInfo(file).isReadable();
|
||||
}
|
||||
|
||||
QString StackFrame::toString() const
|
||||
{
|
||||
QString res;
|
||||
QTextStream str(&res);
|
||||
str << StackHandler::tr("Address:") << ' ' << address << ' '
|
||||
<< StackHandler::tr("Function:") << ' ' << function << ' '
|
||||
<< StackHandler::tr("File:") << ' ' << file << ' '
|
||||
<< StackHandler::tr("Line:") << ' ' << line << ' '
|
||||
<< StackHandler::tr("From:") << ' ' << from << ' '
|
||||
<< StackHandler::tr("To:") << ' ' << to;
|
||||
return res;
|
||||
}
|
||||
|
||||
QString StackFrame::toToolTip() const
|
||||
{
|
||||
QString res;
|
||||
QTextStream str(&res);
|
||||
str << "<html><body><table>"
|
||||
<< "<tr><td>" << StackHandler::tr("Address:") << "</td><td>" << address << "</td></tr>"
|
||||
<< "<tr><td>" << StackHandler::tr("Function:") << "</td><td>" << function << "</td></tr>"
|
||||
<< "<tr><td>" << StackHandler::tr("File:") << "</td><td>" << QDir::toNativeSeparators(file) << "</td></tr>"
|
||||
<< "<tr><td>" << StackHandler::tr("Line:") << "</td><td>" << line << "</td></tr>"
|
||||
<< "<tr><td>" << StackHandler::tr("From:") << "</td><td>" << from << "</td></tr>"
|
||||
<< "<tr><td>" << StackHandler::tr("To:") << "</td><td>" << to << "</td></tr>"
|
||||
<< "</table></body></html>";
|
||||
return res;
|
||||
}
|
||||
|
||||
QDebug operator<<(QDebug d, const StackFrame &f)
|
||||
{
|
||||
QString res;
|
||||
QTextStream str(&res);
|
||||
str << "level=" << f.level << " address=" << f.address;
|
||||
if (!f.function.isEmpty())
|
||||
str << ' ' << f.function;
|
||||
if (!f.file.isEmpty())
|
||||
str << ' ' << f.file << ':' << f.line;
|
||||
if (!f.from.isEmpty())
|
||||
str << " from=" << f.from;
|
||||
if (!f.to.isEmpty())
|
||||
str << " to=" << f.to;
|
||||
d.nospace() << res;
|
||||
return d;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
@@ -262,161 +199,5 @@ bool StackHandler::isDebuggingDebuggingHelpers() const
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ThreadsHandler
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ThreadData::ThreadData(int threadId)
|
||||
{
|
||||
notifyRunning();
|
||||
id = threadId;
|
||||
}
|
||||
|
||||
void ThreadData::notifyRunning()
|
||||
{
|
||||
address = 0;
|
||||
function.clear();
|
||||
fileName.clear();
|
||||
frameLevel = -1;
|
||||
state.clear();
|
||||
lineNumber = -1;
|
||||
}
|
||||
|
||||
enum { IdColumn, AddressColumn, FunctionColumn, FileColumn, LineColumn, ColumnCount };
|
||||
|
||||
ThreadsHandler::ThreadsHandler(QObject *parent) :
|
||||
QAbstractTableModel(parent),
|
||||
m_currentIndex(0),
|
||||
m_positionIcon(QLatin1String(":/debugger/images/location_16.png")),
|
||||
m_emptyIcon(QLatin1String(":/debugger/images/debugger_empty_14.png"))
|
||||
{
|
||||
}
|
||||
|
||||
int ThreadsHandler::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
// Since the stack is not a tree, row count is 0 for any valid parent
|
||||
return parent.isValid() ? 0 : m_threads.size();
|
||||
}
|
||||
|
||||
int ThreadsHandler::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
return parent.isValid() ? 0 : int(ColumnCount);
|
||||
}
|
||||
|
||||
QVariant ThreadsHandler::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
return QVariant();
|
||||
const int row = index.row();
|
||||
if (row >= m_threads.size())
|
||||
return QVariant();
|
||||
const ThreadData &thread = m_threads.at(row);
|
||||
|
||||
if (role == Qt::DisplayRole) {
|
||||
switch (index.column()) {
|
||||
case IdColumn:
|
||||
return thread.id;
|
||||
case FunctionColumn:
|
||||
return thread.function;
|
||||
case FileColumn:
|
||||
return thread.fileName;
|
||||
case LineColumn:
|
||||
return thread.lineNumber >= 0 ? QString::number(thread.lineNumber) : QString();
|
||||
case AddressColumn:
|
||||
return thread.address > 0 ? QLatin1String("0x") + QString::number(thread.address, 16) : QString();
|
||||
}
|
||||
} else if (role == Qt::ToolTipRole) {
|
||||
if (thread.address == 0)
|
||||
return tr("Thread: %1").arg(thread.id);
|
||||
// Stopped
|
||||
if (thread.fileName.isEmpty())
|
||||
return tr("Thread: %1 at %2 (0x%3)").arg(thread.id).arg(thread.function).arg(thread.address, 0, 16);
|
||||
return tr("Thread: %1 at %2, %3:%4 (0x%5)").
|
||||
arg(thread.id).arg(thread.function, thread.fileName).arg(thread.lineNumber).arg(thread.address, 0, 16);
|
||||
} else if (role == Qt::DecorationRole && index.column() == 0) {
|
||||
// Return icon that indicates whether this is the active stack frame
|
||||
return (index.row() == m_currentIndex) ? m_positionIcon : m_emptyIcon;
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QVariant ThreadsHandler::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
|
||||
return QVariant();
|
||||
switch (section) {
|
||||
case IdColumn:
|
||||
return tr("Thread ID");
|
||||
case FunctionColumn:
|
||||
return tr("Function");
|
||||
case FileColumn:
|
||||
return tr("File");
|
||||
case LineColumn:
|
||||
return tr("Line");
|
||||
case AddressColumn:
|
||||
return tr("Address");
|
||||
}
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
int ThreadsHandler::currentThreadId() const
|
||||
{
|
||||
if (m_currentIndex < 0 || m_currentIndex >= m_threads.size())
|
||||
return -1;
|
||||
return m_threads[m_currentIndex].id;
|
||||
}
|
||||
|
||||
void ThreadsHandler::setCurrentThread(int index)
|
||||
{
|
||||
if (index == m_currentIndex)
|
||||
return;
|
||||
|
||||
// Emit changed for previous frame
|
||||
QModelIndex i = ThreadsHandler::index(m_currentIndex, 0);
|
||||
emit dataChanged(i, i);
|
||||
|
||||
m_currentIndex = index;
|
||||
|
||||
// Emit changed for new frame
|
||||
i = ThreadsHandler::index(m_currentIndex, 0);
|
||||
emit dataChanged(i, i);
|
||||
}
|
||||
|
||||
void ThreadsHandler::setThreads(const QList<ThreadData> &threads)
|
||||
{
|
||||
m_threads = threads;
|
||||
if (m_currentIndex >= m_threads.size())
|
||||
m_currentIndex = m_threads.size() - 1;
|
||||
reset();
|
||||
}
|
||||
|
||||
QList<ThreadData> ThreadsHandler::threads() const
|
||||
{
|
||||
return m_threads;
|
||||
}
|
||||
|
||||
void ThreadsHandler::removeAll()
|
||||
{
|
||||
m_threads.clear();
|
||||
m_currentIndex = 0;
|
||||
reset();
|
||||
}
|
||||
|
||||
void ThreadsHandler::notifyRunning()
|
||||
{
|
||||
// Threads stopped (that is, address != 0 showing)?
|
||||
if (m_threads.empty())
|
||||
return;
|
||||
if (m_threads.front().address == 0)
|
||||
return;
|
||||
const QList<ThreadData>::iterator end = m_threads.end();
|
||||
for (QList<ThreadData>::iterator it = m_threads.begin(); it != end; ++it)
|
||||
it->notifyRunning();
|
||||
emit dataChanged(index(0, 1), index(m_threads.size()- 1, ColumnCount - 1));
|
||||
}
|
||||
|
||||
} // namespace Internal
|
||||
} // namespace Debugger
|
||||
|
||||
Reference in New Issue
Block a user