From bb35660c40f576274d28df516c346ba2f2fc6af8 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Date: Mon, 15 Mar 2010 12:07:47 +0100
Subject: [PATCH] Debugger[CDB]: Inconsistent data shown when switching frames.

IDebugSymbolGroup apparently needs to have IDebugSymbols set to
the current scope.
Task-number: QTCREATORBUG-861
---
 .../debugger/cdb/stacktracecontext.cpp        | 32 +++++++++++++++----
 src/plugins/debugger/cdb/stacktracecontext.h  |  2 ++
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/plugins/debugger/cdb/stacktracecontext.cpp b/src/plugins/debugger/cdb/stacktracecontext.cpp
index 6352664ac58..8a6b753ecca 100644
--- a/src/plugins/debugger/cdb/stacktracecontext.cpp
+++ b/src/plugins/debugger/cdb/stacktracecontext.cpp
@@ -97,7 +97,8 @@ StackTraceContext::SpecialFunction StackTraceContext::specialFunction(const QStr
 
 StackTraceContext::StackTraceContext(const ComInterfaces *cif) :
         m_cif(cif),
-        m_instructionOffset(0)
+        m_instructionOffset(0),
+        m_lastIndex(-1)
 {
 }
 
@@ -212,8 +213,15 @@ SymbolGroupContext *StackTraceContext::symbolGroupContextAt(int index, QString *
                         arg(QLatin1String(Q_FUNC_INFO)).arg(index).arg(m_frameContexts.size());
         return 0;
     }
-    if (m_frameContexts.at(index))
+    if (m_frameContexts.at(index)) {
+        // Symbol group only functions correctly if IDebugSymbols has the right scope.
+        if (m_lastIndex != index) {
+            if (!setScope(index, errorMessage))
+                return 0;
+            m_lastIndex = index;
+        }
         return m_frameContexts.at(index);
+    }
     CIDebugSymbolGroup *comSymbolGroup  = createCOM_SymbolGroup(index, errorMessage);
     if (!comSymbolGroup) {
         *errorMessage = msgFrameContextFailed(index, m_frames.at(index), *errorMessage);
@@ -229,6 +237,19 @@ SymbolGroupContext *StackTraceContext::symbolGroupContextAt(int index, QString *
     return sc;
 }
 
+bool StackTraceContext::setScope(int index, QString *errorMessage)
+{
+    if (debug)
+        qDebug() << "setScope" << index;
+    const HRESULT hr = m_cif->debugSymbols->SetScope(0, m_cdbFrames + index, NULL, 0);
+    if (FAILED(hr)) {
+        *errorMessage = QString::fromLatin1("Cannot set scope %1: %2").
+                        arg(index).arg(CdbCore::msgComFailed("SetScope", hr));
+        return false;
+    }
+    return true;
+}
+
 CIDebugSymbolGroup *StackTraceContext::createCOM_SymbolGroup(int index, QString *errorMessage)
 {
     CIDebugSymbolGroup *sg = 0;
@@ -237,10 +258,8 @@ CIDebugSymbolGroup *StackTraceContext::createCOM_SymbolGroup(int index, QString
         *errorMessage = CdbCore::msgComFailed("GetScopeSymbolGroup", hr);
         return 0;
     }
-
-    hr = m_cif->debugSymbols->SetScope(0, m_cdbFrames + index, NULL, 0);
-    if (FAILED(hr)) {
-        *errorMessage = CdbCore::msgComFailed("SetScope", hr);
+    // Set debugSymbols's scope.
+    if (!setScope(index, errorMessage)) {
         sg->Release();
         return 0;
     }
@@ -251,6 +270,7 @@ CIDebugSymbolGroup *StackTraceContext::createCOM_SymbolGroup(int index, QString
         sg->Release();
         return 0;
     }
+    m_lastIndex = index;
     return sg;
 }
 
diff --git a/src/plugins/debugger/cdb/stacktracecontext.h b/src/plugins/debugger/cdb/stacktracecontext.h
index 989265a8a31..350c31b8496 100644
--- a/src/plugins/debugger/cdb/stacktracecontext.h
+++ b/src/plugins/debugger/cdb/stacktracecontext.h
@@ -142,6 +142,7 @@ protected:
     static QString msgFrameContextFailed(int index, const StackFrame &f, const QString &why);
 
 private:    
+    bool setScope(int index, QString *errorMessage);
     CIDebugSymbolGroup *createCOM_SymbolGroup(int index, QString *errorMessage);
     inline static StackFrame frameFromFRAME(const CdbCore::ComInterfaces &cif,
                                             const DEBUG_STACK_FRAME &s);
@@ -153,6 +154,7 @@ private:
     QVector <SymbolGroupContext*> m_frameContexts;
     QVector<StackFrame> m_frames;
     ULONG64 m_instructionOffset;
+    int m_lastIndex;
 };
 
 QDebug operator<<(QDebug d, const StackTraceContext &);
-- 
GitLab