Refactor variables handler into Variables class
authorIgor Kulaychuk <i.kulaychuk@samsung.com>
Thu, 18 Jan 2018 19:17:11 +0000 (22:17 +0300)
committerIgor Kulaychuk <i.kulaychuk@samsung.com>
Thu, 18 Jan 2018 19:17:11 +0000 (22:17 +0300)
src/debug/netcoredbg/debugger.h
src/debug/netcoredbg/main.cpp
src/debug/netcoredbg/variables.cpp

index 18985d120d126a28ec2407ff47661ad0371e6076..0e69ae7b42f9a5e59ef8e661d779d0b3fb455826 100644 (file)
@@ -13,8 +13,6 @@
 class ManagedCallback;
 class Protocol;
 
-struct Member;
-
 enum ValueKind
 {
     ValueIsScope,
@@ -207,55 +205,8 @@ public:
         std::vector<Breakpoint> &breakpoints);
 };
 
-class Debugger
+class Variables
 {
-public:
-    enum StepType {
-        STEP_IN = 0,
-        STEP_OVER,
-        STEP_OUT
-    };
-
-private:
-    friend class ManagedCallback;
-    enum ProcessAttachedState
-    {
-        ProcessAttached,
-        ProcessUnattached
-    };
-    std::mutex m_processAttachedMutex;
-    std::condition_variable m_processAttachedCV;
-    ProcessAttachedState m_processAttachedState;
-
-    void NotifyProcessCreated();
-    void NotifyProcessExited();
-    void WaitProcessExited();
-    HRESULT CheckNoProcess();
-
-    std::mutex m_lastStoppedThreadIdMutex;
-    int m_lastStoppedThreadId;
-
-    void SetLastStoppedThread(ICorDebugThread *pThread);
-
-    Modules m_modules;
-    Evaluator m_evaluator;
-    Breakpoints m_breakpoints;
-    Protocol *m_protocol;
-    ToRelease<ManagedCallback> m_managedCallback;
-    ICorDebug *m_pDebug;
-    ICorDebugProcess *m_pProcess;
-
-    bool m_justMyCode;
-
-    std::mutex m_startupMutex;
-    std::condition_variable m_startupCV;
-    bool m_startupReady;
-    HRESULT m_startupResult;
-
-    PVOID m_unregisterToken;
-    DWORD m_processId;
-    std::string m_clrPath;
-
     struct VariableReference
     {
         uint32_t variablesReference; // key
@@ -290,25 +241,35 @@ private:
         bool IsScope() const { return valueKind == ValueIsScope; }
 
         VariableReference(VariableReference &&that) = default;
-    private:
         VariableReference(const VariableReference &that) = delete;
     };
+
+    Evaluator &m_evaluator;
+
     std::unordered_map<uint32_t, VariableReference> m_variables;
     uint32_t m_nextVariableReference;
 
     void AddVariableReference(Variable &variable, uint64_t frameId, ICorDebugValue *value, ValueKind valueKind);
 
-    static VOID StartupCallback(IUnknown *pCordb, PVOID parameter, HRESULT hr);
-    HRESULT Startup(IUnknown *punk, int pid);
-
-    void Cleanup();
+    HRESULT GetStackVariables(
+        uint64_t frameId,
+        ICorDebugThread *pThread,
+        ICorDebugFrame *pFrame,
+        int start,
+        int count,
+        std::vector<Variable> &variables);
 
-    static HRESULT DisableAllSteppers(ICorDebugProcess *pProcess);
+    HRESULT GetChildren(
+        VariableReference &ref,
+        ICorDebugThread *pThread,
+        ICorDebugFrame *pFrame,
+        int start,
+        int count,
+        std::vector<Variable> &variables);
 
-    HRESULT SetupStep(ICorDebugThread *pThread, StepType stepType);
+    struct Member;
 
-    HRESULT GetStackVariables(uint64_t frameId, ICorDebugThread *pThread, ICorDebugFrame *pFrame, int start, int count, std::vector<Variable> &variables);
-    HRESULT GetChildren(VariableReference &ref, ICorDebugThread *pThread, ICorDebugFrame *pFrame, int start, int count, std::vector<Variable> &variables);
+    static void FixupInheritedFieldNames(std::vector<Member> &members);
 
     HRESULT FetchFieldsAndProperties(
         ICorDebugValue *pInputValue,
@@ -325,6 +286,84 @@ private:
         unsigned int &numchild,
         bool static_members = false);
 
+public:
+
+    Variables(Evaluator &evaluator) : m_evaluator(evaluator), m_nextVariableReference(1) {}
+
+    int GetNamedVariables(uint32_t variablesReference);
+
+    HRESULT Variables::GetVariables(
+        ICorDebugProcess *pProcess,
+        uint32_t variablesReference,
+        VariablesFilter filter,
+        int start,
+        int count,
+        std::vector<Variable> &variables);
+
+    HRESULT GetScopes(ICorDebugProcess *pProcess, uint64_t frameId, std::vector<Scope> &scopes);
+
+    HRESULT Evaluate(ICorDebugProcess *pProcess, uint64_t frameId, const std::string &expression, Variable &variable);
+};
+
+class Debugger
+{
+public:
+    enum StepType {
+        STEP_IN = 0,
+        STEP_OVER,
+        STEP_OUT
+    };
+
+private:
+    friend class ManagedCallback;
+    enum ProcessAttachedState
+    {
+        ProcessAttached,
+        ProcessUnattached
+    };
+    std::mutex m_processAttachedMutex;
+    std::condition_variable m_processAttachedCV;
+    ProcessAttachedState m_processAttachedState;
+
+    void NotifyProcessCreated();
+    void NotifyProcessExited();
+    void WaitProcessExited();
+    HRESULT CheckNoProcess();
+
+    std::mutex m_lastStoppedThreadIdMutex;
+    int m_lastStoppedThreadId;
+
+    void SetLastStoppedThread(ICorDebugThread *pThread);
+
+    Modules m_modules;
+    Evaluator m_evaluator;
+    Breakpoints m_breakpoints;
+    Variables m_variables;
+    Protocol *m_protocol;
+    ToRelease<ManagedCallback> m_managedCallback;
+    ICorDebug *m_pDebug;
+    ICorDebugProcess *m_pProcess;
+
+    bool m_justMyCode;
+
+    std::mutex m_startupMutex;
+    std::condition_variable m_startupCV;
+    bool m_startupReady;
+    HRESULT m_startupResult;
+
+    PVOID m_unregisterToken;
+    DWORD m_processId;
+    std::string m_clrPath;
+
+    static VOID StartupCallback(IUnknown *pCordb, PVOID parameter, HRESULT hr);
+    HRESULT Startup(IUnknown *punk, int pid);
+
+    void Cleanup();
+
+    static HRESULT DisableAllSteppers(ICorDebugProcess *pProcess);
+
+    HRESULT SetupStep(ICorDebugThread *pThread, StepType stepType);
+
     HRESULT GetStackTrace(ICorDebugThread *pThread, int lowFrame, int highFrame, std::vector<StackFrame> &stackFrames);
     HRESULT GetFrameLocation(ICorDebugFrame *pFrame, int threadId, uint32_t level, StackFrame &stackFrame);
 public:
index 86e5af38fbbba0dbad8332404829e45630c8b750..500e5c756821755e32615b3788f78d35c8bf3ff1 100644 (file)
@@ -672,6 +672,7 @@ Debugger::Debugger() :
     m_processAttachedState(ProcessUnattached),
     m_evaluator(m_modules),
     m_breakpoints(m_modules),
+    m_variables(m_evaluator),
     m_managedCallback(new ManagedCallback(*this)),
     m_pDebug(nullptr),
     m_pProcess(nullptr),
@@ -679,8 +680,7 @@ Debugger::Debugger() :
     m_startupReady(false),
     m_startupResult(S_OK),
     m_unregisterToken(nullptr),
-    m_processId(0),
-    m_nextVariableReference(1)
+    m_processId(0)
 {
 }
 
index ec3600fe045c22b9c30e33becfd40f487e033e9c..703465f9db609abd1afb17fbe8051a5bac9df8db 100644 (file)
@@ -20,7 +20,7 @@
 #include "frames.h"
 
 
-HRESULT Debugger::GetNumChild(
+HRESULT Variables::GetNumChild(
     ICorDebugValue *pValue,
     unsigned int &numchild,
     bool static_members)
@@ -57,7 +57,7 @@ HRESULT Debugger::GetNumChild(
     return S_OK;
 }
 
-struct Member
+struct Variables::Member
 {
     std::string name;
     std::string ownerType;
@@ -68,11 +68,10 @@ struct Member
         value(std::move(value))
     {}
     Member(Member &&that) = default;
-private:
     Member(const Member &that) = delete;
 };
 
-HRESULT Debugger::FetchFieldsAndProperties(
+HRESULT Variables::FetchFieldsAndProperties(
     ICorDebugValue *pInputValue,
     ICorDebugThread *pThread,
     ICorDebugILFrame *pILFrame,
@@ -138,6 +137,11 @@ HRESULT Debugger::FetchFieldsAndProperties(
 }
 
 int Debugger::GetNamedVariables(uint32_t variablesReference)
+{
+    return m_variables.GetNamedVariables(variablesReference);
+}
+
+int Variables::GetNamedVariables(uint32_t variablesReference)
 {
     auto it = m_variables.find(variablesReference);
     if (it == m_variables.end())
@@ -145,8 +149,27 @@ int Debugger::GetNamedVariables(uint32_t variablesReference)
     return it->second.namedVariables;
 }
 
-HRESULT Debugger::GetVariables(uint32_t variablesReference, VariablesFilter filter, int start, int count, std::vector<Variable> &variables)
+HRESULT Debugger::GetVariables(
+    uint32_t variablesReference,
+    VariablesFilter filter,
+    int start,
+    int count,
+    std::vector<Variable> &variables)
+{
+    return m_variables.GetVariables(m_pProcess, variablesReference, filter, start, count, variables);
+}
+
+HRESULT Variables::GetVariables(
+    ICorDebugProcess *pProcess,
+    uint32_t variablesReference,
+    VariablesFilter filter,
+    int start,
+    int count,
+    std::vector<Variable> &variables)
 {
+    if (pProcess == nullptr)
+        return E_FAIL;
+
     auto it = m_variables.find(variablesReference);
     if (it == m_variables.end())
         return E_FAIL;
@@ -157,7 +180,7 @@ HRESULT Debugger::GetVariables(uint32_t variablesReference, VariablesFilter filt
 
     StackFrame stackFrame(ref.frameId);
     ToRelease<ICorDebugThread> pThread;
-    IfFailRet(m_pProcess->GetThread(stackFrame.GetThreadId(), &pThread));
+    IfFailRet(pProcess->GetThread(stackFrame.GetThreadId(), &pThread));
     ToRelease<ICorDebugFrame> pFrame;
     IfFailRet(GetFrameAt(pThread, stackFrame.GetLevel(), &pFrame));
 
@@ -176,7 +199,7 @@ HRESULT Debugger::GetVariables(uint32_t variablesReference, VariablesFilter filt
     return S_OK;
 }
 
-void Debugger::AddVariableReference(Variable &variable, uint64_t frameId, ICorDebugValue *value, ValueKind valueKind)
+void Variables::AddVariableReference(Variable &variable, uint64_t frameId, ICorDebugValue *value, ValueKind valueKind)
 {
     HRESULT Status;
     unsigned int numChild = 0;
@@ -191,7 +214,13 @@ void Debugger::AddVariableReference(Variable &variable, uint64_t frameId, ICorDe
     m_variables.emplace(std::make_pair(variable.variablesReference, std::move(variableReference)));
 }
 
-HRESULT Debugger::GetStackVariables(uint64_t frameId, ICorDebugThread *pThread, ICorDebugFrame *pFrame, int start, int count, std::vector<Variable> &variables)
+HRESULT Variables::GetStackVariables(
+    uint64_t frameId,
+    ICorDebugThread *pThread,
+    ICorDebugFrame *pFrame,
+    int start,
+    int count,
+    std::vector<Variable> &variables)
 {
     HRESULT Status;
 
@@ -239,11 +268,19 @@ HRESULT Debugger::GetStackVariables(uint64_t frameId, ICorDebugThread *pThread,
 
 HRESULT Debugger::GetScopes(uint64_t frameId, std::vector<Scope> &scopes)
 {
+    return m_variables.GetScopes(m_pProcess, frameId, scopes);
+}
+
+HRESULT Variables::GetScopes(ICorDebugProcess *pProcess, uint64_t frameId, std::vector<Scope> &scopes)
+{
+    if (pProcess == nullptr)
+        return E_FAIL;
+
     HRESULT Status;
 
     StackFrame stackFrame(frameId);
     ToRelease<ICorDebugThread> pThread;
-    IfFailRet(m_pProcess->GetThread(stackFrame.GetThreadId(), &pThread));
+    IfFailRet(pProcess->GetThread(stackFrame.GetThreadId(), &pThread));
     ToRelease<ICorDebugFrame> pFrame;
     IfFailRet(GetFrameAt(pThread, stackFrame.GetLevel(), &pFrame));
 
@@ -275,7 +312,7 @@ HRESULT Debugger::GetScopes(uint64_t frameId, std::vector<Scope> &scopes)
     return S_OK;
 }
 
-static void FixupInheritedFieldNames(std::vector<Member> &members)
+void Variables::FixupInheritedFieldNames(std::vector<Member> &members)
 {
     std::unordered_set<std::string> names;
     for (auto &it : members)
@@ -288,12 +325,13 @@ static void FixupInheritedFieldNames(std::vector<Member> &members)
     }
 }
 
-HRESULT Debugger::GetChildren(VariableReference &ref,
-                              ICorDebugThread *pThread,
-                              ICorDebugFrame *pFrame,
-                              int start,
-                              int count,
-                              std::vector<Variable> &variables)
+HRESULT Variables::GetChildren(
+    VariableReference &ref,
+    ICorDebugThread *pThread,
+    ICorDebugFrame *pFrame,
+    int start,
+    int count,
+    std::vector<Variable> &variables)
 {
     if (ref.IsScope())
         return E_INVALIDARG;
@@ -356,11 +394,23 @@ HRESULT Debugger::GetChildren(VariableReference &ref,
 
 HRESULT Debugger::Evaluate(uint64_t frameId, const std::string &expression, Variable &variable)
 {
+    return m_variables.Evaluate(m_pProcess, frameId, expression, variable);
+}
+
+HRESULT Variables::Evaluate(
+    ICorDebugProcess *pProcess,
+    uint64_t frameId,
+    const std::string &expression,
+    Variable &variable)
+{
+    if (pProcess == nullptr)
+        return E_FAIL;
+
     HRESULT Status;
 
     StackFrame stackFrame(frameId);
     ToRelease<ICorDebugThread> pThread;
-    IfFailRet(m_pProcess->GetThread(stackFrame.GetThreadId(), &pThread));
+    IfFailRet(pProcess->GetThread(stackFrame.GetThreadId(), &pThread));
     ToRelease<ICorDebugFrame> pFrame;
     IfFailRet(GetFrameAt(pThread, stackFrame.GetLevel(), &pFrame));