Refactor printing threads state
authorIgor Kulaychuk <i.kulaychuk@samsung.com>
Wed, 10 Jan 2018 19:59:21 +0000 (22:59 +0300)
committerIgor Kulaychuk <i.kulaychuk@samsung.com>
Wed, 10 Jan 2018 20:14:09 +0000 (23:14 +0300)
src/debug/netcoredbg/commands.cpp
src/debug/netcoredbg/debugger.h
src/debug/netcoredbg/frames.cpp
src/debug/netcoredbg/frames.h
src/debug/netcoredbg/protocol.h [new file with mode: 0644]

index fd041f67cb311f40134aa24a03cccd21e50ad102..f1f70bad20eaca65b23e94b4a1eeee1185f86c2c 100644 (file)
@@ -205,7 +205,28 @@ HRESULT Debugger::StepCommand(ICorDebugProcess *pProcess,
 static HRESULT ThreadInfoCommand(ICorDebugProcess *pProcess, const std::vector<std::string> &, std::string &output)
 {
     if (!pProcess) return E_FAIL;
-    return PrintThreadsState(pProcess, output);
+
+    HRESULT Status = S_OK;
+
+    std::vector<Thread> threads;
+    IfFailRet(GetThreadsState(pProcess, threads));
+
+    std::stringstream ss;
+
+    ss << "threads=[";
+
+    const char *sep = "";
+    for (const Thread& thread : threads)
+    {
+        ss << "{id=\"" << thread.id
+           << "\",name=\"" << Debugger::EscapeMIValue(thread.name) << "\",state=\""
+           << (thread.running ? "running" : "stopped") << "\"}" << sep;
+        sep = ",";
+    }
+
+    ss << "]";
+    output = ss.str();
+    return S_OK;
 }
 
 HRESULT Debugger::HandleCommand(std::string command,
index 582fc4085127c7bc2ff693695b7cc87a601b9bc8..67fef3176d14bf3b16ff8663937f0ef1f495e7b8 100644 (file)
@@ -2,6 +2,9 @@
 // Distributed under the MIT License.
 // See the LICENSE file in the project root for more information.
 
+#include "protocol.h"
+
+
 class ManagedCallback;
 
 class Debugger
index 268fcb3b267d10bc48424d9dc0da6d11b1fc99c4..75be127ebd83ea85fe6908664dc111883ec8a4eb 100644 (file)
 #include "frames.h"
 
 
-HRESULT PrintThread(ICorDebugThread *pThread, std::string &output)
-{
-    HRESULT Status = S_OK;
-
-    std::stringstream ss;
-
-    DWORD threadId = 0;
-    IfFailRet(pThread->GetID(&threadId));
-
-    ToRelease<ICorDebugProcess> pProcess;
-    IfFailRet(pThread->GetProcess(&pProcess));
-    BOOL running = FALSE;
-    IfFailRet(pProcess->IsRunning(&running));
-
-    ss << "{id=\"" << threadId
-       << "\",name=\"<No name>\",state=\""
-       << (running ? "running" : "stopped") << "\"}";
-
-    output = ss.str();
-
-    return S_OK;
-}
-
-HRESULT PrintThreadsState(ICorDebugController *controller, std::string &output)
+static HRESULT GetThreadsState(ICorDebugController *controller, std::vector<Thread> &threads)
 {
     HRESULT Status = S_OK;
 
     ToRelease<ICorDebugThreadEnum> pThreads;
     IfFailRet(controller->EnumerateThreads(&pThreads));
 
-    std::stringstream ss;
-
-    ss << "threads=[";
-
     ICorDebugThread *handle;
     ULONG fetched;
-    const char *sep = "";
+
     while (SUCCEEDED(Status = pThreads->Next(1, &handle, &fetched)) && fetched == 1)
     {
         ToRelease<ICorDebugThread> pThread(handle);
 
-        std::string threadOutput;
-        PrintThread(pThread, threadOutput);
+        DWORD threadId = 0;
+        IfFailRet(pThread->GetID(&threadId));
 
-        ss << sep << threadOutput;
-        sep = ",";
+        ToRelease<ICorDebugProcess> pProcess;
+        IfFailRet(pThread->GetProcess(&pProcess));
+        BOOL running = FALSE;
+        IfFailRet(pProcess->IsRunning(&running));
+
+        threads.emplace_back(threadId, "<No name>", running);
+        std::string threadOutput;
     }
 
-    ss << "]";
-    output = ss.str();
     return S_OK;
 }
 
index db5c11841098a87daced81d9eb7c1972dc72e795..ab248a121c58da8a649dbd554e969aa29658a564 100644 (file)
@@ -5,5 +5,5 @@
 HRESULT PrintFrameLocation(ICorDebugFrame *pFrame, std::string &output);
 HRESULT GetFrameAt(ICorDebugThread *pThread, int level, ICorDebugFrame **ppFrame);
 
-HRESULT PrintThreadsState(ICorDebugController *controller, std::string &output);
+HRESULT GetThreadsState(ICorDebugController *controller, std::vector<Thread> &threads);
 HRESULT PrintFrames(ICorDebugThread *pThread, std::string &output, int lowFrame = 0, int highFrame = INT_MAX);
diff --git a/src/debug/netcoredbg/protocol.h b/src/debug/netcoredbg/protocol.h
new file mode 100644 (file)
index 0000000..447ae5d
--- /dev/null
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <string>
+
+struct Thread
+{
+    int id;
+    std::string name;
+    bool running;
+
+    Thread(int id, std::string name, bool running) : id(id), name(name), running(running) {}
+};