Commit 0d9a56ab authored by hjk's avatar hjk

Debugger: Rework thread data aquisition

Listen to gdb/MI notifications when possible. This is more
granular, gives access to the thread group id and takes
care of thread changes at any time.

Change-Id: I8c67f6f19b204059deefdf7e07fe3b4ce13ed963
Reviewed-by: default avatarFriedemann Kleint <Friedemann.Kleint@digia.com>
parent 617f74aa
......@@ -1302,15 +1302,6 @@ void CdbEngine::assignValueInDebugger(const WatchData *w, const QString &expr, c
updateLocals();
}
void CdbEngine::parseThreads(const GdbMi &data, int forceCurrentThreadId /* = -1 */)
{
int currentThreadId;
Threads threads = ThreadsHandler::parseGdbmiThreads(data, &currentThreadId);
threadsHandler()->setThreads(threads);
threadsHandler()->setCurrentThreadId(forceCurrentThreadId >= 0 ?
forceCurrentThreadId : currentThreadId);
}
void CdbEngine::handleThreads(const CdbExtensionCommandPtr &reply)
{
if (debug)
......@@ -1318,7 +1309,7 @@ void CdbEngine::handleThreads(const CdbExtensionCommandPtr &reply)
if (reply->success) {
GdbMi data;
data.fromString(reply->reply);
parseThreads(data);
threadsHandler()->updateThreads(data);
// Continue sequence
postCommandSequence(reply->commandSequence);
} else {
......@@ -1514,15 +1505,14 @@ void CdbEngine::updateLocals(bool forNewStackFrame)
QVariant(flags));
}
void CdbEngine::selectThread(int index)
void CdbEngine::selectThread(ThreadId threadId)
{
if (index < 0 || index == threadsHandler()->currentThread())
if (!threadId.isValid() || threadId == threadsHandler()->currentThread())
return;
const int newThreadId = threadsHandler()->threads().at(index).id;
threadsHandler()->setCurrentThread(index);
threadsHandler()->setCurrentThread(threadId);
const QByteArray cmd = '~' + QByteArray::number(newThreadId) + " s";
const QByteArray cmd = '~' + QByteArray::number(threadId.raw()) + " s";
postBuiltinCommand(cmd, 0, &CdbEngine::dummyHandler, CommandListStack);
}
......@@ -2179,7 +2169,7 @@ void CdbEngine::processStop(const GdbMi &stopReason, bool conditionalBreakPointT
// Further examine stop and report to user
QString message;
QString exceptionBoxMessage;
int forcedThreadId = -1;
ThreadId forcedThreadId;
const unsigned stopFlags = examineStopReason(stopReason, &message, &exceptionBoxMessage,
conditionalBreakPointTriggered);
// Do the non-blocking log reporting
......@@ -2216,7 +2206,7 @@ void CdbEngine::processStop(const GdbMi &stopReason, bool conditionalBreakPointT
if (stopFlags & StopInArtificialThread) {
showMessage(tr("Switching to main thread..."), LogMisc);
postCommand("~0 s", 0);
forcedThreadId = 0;
forcedThreadId = ThreadId(0);
// Re-fetch stack again.
postCommandSequence(CommandListStack);
} else {
......@@ -2232,7 +2222,9 @@ void CdbEngine::processStop(const GdbMi &stopReason, bool conditionalBreakPointT
}
const GdbMi threads = stopReason.findChild("threads");
if (threads.isValid()) {
parseThreads(threads, forcedThreadId);
threadsHandler()->updateThreads(threads);
if (forcedThreadId.isValid())
threadsHandler()->setCurrentThread(forcedThreadId);
} else {
showMessage(QString::fromLatin1(stopReason.findChild("threaderror").data()), LogError);
}
......@@ -2938,7 +2930,7 @@ void CdbEngine::postCommandSequence(unsigned mask)
return;
}
if (mask & CommandListRegisters) {
QTC_ASSERT(threadsHandler()->currentThread() >= 0, return);
QTC_ASSERT(threadsHandler()->currentThreadIndex() >= 0, return);
postExtensionCommand("registers", QByteArray(), 0, &CdbEngine::handleRegisters, mask & ~CommandListRegisters);
return;
}
......
......@@ -32,6 +32,7 @@
#include "debuggerengine.h"
#include "breakpoint.h"
#include "threaddata.h"
#include <QSharedPointer>
#include <QProcess>
......@@ -113,7 +114,7 @@ public:
virtual void executeDebuggerCommand(const QString &command, DebuggerLanguages languages);
virtual void activateFrame(int index);
virtual void selectThread(int index);
virtual void selectThread(ThreadId threadId);
virtual bool stateAcceptsBreakpointChanges() const;
virtual bool acceptsBreakpoint(BreakpointModelId id) const;
......@@ -242,7 +243,6 @@ private:
int elapsedLogTime() const;
void addLocalsOptions(ByteArrayInputStream &s) const;
unsigned parseStackTrace(const GdbMi &data, bool sourceStepInto);
void parseThreads(const GdbMi &, int forceCurrentThreadId = -1);
const QByteArray m_creatorExtPrefix;
const QByteArray m_tokenPrefix;
......
......@@ -263,7 +263,7 @@ static inline bool parseThread(QByteArray line, ThreadData *thread, bool *curren
thread->targetId = QLatin1String("0x") + QString::fromLatin1(pidTid.mid(dotPos + 1));
}
case 1:
thread->id = tokens.at(0).toInt();
thread->id = ThreadId(tokens.at(0).toInt());
break;
} // switch size
return true;
......
......@@ -1233,10 +1233,6 @@ void DebuggerEngine::setState(DebuggerState state, bool forced)
handler->notifyBreakpointReleased(id);
}
const bool running = d->m_state == InferiorRunOk;
if (running)
threadsHandler()->notifyRunning();
showMessage(msg, LogDebug);
updateViews();
......@@ -1586,10 +1582,6 @@ void DebuggerEngine::changeBreakpoint(BreakpointModelId id)
QTC_CHECK(false);
}
void DebuggerEngine::selectThread(int)
{
}
void DebuggerEngine::assignValueInDebugger(const WatchData *,
const QString &, const QVariant &)
{
......
......@@ -32,7 +32,8 @@
#include "debugger_global.h"
#include "debuggerconstants.h"
#include "breakpoint.h" // For 'BreakpointId'
#include "breakpoint.h" // For BreakpointModelId.
#include "threaddata.h" // For ThreadId.
#include <QObject>
#include <QStringList>
......@@ -205,7 +206,7 @@ public:
virtual bool acceptsDebuggerCommands() const { return true; }
virtual void assignValueInDebugger(const Internal::WatchData *data,
const QString &expr, const QVariant &value);
virtual void selectThread(int index);
virtual void selectThread(Internal::ThreadId threadId) = 0;
virtual Internal::ModulesHandler *modulesHandler() const;
virtual Internal::RegisterHandler *registerHandler() const;
......
......@@ -494,6 +494,7 @@ public:
bool hasCapability(unsigned cap) const;
bool acceptsBreakpoint(BreakpointModelId) const { return false; }
bool acceptsDebuggerCommands() const { return false; }
void selectThread(ThreadId) {}
};
bool DummyEngine::hasCapability(unsigned cap) const
......@@ -781,7 +782,8 @@ public slots:
void selectThread(int index)
{
currentEngine()->selectThread(index);
ThreadId id = m_currentEngine->threadsHandler()->threadAt(index);
m_currentEngine->selectThread(id);
}
void breakpointSetMarginActionTriggered()
......@@ -2205,7 +2207,7 @@ void DebuggerPluginPrivate::updateState(DebuggerEngine *engine)
QTC_ASSERT(m_returnWindow->model(), return);
QTC_ASSERT(!engine->isSlaveEngine(), return);
m_threadBox->setCurrentIndex(engine->threadsHandler()->currentThread());
m_threadBox->setCurrentIndex(engine->threadsHandler()->currentThreadIndex());
engine->watchHandler()->updateWatchersWindow();
const DebuggerState state = engine->state();
......
......@@ -40,7 +40,7 @@ namespace Internal {
QDataStream &operator<<(QDataStream &stream, const ThreadData &d)
{
stream << (qint64)d.id;
stream << d.id.raw();
stream << d.address;
stream << d.function;
stream << d.fileName;
......@@ -54,7 +54,7 @@ QDataStream &operator>>(QDataStream &stream, ThreadData &d)
{
qint64 id;
stream >> id;
d.id = id;
d.id = ThreadId(id);
stream >> d.address;
stream >> d.function;
stream >> d.fileName;
......
......@@ -467,6 +467,8 @@ void GdbEngine::handleResponse(const QByteArray &buff)
m_pendingLogStreamOutput.clear();
m_pendingConsoleStreamOutput.clear();
} else if (asyncClass == "running") {
GdbMi threads = result.findChild("thread-id");
threadsHandler()->notifyRunning(threads.data());
if (state() == InferiorRunOk || state() == InferiorSetupRequested) {
// We get multiple *running after thread creation and in Windows terminals.
showMessage(QString::fromLatin1("NOTE: INFERIOR STILL RUNNING IN STATE %1.").
......@@ -527,6 +529,10 @@ void GdbEngine::handleResponse(const QByteArray &buff)
//"{id="1",group-id="28902"}"
QByteArray id = result.findChild("id").data();
showStatusMessage(tr("Thread %1 created").arg(_(id)), 1000);
ThreadData thread;
thread.id = ThreadId(id.toLong());
thread.groupId = result.findChild("group-id").data();
threadsHandler()->updateThread(thread);
} else if (asyncClass == "thread-group-exited") {
// Archer has "{id="28902"}"
QByteArray id = result.findChild("id").data();
......@@ -538,6 +544,7 @@ void GdbEngine::handleResponse(const QByteArray &buff)
QByteArray groupid = result.findChild("group-id").data();
showStatusMessage(tr("Thread %1 in group %2 exited")
.arg(_(id)).arg(_(groupid)), 1000);
threadsHandler()->removeThread(ThreadId(id.toLong()));
} else if (asyncClass == "thread-selected") {
QByteArray id = result.findChild("id").data();
showStatusMessage(tr("Thread %1 selected").arg(_(id)), 1000);
......@@ -1397,6 +1404,9 @@ void GdbEngine::handleStopResponse(const GdbMi &data)
return;
}
GdbMi threads = data.findChild("stopped-thread");
threadsHandler()->notifyStopped(threads.data());
const QByteArray reason = data.findChild("reason").data();
if (isExitedReason(reason)) {
......@@ -1748,10 +1758,6 @@ void GdbEngine::handleStop2(const GdbMi &data)
// Let the event loop run before deciding whether to update the stack.
m_stackNeeded = true; // setTokenBarrier() might reset this.
if (isStopperThread)
m_currentThreadId = 0;
else
m_currentThreadId = data.findChild("thread-id").data().toInt();
QTimer::singleShot(0, this, SLOT(handleStop2()));
}
......@@ -3489,15 +3495,12 @@ void GdbEngine::reloadSourceFilesInternal()
//
//////////////////////////////////////////////////////////////////////
void GdbEngine::selectThread(int index)
void GdbEngine::selectThread(ThreadId threadId)
{
threadsHandler()->setCurrentThread(index);
Threads threads = threadsHandler()->threads();
QTC_ASSERT(index < threads.size(), return);
const int id = threads.at(index).id;
threadsHandler()->setCurrentThread(threadId);
showStatusMessage(tr("Retrieving data for stack view thread 0x%1...")
.arg(id, 0, 16), 10000);
postCommand("-thread-select " + QByteArray::number(id), Discardable,
.arg(threadId.raw(), 0, 16), 10000);
postCommand("-thread-select " + QByteArray::number(threadId.raw()), Discardable,
CB(handleStackSelectThread));
}
......@@ -3641,11 +3644,7 @@ void GdbEngine::handleStackSelectFrame(const GdbResponse &response)
void GdbEngine::handleThreadInfo(const GdbResponse &response)
{
if (response.resultClass == GdbResultDone) {
int currentThreadId;
const Threads threads =
ThreadsHandler::parseGdbmiThreads(response.data, &currentThreadId);
threadsHandler()->setThreads(threads);
threadsHandler()->setCurrentThreadId(currentThreadId);
threadsHandler()->updateThreads(response.data);
updateViews(); // Adjust Threads combobox.
if (m_hasInferiorThreadList && debuggerCore()->boolSetting(ShowThreadNames)) {
postCommand("threadnames " +
......@@ -3663,37 +3662,28 @@ void GdbEngine::handleThreadListIds(const GdbResponse &response)
{
// "72^done,{thread-ids={thread-id="2",thread-id="1"},number-of-threads="2"}
// In gdb 7.1+ additionally: current-thread-id="1"
ThreadsHandler *handler = threadsHandler();
const QList<GdbMi> items = response.data.findChild("thread-ids").children();
Threads threads;
for (int index = 0, n = items.size(); index != n; ++index) {
ThreadData thread;
thread.id = items.at(index).data().toInt();
threads.append(thread);
thread.id = ThreadId(items.at(index).data().toInt());
handler->updateThread(thread);
}
threadsHandler()->setThreads(threads);
threadsHandler()->setCurrentThreadId(m_currentThreadId);
}
void GdbEngine::handleThreadNames(const GdbResponse &response)
{
if (response.resultClass == GdbResultDone) {
ThreadsHandler *handler = threadsHandler();
GdbMi names;
names.fromString(response.consoleStreamOutput);
Threads threads = threadsHandler()->threads();
foreach (const GdbMi &name, names.children()) {
int id = name.findChild("id").data().toInt();
for (int index = 0, n = threads.size(); index != n; ++index) {
ThreadData &thread = threads[index];
if (thread.id == quint64(id)) {
thread.name = decodeData(name.findChild("value").data(),
name.findChild("valueencoded").data().toInt());
break;
}
}
ThreadData thread;
thread.id = ThreadId(name.findChild("id").data().toInt());
thread.name = decodeData(name.findChild("value").data(),
name.findChild("valueencoded").data().toInt());
handler->updateThread(thread);
}
threadsHandler()->setThreads(threads);
updateViews();
}
}
......
......@@ -35,6 +35,7 @@
#include "stackframe.h"
#include "watchhandler.h"
#include "watchutils.h"
#include "threaddata.h"
#include <QByteArray>
#include <QProcess>
......@@ -437,7 +438,7 @@ private: ////////// Inferior Management //////////
private: ////////// View & Data Stuff //////////
protected:
void selectThread(int index);
void selectThread(ThreadId threadId);
void activateFrame(int index);
void resetLocation();
......@@ -664,7 +665,6 @@ protected:
// For short-circuiting stack and thread list evaluation.
bool m_stackNeeded;
int m_currentThreadId;
//
// Qml
......
......@@ -208,7 +208,6 @@ void IPCEngineHost::executeJumpToLine(const ContextData &data)
rpcCall(ExecuteJumpToLine, p);
}
void IPCEngineHost::activateFrame(int index)
{
resetLocation();
......@@ -221,16 +220,15 @@ void IPCEngineHost::activateFrame(int index)
rpcCall(ActivateFrame, p);
}
void IPCEngineHost::selectThread(int index)
void IPCEngineHost::selectThread(ThreadId id)
{
resetLocation();
Threads threads = threadsHandler()->threads();
QTC_ASSERT(index < threads.size(), return);
QTC_ASSERT(id.isValid(), return);
QByteArray p;
{
QDataStream s(&p, QIODevice::WriteOnly);
SET_NATIVE_BYTE_ORDER(s);
s << quint64(threads.at(index).id);
s << id.raw();
}
rpcCall(SelectThread, p);
}
......@@ -442,7 +440,7 @@ void IPCEngineHost::rpcCallback(quint64 f, QByteArray payload)
SET_NATIVE_BYTE_ORDER(s);
quint64 token;
s >> token;
threadsHandler()->setCurrentThreadId(token);
threadsHandler()->setCurrentThread(ThreadId(token));
}
break;
case IPCEngineGuest::ListFrames:
......
......@@ -104,7 +104,7 @@ public:
void executeRunToFunction(const QString &functionName);
void executeJumpToLine(const ContextData &data);
void activateFrame(int index);
void selectThread(int index);
void selectThread(ThreadId index);
void fetchDisassembler(DisassemblerAgent *);
bool acceptsBreakpoint(BreakpointModelId) const { return true; } // FIXME
void insertBreakpoint(BreakpointModelId id);
......
......@@ -321,9 +321,9 @@ void PdbEngine::activateFrame(int frameIndex)
gotoLocation(handler->currentFrame());
}
void PdbEngine::selectThread(int index)
void PdbEngine::selectThread(ThreadId threadId)
{
Q_UNUSED(index)
Q_UNUSED(threadId)
}
bool PdbEngine::acceptsBreakpoint(BreakpointModelId id) const
......
......@@ -86,7 +86,7 @@ private:
void executeJumpToLine(const ContextData &data);
void activateFrame(int index);
void selectThread(int index);
void selectThread(ThreadId threadId);
bool acceptsBreakpoint(BreakpointModelId id) const;
void insertBreakpoint(BreakpointModelId id);
......
......@@ -267,9 +267,9 @@ bool QmlCppEngine::acceptsBreakpoint(BreakpointModelId id) const
|| d->m_qmlEngine->acceptsBreakpoint(id);
}
void QmlCppEngine::selectThread(int index)
void QmlCppEngine::selectThread(ThreadId threadId)
{
d->m_activeEngine->selectThread(index);
d->m_activeEngine->selectThread(threadId);
}
void QmlCppEngine::assignValueInDebugger(const WatchData *data,
......
......@@ -77,7 +77,7 @@ public:
void attemptBreakpointSynchronization();
bool acceptsBreakpoint(BreakpointModelId id) const;
void selectThread(int index);
void selectThread(ThreadId threadId);
void assignValueInDebugger(const WatchData *data,
const QString &expr, const QVariant &value);
......
......@@ -778,9 +778,9 @@ void QmlEngine::activateFrame(int index)
gotoLocation(stackHandler()->frames().value(index));
}
void QmlEngine::selectThread(int index)
void QmlEngine::selectThread(ThreadId threadId)
{
Q_UNUSED(index)
Q_UNUSED(threadId)
}
void QmlEngine::insertBreakpoint(BreakpointModelId id)
......
......@@ -139,7 +139,7 @@ private:
void executeJumpToLine(const ContextData &data);
void activateFrame(int index);
void selectThread(int index);
void selectThread(ThreadId threadId);
void attemptBreakpointSynchronization();
void removeBreakpoint(BreakpointModelId id);
......
......@@ -438,9 +438,9 @@ void ScriptEngine::activateFrame(int index)
Q_UNUSED(index)
}
void ScriptEngine::selectThread(int index)
void ScriptEngine::selectThread(ThreadId threadId)
{
Q_UNUSED(index)
Q_UNUSED(threadId)
}
bool ScriptEngine::acceptsBreakpoint(BreakpointModelId id) const
......
......@@ -85,7 +85,7 @@ private:
void executeJumpToLine(const ContextData &data);
void activateFrame(int index);
void selectThread(int index);
void selectThread(ThreadId threadId);
bool acceptsBreakpoint(BreakpointModelId id) const;
void attemptBreakpointSynchronization();
......
......@@ -30,24 +30,49 @@
#ifndef THREADDATA_H
#define THREADDATA_H
#include <QVector>
#include <QString>
#include <QVector>
namespace Debugger {
namespace Internal {
////////////////////////////////////////////////////////////////////////
//
// ThreadId
//
////////////////////////////////////////////////////////////////////////
/*! A typesafe identifier. */
class ThreadId
{
public:
ThreadId() : m_id(-1) {}
explicit ThreadId(qint64 id) : m_id(id) {}
bool isValid() const { return m_id != -1; }
qint64 raw() const { return m_id; }
bool operator==(const ThreadId other) const { return m_id == other.m_id; }
private:
qint64 m_id;
};
////////////////////////////////////////////////////////////////////////
//
// ThreadData
//
////////////////////////////////////////////////////////////////////////
/*! A structure containing information about a single thread */
/*! A structure containing information about a single thread. */
struct ThreadData
{
ThreadData(quint64 threadid = 0)
: id(threadid), frameLevel(-1), address (0), lineNumber(-1)
{}
ThreadData()
{
frameLevel = -1;
lineNumber = -1;
address = 0;
stopped = true;
}
enum {
IdColumn,
......@@ -61,7 +86,9 @@ struct ThreadData
DetailsColumn,
CoreColumn,
ComboNameColumn,
ColumnCount = CoreColumn
ColumnCount = CoreColumn,
IdRole = Qt::UserRole
};
void notifyRunning() // Clear state information.
......@@ -72,22 +99,25 @@ struct ThreadData
frameLevel = -1;
state.clear();
lineNumber = -1;
stopped = false;
}
// Permanent data.
quint64 id;
ThreadId id;
QByteArray groupId;
QString targetId;
QString core;
bool stopped;
// State information when stopped.
qint32 frameLevel;
qint32 lineNumber;
quint64 address;
QString function;
QString module;
QString fileName;
QString details;
QString state;
qint32 lineNumber;
QString name;
};
......
This diff is collapsed.
......@@ -57,26 +57,35 @@ class ThreadsHandler : public QAbstractTableModel
public:
ThreadsHandler();
int currentThread() const { return m_currentIndex; }
void setCurrentThread(int index);
int currentThreadId() const;
void setCurrentThreadId(int id);
int indexOf(quint64 threadId) const;
int currentThreadIndex() const { return m_currentIndex; }
ThreadId currentThread() const;
ThreadId threadAt(int index) const;
void setCurrentThread(ThreadId id);
void updateThread(const ThreadData &thread);
void updateThreads(const GdbMi &data);
void removeThread(ThreadId threadId);
void setThreads(const Threads &threads);
void removeAll();
Threads threads() const;
ThreadData thread(ThreadId id) const;
QAbstractItemModel *model();
// Clear out all frame information
void notifyRunning();
void notifyRunning(const QByteArray &data);
void notifyRunning(ThreadId id);
void notifyAllRunning();
static Threads parseGdbmiThreads(const GdbMi &data, int *currentThread = 0);
void notifyStopped(const QByteArray &data);
void notifyStopped(ThreadId id);
void notifyAllStopped();
void resetLocation();
void scheduleResetLocation();
private:
int indexOf(ThreadId threadId) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;