From a838edd4aa4dc6b1514afc66161210b9f48c1af7 Mon Sep 17 00:00:00 2001 From: Igor Kulaychuk Date: Wed, 10 Jan 2018 22:59:21 +0300 Subject: [PATCH] Refactor printing threads state --- src/debug/netcoredbg/commands.cpp | 23 +++++++++++++++++++- src/debug/netcoredbg/debugger.h | 3 +++ src/debug/netcoredbg/frames.cpp | 46 ++++++++++----------------------------- src/debug/netcoredbg/frames.h | 2 +- src/debug/netcoredbg/protocol.h | 12 ++++++++++ 5 files changed, 49 insertions(+), 37 deletions(-) create mode 100644 src/debug/netcoredbg/protocol.h diff --git a/src/debug/netcoredbg/commands.cpp b/src/debug/netcoredbg/commands.cpp index fd041f6..f1f70ba 100644 --- a/src/debug/netcoredbg/commands.cpp +++ b/src/debug/netcoredbg/commands.cpp @@ -205,7 +205,28 @@ HRESULT Debugger::StepCommand(ICorDebugProcess *pProcess, static HRESULT ThreadInfoCommand(ICorDebugProcess *pProcess, const std::vector &, std::string &output) { if (!pProcess) return E_FAIL; - return PrintThreadsState(pProcess, output); + + HRESULT Status = S_OK; + + std::vector 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, diff --git a/src/debug/netcoredbg/debugger.h b/src/debug/netcoredbg/debugger.h index 582fc40..67fef31 100644 --- a/src/debug/netcoredbg/debugger.h +++ b/src/debug/netcoredbg/debugger.h @@ -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 diff --git a/src/debug/netcoredbg/frames.cpp b/src/debug/netcoredbg/frames.cpp index 268fcb3..75be127 100644 --- a/src/debug/netcoredbg/frames.cpp +++ b/src/debug/netcoredbg/frames.cpp @@ -20,56 +20,32 @@ #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 pProcess; - IfFailRet(pThread->GetProcess(&pProcess)); - BOOL running = FALSE; - IfFailRet(pProcess->IsRunning(&running)); - - ss << "{id=\"" << threadId - << "\",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 &threads) { HRESULT Status = S_OK; ToRelease 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 pThread(handle); - std::string threadOutput; - PrintThread(pThread, threadOutput); + DWORD threadId = 0; + IfFailRet(pThread->GetID(&threadId)); - ss << sep << threadOutput; - sep = ","; + ToRelease pProcess; + IfFailRet(pThread->GetProcess(&pProcess)); + BOOL running = FALSE; + IfFailRet(pProcess->IsRunning(&running)); + + threads.emplace_back(threadId, "", running); + std::string threadOutput; } - ss << "]"; - output = ss.str(); return S_OK; } diff --git a/src/debug/netcoredbg/frames.h b/src/debug/netcoredbg/frames.h index db5c118..ab248a1 100644 --- a/src/debug/netcoredbg/frames.h +++ b/src/debug/netcoredbg/frames.h @@ -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 &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 index 0000000..447ae5d --- /dev/null +++ b/src/debug/netcoredbg/protocol.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +struct Thread +{ + int id; + std::string name; + bool running; + + Thread(int id, std::string name, bool running) : id(id), name(name), running(running) {} +}; -- 2.7.4