DAP: Refactor dap responses handling

Change-Id: Iebc16d7e497b4f3a1deec6f52f7b89815c2cde3b
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Artem Sokolovskii
2023-08-09 14:09:02 +02:00
parent 658db809ef
commit 8b1047aa8f
3 changed files with 231 additions and 198 deletions

View File

@@ -141,23 +141,10 @@ void DapEngine::shutdownEngine()
m_dataGenerator->kill(); m_dataGenerator->kill();
} }
// From the docs:
// The sequence of events/requests is as follows:
// * adapters sends initialized event (after the initialize request has returned)
// * client sends zero or more setBreakpoints requests
// * client sends one setFunctionBreakpoints request
// (if corresponding capability supportsFunctionBreakpoints is true)
// * client sends a setExceptionBreakpoints request if one or more exceptionBreakpointFilters
// have been defined (or if supportsConfigurationDoneRequest is not true)
// * client sends other future configuration requests
// * client sends one configurationDone request to indicate the end of the configuration.
void DapEngine::handleDapStarted() void DapEngine::handleDapStarted()
{ {
QTC_ASSERT(state() == EngineRunRequested, qCDebug(dapEngineLog) << state()); QTC_ASSERT(state() == EngineRunRequested, qCDebug(dapEngineLog) << state());
// CHECK_STATE(EngineRunRequested);
postDirectCommand({ postDirectCommand({
{"command", "initialize"}, {"command", "initialize"},
{"type", "request"}, {"type", "request"},
@@ -174,8 +161,6 @@ void DapEngine::handleDapConfigurationDone()
{ {
QTC_ASSERT(state() == EngineRunRequested, qCDebug(dapEngineLog) << state()); QTC_ASSERT(state() == EngineRunRequested, qCDebug(dapEngineLog) << state());
// CHECK_STATE(EngineRunRequested);
postDirectCommand({ postDirectCommand({
{"command", "configurationDone"}, {"command", "configurationDone"},
{"type", "request"} {"type", "request"}
@@ -402,7 +387,7 @@ void DapEngine::insertBreakpoint(const Breakpoint &bp)
}} }}
}); });
notifyBreakpointChangeOk(bp); notifyBreakpointInsertOk(bp);
qCDebug(dapEngineLog) << "insertBreakpoint" << bp->modelId() << bp->responseId(); qCDebug(dapEngineLog) << "insertBreakpoint" << bp->modelId() << bp->responseId();
} }
@@ -662,10 +647,26 @@ void DapEngine::handleOutput(const QJsonDocument &data)
const QJsonValue t = ob.value("type"); const QJsonValue t = ob.value("type");
const QString type = t.toString(); const QString type = t.toString();
qCDebug(dapEngineLog) << "response" << ob;
qCDebug(dapEngineLog) << "dap response" << ob;
if (type == "response") { if (type == "response") {
const QString command = ob.value("command").toString(); handleResponse(ob);
return;
}
if (type == "event") {
handleEvent(ob);
return;
}
showMessage("UNKNOWN TYPE:" + type);
}
void DapEngine::handleResponse(const QJsonObject &response)
{
const QString command = response.value("command").toString();
if (command == "configurationDone") { if (command == "configurationDone") {
showMessage("configurationDone", LogDebug); showMessage("configurationDone", LogDebug);
qCDebug(dapEngineLog) << "configurationDone success"; qCDebug(dapEngineLog) << "configurationDone success";
@@ -681,7 +682,42 @@ void DapEngine::handleOutput(const QJsonDocument &data)
} }
if (command == "stackTrace") { if (command == "stackTrace") {
QJsonArray stackFrames = ob.value("body").toObject().value("stackFrames").toArray(); handleStackTraceResponse(response);
return;
}
if (command == "scopes") {
handleScopesResponse(response);
return;
}
if (command == "variables") {
auto variables = response.value("body").toObject().value("variables").toArray();
refreshLocals(variables);
return;
}
if (command == "stepIn" || command == "stepOut" || command == "next") {
if (response.value("success").toBool()) {
showMessage(command, LogDebug);
notifyInferiorRunOk();
} else {
notifyInferiorRunFailed();
}
return;
}
if (command == "threads") {
handleThreadsResponse(response);
return;
}
showMessage("UNKNOWN RESPONSE:" + command);
}
void DapEngine::handleStackTraceResponse(const QJsonObject &response)
{
QJsonArray stackFrames = response.value("body").toObject().value("stackFrames").toArray();
if (stackFrames.isEmpty()) if (stackFrames.isEmpty())
return; return;
@@ -694,13 +730,15 @@ void DapEngine::handleOutput(const QJsonDocument &data)
refreshStack(stackFrames); refreshStack(stackFrames);
dapScopes(stackFrame.value("id").toInt()); dapScopes(stackFrame.value("id").toInt());
return;
} }
if (command == "scopes") { void DapEngine::handleScopesResponse(const QJsonObject &response)
if (ob.value("success").toBool()) { {
auto scopes = ob.value("body").toObject().value("scopes").toArray(); if (!response.value("success").toBool())
for (auto scope : scopes) { return;
QJsonArray scopes = response.value("body").toObject().value("scopes").toArray();
for (const QJsonValueRef &scope : scopes) {
const QString name = scope.toObject().value("name").toString(); const QString name = scope.toObject().value("name").toString();
const int variablesReference = scope.toObject().value("variablesReference").toInt(); const int variablesReference = scope.toObject().value("variablesReference").toInt();
qCDebug(dapEngineLog) << "scoped success" << name << variablesReference; qCDebug(dapEngineLog) << "scoped success" << name << variablesReference;
@@ -713,31 +751,16 @@ void DapEngine::handleOutput(const QJsonDocument &data)
} }
} }
} }
}
if (command == "variables") { void DapEngine::handleThreadsResponse(const QJsonObject &response)
auto variables = ob.value("body").toObject().value("variables").toArray(); {
refreshLocals(variables); QJsonArray threads = response.value("body").toObject().value("threads").toArray();
}
if (command == "stepIn" || command == "stepOut" || command == "next") {
if (ob.value("success").toBool()) {
showMessage(command, LogDebug);
notifyInferiorRunOk();
} else {
notifyInferiorRunFailed();
}
return;
}
if (command == "threads") {
QJsonArray threads = ob.value("body").toObject().value("threads").toArray();
if (threads.isEmpty()) if (threads.isEmpty())
return; return;
ThreadsHandler *handler = threadsHandler(); ThreadsHandler *handler = threadsHandler();
for (auto thread : threads) { for (const QJsonValueRef &thread : threads) {
ThreadData threadData; ThreadData threadData;
threadData.id = QString::number(thread.toObject().value("id").toInt()); threadData.id = QString::number(thread.toObject().value("id").toInt());
threadData.name = thread.toObject().value("name").toString(); threadData.name = thread.toObject().value("name").toString();
@@ -745,23 +768,21 @@ void DapEngine::handleOutput(const QJsonDocument &data)
} }
if (m_currentThreadId) if (m_currentThreadId)
handler->setCurrentThread( handler->setCurrentThread(threadsHandler()->threadForId(QString::number(m_currentThreadId)));
threadsHandler()->threadForId(QString::number(m_currentThreadId)));
return;
}
} }
if (type == "event") { void DapEngine::handleEvent(const QJsonObject &event)
const QString event = ob.value("event").toString(); {
const QJsonObject body = ob.value("body").toObject(); const QString eventType = event.value("event").toString();
const QJsonObject body = event.value("body").toObject();
showMessage(eventType, LogDebug);
if (event == "exited") { if (eventType == "exited") {
notifyInferiorExited(); notifyInferiorExited();
showMessage("exited", LogDebug);
return; return;
} }
if (event == "output") { if (eventType == "output") {
const QString category = body.value("category").toString(); const QString category = body.value("category").toString();
const QString output = body.value("output").toString(); const QString output = body.value("output").toString();
if (category == "stdout") if (category == "stdout")
@@ -772,22 +793,41 @@ void DapEngine::handleOutput(const QJsonDocument &data)
showMessage(output, LogDebug); showMessage(output, LogDebug);
return; return;
} }
qCDebug(dapEngineLog) << data;
if (event == "initialized") { if (eventType == "initialized") {
showMessage(event, LogDebug);
qCDebug(dapEngineLog) << "initialize success"; qCDebug(dapEngineLog) << "initialize success";
handleDapLaunch(); handleDapLaunch();
handleDapConfigurationDone(); handleDapConfigurationDone();
return; return;
} }
if (event == "stopped") { if (eventType == "stopped") {
handleStoppedEvent(event);
return;
}
if (eventType == "thread") {
threads();
if (body.value("reason").toString() == "started" && body.value("threadId").toInt() == 1)
claimInitialBreakpoints();
return;
}
if (eventType == "breakpoint") {
handleBreakpointEvent(event);
return;
}
showMessage("UNKNOWN EVENT:" + eventType);
}
void DapEngine::handleStoppedEvent(const QJsonObject &event)
{
const QJsonObject body = event.value("body").toObject();
m_currentThreadId = body.value("threadId").toInt(); m_currentThreadId = body.value("threadId").toInt();
showMessage(event, LogDebug);
if (body.value("reason").toString() == "breakpoint") { if (body.value("reason").toString() == "breakpoint") {
QString id = QString::number( QString id = QString::number(body.value("hitBreakpointIds").toArray().first().toInteger());
body.value("hitBreakpointIds").toArray().first().toInteger());
Breakpoint bp = breakHandler()->findBreakpointByResponseId(id); Breakpoint bp = breakHandler()->findBreakpointByResponseId(id);
if (bp) { if (bp) {
@@ -803,21 +843,13 @@ void DapEngine::handleOutput(const QJsonDocument &data)
dapStackTrace(); dapStackTrace();
threads(); threads();
return;
} }
if (event == "thread") { void DapEngine::handleBreakpointEvent(const QJsonObject &event)
// threads(); // breaks cmake debugging for now {
const QJsonObject body = event.value("body").toObject();
showMessage(event, LogDebug);
if (body.value("reason").toString() == "started" && body.value("threadId").toInt() == 1)
claimInitialBreakpoints();
return;
}
if (event == "breakpoint") {
showMessage(event, LogDebug);
QJsonObject breakpoint = body.value("breakpoint").toObject(); QJsonObject breakpoint = body.value("breakpoint").toObject();
Breakpoint bp = breakHandler()->findBreakpointByResponseId( Breakpoint bp = breakHandler()->findBreakpointByResponseId(
QString::number(breakpoint.value("id").toInt())); QString::number(breakpoint.value("id").toInt()));
qCDebug(dapEngineLog) << "breakpoint id :" << breakpoint.value("id").toInt(); qCDebug(dapEngineLog) << "breakpoint id :" << breakpoint.value("id").toInt();
@@ -843,14 +875,6 @@ void DapEngine::handleOutput(const QJsonDocument &data)
} }
return; return;
} }
return;
}
showMessage("UNKNOWN EVENT:" + event);
return;
}
showMessage("UNKNOWN TYPE:" + type);
} }
void DapEngine::refreshLocals(const QJsonArray &variables) void DapEngine::refreshLocals(const QJsonArray &variables)

