From 9d20a2abb45af2b1bd038acb191ece351d958d74 Mon Sep 17 00:00:00 2001
From: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
Date: Fri, 30 Oct 2009 17:40:29 +0100
Subject: [PATCH] make attaching adapters more robust

to make the behavior consistent across gdb versions, ignore the initial
*stopped which gdb7 delivers while attaching.

Reviewed-By: hjk
---
 src/plugins/debugger/gdb/attachgdbadapter.cpp | 12 ++----------
 src/plugins/debugger/gdb/gdbengine.cpp        | 15 ++++++++-------
 src/plugins/debugger/gdb/remotegdbadapter.cpp |  3 ++-
 src/plugins/debugger/gdb/termgdbadapter.cpp   |  3 ++-
 src/plugins/debugger/gdb/trkgdbadapter.cpp    |  1 +
 5 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/src/plugins/debugger/gdb/attachgdbadapter.cpp b/src/plugins/debugger/gdb/attachgdbadapter.cpp
index 4c0968233f2..0f6b50b6bd7 100644
--- a/src/plugins/debugger/gdb/attachgdbadapter.cpp
+++ b/src/plugins/debugger/gdb/attachgdbadapter.cpp
@@ -77,17 +77,9 @@ void AttachGdbAdapter::startInferior()
 
 void AttachGdbAdapter::handleAttach(const GdbResponse &response)
 {
+    QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
     if (response.resultClass == GdbResultDone) {
-        // We don't know the exact 6.8.50 build where gdb started emitting
-        // *stopped here, so allow for some slack.
-        if (m_engine->m_gdbVersion < 60850) {
-            QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
-            setState(InferiorStopped);
-        } else if (m_engine->m_gdbVersion < 70000 && state() == InferiorStarting) {
-            setState(InferiorStopped);
-        } else {
-            QTC_ASSERT(state() == InferiorStopped, qDebug() << state());
-        }
+        setState(InferiorStopped);
         debugMessage(_("INFERIOR ATTACHED"));
         showStatusMessage(msgAttachedToStoppedInferior());
         emit inferiorPrepared();
diff --git a/src/plugins/debugger/gdb/gdbengine.cpp b/src/plugins/debugger/gdb/gdbengine.cpp
index f6ab953e5d4..133b20fe2b9 100644
--- a/src/plugins/debugger/gdb/gdbengine.cpp
+++ b/src/plugins/debugger/gdb/gdbengine.cpp
@@ -889,6 +889,7 @@ void GdbEngine::updateAll()
 {
     QTC_ASSERT(state() == InferiorUnrunnable || state() == InferiorStopped, /**/);
     tryLoadDebuggingHelpers();
+    reloadModulesInternal();
     postCommand(_("-stack-list-frames"), WatchUpdate, CB(handleStackListFrames),
         QVariant::fromValue<StackCookie>(StackCookie(false, true)));
     manager()->stackHandler()->setCurrentIndex(0);
@@ -1009,6 +1010,11 @@ void GdbEngine::handleAqcuiredInferior()
 
 void GdbEngine::handleStopResponse(const GdbMi &data)
 {
+    // This is gdb 7+'s initial *stopped in response to attach.
+    // For consistency, we just discard it.
+    if (state() == InferiorStarting)
+        return;
+
     const QByteArray reason = data.findChild("reason").data();
 
     if (isExitedReason(reason)) {
@@ -1051,16 +1057,12 @@ void GdbEngine::handleStopResponse(const GdbMi &data)
         return;
     }
 
-    bool initHelpers = true;
     if (state() == InferiorRunning) {
         // Stop triggered by a breakpoint or otherwise not directly
         // initiated by the user.
         setState(InferiorStopping);
     } else {
-        if (state() == InferiorStarting)
-            initHelpers = false;
-        else
-            QTC_ASSERT(state() == InferiorStopping, qDebug() << state());
+        QTC_ASSERT(state() == InferiorStopping, qDebug() << state());
     }
     setState(InferiorStopped);
 
@@ -1138,8 +1140,7 @@ void GdbEngine::handleStopResponse(const GdbMi &data)
         }
     }
 
-    if (initHelpers && m_debuggingHelperState != DebuggingHelperUninitialized)
-        initHelpers = false;
+    bool initHelpers = (m_debuggingHelperState == DebuggingHelperUninitialized);
     // Don't load helpers on stops triggered by signals unless it's
     // an intentional trap.
     if (initHelpers && reason == "signal-received"
diff --git a/src/plugins/debugger/gdb/remotegdbadapter.cpp b/src/plugins/debugger/gdb/remotegdbadapter.cpp
index 006f314584a..d49a03aaced 100644
--- a/src/plugins/debugger/gdb/remotegdbadapter.cpp
+++ b/src/plugins/debugger/gdb/remotegdbadapter.cpp
@@ -191,8 +191,9 @@ void RemoteGdbAdapter::handleFileExecAndSymbols(const GdbResponse &response)
 
 void RemoteGdbAdapter::handleTargetRemote(const GdbResponse &record)
 {
+    QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
     if (record.resultClass == GdbResultDone) {
-        QTC_ASSERT(state() == InferiorStopped, qDebug() << state());
+        setState(InferiorStopped);
         // gdb server will stop the remote application itself.
         debugMessage(_("INFERIOR STARTED"));
         showStatusMessage(msgAttachedToStoppedInferior());
diff --git a/src/plugins/debugger/gdb/termgdbadapter.cpp b/src/plugins/debugger/gdb/termgdbadapter.cpp
index fbdc6a6dd84..e5f43eef211 100644
--- a/src/plugins/debugger/gdb/termgdbadapter.cpp
+++ b/src/plugins/debugger/gdb/termgdbadapter.cpp
@@ -114,8 +114,9 @@ void TermGdbAdapter::startInferior()
 
 void TermGdbAdapter::handleStubAttached(const GdbResponse &response)
 {
+    QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
     if (response.resultClass == GdbResultDone) {
-        QTC_ASSERT(state() == InferiorStopped, qDebug() << state());
+        setState(InferiorStopped);
         debugMessage(_("INFERIOR ATTACHED"));
         emit inferiorPrepared();
     } else if (response.resultClass == GdbResultError) {
diff --git a/src/plugins/debugger/gdb/trkgdbadapter.cpp b/src/plugins/debugger/gdb/trkgdbadapter.cpp
index 82ce54376bd..dcbe2851eea 100644
--- a/src/plugins/debugger/gdb/trkgdbadapter.cpp
+++ b/src/plugins/debugger/gdb/trkgdbadapter.cpp
@@ -1616,6 +1616,7 @@ void TrkGdbAdapter::handleTargetRemote(const GdbResponse &record)
 {
     QTC_ASSERT(state() == InferiorStarting, qDebug() << state());
     if (record.resultClass == GdbResultDone) {
+        setState(InferiorStopped);
         emit inferiorPrepared();
     } else {
         QString msg = tr("Connecting to TRK server adapter failed:\n")
-- 
GitLab