Debugger: Streamline ThreadHandler

- Use the TreeItem/data pattern recently introduced with Breakpoints
  to remove the need of keeping track of id/object mapping. Opens
  possibility to have thread groups as intermediate level.
- Use the ThreadHandler directly as model for the thread combobox
  to remove the need of manual combo box updates.
- Move setting current thread from individual engines to central code.

Change-Id: I030e21a4aa5ab30b0efbc84528d9cecf29cbbe30
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2018-08-15 17:28:00 +02:00
parent 7dcfe86c34
commit 9f9c72302f
15 changed files with 258 additions and 366 deletions

View File

@@ -53,168 +53,156 @@ namespace Internal {
//
///////////////////////////////////////////////////////////////////////
class ThreadItem : public TreeItem
// ThreadItem
ThreadItem::ThreadItem(const ThreadsHandler *handler, const ThreadData &data)
: threadData(data), handler(handler)
{}
QVariant ThreadItem::data(int column, int role) const
{
Q_DECLARE_TR_FUNCTIONS(Debugger::Internal::ThreadsHandler)
public:
ThreadItem(const ThreadsHandler *handler, const ThreadData &data = ThreadData())
: threadData(data), handler(handler)
{}
QVariant data(int column, int role) const override
{
switch (role) {
case Qt::DisplayRole:
return threadPart(column);
case Qt::ToolTipRole:
return threadToolTip();
case Qt::DecorationRole:
// Return icon that indicates whether this is the active stack frame.
if (column == 0)
return threadData.id == handler->currentThread() ? Icons::LOCATION.icon()
: Icons::EMPTY.icon();
break;
case ThreadData::IdRole:
return threadData.id.raw();
default:
break;
}
return QVariant();
switch (role) {
case Qt::DisplayRole:
if (column == 0)
return QString("#%1 %2").arg(threadData.id).arg(threadData.name);
return threadPart(column);
case Qt::ToolTipRole:
return threadToolTip();
case Qt::DecorationRole:
// Return icon that indicates whether this is the active stack frame.
if (column == 0)
return this == handler->currentThread() ? Icons::LOCATION.icon()
: Icons::EMPTY.icon();
break;
default:
break;
}
return QVariant();
}
Qt::ItemFlags flags(int column) const override
{
return threadData.stopped ? TreeItem::flags(column) : Qt::ItemFlags({});
Qt::ItemFlags ThreadItem::flags(int column) const
{
return threadData.stopped ? TreeItem::flags(column) : Qt::ItemFlags({});
}
QString ThreadItem::threadToolTip() const
{
const char start[] = "<tr><td>";
const char sep[] = "</td><td>";
const char end[] = "</td>";
QString rc;
QTextStream str(&rc);
str << "<html><head/><body><table>"
<< start << ThreadsHandler::tr("Thread&nbsp;id:")
<< sep << threadData.id << end;
if (!threadData.targetId.isEmpty())
str << start << ThreadsHandler::tr("Target&nbsp;id:")
<< sep << threadData.targetId << end;
if (!threadData.groupId.isEmpty())
str << start << ThreadsHandler::tr("Group&nbsp;id:")
<< sep << threadData.groupId << end;
if (!threadData.name.isEmpty())
str << start << ThreadsHandler::tr("Name:")
<< sep << threadData.name << end;
if (!threadData.state.isEmpty())
str << start << ThreadsHandler::tr("State:")
<< sep << threadData.state << end;
if (!threadData.core.isEmpty())
str << start << ThreadsHandler::tr("Core:")
<< sep << threadData.core << end;
if (threadData.address) {
str << start << ThreadsHandler::tr("Stopped&nbsp;at:") << sep;
if (!threadData.function.isEmpty())
str << threadData.function << "<br>";
if (!threadData.fileName.isEmpty())
str << threadData.fileName << ':' << threadData.lineNumber << "<br>";
str << formatToolTipAddress(threadData.address);
}
str << "</table></body></html>";
return rc;
}
QString threadToolTip() const
{
const char start[] = "<tr><td>";
const char sep[] = "</td><td>";
const char end[] = "</td>";
QString rc;
QTextStream str(&rc);
str << "<html><head/><body><table>"
<< start << ThreadsHandler::tr("Thread&nbsp;id:")
<< sep << threadData.id.raw() << end;
if (!threadData.targetId.isEmpty())
str << start << ThreadsHandler::tr("Target&nbsp;id:")
<< sep << threadData.targetId << end;
if (!threadData.groupId.isEmpty())
str << start << ThreadsHandler::tr("Group&nbsp;id:")
<< sep << threadData.groupId << end;
if (!threadData.name.isEmpty())
str << start << ThreadsHandler::tr("Name:")
<< sep << threadData.name << end;
if (!threadData.state.isEmpty())
str << start << ThreadsHandler::tr("State:")
<< sep << threadData.state << end;
if (!threadData.core.isEmpty())
str << start << ThreadsHandler::tr("Core:")
<< sep << threadData.core << end;
if (threadData.address) {
str << start << ThreadsHandler::tr("Stopped&nbsp;at:") << sep;
if (!threadData.function.isEmpty())
str << threadData.function << "<br>";
if (!threadData.fileName.isEmpty())
str << threadData.fileName << ':' << threadData.lineNumber << "<br>";
str << formatToolTipAddress(threadData.address);
}
str << "</table></body></html>";
return rc;
QVariant ThreadItem::threadPart(int column) const
{
switch (column) {
case ThreadData::IdColumn:
return threadData.id;
case ThreadData::FunctionColumn:
return threadData.function;
case ThreadData::FileColumn:
return threadData.fileName.isEmpty() ? threadData.module : threadData.fileName;
case ThreadData::LineColumn:
return threadData.lineNumber >= 0
? QString::number(threadData.lineNumber) : QString();
case ThreadData::AddressColumn:
return threadData.address > 0
? QLatin1String("0x") + QString::number(threadData.address, 16)
: QString();
case ThreadData::CoreColumn:
return threadData.core;
case ThreadData::StateColumn:
return threadData.state;
case ThreadData::TargetIdColumn:
if (threadData.targetId.startsWith(QLatin1String("Thread ")))
return threadData.targetId.mid(7);
return threadData.targetId;
case ThreadData::NameColumn:
return threadData.name;
case ThreadData::DetailsColumn:
return threadData.details;
case ThreadData::ComboNameColumn:
return QString::fromLatin1("#%1 %2").arg(threadData.id).arg(threadData.name);
}
return QVariant();
}
QVariant threadPart(int column) const
{
switch (column) {
case ThreadData::IdColumn:
return threadData.id.raw();
case ThreadData::FunctionColumn:
return threadData.function;
case ThreadData::FileColumn:
return threadData.fileName.isEmpty() ? threadData.module : threadData.fileName;
case ThreadData::LineColumn:
return threadData.lineNumber >= 0
? QString::number(threadData.lineNumber) : QString();
case ThreadData::AddressColumn:
return threadData.address > 0
? QLatin1String("0x") + QString::number(threadData.address, 16)
: QString();
case ThreadData::CoreColumn:
return threadData.core;
case ThreadData::StateColumn:
return threadData.state;
case ThreadData::TargetIdColumn:
if (threadData.targetId.startsWith(QLatin1String("Thread ")))
return threadData.targetId.mid(7);
return threadData.targetId;
case ThreadData::NameColumn:
return threadData.name;
case ThreadData::DetailsColumn:
return threadData.details;
case ThreadData::ComboNameColumn:
return QString::fromLatin1("#%1 %2").arg(threadData.id.raw()).arg(threadData.name);
}
return QVariant();
}
void ThreadItem::notifyRunning() // Clear state information.
{
threadData.address = 0;
threadData.function.clear();
threadData.fileName.clear();
threadData.frameLevel = -1;
threadData.state.clear();
threadData.lineNumber = -1;
threadData.stopped = false;
update();
}
void notifyRunning() // Clear state information.
{
threadData.address = 0;
threadData.function.clear();
threadData.fileName.clear();
threadData.frameLevel = -1;
threadData.state.clear();
threadData.lineNumber = -1;
threadData.stopped = false;
update();
}
void ThreadItem::notifyStopped()
{
threadData.stopped = true;
update();
}
void notifyStopped()
{
threadData.stopped = true;
update();
}
void mergeThreadData(const ThreadData &other)
{
if (!other.core.isEmpty())
threadData.core = other.core;
if (!other.fileName.isEmpty())
threadData.fileName = other.fileName;
if (!other.targetId.isEmpty())
threadData.targetId = other.targetId;
if (!other.name.isEmpty())
threadData.name = other.name;
if (other.frameLevel != -1)
threadData.frameLevel = other.frameLevel;
if (!other.function.isEmpty())
threadData.function = other.function;
if (other.address)
threadData.address = other.address;
if (!other.module.isEmpty())
threadData.module = other.module;
if (!other.details.isEmpty())
threadData.details = other.details;
if (!other.state.isEmpty())
threadData.state = other.state;
if (other.lineNumber != -1)
threadData.lineNumber = other.lineNumber;
update();
}
void ThreadItem::mergeThreadData(const ThreadData &other)
{
if (!other.core.isEmpty())
threadData.core = other.core;
if (!other.fileName.isEmpty())
threadData.fileName = other.fileName;
if (!other.targetId.isEmpty())
threadData.targetId = other.targetId;
if (!other.name.isEmpty())
threadData.name = other.name;
if (other.frameLevel != -1)
threadData.frameLevel = other.frameLevel;
if (!other.function.isEmpty())
threadData.function = other.function;
if (other.address)
threadData.address = other.address;
if (!other.module.isEmpty())
threadData.module = other.module;
if (!other.details.isEmpty())
threadData.details = other.details;
if (!other.state.isEmpty())
threadData.state = other.state;
if (other.lineNumber != -1)
threadData.lineNumber = other.lineNumber;
update();
}
public:
ThreadData threadData;
const ThreadsHandler * const handler;
};
////////////////////////////////////////////////////////////////////////
//
// ThreadsHandler
//
///////////////////////////////////////////////////////////////////////
/*!
\class Debugger::Internal::ThreadData
@@ -235,17 +223,17 @@ ThreadsHandler::ThreadsHandler(DebuggerEngine *engine)
{
setObjectName(QLatin1String("ThreadsModel"));
setHeader({
QLatin1String(" ") + tr("ID") + QLatin1String(" "),
tr("Address"), tr("Function"), tr("File"), tr("Line"), tr("State"),
tr("Name"), tr("Target ID"), tr("Details"), tr("Core"),
});
QLatin1String(" ") + tr("ID") + QLatin1String(" "),
tr("Address"), tr("Function"), tr("File"), tr("Line"), tr("State"),
tr("Name"), tr("Target ID"), tr("Details"), tr("Core"),
});
}
bool ThreadsHandler::setData(const QModelIndex &idx, const QVariant &data, int role)
{
if (role == BaseTreeView::ItemActivatedRole) {
ThreadId id = ThreadId(idx.data(ThreadData::IdRole).toLongLong());
m_engine->selectThread(id);
const Thread thread = itemForIndexAtLevel<1>(idx);
m_engine->selectThread(thread);
return true;
}
@@ -263,21 +251,9 @@ bool ThreadsHandler::setData(const QModelIndex &idx, const QVariant &data, int r
return false;
}
static ThreadItem *itemForThreadId(const ThreadsHandler *handler, ThreadId threadId)
{
const auto matcher = [threadId](ThreadItem *item) { return item->threadData.id == threadId; };
return handler->findItemAtLevel<1>(matcher);
}
static int indexForThreadId(const ThreadsHandler *handler, ThreadId threadId)
{
ThreadItem *item = itemForThreadId(handler, threadId);
return item ? handler->rootItem()->indexOf(item) : -1;
}
int ThreadsHandler::currentThreadIndex() const
{
return indexForThreadId(this, m_currentId);
return rootItem()->indexOf(m_currentThread);
}
void ThreadsHandler::sort(int column, Qt::SortOrder order)
@@ -294,36 +270,29 @@ void ThreadsHandler::sort(int column, Qt::SortOrder order)
});
}
ThreadId ThreadsHandler::currentThread() const
Thread ThreadsHandler::currentThread() const
{
return m_currentId;
return m_currentThread;
}
ThreadId ThreadsHandler::threadAt(int index) const
Thread ThreadsHandler::threadAt(int index) const
{
QTC_ASSERT(index >= 0 && index < rootItem()->childCount(), return ThreadId());
return rootItem()->childAt(index)->threadData.id;
QTC_ASSERT(index >= 0 && index < rootItem()->childCount(), return Thread());
return rootItem()->childAt(index);
}
void ThreadsHandler::setCurrentThread(ThreadId id)
void ThreadsHandler::setCurrentThread(const Thread &thread)
{
if (id == m_currentId)
if (thread == m_currentThread)
return;
ThreadItem *newItem = itemForThreadId(this, id);
if (!newItem) {
qWarning("ThreadsHandler::setCurrentThreadId: No such thread %d.", int(id.raw()));
if (!threadForId(thread->id())) {
qWarning("ThreadsHandler::setCurrentThreadId: No such thread %s.", qPrintable(thread->id()));
return;
}
ThreadItem *oldItem = itemForThreadId(this, m_currentId);
m_currentId = id;
if (oldItem)
oldItem->update();
newItem->update();
updateThreadBox();
m_currentThread = thread;
thread->update();
}
QString ThreadsHandler::pidForGroupId(const QString &groupId) const
@@ -338,43 +307,16 @@ void ThreadsHandler::notifyGroupCreated(const QString &groupId, const QString &p
void ThreadsHandler::updateThread(const ThreadData &threadData)
{
if (ThreadItem *item = itemForThreadId(this, threadData.id))
item->mergeThreadData(threadData);
if (Thread thread = threadForId(threadData.id))
thread->mergeThreadData(threadData);
else
rootItem()->appendChild(new ThreadItem(this, threadData));
}
void ThreadsHandler::removeThread(ThreadId threadId)
void ThreadsHandler::removeThread(const QString &id)
{
if (ThreadItem *item = itemForThreadId(this, threadId))
destroyItem(item);
}
void ThreadsHandler::setThreads(const Threads &threads)
{
auto root = new ThreadItem(this);
for (int i = 0, n = threads.size(); i < n; ++i)
root->appendChild(new ThreadItem(this, threads.at(i)));
rootItem()->removeChildren();
setRootItem(root);
m_resetLocationScheduled = false;
updateThreadBox();
}
void ThreadsHandler::updateThreadBox()
{
QStringList list;
forItemsAtLevel<1>([&list](ThreadItem *item) {
list.append(QString::fromLatin1("#%1 %2").arg(item->threadData.id.raw()).arg(item->threadData.name));
});
m_engine->setThreadBoxContents(list, indexForThreadId(this, m_currentId));
}
ThreadData ThreadsHandler::thread(ThreadId id) const
{
if (ThreadItem *item = itemForThreadId(this, id))
return item->threadData;
return ThreadData();
if (Thread thread = threadForId(id))
destroyItem(thread);
}
void ThreadsHandler::removeAll()
@@ -396,54 +338,27 @@ bool ThreadsHandler::notifyGroupExited(const QString &groupId)
return m_pidForGroupId.isEmpty();
}
void ThreadsHandler::notifyRunning(const QString &data)
Thread ThreadsHandler::threadForId(const QString &id) const
{
if (data.isEmpty() || data == "all") {
notifyAllRunning();
} else {
bool ok;
qlonglong id = data.toLongLong(&ok);
if (ok)
notifyRunning(ThreadId(id));
else // FIXME
notifyAllRunning();
}
return findItemAtLevel<1>([id](const Thread &item) {
return item->threadData.id == id;
});
}
void ThreadsHandler::notifyAllRunning()
void ThreadsHandler::notifyRunning(const QString &id)
{
forItemsAtLevel<1>([](ThreadItem *item) { item->notifyRunning(); });
if (id.isEmpty() || id == "all")
forItemsAtLevel<1>([](const Thread &thread) { thread->notifyRunning(); });
else if (Thread thread = threadForId(id))
thread->notifyRunning();
}
void ThreadsHandler::notifyRunning(ThreadId threadId)
void ThreadsHandler::notifyStopped(const QString &id)
{
if (ThreadItem *item = itemForThreadId(this, threadId))
item->notifyRunning();
}
void ThreadsHandler::notifyStopped(const QString &data)
{
if (data.isEmpty() || data == "all") {
notifyAllStopped();
} else {
bool ok;
qlonglong id = data.toLongLong(&ok);
if (ok)
notifyRunning(ThreadId(id));
else // FIXME
notifyAllStopped();
}
}
void ThreadsHandler::notifyAllStopped()
{
forItemsAtLevel<1>([](ThreadItem *item) { item->notifyStopped(); });
}
void ThreadsHandler::notifyStopped(ThreadId threadId)
{
if (ThreadItem *item = itemForThreadId(this, threadId))
item->notifyStopped();
if (id.isEmpty() || id == "all")
forItemsAtLevel<1>([](const Thread &thread) { thread->notifyStopped(); });
else if (Thread thread = threadForId(id))
thread->notifyStopped();
}
void ThreadsHandler::updateThreads(const GdbMi &data)
@@ -453,13 +368,11 @@ void ThreadsHandler::updateThreads(const GdbMi &data)
// file="/.../app.cpp",fullname="/../app.cpp",line="1175"},
// state="stopped",core="0"}],current-thread-id="1"
const QVector<GdbMi> items = data["threads"].children();
const int n = int(items.size());
for (int index = 0; index != n; ++index) {
const GdbMi item = items[index];
const GdbMi frame = item["frame"];
const QVector<GdbMi> &items = data["threads"].children();
for (const GdbMi &item : items) {
const GdbMi &frame = item["frame"];
ThreadData thread;
thread.id = ThreadId(item["id"].toInt());
thread.id = item["id"].data();
thread.targetId = item["target-id"].data();
thread.details = item["details"].data();
thread.core = item["core"].data();
@@ -474,10 +387,8 @@ void ThreadsHandler::updateThreads(const GdbMi &data)
updateThread(thread);
}
const GdbMi current = data["current-thread-id"];
m_currentId = current.isValid() ? ThreadId(current.data().toLongLong()) : ThreadId();
updateThreadBox();
const QString &currentId = data["current-thread-id"].data();
m_currentThread = threadForId(currentId);
}
void ThreadsHandler::scheduleResetLocation()