View File

@@ -117,8 +117,18 @@ protected:
void handleDapDone(); void handleDapDone();
void readDapStandardOutput(); void readDapStandardOutput();
void readDapStandardError(); void readDapStandardError();
void handleOutput(const QJsonDocument &data); void handleOutput(const QJsonDocument &data);
void handleResponse(const QString &ba);
void handleResponse(const QJsonObject &response);
void handleStackTraceResponse(const QJsonObject &response);
void handleScopesResponse(const QJsonObject &response);
void handleThreadsResponse(const QJsonObject &response);
void handleEvent(const QJsonObject &event);
void handleBreakpointEvent(const QJsonObject &event);
void handleStoppedEvent(const QJsonObject &event);
void updateAll() override; void updateAll() override;
void updateLocals() override; void updateLocals() override;
void connectDataGeneratorSignals(); void connectDataGeneratorSignals();

View File

@@ -1179,7 +1179,6 @@ DebuggerPluginPrivate::DebuggerPluginPrivate(const QStringList &arguments)
m_startCmakeAction.setText(Tr::tr("Start CMake Debugging")); m_startCmakeAction.setText(Tr::tr("Start CMake Debugging"));
m_startCmakeAction.setEnabled(true); m_startCmakeAction.setEnabled(true);
m_startCmakeAction.setIcon(startIcon(true)); m_startCmakeAction.setIcon(startIcon(true));
m_startCmakeAction.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
m_startCmakeAction.setVisible(true); m_startCmakeAction.setVisible(true);
m_perspectiveCmake->addToolBarAction(&m_startCmakeAction); m_perspectiveCmake->addToolBarAction(&m_startCmakeAction);